Computes a test statistic over multiple replicates from a set of data. Replicates are created drawing a sample of the same size and with replacement from the original set of data.

bootstrap(x, t_x = mean, N = 1000, summary = NULL, ...)

Arguments

x

A vector of values.

t_x

A function to compute a test statistic; the first argument must be for x.

N

The number of samples with replacement to draw from X.

summary

An optional function to apply to the test statistics after resampling.

...

Additional parameters for the t_x function.

Value

A list consisting of...

  • observed = the value of the test statistic for the original data set.

  • replicates = the values of the test statistic for each of the replicates produced via resampling.

  • summary = the output of summary function applied over the replicates; NULL if no summary function was specified.

Examples

# Simulate from normal distribution
set.seed(200)
x <- rnorm(50, mean = 100, sd = 15)

# Use bootstrap method to estimate
# sampling distribution for the mean
btstrp <- bootstrap(x)

hist(btstrp$replicates,
  main = "",
  xlab = "Sampling distribution - mean"
)
# True mean
abline(v = 100, lwd = 1)


# Estimate of standard error
print(round(sem(x), 1))
#> [1] 1.7

# Estimate of standard error
# computed from bootstrapped samples
btstrp <- bootstrap(x, summary = sd)
print(round(btstrp$summary, 1))
#> [1] 1.7

# 95% confidence interval around the mean
# using bootstrapped samples
f <- function(y) {
  paste(round(quantile(y, c(.025, .975)), 1),
    collapse = " to "
  )
}
btstrp <- bootstrap(x, summary = f)
print(btstrp$summary)
#> [1] "95.4 to 102.1"

# Use bootstrap method to estimate
# sampling distribution for the median
# (which has no close-formed solution)
btstrp <- bootstrap(x, t_x = median)
hist(btstrp$replicates,
  main = "",
  xlab = "Sampling distribution - median"
)