code below first prints out lm for mpg ~ disp then for mpg ~ disp + wt. I would like to create another loop over the models (note that the second lm is my personalize model, and for the simplicity, we can assume it is lm). how can I loop over different models?
data("mtcars")
formulas <- list(
mpg ~ disp,
mpg ~ disp + wt
)
models <- list(lm, lm)
res <- vector("list", length = length(formulas))
for(i in seq_along(formulas)){
res[[i]] <- lm(formulas[[i]], data = mtcars)
}
res
or
lapply(formulas, lm, data = mtcars)