[R] understanding print.summary.lm and perhaps print/show in general

Paul Bailey paul.bailey at alumni.grinnell.edu
Sat Mar 10 04:22:51 CET 2007


Petr,

Thanks, you set me on the right path. It turns out that the behavior that 
surprises me is this: when you change $coef it isn't the same as changing 
$coefficients. The first changes the value, but the second changes the value and 
makes the print/show metod change its output from when the summary.lm was 
created. I think the sample code below highlights this behavior nicely.

Why would you want this behavior?

Cheers,
Paul

#### R code:
lma <-  lm(dist ~ speed, data=cars) 
suma <- summary(lma)
colnames(suma$coef) <- c(LETTERS[1:4]) 
dimnames(suma$coef) # after setting colnames, dimnames of coefficients 
variable is set
suma # but printing is still the old print
dimnames(suma$coefficients) <- list(names(suma$coefficients), c(LETTERS[1:4])) 
dimnames(suma$coef) # no change in dimnames from before
suma # but the summary output is now refreshed!
######

>
>Another solution is to look into the code of summary.lm a few lines 
>above where the (dim)names are assigned. Based on this, you may try
>
>lma <- lm(dist ~ speed, data=cars)
>suma <- summary(lma)
>colnames(suma$coef) <- c(LETTERS[1:4])
>printCoefmat(suma$coef) # prints what I expect
>suma
>
>dimnames(suma$coefficients) <- list(names(suma$coefficients), 
>c(LETTERS[1:4]))
>suma
>
>You might also find reading the chapter on generic functions in the 
>R-lang (R language definition) manual useful.
>Petr
>



More information about the R-help mailing list