wants <- c("boot")
has <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])
set.seed(123)
n1 <- 18
n2 <- 21
DVm <- rnorm(n1, 180, 10)
DVf <- rnorm(n2, 175, 6)
tDf <- data.frame(DV=c(DVm, DVf),
IV=factor(rep(c("m", "f"), c(n1, n2))))
Function to return difference between group means.
getDM <- function(dat, idx) {
Mfm <- aggregate(DV ~ IV, data=dat, subset=idx, FUN=mean)
-diff(Mfm$DV)
}
Bootstrap with strata
option for stratification.
library(boot)
bsTind <- boot(tDf, statistic=getDM, strata=tDf$IV, R=999)
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.380, -1.413 ) (-11.627, -1.630 )
Calculations and Intervals on Original Scale
Compare with parametric confidence interval
[1] -11.608769 -1.522242
attr(,"conf.level")
[1] 0.95
R markdown - markdown - R code - all posts