@@ -1205,48 +1205,49 @@ loo_subsample_estimation_diff_srs <- function(x) {
12051205# ' @seealso [loo_subsample()]
12061206# '
12071207# ' @examples
1208- # ' ### This example predicts wine quality (data from Cortez et al., 2009).
1209- # ' ## The code is commented out for easier installation of the package
1210- # ' ## because brm() takes two or three seconds to fit.
1211- # ' ## A log_lik_matrix is generated from a fit, then it is used for srs_diff_est().
1212- # ' # library(dplyr)
1213- # ' # library(brms)
1214- # ' # options(brms.backend = "cmdstanr")
1215- # ' # options(mc.cores = 4)
1216- # ' # library(loo)
1217- # ' #
1218- # ' # wine <- read.delim(root("winequality-red", "winequality-red.csv"), sep = ";") |>
1219- # ' # distinct()
1220- # ' #
1221- # ' # wine_scaled <- as.data.frame(scale(wine))
1222- # ' #
1223- # ' # fitos <- brm(ordered(quality) ~ .,
1224- # ' # family = cumulative("logit"),
1225- # ' # prior = prior(R2D2(mean_R2 = 1/3, prec_R2 = 3)),
1226- # ' # data = wine_scaled,
1227- # ' # seed = 1,
1228- # ' # silent = 2,
1229- # ' # refresh = 0)
1230- # ' #
1231- # ' # log_lik_matrix <- log_lik(fitos)
1232- # ' #
1233- # ' # N <- nrow(wine_scaled)
1234- # ' # Nsub <- 100
1235- # ' #
1236- # ' # # posterior log-score
1237- # ' # lpd <- elpd(log_lik_matrix)
1238- # ' # sum(lpd$pointwise[,"elpd"])
1239- # ' # # Use PSIS-LOO for subsample of Nsub randomly selected observations
1240- # ' # set.seed(1)
1241- # ' # idx <- sample(1:N, Nsub)
1242- # ' # elpd_loo_sub <- loo(log_lik_matrix[,idx])
1243- # ' # sum(elpd_loo_sub$pointwise[,"elpd_loo"]) / Nsub * N
1244- # ' #
1245- # ' # # Use difference estimator to combine fast result and subsampled accurate result
1246- # ' # loo:::srs_diff_est(lpd$pointwise[,"elpd"], elpd_loo_sub$pointwise[,"elpd_loo"], idx)
1247- # ' #
1248- # ' # # Comparison to using PSIS-LOO for all observations
1249- # ' # loo(log_lik_matrix)
1208+ # ' ## This example predicts wine quality (data from Cortez et al., 2009).
1209+ # ' # The code is commented as ## Not run: because brm() takes two or three seconds to fit.
1210+ # ' # Copy the code to your console to execute it.
1211+ # ' # A log_lik_matrix is generated from a fit, then it is used for srs_diff_est().
1212+ # ' \dontrun{
1213+ # ' library(dplyr)
1214+ # ' library(brms)
1215+ # ' options(brms.backend = "cmdstanr")
1216+ # ' options(mc.cores = 4)
1217+ # '
1218+ # ' wine <- read.delim("../data-raw/winequality-red.csv", sep = ";") |>
1219+ # ' distinct()
1220+ # '
1221+ # ' wine_scaled <- as.data.frame(scale(wine))
1222+ # '
1223+ # ' fitos <- brm(ordered(quality) ~ .,
1224+ # ' family = cumulative("logit"),
1225+ # ' prior = prior(R2D2(mean_R2 = 1/3, prec_R2 = 3)),
1226+ # ' data = wine_scaled,
1227+ # ' seed = 1,
1228+ # ' silent = 2,
1229+ # ' refresh = 0)
1230+ # '
1231+ # ' log_lik_matrix <- log_lik(fitos)
1232+ # '
1233+ # ' N <- nrow(wine_scaled)
1234+ # ' Nsub <- 100
1235+ # '
1236+ # ' # posterior log-score
1237+ # ' lpd <- elpd(log_lik_matrix)
1238+ # ' sum(lpd$pointwise[,"elpd"])
1239+ # ' # Use PSIS-LOO for subsample of Nsub randomly selected observations
1240+ # ' set.seed(1)
1241+ # ' idx <- sample(1:N, Nsub)
1242+ # ' elpd_loo_sub <- loo(log_lik_matrix[,idx])
1243+ # ' sum(elpd_loo_sub$pointwise[,"elpd_loo"]) / Nsub * N
1244+ # '
1245+ # ' # Use difference estimator to combine fast result and subsampled accurate result
1246+ # ' loo::srs_diff_est(lpd$pointwise[,"elpd"], elpd_loo_sub$pointwise[,"elpd_loo"], idx)
1247+ # '
1248+ # ' # Comparison to using PSIS-LOO for all observations
1249+ # ' loo(log_lik_matrix)
1250+ # ' }
12501251# ' @export
12511252srs_diff_est <- function (y_approx , y , y_idx ) {
12521253 checkmate :: assert_numeric(y_approx )
0 commit comments