ComenzarEmpieza gratis

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\).

Este ejercicio forma parte del curso

Bayesian Modeling with RJAGS

Ver curso

Instrucciones del ejercicio

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

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

# 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)
Editar y ejecutar código