Is this possible? I can get mapply to work with the help examples, but I can’t get a trivial example with lm to work. Here’s my attempt which returns a matrix, instead of a list of lm objects.
temp.df <- list(
data.frame(a = rep(1:10, each = 10), b = 1:100, c = rnorm(100), d = rnorm(100, 2))
)
temp.df[[2]] <- subset(temp.df[[1]], a > 2)
temp.mod <- list(a ~ b,
a ~ b + c,
a ~ b + c + d)
temp.lm <- mapply(lm, formula = temp.mod, data = temp.df[c(1,1,2)])
temp.sum <- lapply(temp.lm, summary)
Should I just stick with lapply and specifying data = each time? Thanks!
Not sure what you mean by specifying data each time, but if you pack everything into a bigger (nested) list, and write your own wrapper function that calls lm() and summary(), lapply is a good option:
Edit: Just to follow up on the mapply behavior, when I run your code, I get a 12×3 matrix that actually does contain all the relevant lm object information, but the class attribute has been lost. Resetting that and then lapply-ing summary() works with your original code, I believe. But I think a nested list of arguments and lapply() is simpler in this case.