Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ If you read this from a place other than <https://mc-stan.org/projpred/news/inde

## Bug fixes

* With subsampled LOO (`nloo` below the number of observations) and unequal observation weights, the SEs of `"mse"`, `"rmse"`, `"R2"` and pointwise custom statistics were too small. All SEs with subsampled LOO are now also numerically stable for values with a large common offset. (GitHub: #679)

* For `rstanarm::stan_gamm4()` reference models with `t2()` terms, `rstanarm::posterior_linpred()` gives wrong linear predictors. projpred now computes them itself, so the reference model predictions, the projections and the predictive performance results are correct. (GitHub: #677)


Expand Down
32 changes: 26 additions & 6 deletions R/summary_funs.R
Original file line number Diff line number Diff line change
Expand Up @@ -723,36 +723,56 @@ get_nfeat_baseline <- function(object, baseline, stat, custom_stats = list(),
}

## Difference estimation using SRS-WOR sampling (Magnusson et al., 2020)
## copied from loo:::srs_diff_est and added weights
## based on loo:::srs_diff_est(), with observation weights
.srs_diff_est_w <- function(y_approx, y, y_idx, wobs = 1) {

N <- length(y_approx)
m <- length(y)
y_approx_m <- y_approx[y_idx]
# weights with mean 1 over all observations and over the subsample
wobs_m <- 1
if (length(wobs) > 1) {
wobs_m <- wobs[y_idx]
wobs_m <- length(wobs_m) * wobs_m / sum(wobs_m)
wobs <- length(wobs) * wobs / sum(wobs)
}
# Subtract the (weighted) mean of `y_approx` from all values, so that the
# sums of squares in eq (9) do not cancel when the values have a large
# common offset. In exact arithmetic, `v_y_hat` and `hat_v_y` do not depend
# on the shift; `N * shift` is added to `y_hat` at the end. A non-finite
# mean (`NA` or infinite values) gives no shift.
shift <- sum(wobs * y_approx) / N
if (!is.finite(shift)) {
shift <- 0
}
y_approx <- y_approx - shift
y <- y - shift
y_approx_m <- y_approx[y_idx]

e_i <- y - y_approx_m
t_pi_tilde <- sum(wobs * y_approx)
t_pi2_tilde <- sum(wobs * y_approx^2)
t_e <- N * mean(wobs_m * e_i)
t_hat_epsilon <- N * mean(wobs_m * (y^2 - y_approx_m^2))
t_hat_epsilon <- N * mean(wobs_m * e_i * (y + y_approx_m))

est_list <- nlist(m, N)
# eq (7)
est_list$y_hat <- t_pi_tilde + t_e
# eq (8)
var_e_i <- .weighted_sd(e_i, w = wobs_m)^2
est_list$v_y_hat <- N^2 * (1 - m / N) * var_e_i / m
# eq (8); with weights, the difference term is a ratio estimator, and its
# linearization variance is the sample variance of `wobs_m * (e_i -
# e_mean_w)` (with `wobs_m` scaled by the subsample mean of the weights, not
# by the mean over all observations)
e_mean_w <- t_e / N
est_list$v_y_hat <- if (m < 2) {
NA_real_
} else {
N^2 * (1 - m / N) * sum((wobs_m * (e_i - e_mean_w))^2) / ((m - 1) * m)
}
# eq (9) first row second `+` should be `-`
# Supplementary material eq (6) has this correct
# Here the variance is for sum, while in the paper the variance is for mean
# which explains the proportional difference of 1/N
est_list$hat_v_y <- (t_pi2_tilde + t_hat_epsilon) - # a (has been checked)
(1 / N) * (t_e^2 - est_list$v_y_hat + 2 * t_pi_tilde * est_list$y_hat - t_pi_tilde^2) # b
est_list$y_hat <- est_list$y_hat + N * shift
return(est_list)
}
71 changes: 71 additions & 0 deletions tests/testthat/test_misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,77 @@ test_that(".srs_diff_est_w() works as expected with `wobs` not full of ones", {
expect_equal(sqrt(res$hat_v_y), sigma_hat_true, tol = 1e-1)
})

test_that(".srs_diff_est_w() gives a correct `v_y_hat` with unequal `wobs`", {
withr::local_seed(3)
N <- 1e3
m <- 1e2
S <- 1e4
wobs <- rlnorm(N)
y_true <- rnorm(N, -1, 1)
y_approx <- y_true + rnorm(N, 0.2, 0.5)
sum_y_true <- N * weighted.mean(y_true, w = wobs)
y_hat <- v_y_hat <- numeric(S)
for (i in seq_len(S)) {
y_idx <- sample.int(N, size = m)
res <- .srs_diff_est_w(y_approx = y_approx,
y = y_true[y_idx],
y_idx = y_idx,
wobs = wobs)
y_hat[i] <- res$y_hat
v_y_hat[i] <- res$v_y_hat
}
# `v_y_hat` estimates the variance of `y_hat` over the subsamples (the
# linearization variance is a little too small for heavy-tailed `wobs`):
expect_equal(mean(v_y_hat), var(y_hat), tolerance = 2e-1)
q_ci <- qnorm(0.975)
in_ci <- abs(y_hat - sum_y_true) < q_ci * sqrt(v_y_hat)
# Here, the coverage is 0.931 (0.737 with the old variance formula):
expect_equal(mean(in_ci), 0.95, tolerance = 4e-2)
})

test_that(".srs_diff_est_w() is stable for values with a large common offset", {
withr::local_seed(4)
N <- 1e5
m <- 1e3
wobs <- rlnorm(N)
y_true <- rnorm(N, 0, 0.3)
y_approx <- y_true + rnorm(N, 0, 0.1)
y_idx <- sample.int(N, size = m)
res_ref <- .srs_diff_est_w(y_approx = y_approx,
y = y_true[y_idx],
y_idx = y_idx,
wobs = wobs)
offs <- 1e8
res_offs <- .srs_diff_est_w(y_approx = y_approx + offs,
y = y_true[y_idx] + offs,
y_idx = y_idx,
wobs = wobs)
expect_equal(res_offs$hat_v_y, res_ref$hat_v_y, tolerance = 1e-6)
expect_equal(res_offs$v_y_hat, res_ref$v_y_hat, tolerance = 1e-6)
expect_equal(res_offs$y_hat, res_ref$y_hat + N * offs, tolerance = 1e-12)
})

test_that(".srs_diff_est_w() with equal `wobs` equals the unweighted case", {
withr::local_seed(5)
N <- 1e3
m <- 1e2
y_true <- rnorm(N)
y_approx <- y_true + rnorm(N, 0.2, 0.5)
y_idx <- sample.int(N, size = m)
res_unw <- .srs_diff_est_w(y_approx = y_approx,
y = y_true[y_idx],
y_idx = y_idx)
res_w <- .srs_diff_est_w(y_approx = y_approx,
y = y_true[y_idx],
y_idx = y_idx,
wobs = rep(3, N))
expect_equal(res_w, res_unw, tolerance = 1e-12)
e_i <- y_true[y_idx] - y_approx[y_idx]
expect_equal(res_unw$y_hat, sum(y_approx) + N * mean(e_i), tolerance = 1e-12)
expect_equal(res_unw$v_y_hat, N^2 * (1 - m / N) * var(e_i) / m,
tolerance = 1e-12)
})

test_that(".srs_diff_est_w() propagates input `NA`s to its output", {
nloo_tst <- nobsv %/% 5L
loo_inds_tst <- ceiling(seq(1L, nobsv, length.out = nloo_tst))
Expand Down
Loading