Thank you Berend and Mark, It seems pretty clear, the problem is with the numbers and not with R.
Intuitively, I didn't think that regressing on Y on X would give different results than regressing Y on X - C (where C is a constant). So, I thought that R was doing something strange with rounding. However, I think it has more to do with the fact that the range of X is dwarfed when a sufficiently large constant is introduced. The one thing that made me think that R was doing something "wrong" was that the slope function in excel worked in both cases. So, that's why I posted the question rather than thinking that I was doing something wrong. I appreciate your references to the wikipeida articles, those will be helpful. I want to spend a little more time thinking about this later on. Also, thanks for the code to illustrate your point (both Bererd and Mark). I think I might just use this function for now to approximate the simple slope function in excel: slope = function(x,y) { ret = sum((x-mean(x))*(y-mean(y)))/sum((x-mean(x))^2) return(ret) } On Sat, Apr 14, 2012 at 7:40 AM, Berend Hasselman <b...@xs4all.nl> wrote: > > 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 > > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.