[R] r2 for lm() with zero intercept

Berwin A Turlach berwin at maths.uwa.edu.au
Tue Dec 2 08:08:57 CET 2008

G'day Glenn,

On Tue, 2 Dec 2008 17:02:26 +1100
<Glenn.Newnham at csiro.au> wrote:

> Obviously the code is functioning properly then, but do you consider
> this the best way of computing R^2 for a zero intercept?

The way R does.  What else would I say. ;-)

That formula compares the variance explained by the model to the
null-model in which all covariates are removed.

> I just checked what excel and genstat do in this situation and the
> R^2 they come up with reduces for a zero intercept rather than
> increases.

I am not surprised that excel does the wrong thing; just one more
example where it misbehaves.  But I am surprised to hear that genstat
does the same, I thought that genstat was developed by

> This seems more logical to me since fixing the intercept leads to a
> model that, at least in appearance, explains less of the variance in
> the data.

If you use the formula for models with an intercept on models without
an intercept then it may happen that you end up with a negative R^2.
Try to explain that to a user; that a quantity that is called R-squared
is negative.

My only gripe is that if one does something like:

	fm <- lm(Y ~ A/x - 1, data)

where A is a factor, R will handle this model as a no intercept term.
Although it has implicitly an intercept and I just choose to use this
formula since I was interested in parameterisation implied by this

But, you can always be subversive and say something like

	attributes(fm$terms)$intercept <- 1

before calling summary(fm) if you want to use the formula for models
with intercept.  Of course, in future versions (and past if you use an
older one), it would be advisable to study summary.lm first to see if
this trick will work; in R 2.8.0 it does.



More information about the R-help mailing list