Skip to content

Commit bc11e9c

Browse files
tests for percentile estimation function
1 parent a9e6d3b commit bc11e9c

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

R/boot_bw_estimate.R

+18-13
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,27 @@ boot_bw_estimate <- function(boot_df) {
2525
est <- lapply(
2626
X = boot_df,
2727
FUN = boot_percentile
28-
) |>
28+
)
29+
30+
## Structure list names ----
31+
if (nrow(est[[1]]) == 1) {
32+
names(est) <- paste(
33+
names(est),
34+
lapply(X = est, FUN = row.names) |> unlist(),
35+
sep = "."
36+
)
37+
}
38+
39+
## Flatten list ----
40+
est <- est |>
2941
do.call(rbind, args = _)
3042

3143
## Re-structure results ----
32-
if (all(grepl("\\.", row.names(est)))) {
33-
est <- data.frame(
34-
strata = gsub("\\.[^\\.]{1,}", "", row.names(est)),
35-
indicator = gsub("[^\\.]{1,}\\.", "", row.names(est)),
36-
est
37-
)
38-
} else {
39-
est <- data.frame(
40-
indicator = row.names(est),
41-
est
42-
)
43-
}
44+
est <- data.frame(
45+
strata = gsub("\\.[^\\.]{1,}", "", row.names(est)),
46+
indicator = gsub("[^\\.]{1,}\\.", "", row.names(est)),
47+
est
48+
)
4449

4550
## Tidy up row names ----
4651
row.names(est) <- NULL

R/utils.R

+4-6
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ tidy_boot <- function(boot, w, strata, outputColumns) {
118118
boot
119119
}
120120

121+
121122
#'
122123
#' Boot estimate
123124
#'
@@ -135,14 +136,11 @@ boot_percentile <- function(boot_df) {
135136
do.call(rbind, args = _) |>
136137
data.frame()
137138

138-
# est <- data.frame(
139-
# indicator = row.names(est),
140-
# est
141-
# )
142-
143139
names(est) <- c("est", "lcl", "ucl")
144140
} else {
145-
est <- quantile(x = boot_df, probs = c(0.5, 0.025, 0.975), na.rm = TRUE) |>
141+
est <- stats::quantile(
142+
x = boot_df, probs = c(0.5, 0.025, 0.975), na.rm = TRUE
143+
) |>
146144
rbind() |>
147145
data.frame()
148146

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Tests for boot_bw_estimate function/s ----------------------------------------
2+
3+
test_that("boot_bw_estimate works as expected", {
4+
boot_df <- boot_bw(
5+
x = indicatorsHH, w = villageData, statistic = bootClassic,
6+
params = "anc1", replicates = 99
7+
)
8+
9+
expect_s3_class(boot_bw_estimate(boot_df), "data.frame")
10+
11+
boot_df <- boot_bw(
12+
x = indicatorsHH, w = villageData, statistic = bootClassic,
13+
params = "anc1", replicates = 99, strata = "region"
14+
)
15+
16+
expect_s3_class(boot_bw_estimate(boot_df), "data.frame")
17+
})

0 commit comments

Comments
 (0)