[R-sig-eco] Bootstrapping with pseudo-replicates

Hadley Wickham hadley at rice.edu
Fri Nov 18 14:28:31 CET 2011


On Thu, Nov 17, 2011 at 9:02 AM, Johannes Radinger <JRadinger at gmx.at> wrote:
> Hello Dixon,
>
> As there is no real predefined function for doing that resampling and regression what I want I tried to work on my own code. So far I get a code which is working in a for-loop. There is still a problem because I don't know how to collect the results-vector for each loop step into a data frame or list etc.
>
> Maybe someone can help me. So far I got following:

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

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-sig-ecology mailing list