由于 R 中的大整数日期时间,线性模型奇异?
Linear model singular because of large integer datetime in R?
日期随机正态的简单回归失败,但具有小整数而不是日期的相同数据按预期工作。
# Example dataset with 100 observations at 2 second intervals.
set.seed(1)
df <- data.frame(x=as.POSIXct("2017-03-14 09:00:00") + seq(0, 199, 2),
y=rnorm(100))
#> head(df)
# x y
# 1 2017-03-14 09:00:00 -0.6264538
# 2 2017-03-14 09:00:02 0.1836433
# 3 2017-03-14 09:00:04 -0.8356286
# Simple regression model.
m <- lm(y ~ x, data=df)
由于数据的奇异性,斜率缺失。调用摘要演示了这一点:
summary(m)
# Coefficients: (1 not defined because of singularities)
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.10889 0.08982 1.212 0.228
# x NA NA NA NA
难道是因为 POSIXct
class?
# Convert date variable to integer.
df$x2 <- as.integer(df$x)
lm(y ~ x2, data=df)
# Coefficients:
# (Intercept) x2
# 0.1089 NA
不,x2
的系数仍然缺失。
如果我们将 x2
的基线设为零会怎样?
# Subtract minimum of x.
df$x3 <- df$x2 - min(df$x2)
lm(y ~ x3, data=df)
# Coefficients:
# (Intercept) x3
# 0.1312147 -0.0002255
这有效!
再举一个例子来排除这是由于日期时间变量造成的。
# Subtract large constant from date (data is now from 1985).
df$x4 <- df$x - 1000000000
lm(y ~ x4, data=df)
# Coefficients:
# (Intercept) x4
# 1.104e+05 -2.255e-04
不符合预期(为什么相差 30 年的相同数据集会导致不同的行为?),但这也有效。
可能与.Machine$integer.max
(我电脑上的2147483647)有关,但我想不通。如果有人能解释这里发生了什么,我们将不胜感激。
是的,可以。 QR分解是稳定的,但不是万能的神
X <- cbind(1, 1e+11 + 1:10000)
qr(X)$rank
# 1
这里的X
就像你的线性回归模型的模型矩阵,其中有一个全为1的截距列,还有一个日期时间序列(注意大偏移量)。
如果将日期时间列居中,这两列将正交因此非常稳定(即使直接求解标准方程!)。
日期随机正态的简单回归失败,但具有小整数而不是日期的相同数据按预期工作。
# Example dataset with 100 observations at 2 second intervals.
set.seed(1)
df <- data.frame(x=as.POSIXct("2017-03-14 09:00:00") + seq(0, 199, 2),
y=rnorm(100))
#> head(df)
# x y
# 1 2017-03-14 09:00:00 -0.6264538
# 2 2017-03-14 09:00:02 0.1836433
# 3 2017-03-14 09:00:04 -0.8356286
# Simple regression model.
m <- lm(y ~ x, data=df)
由于数据的奇异性,斜率缺失。调用摘要演示了这一点:
summary(m)
# Coefficients: (1 not defined because of singularities)
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.10889 0.08982 1.212 0.228
# x NA NA NA NA
难道是因为 POSIXct
class?
# Convert date variable to integer.
df$x2 <- as.integer(df$x)
lm(y ~ x2, data=df)
# Coefficients:
# (Intercept) x2
# 0.1089 NA
不,x2
的系数仍然缺失。
如果我们将 x2
的基线设为零会怎样?
# Subtract minimum of x.
df$x3 <- df$x2 - min(df$x2)
lm(y ~ x3, data=df)
# Coefficients:
# (Intercept) x3
# 0.1312147 -0.0002255
这有效!
再举一个例子来排除这是由于日期时间变量造成的。
# Subtract large constant from date (data is now from 1985).
df$x4 <- df$x - 1000000000
lm(y ~ x4, data=df)
# Coefficients:
# (Intercept) x4
# 1.104e+05 -2.255e-04
不符合预期(为什么相差 30 年的相同数据集会导致不同的行为?),但这也有效。
可能与.Machine$integer.max
(我电脑上的2147483647)有关,但我想不通。如果有人能解释这里发生了什么,我们将不胜感激。
是的,可以。 QR分解是稳定的,但不是万能的神
X <- cbind(1, 1e+11 + 1:10000)
qr(X)$rank
# 1
这里的X
就像你的线性回归模型的模型矩阵,其中有一个全为1的截距列,还有一个日期时间序列(注意大偏移量)。
如果将日期时间列居中,这两列将正交因此非常稳定(即使直接求解标准方程!)。