bootstrap: Non-Parametric Bootstrap

View source: R/S05_Statistics.R

bootstrapR Documentation

Non-Parametric Bootstrap

Description

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.

Usage

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))

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

# 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)

# 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"
)

rettopnivek/arfpam documentation built on Oct. 20, 2024, 7:24 p.m.