Paul Bailey
2007-Mar-09 14:34 UTC
[R] understanding print.summary.lm and perhaps print/show in general
I'm trying to understand how R prints summary.lm objects and trying to change it slightly for a summary function that calculates standard errors using an alternative method. I've found that I can modify a summary.lm object and then it prints the modified way but I want to change a few things in the print method that I think I might just be able to do. One is that I want the coefficients table to print a different header (other than "Std. Error"). I've tried changing the column name of the summary$coef matrix and this works for calls to printCoefmat but it still prints out "Std. Error" when I pass the summary.lm to the command line by itself. I don't understand this behavior. When I do this (enter an object on the command line by itself), does it then calls the print / show method associated with that objects class, in this case, summary.lm? Below is some sample code to reproduce the behavior I don't understand and a comment regarding the result I don't understand. Cheers, Paul ##### lma <- lm(dist ~ speed, data=cars) suma <- summary(lma) colnames(suma$coef) <- c(LETTERS[1:4]) printCoefmat(suma$coef) # prints what I expect suma # the above is the print behavior question regards, # why does the coefficients matrix have in its header # the usual "Estimate Std. Error t value Pr(>|t|)" # I expect "A B C D" as above in the call to printCoefmat
Christos Hatzis
2007-Mar-09 15:09 UTC
[R] understanding print.summary.lm and perhaps print/show in general
Paul,
Usually summary methods perform some computations if needed and then change
the class of the original object so that a print method can be called for
the new summary object.
In this case, this is done at the end of the summary.lm method:
...
if (!is.null(z$na.action))
ans$na.action <- z$na.action
class(ans) <- "summary.lm"
^^^^^^^^^^^^^^^^^^^^^^^^^^
ans
}
So then print.summary.lm does all the job displaying the summary.lm object.
To see that function do
getAnywhere(print.summary.lm)
Then you can then modify that function as needed.
-Christos
Christos Hatzis, Ph.D.
Nuvera Biosciences, Inc.
400 West Cummings Park
Suite 5350
Woburn, MA 01801
Tel: 781-938-3830
www.nuverabio.com
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Paul Bailey
> Sent: Friday, March 09, 2007 9:34 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] understanding print.summary.lm and perhaps
> print/show in general
>
> I'm trying to understand how R prints summary.lm objects and
> trying to change it slightly for a summary function that
> calculates standard errors using an alternative method.
>
> I've found that I can modify a summary.lm object and then it
> prints the modified way but I want to change a few things in
> the print method that I think I might just be able to do. One
> is that I want the coefficients table to print a different
> header (other than "Std. Error"). I've tried changing the
> column name of the summary$coef matrix and this works for
> calls to printCoefmat but it still prints out "Std. Error"
> when I pass the summary.lm to the command line by itself. I
> don't understand this behavior. When I do this (enter an
> object on the command line by itself), does it then calls the
> print / show method associated with that objects class, in
> this case, summary.lm? Below is some sample code to reproduce
> the behavior I don't understand and a comment regarding the
> result I don't understand.
>
> Cheers,
> Paul
>
> #####
> lma <- lm(dist ~ speed, data=cars)
> suma <- summary(lma)
> colnames(suma$coef) <- c(LETTERS[1:4])
> printCoefmat(suma$coef) # prints what I expect suma # the
> above is the print behavior question regards, # why does the
> coefficients matrix have in its header # the usual "Estimate
> Std. Error t value Pr(>|t|)"
> # I expect "A B C D" as above in the call to printCoefmat
>
> ______________________________________________
> 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.
>
>
Paul Bailey
2007-Mar-10 03:22 UTC
[R] understanding print.summary.lm and perhaps print/show in general
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 >