Rev 6808 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{boot}\alias{boot}\alias{boot.return}\alias{c.boot}\title{Bootstrap Resampling}\description{Generate \code{R} bootstrap replicates of a statistic applied to data. Bothparametric and nonparametric resampling are possible. For the nonparametricbootstrap, possible resampling methods are the ordinary bootstrap, thebalanced bootstrap, antithetic resampling, and permutation.For nonparametric multi-sample problems stratified resampling is used:this is specified by including a vector of strata in the call to boot.Importance resampling weights may be specified.}\usage{boot(data, statistic, R, sim = "ordinary", stype = c("i", "f", "w"),strata = rep(1,n), L = NULL, m = 0, weights = NULL,ran.gen = function(d, p) d, mle = NULL, simple = FALSE, ...,parallel = c("no", "multicore", "snow"),ncpus = getOption("boot.ncpus", 1L), cl = NULL)}\arguments{\item{data}{The data as a vector, matrix or data frame. If it is a matrix ordata frame then each row is considered as one multivariateobservation.}\item{statistic}{A function which when applied to data returns a vector containingthe statistic(s) of interest. When \code{sim = "parametric"}, thefirst argument to \code{statistic} must be the data. For eachreplicate a simulated dataset returned by \code{ran.gen} will bepassed. In all other cases \code{statistic} must take at least twoarguments. The first argument passed will always be the originaldata. The second will be a vector of indices, frequencies or weightswhich define the bootstrap sample. Further, if predictions arerequired, then a third argument is required which would be a vectorof the random indices used to generate the bootstrap predictions.Any further arguments can be passed to \code{statistic} through the\code{\dots} argument.}\item{R}{The number of bootstrap replicates. Usually this will be a singlepositive integer. For importance resampling, some resamples may useone set of weights and others use a different set of weights. Inthis case \code{R} would be a vector of integers where eachcomponent gives the number of resamples from each of the rows ofweights.}\item{sim}{A character string indicating the type of simulation required.Possible values are \code{"ordinary"} (the default),\code{"parametric"}, \code{"balanced"}, \code{"permutation"}, or\code{"antithetic"}. Importance resampling is specified byincluding importance weights; the type of importance resampling muststill be specified but may only be \code{"ordinary"} or\code{"balanced"} in this case.}\item{stype}{A character string indicating what the second argument of \code{statistic}represents. Possible values of stype are \code{"i"} (indices - thedefault), \code{"f"} (frequencies), or \code{"w"} (weights). Notused for \code{sim = "parametric"}.}\item{strata}{An integer vector or factor specifying the strata for multi-sampleproblems. This may be specified for any simulation, but is ignoredwhen \code{sim = "parametric"}. When \code{strata} issupplied for a nonparametric bootstrap, the simulations are donewithin the specified strata.}\item{L}{Vector of influence values evaluated at the observations. This isused only when \code{sim} is \code{"antithetic"}. If not supplied,they are calculated through a call to \code{empinf}. This will usethe infinitesimal jackknife provided that \code{stype} is\code{"w"}, otherwise the usual jackknife is used.}\item{m}{The number of predictions which are to be made at each bootstrapreplicate. This is most useful for (generalized) linear models.This can only be used when \code{sim} is \code{"ordinary"}.\code{m} will usually be a single integer but, if there are strata,it may be a vector with length equal to the number of strata,specifying how many of the errors for prediction should come fromeach strata. The actual predictions should be returned as the finalpart of the output of \code{statistic}, which should also take anargument giving the vector of indices of the errors to be used forthe predictions.}\item{weights}{Vector or matrix of importance weights. If a vector then it shouldhave as many elements as there are observations in \code{data}.When simulation from more than one set of weights is required,\code{weights} should be a matrix where each row of the matrix isone set of importance weights. If \code{weights} is a matrix then\code{R} must be a vector of length \code{nrow(weights)}. Thisparameter is ignored if \code{sim} is not \code{"ordinary"} or\code{"balanced"}.}\item{ran.gen}{This function is used only when \code{sim = "parametric"}when it describes how random values are to be generated. It shouldbe a function of two arguments. The first argument should be theobserved data and the second argument consists of any otherinformation needed (e.g. parameter estimates). The second argumentmay be a list, allowing any number of items to be passed to\code{ran.gen}. The returned value should be a simulated data setof the same form as the observed data which will be passed to\code{statistic} to get a bootstrap replicate. It is important that thereturned value be of the same shape and type as the originaldataset. If \code{ran.gen} is not specified, the default is afunction which returns the original \code{data} in which case allsimulation should be included as part of \code{statistic}. Use of\code{sim = "parametric"} with a suitable \code{ran.gen} allows theuser to implement any types of nonparametric resampling which arenot supported directly.}\item{mle}{The second argument to be passed to \code{ran.gen}. Typically thesewill be maximum likelihood estimates of the parameters. Forefficiency \code{mle} is often a list containing all of the objectsneeded by \code{ran.gen} which can be calculated using the originaldata set only.}\item{simple}{logical, only allowed to be \code{TRUE} for\code{sim = "ordinary", stype = "i", n = 0} (otherwise ignored with awarning). By default a \code{n} by \code{R} index array is created:this can be large and if \code{simple = TRUE} this is avoided bysampling separately for each replication, which is slower but usesless memory.}\item{\dots}{Other named arguments for \code{statistic} which are passedunchanged each time it is called. Any such arguments to\code{statistic} should follow the arguments which \code{statistic} isrequired to have for the simulation. Beware of partial matching toarguments of \code{boot} listed above, and that arguments named\code{X} and \code{FUN} cause conflicts in some versions of\pkg{boot} (but not this one).}\item{parallel}{The type of parallel operation to be used (if any). If missing, thedefault is taken from the option \code{"boot.parallel"} (and if thatis not set, \code{"no"}).}\item{ncpus}{integer: number of processes to be used in parallel operation:typically one would chose this to the number of available CPUs.}\item{cl}{An optional \pkg{parallel} or \pkg{snow} cluster for use if\code{parallel = "snow"}. If not supplied, a cluster on thelocal machine is created for the duration of the \code{boot} call.}}\value{The returned value is an object of class \code{"boot"}, containing thefollowing components:\item{t0}{The observed value of \code{statistic} applied to \code{data}.}\item{t}{A matrix with \code{sum(R)} rows each of which is a bootstrap replicateof the result of calling \code{statistic}.}\item{R}{The value of \code{R} as passed to \code{boot}.}\item{data}{The \code{data} as passed to \code{boot}.}\item{seed}{The value of \code{.Random.seed} when \code{boot} started work.}\item{statistic}{The function \code{statistic} as passed to \code{boot}.}\item{sim}{Simulation type used.}\item{stype}{Statistic type as passed to \code{boot}.}\item{call}{The original call to \code{boot}.}\item{strata}{The strata used. This is the vector passed to \code{boot}, if itwas supplied or a vector of ones if there were no strata. It is notreturned if \code{sim} is \code{"parametric"}.}\item{weights}{The importance sampling weights as passed to \code{boot} or the empiricaldistribution function weights if no importance sampling weights werespecified. It is omitted if \code{sim} is not one of\code{"ordinary"} or \code{"balanced"}.}\item{pred.i}{If predictions are required (\code{m > 0}) this is the matrix ofindices at which predictions were calculated as they were passed tostatistic. Omitted if \code{m} is \code{0} or \code{sim} is not\code{"ordinary"}.}\item{L}{The influence values used when \code{sim} is \code{"antithetic"}.If no such values were specified and \code{stype} is not \code{"w"}then \code{L} is returned as consecutive integers corresponding tothe assumption that data is ordered by influence values. Thiscomponent is omitted when \code{sim} is not \code{"antithetic"}.}\item{ran.gen}{The random generator function used if \code{sim} is\code{"parametric"}. This component is omitted for any other valueof \code{sim}.}\item{mle}{The parameter estimates passed to \code{boot} when \code{sim} is\code{"parametric"}. It is omitted for all other values of\code{sim}.}There are \code{c}, \code{plot} and \code{print} methods for this class.}\details{The statistic to be bootstrapped can be as simple or complicated asdesired as long as its arguments correspond to the dataset and (fora nonparametric bootstrap) a vector of indices, frequencies orweights. \code{statistic} is treated as a black box by the\code{boot} function and is not checked to ensure that theseconditions are met.The first order balanced bootstrap is described in Davison, Hinkleyand Schechtman (1986). The antithetic bootstrap is described byHall (1989) and is experimental, particularly when used with strata.The other non-parametric simulation types are the ordinary bootstrap(possibly with unequal probabilities), and permutation which returnsrandom permutations of cases. All of these methods workindependently within strata if that argument is supplied.For the parametric bootstrap it is necessary for the user to specifyhow the resampling is to be conducted. The best way ofaccomplishing this is to specify the function \code{ran.gen} whichwill return a simulated data set from the observed data set and aset of parameter estimates specified in \code{mle}.}\section{Parallel operation}{When \code{parallel = "multicore"} is used (not available on Windows),each worker process inherits the environment of the current session,including the workspace and the loaded namespaces and attachedpackages (but not the random number seed: see below).More work is needed when \code{parallel = "snow"} is used: the workerprocesses are newly created \R processes, and \code{statistic} needsto arrange to set up the environment it needs: often a good way to dothat is to make use of lexical scoping since when \code{statistic} issent to the worker processes its enclosing environment is also sent.(E.g. see the example for \code{\link{jack.after.boot}} whereancillary functions are nested inside the \code{statistic} function.)\code{parallel = "snow"} is primarily intended to be used onmulti-core Windows machine where \code{parallel = "multicore"} is notavailable.For most of the \code{boot} methods the resampling is done in themaster process, but not if \code{simple = TRUE} nor \code{sim ="parametric"}. In those cases (or where \code{statistic} itself usesrandom numbers), more care is needed if the results need to bereproducible. Resampling is done in the worker processes by\code{\link{censboot}(sim = "wierd")} and by most of the schemes in\code{\link{tsboot}} (the exceptions being \code{sim == "fixed"} and\code{sim == "geom"} with the default \code{ran.gen}).Where random-number generation is done in the worker processes, thedefault behaviour is that each worker chooses a separate seed,non-reproducibly. However, with \code{parallel = "multicore"} or\code{parallel = "snow"} using the default cluster, a second approachis used if \code{\link{RNGkind}("L'Ecuyer-CMRG")} has been selected.In that approach each worker gets a different subsequence of the RNGstream based on the seed at the time the worker is spawned and so theresults will be reproducible if \code{ncpus} is unchanged, and for\code{parallel = "multicore"} if \code{parallel::\link{mc.reset.stream}()} iscalled: see the examples for \code{\link{mclapply}}.Note that loading the \pkg{parallel} namespace may change the randomseed, so for maximum reproducibility this should be done beforecalling this function.}\references{There are many references explaining the bootstrap and its variations.Among them are :Booth, J.G., Hall, P. and Wood, A.T.A. (1993) Balanced importance resamplingfor the bootstrap. \emph{Annals of Statistics}, \bold{21}, 286--298.Davison, A.C. and Hinkley, D.V. (1997)\emph{Bootstrap Methods and Their Application}. Cambridge University Press.Davison, A.C., Hinkley, D.V. and Schechtman, E. (1986) Efficient bootstrapsimulation. \emph{Biometrika}, \bold{73}, 555--566.Efron, B. and Tibshirani, R. (1993) \emph{An Introduction to the Bootstrap}.Chapman & Hall.Gleason, J.R. (1988) Algorithms for balanced bootstrap simulations.\emph{ American Statistician}, \bold{42}, 263--266.Hall, P. (1989) Antithetic resampling for the bootstrap. \emph{Biometrika},\bold{73}, 713--724.Hinkley, D.V. (1988) Bootstrap methods (with Discussion).\emph{Journal of the Royal Statistical Society, B}, \bold{50},312--337, 355--370.Hinkley, D.V. and Shi, S. (1989) Importance sampling and the nested bootstrap.\emph{Biometrika}, \bold{76}, 435--446.Johns M.V. (1988) Importance sampling for bootstrap confidence intervals.\emph{Journal of the American Statistical Association}, \bold{83}, 709--714.Noreen, E.W. (1989) \emph{Computer Intensive Methods for Testing Hypotheses}.John Wiley & Sons.}\seealso{\code{\link{boot.array}}, \code{\link{boot.ci}},\code{\link{censboot}}, \code{\link{empinf}},\code{\link{jack.after.boot}}, \code{\link{tilt.boot}},\code{\link{tsboot}}.}\examples{\dontshow{op <- options(digits = 5)}# Usual bootstrap of the ratio of means using the city dataratio <- function(d, w) sum(d$x * w)/sum(d$u * w)boot(city, ratio, R = 999, stype = "w")# Stratified resampling for the difference of means. In this# example we will look at the difference of means between the final# two series in the gravity data.diff.means <- function(d, f){ n <- nrow(d)gp1 <- 1:table(as.numeric(d$series))[1]m1 <- sum(d[gp1,1] * f[gp1])/sum(f[gp1])m2 <- sum(d[-gp1,1] * f[-gp1])/sum(f[-gp1])ss1 <- sum(d[gp1,1]^2 * f[gp1]) - (m1 * m1 * sum(f[gp1]))ss2 <- sum(d[-gp1,1]^2 * f[-gp1]) - (m2 * m2 * sum(f[-gp1]))c(m1 - m2, (ss1 + ss2)/(sum(f) - 2))}grav1 <- gravity[as.numeric(gravity[,2]) >= 7,]boot(grav1, diff.means, R = 999, stype = "f", strata = grav1[,2])# In this example we show the use of boot in a prediction from# regression based on the nuclear data. This example is taken# from Example 6.8 of Davison and Hinkley (1997). Notice also# that two extra arguments to 'statistic' are passed through boot.nuke <- nuclear[, c(1, 2, 5, 7, 8, 10, 11)]nuke.lm <- glm(log(cost) ~ date+log(cap)+ne+ct+log(cum.n)+pt, data = nuke)nuke.diag <- glm.diag(nuke.lm)nuke.res <- nuke.diag$res * nuke.diag$sdnuke.res <- nuke.res - mean(nuke.res)# We set up a new data frame with the data, the standardized# residuals and the fitted values for use in the bootstrap.nuke.data <- data.frame(nuke, resid = nuke.res, fit = fitted(nuke.lm))# Now we want a prediction of plant number 32 but at date 73.00new.data <- data.frame(cost = 1, date = 73.00, cap = 886, ne = 0,ct = 0, cum.n = 11, pt = 1)new.fit <- predict(nuke.lm, new.data)nuke.fun <- function(dat, inds, i.pred, fit.pred, x.pred){lm.b <- glm(fit+resid[inds] ~ date+log(cap)+ne+ct+log(cum.n)+pt,data = dat)pred.b <- predict(lm.b, x.pred)c(coef(lm.b), pred.b - (fit.pred + dat$resid[i.pred]))}nuke.boot <- boot(nuke.data, nuke.fun, R = 999, m = 1,fit.pred = new.fit, x.pred = new.data)# The bootstrap prediction squared error would then be found bymean(nuke.boot$t[, 8]^2)# Basic bootstrap prediction limits would benew.fit - sort(nuke.boot$t[, 8])[c(975, 25)]# Finally a parametric bootstrap. For this example we shall look# at the air-conditioning data. In this example our aim is to test# the hypothesis that the true value of the index is 1 (i.e. that# the data come from an exponential distribution) against the# alternative that the data come from a gamma distribution with# index not equal to 1.air.fun <- function(data) {ybar <- mean(data$hours)para <- c(log(ybar), mean(log(data$hours)))ll <- function(k) {if (k <= 0) 1e200 else lgamma(k)-k*(log(k)-1-para[1]+para[2])}khat <- nlm(ll, ybar^2/var(data$hours))$estimatec(ybar, khat)}air.rg <- function(data, mle) {# Function to generate random exponential variates.# mle will contain the mean of the original dataout <- dataout$hours <- rexp(nrow(out), 1/mle)out}air.boot <- boot(aircondit, air.fun, R = 999, sim = "parametric",ran.gen = air.rg, mle = mean(aircondit$hours))# The bootstrap p-value can then be approximated bysum(abs(air.boot$t[,2]-1) > abs(air.boot$t0[2]-1))/(1+air.boot$R)\dontshow{options(op)}}\keyword{nonparametric}\keyword{htest}