Learn R Programming

performance (version 0.1.0)

r2_bayes: Bayesian R2

Description

Compute R2 for Bayesian models. For mixed models (including a random part), it additionally computes the R2 related to the fixed effects only (marginal R2).

Usage

r2_bayes(model, robust = TRUE)

Arguments

model

A Bayesian regression model.

robust

Logical, if TRUE, the median instead of mean is used to calculate the central tendency of the variances.

Value

A list with the Bayesian R2 value. For mixed models, a list with the Bayesian R2 value and the marginal Bayesian R2 value. The standard errors for the R2 values are saved as attributes.

Details

r2_bayes() returns an "unadjusted" R2 value. See r2_loo to calculate a LOO-adjusted R2, which comes conceptionally closer to an adjusted R2 measure.

For mixed models, the conditional and marginal R2 are returned. The marginal R2 considers only the variance of the fixed effects, while the conditional R2 takes both the fixed and random effects into account.

References

Gelman, A., Goodrich, B., Gabry, J., & Vehtari, A. (2018). R-squared for Bayesian regression models. The American Statistician, 1<U+2013>6. 10.1080/00031305.2018.1549100

Examples

Run this code
# NOT RUN {
library(rstanarm)

model <- stan_glm(mpg ~ wt + cyl, data = mtcars, chains = 1, iter = 500)
r2_bayes(model)

model <- stan_lmer(
  Petal.Length ~ Petal.Width + (1 | Species),
  data = iris,
  chains = 1,
  iter = 500
)
r2_bayes(model)

# }
# NOT RUN {
library(brms)
model <- brms::brm(mpg ~ wt + cyl, data = mtcars)
r2_bayes(model)

model <- brms::brm(Petal.Length ~ Petal.Width + (1 | Species), data = iris)
r2_bayes(model)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab