0

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?)
rnorouzian
  • 7,397
  • 5
  • 27
  • 72
  • 1
    @akrun, that's fine it's a package question, thank you. – rnorouzian Mar 21 '18 at 03:17
  • `newdata` should be a dataframe containing sets of values for each predictor that you're interested in. So you need a `newdata` dataframe where some rows have `vs = 0` and some rows have `vs = 1`, you have to figure out what the values of the other predictors should be to give meaningful comparisons. – Marius Mar 21 '18 at 03:22

1 Answers1

0

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.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • @rnorouzian I don't see why. I would always `predict` on the full (new)data; then you can pull out regions of interest in your predictor/parameter space. – Maurits Evers Mar 21 '18 at 04:00
  • @rnorouzian Ok, I'm beginning to understand; if you're interested in looking at the effect of e.g. `vs` on the outcome `mpg` through the ppd, then you are right in using `posterior_predict` on e.g. `subset(mtcars, vs == 0)` and `subset(mtcars, vs == 1)` separately. – Maurits Evers Mar 21 '18 at 04:13
  • @rnorouzian ?? Yes I am sure. To quote from `?rstanarm::posterior_predict`: "Drawing from the posterior predictive distribution at interesting values of the predictors also lets us visualize how a manipulation of a predictor affects (a function of) the outcome(s)." – Maurits Evers Mar 21 '18 at 04:27