Debian Codegraph @

R/boot.R

raw
1 #' @templateVar class boot
2 #' @template title_desc_tidy
3 #'
4 #' @param x A [boot::boot()] object.
5 #' @template param_confint
6 #' @param conf.method Passed to the `type` argument of [boot::boot.ci()].
7 #' Defaults to `"perc"`. The allowed types are `"perc"`, `"basic"`,
8 #' `"bca"`, and `"norm"`. Does not support `"stud"` or `"all"`.
9 #' @template param_unused_dots
10 #' @template param_exponentiate
11 #'
12 #' @evalRd return_tidy("bias", "std.error", "term",
13 #' statistic = "Original value of the statistic."
14 #' )
15 #'
16 #' @details If weights were provided to the `boot` function, an `estimate`
17 #' column is included showing the weighted bootstrap estimate, and the
18 #' standard error is of that estimate.
19 #'
20 #' If there are no original statistics in the "boot" object, such as with a
21 #' call to `tsboot` with `orig.t = FALSE`, the `original`
22 #' and `statistic` columns are omitted, and only `estimate` and
23 #' `std.error` columns shown.
24 #'
25 #' @examplesIf rlang::is_installed("boot")
26 #'
27 #' # load modeling library
28 #' library(boot)
29 #'
30 #' clotting <- data.frame(
31 #' u = c(5, 10, 15, 20, 30, 40, 60, 80, 100),
32 #' lot1 = c(118, 58, 42, 35, 27, 25, 21, 19, 18),
33 #' lot2 = c(69, 35, 26, 21, 18, 16, 13, 12, 12)
34 #' )
35 #'
36 #' # fit models
37 #' g1 <- glm(lot2 ~ log(u), data = clotting, family = Gamma)
38 #'
39 #' bootfun <- function(d, i) {
40 #' coef(update(g1, data = d[i, ]))
41 #' }
42 #'
43 #' bootres <- boot(clotting, bootfun, R = 999)
44 #'
45 #' # summarize model fits with tidiers
46 #' tidy(g1, conf.int = TRUE)
47 #' tidy(bootres, conf.int = TRUE)
48 #'
49 #' @export
50 #' @aliases boot_tidiers
51 #' @seealso [tidy()], [boot::boot()], [boot::tsboot()], [boot::boot.ci()],
52 #' [rsample::bootstraps()]
53 tidy.boot <- function(
54 x,
55 conf.int = FALSE,
56 conf.level = 0.95,
57 conf.method = c("perc", "bca", "basic", "norm"),
58 exponentiate = FALSE,
59 ...
60 ) {
61 conf.method <- rlang::arg_match(conf.method)
62
63 # calculate the bias and standard error
64 # this is an adapted version of the code in print.boot, where the bias
65 # and standard error are calculated
66 boot.out <- x
67 index <- 1:ncol(boot.out$t)
68 t <- matrix(boot.out$t[, index], nrow = nrow(boot.out$t))
69 allNA <- apply(t, 2L, function(t) all(is.na(t)))
70 index <- index[!allNA]
71 t <- matrix(t[, !allNA], nrow = nrow(t))
72
73 if (is.null(t0 <- boot.out$t0)) {
74 if (is.null(boot.out$call$weights)) {
75 op <- cbind(
76 apply(t, 2L, mean, na.rm = TRUE),
77 sqrt(apply(t, 2L, function(t.st) var(t.st[!is.na(t.st)])))
78 )
79 } else {
80 op <- NULL
81 for (i in index)
82 op <- rbind(op, boot::imp.moments(boot.out, index = i)$rat)
83 op[, 2L] <- sqrt(op[, 2])
84 }
85 colnames(op) <- c("estimate", "std.error")
86 } else {
87 t0 <- boot.out$t0[index]
88 if (is.null(boot.out$call$weights)) {
89 op <- cbind(
90 t0,
91 apply(t, 2L, mean, na.rm = TRUE) -
92 t0,
93 sqrt(apply(t, 2L, function(t.st) var(t.st[!is.na(t.st)])))
94 )
95 colnames(op) <- c("statistic", "bias", "std.error")
96 } else {
97 op <- NULL
98 for (i in index) {
99 op <- rbind(op, boot::imp.moments(boot.out, index = i)$rat)
100 }
101 op <- cbind(
102 t0,
103 op[, 1L] - t0,
104 sqrt(op[, 2L]),
105 apply(t, 2L, mean, na.rm = TRUE)
106 )
107 colnames(op) <- c("statistic", "bias", "std.error", "estimate")
108 }
109 }
110
111 # bring in rownames as "term" column, and turn into a data.frame
112 ret <- as_tidy_tibble(op)
113
114 if (conf.int) {
115 ci.list <- lapply(
116 seq_along(x$t0),
117 boot::boot.ci,
118 boot.out = x,
119 conf = conf.level,
120 type = conf.method
121 )
122
123 # boot.ci uses c("norm", "basic", "perc", "stud") for types
124 # stores them with longer names
125 ci.pos <- pmatch(conf.method, names(ci.list[[1]]))
126
127 if (conf.method == "norm") {
128 extract_ci <- function(res) {
129 res[["normal"]][2:3]
130 }
131 ci.tab <- t(vapply(ci.list, extract_ci, numeric(2)))
132 } else {
133 ci.tab <- t(sapply(ci.list, function(x) x[[ci.pos]][4:5]))
134 }
135
136 colnames(ci.tab) <- c("conf.low", "conf.high")
137 ret <- cbind(ret, ci.tab)
138 }
139
140 if (exponentiate) {
141 ret <- exponentiate(ret, col = "statistic")
142 }
143
144 as_tibble(ret)
145 }