Compute Bayesian R-squared values for qbrms regression models following the method of Gelman et al. (2019). This corrected version properly handles mixed-effects models to match brms output exactly.
bayes_R2(
object,
summary = TRUE,
robust = FALSE,
probs = c(0.025, 0.975),
ndraws = 1000,
newdata = NULL,
verbose = TRUE
)If summary = TRUE, a matrix with summary statistics.
If summary = FALSE, a vector of R-squared values from posterior draws.
A qbrms_fit object.
Logical; if TRUE (default), return summary statistics.
If FALSE, return the posterior draws.
Logical; if TRUE, use robust summary statistics.
Numeric vector of quantiles for summary (default: c(0.025, 0.975)).
Number of posterior draws to use (default: 1000).
Optional data frame for predictions. If NULL, uses
the original data.
Logical; print progress information.
This implementation handles mixed-effects models by:
Using INLA's fitted values that include random effects when available
Correctly sampling random effects from their posterior distributions
Properly accounting for the variance decomposition in mixed models