2

Question upfront: If use summary, I get a much more detailed view on my model than just print produces. In R, how can I know what extra information my object holds, such as revealed by summary, which I would not see without the knowledge of that generic function? Put in other words, how do I know what functions are available that yield additional information?

I conduct a quick linear least squares regression with:

model <- lm(seq(10) + runif(10) ~ seq(10))

Now, when I print the model, I get:

print(model)

Call:
lm(formula = seq(10) + runif(10) ~ seq(10))

Coefficients:
(Intercept)      seq(10)  
     0.3642       1.0413

Instead, when I use summary(model), I get a much more detailed view. Why wouldn't I obtain that in the first place?

> summary(model)

Call:
lm(formula = seq(10) + runif(10) ~ seq(10))

Residuals:
     Min       1Q   Median       3Q      Max 
-0.42297 -0.20032  0.00175  0.18183  0.39827 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.36419    0.18980   1.919   0.0913 .  
seq(10)      1.04133    0.03059  34.043 6.06e-10 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.2778 on 8 degrees of freedom
Multiple R-squared:  0.9931,    Adjusted R-squared:  0.9923 
F-statistic:  1159 on 1 and 8 DF,  p-value: 6.057e-10
Xiphias
  • 4,468
  • 4
  • 28
  • 51
  • 2
    you want `?str` and [this question](http://stackoverflow.com/questions/6583265/what-does-s3-methods-mean-in-r) – rawr Dec 12 '15 at 00:40
  • `?str` is great! However, the question you referred me to seems to go the other way round. If I know a generic such as `summary`, I'll be able to see all summary implementations loaded in the environment. But where do I see that my model has an implementation for that generic? – Xiphias Dec 12 '15 at 00:46
  • More generally speaking, can I retrieve a list of functions which I can run on the object? – Xiphias Dec 12 '15 at 00:54
  • 1
    don't think there is one built in, but you can use [this handy function](https://gist.github.com/MrFlick/55ed854eb935e5c21f71) which comes from [this question/answer](http://stackoverflow.com/questions/23840404/function-to-return-all-s3-methods-applicable-to-an-object/23840714#23840714) – rawr Dec 12 '15 at 00:55
  • In general, everything you see in `summary` **isn't** necessarily in the model object already. If you look at the code for `summary.lm`, for example, you'll see that it does a fair amount of calculations all by itself. Everything you see with `summary` *can be derived* from the model object, but some of it isn't explicitly there. – Gregor Thomas Dec 12 '15 at 01:12
  • You might also want to look into the `broom` package. – Gregor Thomas Dec 12 '15 at 01:12
  • Also check out `ls(model)` or `attributes(model)` for things already contained in the object. – maccruiskeen Dec 12 '15 at 02:30
  • @rawr This was extremely helpful. – Xiphias Dec 12 '15 at 11:17
  • @choff @Gregor These are helpful as well, but they show a more organised view on what `str` already reveals. rawr's function is the most promising, I believe. – Xiphias Dec 12 '15 at 11:18

1 Answers1

4

If you want a list of methods to use on a particular object try:

methods(class(object))

There was a relatively recent change to that function which now offers the S4 methods as well as the S3 ones it previously listed. It used to be that one needed to execute both that code as well as:

showMethods( classes=class(object) )

I will often use:

names(object)

... because the output of str(object) will be so extensive, but I really only want to check a few places and need the correct element name.

Xiphias
  • 4,468
  • 4
  • 28
  • 51
IRTFM
  • 258,963
  • 21
  • 364
  • 487