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

Stefan Schlager stefan.schlager at uniklinik-freiburg.de
Wed Mar 30 14:17:42 CEST 2011


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 at r-project.org.



More information about the R-help mailing list