R/bayes_R2.R
bayes_R2.stanreg.Rd
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, ...)
object | A fitted model object returned by one of the
rstanarm modeling functions. See |
---|---|
... | Currently ignored. |
re.form | For models with group-level terms, |
A vector of R-squared values with length equal to the posterior sample size (the posterior distribution of R-squared).
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)
fit <- stan_glm( mpg ~ wt + cyl, data = mtcars, QR = TRUE, chains = 2, refresh = 0 ) rsq <- bayes_R2(fit) print(median(rsq))#> [1] 0.814778hist(rsq)loo_rsq <- loo_R2(fit)#> Warning: Some Pareto k diagnostic values are slightly high. See help('pareto-k-diagnostic') for details.#> [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#> [1] 0.7564278#> [1] 0.6226784