Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute effective number of parameters for K-fold CV #462

Merged
merged 3 commits into from
Nov 30, 2020
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
11 changes: 8 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Items for next release

* New vignette on AB testing. (#409)

* `stan_jm()` gains an offset term for the longitudinal submodel. (#415, @pamelanluna)
### Bug fixes

* Fix bug where `loo()` with `k_threshold` argument specified would error if the model formula was a string instead of a formula object. (#454)

Expand All @@ -16,6 +14,13 @@ models fit with `stan_polr()`. (#450)

* Fix bug where `stan_glmer()` would error if `prior_aux=NULL`. (#482)

### New features

* New vignette on AB testing. (#409)

* `stan_jm()` gains an offset term for the longitudinal submodel. (#415, @pamelanluna)

* Effective number of parameters are computed for K-fold CV not just LOO CV. (#462)

# rstanarm 2.21.1

Expand Down
13 changes: 10 additions & 3 deletions R/loo-kfold.R
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,22 @@ kfold.stanreg <-
elpds <- rep(NA, length(elpds_unord))
elpds[obs_order] <- elpds_unord

pointwise <- cbind(elpd_kfold = elpds, p_kfold = NA, kfoldic = -2 * elpds)
# for computing effective number of parameters
ll_full <- log_lik(x)
lpds <- apply(ll_full, 2, log_mean_exp)
ps <- lpds - elpds

pointwise <- cbind(elpd_kfold = elpds, p_kfold = ps, kfoldic = -2 * elpds)
est <- colSums(pointwise)
se_est <- sqrt(N * apply(pointwise, 2, var))

out <- list(
estimates = cbind(Estimate = est, SE = se_est),
pointwise = pointwise,
elpd_kfold = est[1],
se_elpd_kfold = se_est[1]
elpd_kfold = est[["elpd_kfold"]],
se_elpd_kfold = se_est[["elpd_kfold"]],
p_kfold = est[["p_kfold"]],
se_p_kfold = se_est[["p_kfold"]]
)
rownames(out$estimates) <- colnames(pointwise)

Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test_loo.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,14 @@ test_that("kfold works on some examples", {
SW(kf <- kfold(fit_gaus, 4))
SW(kf2 <- kfold(example_model, 2))

expect_named(kf, c("estimates", "pointwise", "elpd_kfold", "se_elpd_kfold"))
expect_named(kf2, c("estimates", "pointwise", "elpd_kfold", "se_elpd_kfold"))
expect_named(kf, c("estimates", "pointwise", "elpd_kfold", "se_elpd_kfold", "p_kfold", "se_p_kfold"))
expect_named(kf2, c("estimates", "pointwise", "elpd_kfold", "se_elpd_kfold", "p_kfold", "se_p_kfold"))
expect_named(attributes(kf), c("names", "class", "K", "dims", "model_name", "discrete", "yhash", "formula"))
expect_named(attributes(kf2), c("names", "class", "K", "dims", "model_name", "discrete", "yhash", "formula"))
expect_s3_class(kf, c("kfold", "loo"))
expect_s3_class(kf2, c("kfold", "loo"))
expect_false(is.na(kf$p_kfold))
expect_false(is.na(kf2$p_kfold))

SW(kf <- kfold(fit_gaus, K = 2, save_fits = TRUE))

Expand Down