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

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Thu Jan 3 18:17:15 CET 2002


Paul Johnson <pauljohn at ku.edu> writes:

> 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.

I'd use something like

x <- rnorm(1000)
f <- function(i) {y <- getPhonyData(x); try(glm.nb(y~x,link="log"))}
l <- lapply( 1: nOfRuns, f)
 
>  > 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.

Be careful with those brackets: You want result[[1]]$coefficients etc. 

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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