bootstat = bootstrp(nboot,bootfun,d1,...)
[bootstat,bootsam] = bootstrp(...)
bootstat = bootstrp(...,'Name',Value)
bootstat = bootstrp(nboot,bootfun,d1,...) draws nboot bootstrap data samples, computes statistics on each sample using bootfun, and returns the results in the matrix bootstat. nboot must be a positive integer. bootfun is a function handle specified with @. Each row of bootstat contains the results of applying bootfun to one bootstrap sample. If bootfun returns a matrix or array, then this output is converted to a row vector for storage in bootstat.
The third and later input arguments (d1,...) are data (scalars, column vectors, or matrices) used to create inputs to bootfun. bootstrp creates each bootstrap sample by sampling with replacement from the rows of the non-scalar data arguments (these must have the same number of rows). bootfun accepts scalar data unchanged.
[bootstat,bootsam] = bootstrp(...) returns an n-by-nboot matrix of bootstrap indices, bootsam. Each column in bootsam contains indices of the values that were drawn from the original data sets to constitute the corresponding bootstrap sample. For example, if d1,... each contain 16 values, and nboot = 4, then bootsam is a 16-by-4 matrix. The first column contains the indices of the 16 values drawn from d1,..., for the first of the four bootstrap samples, the second column contains the indices for the second of the four bootstrap samples, and so on. (The bootstrap indices are the same for all input data sets.) To get the output samples bootsam without applying a function, set bootfun to empty ().
bootstat = bootstrp(...,'Name',Value) uses additional arguments specified by one or more Name,Value pair arguments. The name-value pairs must appear after the data arguments. The available name-value pairs:
'Weights' — Observation weights. The weights value must be a vector of nonnegative numbers with at least one positive element. The number of elements in weights must be equal to the number of rows in non-scalar input arguments to bootstrp. To obtain one bootstrap replicate, bootstrp samples N out of N with replacement using these weights as multinomial sampling probabilities.
'Options' — The value is a structure that contains options specifying whether to compute bootstrap iterations in parallel, and specifying how to use random numbers during the bootstrap sampling. Create the options structure with statset. Applicable statset parameters are:
'UseParallel' — If true and if a parpool of the Parallel Computing Toolbox™ is open, compute bootstrap iterations in parallel. If the Parallel Computing Toolbox is not installed, or a parpool is not open, computation occurs in serial mode. Default is false, meaning serial computation.
UseSubstreams — Set to true to compute in parallel in a reproducible fashion. Default is false. To compute reproducibly, set Streams to a type allowing substreams: 'mlfg6331_64' or 'mrg32k3a'.
Streams — A RandStream object or cell array of such objects. If you do not specify Streams, bootstrp uses the default stream or streams. If you choose to specify Streams, use a single object except in the case
You have an open Parallel pool
UseParallel is true
UseSubstreams is false
In that case, use a cell array the same size as the Parallel pool.
Load a data set containing the LSAT scores and law-school GPA for 15 students. These 15 data points are resampled to create 1000 different data sets, and the correlation between the two variables is computed for each data set.
load lawdata [bootstat,bootsam] = bootstrp(1000,@corr,lsat,gpa);
Display the first 5 bootstrapped correlation coefficients.
bootstat(1:5,:) ans = 0.6600 0.7969 0.5807 0.8766 0.9197
Display the indices of the data selected for the first 5 bootstrap samples.
bootsam(:,1:5) ans = 9 8 15 11 15 14 7 6 7 14 4 6 10 3 11 3 10 11 9 2 15 4 13 4 14 9 4 5 2 10 8 5 4 3 13 1 9 1 15 11 10 8 6 12 3 1 4 5 2 8 1 1 10 6 2 3 10 15 10 8 14 6 10 3 8 13 12 1 2 4 12 6 4 9 8 hist(bootstat)
The histogram shows the variation of the correlation coefficient across all the bootstrap samples. The sample minimum is positive, indicating that the relationship between LSAT score and GPA is not accidental.
Finally, compute a bootstrap standard of error for the estimated correlation coefficient.
se = std(bootstat) se = 0.1327
Compute a sample of 100 bootstrapped means of random samples taken from the vector Y, and plot an estimate of the density of these bootstrapped means:
y = exprnd(5,100,1); m = bootstrp(100,@mean,y); [fi,xi] = ksdensity(m); plot(xi,fi);
Compute a sample of 100 bootstrapped means and standard deviations of random samples taken from the vector Y, and plot the bootstrap estimate pairs:
y = exprnd(5,100,1); stats = bootstrp(100,@(x)[mean(x) std(x)],y); plot(stats(:,1),stats(:,2),'o')
Estimate the standard errors for a coefficient vector in a linear regression by bootstrapping residuals:
load hald x = [ones(size(heat)),ingredients]; y = heat; b = regress(y,x); yfit = x*b; resid = y - yfit; se = std(bootstrp(... 1000,@(bootr)regress(yfit+bootr,x),resid));