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 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
[42] 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0
[83] 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 0 0 0 0 0 0 0 0 0 0 0 0 0
[124] 0 1 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 1 0 0 0 0 0 0 1 0 0 0 0 1 0
[165] 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 1 1 0 0
[206] 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 1 0 0 0 0 0 0 1 0 0 0 0 0
[247] 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 0 0 0 0 0 0 0
[288] 0 0 0 0 0 0 0 1 0 1 0 0 1
sum(y)
[1] 22
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 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
[42] 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0
[83] 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 0 0 0 0 0 0 0 0 0 0 0 0 0
[124] 0 1 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 1 0 0 0 0 0 0 1 0 0 0 0 1 0
[165] 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 1 1 0 0
[206] 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 1 0 0 0 0 0 0 1 0 0 0 0 0
[247] 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 0 0 0 0 0 0 0
[288] 0 0 0 0 0 0 0 1 0 1 0 0 1
$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
for ( i in 1:Ntotal ) {
Py[i] ~ dbern( nu ) # Predictive distribution
}
Py.sum = sum(Py)
}
"
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: 303
Total graph size: 616
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","Py.sum"),
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 13331.11 0.07750616 0.07620031 0.07244335 0.95 0.05029071 0.1053655 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 10501.35 0.03235293 0.03055315 0.02857568 0.95 4.78399e-05 0.06461207 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 15267.26 0.9098999 0.9100733 0.909354 0.95 0.8924607 0.9275888 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 11836.43 0.9503674 0.9505879 0.9511688 0.95 0.9370366 0.9633996 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 13331.11 0.07750616 0.07620031 0.07244335 0.95 0.05029071 0.1053655 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 10501.35 0.03235293 0.03055315 0.02857568 0.95 4.78399e-05 0.06461207 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 15267.26 0.9098999 0.9100733 0.909354 0.95 0.8924607 0.9275888 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 11836.43 0.9503674 0.9505879 0.9511688 0.95 0.9370366 0.9633996 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
Py.sum 23.26132 6.335329 4.007e-02 4.693e-02
eta 0.90990 0.009064 5.733e-05 7.408e-05
nu 0.07751 0.014508 9.176e-05 1.239e-04
pi 0.03235 0.017942 1.135e-04 1.754e-04
theta 0.95037 0.006721 4.250e-05 6.233e-05
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
Py.sum 12.000000 19.00000 23.00000 27.00000 37.00000
eta 0.891546 0.90395 0.91007 0.91618 0.92693
nu 0.053029 0.06699 0.07620 0.08650 0.10963
pi 0.003448 0.01889 0.03055 0.04375 0.07188
theta 0.936477 0.94592 0.95059 0.95505 0.96293