[R] storing objects (lm results) in an array

Spencer Graves spencer.graves at pdf.com
Wed Sep 24 20:27:38 CEST 2003


As you noted, the output of, e.g., "lm" is a list.  When I've done many 
fits like this, I've usely been interested in only a few components of 
the list.  In that case, I first construct a matrix or higher 
dimensional array and then store what I want in that array.  Example: 

 > DF0 <- data.frame(y1=rnorm(9), y2=rnorm(9))
 > coefs <- array(NA, dim=c(2, 1))
 > for(i in 1:2){
+ mdl <- paste("y", i, " ~ 1", sep="")
+ fit <- lm(formula(mdl), DF0)
+ coefs[i,] <- fit$coefficients
+ }
 > coefs
           [,1]
[1,]  0.5439368
[2,] -0.1862047

hope this helps.  spencer graves

Jason Turner wrote:

>On Thu, 2003-09-25 at 05:34, Christoph Lehmann wrote:
>  
>
>>Hi
>>
>>I have calculated lots (>1000) of linear models and would like to store
>>each single result as an element of a 3D matrix or a similar storage:
>>something like
>>
>>glm[i][j][k] = lm(...)
>>
>>Since I read that results are lists: Is it possible to define a matrix
>>of type list? 
>>
>>Or what do you recommend?
>>    
>>
>
>1) glm is already a function name in R.  I'd suggest a different name.
>
>2) I don't recommend a matrix.  Matricies require contiguous memory
>chunks - it's possible to not have a long enough "block" of memory
>available, even when the total free memory is sufficient, when storing
>large matricies.
>
>What's wrong with nested lists?  Something like my.lms[[i]][[j]][[k]] <-
>lm(...)
>
>This can "scatter" its storage across discontinuous chunks of memory.
>
>Cheers
>
>Jason
>  
>




More information about the R-help mailing list