3

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)
merv
  • 67,214
  • 13
  • 180
  • 245
James White
  • 705
  • 2
  • 7
  • 20
  • This exercise is futile. AIC is useful for comparing nested models that were fit to the same dataset. Your models are not nested. – Roland Jul 07 '15 at 10:52
  • @Roland thanks for the comment, what would be an alternative? p-values? In my dataset I have a large number (>100) explanatory variables – James White Jul 07 '15 at 11:46
  • I don't know why you want to do this. You should probably ask on stats.stackexchange.com. – Roland Jul 07 '15 at 13:25
  • http://stats.stackexchange.com/questions/160294/is-it-feasible-to-rank-several-unnested-generalized-additive-and-linear-models Thanks, I actually just finished putting it there, it may be more clear here. – James White Jul 07 '15 at 13:29

1 Answers1

1

you should remove the summary like this

lm_multiple <- lapply(mtcars[,-1], function(x) lm(mtcars$mpg ~ x))
sapply(X = lm_multiple, FUN = AIC)
Mamoun Benghezal
  • 5,264
  • 7
  • 28
  • 33