How can I index multiple linear models? (Without getting a warning.)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How can I index multiple linear models? (Without getting a warning.)

BSanders

My code looks like this:


lin = NA

for(i in 1:15){
lin[i] = lm(reservesub[,3]~ reservesub[,i+3])
}

For which I'm given 15 warning messages which say :

"1: In lin[i] = lm(reservesub[, 3] ~ reservesub[, i + 3]) :
  number of items to replace is not a multiple of replacement length"

I'm am able to generate the 15 different models and get the coefficients. I am able to access
each coefficient, for example , the slope of the third model can be called using lin[[3]][2], which is helpful, but none of the other objects that go along with the linear model, like residuals, are accessible (for me.)

I feel like I'm missing something kind of fundamental here, but I can't put my finger on it.

Thanks in advance.

Barry Sanders.

Reply | Threaded
Open this post in threaded view
|

Re: How can I index multiple linear models? (Without getting a warning.)

Dieter Menne
BSanders wrote
.....
lin[i] = lm(reservesub[,3]~ reservesub[,i+3])

For which I'm given 15 warning messages which say :

"1: In lin[i] = lm(reservesub[, 3] ~ reservesub[, i + 3]) :
  number of items to replace is not a multiple of replacement length"


lin = list()
for(i in 1:15){
  b = rnorm(10)
  q = rnorm(10)
  #lin[i] = lm(b~q) #warnings
  lin[[i]] = lm(b~q) #warnings
}


Dieter