4

This problem comes from easier problem which I managed to solve myself. So here is my original question.

In my data I have lots of categories, but i'm not interested in estimating coefficients for all of them, I just want to test the hypothesis, that there is no difference in categories. And calling summary on my object produces most information that I don't need for my report.

set.seed(42)
dat <- data.frame(cat=factor(sample(1:10, 100, replace=T)), y=rnorm(100))
l1 <- lm(y~cat-1, data=dat)
summary(l1)

How do I extract only the last line from call to summary(l1)?

In this particular case I can just used anova function

anova(l1)

and got only the info that I needed, just in different formatting than summary(l1) produces.

What if I have some kind of a summary on an object and I want to extract just particular part of summary(object) how do I do that? For example, how do I get R to print only the line of call summary(l1)?

p.s. I am aware of summary(l1)$fstatistic.

jem77bfp
  • 1,270
  • 11
  • 13

2 Answers2

4

Try using str to explore. For example, have a look at

str(summary(l1))

the output of which includes

$ fstatistic   : Named num [1:3] 1.32 10 90

then you can try

summary(l1)$fstatistic

#   value     numdf     dendf 
#1.323275 10.000000 90.000000 

the p-value is a little tricker. Have a read of this post for more info, but here is a solution:

anova(l1)$"Pr(>F)"[1]
# [1] 0.2303172

...not pretty, but it seems to work!

seancarmody
  • 6,182
  • 2
  • 34
  • 31
  • Related answers [here](http://stackoverflow.com/questions/5587676/pull-out-p-values-and-r-squared-from-a-linear-regression/5587781#5587781) – Chase Nov 05 '12 at 13:37
4

OK, I'm in a literal mood, but using capture.output will return the character representation of an evaluated object. So for example, using your l1 object:

l1Out <- capture.output(summary(l1))
grep("^F-st", l1Out, value = TRUE)
# [1] "F-statistic: 1.323 on 10 and 90 DF,  p-value: 0.2303 "

Note, however, that this is not the last line of output:

tail(l1Out, 1)
# [1] ""

And for many components of the summary object, there are better ways to extract information, such as @seancarmody wrote

l1$call
# lm(formula = y ~ cat - 1, data = dat)
BenBarnes
  • 19,114
  • 6
  • 56
  • 74