Search code examples
rbayesianrstanhierarchical-bayesianrstanarm

posterior prediction based on a grouping variable from `stan_glm()` in `rstanarm` package?


I was wondering how to obtain the posterior prediction based on a grouping variable from stan_glm() in rstanarm package?

For example, if I have a binary (0, 1) coded grouping variable called "vs" in my data (base R data: mtcars), how can I obtain the prediction for when vs == 0 and when vs == 1?

Here is my R code:

library(rstanarm)
fit <- stan_glm(mpg ~., data = mtcars)

posterior_predict(fit, newdata = WHAT SHOULD BE HERE?)

Solution

  • To explore the effect of e.g. vs on the outcome (in your case mpg) you can use posterior_predict on the subsets where vs == 0 and vs == 1, respectively:

    posterior_predict(fit, newdata = subset(mtcars[1:10, ], vs == 0));
    

    and

    posterior_predict(fit, newdata = subset(mtcars[1:10, ], vs == 1));
    

    More details are given in ?rstanarm::posterior_predict.