Hello All, I would like to retrieve some of the results from the lmer(...) function in library lme4. If I run a model, say fm.1 <- lmer(y ~ 1 + (1 | x), data = dog) and try names(fm.1), I get NULL. Is there anyway to retrieve the information? Thanks
On 7/15/06, A.R. Criswell <rstatistics at gmail.com> wrote:> Hello All, > > I would like to retrieve some of the results from the lmer(...) > function in library lme4. If I run a model, say > > fm.1 <- lmer(y ~ 1 + (1 | x), data = dog) > > and try names(fm.1), I get NULL. Is there anyway to retrieve the information?Yes. The recommended way of retrieving information form a fitted model object like an lmer object is with extractor functions. See ?lmer-class for a list of such methods. That help page also documents the slots in the object. The lmer class is an S4 class and uses typed slots instead of named components. The str function displays the structure of pretty well any type of R object, including S3 classed objects or S4 classed objects. That is my favorite way of checking the structure of an object. Please remember that it is risky to count on being able to reach in to an object and pull out slots or components and operate on them. The names and contents of slots are not guaranteed to stay constant. The lme4 and Matrix packages have been under development for a long time and should continue to be regarded as under development. When we change the internal representation we do change the extractor functions accordingly. It is a bug if an internal change causes an extractor function in the package to fail to return correct results. It is not a bug if an internal change causes your code that assumes a particular, obsolete representation to break.
lme4 is S4 package.So you should use slotNames to see what slots an object has,and use @ instead of $ to extract the slot you want.> library(lme4)Loading required package: Matrix Loading required package: lattice Loading required package: lattice> example(lmer) > slotNames(fm2)[1] "assign" "frame" "terms" "flist" "Zt" "X" [7] "y" "wts" "wrkres" "method" "useScale" "family" [13] "call" "cnames" "nc" "Gp" "XtX" "ZtZ" [19] "ZtX" "Zty" "Xty" "Omega" "L" "RZX" [25] "RXX" "rZy" "rXy" "devComp" "deviance" "fixef" [31] "ranef" "RZXinv" "bVar" "gradComp" "status"> fm2 at ranef[1] 1.5127787 -40.3739988 -39.1811143 24.5188772 22.9144206 9.2219771 [7] 17.1561300 -7.4517287 0.5786303 34.7680264 -25.7543295 -13.8649853 [13] 4.9159565 20.9290870 3.2586571 -26.4758005 0.9056393 12.4217767 [19] 9.3234692 -8.5991469 -5.3877753 -4.9686374 -3.1939301 -0.3084938 [25] -0.2872083 1.1159883 -10.9059430 8.6275943 1.2806874 6.7563873 [31] -3.0751268 3.5122002 0.8730485 4.9837782 -1.0052909 1.2583989 2006/7/15, A.R. Criswell <rstatistics at gmail.com>:> Hello All, > > I would like to retrieve some of the results from the lmer(...) > function in library lme4. If I run a model, say > > fm.1 <- lmer(y ~ 1 + (1 | x), data = dog) > > and try names(fm.1), I get NULL. Is there anyway to retrieve the information? > > Thanks > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- ?????? Department of Sociology Fudan University
Spencer Graves
2006-Jul-19 03:38 UTC
[R] How to find S4 generics? (was: inames() function and lmer())
***** ***** "methods" ***** ***** You have asked an excellent question. I can provide a partial answer below. First, however, I wish to pose a question of my own, which could help answer your question: How can one obtain a simple list of the available generics for a class? For an S3 class, the 'methods' functions provide that. What about an S4 class? That's entirely opaque to me, if I somehow can't find the relevant information in other ways. For example, ?lmer-class lists many but not all of the methods available for objects of class 'lmer'. I think I once found a way to get that, but I'm not able to find documentation on it now. ***** ***** Retrieving information from an 'lmer' object ***** ***** There are many ways to retrieve information from an object. For 'lmer', I think the best source of information is probably the help pages for 'lmer' and 'lmer-class'. The latter tells you that there are "Methods" anova, coef, coerce, deviance, logLik, update, simulate, solve, terms, vcov, ranef and VarCorr for extracting information or modifying an object of class 'lmer'. There are also other methods not listed on that page like 'fixef'. I don't know how to find those easily. There is also the excellent vignette("MlmSoftRev") in the 'mlmRev' package. (For help with it, see, e.g., "http://finzi.psych.upenn.edu/R/Rhelp02a/archive/76134.html".) Finally, the function 'str' will in most cases expose the structure of an object, thereby making it relatively easy to figure out how to get what is needed for many applications. That does not always work, however, because 'str' is a generic function, not a primitive. 'str' seems to expose everything one might possibly want to know about an 'lmer' object (apart from the theory behind it). This is not the case for an object of class 'logLik', because a function 'str.logLik' provides a succinct summary of a 'logLik' object in a non-standard format. The following will defeat 'str.logLik': > fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy) > lglk1 <- logLik(fm1) > str(lglk1) Class 'logLik' : -871.8 (df=5) # I don't understand this. > str(unclass(lglk1)) # This is in the standard 'str' format: atomic [1:1] -872 - attr(*, "nobs")= int 180 - attr(*, "nall")= int 180 - attr(*, "df")= num 5 - attr(*, "REML")= logi TRUE Best Wishes, Spencer Graves A.R. Criswell wrote:> Hello All, > > I would like to retrieve some of the results from the lmer(...) > function in library lme4. If I run a model, say > > fm.1 <- lmer(y ~ 1 + (1 | x), data = dog) > > and try names(fm.1), I get NULL. Is there anyway to retrieve the information? > > Thanks > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html