Get startedGet started for free

Visualizing the regression priors

In the previous exercise, you simulated 10,000 samples for each parameter (\(a\), \(b\), \(s\)) in the Bayesian regression model of weight \(Y\) by height \(X\): \(Y \sim N(m, s^2)\) with mean \(m = a + bX\). The set of \(a\), \(b\), and \(s\) values in each row of samples represents a prior plausible regression scenario. To explore the scope of these prior scenarios, you will simulate 50 pairs of height and weight values from each of the first 12 sets of prior parameters \(a\), \(b\), and \(s\).

This exercise is part of the course

Bayesian Modeling with RJAGS

View Course

Exercise instructions

  • Create a data frame prior_simulation which includes n = 50 replicates of the first 12 sets of prior parameters in samples (600 rows in total!).
  • For each of the 600 prior_simulation rows:
    • Simulate a height value from a \(N(170, 10^2)\) model.
    • Simulate a weight value from \(N(a + b X, s^2)\) where \(X\) is height and \((a,b,s)\) are the prior parameter set.
  • You now have 50 simulated height and weight pairs for each of the 12 parameter sets. Use ggplot() to construct a scatterplot of these 50 pairs for each set of parameter values. Be sure to put weight on the y-axis!

Hands-on interactive exercise

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

# Replicate the first 12 parameter sets 50 times each
prior_scenarios_rep <- bind_rows(replicate(n = ___, expr = samples[1:12, ], simplify = FALSE)) 

# Simulate 50 height & weight data points for each parameter set
prior_simulation <- prior_scenarios_rep %>% 
    mutate(height = rnorm(n = 600, mean = ___, sd = ___)) %>% 
    mutate(weight = rnorm(n = 600, mean = ___, sd = ___))

# Plot the simulated data & regression model for each parameter set
ggplot(prior_simulation, aes(x = ___, y = ___)) + 
    geom_point() + 
    geom_smooth(method = "lm", se = FALSE, size = 0.75) + 
    facet_wrap(~ set)
Edit and Run Code