Get startedGet started for free

Simulating the Normal-Normal in RJAGS

1. Simulating the Normal-Normal in RJAGS

With the pieces of the Normal-Normal model in place, we're ready to simulate the posterior in RJAGS.

2. Sleep study

First, recall that $Y_i$ measures subject $i$'s change in reaction time after 3 days of sleep deprivation. Our assumption is that the variability in $Y_i$ from subject to subject can be modeled by a Normal distribution centered at mean $m$ with standard deviation $s$.

3. Insights from the priors

Our prior models of $m$ and $s$ provide some insight into these parameters. First, built using information from previous sleep studies, the prior model for $m$ suggests that the average change in reaction time is likely positive and around 50 ms. Further, the *standard deviation* in the reaction time changes is equally likely to be anywhere from 0 to 200 ms.

4. Insights from the data (& likelihood)

The observed sleep study data also provide insight into parameters $m$ and $s$. Among the 18 subjects, changes in reaction times ranged from a *decrease* of around 50 ms to an *increase* of more than 100 ms. The *average* change in reaction time was an increase of 26 ms and the changes varied with a standard deviation of 37 ms. Thus, assuming these $Y_i$ data are generated from a Normal model with mean $m$ and standard deviation $s$, they are most likely to have occurred if $m$ and $s$ match these sample features.

5. Posterior insights

The next step in our analysis is to combine the insights from the priors and data (via the likelihood model) to construct a posterior model of parameters m and s. Again, we'll focus on simulating the posterior in RJAGS.

6. DEFINE the Normal-Normal

Recall the three steps of an RJAGS analysis: define, compile, and simulate. To begin, we'll *define* our Bayesian model by the `sleep_model` string.

7. DEFINE the Normal-Normal

Let's start with the Normal likelihood model for changes in reaction time $Y_i$. In our sleep study, we observed $Y_i$ values for each of 18 different subjects.

8. DEFINE the Normal-Normal

We indicate as much using the `for(i in 1:length(Y))` statement where `length(Y)` is 18.

9. DEFINE the Normal-Normal

Inside this for loop, we specify the Normal likelihood model for each of the 18 subjects. The required RJAGS syntax differs from that in base R. Specifically, the RJAGS `dnorm()` function is defined by the mean and *precision*, or inverse variance, of the Normal model. Here the mean is parameter `m` and the precision is the inverse of `s^2`.

10. DEFINE the Normal-Normal

Similar syntax defines the Normal prior model of $m$ with mean `50` and precision equal to the inverse of variance `25^2`.

11. DEFINE the Normal-Normal

Finally, `dunif()` defines the Uniform prior for $s$ on the interval from 0 to 200.

12. COMPILE the Normal-Normal

Next, we *compile* this model using the `jags.model()` function. In the first argument, we provide a `textConnection()` to the defined `sleep_model` string. For the `data` argument, we supply a list of the observed data. Here this is simply the changes in reaction time `Y` stored in the `diff_3` variable of the `sleep_study` data set. Ignore the `inits` argument for now - we'll discuss this later in the chapter.

13. SIMULATE the Normal-Normal

Finally, we *simulate* the posterior by applying `coda.samples()` to the compiled `sleep_jags` model. In doing so, we specify the labels of the `variable.names`, or model parameters (here `m` and `s`). Further, we set `n.iter`, the desired number of iterations or sample size, to 10,000. The results, stored in `sleep_sim`, are an `mcmc.list` object containing an approximate sample of size 10,000 from the posterior.

14. SIMULATE the Normal-Normal

Quick density plots of the 10,000 $m$ and $s$ sample values *approximate* their corresponding posterior models.

15. SIMULATE the Normal-Normal

These posteriors reflect our combined insights from the prior models (shown in teal) and the scaled likelihoods built from the observed sleep study data (shown in light gray). The strong similarity between the posteriors and likelihoods reflects the strong influence that the observed data have on our analysis.

16. Let's practice!

Next, it's your turn to define, compile, simulate, and subsequently examine the Normal-Normal in RJAGS.