Compute a Bayesian version of R-squared for regression models
# S3 method for stanreg
bayes_R2(object, newdata = NULL, re.form = NULL,
offset = NULL, ...)
A fitted model object returned by one of the
rstanarm modeling functions. See stanreg-objects
.
Similar to the newdata
argument to
posterior_linpred
and posterior_predict
except,
in addition to new observations of the predictors, new observations of the
outcome must be also included. See the Examples section below.
For models with group-level terms, these arguments are
passed to posterior_linpred
if the newdata
argument is
specified.
Currently ignored.
A vector of Bayesian R-squared values with length equal to the posterior sample size.
# NOT RUN {
fit <- stan_glm(mpg ~ wt + cyl, data = mtcars, QR = TRUE, chains = 2)
rsq <- bayes_R2(fit)
print(median(rsq))
# specifying newdata (including outcome variable 'mpg')
nd <- data.frame(mpg = c(10, 20, 30), wt = c(4, 3, 2), cyl = c(8, 6, 4))
rsq_new <- bayes_R2(fit, newdata = nd)
print(median(rsq_new))
# multilevel binomial model
if (!exists("example_model")) example(example_model)
print(example_model)
median(bayes_R2(example_model))
median(bayes_R2(example_model, re.form = NA)) # exclude group-level
# }
Run the code above in your browser using DataLab