Skip to contents

Calculate one or multiple bootstrap confidence intervals, given a sample of bootstrap replications.

Usage

bootstrap_CIs(
  boot_est,
  boot_se = NULL,
  est = NULL,
  se = NULL,
  CI_type = "percentile",
  level = 0.95,
  B_vals = length(boot_est),
  reps = 1L,
  format = "wide",
  seed = NULL
)

Arguments

boot_est

vector of bootstrap replications of an estimator.

boot_se

vector of estimated standard errors from each bootstrap replication.

est

numeric value of the estimate based on the original sample. Required for CI_type = "normal", CI_type = "basic", and CI_type = "student".

se

numeric value of the estimated standard error based on the original sample. Required for CI_type = "student".

CI_type

Character string or vector of character strings indicating types of confidence intervals to calculate. Options are "normal", "basic", "student", and "percentile" (the default).

level

numeric value between 0 and 1 for the desired coverage level, with a default of 0.95.

B_vals

vector of sub-sample sizes for which to calculate confidence intervals. Setting B_vals = length(boot_est) (the default) will return bootstrap confidence intervals calculated on the full set of bootstrap replications. For B_vals < length(boot_est), confidence intervals will be calculated after sub-sampling (without replacement) the bootstrap replications.

reps

integer value for the number of sub-sample confidence intervals to generate when B_vals < length(boot_est), with a default of reps = 1.

format

character string controlling the format of the output. If format = "wide" (the default), then different types of confidence intervals will be returned in separate columns. If format = "long", then confidence intervals of different types will appear on different rows of dataset. If format = "wide-list", then different types of confidence intervals will be returned in separate columns and the result will be wrapped in an unnamed list.

seed

Single numeric value to which the random number generator seed will be set. Default is NULL, which does not set a seed.

Value

If format = "wide", the function returns a data.frame

with reps rows per entry of B_vals, where each row contains confidence intervals for one sub-sample replication.

If format = "long", the function returns a data.frame with one row for each CI_type, each replication, and each entry of

B_vals, where each row contains a single confidence interval for one sub-sample replication.

If format = "wide-list", then the output will be structured as in

format = "wide" but will be wrapped in an unnamed list, which makes it easier to sore the output in a tibble, and will be assigned the class

"bootstrap_CIs".

Details

Confidence intervals are calculated following the methods described in Chapter 5 of Davison and Hinkley (1997). For basic non-parametric bootstraps, the methods are nearly identical to the implementation in boot.ci from the boot package.

References

Davison, A.C. and Hinkley, D.V. (1997). _Bootstrap Methods and Their Application_, Chapter 5. Cambridge University Press.

Examples

# generate t-distributed data
N <- 50
mu <- 2
nu <- 5
dat <- mu + rt(N, df = nu)

# create bootstrap replications
f <- \(x) {
 c(
   M = mean(x, trim = 0.1),
   SE = sd(x) / sqrt(length(x))
 )
}

booties <- replicate(399, {
  sample(dat, replace = TRUE, size = N) |>
  f()
})

res <- f(dat)

# calculate bootstrap CIs from full set of bootstrap replicates
bootstrap_CIs(
  boot_est = booties[1,],
  boot_se = booties[2,],
  est = res[1],
  se = res[2],
  CI_type = c("normal","basic","student","percentile"),
  format = "long"
)
#>   bootstraps       type    lower    upper
#> 1        399     normal 1.956827 2.713912
#> 2        399      basic 1.932965 2.698004
#> 3        399    student 1.916762 2.720168
#> 4        399 percentile 1.969730 2.734769

# calculate multiple bootstrap CIs using sub-sampling of replicates
bootstrap_CIs(
  boot_est = booties[1,],
  boot_se = booties[2,],
  est = res[1],
  se = res[2],
  CI_type = c("normal","basic","student","percentile"),
  B_vals = 199,
  reps = 4L,
  format = "long"
)
#>    bootstraps       type    lower    upper
#> 1         199     normal 1.962246 2.704077
#> 2         199      basic 1.932965 2.687525
#> 3         199    student 1.916762 2.720168
#> 4         199 percentile 1.980209 2.734769
#> 5         199     normal 1.980325 2.677669
#> 6         199      basic 1.947668 2.691840
#> 7         199    student 1.929162 2.704623
#> 8         199 percentile 1.975895 2.720066
#> 9         199     normal 1.962055 2.699546
#> 10        199      basic 1.932965 2.698004
#> 11        199    student 1.915225 2.704483
#> 12        199 percentile 1.969730 2.734769
#> 13        199     normal 1.971123 2.694702
#> 14        199      basic 1.962497 2.691840
#> 15        199    student 1.948314 2.704483
#> 16        199 percentile 1.975895 2.705237

# calculate multiple bootstrap CIs using sub-sampling of replicates,
# for each of several sub-sample sizes.
bootstrap_CIs(
  boot_est = booties[1,],
  boot_se = booties[2,],
  est = res[1],
  se = res[2],
  CI_type = c("normal","basic","student","percentile"),
  B_vals = c(49,99,199),
  reps = 4L,
  format = "long"
)
#>    bootstraps       type    lower    upper
#> 1          49     normal 1.983246 2.657481
#> 2          49      basic 1.935747 2.655371
#> 3          49    student 1.900402 2.658263
#> 4          49 percentile 2.012363 2.731987
#> 5          49     normal 1.925421 2.741237
#> 6          49      basic 2.013102 2.706281
#> 7          49    student 1.999313 2.720485
#> 8          49 percentile 1.961454 2.654633
#> 9          49     normal 1.962511 2.691241
#> 10         49      basic 2.046974 2.686846
#> 11         49    student 2.023731 2.749270
#> 12         49 percentile 1.980888 2.620760
#> 13         49     normal 1.856480 2.672109
#> 14         49      basic 1.869076 2.691840
#> 15         49    student 1.884228 2.720485
#> 16         49 percentile 1.975895 2.798658
#> 17         99     normal 1.947210 2.714605
#> 18         99      basic 1.935747 2.706281
#> 19         99    student 1.916762 2.703522
#> 20         99 percentile 1.961454 2.731987
#> 21         99     normal 1.942369 2.716418
#> 22         99      basic 1.947668 2.691840
#> 23         99    student 1.929162 2.720485
#> 24         99 percentile 1.975895 2.720066
#> 25         99     normal 1.933542 2.720124
#> 26         99      basic 1.907278 2.665325
#> 27         99    student 1.915225 2.688357
#> 28         99 percentile 2.002410 2.760457
#> 29         99     normal 2.007789 2.709735
#> 30         99      basic 2.016382 2.691840
#> 31         99    student 2.023731 2.720485
#> 32         99 percentile 1.975895 2.651352
#> 33        199     normal 1.945952 2.718207
#> 34        199      basic 1.902470 2.746083
#> 35        199    student 1.914183 2.757393
#> 36        199 percentile 1.921651 2.765264
#> 37        199     normal 1.972022 2.676960
#> 38        199      basic 1.932965 2.655265
#> 39        199    student 1.915225 2.694993
#> 40        199 percentile 2.012469 2.734769
#> 41        199     normal 1.944953 2.693485
#> 42        199      basic 1.907278 2.691840
#> 43        199    student 1.915225 2.720485
#> 44        199 percentile 1.975895 2.760457
#> 45        199     normal 1.959076 2.733094
#> 46        199      basic 1.950599 2.706281
#> 47        199    student 1.943938 2.749270
#> 48        199 percentile 1.961454 2.717136