Consider the following R code:
example <- function(n) {
X <- 1:n
Y <- rep(1,n)
return(lm(Y~X))
}
#(2.13.0, i386-pc-mingw32)
summary(example(7)) #R^2 = .1963
summary(example(62)) #R^2 = .4529
summary(example(4540)) #R^2 = .7832
summary(example(104))) #R^2 = 0
#I did a search for n 6:10000, the result for R^2 is NaN for
#n = 2, 4, 16, 64, 256, 1024, 2085 (not a typo), 4096, 6175 (not a typo), and 8340 (not a typo)
Looking at http://svn.r-project.org/R/trunk/src/appl/dqrls.f) did not help me understand what is going on, because I do not know Fortran. In another question it was answered that floating point machine tolerance errors were are to blame for coefficients for X that are close to, but not quite 0.
$R^2$ is greater when the value for coef(example(n))["X"] is closer to 0. But...
- Why is there an $R^2$ value at all?
- What (specifically) is determining it?
- Why the seemingly orderly progression of
NaNresults? - Why the violations of that progression?
- What of this is 'expected' behavior?
Y <- rep(1,n)+runif(n)*ynoise), that would be interesting :-) – Carl Witthoft Feb 14 at 19:45apply(as.matrix(2:17), 1, function(n){example(n)$coefficients[-1]}). (My results, on a Win 7 x64 Xeon, range from -8e-17 to +3e-16; about half are true zeros.) BTW, the Fortran source is of no help: it's just a wrapper for dqrdc; that's the code you want to look at. – whuber♦ Feb 14 at 21:30