-
Notifications
You must be signed in to change notification settings - Fork 2
/
mcmcComparisonPlot.R
560 lines (444 loc) · 19.6 KB
/
mcmcComparisonPlot.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
library(gtools)
library(rstan)
library(rjags)
library(shinystan)
library(VGAM) #Pareto
set.seed(1245)
options(scipen=99)
#Code from Kruschke's Doing Bayesian Data Analysis
HDIofMCMC = function(sampleVec, credMass=0.95) {
# Computes highest density interval from a sample of representative values,
# estimated as shortest credible interval.
# Arguments:
# sampleVec
# is a vector of representative values from a probability distribution.
# credMass
# is a scalar between 0 and 1, indicating the mass within the credible
# interval that is to be estimated.
# Value:
# HDIlim is a vector containing the limits of the HDI
sortedPts = sort(sampleVec)
ciIdxInc = ceiling(credMass * length(sortedPts))
nCIs = length(sortedPts) - ciIdxInc
ciWidth = rep(0, nCIs)
for(i in 1:nCIs) {
ciWidth[i]= sortedPts[i + ciIdxInc] - sortedPts[i]
}
HDImin = sortedPts[which.min(ciWidth)]
HDImax = sortedPts[which.min(ciWidth) + ciIdxInc]
HDIlim = c(HDImin, HDImax)
return(HDIlim)
}
#Function to calculate initial values for MCMC chains
initcalculator <- function(dat, starts, ends){
notusinit <- dim(dat)[2]
#get initial values for p
p <- array(dim = c(max(ends),
notusinit,
2)
)
for(j in 1:2){
for(i in 1:max(ends)){
p[i,,j] <- unlist(dat[i,1:notusinit] /sum(dat[i,1:notusinit]))
}
}
#Generate initial values for alpha
#Note that I add a small value to avoid infinite density errors.
alphas <- apply(p[,1:notusinit,1], 2, mean)
names(alphas) <- NULL
alphas2 <- apply(p[,1:notusinit,2], 2, mean)
names(alphas2) <- NULL
#Generate initial values for pi
pi <- array(dim = c(length(starts),
notusinit,
2))
for(j in 1:2){
for(i in 1:length(starts)){
pi[i,,j] <- (apply(dat[starts[i]:ends[i],1:notusinit], 2, mean) + 1) /
(sum(apply(dat[starts[i]:ends[i],1:notusinit], 2, mean) + 1))
pi[i,,j] <- pi[i,,j]
}
}
#Generate initial values for theta
theta <- matrix(nrow = 2,
ncol = length(starts))
for(j in 1:2){
for(i in 1:length(starts)){
thetas <- alphas / pi[i,,1] #There is no reason to use both parts of array
theta <- mean(thetas[which(thetas < 15000)])
#Here we avoid initializing using high values of theta that seem
#unrealistic.
}
}
return(list(list("p" = p[,,1],
"pi" = pi[,,1]
# "alpha" = c(alphas)/sum(alphas),
#"theta" = theta
),
list("p" = p[,,2],
"pi" = pi[,,2]
#"alpha" = c(alphas2)/sum(alphas2),
#"theta" = theta
)
)
)
}
# Function to simulate data.
simCom <- function(modeltype,
notus,
nreads,
nreps,
precision,
num_da = 0.25,
effectsize,
seed = 666,
thresh = 3){
set.seed(seed)
#######################################
# Simulate Dirichlet alpha parameters #
#######################################
enough <- "F"
while(enough == "F"){
alphas <- VGAM::rpareto(notus,
scale = 1, #floor of distribution, i.e. min. of range
shape = 4) #Degree of skew in distribution
abundant <- which(alphas >= 5 )
medium <- which(alphas < 5 & alphas > 2)
rare <- which( alphas <= 2)
#This is to enforce a resample if we didn't get any abundant features
if(length(abundant) > 0 & length(medium) > 0 & length(rare) > 0){
enough <- "T"
#Create a vector describing which indices where in which abundance category.
#This is used in the diffAbund script to see how well the model worked in each
#category.
abund_category <- rep("NA", length(alphas))
abund_category[abundant] <- "abund"
abund_category[medium] <- "med"
abund_category[rare] <- "rare"
#Choose members of each abundance category for effect size application
#NUM_DA is a proportion
abund_indices <- sample(abundant, size = round(length(abundant)*(num_da / 3)))
while(length(abund_indices) == 0){
abund_indices <- sample(abundant, size = 1)
}
medium_indices <- sample(medium, size = round(length(medium)*(num_da / 3)))
while(length(medium_indices) == 0){
medium_indices <- sample(medium, size = 1)
}
rare_indices <- sample(rare, size = round(length(rare)*(num_da / 3)))
while(length(rare_indices) == 0){
rare_indices <- sample(rare, size = 1)
}
toAffect <- na.omit(c(abund_indices, medium_indices, rare_indices))
#Adding this to avoid odd ball situations where toAffect is very small.
if(length(toAffect) < 10){
enough <- "F"
}
}
}
#Must make sure the exact same features differ from one group to the next and
#the sum of each vector is the same.
#To do this, we sample indices from each of the feature abundance classes
#(abund, med, rare) and duplicate those features by appending them onto the
#end of the alpha vector.
#Then, I multiply those appended features by the effect size, this makes alphas for
#group 1,
#Next, I multiply the features in group 2 that were not appended by the effect size.
#Thus the last features in the alpha vector differ among groups and the features
#corresponding to the indices in toAffect also differ.
#For example:
#Original alpha vector: 0.5,0.1,0.01
#New alpha vector: 0.5,0.1,0.01, 0.5,0.1,0.01
#After applying effect sizes and making vectors for both groups:
#I added tabs to make this a bit clearer to read.
#group 1: 0.5,0.1,0.01, 0.5*effect,0.1*effect,0.01*effect
#group 2: 0.5*effect,0.1*effect,0.01*effect, 0.5,0.1,0.01
#Apply effect size to make group 1 alpha parameter vector
alphas_group1 <- c(alphas, alphas[toAffect]*effectsize)
#Apply effect size to make group 2 alpha parameter vector
alphas_group2 <- c(alphas, alphas[toAffect])
alphas_group2[toAffect] <- alphas_group2[toAffect]*effectsize
#Sanity checks
#alphas_group2 - alphas_group1
#Make sure vectors add up to the same thing
sum(alphas_group2)
sum(alphas_group1)
#Define a vector of indices for features that should differ between groups. Note
#That this is for labeling purposes and gets overwritten after sampling from
#Dirichlet and multinomial distributions.
differentTaxa <- c(toAffect,
seq(length(alphas) + 1,
length(alphas) + length(toAffect), by = 1)
)
#Sample from the Dirichlet to make p parameters for the multinomial.
#This is the part that should vary among replicates, therefore we redefine the seed.
set.seed(NULL)
comMat <- matrix(0,
ncol = length(alphas_group1), #Both groups are same dimensions
nrow = nreps)
for(i in 1:(nreps / 2)){
comMat[i,] <- rmultinom(1,
nreads,
prob = rdirichlet(1, alpha = alphas_group1*precision) )
}
for(i in (1 + (nreps / 2)):nreps){
comMat[i,] <- rmultinom(1,
nreads,
prob = rdirichlet(1, alpha = alphas_group2*precision))
}
#Assign meaningful names to the different features,
#This will allow us to identify which features should differ later on.
colnames(comMat) <- paste("Feature_",
seq(1, length(alphas_group1),
by = 1),
abund_category, sep = "")
colnames(comMat)[differentTaxa] <- paste(colnames(comMat)[differentTaxa],
"_different",
sep = "")
comMat <- data.frame(comMat)
#Remove features with zero counts / low abundance across replicates.
#Remove these features from the differentTaxa object as well by remaking it.
#This is inelegant, but gets the job done.
comMat <- comMat[, which(colSums(comMat) > thresh )]
differentTaxa <- grep("different", colnames(comMat))
#Save alphas as proportions. We use this to compute model RMSE
#NOTE: we do not include low abundance features here, see following lines.
comprop <- rbind(alphas_group1[which(colSums(comMat) > thresh )] /
sum(alphas_group1[which(colSums(comMat) > thresh )]),
alphas_group2[which(colSums(comMat) > thresh )] /
sum(alphas_group2[which(colSums(comMat) > thresh )]))
#Add a 1 to avoid infinite density slicer error generation by rjags
comMat <- comMat + 1
return(list(simulatedCommunity = comMat,
differentTaxa = differentTaxa,
Parameters_alpha1_2_pi1_2 = comprop,
abund_category = abund_category,
exp_abund_differ = grep("abund_different", colnames(comMat)),
exp_med_differ = grep("med_different", colnames(comMat)),
exp_rare_differ = grep("rare_different", colnames(comMat))
))
}
simcom_out <- simCom(modeltype = "pareto_shape_4",
notus = 100, #Note that if this is too small, this function will hang bc it is not generating enough features in each abundance class
nreads = 1000,
nreps = 40,
precision = 1,
num_da = 0.25,
effectsize = 2,
seed = 666,
thresh = 3)
#Make a design variable, describing which sample goes with which group.
groupings <- c(rep("group1", 40 / 2),
rep("group2", 40 / 2))
DM<-stan_model("DM.stan", model_name="DM")
x <- proc.time()
inits <- initcalculator(dat = simcom_out$simulatedCommunity,
starts = c(min(which(groupings == unique(groupings)[1])),
min(which(groupings ==unique(groupings)[2]))),
ends = c(max(which(groupings == unique(groupings)[1])),
max(which(groupings == unique(groupings)[2]))))
fitstan_VB <-vb(DM,
data=list("datamatrix"=simcom_out$simulatedCommunity,
"nreps"=nrow(simcom_out$simulatedCommunity),
"notus"=ncol(simcom_out$simulatedCommunity),
"N"=2,
"start" = c(min(which(groupings == unique(groupings)[1])),
min(which(groupings ==unique(groupings)[2]))),
"end" = c(max(which(groupings == unique(groupings)[1])),
max(which(groupings == unique(groupings)[2])))),
algorithm="meanfield",
output_samples=500,
check_data = T,
seed=123,
pars<-c("pi")
,init = list("p"= list(inits[[1]]$p, inits[[2]]$p),
"pi"= inits[[1]]$pi)
)
print("Time for Stan to run using VB algorithm")
vb_time <- proc.time()-x
#Run NUTS algorithm
x <- proc.time()
fitstan_NUTS<-sampling(DM,
data=list(datamatrix=simcom_out$simulatedCommunity,
nreps=nrow(simcom_out$simulatedCommunity),
notus=ncol(simcom_out$simulatedCommunity), N=2,
start = c(min(which(groupings == unique(groupings)[1])),
min(which(groupings ==unique(groupings)[2]))),
end = c(max(which(groupings == unique(groupings)[1])),
max(which(groupings == unique(groupings)[2])))),
chains=2,
control = list(max_treedepth = 15),
warmup=10,
iter=20,
thin=2,
algorithm="NUTS",
cores=2,
pars<-c("pi"),
init = initcalculator(dat = simcom_out$simulatedCommunity,
starts = c(min(which(groupings == unique(groupings)[1])),
min(which(groupings == unique(groupings)[2]))),
ends = c(max(which(groupings == unique(groupings)[1])),
max(which(groupings == unique(groupings)[2])))),
verbose=T)
print("Time for Stan to run using NUTS algorithm")
nutsTime <- proc.time()-x
community.model.level <- "model{
for(i in 1:N){
for(j in start[i]:end[i]){
datamatrix[j,] ~ dmulti(p[j,], nreads[j])
p[j,1:notus] ~ ddirch(pi[i,]*theta[i])
}
pi[i,1:notus] ~ ddirch(alpha)
theta[i] ~ dunif(0.1, 4000)
}
for(k in 1:notus){
alpha[k] <- 0.0000001
}
}"
#########################
# Function to run model #
#########################
modelRun <- function(SimulatedData,
groupings){
#Specify model
sim.mod.jags <- rjags::jags.model(textConnection(community.model.level),
data = list(
datamatrix = SimulatedData,
notus = dim(SimulatedData)[2],
nreads = rowSums(SimulatedData),
N = length(unique(groupings)),
start = c(min(which(
groupings == unique(groupings)[1])),
min(which(
groupings == unique(groupings)[2]))),
end = c(max(which(
groupings == unique(groupings)[1])),
max(which(
groupings == unique(groupings)[2])))
),
inits = initcalculator(dat = SimulatedData,
starts = c(min(which(groupings == unique(groupings)[1])),
min(which(groupings == unique(groupings)[2]))),
ends = c(max(which(groupings == unique(groupings)[1])),
max(which(groupings == unique(groupings)[2])))),
n.chains = 2,
n.adapt = 0
)
#Adapt model for as long as needed
iter_needed <- 0
y = FALSE
while(y == FALSE){
y <- adapt(sim.mod.jags,
n.iter = 10,
end.adaptation = FALSE)
iter_needed <- 10 + iter_needed
if(iter_needed > 50){break}
}
#Burn in model. This should lead to convergence on the high density interval
#of posterior parameter space.
update(sim.mod.jags,
n.iter = 50)
#Here we extract the MCMC samples
sim.mod.sam <- rjags::jags.samples(model = sim.mod.jags,
variable.names=c("pi"
),
n.iter = 40, thin = 4)
return(sim.mod.sam)
}
sim.mod.sam <- modelRun(SimulatedData = simcom_out$simulatedCommunity,
groupings = groupings)
est.pi <- extract(fitstan_NUTS,"pi")
est.pi_vb <- extract(fitstan_VB,"pi")
#Plot
add.alpha <- function(col, alpha=1){
if(missing(col))
stop("Please provide a vector of colours.")
apply(sapply(col, col2rgb)/255, 2,
function(x)
rgb(x[1], x[2], x[3], alpha=alpha))
}
#order by expected relative abundance
orderUse <- order(simcom_out$Parameters_alpha1_2_pi1_2[1,])
par(oma = c(1,1,0,0))
plot(apply(est.pi$pi[,1,], 2, FUN=mean)[orderUse],
pch = 16,
ylab = "",
xlab = "Feature (ordered by relative abundance)",
frame.plot = F,
yaxt = "n",
xaxt = "n",
ylim = c(0.005, .045),
xlim = c(0,110),
col = add.alpha("darkslateblue", 0.6))
axis(side = 2,at = c(seq(0.005, 0.045, by = 0.01)), labels = c(seq(0.005, 0.045, by = 0.01)),
las = 2)
axis(side = 1,
at = c(seq(0, 100, by = 20),110),
labels = c(seq(0, 100, by = 20),110))
text(expression(paste(pi, " estimate"), sep = ""),
x = -19, y = 0.025, srt = 90,xpd = NA, cex = 1.5)
points(fitstan_VB@sim$est$pi[1,orderUse],
col = add.alpha("darkseagreen4", 0.6),
pch = 16)
points(apply(sim.mod.sam$pi[1,orderUse,,1:2], 1, mean),
col = add.alpha("coral3", 0.6),
pch = 16)
points(simcom_out$Parameters_alpha1_2_pi1_2[1,orderUse],
col = "black",
pch = 3,
cex = 0.3)
#jags
segments(x0 = c(seq(1, 110, by = 1) - 0.3),
x1 = c(seq(1, 110, by = 1) + 0.3),
y0 = apply(sim.mod.sam$pi[1,orderUse,,1:2], 1, HDIofMCMC, credMass = 0.95)[1,],
y1 = apply(sim.mod.sam$pi[1,orderUse,,1:2], 1, HDIofMCMC, credMass = 0.95)[1,],
col = add.alpha("coral3", 0.6))
segments(x0 = c(seq(1, 110, by = 1) - 0.3),
x1 = c(seq(1, 110, by = 1) + 0.3),
y0 = apply(sim.mod.sam$pi[1,orderUse,,1:2], 1, HDIofMCMC, credMass = 0.95)[2,],
y1 = apply(sim.mod.sam$pi[1,orderUse,,1:2], 1, HDIofMCMC, credMass = 0.95)[2,],
col = add.alpha("coral3", 0.6))
segments(x0 = c(seq(1, 110, by = 1) - 0.05),
x1 = c(seq(1, 110, by = 1) - 0.05),
y0 = apply(sim.mod.sam$pi[1,orderUse,,1:2], 1, HDIofMCMC, credMass = 0.95)[1,],
y1 = apply(sim.mod.sam$pi[1,orderUse,,1:2], 1, HDIofMCMC, credMass = 0.95)[2,],
col = add.alpha("coral3", 0.6),
lty = 3)
#nuts
segments(x0 = c(seq(1, 110, by = 1) - 0.3),
x1 = c(seq(1, 110, by = 1) + 0.3),
y0 = apply(est.pi$pi[,1,orderUse], 2, FUN=HDIofMCMC)[1,],
y1 = apply(est.pi$pi[,1,orderUse], 2, FUN=HDIofMCMC)[1,],
col = add.alpha("darkseagreen4", 0.6),
pch = 1)
segments(x0 = c(seq(1, 110, by = 1) - 0.3),
x1 = c(seq(1, 110, by = 1) + 0.3),
y0 = apply(est.pi$pi[,1,orderUse], 2, FUN=HDIofMCMC)[2,],
y1 = apply(est.pi$pi[,1,orderUse], 2, FUN=HDIofMCMC)[2,],
col = add.alpha("darkseagreen4", 0.6),
pch = 1)
segments(x0 = c(seq(1, 110, by = 1) + 0.05),
x1 = c(seq(1, 110, by = 1) + 0.05),
y0 =apply(est.pi$pi[,1,orderUse], 2, FUN=HDIofMCMC)[1,],
y1 = apply(est.pi$pi[,1,orderUse], 2, FUN=HDIofMCMC)[2,],
col = add.alpha("darkseagreen4", 0.6),
lty = 3)
#vb
segments(x0 = c(seq(1, 110, by = 1) - 0.3),
x1 = c(seq(1, 110, by = 1) + 0.3),
y0 = apply(est.pi_vb$pi[,1,orderUse], 2, FUN=HDIofMCMC)[1,],
y1 = apply(est.pi_vb$pi[,1,orderUse], 2, FUN=HDIofMCMC)[1,],
col = add.alpha("darkslateblue", 0.6),
pch = 1)
segments(x0 = c(seq(1, 110, by = 1) - 0.3),
x1 = c(seq(1, 110, by = 1) + 0.3),
y0 = apply(est.pi_vb$pi[,1,orderUse], 2, FUN=HDIofMCMC)[2,],
y1 = apply(est.pi_vb$pi[,1,orderUse], 2, FUN=HDIofMCMC)[2,],
col = add.alpha("darkslateblue", 0.6),
pch = 1)
segments(x0 = c(seq(1, 110, by = 1)),
x1 = c(seq(1, 110, by = 1)),
y0 =apply(est.pi_vb$pi[,1,orderUse], 2, FUN=HDIofMCMC)[1,],
y1 = apply(est.pi_vb$pi[,1,orderUse], 2, FUN=HDIofMCMC)[2,],
col = add.alpha("darkslateblue", 0.6),
lty = 3)