Reputation: 33
I have 117 models, named m1, m2, m3, ..., m117. I need to compare their AIC using model.sel in the MuMIn package. The syntax for model comparison is model.sel(object, ...) in which the object is the list of models. Thus, I tried to create a loop to read all the 117 models into a list:
list<-list()
for (i in 1:117)
{
list[[i]]<- cat(paste("f",i))
}
however, all I have is
f 1f 2f 3f 4f 5f 6f 7f 8f 9f 10f 11f 12f 13f 14f 15f 16f 17f 18f 19f 20f 21f 22f 23f 24f 25f 26f 27f 28f 29f 30f 31f 32f 33f 34f 35f 36f 37f 38f 39f 40f 41f 42f 43f 44f 45f 46f 47f 48f 49f 50f 51f 52f 53f 54f 55f 56f 57f 58f 59f 60f 61f 62f 63f 64f 65f 66f 67f 68f 69f 70f 71f 72f 73f 74f 75f 76f 77f 78f 79f 80f 81f 82f 83f 84f 85f 86f 87f 88f 89f 90f 91f 92f 93f 94f 95f 96f 97f 98f 99f 100f 101f 102f 103f 104f 105f 106f 107f 108f 109f 110f 111f 112f 113f 114f 115f 116f 117
I think with my codes, R only read characters into the list, not the objects (models) that I have. Any help is appreciated. I don't even know if I should use the loop in the first place.
Upvotes: 3
Views: 4552
Reputation: 7153
Here's a solution using 2 linear regression on iris data.
The models:
m1<-lm(Petal.Length ~ 1, data=iris)
m2 <- lm(Petal.Length ~ Sepal.Length, data=iris)
models <- paste0("m", 1:2)
To get the AIC value as a vector:
sapply(models, function(x)AIC(get(x)))
m1 m2
599.1741 387.1350
To create a list of linear regression model:
lapply(models, function(x)get(x))
[[1]]
Call:
lm(formula = Petal.Length ~ 1, data = iris)
Coefficients:
(Intercept)
3.758
[[2]]
Call:
lm(formula = Petal.Length ~ Sepal.Length, data = iris)
Coefficients:
(Intercept) Sepal.Length
-7.101 1.858
Upvotes: 6