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). While r2_bayes()
returns a single R2 value,
r2_posterior()
returns a posterior sample of Bayesian R2 values.
Usage
r2_bayes(model, robust = TRUE, ci = 0.95, verbose = TRUE, ...)
r2_posterior(model, ...)
# S3 method for brmsfit
r2_posterior(model, verbose = TRUE, ...)
# S3 method for stanreg
r2_posterior(model, verbose = TRUE, ...)
# S3 method for BFBayesFactor
r2_posterior(model, average = FALSE, prior_odds = NULL, verbose = TRUE, ...)
Arguments
- model
A Bayesian regression model (from brms, rstanarm, BayesFactor, etc).
- robust
Logical, if
TRUE
, the median instead of mean is used to calculate the central tendency of the variances.- ci
Value or vector of probability of the CI (between 0 and 1) to be estimated.
- verbose
Toggle off warnings.
- ...
Arguments passed to
r2_posterior()
.- average
Compute model-averaged index? See
bayestestR::weighted_posteriors()
.- prior_odds
Optional vector of prior odds for the models compared to the first model (or the denominator, for
BFBayesFactor
objects). Fordata.frame
s, this will be used as the basis of weighting.
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 and credible intervals 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
conceptually 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.
r2_posterior()
is the actual workhorse for r2_bayes()
and
returns a posterior sample of Bayesian R2 values.
References
Gelman, A., Goodrich, B., Gabry, J., and Vehtari, A. (2018). R-squared for Bayesian regression models. The American Statistician, 1–6. doi:10.1080/00031305.2018.1549100
Examples
library(performance)
if (require("rstanarm") && require("rstantools")) {
model <- suppressWarnings(stan_glm(
mpg ~ wt + cyl,
data = mtcars,
chains = 1,
iter = 500,
refresh = 0,
show_messages = FALSE
))
r2_bayes(model)
model <- suppressWarnings(stan_lmer(
Petal.Length ~ Petal.Width + (1 | Species),
data = iris,
chains = 1,
iter = 500,
refresh = 0
))
r2_bayes(model)
}
#> # Bayesian R2 with Compatibility Interval
#>
#> Conditional R2: 0.953 (95% CI [0.942, 0.966])
#> Marginal R2: 0.824 (95% CI [0.737, 0.899])
if (require("BayesFactor")) {
BFM <- generalTestBF(mpg ~ qsec + gear, data = mtcars, progress = FALSE)
FM <- lmBF(mpg ~ qsec + gear, data = mtcars)
r2_bayes(FM)
r2_bayes(BFM[3])
r2_bayes(BFM, average = TRUE) # across all models
# with random effects:
mtcars$gear <- factor(mtcars$gear)
model <- lmBF(
mpg ~ hp + cyl + gear + gear:wt,
mtcars,
progress = FALSE,
whichRandom = c("gear", "gear:wt")
)
r2_bayes(model)
}
#> # Bayesian R2 with Compatibility Interval
#>
#> Conditional R2: 0.367 (95% CI [0.260, 0.633])
#> Marginal R2: 0.209 (95% CI [2.871e-05, 0.500])
# \donttest{
if (require("brms")) {
model <- suppressWarnings(brms::brm(
mpg ~ wt + cyl,
data = mtcars,
silent = 2,
refresh = 0
))
r2_bayes(model)
model <- suppressWarnings(brms::brm(
Petal.Length ~ Petal.Width + (1 | Species),
data = iris,
silent = 2,
refresh = 0
))
r2_bayes(model)
}
#> Loading required package: brms
#> Loading 'brms' package (version 2.20.4). Useful instructions
#> can be found by typing help('brms'). A more detailed introduction
#> to the package is available through vignette('brms_overview').
#>
#> Attaching package: ‘brms’
#> The following objects are masked from ‘package:rstanarm’:
#>
#> dirichlet, exponential, get_y, lasso, ngrps
#> The following object is masked from ‘package:mclust’:
#>
#> me
#> The following object is masked from ‘package:psych’:
#>
#> cs
#> The following object is masked from ‘package:lme4’:
#>
#> ngrps
#> The following object is masked from ‘package:stats’:
#>
#> ar
#> # Bayesian R2 with Compatibility Interval
#>
#> Conditional R2: 0.954 (95% CI [0.951, 0.957])
#> Marginal R2: 0.386 (95% CI [0.171, 0.590])
# }