@@ -1206,8 +1206,9 @@ loo_subsample_estimation_diff_srs <- function(x) {
12061206# '
12071207# ' @examples
12081208# ' ### This example predicts wine quality (data from Cortez et al., 2009).
1209- # ' ## First, commented out code shows to generate a loglik_matrix.
1210- # ' ## Second, running code illustrates how to use srs_diff_est().
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().
12111212# ' # library(dplyr)
12121213# ' # library(brms)
12131214# ' # options(brms.backend = "cmdstanr")
@@ -1227,10 +1228,25 @@ loo_subsample_estimation_diff_srs <- function(x) {
12271228# ' # silent = 2,
12281229# ' # refresh = 0)
12291230# ' #
1230- # ' # wine_loglik_matrix <- log_lik(fitos)
1231- # ' wine_loglik_matrix <- example_wine_loglik_matrix() # Installed with loo to save time of fitting model shown above
1232- # '
1233- # '
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)
12341250# ' @export
12351251srs_diff_est <- function (y_approx , y , y_idx ) {
12361252 checkmate :: assert_numeric(y_approx )
0 commit comments