[R] How to call R-squared values from lm's?

Marc Schwartz marc_schwartz at me.com
Thu Dec 2 15:21:59 CET 2010


On Dec 2, 2010, at 8:15 AM, Wegan, Michael (DNRE) wrote:

> I would like to call both p-values and R-squared values from lm's in a function.  I can get the p-values from coef(summary(name.lm))[r,c], however, I cannot figure out how to call the R-squared values without manually calling the summary and inserting them in the script - which negates the value of automating the process through a function.
> 
> Thanks,
> Mike

str() is your friend.

From ?lm

> summary(lm.D9)

Call:
lm(formula = weight ~ group)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.0710 -0.4938  0.0685  0.2462  1.3690 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   5.0320     0.2202  22.850 9.55e-15 ***
groupTrt     -0.3710     0.3114  -1.191    0.249    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.6964 on 18 degrees of freedom
Multiple R-squared: 0.07308,	Adjusted R-squared: 0.02158 
F-statistic: 1.419 on 1 and 18 DF,  p-value: 0.249 



# Get the structure of the model summary object
> str(summary(lm.D9))
List of 11
 $ call         : language lm(formula = weight ~ group)
 $ terms        :Classes 'terms', 'formula' length 3 weight ~ group
  .. ..- attr(*, "variables")= language list(weight, group)
  .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:2] "weight" "group"
  .. .. .. ..$ : chr "group"
  .. ..- attr(*, "term.labels")= chr "group"
  .. ..- attr(*, "order")= int 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(weight, group)
  .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "factor"
  .. .. ..- attr(*, "names")= chr [1:2] "weight" "group"
 $ residuals    : Named num [1:20] -0.862 0.548 0.148 1.078 -0.532 ...
  ..- attr(*, "names")= chr [1:20] "1" "2" "3" "4" ...
 $ coefficients : num [1:2, 1:4] 5.032 -0.371 0.22 0.311 22.85 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:2] "(Intercept)" "groupTrt"
  .. ..$ : chr [1:4] "Estimate" "Std. Error" "t value" "Pr(>|t|)"
 $ aliased      : Named logi [1:2] FALSE FALSE
  ..- attr(*, "names")= chr [1:2] "(Intercept)" "groupTrt"
 $ sigma        : num 0.696
 $ df           : int [1:3] 2 18 2
 $ r.squared    : num 0.0731
 $ adj.r.squared: num 0.0216
 $ fstatistic   : Named num [1:3] 1.42 1 18
  ..- attr(*, "names")= chr [1:3] "value" "numdf" "dendf"
 $ cov.unscaled : num [1:2, 1:2] 0.1 -0.1 -0.1 0.2
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:2] "(Intercept)" "groupTrt"
  .. ..$ : chr [1:2] "(Intercept)" "groupTrt"
 - attr(*, "class")= chr "summary.lm"



> summary(lm.D9)$r.squared
[1] 0.0730776

> summary(lm.D9)$adj.r.squared
[1] 0.02158191


HTH,

Marc Schwartz



More information about the R-help mailing list