c("boot")
wants <- wants %in% rownames(installed.packages())
has <-if(any(!has)) install.packages(wants[!has])
set.seed(123)
18
n1 <- 21
n2 <- rnorm(n1, 180, 10)
DVm <- rnorm(n2, 175, 6)
DVf <- data.frame(DV=c(DVm, DVf),
tDf <-IV=factor(rep(c("m", "f"), c(n1, n2))))
Function to return difference between group means.
function(dat, idx) {
getDM <- aggregate(DV ~ IV, data=dat, subset=idx, FUN=mean)
Mfm <--diff(Mfm$DV)
}
Bootstrap with strata
option for stratification.
library(boot)
boot(tDf, statistic=getDM, strata=tDf$IV, R=999)
bsTind <-boot.ci(bsTind, conf=0.95, type=c("basic", "bca"))
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 999 bootstrap replicates
CALL :
boot.ci(boot.out = bsTind, conf = 0.95, type = c("basic", "bca"))
Intervals :
Level Basic BCa
95% (-11.487, -1.601 ) (-11.243, -1.586 )
Calculations and Intervals on Original Scale
Compare with parametric confidence interval
t.test(DV ~ IV, alternative="two.sided", var.equal=TRUE, data=tDf)
tt <-$conf.int tt
[1] -11.608769 -1.522242
attr(,"conf.level")
[1] 0.95
try(detach(package:boot))
R markdown - markdown - R code - all posts