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 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
[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 1 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 1 0 0 0 0
[124] 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 0 0 0 0 0 0 0 0 0 0
[165] 0 0 1 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 0 0 0 0 0 0 0 0 0 0
[206] 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 1 0 0 0 0 0 0 0 0 0 0 0 0 0
[247] 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 0 0 0 0 0 0 0 0 1 0 0 0
[288] 0 0 0 0 0 0 0 0 0 0 0 0 0
sum(y)
[1] 13
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 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
[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 1 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 1 0 0 0 0
[124] 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 0 0 0 0 0 0 0 0 0 0
[165] 0 0 1 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 0 0 0 0 0 0 0 0 0 0
[206] 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 1 0 0 0 0 0 0 0 0 0 0 0 0 0
[247] 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 0 0 0 0 0 0 0 0 1 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
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 9806.405 0.05722616 0.05607167 0.05390009 0.95 0.03943463 0.07598543 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 6897.901 0.0120943 0.009606438 0.002448983 0.95 7.312177e-08 0.03237006 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 15896.27 0.9098907 0.9101521 0.9108591 0.95 0.8917812 0.9271053 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 13278.71 0.9532227 0.9534628 0.9546648 0.95 0.9411447 0.9646693 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 9806.405 0.05722616 0.05607167 0.05390009 0.95 0.03943463 0.07598543 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 6897.901 0.0120943 0.009606438 0.002448983 0.95 7.312177e-08 0.03237006 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 15896.27 0.9098907 0.9101521 0.9108591 0.95 0.8917812 0.9271053 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 13278.71 0.9532227 0.9534628 0.9546648 0.95 0.9411447 0.9646693 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 17.16944 4.922879 3.114e-02 3.735e-02
eta 0.90989 0.009063 5.732e-05 7.211e-05
nu 0.05723 0.009582 6.060e-05 9.728e-05
pi 0.01209 0.010189 6.444e-05 1.225e-04
theta 0.95322 0.006032 3.815e-05 5.131e-05
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
Py.sum 9.000000 14.000000 17.000000 20.00000 28.00000
eta 0.891352 0.903849 0.910152 0.91624 0.92678
nu 0.041666 0.050487 0.056072 0.06270 0.07917
pi 0.000405 0.004218 0.009606 0.01733 0.03764
theta 0.940792 0.949248 0.953463 0.95742 0.96444