After applying a model between one response variable and several exlanatory variables across a dataframe, I would like to rank each model by the AIC score. I have encountered a very similar question that does exactly what I want to do. Using lapply on a list of models, but it does not seem to work for me and I'm not sure why. Here's an example using the mtcars dataset:
lm_multiple <- lapply(mtcars[,-1], function(x) summary(lm(mtcars$mpg ~ x)))
An approved answer from the link above suggested:
sapply(X = lm_multiple, FUN = AIC)
But this does not work for me, I get this warning message.
Error in UseMethod("logLik") :
no applicable method for 'logLik' applied to an object of class "summary.lm"
Here is an answer from the original question...
x <- seq(1:10)
y <- sin(x)^2
model.list <- list(model1 = lm(y ~ x),
model2 = lm(y ~ x + I(x^2) + I(x^3)))
sapply(X = model.list, FUN = AIC)