c("boot")
wants <- wants %in% rownames(installed.packages())
has <-if(any(!has)) install.packages(wants[!has])
set.seed(123)
100
N <- rnorm(N, 175, 7)
X1 <- rnorm(N, 30, 8)
X2 <- abs(rnorm(N, 60, 30))
X3 <- 0.5*X1 - 0.3*X2 - 0.4*X3 + 10 + rnorm(N, 0, 3)
Y <- data.frame(X1, X2, X3, Y) dfRegr <-
lm(Y ~ X1 + X2 + X3, data=dfRegr)) (fit <-
Call:
lm(formula = Y ~ X1 + X2 + X3, data = dfRegr)
Coefficients:
(Intercept) X1 X2 X3
13.9252 0.4762 -0.2827 -0.4057
sqrt(diag(vcov(fit)))
(Intercept) X1 X2 X3
9.05357721 0.05008996 0.04104598 0.01122308
confint(fit)
2.5 % 97.5 %
(Intercept) -4.0460232 31.8963943
X1 0.3768077 0.5756632
X2 -0.3641438 -0.2011926
X3 -0.4280163 -0.3834611
function(dat, idx) {
getRegr <- lm(Y ~ X1 + X2 + X3, subset=idx, data=dat)
bsFit <-coef(bsFit)
}
library(boot)
999
nR <- boot(dfRegr, statistic=getRegr, R=nR)) (bsRegr <-
ORDINARY NONPARAMETRIC BOOTSTRAP
Call:
boot(data = dfRegr, statistic = getRegr, R = nR)
Bootstrap Statistics :
original bias std. error
t1* 13.9251855 0.0965488381 8.00579816
t2* 0.4762354 -0.0003379187 0.04459103
t3* -0.2826682 -0.0007962273 0.03970876
t4* -0.4057387 -0.0003034646 0.01168983
boot.ci(bsRegr, conf=0.95, type="bca", index=1)$bca
conf
[1,] 0.95 16.47 964.1 -3.211747 28.48014
boot.ci(bsRegr, conf=0.95, type="bca", index=2)$bca
conf
[1,] 0.95 32.58 981.51 0.3918115 0.568677
boot.ci(bsRegr, conf=0.95, type="bca", index=3)$bca
conf
[1,] 0.95 20.67 969.91 -0.3723436 -0.2112341
boot.ci(bsRegr, conf=0.95, type="bca", index=4)$bca
conf
[1,] 0.95 23.16 973.1 -0.4292687 -0.3836797
Under the null hypothesis
4
P <- c(41, 37, 42, 40)
Nj <- rep(c(-1, 0, 1, 2), Nj)
muJ <- data.frame(IV=factor(rep(LETTERS[1:P], Nj)),
dfCRp <-DV=rnorm(sum(Nj), muJ, 6))
anova(lm(DV ~ IV, data=dfCRp))
anBase <- anBase["IV", "F value"]
Fbase <- anBase["IV", "Pr(>F)"]) (pBase <-
[1] 0.218359
lm(DV ~ 1, data=dfCRp) ## fit 0-model
fit0 <- residuals(fit0) ## residuals
E <- E / sqrt(1-hatvalues(fit0)) ## rescaled residuals
Er <- fitted(fit0) ## prediction
Yhat <-
function(dat, idx) {
getAnova <- Yhat + Er[idx]
Ystar <- anova(lm(Ystar ~ IV, data=dat))
anBS <-"IV", "F value"]
anBS[
}
library(boot)
999
nR <- boot(dfCRp, statistic=getAnova, R=nR)) (bsAnova <-
ORDINARY NONPARAMETRIC BOOTSTRAP
Call:
boot(data = dfCRp, statistic = getAnova, R = nR)
Bootstrap Statistics :
original bias std. error
t1* 1.493977 -0.4779045 0.8281859
bsAnova$t
Fstar <-
# don't use >= because of floating point arithmetic problems
.Machine$double.eps^0.5
tol <- (Fstar > Fbase) | (abs(Fstar-Fbase) < tol)
FsIsGEQ <- (sum(FsIsGEQ) + 1) / (length(Fstar) + 1)) (pValBS <-
[1] 0.215
plot(Fstar, ecdf(Fstar)(Fstar), col="gray60", pch=1, xlab="f* bzw. f",
ylab="P(F <= f)", main="F*: cumulative rel. freqs and F CDF")
curve(pf(x, P-1, sum(Nj) - P), 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("F*", "F"))
Under the null hypothesis
function(dat, idx) {
getAnovaWild <- length(idx) ## size of replication
n <-## 1st choice for random variate U: Rademacher-variables
sample(c(-1, 1), size=n, replace=TRUE, prob=c(0.5, 0.5))
Ur <-
## 2nd option for choosing random variate U
sample(c(-(sqrt(5) - 1)/2, (sqrt(5) + 1)/2), size=n, replace=TRUE,
Uf <-prob=c((sqrt(5) + 1)/(2*sqrt(5)), (sqrt(5) - 1)/(2*sqrt(5))))
Yhat + (Er*Ur)[idx] ## for E* with Rademacher-variables
Ystar <-# Ystar <- Yhat + (Er*Uf)[idx] ## for E* with 2nd option
anova(lm(Ystar ~ IV, data=dat))
anBS <-"IV", "F value"]
anBS[ }
library(boot)
999
nR <- boot(dfCRp, statistic=getAnovaWild, R=nR)
bsAnovaW <- bsAnovaW$t
FstarW <-
# don't use >= because of floating point arithmetic problems
.Machine$double.eps^0.5
tol <- (FstarW > Fbase) | (abs(FstarW-Fbase) < tol)
FsIsGEQ <- (sum(FsIsGEQ) + 1) / (length(FstarW) + 1)) (pValBSw <-
[1] 0.211
try(detach(package:boot))
R markdown - markdown - R code - all posts