Bayesian Regression Modeling with rstanarm
Jake Thompson
Psychometrician, ATLAS, University of Kansas
stan_model <- stan_glm(kid_score ~ mom_iq + mom_hs, data = kidiq)
posteriors <- posterior_predict(stan_model) posteriors[1:10, 1:5]
1 2 3 4 5
[1,] 61.08989 58.57298 80.68946 101.00810 76.37946
[2,] 111.52704 49.92284 99.09657 97.33291 72.98906
[3,] 83.36793 81.35768 94.16414 101.73570 64.69375
[4,] 118.15092 74.00476 107.28852 75.75912 91.93991
[5,] 103.95042 58.98491 128.40312 121.42753 62.70008
[6,] 102.29874 127.74050 84.10661 67.94056 82.02546
[7,] 91.39445 88.49029 75.05702 94.48594 102.50331
[8,] 93.33446 84.99589 101.49261 66.74698 68.26968
[9,] 101.85065 91.46998 123.43011 76.53226 74.93288
[10,] 79.61489 101.29745 105.97636 97.48332 99.80582
predict_data <- data.frame(
mom_iq = 110,
mom_hs = c(0, 1))
predict_data
mom_iq mom_hs
1 110 0
2 110 1
new_predictions <- posterior_predict(stan_model, newdata = predict_data)
new_predictions[1:10,]
1 2
[1,] 90.90581 107.75710
[2,] 78.72466 139.86677
[3,] 80.67743 88.81523
[4,] 83.47852 74.06063
[5,] 69.07708 87.81177
[6,] 40.46229 85.45969
[7,] 79.41597 64.19011
[8,] 107.93867 117.49345
[9,] 95.31493 82.51476
[10,] 91.18056 94.22732
summary(new_predictions[, 1])
Min. 1st Qu. Median Mean 3rd Qu. Max.
20.90 75.26 87.64 87.68 100.02 156.00
summary(new_predictions[, 2])
Min. 1st Qu. Median Mean 3rd Qu. Max.
34.78 81.32 93.49 93.66 105.62 159.82
Bayesian Regression Modeling with rstanarm