Learn R Programming

qbrms (version 1.0.1)

bayes_R2: Bayesian R-squared for qbrms Models

Description

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.

Usage

bayes_R2(
  object,
  summary = TRUE,
  robust = FALSE,
  probs = c(0.025, 0.975),
  ndraws = 1000,
  newdata = NULL,
  verbose = TRUE
)

Value

If summary = TRUE, a matrix with summary statistics. If summary = FALSE, a vector of R-squared values from posterior draws.

Arguments

object

A qbrms_fit object.

summary

Logical; if TRUE (default), return summary statistics. If FALSE, return the posterior draws.

robust

Logical; if TRUE, use robust summary statistics.

probs

Numeric vector of quantiles for summary (default: c(0.025, 0.975)).

ndraws

Number of posterior draws to use (default: 1000).

newdata

Optional data frame for predictions. If NULL, uses the original data.

verbose

Logical; print progress information.

Details

This implementation handles mixed-effects models by:

  1. Using INLA's fitted values that include random effects when available

  2. Correctly sampling random effects from their posterior distributions

  3. Properly accounting for the variance decomposition in mixed models