-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuni.final.func.r
359 lines (316 loc) · 14.5 KB
/
uni.final.func.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# NEJM p-values
NEJM_pval <-function(x){ ifelse(x >= .001, signif(x,2) , 'P<0.001') }
# In general, P values larger than 0.01 should be reported to two decimal places,
# those between 0.01 and 0.001 to three decimal places;
# P values smaller than 0.001 should be reported as P<0.001.
######
parseSummary <- function(summary,odsig=3,glm=FALSE,z=1.96){
if(!is.data.frame(summary)){
# get matrix
summary = do.call(rbind,summary);
# order and data.frame
if(glm){
summary = data.frame(summary[order(rownames(summary),summary[,4]),])
}else{
summary = data.frame(summary[order(summary[,4]),])
}
}
# odds and SE
summary$'OR' = signif( exp(summary$Estimate) , odsig )
# summary$'95 CI' = paste( '(', signif( exp(summary$Estimate - (z)*summary[,2]) ,odsig),' - ', signif( exp(summary$Estimate + (z)*summary[,2]) ,odsig), ')',sep='')
# exp(Estimate) + 1.96 * exp(Estimate) * Std.err
summary$'95 CI' = paste( '(', signif( exp(summary$Estimate) - 1.96 * exp(summary$Estimate) * summary[,2] ,odsig),' - ', signif( exp(summary$Estimate) + 1.96 * exp(summary$Estimate) * summary[,2] ,odsig), ')',sep='')
# print(paste('95% CI =',exp((z)*summary[,2])))
print(paste('95% CI = ',1.96 * exp(summary$Estimate) * summary[,2]))
#pvalue adjust
summary$'Pr(W)' = NEJM_pval(summary[,4])
#rownames
rownames(summary) = gsub('_ug\\.mL', '' ,rownames(summary))
# gen table
print(xtab <- xtable(summary[,5:7]),floating=FALSE)
return(xtab)
}
#########
## aggregation functions
cumprob_f <- function(Prob,n){
1-rollapply(1-Prob,2,function(x) prod(na.omit(x)),fill=NA)
}
geomeanprob_f <- function(Prob,n){
rollapply(Prob,n,function(x) exp(mean(log(na.omit(x)))),fill=NA)
}
meanprob_f <- function(Prob,n){
rollapply(Prob,n,function(x) mean(na.omit(x)),fill=NA)
}
meanprob_f_debug <- function(Prob,n){
rollapply(Prob,n,function(x) print_n_func(mean,x),fill=NA)
}
print_n_func <- function(f,x){
print(x)
f_out <- f(x,na.rm = TRUE)
print(f_out)
f_out
}
library(gridExtra)
g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
legend
}
#' cross_valid_resampling_sensitive
# cross validation method for sampling around subject resampling bias
cross_valid_resampling_sensitive <- function(data,formula,resp=all.vars(formula)[1],family,K=10,model=glm,resampled_var='ParticipantID',j=40,valid_prop=.2){
K=K+1
#ind = sample(1:nrow(data))
#interval = floor(nrow(data)/K)
#ranges = seq(1,nrow(data),interval)
pred = list(); train = list(); labels = list()
for(k in 1:(K-1)){ # k-th fold
# iterate over j subjects without replacement
ind = sapply( sample(unique(data[,resampled_var]),j), function(id){
sample(which(data[,resampled_var]==id),1) }) # randomly choose a sample from each chosen subject
# low = ranges[k] ; high = ranges[k+1]
#valid_ind = ind[low:(high-1)]
#train_ind = ind[!ind %in% valid_ind]
#valid_max = round(length(ind)*valid_prop)
#valid_ind = ind[1:valid_max]
#train_ind = ind[(valid_max+1):length(ind)]
#glm_i = model( formula , data=data[train_ind,] , family=family)
#pred[[as.character(k)]] = predict(glm_i,newdata=data[,])
pred[[as.character(k)]] = data[ind,all.vars(formula)[-1]]
train[[as.character(k)]] = ind
labels[[as.character(k)]] = data[ind,resp]
}
return(list(
pred_out = do.call(cbind,pred),
train_out = do.call(cbind,train),
labels_out = do.call(cbind,labels)
))
}
####
# Get groupings
#' Returns case-control groupings
#'
#' @param ParticipantID A vector of participant IDs. Must alternate between cases and corresponding controls (e.g. case1,control1a,control1b...control1f,case2,control2a,control2b...)
#' @param cases A vector of cases.
#' @return A numeric vector denoting the group of each participant.
#' @examples
#' get_groupings(c('a','b','c','d'), c('a','c')) # -> c(1,1,2,2)
#' get_groupings(c('a1','a2','b','c','d1','d2'), c('a','c')) # -> c(1,1,1,2,2,2)
get_groupings <- function(ParticipantID,cases){
#checks
if(!all(cases %in% ParticipantID)) {warning(" 'ParticipantID' should contain all cases")}
group=c()
for(i in 1:length(ParticipantID)){ # for each participant ID
if(ParticipantID[i] %in% cases){ # if ID is a case, begin a new group
group[i]=which(cases==ParticipantID[i])
}else{
group[i] = group[i-1]
}
}
return(group)
}
clogit_screen<-function(){
library(survival)
summaryHMO <- list()
variables = unique(c(grep('_ug',HMOs,value=T)))
for(var in variables ){
#### clogit Model for temporal HMOs and covariates
form = as.formula( paste(c('NEC.binary ~ I(sqrt(DPP))+',
ifelse(var %in% covariates,var,paste('scale(',var,')') ), '+ strata(matchedgroups)') ,collapse=' ') )
print(form)
NEC_data_tmp = unique(na.omit(NEC_data[,c('NEC.binary','DPP',var,'matchedgroups')])) # remove na and unique rows for var-specific data frame
mod = clogit(form , data = NEC_data_tmp )
summaryHMO[[var]] = coef(summary(mod))[-1,-2] # HMOs
}
summaryHMO = data.frame(do.call(rbind,summaryHMO))
colnames(summaryHMO)[1] = 'Estimate'
summaryHMO = summaryHMO[order(summaryHMO[,4]),]
summaryHMO.tab = parseSummary(summaryHMO);
}
########
# visualization
####
# Load Visualization Libraries
####
library(ggplot2)
library(directlabels)
library(wordcloud)
library(tm)
####
# Regression Volcano Plot
#' Volcano plot using wald probabilies and the corresponding beta statistics
#'
#' @param pval A numeric vector containing p-values
#' @param beta A numeric vector containing corresponding beta values
#' @param name A character vector indicating the filename
#' @param thresh A number indicating the theshold for including lables
#' @param plot.directory, A directory, indicating where the plot should be saved. If NULL, no plot is saved.
#' @examples
#' volcano.reg(c(.4,.04),c(.1,5))
volcano.reg<-function(pval,beta,nam,rnames,thresh=5,plot.directory=file.path(getwd(),'volcano.reg/'),exclude.outlier=TRUE){
gees.df = data.frame(Beta=beta,Pr_wald=pval)
print(gees.df)
rownames(gees.df) = rnames # adding anova and
gees.df$q_wald = p.adjust(gees.df$Pr_wald,'fdr')
gees.df$negLog_10_wald = -log(gees.df$q_wald+1e-20,10)
# save(beta,pval,rnames,gees.df,file='tmp.rda')
gees.df$HMO = rownames(gees.df)
#qplot(x=Beta,y=negLog_10_wald,data=gees.df)
label.dat = gees.df[gees.df$negLog_10_wald>=thresh,]
gees.df = gees.df[gees.df$Beta<10,] # remove extreme values
if(exclude.outlier){
gees.df = gees.df[abs(gees.df$Beta)<(2*sd(gees.df$Beta)),]
}
#
# with(gees.df, textplot(Beta,negLog_10_wald,ifelse(negLog_10_wald>thresh,HMO,'')))
#
# gees.df$HMO_lab <- ifelse(gees.df$negLog_10_wald>thresh,gees.df$HMO,'')
# direct.label(xyplot(Beta~negLog_10_wald,gees.df,groups=HMO_lab, col="black"))
#
g<-ggplot(gees.df, aes(Beta,negLog_10_wald)) + geom_point(color=gees.df$negLog_10_wald)
#geom_text(data = label.dat, aes(Beta,negLog_10_wald, label = HMO,color=negLog_10_wald), size = 2)
# geom_dl(aes(label=HMO, colour=negLog_10_wald), list('last.bumpup', cex = 1.3, hjust = 1))
#direct.label(g, list("last.points", cex=.7, hjust=1))
# theme(axis.title.x=element_text('-log(Pr(wald))'))
if(!is.null(plot.directory)){
dir.create(plot.directory)
ggsave(file.path(plot.directory,nam),plot=g)
dev.off()
write.csv(gees.df[order(gees.df$Pr_wald,-abs(gees.df$Beta)),],quote=FALSE,file=paste(file.path(plot.directory,nam),'.csv',sep=''))
}
return(gees.df)
}
####
# Spagetti Plot
#' Line plot for longitudinal data.
#'
#' @param var.string A string refering to the column name of \code{var} in data.frame \code{dat}.
#' @param dat A data.frame containing a time variable (DPP), reponse variable (NEC).
#' @param scale A boolean indicating if the variable indicated by var.string should be scaled.
#' @param plot.directory, A directory, indicating where the plot should be saved. If NULL, no plot is saved.
#' @examples
#' spagetify( 'var1',dat)
spagetify<-function(var.str,dat,scale=TRUE,plot.directory=file.path(getwd(),'spagetti/'),ymin=ifelse(scale,-3,0),ymax=ifelse(scale,3,1000)){
if(!is.null(plot.directory)){
dir.create(plot.directory)
nam = paste(c('HMO',ifelse(scale,'scaled','raw'),var.str,'pdf') , collapse='.')
pdf(file.path(plot.directory,nam))
}
id <- unique(dat$ParticipantID)
plot(x=c(0, 30), y= c(ymin, ymax ) )
for (ii in 1:length(id)){
tmp <- subset(dat, ParticipantID==id[ii], c("DPP",var.str, "NEC.binary"))
if(scale){
tmp[,2] = scale(tmp[,2])
}
lines(tmp[, "DPP"], tmp[,var.str], col=tmp[,"NEC.binary"] + 1, type="l", pch=16)
}
if(!is.null(plot.directory)){
dev.off()
}
}
####
# GGPlot2 Lasagna Plot
#' Heatmap for longitudinal data.
#'
#' @param var A numeric vector from the data.frame \code{dat}.
#' @param var.string A string refering to the column name of \code{var} in data.frame \code{dat}.
#' @param dat A data.frame containing a time variable (DPP), reponse variable (NEC).
#' @param smooth An integer, number of adjacent days to consider in generated mean and standard deviation.
#' @param maxCNT, An integer, the maximum repsonse variable to be considered Control.
#' @param plot.pval, A boolean, inclusion of z-test pvalue describing the change relative to the mean and standard deviation produced by the \code{smooth} window.
#' @param plot.fill, A string, indicating the fill of the heatmap: 'logfoldchange','foldChange'
#' @param plot.directory, A directory, indicating where the plot should be saved. If NULL, no plot is saved.
#' @return A ggplot2 plot object.
#' @examples
#' lasagnify(dat$var1, 'var1',dat)
lasagnify<-function(var,var.str,dat,smooth=1,maxCNT=0,plot.pval=TRUE,plot.fill='logfoldChange',plot.directory=file.path(getwd(),'lasagnia/'),param=NULL){
if(length(var) != nrow(dat)){stop("v")}
# find the groupwise aggregated median
datprep = data.frame(var=var,DPP=dat$DPP,MatchGroup=dat$matchedgroups,NEC=dat$NEC)
d = aggregate(datprep$var,by=list(DPP=datprep$DPP,MatchGroup=datprep$MatchGroup,NEC=datprep$NEC),FUN=median,na.rm=TRUE) # aggregates sampes, messes up standard deviation
caseNEC=c( 2.2 , 2.1 , 1.2 , 3.2 , 3.1 , 2.4 , 2.3 , 3.3 , 1.1 , 2.5 )
cases = c('A003','A029','A066','B032','C005','C024','C027','E002','D015','D024')
d$NECgroups = paste(cases[d$MatchGroup],'- Bell: ',d$NEC)
d$NECgroups2 = paste(paste('Stage:',caseNEC[d$MatchGroup]), cases[d$MatchGroup] )####
# d$NECgroups = paste("Group:",letters[d$MatchGroup],'- NEC:',d$NEC)
d_m = c()
d_s = c()
d_md= c()
Necdays = c()
for(i in 1:nrow(d)){
DPP_cnt = which( d$MatchGroup[i] == dat$matchedgroups
& d$DPP[i]-smooth <= dat$DPP
& d$DPP[i]+smooth >= dat$DPP
& maxCNT >= as.numeric(dat$NEC) ) # find matching data in orgininal (unaggregated dat matrix)
#DPP_cnt = which.min( dat$DPP[DPP_cnt_pre])
d_md[i] = median(na.omit(var[DPP_cnt]))
d_m[i]= mean(na.omit(var[DPP_cnt]))
d_s[i] = sd(na.omit(var[DPP_cnt]))
# print(i)
# print(DPP_cnt)
# print(var[DPP_cnt])
}
#a<<-data.frame(d_s,d_m,d_md,d$x,d$NECgroups2,d$NECgroups,d$DPP)
#print(a[a[,5]=='D015'])
# d_s = ifelse( is.na(d_s) , mean(na.omit(d_s)) , d_s)
# d_m = ifelse( is.na(d_m) , mean(na.omit(d_m)) , d_m)
# d_md = ifelse( is.na(d_md), mean(na.omit(d_md)), d_md)
d$median = d_m
d$stdev = d_s
d$mean = d_md
#d$foldChange = d$x / d$median
d$foldChange = d$x / d$mean
d$logfoldChange = log(d$foldChange)
d$z = (d$x - d$mean)/ d$stdev
d$pValue_z = 2*pnorm(-abs(d$z))
d = na.omit(d)
NECdays=data.frame(DPP=1:4,NECgroups=1:8,bin=1)
d$DPP[d$NEC==0] = -d$DPP[d$NEC==0]
if(plot.pval){
if(plot.fill=='logfoldChange'){
plt <- ggplot(d, aes(x=DPP,y=NECgroups2,fill=logfoldChange,alpha=1-(pValue_z/2)))
maxnum = max(abs(d$logfoldChange))
}else if(plot.fill=='foldChange'){
plt <- ggplot(d, aes(x=DPP,y=NECgroups2,fill=foldChange,alpha=1-(pValue_z/2)))
maxnum = max(abs(d$foldChange))
}
}else{
if(plot.fill=='logfoldChange'){
plt <- ggplot(d, aes(x=DPP,y=NECgroups2,fill=logfoldChange))
maxnum = max(abs(d$logfoldChange))
}else if(plot.fill=='foldChange'){
plt <- ggplot(d, aes(x=DPP,y=NECgroups2,fill=foldChange))
maxnum = max(abs(d$foldChange))
}
}
# plt + theme(panel.background = element_rect(fill = "grey"),panel.grid.major = element_blank(),panel.grid.minor = element_blank())+geom_tile() + theme(axis.text.y=element_text(colour=c('blue','red')) ) + scale_fill_gradient2(low="red", mid = "white",high="blue" , breaks=seq(-4,2,.5),guide = guide_legend(title = paste('Log(fold change)\n',var.str) ))
# plt + theme(panel.background = element_rect(fill = "white"),panel.grid.major = element_blank(),panel.grid.minor = element_blank())+geom_tile() + theme(axis.text.y=element_text(colour=c('darkgreen','darkred')) ) + scale_fill_gradientn(colours=rev(c("darkgreen", "green", "yellow", "red", "darkred")) , breaks=seq(-4,2,.5),guide = guide_legend(title = paste('Log(fold change)\n',var.str) ))
if(maxnum<5){maxnum = 2.5}
# plt <- ggplot(d, aes(x=DPP,y=NECgroups2,fill=logfoldChange))
plt <- plt + theme(panel.background = element_rect(fill = "grey"),panel.grid.major = element_blank(),panel.grid.minor = element_blank() )+
geom_tile() + #theme(axis.text.y=element_text(colour=c('darkblue','white','red', 'darkred')[d$NEC]) ) +
theme(axis.text.y= element_text(size = rel(.7)) ) +
scale_fill_gradientn(colours=rev(c("darkblue", "blue", "white", "red", "darkred")) ,
breaks=seq(round(-maxnum-.5), round(maxnum+.5),1),
limits=c(-maxnum-.1, maxnum+.1), ##### fix scale
#values=seq(-2,2,length=10), rescaler = function(x, ...) x, oob = identity,
guide = 'colourbar' )+ #guide_legend(title = paste('Log(fold change)\n',var.str) )) +
geom_tile() #+
# geom_tile(onset_long, aes(x=DPP,y=ParticipantID,fill=NEC))
# plt
####
# NEC onset
NEC_onset = read.csv('~/Documents/Grad/Labs/Lewis/NEC_HMO/data/NEC_onset.csv')
onset_long = as.data.frame(do.call(rbind,apply(NEC_onset,1,function(x) cbind(x[1],cbind(x[2],x[3]:x[4])))))
colnames(onset_long)=c('ParticipantID','NEC', 'DPP')
onset_long$DPP = as.numeric(onset_long$DPP)
# plt2 <- ggplot(onset_long, aes(x=DPP,y=ParticipantID,fill=NEC))
if(!is.null(plot.directory)){
dir.create(plot.directory)
nam = paste(c('HMO',var.str,'smooth',smooth,'maxCNT',maxCNT,ifelse(plot.pval,'pval','withoutPval'),param,'eps') , collapse='.')
ggsave(file.path(plot.directory,nam),plot=plt,height=2,width=7)
}
return(plt)
}