library(rjags)
library(runjags)
library(tictoc)
source("DBDA2E-utilities.R")
*********************************************************************
Kruschke, J. K. (2015). Doing Bayesian Data Analysis, Second Edition:
A Tutorial with R, JAGS, and Stan. Academic Press / Elsevier.
*********************************************************************
Now simulate the sum assuming a particular probability of a positive test, within each event. The model of the distribution of the nu’s is assumed to center at 0.05 and have a concentration of 100.
omega <- 0.05 # mode of the beta
kappa <- 100
Simulate different rates for the different events.
k <- 100 # 5, 10, 50, 100
prob_nu <- rbeta(k, omega*(kappa-2)+1, (1 - omega)*(kappa - 2) + 1)
prob_nu
[1] 0.07044188 0.07596075 0.04933019 0.05546887 0.04935177 0.06546845 0.03663763
[8] 0.04495812 0.04658013 0.04183330 0.04582573 0.03821744 0.09956540 0.09938542
[15] 0.05892924 0.02318231 0.04182647 0.03608764 0.05072006 0.07821026 0.04762550
[22] 0.03923565 0.04501793 0.03503304 0.06018106 0.03572779 0.04498868 0.04714249
[29] 0.03644711 0.04945165 0.03067050 0.04071703 0.03584696 0.07725377 0.10142347
[36] 0.06110627 0.02900004 0.10096490 0.04153968 0.08740562 0.06453554 0.03629171
[43] 0.01724166 0.04664005 0.05454658 0.03044524 0.06164009 0.03277459 0.05530560
[50] 0.03351285 0.05691430 0.12741136 0.03841059 0.03043895 0.08027541 0.04725561
[57] 0.03693210 0.08228042 0.07486827 0.11284365 0.04836635 0.04462944 0.05255156
[64] 0.04008532 0.07114626 0.06088969 0.06537890 0.07527178 0.07504942 0.03001773
[71] 0.04279550 0.13447152 0.05481039 0.02629983 0.06756783 0.03749970 0.05683402
[78] 0.05266612 0.10133448 0.02788218 0.09941478 0.04003526 0.06995412 0.07048367
[85] 0.07405281 0.06649331 0.05929455 0.09669213 0.07311231 0.05223942 0.04462727
[92] 0.07098624 0.07835186 0.06436245 0.04776439 0.03836016 0.02864156 0.10096296
[99] 0.05034047 0.03803735
n <- 5000 # 300, 400, 1000, 5000
y <- rbinom(k, n, prob_nu)
y
[1] 364 356 255 291 256 357 184 228 217 211 230 199 514 498 289 120 191 189 247 373
[21] 246 164 193 162 307 178 225 233 175 264 146 205 165 402 484 310 158 519 213 438
[41] 337 205 92 212 279 158 278 161 278 154 302 646 197 145 413 259 193 418 358 600
[61] 256 222 273 205 316 306 347 349 395 163 216 671 260 113 368 218 290 258 497 128
[81] 523 197 351 348 374 328 289 501 374 264 215 351 398 284 242 174 138 457 228 207
JAGS need the data to be in a list of values, vectors, and matrices.
dataList <- list(
y = y,
Ntotal = n,
k = k
)
dataList
$y
[1] 364 356 255 291 256 357 184 228 217 211 230 199 514 498 289 120 191 189 247 373
[21] 246 164 193 162 307 178 225 233 175 264 146 205 165 402 484 310 158 519 213 438
[41] 337 205 92 212 279 158 278 161 278 154 302 646 197 145 413 259 193 418 358 600
[61] 256 222 273 205 316 306 347 349 395 163 216 671 260 113 368 218 290 258 497 128
[81] 523 197 351 348 374 328 289 501 374 264 215 351 398 284 242 174 138 457 228 207
$Ntotal
[1] 5000
$k
[1] 100
betaABfromModeKappa( mode=0.05, kappa=100 )
$a
[1] 5.9
$b
[1] 94.1
gammaShRaFromModeSD(mode = 10, sd = 5)
$shape
[1] 5.828427
$rate
[1] 0.4828427
modelString <- "
model {
for (i in 1:k){
y[i] ~ dbin( nu[i], Ntotal ) # Likelihood in code
nu[i] = eta*pi[i] + (1 - theta)*(1-pi[i])
pi[i] ~ dbeta( omega*(kappa - 2) + 1, (1 - omega)*(kappa - 2) + 1 )
Py[i] ~ dbin( nu[i], Ntotal ) # Predictive distribution
}
omega ~ dbeta( 6, 95) # mode of the beta on pi[i]
kappa = kappaMinusTwo + 2 # concentration for the beta on the pi[i]
kappaMinusTwo ~ dgamma( 5.8, .48 ) # (kappa - 2) cannot be negative
eta ~ dbeta( 910 , 90 ) # prior
theta ~ dbeta( 950 , 50 ) # prior
Py_tot = sum(Py)
}
"
modelString
[1] "\nmodel {\n for (i in 1:k){\n y[i] ~ dbin( nu[i], Ntotal ) # Likelihood in code\n nu[i] = eta*pi[i] + (1 - theta)*(1-pi[i])\n pi[i] ~ dbeta( omega*(kappa - 2) + 1, (1 - omega)*(kappa - 2) + 1 ) \n Py[i] ~ dbin( nu[i], Ntotal ) # Predictive distribution\n }\n omega ~ dbeta( 6, 95) # mode of the beta on pi[i]\n kappa = kappaMinusTwo + 2 # concentration for the beta on the pi[i]\n kappaMinusTwo ~ dgamma( 5.8, .48 ) # (kappa - 2) cannot be negative\n eta ~ dbeta( 910 , 90 ) # prior\n theta ~ dbeta( 950 , 50 ) # prior\n Py_tot = sum(Py)\n}\n"
writeLines( modelString, con="model.txt" ) # write to file
omega_init <- 0.05
kappa_init <- 10
eta_init <- 0.95
theta_init <- 0.95
initsList = list( omega = omega_init,
eta = eta_init,
theta = theta_init)
nChains=4
nAdaptSteps=50000
nBurninSteps=25000
nUseSteps=25000 # total number of used steps
nThinSteps=100
Single threaded program.
tic()
jagsModel <- jags.model( file = textConnection(modelString),
data = dataList,
inits = initsList,
n.chains = nChains,
n.adapt = nAdaptSteps)
Compiling model graph
Resolving undeclared variables
Allocating nodes
Graph information:
Observed stochastic nodes: 10
Unobserved stochastic nodes: 24
Total graph size: 96
Initializing model
|
| | 0%
|
|+ | 2%
|
|++ | 4%
|
|+++ | 6%
|
|++++ | 8%
|
|+++++ | 10%
|
|++++++ | 12%
|
|+++++++ | 14%
|
|++++++++ | 16%
|
|+++++++++ | 18%
|
|++++++++++ | 20%
|
|+++++++++++ | 22%
|
|++++++++++++ | 24%
|
|+++++++++++++ | 26%
|
|++++++++++++++ | 28%
|
|+++++++++++++++ | 30%
|
|++++++++++++++++ | 32%
|
|+++++++++++++++++ | 34%
|
|++++++++++++++++++ | 36%
|
|+++++++++++++++++++ | 38%
|
|++++++++++++++++++++ | 40%
|
|+++++++++++++++++++++ | 42%
|
|++++++++++++++++++++++ | 44%
|
|+++++++++++++++++++++++ | 46%
|
|++++++++++++++++++++++++ | 48%
|
|+++++++++++++++++++++++++ | 50%
|
|++++++++++++++++++++++++++ | 52%
|
|+++++++++++++++++++++++++++ | 54%
|
|++++++++++++++++++++++++++++ | 56%
|
|+++++++++++++++++++++++++++++ | 58%
|
|++++++++++++++++++++++++++++++ | 60%
|
|+++++++++++++++++++++++++++++++ | 62%
|
|++++++++++++++++++++++++++++++++ | 64%
|
|+++++++++++++++++++++++++++++++++ | 66%
|
|++++++++++++++++++++++++++++++++++ | 68%
|
|+++++++++++++++++++++++++++++++++++ | 70%
|
|++++++++++++++++++++++++++++++++++++ | 72%
|
|+++++++++++++++++++++++++++++++++++++ | 74%
|
|++++++++++++++++++++++++++++++++++++++ | 76%
|
|+++++++++++++++++++++++++++++++++++++++ | 78%
|
|++++++++++++++++++++++++++++++++++++++++ | 80%
|
|+++++++++++++++++++++++++++++++++++++++++ | 82%
|
|++++++++++++++++++++++++++++++++++++++++++ | 84%
|
|+++++++++++++++++++++++++++++++++++++++++++ | 86%
|
|++++++++++++++++++++++++++++++++++++++++++++ | 88%
|
|+++++++++++++++++++++++++++++++++++++++++++++ | 90%
|
|++++++++++++++++++++++++++++++++++++++++++++++ | 92%
|
|+++++++++++++++++++++++++++++++++++++++++++++++ | 94%
|
|++++++++++++++++++++++++++++++++++++++++++++++++ | 96%
|
|+++++++++++++++++++++++++++++++++++++++++++++++++ | 98%
|
|++++++++++++++++++++++++++++++++++++++++++++++++++| 100%
update(jagsModel, n.iter = nBurninSteps) # Burn-in
|
| | 0%
|
|* | 2%
|
|** | 4%
|
|*** | 6%
|
|**** | 8%
|
|***** | 10%
|
|****** | 12%
|
|******* | 14%
|
|******** | 16%
|
|********* | 18%
|
|********** | 20%
|
|*********** | 22%
|
|************ | 24%
|
|************* | 26%
|
|************** | 28%
|
|*************** | 30%
|
|**************** | 32%
|
|***************** | 34%
|
|****************** | 36%
|
|******************* | 38%
|
|******************** | 40%
|
|********************* | 42%
|
|********************** | 44%
|
|*********************** | 46%
|
|************************ | 48%
|
|************************* | 50%
|
|************************** | 52%
|
|*************************** | 54%
|
|**************************** | 56%
|
|***************************** | 58%
|
|****************************** | 60%
|
|******************************* | 62%
|
|******************************** | 64%
|
|********************************* | 66%
|
|********************************** | 68%
|
|*********************************** | 70%
|
|************************************ | 72%
|
|************************************* | 74%
|
|************************************** | 76%
|
|*************************************** | 78%
|
|**************************************** | 80%
|
|***************************************** | 82%
|
|****************************************** | 84%
|
|******************************************* | 86%
|
|******************************************** | 88%
|
|********************************************* | 90%
|
|********************************************** | 92%
|
|*********************************************** | 94%
|
|************************************************ | 96%
|
|************************************************* | 98%
|
|**************************************************| 100%
codaSamples <- coda.samples(jagsModel,
variable.names = c("nu","pi",
"omega","kappa",
"eta","theta","Py","Py_tot"),
thin = nThinSteps,
n.iter = ceiling(nUseSteps*nThinSteps/nChains)) # Record sampled values
|
| | 0%
|
|* | 2%
|
|** | 4%
|
|*** | 6%
|
|**** | 8%
|
|***** | 10%
|
|****** | 12%
|
|******* | 14%
|
|******** | 16%
|
|********* | 18%
|
|********** | 20%
|
|*********** | 22%
|
|************ | 24%
|
|************* | 26%
|
|************** | 28%
|
|*************** | 30%
|
|**************** | 32%
|
|***************** | 34%
|
|****************** | 36%
|
|******************* | 38%
|
|******************** | 40%
|
|********************* | 42%
|
|********************** | 44%
|
|*********************** | 46%
|
|************************ | 48%
|
|************************* | 50%
|
|************************** | 52%
|
|*************************** | 54%
|
|**************************** | 56%
|
|***************************** | 58%
|
|****************************** | 60%
|
|******************************* | 62%
|
|******************************** | 64%
|
|********************************* | 66%
|
|********************************** | 68%
|
|*********************************** | 70%
|
|************************************ | 72%
|
|************************************* | 74%
|
|************************************** | 76%
|
|*************************************** | 78%
|
|**************************************** | 80%
|
|***************************************** | 82%
|
|****************************************** | 84%
|
|******************************************* | 86%
|
|******************************************** | 88%
|
|********************************************* | 90%
|
|********************************************** | 92%
|
|*********************************************** | 94%
|
|************************************************ | 96%
|
|************************************************* | 98%
|
|**************************************************| 100%
toc()
117.974 sec elapsed
save(codaSamples, file = paste0("Mcmc.Rdata"))
Using parallel processing.
tic()
runJagsOut <- run.jags( method="parallel",
model="model.txt",
monitor=c("nu","pi","omega","kappa","eta","theta","Py", "Py_tot"),
data=dataList,
inits=initsList,
n.chains=nChains,
adapt=nAdaptSteps,
burnin=nBurninSteps,
sample=ceiling(nUseSteps/nChains),
thin=nThinSteps,
summarise=FALSE,
plots=FALSE )
Calling 4 simulations using the parallel method...
Following the progress of chain 1 (the program will wait for all chains to
finish before continuing):
Welcome to JAGS 4.3.0 on Wed Nov 18 17:52:50 2020
JAGS is free software and comes with ABSOLUTELY NO WARRANTY
Loading module: basemod: ok
Loading module: bugs: ok
. . Reading data file data.txt
. Compiling model graph
Resolving undeclared variables
Allocating nodes
Graph information:
Observed stochastic nodes: 100
Unobserved stochastic nodes: 204
Total graph size: 726
. Reading parameter file inits1.txt
. Initializing model
. Adapting 50000
-------------------------------------------------| 50000
++++++++++++++++++++++++++++++++++++++++++++++++++ 100%
Adaptation successful
. Updating 25000
-------------------------------------------------| 25000
************************************************** 100%
. . . . . . . . . Updating 625000
-------------------------------------------------| 625000
************************************************** 100%
. . . . Updating 0
. Deleting model
.
All chains have finished
Simulation complete. Reading coda files...
Coda files loaded successfully
Finished running the simulation
toc()
301.719 sec elapsed
codaSamples = as.mcmc.list( runJagsOut )
extract(runJagsOut, what='samplers')
plot(codaSamples)
diagMCMC( codaObject=codaSamples, parName="pi[1]" )
diagMCMC( codaObject=codaSamples, parName="pi[2]" )
diagMCMC( codaObject=codaSamples, parName="pi[3]" )
diagMCMC( codaObject=codaSamples, parName="pi[4]" )
diagMCMC( codaObject=codaSamples, parName="pi[5]" )
diagMCMC( codaObject=codaSamples, parName="nu[1]" )
diagMCMC( codaObject=codaSamples, parName="nu[2]" )
diagMCMC( codaObject=codaSamples, parName="nu[3]" )
diagMCMC( codaObject=codaSamples, parName="nu[4]" )
diagMCMC( codaObject=codaSamples, parName="nu[5]" )
diagMCMC( codaObject=codaSamples, parName="omega" )
diagMCMC( codaObject=codaSamples, parName="kappa" )
diagMCMC( codaObject=codaSamples, parName="eta" )
diagMCMC( codaObject=codaSamples, parName="theta" )
traceplot(codaSamples)
plotPost( codaSamples[,"pi[1]"], main="pi[1]", xlab=bquote(pi[1]) )
plotPost( codaSamples[,"pi[2]"], main="pi[2]", xlab=bquote(pi[2]) )
plotPost( codaSamples[,"pi[3]"], main="pi[3]", xlab=bquote(pi[3]) )
plotPost( codaSamples[,"pi[4]"], main="pi[4]", xlab=bquote(pi[4]) )
plotPost( codaSamples[,"pi[5]"], main="pi[5]", xlab=bquote(pi[5]) )
plotPost( codaSamples[,"nu[1]"], main="nu[1]", xlab=bquote(nu[1]) )
plotPost( codaSamples[,"nu[2]"], main="nu[2]", xlab=bquote(nu[2]) )
plotPost( codaSamples[,"nu[3]"], main="nu[3]", xlab=bquote(nu[3]) )
plotPost( codaSamples[,"nu[4]"], main="nu[4]", xlab=bquote(nu[4]) )
plotPost( codaSamples[,"nu[5]"], main="nu[5]", xlab=bquote(nu[5]) )
plotPost( codaSamples[,"omega"], main="omega", xlab=bquote(omega) )
plotPost( codaSamples[,"kappa"], main="kappa", xlab=bquote(kappa) )
plotPost( codaSamples[,"eta"], main="eta", xlab=bquote(eta) )
plotPost( codaSamples[,"theta"], main="theta", xlab=bquote(theta) )
plotPost( codaSamples[,"omega"] , main="omega" , xlab=bquote(omega) ,
cenTend="median" )
plotPost( codaSamples[,"kappa"] , main="kappa" , xlab=bquote(kappa) ,
cenTend="median" )
plotPost( codaSamples[,"eta"] , main="eta" , xlab=bquote(eta) ,
cenTend="median" )
plotPost( codaSamples[,"theta"] , main="theta" , xlab=bquote(theta) ,
cenTend="median" )
summary(codaSamples)
prob_nu
y
sum(y)
library(tidyverse)
library(tidybayes)
codaSamples %>% spread_draws(eta) %>%
median_qi(.width = c(.95, .66)) %>%
ggplot(aes( x = eta, xmin = .lower, xmax = .upper)) +
geom_pointinterval()
codaSamples %>% spread_draws(theta) %>%
median_qi(.width = c(.95, .66)) %>%
ggplot(aes( x = theta, xmin = .lower, xmax = .upper)) +
geom_pointinterval()
codaSamples %>% spread_draws(nu[k]) %>%
median_qi(.width = c(.95, .66)) %>%
ggplot(aes(y = k, x = nu[k], xmin = .lower, xmax = .upper)) +
geom_pointinterval()
codaSamples %>% spread_draws(pi[k]) %>%
median_qi(.width = c(.95, .66)) %>%
ggplot(aes(y = k, x = pi[k], xmin = .lower, xmax = .upper)) +
geom_pointinterval()
codaSamples %>% spread_draws(Py[k]) %>%
median_qi(.width = c(.95, .66)) %>%
ggplot(aes(y = k, x = Py[k], xmin = .lower, xmax = .upper)) +
geom_pointinterval()
codaSamples %>% spread_draws(Py_tot) %>%
median_qi(.width = c(.95, .66)) %>%
ggplot(aes( x = Py_tot, xmin = .lower, xmax = .upper)) +
geom_pointinterval()
codaSamples %>%
spread_draws(nu[k]) %>%
compare_levels(nu, by = k) %>%
ggplot(aes(y = k, x = nu)) +
stat_halfeye()
codaSamples %>%
spread_draws(pi[k]) %>%
compare_levels(pi, by = k) %>%
ggplot(aes(y = k, x = pi)) +
stat_halfeye()
codaSamples %>%
spread_draws(Py[k]) %>%
compare_levels(Py, by = k) %>%
ggplot(aes(y = k, x = Py)) +
stat_halfeye()