[R] lm output

(Ted Harding) Ted.Harding at manchester.ac.uk
Fri Oct 9 23:41:41 CEST 2009

On 09-Oct-09 21:12:18, Brecknock, Peter wrote:
> Hi All
> I am running a linear regression using the lm object.
> In the event that my independent variable is the same across all
> observations the regression slope is returned as an NA.
> For example, if I have the following 
> y=c(10,12,17)
> x=c(5,5,5)
> lm = lm(y~x)
> produces the following 
> Coefficients:
> (Intercept)            x  
>       13           NA  
> Other than post-processing the results, is there a way to output the
> slope as 0 rather than NA?
> Thanks
> Pete

You should post-process! To incorporate such a thing into the
function lm() itself would be an arbitrary resolution of an
indeterminate situation, and would not be appropriate for a
general-purpose function. Your situation is, graphically,

17 +    *
15 +
12 +    *
10 +    *
 5 +----+----+
   0    5    10

Any line whatever through the point (5,13) will fit the three
points as well as any other, so the slope is indeterminate.[*]
You have decided that you would like it to be zero, but that
is an arbitrary decision!

[*] Note that a vertical line (with slope +/- Inf) does not
resolve the indeterminacy, since, although it goes through all
three points, you do not know which point[s] on the line to
use in evaluating the error -- the difference between a point
on the line with given value of x and "the" y-value on the line
corresponding to x -- because all points on the line qualify
as potential y values, since they all have the same value of x.

Since the slope is indeterminate, it is NA (that is what NA means).
It arises numerically because the calculation of the slope is

  (N*sum(x*y) - sum(x)*sum(y))/(N*sum(x^2) - sum(x)^2)

which is 0/0 -- likewise NA.

Hoping this helps,

E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 09-Oct-09                                       Time: 22:41:37
------------------------------ XFMail ------------------------------

More information about the R-help mailing list