Is there a simple way to extract residuals from a list of lm objects generated by lapply()?
I am attempting to run separate lm models to correct for covariates for a list of variables in a dataset split by sex and event. I've been able to use lapply() to create a list of lm objects for each variable and data subset, and I would now like to extract the residuals from these models to join with the original data frame. I have found a long form way to do this, but I'm sure there's a simpler solution.
UPDATE/EDIT This produces a simplified data set with the same structure for three variables and one covariate:
set.seed(42)
n <- 6
dat <- data.frame(id=rep(c(1:n),3),
sex=rep(rep(c("F","M"),n/2),3),
event=c(rep(1,n),rep(2,n),rep(3,n)),
a=rnorm(n*3),
b=rnorm(n*3),
e=rnorm(n*3),
y=rnorm(n*3))
dat$sex.event<-paste0(dat$sex,dat$event)
myvars<-as.list(c("a","b","e"))
by.sex.event<-split(dat,dat$sex.event)
my_reg<-function(x){lapply(by.sex.event,function(dd)lm(get(x)~y,
na.action=na.exclude,data=dd))}
models<-lapply(myvars,my_reg)
The long form solution I have found is to run this line separately for each variable (a/models[[1]], b/models[[2]], e/models[[3]]) and data subset ($F1, $F2, $F3, $M1, $M2, $M3):
## rejoin residuals with data by sex.event
by.sex.event$F1$a_resid<-residuals(models[[1]]$F1)
To then transform back to the original data structure with residuals for each variable added using:
## return data to original (unsplit) form
dat<-do.call("rbind",by.sex.event)
I hope the clarifies the structural issues I'm encountering!