Skip to content

Extracting Coefficients and Such from mle2 Output

6 messages · Tom La Bone, David Winsemius, Marc Schwartz +2 more

#
I had asked this question once before about a function in the NADA 
package, and you provided this neat response:

[Begin quote]

An approach that may yield somewhat more self-documenting code would be 
to examine either the fit object or the summary object with str and then 
to access results by extracting named elements. Since I don't have the 
package in question, let me use the lm object on its help page as an 
example:

  ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
  trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
  group <- gl(2,10,20, labels=c("Ctl","Trt"))
  weight <- c(ctl, trt)
  lm.D9 <- lm(weight ~ group)

  str(lm.D9)
  str(summary(lm.D9))
  summary(lm.D9)$coefficients
  summary(lm.D9)$coefficients["groupTrt", "Pr(>|t|)"]

# to get the p-value
 > summary(lm.D9)$coefficients["groupTrt","Pr(>|t|)"]
[1] 0.2490232

[End quote]

However, this does not seem to work with mle2, and the methods you 
suggested this time don't allow me access to the "etc" (the standard 
error and its probability for example). Is there a way to do this 
similar to what you suggested for lm, where I can at anything reported 
by mle2?

Tom
David Winsemius wrote:
#
As Ben Bolker's reply hints, our difficulty was that because we were  
insufficiently knowledgeable regarding working with S4 objects such  
as  "a" and summary(a) object. Both mle2 and summary(a) produce S4  
objects which need a different set of extraction technology. Although  
I did not know it last night, I have now looked at the help page for  
"slot", followed the link to the help page for "@" labeled "slotOp",   
and found that one can use "@" in much the same way one uses "$".

 > str(summary(a))
#Formal class 'summary.mle2' [package "bbmle"] with 3 slots
   ..@ call  : language mle2(minuslogl = LL, fixed = list(xhalf = 6))
   ..@ coef  : num [1, 1:4] 1.93e+01 1.71 1.13e+01 1.86e-29
   .. ..- attr(*, "dimnames")=List of 2
   .. .. ..$ : chr "ymax"
   .. .. ..$ : chr [1:4] "Estimate" "Std. Error" "z value" "Pr(z)"
   ..@ m2logL: num 60.4

# So ....coef in this case is a 1 by 4 matrix and ...

summary(a)@coef["ymax","Std. Error"]

#yields
#[1] 1.711541

So we still have access to any of the components of an S4 object such  
as a or summary(a), if we follow the trail of their respective  
structures
#
Let me  admit at this point that I am not really sure that S4 classes  
is the correct classification of such "@"-using extraction processes.  
The documentation refers to "formal methods" and I don't yet have  
enough knowledge to know that how tightly slots and formal classes are  
linked with S4 or S3.
#
on 02/01/2009 10:45 AM David Winsemius wrote:
David,

The use of '@' as a slot extractor is specific to S4 methods and you
will actually get an error if attempting to use it on other object
types. As per the Details section of ?"@":

As from R 2.7.0 it is checked that object is an S4 object (see isS4),
and as from R 2.8.0 it is an error to attempt to use @ on any other
object. (There is an exception for name .Data for internal use only.)



There are some good references on S4 methods in the wiki if you wish to
read further:

 http://wiki.r-project.org/rwiki/doku.php?id=tips:classes-s4

HTH,

Marc Schwartz
#
Marc Schwartz <marc_schwartz <at> comcast.net> writes:
My other two cents about this issue:

 David is quite right that you can basically think of "@"
as equivalent to "$" for getting bits out of S4 objects.
However ... in general S4 classes make it harder to get
internal bits out of objects, and in some sense they're
really *supposed* to make it harder.  Really the thing to
do when you encounter information that's buried somewhere
in an S4 object, the appropriate solution is what you did --
write to the author and say "why isn't there an accessor
method [the technical jargon for functions like coef()]
to get this piece of information out of this object, or
why isn't it properly documented? (Or is there some 
reason that I shouldn't be trying to access this piece
of information?)"

  cheers
   Ben Bolker
#
A minor footnote to Ben's response.

Why is it *supposed* to be harder to get bits out of objects like this and why should you work through accessor functions?  ("If it works, why shouldn't I use it?" - why indeed.)

The answer I would give is that if you work through the official accessor functions provided by the package, then your code stands a good chance of working even if the author of the package decides to change the representation of the object in future releases.  That's why your should steer clear of using anything that relies on the internal details of an object and not just the public face presented.  

This strategy also makes your code easier for someone else to follow.  Your intentions become clearer.

Bill Venables
http://www.cmis.csiro.au/bill.venables/ 


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Ben Bolker
Sent: Monday, 2 February 2009 10:18 AM
To: r-help at stat.math.ethz.ch
Subject: Re: [R] Extracting Coefficients and Such from mle2 Output

Marc Schwartz <marc_schwartz <at> comcast.net> writes:
My other two cents about this issue:

 David is quite right that you can basically think of "@"
as equivalent to "$" for getting bits out of S4 objects.
However ... in general S4 classes make it harder to get
internal bits out of objects, and in some sense they're
really *supposed* to make it harder.  Really the thing to
do when you encounter information that's buried somewhere
in an S4 object, the appropriate solution is what you did --
write to the author and say "why isn't there an accessor
method [the technical jargon for functions like coef()]
to get this piece of information out of this object, or
why isn't it properly documented? (Or is there some 
reason that I shouldn't be trying to access this piece
of information?)"

  cheers
   Ben Bolker

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.