[R] Saving objects in a list and preserving attributes. How to?

Paul Johnson pauljohn at ku.edu
Thu Jan 3 17:47:38 CET 2002


I've been writing a bunch of simulation experiments to test models in 
MASS (glm.nb) and JK Lindsey's repeated library (gar and kalcount).  If 
I generate data over and over, and estimate a model for each, I end have 
syntax like this:

x <- rnorm(1000)
for (i in 1: nOfRuns){
	y <- getPhonyData(x)
	estim <- glm.nb(y~x,link="log")
}

Except for problems of nonconvergence and other maximum likelihood 
estimation problems, which I gather are standard things you have to work 
around by fiddling initial values of parameters (?), this works OK, 
except I can't figure how to keep the estimate objects for later analysis.

Inside the loop, I am able to grab coefficients out of estim and put 
them in a matrix, but what I really want is to save the whole object, so 
I can go back and look at the runs and find out more about them.  I was 
wishing lists in R worked like lists in Java, just "addLast: estim" and 
collect up the objects.

It doesn't work to just try to put the estimate objects into a vector:

 > getPhonyData <- function (x){
+   e<-rnorm(1000)
+   y<-3.4 + 5*x + e
+ }
 >
 > x<- 10+5*rnorm(1000)
 >
 > result <- c(rep(NA,10))
 >
 > for (i in 1:10) {
+   y <- getPhonyData(x)
+   estim <- lm(y~x)
+   result[i] <- estim
+ }
Warning messages:
1: number of items to replace is not a multiple of replacement length
2: number of items to replace is not a multiple of replacement length
3: number of items to replace is not a multiple of replacement length
4: number of items to replace is not a multiple of replacement length
5: number of items to replace is not a multiple of replacement length
6: number of items to replace is not a multiple of replacement length
7: number of items to replace is not a multiple of replacement length
8: number of items to replace is not a multiple of replacement length
9: number of items to replace is not a multiple of replacement length
10: number of items to replace is not a multiple of replacement length
 > result[1]
result[1]
[[1]]
(Intercept)           x
    3.427956    4.998404

 > result[1]$coefficients
result[1]$coefficients
NULL
 > est <- result[1]
est <- result[1]
 > attributes(est)
attributes(est)
NULL
 >


I get the same problem if I create an R list object and then add estim 
to the list, then it does build a list, but none of the elements of the 
list have any attributes. The list printout makes it look as though the 
stored items are just the  text printout you get when you type "estim" 
on the command line, rather than an object that answers to
    myList[1]$coeffients

or some such.

-- 
Paul E. Johnson                       email: pauljohn at ukans.edu
Dept. of Political Science            http://lark.cc.ku.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list