Skip to contents

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 numeric vector.

...

Extra arguments to be passed to do.

do

A function that takes object as an argument and returns a single numeric value.

n

A non-negative integer giving the number of bootstrap replications.

f

A function that takes a single numeric vector (the result of do) as argument.

level

A length-one numeric vector giving the confidence level. Must be a single number between \(0\) and \(1\). Only used if f is NULL.

interval

A character string giving the type of confidence interval to be returned. It must be one "basic" (the default), "normal" or "percentiles" (see confidence_bootstrap()). Any unambiguous substring can be given. Only used if f is NULL.

Value

If f is NULL (the default), bootstrap() returns a named numeric vector with the following elements:

original

The observed value of do applied to object.

mean

The bootstrap estimate of mean of do.

bias

The bootstrap estimate of bias of do.

error

The bootstrap estimate of standard error of do.

lower

The lower limit of the bootstrap confidence interval at level.

upper

The 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

Author

N. Frerebeau

Examples

x <- rnorm(20)

## Bootstrap
bootstrap(x, do = mean, n = 100)
#>    original        mean        bias       error       lower       upper 
#> -0.31003022 -0.36538846 -0.05535824  0.25116541 -0.80452446  0.29147031 

## 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.4233110 -0.1714454 

## Get the n bootstrap estimates
(z <- bootstrap(x, n = 100, do = mean, f = function(x) { x }))
#>   [1]  0.05882350 -0.15970975 -0.16824740 -0.25337802  0.38357259 -0.27767305
#>   [7] -0.56937703 -0.17413748 -0.52393747 -0.03761762 -0.37639113 -0.62335220
#>  [13]  0.02098242 -0.48991571 -0.38256150 -0.35694976 -0.07544047 -0.28480161
#>  [19] -0.38464752 -0.55362749 -0.27333930 -0.48971194 -0.11691814 -0.98175087
#>  [25] -0.74759480 -0.38207080 -0.52612229 -0.52627593 -0.48806821 -0.03992511
#>  [31] -0.38862562 -0.01227077 -0.97275850 -0.41971631 -0.03975755 -0.07345552
#>  [37] -0.47651196  0.07988537 -0.34898297 -0.52959421 -0.32875221 -0.53683465
#>  [43] -0.53870283 -0.15155093 -0.22187541 -0.55008251 -0.93603395 -0.25134170
#>  [49]  0.09068206 -0.30633353  0.01512999  0.02636154 -0.62805636 -0.41543211
#>  [55] -0.73731395 -0.60169945 -0.10066947 -0.26596719 -0.10152999  0.01594432
#>  [61] -0.20390957  0.25543398 -0.17004627 -1.00197428  0.03192212  0.09843582
#>  [67] -0.71917920 -0.72579064 -0.39998269 -0.49303580 -0.37553424 -0.42163805
#>  [73] -0.42700161 -0.26265073 -0.43257369 -0.29944782 -0.33025893 -0.21952608
#>  [79] -0.33849245 -0.68065268 -0.25271155  0.12362128  0.01747910 -0.54110655
#>  [85] -0.15135539  0.03421979 -0.06693228 -0.79303070 -0.04313817 -0.34621952
#>  [91]  0.25973890 -0.57828512 -0.18786661 -0.05036271 -0.42062992 -0.10856037
#>  [97] -0.25766274 -0.01699731 -0.19125433 -0.21562539

## Basic bootstrap confidence interval
confidence_bootstrap(z, level = 0.95, type = "basic", t0 = mean(x))
#>      lower      upper 
#> -0.8773589  0.3565926