[R] wrong calculation of R-squared in summary.lm

From: Stefan Schlager <stefan.schlager_at_uniklinik-freiburg.de>
Date: Wed, 30 Mar 2011 14:17:42 +0200

Dear all,

I just stumbled upon the fact, that when I perform a regression on multivariate responses, that are not centred, I get a ricilulously high R-squared value. After reading the code of summary.lm, I found a bug in the function summary.lm:

mss is calculated by:
  mss <-sum((f - mean(f))^2) - where f are the fitted values.

This works only for a single response variable, because otherwise the matrix containing the fitted values is scaled by a scalar. replacing this with:
mss <- scale(f,scale=FALSE) solved the problem for me.

Example for bug:
response<-cbind(rnorm(500)+1000,rnorm(500)+300) predictor<-rnorm(500)
fit<-lm(response ~ predictor)
summary.lm(fit) now reports a R^2 value of 1!!!

Please correct me, if I'm wrong

Stefan

Stefan Schlager M.A.
Anthropologie
Medizinische Fakult├Ąt der der Albert Ludwigs- Universit├Ąt Freiburg Hebelstr. 29

79104 Freiburg

Anthropology
Faculty of Medicine, Albert-Ludwigs-University Freiburg Hebelstr. 29
D- 79104 Freiburg

phone +49 (0)761 203-5522
fax +49 (0)761 203-6898

On 30/03/11 13:54, r-help-request_at_r-project.org wrote:
> Mailing list subscription confirmation notice for mailing list R-help
>
> We have received a request from 129.132.148.130 for subscription of
> your email address, "stefan.schlager_at_uniklinik-freiburg.de", to the
> r-help_at_r-project.org mailing list. To confirm that you want to be
> added to this mailing list, simply reply to this message, keeping the
> Subject: header intact. Or visit this web page:
>
> https://stat.ethz.ch/mailman/confirm/r-help/4aa823dc72684b2ac88f8b630c5669c89da37c7c
>
>
> Or include the following line -- and only the following line -- in a
> message to r-help-request_at_r-project.org:
>
> confirm 4aa823dc72684b2ac88f8b630c5669c89da37c7c
>
> Note that simply sending a `reply' to this message should work from
> most mail readers, since that usually leaves the Subject: line in the
> right form (additional "Re:" text in the Subject: is okay).
>
> If you do not wish to be subscribed to this list, please simply
> disregard this message. If you think you are being maliciously
> subscribed to the list, or have any other questions, send them to
> r-help-owner@r-project.org.



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. Received on Wed 30 Mar 2011 - 12:30:32 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 30 Mar 2011 - 13:30:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive