Skip to content

Commit f2eac6a

Browse files
committed
loo_R2 generic
@paul-buerkner Decided to add the loo_R2 generic to rstantools instead of the loo package since there won't be a default method and so won't add any functionality to loo. [ci skip]
1 parent 6f5cc38 commit f2eac6a

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ S3method(prior_summary,default)
99
export(bayes_R2)
1010
export(init_cpp)
1111
export(log_lik)
12+
export(loo_R2)
1213
export(loo_linpred)
1314
export(loo_pit)
1415
export(loo_predict)

R/bayes_R2.R

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
#' Generic function and default method for Bayesian R-squared
22
#'
33
#' Generic function and default method for Bayesian version of R-squared for
4-
#' regression models. See \code{bayes_R2.stanreg} in the
5-
#' \pkg{\link[rstanarm]{rstanarm}} package for an example of defining a method.
4+
#' regression models. A generic for LOO-adjusted R-squared is also provided. See
5+
#' \code{bayes_R2.stanreg} in the \pkg{\link[rstanarm]{rstanarm}} package for an
6+
#' example of defining a method.
67
#'
78
#' @export
89
#' @template args-object
910
#' @template args-dots
1011
#'
11-
#' @return \code{bayes_R2} methods should return a vector of length equal to the
12-
#' posterior sample size.
12+
#' @return \code{bayes_R2} and \code{loo_R2} methods should return a vector of
13+
#' length equal to the posterior sample size.
1314
#'
14-
#' The default method just takes \code{object} to be a matrix of y-hat values
15+
#' The default \code{bayes_R2} method just takes \code{object} to be a matrix of y-hat values
1516
#' (one column per observation, one row per posterior draw) and \code{y} to be
1617
#' a vector with length equal to \code{ncol(object)}.
1718
#'
19+
#'
20+
#' @references
21+
#' Andrew Gelman, Ben Goodrich, Jonah Gabry, and Aki Vehtari (2018). R-squared
22+
#' for Bayesian regression models. \emph{The American Statistician}, to appear.
23+
#' \href{http://www.stat.columbia.edu/~gelman/research/published/bayes_R2_v3.pdf}{Preprint},
24+
#' \href{https://avehtari.github.io/bayes_R2/bayes_R2.html}{Notebook}.
25+
#'
26+
#'
1827
#' @template seealso-rstanarm-pkg
1928
#' @template seealso-dev-guidelines
2029
#'
@@ -42,3 +51,9 @@ bayes_R2.default <-
4251
var_ypred / (var_ypred + var_e)
4352
}
4453

54+
55+
#' @rdname bayes_R2
56+
#' @export
57+
loo_R2 <- function(object, ...) {
58+
UseMethod("loo_R2")
59+
}

man/bayes_R2.Rd

+15-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)