R中的线性插值(lm),奇怪的行为

Yve*_*ves 4 r linear-interpolation lm

使用R 3.2.2,我发现了一个运行简单线性插值的奇怪行为.第一个数据框给出了正确的结果:

test<-data.frame(dt=c(36996616, 36996620, 36996623, 36996626), value=c(1,2,3,4))
lm(value~dt, test)$coefficients

  (Intercept)            dt 
-1.114966e+07  3.013699e-01 
Run Code Online (Sandbox Code Playgroud)

通过递增dt变量,系数现在为NA:

test$dt<-test$dt+1
lm(value~dt, test)$coefficients

(Intercept)          dt 
        2.5          NA 
Run Code Online (Sandbox Code Playgroud)

知道为什么吗?似乎某处有溢出?

谢谢 !

Whi*_*ard 5

编辑

我找到了一些有关此问题的更好信息.

NA如果预测变量完全相关,则可以得到系数.这似乎是一个不寻常的情况,因为我们只有一个预测器.所以在这种情况下,dt似乎与截距线性相关.

我们可以使用找到线性因变量alias.请参阅https://stats.stackexchange.com/questions/112442/what-are-aliased-coefficients

在第一个例子中

test<-data.frame(dt=c(36996616, 36996620, 36996623, 36996626), value=c(1,2,3,4))
fit1 <- lm(value ~ dt, test)
alias(fit1)
Model :
value ~ dt
Run Code Online (Sandbox Code Playgroud)

没有线性依赖的术语.但在第二个例子中

test$dt <- test$dt + 1
fit2 <- lm(value ~ dt, test)
alias(fit2)
Model :
value ~ dt

Complete :
   [,1]       
dt 147986489/4
Run Code Online (Sandbox Code Playgroud)

这似乎表明dt和之间的线性依赖关系intercept.

有关如何lm处理降级模型的其他信息:https://stat.ethz.ch/pipermail/r-help/2002-February/018512.html.

lm不反转X'X https://stat.ethz.ch/pipermail/r-help/2008-January/152456.html,但我仍然认为下面的内容有助于显示X'X的奇点.

x <- matrix(c(rep(1, 4), test$dt), ncol=2)
y <- test$value

b <- solve(t(x) %*% x) %*% t(x) %*% y
Error in solve.default(t(x) %*% x) : 
system is computationally singular: reciprocal condition number = 7.35654e-30
Run Code Online (Sandbox Code Playgroud)

默认tollm.fit是1E-7,其是用于在计算线性依赖关系的容差qr分解.

qr(t(x) %*% x)$rank
[1] 1
Run Code Online (Sandbox Code Playgroud)

如果减少此值,您将获得参数估计值dt.

# decrease tol in qr
qr(t(x) %*% x, tol = 1e-31)$rank
[1] 2

# and in lm
lm(value~dt, test, tol=1e-31)$coefficients
  (Intercept)            dt 
-1.114966e+07  3.013699e-01 
Run Code Online (Sandbox Code Playgroud)

有关简单线性回归中矩阵代数的详细信息,请参阅https://stats.stackexchange.com/questions/86001/simple-linear-regression-fit-manually-via-matrix-equations-does-not-match-lm-o.