require(rjags)
source("DBDA2E-utilities.R")
*********************************************************************
Kruschke, J. K. (2015). Doing Bayesian Data Analysis, Second Edition:
A Tutorial with R, JAGS, and Stan. Academic Press / Elsevier.
*********************************************************************
Simulate 0 and 1 data assuming a particular probability of a positive test.
prob_nu <- 0.05
n <- 300
y <- rbinom(n, 1, prob_nu)
y
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
[42] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[83] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0
[124] 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[165] 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
[206] 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0
[247] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[288] 0 0 0 0 0 0 0 0 0 0 0 0 0
sum(y)
[1] 15
JAGS need the data to be in a list of values, vectors, and matrices.
dataList <- list(
y = y,
Ntotal = n
)
dataList
$y
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
[42] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[83] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0
[124] 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[165] 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
[206] 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0
[247] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[288] 0 0 0 0 0 0 0 0 0 0 0 0 0
$Ntotal
[1] 300
modelString <- "
model {
for ( i in 1:Ntotal ) {
y[i] ~ dbern( nu ) # Likelihood in code
}
nu = eta*pi + (1 - theta)*(1-pi)
pi ~ dbeta( 1 , 1 ) # prior
eta ~ dbeta( 910 , 90 ) # prior
theta ~ dbeta( 950 , 50 ) # prior
}
"
pi_init <- 0.004
eta_init <- 0.91
theta_init <- 0.95
initsList = list( pi=pi_init,
eta=eta_init,
theta=theta_init)
jagsModel <- jags.model( file = textConnection(modelString),
data = dataList,
inits = initsList,
n.chains = 5,
n.adapt = 10000 )
Compiling model graph
Resolving undeclared variables
Allocating nodes
Graph information:
Observed stochastic nodes: 300
Unobserved stochastic nodes: 3
Total graph size: 314
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 = 5000 )
|
| | 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","eta","theta"),
n.iter=5000 )
|
| | 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%
save( codaSamples, file=paste0("Mcmc.Rdata") )
plot(codaSamples)
diagMCMC( codaObject=codaSamples, parName="nu" )
diagMCMC( codaObject=codaSamples, parName="pi" )
diagMCMC( codaObject=codaSamples, parName="eta" )
diagMCMC( codaObject=codaSamples, parName="theta" )
traceplot(codaSamples)
plotPost( codaSamples[,"nu"], main="nu", xlab=bquote(nu) )
ESS mean median mode hdiMass hdiLow hdiHigh compVal
nu 11137.86 0.06068431 0.05944169 0.05737693 0.95 0.04207258 0.08201826 NA
pGtCompVal ROPElow ROPEhigh pLtROPE pInROPE pGtROPE
nu NA NA NA NA NA NA
plotPost( codaSamples[,"pi"], main="pi", xlab=bquote(pi) )
ESS mean median mode hdiMass hdiLow hdiHigh compVal
pi 8050.804 0.01502092 0.0126152 0.00361433 0.95 3.10866e-06 0.03733413 NA
pGtCompVal ROPElow ROPEhigh pLtROPE pInROPE pGtROPE
pi NA NA NA NA NA NA
plotPost( codaSamples[,"eta"], main="eta", xlab=bquote(eta) )
ESS mean median mode hdiMass hdiLow hdiHigh compVal
eta 15969.45 0.9100761 0.9102435 0.9097791 0.95 0.8925125 0.927532 NA
pGtCompVal ROPElow ROPEhigh pLtROPE pInROPE pGtROPE
eta NA NA NA NA NA NA
plotPost( codaSamples[,"theta"], main="theta", xlab=bquote(theta) )
ESS mean median mode hdiMass hdiLow hdiHigh compVal
theta 13736.83 0.9522833 0.952504 0.9540947 0.95 0.9397764 0.9639612 NA
pGtCompVal ROPElow ROPEhigh pLtROPE pInROPE pGtROPE
theta NA NA NA NA NA NA
plotPost( codaSamples[,"nu"] , main="nu" , xlab=bquote(nu) ,
cenTend="median" )
ESS mean median mode hdiMass hdiLow hdiHigh compVal
nu 11137.86 0.06068431 0.05944169 0.05737693 0.95 0.04207258 0.08201826 NA
pGtCompVal ROPElow ROPEhigh pLtROPE pInROPE pGtROPE
nu NA NA NA NA NA NA
plotPost( codaSamples[,"pi"] , main="pi" , xlab=bquote(pi) ,
cenTend="median" )
ESS mean median mode hdiMass hdiLow hdiHigh compVal
pi 8050.804 0.01502092 0.0126152 0.00361433 0.95 3.10866e-06 0.03733413 NA
pGtCompVal ROPElow ROPEhigh pLtROPE pInROPE pGtROPE
pi NA NA NA NA NA NA
plotPost( codaSamples[,"eta"] , main="eta" , xlab=bquote(eta) ,
cenTend="median" )
ESS mean median mode hdiMass hdiLow hdiHigh compVal
eta 15969.45 0.9100761 0.9102435 0.9097791 0.95 0.8925125 0.927532 NA
pGtCompVal ROPElow ROPEhigh pLtROPE pInROPE pGtROPE
eta NA NA NA NA NA NA
plotPost( codaSamples[,"theta"] , main="theta" , xlab=bquote(theta) ,
cenTend="median" )
ESS mean median mode hdiMass hdiLow hdiHigh compVal
theta 13736.83 0.9522833 0.952504 0.9540947 0.95 0.9397764 0.9639612 NA
pGtCompVal ROPElow ROPEhigh pLtROPE pInROPE pGtROPE
theta NA NA NA NA NA NA
summary(codaSamples)
Iterations = 15001:20000
Thinning interval = 1
Number of chains = 5
Sample size per chain = 5000
1. Empirical mean and standard deviation for each variable,
plus standard error of the mean:
Mean SD Naive SE Time-series SE
eta 0.91008 0.008988 5.685e-05 7.142e-05
nu 0.06068 0.010493 6.636e-05 1.001e-04
pi 0.01502 0.011555 7.308e-05 1.281e-04
theta 0.95228 0.006192 3.916e-05 5.289e-05
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
eta 0.8918750 0.904141 0.91024 0.91626 0.92701
nu 0.0437437 0.053228 0.05944 0.06682 0.08465
pi 0.0006201 0.005885 0.01262 0.02153 0.04336
theta 0.9395667 0.948251 0.95250 0.95655 0.96379