Re: [R] Calculating R2 for a unit slope regression

From: Charles C. Berry <>
Date: Mon, 03 Nov 2008 11:38:01 -0800

On Mon, 3 Nov 2008, J. Sebastian Tello wrote:

> Does anyone know of a literature reference, or a piece of code that can
> help me calculate the amount of variation explained (R2 value), in a
> regression constrained to have a slope of 1 and an intercept of 0?


In the future, please follow the posting guide or use help.request() to craft a better posting to this list.

Something like this is what you are after?

> x <- rnorm(100)
> y <- rnorm(100,x)
> # unexplained
> sum(residuals(lm(y~0+offset(x)))^2)/sum(y^2)
[1] 0.500178
> (sum(y^2) - sum( residuals( lm(y~0+offset(x)) )^2))/sum(y^2)
[1] 0.499822

Of course, I could have finessed the use of lm(), but why pass up an opportunity to show how the formula language handles this?


Be advised that this (fixing values of coefficients in tow models and 
then comparing them) is a tricky business.

You can get 'explained' values that are not in [0,1], which is a source of 
confusion to many. You can use

 	RSiteSearch("R2 intercept")

to find threads on this.

The usual distribution theory for nested linear models does not apply. 
(Read: do not try to compute a p-value unless you have the assistance of a 
statistician who can explain this sentence.)



> Thanks!
> Sebastian
> J. Sebastián Tello
> Department of Biological Sciences
> 285 Life Sciences Building
> Louisiana State University
> Baton Rouge, LA, 70803
> (225) 578-4284 (office and lab.)
> [[alternative HTML version deleted]]
Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E UC San Diego La Jolla, San Diego 92093-0901

______________________________________________ mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code.

Received on Mon 03 Nov 2008 - 19:42:02 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 Mon 03 Nov 2008 - 20:30:21 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive