Skip to content

Commit 30cc34e

Browse files
committed
added full example
1 parent 535f464 commit 30cc34e

3 files changed

Lines changed: 45 additions & 14 deletions

File tree

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* Added contribution section. by @VisruthSK in #286
1818
* Update LOO uncertainty paper to use BA doi by @avehtari in #311
1919
* Update documentation for `E_loo()` function by @avehtari in #312
20-
* Export `srs_diff_est()` function by @vinniott in #340
20+
* Export `srs_diff_est()` function by @vinniott and @avehtari in #340
2121

2222

2323
# loo 2.8.0

R/loo_subsample.R

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
12351251
srs_diff_est <- function(y_approx, y, y_idx) {
12361252
checkmate::assert_numeric(y_approx)

man/srs_diff_est.Rd

Lines changed: 22 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)