Skip to content

Commit

Permalink
fix R cmd check notes and revert name change
Browse files Browse the repository at this point in the history
brms imports some of these functions so change their names back to include the `.` to avoid breaking brms
  • Loading branch information
jgabry committed Feb 7, 2024
1 parent 0d53b04 commit df94bee
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 20 deletions.
17 changes: 10 additions & 7 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
S3method("$",loo)
S3method("[",loo)
S3method("[[",loo)
S3method(.compute_point_estimate,default)
S3method(.compute_point_estimate,matrix)
S3method(.ndraws,default)
S3method(.ndraws,matrix)
S3method(.thin_draws,matrix)
S3method(.thin_draws,numeric)
S3method(E_loo,default)
S3method(E_loo,matrix)
S3method(E_loo_khat,default)
Expand All @@ -14,8 +20,6 @@ S3method(as.psis_loo,psis_loo)
S3method(as.psis_loo,psis_loo_ss)
S3method(as.psis_loo_ss,psis_loo)
S3method(as.psis_loo_ss,psis_loo_ss)
S3method(compute_point_estimate,default)
S3method(compute_point_estimate,matrix)
S3method(crps,matrix)
S3method(crps,numeric)
S3method(dim,importance_sampling)
Expand All @@ -42,8 +46,6 @@ S3method(loo_moment_match,default)
S3method(loo_predictive_metric,matrix)
S3method(loo_scrps,matrix)
S3method(loo_subsample,"function")
S3method(n_draws,default)
S3method(n_draws,matrix)
S3method(nobs,psis_loo_ss)
S3method(plot,loo)
S3method(plot,psis)
Expand Down Expand Up @@ -83,9 +85,6 @@ S3method(scrps,numeric)
S3method(sis,array)
S3method(sis,default)
S3method(sis,matrix)
S3method(thin_draws,default)
S3method(thin_draws,matrix)
S3method(thin_draws,numeric)
S3method(tis,array)
S3method(tis,default)
S3method(tis,matrix)
Expand All @@ -94,6 +93,9 @@ S3method(waic,"function")
S3method(waic,array)
S3method(waic,matrix)
S3method(weights,importance_sampling)
export(.compute_point_estimate)
export(.ndraws)
export(.thin_draws)
export(E_loo)
export(compare)
export(crps)
Expand Down Expand Up @@ -149,6 +151,7 @@ export(relative_eff)
export(scrps)
export(sis)
export(stacking_weights)
export(thin_draws.default)
export(tis)
export(waic)
export(waic.array)
Expand Down
35 changes: 22 additions & 13 deletions R/loo_subsample.R
Original file line number Diff line number Diff line change
Expand Up @@ -620,52 +620,58 @@ elpd_loo_approximation <- function(.llfun, data, draws, cores, loo_approximation

#' Compute a point estimate from a draws object
#'
#' @noRd
#' @keywords internal
#' @export
#' @details This is a generic function to compute point estimates from draws
#' objects. The function is internal and should only be used by developers to
#' enable [loo_subsample()] for arbitrary draws objects.
#'
#' @param draws A draws object with draws from the posterior.
#' @return A 1 by P matrix with point estimates from a draws object.
compute_point_estimate <- function(draws) {
.compute_point_estimate <- function(draws) {
UseMethod("compute_point_estimate")
}
#' @rdname dot-compute_point_estimate
#' @export
compute_point_estimate.matrix <- function(draws) {
.compute_point_estimate.matrix <- function(draws) {
t(as.matrix(colMeans(draws)))
}
#' @rdname dot-compute_point_estimate
#' @export
compute_point_estimate.default <- function(draws) {
.compute_point_estimate.default <- function(draws) {
stop("compute_point_estimate() has not been implemented for objects of class '", class(draws), "'")
}

#' Thin a draws object
#'
#' @noRd
#' @keywords internal
#' @export
#' @details This is a generic function to thin draws from arbitrary draws
#' objects. The function is internal and should only be used by developers to
#' enable [loo_subsample()] for arbitrary draws objects.
#'
#' @param draws A draws object with posterior draws.
#' @param loo_approximation_draws The number of posterior draws to return (ie after thinning).
#' @return A thinned draws object.
thin_draws <- function(draws, loo_approximation_draws) {
.thin_draws <- function(draws, loo_approximation_draws) {
UseMethod("thin_draws")
}

#' @rdname dot-thin_draws
#' @export
thin_draws.matrix <- function(draws, loo_approximation_draws) {
.thin_draws.matrix <- function(draws, loo_approximation_draws) {
if (is.null(loo_approximation_draws)) return(draws)
checkmate::assert_int(loo_approximation_draws, lower = 1, upper = n_draws(draws), null.ok = TRUE)
S <- n_draws(draws)
idx <- 1:loo_approximation_draws * S %/% loo_approximation_draws
draws <- draws[idx, , drop = FALSE]
draws
}
#' @rdname dot-thin_draws
#' @export
thin_draws.numeric <- function(draws, loo_approximation_draws) {
.thin_draws.numeric <- function(draws, loo_approximation_draws) {
thin_draws.matrix(as.matrix(draws), loo_approximation_draws)
}
#' @rdname dot-thin_draws
#' @export
thin_draws.default <- function(draws, loo_approximation_draws) {
stop("thin_draws() has not been implemented for objects of class '", class(draws), "'")
Expand All @@ -674,22 +680,25 @@ thin_draws.default <- function(draws, loo_approximation_draws) {

#' The number of posterior draws in a draws object.
#'
#' @noRd
#' @keywords internal
#' @export
#' @details This is a generic function to return the total number of draws from
#' an arbitrary draws objects. The function is internal and should only be
#' used by developers to enable [loo_subsample()] for arbitrary draws objects.
#'
#' @param x A draws object with posterior draws.
#' @return An integer with the number of draws.
n_draws <- function(x) {
.ndraws <- function(x) {
UseMethod("n_draws")
}
#' @rdname dot-ndraws
#' @export
n_draws.matrix <- function(x) {
.ndraws.matrix <- function(x) {
nrow(x)
}
#' @rdname dot-ndraws
#' @export
n_draws.default <- function(x) {
.ndraws.default <- function(x) {
stop("n_draws() has not been implemented for objects of class '", class(x), "'")
}

Expand Down
29 changes: 29 additions & 0 deletions man/dot-compute_point_estimate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions man/dot-ndraws.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions man/dot-thin_draws.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit df94bee

Please sign in to comment.