Learn R Programming

resample (version 0.2)

bootstrap: One and two sample bootstrap sampling and permutation tests.

Description

Basic resampling. Supply the data and statistic to resample.

Usage

bootstrap(data, statistic, B = 1000,
          args.stat = NULL, seed = NULL, sampler = samp.bootstrap,
          label = NULL, statisticNames = NULL, block.size = 100,
          trace = FALSE)
bootstrap2(data, statistic, treatment, data2 = NULL, B = 1000,
          ratio = FALSE,
          args.stat = NULL, seed = NULL, sampler = samp.bootstrap,
          label = NULL, statisticNames = NULL, block.size = 100,
          trace = FALSE)
permutationTest(data, statistic, B = 999,
          alternative = "two.sided", resampleColumns = NULL,
          args.stat = NULL, seed = NULL, sampler = samp.permute,
          label = NULL, statisticNames = NULL, block.size = 100,
          trace = FALSE)
permutationTest2(data, statistic, treatment, data2 = NULL, B = 999,
          alternative = "two.sided", ratio = FALSE, paired = FALSE,
          args.stat = NULL, seed = NULL, sampler = samp.permute,
          label = NULL, statisticNames = NULL, block.size = 100,
          trace = FALSE)

Arguments

data
vector, matrix, or data frame.
statistic
a function, or expression (e.g. mean(myData, trim = .2).
B
number of replicates (bootstrap samples or permutation resamples).
treatment
a vector with two unique values. For two-sample applications, suppy either treatment or data2.
data2
an object like data; the second sample.
alternative
one of "two.sided", "greater", or "less". If statistic returns a vector, this may be a vector of the same length.
ratio
logical, if FALSE then statistics for two samples are combined using statistic1 - statistic2 (the statistics from the two samples). If TRUE, it uses statistic1 / statistic2.
resampleColumns
integer, or character (a subset of the column names of data); if supplied then only these columns of the data are permuted. For example, for a permutation test of the correlation of x and y, only one of the variables should be per
args.stat
a list of additional arguments to pass to statistic, if it is a function.
paired
logical, if TRUE then observations in data and data2 are paired, and permutations are done within each pair. Not yet implemented.
seed
old value of .Random.seed, or argument to set.seed.
sampler
a function for resampling, see help(samp.bootstrap).
label
used for labeling plots (in a future version).
statisticNames
a character vector the same length as the vector returned by statistic, giving names.
block.size
integer. The B replicates are done this many at a time.
trace
logical, if TRUE an indication of progress is printed.

Value

  • a list with class that matches the name of the function (e.g. "bootstrap2"), that inherits from "resample", with components:
  • observedthe value of the statistic for the original data.
  • replicatesa matrix with B rows and p columns.
  • nnumber of observations in the original data, or vector of length 2 in two-sample problems.
  • plength(observed).
  • Bnumber of replications.
  • seedthe value of the seed at the start of sampling.
  • callthe matched call.
  • statisticsa data frame with p rows, with columns "observed", "mean" (the mean of the replicates), and other columns appropriate to resampling; e.g. the bootstrap objects have columns "SE" and "Bias", while the permutation test objects have "Alternative" and "PValue".
  • There are functions for printing and plotting these objects, in particular print, plot, hist, qqnorm, quantile.

code

data2

itemize

  • statistic = colMeans(data)

item

  • statistic = mean(myData$x)
  • statistic = mean(myData[, "x"])

Details

There is considerable flexibility in how you specify the data and statistic.

For the statistic, you may supply a function, or an expression. For example, if data = x, you may specify any of

  • statistic = mean
statistic = mean(x) statistic = mean(data)

See Also

resample-package, samp.bootstrap, limits.percentile, limits.t.

Examples

Run this code
mydata <- data.frame(a = runif(40), b = rnorm(40))
boot1 <- bootstrap(mydata, colMeans)
boot1
par(mfrow = c(2,1))
plot(boot1)
qqnorm(boot1)
par(mfrow = c(1,1))

boot2 <- bootstrap(mydata, mean(a))
boot2

Run the code above in your browser using DataLab