Paul Johnson
2002-Jan-03 16:47 UTC
[R] Saving objects in a list and preserving attributes. How to?
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2002-Jan-03 17:17 UTC
[R] Saving objects in a list and preserving attributes. How to?
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I get> gc()used (Mb) gc trigger (Mb) Ncells 293826 7.9 531268 14.2 Vcells 564690 4.4 1659889 12.7 but how do I interpret these numbers? I understand (vaguely) the words "Ncells" and "Vcells" after reading ?gc, and "used", but what does "gc trigger" mean? G?ran -- G?ran Brostr?m tel: +46 90 786 5223 professor fax: +46 90 786 6614 Department of Statistics http://www.stat.umu.se/egna/gb/ Ume? University SE-90187 Ume?, Sweden e-mail: gb at stat.umu.se -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._