[R] how to get the p-values from an lm function ?

hadley wickham h.wickham at gmail.com
Thu Jul 12 12:46:13 CEST 2007


On 7/12/07, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> On Thu, 12 Jul 2007, hadley wickham wrote:
>
> > On 7/12/07, Benoit Chemineau <benoitchemineau at gmail.com> wrote:
> >> Hi, dear R-users,
> >>
> >> I am computing a liner regression by rating category using the 'by' function
> >> as stated below:
> >>
> >> tmp <- by(projet, rating, function(x) lm(defaults ~ CGDP+CSAVE+SP500, data =
> >> x))
> >>
> >> I would like to get not only the coefficients but also their p-values. I
> >> can't find the command in the help pages to get them.
> >>
> >> Does anyone have a suggestion ?
> >
> > Hi Benoit,
> >
> > A general approach to find p-values:
> >
> > m <- lm(wt ~ mpg, data=mtcars)
> >
> > First figure out how to display them on screen:
> > m # nope
> > coef(m) # nope
> > summary(m) # got it
> >
> > # Then use str to look at the components
> > str(summary(m))
> >
> > # And pick out the one want
> > summary(m)$coef
> > coef(summary(m)) # slighty better style, but won't work in general
>
> If x$coef works, coef(x) will almost certainly work at least as well.
> But note that in most cases it is x$coefficients and so x$coef is liable
> to partially match erroneously.

I meant in general that x$y, does not correspond to y(x) - I realised
after I wrote it that I was unclear.

> > # In general, you may also need to try
> > str(print(summary(m)))
> > # as sometimes the print method calculates the data you're looking for
>
> But a print method should always return its input, so
>
> str(summary(m))
> str(print(summary(m)))

Oh yes, I was getting confused with print functions which compute
values and print them but do not return them.

And that comment has made me realise many of my print methods don't
return x.   - something to fix.
Hadley



More information about the R-help mailing list