Compute a Bayesian version of R-squared or LOO-adjusted R-squared for regression models.

# S3 method for stanreg
bayes_R2(object, ..., re.form = NULL)

# S3 method for stanreg
loo_R2(object, ...)

Arguments

object

A fitted model object returned by one of the rstanarm modeling functions. See stanreg-objects.

...

Currently ignored.

re.form

For models with group-level terms, re.form is passed to posterior_epred if specified.

Value

A vector of R-squared values with length equal to the posterior sample size (the posterior distribution of R-squared).

References

Andrew Gelman, Ben Goodrich, Jonah Gabry, and Aki Vehtari (2018). R-squared for Bayesian regression models. The American Statistician, to appear. DOI: 10.1080/00031305.2018.1549100. (Journal, Preprint, Notebook)

Examples

fit <- stan_glm( mpg ~ wt + cyl, data = mtcars, QR = TRUE, chains = 2, refresh = 0 ) rsq <- bayes_R2(fit) print(median(rsq))
#> [1] 0.814778
hist(rsq)
loo_rsq <- loo_R2(fit)
#> Warning: Some Pareto k diagnostic values are slightly high. See help('pareto-k-diagnostic') for details.
print(median(loo_rsq))
#> [1] 0.7951127
# multilevel binomial model if (!exists("example_model")) example(example_model) print(example_model)
#> stan_glmer #> family: binomial [logit] #> formula: cbind(incidence, size - incidence) ~ size + period + (1 | herd) #> observations: 56 #> ------ #> Median MAD_SD #> (Intercept) -1.5 0.6 #> size 0.0 0.0 #> period2 -1.0 0.3 #> period3 -1.1 0.3 #> period4 -1.5 0.5 #> #> Error terms: #> Groups Name Std.Dev. #> herd (Intercept) 0.77 #> Num. levels: herd 15 #> #> ------ #> * For help interpreting the printed output see ?print.stanreg #> * For info on the priors used see ?prior_summary.stanreg
median(bayes_R2(example_model))
#> [1] 0.7564278
median(bayes_R2(example_model, re.form = NA)) # exclude group-level
#> [1] 0.6226784