[R] Q: extracting data from lm

Marc Schwartz marc_schwartz at comcast.net
Sat Jul 28 00:46:52 CEST 2007


On Fri, 2007-07-27 at 15:52 -0600, D. R. Evans wrote:
> Warning: I am a complete newbie to R. I have read ISwR, but I am still
> finding myself completely stuck on some simple concepts.
> 
> I have tried everything I can think of to solve this one, and finally
> decided that enough was enough and I need a pointer to a solution.
> 
> I have the following summary from lm():
> 
> ----
> 
> > summary(lm(nu1~nu4))
> 
> Call:
> lm(formula = nu1 ~ nu4)
> 
> Residuals:
>      Min       1Q   Median       3Q      Max
> -1572.62  -150.38   -21.70   168.57  2187.84
> 
> Coefficients:
>             Estimate Std. Error t value Pr(>|t|)
> (Intercept) 29.88739   43.68881   0.684    0.494
> nu4          1.00036    0.01025  97.599   <2e-16 ***
> ---
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> 
> Residual standard error: 470.9 on 298 degrees of freedom
> Multiple R-Squared: 0.9697,     Adjusted R-squared: 0.9696
> F-statistic:  9526 on 1 and 298 DF,  p-value: < 2.2e-16
> 
> ----
> 
> But I want to access some of these numbers programmatically. I finally
> figured out that to get the estimate of the nu4 coefficient I need to do:
> 
> ----
> 
> > lm(nu1~nu4)$coefficients[2]
>      nu4
> 1.000363
> 
> ----
> 
> which to me as a long-time C++ programmer is close to black magic (I've
> been programming since 1972; I have to say that R is unlike anything I've
> ever seen, and it's far from trivial to get my head around some of it --
> for example, how I could have known a priori that the above is the way to
> get the nu4 coefficient is beyond me). Anyway, having figured out how to
> get the estimate of the coefficient, I not-unnaturally wanted also to find
> a way to access the std. error of the estimate (the value 0.01025 in the
> summary). But I am completely mystified as to how to do it :-(
> 
> Any help gratefully (VERY gratefully) received, and I apologise if this is
> a really, really stupid question and that the answer lies somewhere in some
> documentation that I've obviously not properly taken on board.

It looks like Peter references the notion of 'extractor functions' below
middle on page 97, but does not describe them, unless I am going blind
(or just tired).

In either case, you can supplement Peter's great intro book with many of
the of the other free references available via the R web site or An
Introduction to R, available with your installation or from the web
site.

In the latter case go here:

http://cran.r-project.org/doc/manuals/R-intro.html#Statistical-models-in-R

and scroll down to section 11.3.

Take note also of the 'Value' section of ?lm, which describes the
general contents of an lm object.

Another function that is always helpful is str(), which will display the
structure of an R object, enabling you to gain insights into the
components, their names and content. See ?str

In addition, take note of the 'See Also' section of ?lm, which does list
the generic functions coef, effects, residuals, fitted, vcov.

Using coef() will get you the values from the Coefficients part of the
output above, then enabling you to subset them to get the other values
as you require.

Another option relative to seeking assistance, is to review the Posting
Guide, a link to which is on the bottom of every post to the list. There
is a list of resources there that can guide you through how to search
for help, including utilizing the archives of the R help list where
keyword searches will be of tremendous assistance.

HTH,

Marc Schwartz



More information about the R-help mailing list