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.
*********************************************************************
Now simulate the sum assuming a particular probability of a positive test.
prob_nu <- 0.05
n <- 300
y <- rbinom(1, n, prob_nu)
y
[1] 17
JAGS need the data to be in a list of values, vectors, and matrices.
dataList <- list(
y = y,
Ntotal = n
)
dataList
$y
[1] 17
$Ntotal
[1] 300
modelString <- "
model {
y ~ dbin( nu, Ntotal ) # 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
Py ~ dbin( nu, Ntotal ) # Predictive distribution
}
"
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: 1
Unobserved stochastic nodes: 4
Total graph size: 16
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"),
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 12356.67 0.06463734 0.06328798 0.06066284 0.95 0.04428892 0.08819491 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 9218.618 0.0186843 0.01635624 0.008952885 0.95 2.4113e-06 0.04396999 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 14914.22 0.9098209 0.910141 0.9122019 0.95 0.8921015 0.9273443 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 13773.21 0.9514744 0.9517419 0.9524055 0.95 0.9387616 0.9633218 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 12356.67 0.06463734 0.06328798 0.06066284 0.95 0.04428892 0.08819491 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 9218.618 0.0186843 0.01635624 0.008952885 0.95 2.4113e-06 0.04396999 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 14914.22 0.9098209 0.910141 0.9122019 0.95 0.8921015 0.9273443 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 13773.21 0.9514744 0.9517419 0.9524055 0.95 0.9387616 0.9633218 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 19.38396 5.510418 3.485e-02 4.199e-02
eta 0.90982 0.009005 5.695e-05 7.358e-05
nu 0.06464 0.011569 7.317e-05 1.050e-04
pi 0.01868 0.013310 8.418e-05 1.389e-04
theta 0.95147 0.006287 3.976e-05 5.411e-05
2. Quantiles for each variable:
2.5% 25% 50% 75% 97.5%
Py 1.000e+01 16.000000 19.00000 23.00000 31.00000
eta 8.913e-01 0.903921 0.91014 0.91608 0.92671
nu 4.593e-02 0.056379 0.06329 0.07149 0.09106
pi 9.123e-04 0.008241 0.01636 0.02643 0.05026
theta 9.384e-01 0.947386 0.95174 0.95579 0.96311