Samples randomly from the elements of object with replacement.
Usage
bootstrap(object, ...)
# S4 method for class 'numeric'
bootstrap(
object,
do,
n,
...,
f = NULL,
level = 0.95,
interval = c("basic", "normal", "percentiles")
)Arguments
- object
A
numericvector.- ...
Extra arguments to be passed to
do.- do
A
functionthat takesobjectas an argument and returns a single numeric value.- n
A non-negative
integergiving the number of bootstrap replications.- f
A
functionthat takes a single numeric vector (the result ofdo) as argument.- level
A length-one
numericvector giving the confidence level. Must be a single number between \(0\) and \(1\). Only used iffisNULL.- interval
A
characterstring giving the type of confidence interval to be returned. It must be one "basic" (the default), "normal" or "percentiles" (seeconfidence_bootstrap()). Any unambiguous substring can be given. Only used iffisNULL.
Value
If f is NULL (the default), bootstrap() returns a named numeric
vector with the following elements:
originalThe observed value of
doapplied toobject.meanThe bootstrap estimate of mean of
do.biasThe bootstrap estimate of bias of
do.errorThe bootstrap estimate of standard error of
do.lowerThe lower limit of the bootstrap confidence interval at
level.upperThe upper limit of the bootstrap confidence interval at
level
If f is a function, bootstrap() returns the result of f applied to
the n values of do.
References
Davison, A. C. & Hinkley, D. V. (1997). Bootstrap Methods and Their Application. Cambridge Series on Statistical and Probabilistic Mathematics. Cambridge: Cambridge University Press.
See also
Other resampling methods:
jackknife(),
resample_multinomial(),
resample_uniform()
Examples
x <- rnorm(20)
## Bootstrap
bootstrap(x, do = mean, n = 100)
#> original mean bias error lower upper
#> -0.117435227 -0.111210028 0.006225199 0.252842043 -0.778076267 0.478596745
## Estimate the 25th and 95th percentiles
quant <- function(x) { quantile(x, probs = c(0.25, 0.75)) }
bootstrap(x, n = 100, do = mean, f = quant)
#> 25% 75%
#> -0.25801026 0.01351363
## Get the n bootstrap estimates
(z <- bootstrap(x, n = 100, do = mean, f = function(x) { x }))
#> [1] 0.137742259 -0.135825915 0.253578390 -0.193667239 0.097690902
#> [6] -0.016839800 -0.024683298 -0.203493697 -0.806142895 0.008517408
#> [11] -0.059298045 0.142244681 -0.379590072 0.075135128 0.151848449
#> [16] -0.031970635 -0.085168475 0.078620322 -0.504798326 0.011415927
#> [21] -0.167885817 -0.242929365 -0.242388296 -0.361539420 -0.230708605
#> [26] 0.178021328 0.107216373 0.001070759 -0.052606272 -0.426226594
#> [31] 0.177273155 -0.274776655 -0.273849236 -0.405303266 -0.291506941
#> [36] 0.255056071 -0.032611012 -0.175293090 -0.092253192 -0.073539185
#> [41] 0.072721475 -0.002674384 -0.251180400 -0.353639786 -0.150615342
#> [46] -0.119554964 0.314435711 0.228705683 -0.446391574 -0.347532298
#> [51] -0.002730341 -0.141352774 -0.042585060 -0.421963234 -0.366909001
#> [56] -0.170561779 -0.414075273 -0.306638538 0.099981261 -0.550552778
#> [61] 0.059179184 -0.049199117 -0.244255370 -0.369697568 -0.446213655
#> [66] -0.180721938 -0.179295629 0.007779681 0.154170218 -0.007102601
#> [71] -0.600761116 -0.153978766 -0.437716466 -0.078592674 -0.324618440
#> [76] -0.201995834 -0.197402623 -0.070790269 -0.095214639 -0.016974343
#> [81] -0.485141227 0.169879450 -0.652084269 -0.336039885 0.013642024
#> [86] -0.003301966 -0.279581277 -0.234227825 -0.349683535 -0.185248665
#> [91] 0.053710650 -0.401383597 -0.067829885 0.142192885 0.031837535
#> [96] 0.048959706 -0.297017210 -0.100100164 -0.549006869 0.088012025
## Basic bootstrap confidence interval
confidence_bootstrap(z, level = 0.95, type = "basic", t0 = mean(x))
#> lower upper
#> -0.4890888 0.3881186
