
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).
r2_bayes(model, robust = TRUE, ci = 0.89)
A Bayesian regression model.
Logical, if TRUE
, the median instead of mean is used to
calculate the central tendency of the variances.
Value or vector of probability of the CI (between 0 and 1) to be estimated.
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 and credible intervals for the R2 values are saved as attributes.
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.
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
# NOT RUN {
library(performance)
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