Stratified bootstrapping

TODO

  • link to resamplingBoot

Install required packages

boot

wants <- c("boot")
has   <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])

Stratified sampling for two independent groups

Confidence interval for \(\mu_{2} - \mu_{1}\)

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.487,  -1.601 )   (-11.243,  -1.586 )  
Calculations and Intervals on Original Scale

Compare with parametric confidence interval

tt <- t.test(DV ~ IV, alternative="two.sided", var.equal=TRUE, data=tDf)
tt$conf.int
[1] -11.608769  -1.522242
attr(,"conf.level")
[1] 0.95

Detach (automatically) loaded packages (if possible)

try(detach(package:boot))

Get the article source from GitHub

R markdown - markdown - R code - all posts