Get startedGet started for free

Calculating posterior predictions

You just explored the posterior trend in weight \(Y\) among adults with height \(X = 180\): \(m\)180 \(= a + b * 180\). The weight_chains data frame contains 100,000 posterior plausible values of \(m\)180 that you calculated from the corresponding values of \(a\) and \(b\):

> head(weight_chains, 2)
          a        b        s iter     m_180
1 -113.9029 1.072505 8.772007    1  79.14803
2 -115.0644 1.077914 8.986393    2  78.96014

Forget the trend - what if you wanted to predict the weight of a specific 180 cm tall adult? You can! To do so, you must account for individual variability from the trend, modeled by

\(Y\)180 \(\sim N(m\)180\(, s^2)\)

Using this model, you will simulate predictions of weight under each set of posterior plausible parameters in weight_chains.

This exercise is part of the course

Bayesian Modeling with RJAGS

View Course

Exercise instructions

  • Use rnorm() to simulate a single prediction of weight under the parameter settings in the first row of weight_chains.
  • Repeat the above using the parameter settings in the second row of weight_chains.
  • Simulate a single prediction of weight under each of the 100,000 parameter settings in weight_chains. Store these as a new variable Y_180 in weight_chains.
  • Print the first 6 rows of parameter values & predictions in weight_chains.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Simulate 1 prediction under the first parameter set
rnorm(n = 1, mean = ___, sd = ___)

# Simulate 1 prediction under the second parameter set
rnorm(n = 1, mean = ___, sd = ___)

# Simulate & store 1 prediction under each parameter set
weight_chains <- weight_chains  %>% 
    mutate(Y_180 = rnorm(n = 100000, mean = ___, sd = ___))

# Print the first 6 parameter sets & predictions
Edit and Run Code