Search code examples
rloopsglm

How loop glm models for model selection (model.sel)


i'm trying to select the best model through it's AIC. I was given this codes which i was told it's makes glm models combination automatically, but i really don't understand how it's works.

Could anyone could explain me how it works in simple words and how can make it run, because i couldn't.

library(MuMIn)

as.list(rep(NA, 44)) ->models

for (i in 1:44) {
  glm(log(Tbra+1)~bats[,i],data=bats)-> models[[i]]
}

names(models)<-names(bats)[7:50]

model.sel(models)

EDIT 1

I have more questions about this code.

  1. as.list(rep(NA, 44)) ->models

What means (NA, 44)? How the list is made? I wondered that the list have to be done from the data base but this code made an empty list, which i don't understand why.

2.

for (i in 1:44) {
  glm(log(Tbra+1)~bats[,i],data=bats)-> models[[i]]
}

I understand that the glm is made by the "bats" data, but it's requires that Bats is list, right? so how it's made. At first i thought that perhaps it refers to a list made by the as.list like:

as.list(rep(NA, 44)) ->bats

  for (i in 1:44) {
      glm(log(Tbra+1)~bats[,i],data=bats)-> models[[i]]
    }

But it doesn't make a thing. Any thoughts?


Solution

  • The loop makes a list of models. Each model tries to predict the response log(Tbra + 1) using a single variable from the data. Then the function model.sel() takes in the list, evaluates each one's AIC and then displays them on a table along with the coefficients for each variable, the log likelihood, etc.