所以我是R的初学者,但我正在运行一些代码,它模拟了y变量的100次观察,该变量遵循公式y_t = 1 + .5 * y( t-1)+ u。然后我想在y (t-1)和y_(t-2)和常数上运行y的回归。当我使用dyn包运行回归时,它将y_(t-2)上的系数显示为NA。有没有人对此有任何想法?
这是我的代码:
y<-numeric(100)
for (i in 2:100){
u <- rnorm(1, mean= 0, sd = 1)
y[1]<-2
y[i]<-1+.5*y[i-1]+u
}
model<-dyn$lm(y ~ lag(y, -1)+lag(y,-2))
结果如下:
Call:
lm(formula = dyn(y ~ lag(y, -1) + lag(y, -2)))
**Coefficients: (1 not defined because of singularities)**
Residual standard error: 2.147e-16 on 98 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: 1
F-statistic: 2.344e+33 on 1 and 98 DF, p-value: < 2.2e-16
答案 0 :(得分:3)
dyn适用于时间序列对象 - 通常是zoo:
library(dyn)
set.seed(123)
y <- numeric(100)
y[1] <- 2
for (i in 2:100) {
u <- rnorm(1, mean = 0, sd = 1)
y[i] <- 1 + 0.5 * y[i-1] + u
}
z <- zoo(y)
model <- dyn$lm(z ~ lag(z, -1) + lag(z, -2))
,并提供:
> model
Call:
lm(formula = dyn(z ~ lag(z, -1) + lag(z, -2)))
Coefficients:
(Intercept) lag(z, -1) lag(z, -2)
1.20011 0.46826 -0.01038
这也有效:
model <- dyn$lm(z ~ lag(z, -1:-2))