diff --git a/NAMESPACE b/NAMESPACE index b2636522..bfb7ac67 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,6 +7,7 @@ S3method(.compute_point_estimate,default) S3method(.compute_point_estimate,matrix) S3method(.ndraws,default) S3method(.ndraws,matrix) +S3method(.thin_draws,default) S3method(.thin_draws,matrix) S3method(.thin_draws,numeric) S3method(E_loo,default) @@ -151,7 +152,6 @@ export(relative_eff) export(scrps) export(sis) export(stacking_weights) -export(thin_draws.default) export(tis) export(waic) export(waic.array) diff --git a/R/loo_subsample.R b/R/loo_subsample.R index f9a875da..372f582c 100644 --- a/R/loo_subsample.R +++ b/R/loo_subsample.R @@ -138,7 +138,7 @@ loo_subsample.function <- cores <- loo_cores(cores) checkmate::assert_choice(loo_approximation, choices = loo_approximation_choices(), null.ok = FALSE) - checkmate::assert_int(loo_approximation_draws, lower = 1, upper = n_draws(draws), null.ok = TRUE) + checkmate::assert_int(loo_approximation_draws, lower = 1, upper = .ndraws(draws), null.ok = TRUE) checkmate::assert_choice(estimator, choices = estimator_choices()) .llgrad <- .llhess <- NULL @@ -234,7 +234,7 @@ loo_subsample.function <- .llgrad = .llgrad, .llhess = .llhess, data_dim = dim(data), - ndraws = n_draws(draws)) + ndraws = .ndraws(draws)) loo_ss } @@ -537,20 +537,20 @@ elpd_loo_approximation <- function(.llfun, data, draws, cores, loo_approximation if (loo_approximation == "none") return(rep(1L,N)) if (loo_approximation %in% c("tis", "sis")) { - draws <- thin_draws(draws, loo_approximation_draws) + draws <- .thin_draws(draws, loo_approximation_draws) is_values <- suppressWarnings(loo.function(.llfun, data = data, draws = draws, is_method = loo_approximation)) return(is_values$pointwise[, "elpd_loo"]) } if (loo_approximation == "waic") { - draws <- thin_draws(draws, loo_approximation_draws) + draws <- .thin_draws(draws, loo_approximation_draws) waic_full_obj <- waic.function(.llfun, data = data, draws = draws) return(waic_full_obj$pointwise[,"elpd_waic"]) } # Compute the lpd or log p(y_i|y_{-i}) if (loo_approximation == "lpd") { - draws <- thin_draws(draws, loo_approximation_draws) + draws <- .thin_draws(draws, loo_approximation_draws) lpds <- compute_lpds(N, data, draws, .llfun, cores) return(lpds) # Use only the lpd } @@ -561,8 +561,8 @@ elpd_loo_approximation <- function(.llfun, data, draws, cores, loo_approximation loo_approximation == "waic_grad_marginal" | loo_approximation == "waic_hess") { - draws <- thin_draws(draws, loo_approximation_draws) - point_est <- compute_point_estimate(draws) + draws <- .thin_draws(draws, loo_approximation_draws) + point_est <- .compute_point_estimate(draws) lpds <- compute_lpds(N, data, point_est, .llfun, cores) if (loo_approximation == "plpd") return(lpds) # Use only the lpd } @@ -572,7 +572,7 @@ elpd_loo_approximation <- function(.llfun, data, draws, cores, loo_approximation loo_approximation == "waic_hess") { checkmate::assert_true(!is.null(.llgrad)) - point_est <- compute_point_estimate(draws) + point_est <- .compute_point_estimate(draws) # Compute the lpds lpds <- compute_lpds(N, data, point_est, .llfun, cores) @@ -629,7 +629,7 @@ elpd_loo_approximation <- function(.llfun, data, draws, cores, loo_approximation #' @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) { - UseMethod("compute_point_estimate") + UseMethod(".compute_point_estimate") } #' @rdname dot-compute_point_estimate #' @export @@ -639,7 +639,7 @@ elpd_loo_approximation <- function(.llfun, data, draws, cores, loo_approximation #' @rdname dot-compute_point_estimate #' @export .compute_point_estimate.default <- function(draws) { - stop("compute_point_estimate() has not been implemented for objects of class '", class(draws), "'") + stop(".compute_point_estimate() has not been implemented for objects of class '", class(draws), "'") } #' Thin a draws object @@ -654,14 +654,14 @@ elpd_loo_approximation <- function(.llfun, data, draws, cores, loo_approximation #' @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) { - UseMethod("thin_draws") + UseMethod(".thin_draws") } #' @rdname dot-thin_draws #' @export .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) + checkmate::assert_int(loo_approximation_draws, lower = 1, upper = .ndraws(draws), null.ok = TRUE) + S <- .ndraws(draws) idx <- 1:loo_approximation_draws * S %/% loo_approximation_draws draws <- draws[idx, , drop = FALSE] draws @@ -669,12 +669,12 @@ elpd_loo_approximation <- function(.llfun, data, draws, cores, loo_approximation #' @rdname dot-thin_draws #' @export .thin_draws.numeric <- function(draws, loo_approximation_draws) { - thin_draws.matrix(as.matrix(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), "'") +.thin_draws.default <- function(draws, loo_approximation_draws) { + stop(".thin_draws() has not been implemented for objects of class '", class(draws), "'") } @@ -689,7 +689,7 @@ thin_draws.default <- function(draws, loo_approximation_draws) { #' @param x A draws object with posterior draws. #' @return An integer with the number of draws. .ndraws <- function(x) { - UseMethod("n_draws") + UseMethod(".ndraws") } #' @rdname dot-ndraws #' @export @@ -699,7 +699,7 @@ thin_draws.default <- function(draws, loo_approximation_draws) { #' @rdname dot-ndraws #' @export .ndraws.default <- function(x) { - stop("n_draws() has not been implemented for objects of class '", class(x), "'") + stop(".ndraws() has not been implemented for objects of class '", class(x), "'") } ## Subsampling ----- diff --git a/man/dot-thin_draws.Rd b/man/dot-thin_draws.Rd index 98779295..b0b7058a 100644 --- a/man/dot-thin_draws.Rd +++ b/man/dot-thin_draws.Rd @@ -4,7 +4,7 @@ \alias{.thin_draws} \alias{.thin_draws.matrix} \alias{.thin_draws.numeric} -\alias{thin_draws.default} +\alias{.thin_draws.default} \title{Thin a draws object} \usage{ .thin_draws(draws, loo_approximation_draws) @@ -13,7 +13,7 @@ \method{.thin_draws}{numeric}(draws, loo_approximation_draws) -thin_draws.default(draws, loo_approximation_draws) +\method{.thin_draws}{default}(draws, loo_approximation_draws) } \arguments{ \item{draws}{A draws object with posterior draws.}