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.
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.coef(summary(lm(nu1 ~ nu2)))[,2] Also, try the following which is often useful: str(summary(lm(nu1 ~ nu2)))> ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
On 27/07/07, Chuck Cleland <ccleland at optonline.net> wrote:> > coef(summary(lm(nu1 ~ nu2)))[,2] > > Also, try the following which is often useful: > > str(summary(lm(nu1 ~ nu2))) >Oh, wow! Thank you. Incidentally, just in case anyone got the wrong end of the stick, I'm not at all complaining about R. It's good at my age to be faced with something so different. And from an architectural standpoint I appreciate its elegance and innate power. It's just the logistics of knowing exactly what to type that causes me to feel overwhelmed, and although I've become very used in the past couple of days to typing ?<something> I'm not much good yet at finding out how to help myself if that doesn't tell me what I want to know.
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
Marc gave some good general advice, here are a couple more things that are more specific to your problem. Remember that most R functions return information, sometimes invisibly, but it is good to save the results. This includes the summary function (all the numbers that get printed out are also returned in an object). Try something like: fit <- lm(nu1~nu4) coef(fit)[2] sfit <- summary(fit) coef(sfit) se.nu4 <- coef(sfit)[2,2] of course some of us give into the temptation to go for terseness over readability and end up doing this like: se.nu4 <- coef(summary(lm(nu1~nu4)))[2,2] or se.nu4 <- summary(lm(nu1~nu4))$coefficients[2,2] hope this helps, ________________________________ From: r-help-bounces@stat.math.ethz.ch on behalf of D. R. Evans Sent: Fri 7/27/2007 3:52 PM To: r-help@stat.math.ethz.ch Subject: [R] Q: extracting data from lm 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. ______________________________________________ R-help@stat.math.ethz.ch 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. [[alternative HTML version deleted]]