2
votes

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)
1
This exercise is futile. AIC is useful for comparing nested models that were fit to the same dataset. Your models are not nested.Roland
@Roland thanks for the comment, what would be an alternative? p-values? In my dataset I have a large number (>100) explanatory variablesJames White
I don't know why you want to do this. You should probably ask on stats.stackexchange.com.Roland
stats.stackexchange.com/questions/160294/… Thanks, I actually just finished putting it there, it may be more clear here.James White

1 Answers

1
votes

you should remove the summary like this

lm_multiple <- lapply(mtcars[,-1], function(x) lm(mtcars$mpg ~ x))
sapply(X = lm_multiple, FUN = AIC)