rstanarm (version 2.17.3)

bayes_R2.stanreg: Compute a Bayesian version of R-squared for regression models

Description

Compute a Bayesian version of R-squared for regression models

Usage

# S3 method for stanreg
bayes_R2(object, newdata = NULL, re.form = NULL,
  offset = NULL, ...)

Arguments

object

A fitted model object returned by one of the rstanarm modeling functions. See stanreg-objects.

newdata

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.

re.form, offset

For models with group-level terms, these arguments are passed to posterior_linpred if the newdata argument is specified.

...

Currently ignored.

Value

A vector of Bayesian R-squared values with length equal to the posterior sample size.

See Also

https://github.com/jgabry/bayes_R2

Examples

Run this code
# 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 DataCamp Workspace