Skip to content
Prev 2519 / 7420 Next

Bootstrapping with pseudo-replicates

On Thu, Nov 17, 2011 at 9:02 AM, Johannes Radinger <JRadinger at gmx.at> wrote:
I'd do it like this:

library(plyr)

y <- c(1,5,6,2,5,10) # response
x <- c(2,12,8,1,16,17) # predictor
group <- factor(c(1,2,2,3,4,4)) # group
df <- data.frame(y,x,group)

sample_rows <- function(df) df[sample.int(nrow(df), replace = TRUE), ]
sample_groups <- function(df) ddply(df, "group", sample_rows)

# Generate reps
reps <- rdply(50, sample_groups(df))

# Fit models
mods <- dlply(reps, ".n", function(df) lm(y ~ x, data = df))

# Extract coefficients
coefs <- ldply(mods, function(mod) {
  cs <- as.data.frame(coef(summary(mod)))
  cs$Term <- rownames(cs)
  cs
})


I find it much easier to generate all reps, then all models, then
extract all coefficients, rather than working rep-by-rep.

Hadley