c("boot")
wants <- wants %in% rownames(installed.packages())
has <-if(any(!has)) install.packages(wants[!has])
boot
set.seed(123)
100
muH0 <- 40
sdH0 <- 200
N <- rnorm(N, muH0, sdH0) DV <-
Function to calculate the mean and uncorrected variance (=plug-in estimator for the population variance) of a given replication.
function(orgDV, idx) {
getM <- length(orgDV[idx])
n <- mean(orgDV[idx])
bsM <- (((n-1) / n) * var(orgDV[idx])) / n
bsS2M <-c(bsM, bsS2M)
}
library(boot)
999
nR <- boot(DV, statistic=getM, R=nR)) (bsRes <-
ORDINARY NONPARAMETRIC BOOTSTRAP
Call:
boot(data = DV, statistic = getM, R = nR)
Bootstrap Statistics :
original bias std. error
t1* 99.657182 0.1252697500 2.5831186
t2* 7.080822 0.0008318941 0.7763612
Various types of bootstrap confidence intervals
0.05
alpha <-boot.ci(bsRes, conf=1-alpha, type=c("basic", "perc", "norm", "stud", "bca"))
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 999 bootstrap replicates
CALL :
boot.ci(boot.out = bsRes, conf = 1 - alpha, type = c("basic",
"perc", "norm", "stud", "bca"))
Intervals :
Level Normal Basic Studentized
95% ( 94.47, 104.59 ) ( 94.59, 104.84 ) ( 94.77, 105.09 )
Level Percentile BCa
95% ( 94.48, 104.72 ) ( 94.20, 104.56 )
Calculations and Intervals on Original Scale
For the \(t\) test statistic, compare the empirical distribution from the bootstrap replicates against the theoretical \(t_{n-1}\) distribtion.
replicate(nR, getM(DV, sample(seq(along=DV), replace=TRUE)))
res <- res[1, ]
Mstar <- sqrt(res[2, ])
SMstar <- (Mstar-mean(DV)) / SMstar tStar <-
plot(tStar, ecdf(tStar)(tStar), col="gray60", pch=1, xlab="t* bzw. t",
ylab="P(T <= t)", main="t*: cumulative rel. frequency and t CDF")
curve(pt(x, N-1), lwd=2, add=TRUE)
legend(x="topleft", lty=c(NA, 1), pch=c(1, NA), lwd=c(2, 2),
col=c("gray60", "black"), legend=c("t*", "t"))
boot.array(boot(...), indices=TRUE)
gives detailed information about the selected indices for each bootstrap replication. If the sample has \(n\) observations, and there are \(R\) replications, the result is an \((R \times n)\)-matrix with one row for each replication and one column for each observation.
boot.array(bsRes, indices=TRUE)
bootIdx <-
# replications 1-3: first 10 selected indices in each replication
1:3, 1:10] bootIdx[
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 198 48 130 122 114 89 100 178 103 144
[2,] 28 47 181 101 164 69 20 111 35 46
[3,] 182 47 147 101 152 104 58 118 52 33
# selected indices in the first replication
bootIdx[1, ]
repl1Idx <-
# selected values in the first replication
DV[repl1Idx]
repl1DV <-head(repl1DV, n=5)
[1] 49.94915 81.33379 97.14768 62.10102 97.77752
try(detach(package:boot))
R markdown - markdown - R code - all posts