[R] how to improve summary.lm

Jorge I Velez jorgeivanvelez at gmail.com
Sun Aug 21 09:56:09 CEST 2011


Hi Oliver,

For the the output you would like to have, you may take a look at

names(summary(yourmodel))
str(summary(yourmodel))

The above will help you to extract the components you need from the lm 
object.

Below is my attempt to do what you want. However, you will need to work 
a little more in order to get the "***" besides the p-values ;-)   Also, 
note that it is assumed that the "lm" function is used to build up the 
linear model.

# function
myout <- function(lmfit, k = 22){
      res <- summary(fit)
      cat(rep("--", k), "\n")
      print(res$call)
      print(res$coef)
      cat('Multiple R-squared:', res$r.squared, '   Adjusted 
R-squared:', res$adj.r.squared, "\n")
      cat(rep("--", k), "\n")
      }

# some data
set.seed(123)
x1 <- rnorm(100)
x2 <- rnorm(100)
y <- .5 + 1.2*x1 - .2*x2 +  rnorm(100)

# linear model
fit <- lm(y ~ x1 + x2)
myout(fit)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
lm(formula = y ~ x1 + x2)
               Estimate Std. Error   t value     Pr(>|t|)
(Intercept)  0.6350654 0.09614007  6.605627 2.129617e-09
x1           1.0668285 0.10486949 10.172915 5.677194e-17
x2          -0.1761887 0.09899469 -1.779779 7.824368e-02
Multiple R-squared: 0.5284765    Adjusted R-squared: 0.5187544
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

HTH,
Jorge


On 8/21/11 3:16 AM, Oliver Kullmann wrote:
> Hello,
>
> I need a shorter summary.lm, instead of
> --------------------------------------------
> Call:
> lm(formula = E$t ~ E$cfs)
>
> Residuals:
>         Min        1Q    Median        3Q       Max
> -0.239674 -0.007694  0.006430  0.014330  2.496551
>
> Coefficients:
>                Estimate Std. Error t value Pr(>|t|)
> (Intercept) -1.994e-02  1.419e-04  -140.5<2e-16 ***
> E$cfs        1.675e-05  4.714e-09  3552.7<2e-16 ***
> ---
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> Residual standard error: 0.03238 on 65268 degrees of freedom
> Multiple R-squared: 0.9949,     Adjusted R-squared: 0.9949
> F-statistic: 1.262e+07 on 1 and 65268 DF,  p-value:<  2.2e-16
> --------------------------------------------
>
> I need
>
> --------------------------------------------
> lm(formula = E$t ~ E$cfs)
>                Estimate Std. Error t value Pr(>|t|)
> (Intercept) -1.994e-02  1.419e-04  -140.5<2e-16 ***
> E$cfs        1.675e-05  4.714e-09  3552.7<2e-16 ***
> Multiple R-squared: 0.9949,     Adjusted R-squared: 0.9949
> --------------------------------------------
>
> I looked at the code of summary.lm, with the intention
> to copy parts of it into a new function "short_sum_lm",
> but couldn't find the parts I'm interested in.
>
> I hope it's not too complicated to achieve that.
> (In general it would be great to have more influence
> on the summary-function -- typically it's too spacious.)
>
> Thanks for your attention
>
> Oliver
>
> ______________________________________________
> R-help at r-project.org 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.



More information about the R-help mailing list