# [R] Calculating R2 for a unit slope regression

Charles C. Berry cberry at tajo.ucsd.edu
Mon Nov 3 20:38:01 CET 2008

```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?

Sebastien,

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")

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.)

HTH,

Chuck

>
> 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 mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

```