R中的线性插值(lm),奇怪的行为
Linear interpolation (lm) in R, weird behavior
使用 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
通过递增 dt 变量,系数现在为 NA :
test$dt<-test$dt+1
lm(value~dt, test)$coefficients
(Intercept) dt
2.5 NA
知道为什么吗?好像哪里溢出了?
谢谢!
编辑
我发现了一些关于这个问题的更好的信息。
如果 预测变量 完全相关,您可以获得 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
没有线性相关项。但是在第二个例子中
test$dt <- test$dt + 1
fit2 <- lm(value ~ dt, test)
alias(fit2)
Model :
value ~ dt
Complete :
[,1]
dt 147986489/4
这似乎表明 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
lm.fit
中的默认tol
是1e-7,这是qr
分解中计算线性依赖的容差。
qr(t(x) %*% x)$rank
[1] 1
如果减少它,您将得到 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
有关简单线性回归中矩阵代数的详细信息,请参阅 https://stats.stackexchange.com/questions/86001/simple-linear-regression-fit-manually-via-matrix-equations-does-not-match-lm-o。
biglm
中的函数 biglm
似乎直接管理这个:
library(biglm)
test <- data.frame(dt=c(36996616, 36996620, 36996623, 36996626),
value=c(1,2,3,4))
test$dt <- test$dt+1
coefficients(biglm(value ~ dt, test))
# (Intercept) dt
# -1.114966e+07 3.013699e-01
使用 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
通过递增 dt 变量,系数现在为 NA :
test$dt<-test$dt+1
lm(value~dt, test)$coefficients
(Intercept) dt
2.5 NA
知道为什么吗?好像哪里溢出了?
谢谢!
编辑
我发现了一些关于这个问题的更好的信息。
如果 预测变量 完全相关,您可以获得 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
没有线性相关项。但是在第二个例子中
test$dt <- test$dt + 1
fit2 <- lm(value ~ dt, test)
alias(fit2)
Model :
value ~ dt
Complete :
[,1]
dt 147986489/4
这似乎表明 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
lm.fit
中的默认tol
是1e-7,这是qr
分解中计算线性依赖的容差。
qr(t(x) %*% x)$rank
[1] 1
如果减少它,您将得到 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
有关简单线性回归中矩阵代数的详细信息,请参阅 https://stats.stackexchange.com/questions/86001/simple-linear-regression-fit-manually-via-matrix-equations-does-not-match-lm-o。
biglm
中的函数 biglm
似乎直接管理这个:
library(biglm)
test <- data.frame(dt=c(36996616, 36996620, 36996623, 36996626),
value=c(1,2,3,4))
test$dt <- test$dt+1
coefficients(biglm(value ~ dt, test))
# (Intercept) dt
# -1.114966e+07 3.013699e-01