rstanarm으로 배우는 Bayesian 회귀 모델링
Jake Thompson
Psychometrician, ATLAS, University of Kansas
stan_model <- stan_glm(kid_score ~ mom_iq + mom_hs, data = kidiq) predict_data <- data.frame(mom_iq = 110, mom_hs = c(0, 1))posterior <- posterior_predict(stan_model, newdata = predict_data) posterior[1:10,]
1 2
[1,] 76.75484 96.26407
[2,] 74.39001 100.38898
[3,] 90.90370 70.00591
[4,] 70.43835 120.82787
[5,] 113.98411 82.40497
[6,] 56.15829 121.84269
[7,] 90.46640 92.77966
[8,] 98.56337 110.17948
[9,] 108.86147 123.67762
[10,] 94.29429 83.77102
posterior <- as.data.frame(posterior)colnames(posterior) <- c("고졸 아님", "고졸")plot_posterior <- gather(posterior, key = "HS", value = "predict")head(plot_posterior)
HS predict
1 고졸 아님 76.75484
2 고졸 아님 74.39001
3 고졸 아님 90.90370
4 고졸 아님 70.43835
5 고졸 113.98411
6 고졸 아님 56.15829
ggplot(plot_posterior, aes(x = predict)) +
facet_wrap(~ HS, ncol = 1) +
geom_density()

rstanarm으로 배우는 Bayesian 회귀 모델링