質問

So I am a beginner to R but I am running some code which simulates 100 observations of a y variable that follows the formula y_t=1+.5*y(t-1)+u. I then want to run a regression of y on y(t-1) and y_(t-2) and a constant. When I run the regression using the dyn package it shows the coefficient on y_(t-2) as NA. Anyone have any thoughts on this?

Here is my code:

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))

Here is the result:

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
役に立ちましたか?

解決

dyn works with time series objects -- usually 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))

giving:

> 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  

This would also work:

model <- dyn$lm(z ~ lag(z, -1:-2))
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top