Simualted Data

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

Model

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
}
"

Inits

pi_init <- 0.004
eta_init <- 0.91
theta_init <- 0.95

initsList = list( pi=pi_init,
                  eta=eta_init,
                  theta=theta_init) 

Updates

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") )

Examine the chains:

Convergence diagnostics:

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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