Skip to content

Instantly share code, notes, and snippets.

View n8thangreen's full-sized avatar

Dr Nathan Green n8thangreen

View GitHub Profile
@n8thangreen
n8thangreen / Esther's LoS plotting script.R
Last active December 15, 2016 09:54
Length of stay plotting script
data_long = melt(CDIdata, id.vars=c("rowID", "ribotype", "severe", "LoS.total", "LoS.preinf", "LoS.postinf", "discharge.status"))
data_long$value = as.numeric(as.character(data_long$value))
# Change discharge to death in hospital
data_long$variable = factor(data_long$variable, levels=c(unique(as.character(data_long$variable)), "Death in hospital"))
d = data_long$variable=="Discharged" & data_long$discharge.status == "3"
data_long$variable[d] = "Death in hospital"
#write.csv(data_long, file="data_long.csv")
@n8thangreen
n8thangreen / map_tb.R
Last active December 15, 2016 09:55
Plot UK choropleth maps developed using LA and ETS data but can be applied more generally
map.tb <- function(map.data, g=NA, brks=NA, title="", value="tb", file=FALSE){
## plot UK choropleth maps
## developed using LA and ETS data but
## can be applied more generally
##
## map.data: map object loaded by readOGR()
## g: number of groups for values; max(g)=9
## brks: given break points to group values
## title: plot title
## value: variable name of colouring value (string)
\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\begin{document}
\Sexpr{cat(sub("_", "\\\\\\_", version$platform))}
% !Rnw root = TB_root.Rnw
%% some of the output is missing when compiled to PDF
%% when the regression is fit to models with lots of variable levels
@n8thangreen
n8thangreen / covariate shift.R
Last active May 28, 2018 01:53
empirical and model based (logistic) training sample adjustment
covariateShift <- function(data, resla, riskfac, ssize=10000){
## importance sampling approach
## when different distributions for the
## training and test data
require(plyr)
Natsal.riskfac.table <- DistnTable(data, riskfac)
Natsal.riskfac.table <- colNameReplace(Natsal.riskfac.table, "(all)", "Natsalfreq")
@n8thangreen
n8thangreen / ReadinExcelWorkbook.R
Last active December 15, 2016 09:54
Read-in the whole of an Excel workbook and then extract whichever fields wanted
## read from Excel workbook
require(XLConnect)
wb = loadWorkbook("C:/Users/ngreen1/Documents/IDEA/raw_data/TB_database_patientdata_300614.xlsx")
tab.names <- getSheets(wb)
TBlist <- sapply(1:length(tab.names), function(x) readWorksheet(wb, sheet=x, header=TRUE))
names(TBlist) <- tab.names
## extract only columns of interest
require(plyr)
extractNames <- readLines("C:/Users/ngreen1/Documents/IDEA/raw_data/relevant_fields.csv")
@n8thangreen
n8thangreen / forestplot.R
Last active December 15, 2016 09:54
forest plot horizontal lines grouped
library("rmeta", lib.loc="C:/Program Files/R/R-3.1.0/library")
names <- c("Premature", "Not premature",
NA,"Surgical","Not surgical",
NA,"Congenital","Not congenital",
NA,"Catheter","Not catheter",
NA,"Other","Not other",
NA,"Tai","Not Tai",
NA,"Cancer","Not cancer")
@n8thangreen
n8thangreen / plot_CI_Natsal.R
Last active December 15, 2016 09:53
plot confidence interval plots of Natsal-3 variables against testing proportions
library("plotrix", lib.loc="C:/Program Files/R/R-3.1.0/library")
library(Hmisc)
NATSAL.dat <<- read.csv("~/Chlamydia/data/NATSAL/Natsal-3_extract_2April2014.csv")
plotCInatsal <- function(varname, save=TRUE){
tab <- table(NATSAL.dat$cttestly=="yes", NATSAL.dat[,varname])
tab.df <- data.frame(addmargins(tab))
#--run in the old version of R
setwd("C:/Temp/")
packages <- installed.packages()[,"Package"]
save(packages, file="Rpackages")
Followed by this in the new version:
#--run in the new version
setwd("C:/Temp/")
load("Rpackages")
for (p in setdiff(packages, installed.packages()[,"Package"]))
@n8thangreen
n8thangreen / R introduction - Lecture 1.R
Last active December 15, 2016 09:48
Imperial College London MSc Public Health practical
## ----setup, include=FALSE------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)
## ---- echo=T, eval=T-----------------------------------------------------
2 + 2
## ---- echo=T, eval=T-----------------------------------------------------
a <- 2 + 2
## ---- echo=T,eval=T------------------------------------------------------
@n8thangreen
n8thangreen / R introduction - Lecture 2.R
Last active December 15, 2016 09:48
Imperial College London MSc Public Health practical
## ----setup, include=FALSE------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)
## ---- eval=T,echo=T------------------------------------------------------
a <- 1
b <- 2
c <- -1
d <- 2
# Check if two values are the same. Note the difference between