Visualizing a Bayesian model

Bayesian Regression Modeling with rstanarm

Jake Thompson

Psychometrician, ATLAS, University of Kansas

stan_model <- stan_glm(kid_score ~ mom_iq, data = kidiq)
tidy(stan_model)
# A tibble: 2 x 3
  term        estimate std.error
  <chr>          <dbl>     <dbl>
1 (Intercept)   25.7      5.92  
2 mom_iq         0.611    0.0590
tidy_coef <- tidy(stan_model)
model_intercept <- tidy_coef$estimate[1]
model_intercept
model_slope <- tidy_coef$estimate[2]
model_slope
25.67857
0.6110473
Bayesian Regression Modeling with rstanarm

Creating a plot

ggplot(kidiq, aes(x = mom_iq, y = kid_score)) + 
  geom_point() +
  geom_abline(intercept = model_intercept, slope = model_slope)

Bayesian Regression Modeling with rstanarm

Plotting uncertainty

draws <- spread_draws(stan_model, `(Intercept)`, mom_iq)
draws
# A tibble: 4,000 x 5
   .chain .iteration .draw `(Intercept)` mom_iq
    <int>      <int> <int>         <dbl>  <dbl>
 1      1          1     1          28.2  0.586
 2      1          2     2          28.7  0.593
 3      1          3     3          13.5  0.735
 4      1          4     4          30.3  0.564
 5      1          5     5          34.5  0.522
 6      1          6     6          19.2  0.669
 7      1          7     7          34.8  0.523
 8      1          8     8          16.3  0.707
# ... with 3,992 more rows
Bayesian Regression Modeling with rstanarm

Plotting uncertainty

ggplot(kidiq, aes(x = mom_iq, y = kid_score)) + 
  geom_point()

Bayesian Regression Modeling with rstanarm

Plotting uncertainty

ggplot(kidiq, aes(x = mom_iq, y = kid_score)) + 
  geom_point()
  geom_abline(data = draws, aes(intercept = `(Intercept)`, slope = mom_iq),
    size = 0.2, alpha = 0.1, color = "skyblue")

Bayesian Regression Modeling with rstanarm

Plotting uncertainty

ggplot(kidiq, aes(x = mom_iq, y = kid_score)) + 
  geom_point()
  geom_abline(data = draws, aes(intercept = `(Intercept)`, slope = mom_iq),
    size = 0.2, alpha = 0.1, color = "skyblue") +
  geom_abline(intercept = model_intercept, slope = model_slope)

Bayesian Regression Modeling with rstanarm

Let's practice

Bayesian Regression Modeling with rstanarm

Preparing Video For Download...