I can't figure out why this is returning an NA for the slope in one case,
but not in the other.
I can tell that R thinks the first case is singular, but why isn't the
second?
## Define X and Y
## There are two versions of x
## 1) "as is"
## 2) shifted to start at 0
y = c(58, 57, 57, 279, 252, 851, 45, 87, 47)
x1 = c(1334009411.437, 1334009411.437, 1334009411.437, 1334009469.297,
1334009469.297, 1334009469.297, 1334009485.697, 1334009485.697,
1334009485.697)
x2 = x1 - min(x1)
## Why doesn't the LM model work for the "as is" x?
lm(y~x1)
lm(y~x2)
My environment:
Windows XP,
R 2.14.1
[[alternative HTML version deleted]]
Berend Hasselman
2012-Apr-14 12:40 UTC
[R] Seemingly simple "lm" giving unexpected results
On 13-04-2012, at 22:20, Gene Leynes wrote:> I can't figure out why this is returning an NA for the slope in one case, > but not in the other. > > I can tell that R thinks the first case is singular, but why isn't the > second? > > ## Define X and Y > ## There are two versions of x > ## 1) "as is" > ## 2) shifted to start at 0 > y = c(58, 57, 57, 279, 252, 851, 45, 87, 47) > x1 = c(1334009411.437, 1334009411.437, 1334009411.437, 1334009469.297, > 1334009469.297, 1334009469.297, 1334009485.697, 1334009485.697, > 1334009485.697) > x2 = x1 - min(x1) > > ## Why doesn't the LM model work for the "as is" x? > lm(y~x1) > lm(y~x2)With your data the matrix t(X)%*%X is extremely ill-conditioned for the case with x1. See http://en.wikipedia.org/wiki/Condition_number http://mathworld.wolfram.com/ConditionNumber.html http://en.wikipedia.org/wiki/Numerical_methods_for_linear_least_squares You can check it with makeXX <- function(x) { matrix(data=c(x,rep(1,length(x))),nrow=length(x), byrow=FALSE) } X1 <- makeXX(x1) (XTX1 <- t(X1) %*% X1) svd(XTX1) and similar for x2. Berend