Skip to content

Commit

Permalink
correct CIs in tidy.boot() (closes #1212)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Sep 26, 2024
1 parent 4ce9797 commit 8931f0d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# broom (development version)

* Corrected confidence interval values in `tidy.boot()` and addressed errors
when bootstrapping confidence intervals for multiple terms (#1212).

* Reverted deprecation of tidiers for objects from the margins package
now that the package is back on CRAN (#1220).

Expand Down
3 changes: 2 additions & 1 deletion R/boot-tidiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ tidy.boot <- function(x,
ci.pos <- pmatch(conf.method, names(ci.list[[1]]))

if (conf.method == "norm") {
ci.tab <- cbind(ci.list[[1]][ci.pos][[1]][2:3], ci.list[[2]][ci.pos][[1]][2:3])
extract_ci <- function(res) {res[["normal"]][2:3]}
ci.tab <- t(vapply(ci.list, extract_ci, numeric(2)))
} else {
ci.tab <- t(sapply(ci.list, function(x) x[[ci.pos]][4:5]))
}
Expand Down
29 changes: 29 additions & 0 deletions tests/testthat/test-boot.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,32 @@ test_that("tidy.boot for time series", {
check_tidy_output(td)
check_dims(td, expected_cols = 2)
})

test_that("dimensionality of CIs is correct (#1212)", {
set.seed(123)
d = data.frame(v1 = rnorm(100), v2=rnorm(100))
boot1 <- function(d, i) {
d = d[i,]
c(mean(d$v1), mean(d$v2), cor(d$v1, d$v2))
}
boot2 <- function(d, i) {
d = d[i,]
c(mean(d$v1), mean(d$v2))
}
boot3 <- function(d, i) {
d = d[i,]
c(mean(d$v1))
}

r1 <- boot(d, boot1, 100)
r2 <- boot(d, boot2, 100)
r3 <- boot(d, boot3, 100)

td1 <- tidy(r1, conf.int = T, conf.method = "norm")
td2 <- tidy(r2, conf.int = T, conf.method = "norm")
td3 <- tidy(r3, conf.int = T, conf.method = "norm")

check_tidy_output(td1)
check_tidy_output(td2)
check_tidy_output(td3)
})

0 comments on commit 8931f0d

Please sign in to comment.