smars - 9 months ago 39

R Question

I have to make a one-step ahead forecast for a time series *Y(t)* using R. Theory suggests the ideal model should be:

*Y(t) = αX + βY(t-1) - βY(t-2)*

However, I don't know how to deal with the following issues:

- I have to take
*βY(t-1)***minus***βY(t-2)*. - There are both
**autoregressive**(*Y(t-1)*,*Y(t-2)*) and**exogenous**variables (*X*). - I have to test whether or not "
*βY(t-1) - βY(t-2)*" is the best way to express the autoregression, instead of other ARIMA models.

The time series

`Y <- c(57.4, 51.6, 36.1, 34.8, 41.2, 59.1, 62.5, 55.0, 53.8, 52.4, 44.5, 42.2, 50.1, 61.3, 49.6, 38.2, 51.1, 44.7, 40.8, 46.1, 53.5, 54.7, 50.3, 48.8, 53.7, 52.0)`

The exogenous variable

`X <- c(-12.1, 30.0, 13.5, 30.0, -3.8, -24.3, 30.0, 30.0, 30.0, 30.0, -21.6, 30.0, 0.0, 26.5, -30.0, 20.5, -4.8, -9.2, 22.2, -7.3, 15.9, 16.0, 13.7, 5.6, 5.7, 1.8)`

As you may notice, this actual X does not provide much help in predicting Y. Nevertheless, I reported it as an example since I am currently looking for the right values of X.

If anything was wrong or not clear, let me know and I will give the necessary explanations.

Thanks in advance.

Answer Source

You can use the `lag`

function for the transformation and `lm`

or `glm`

for regression:

```
Y <- c(57.4, 51.6, 36.1, 34.8, 41.2, 59.1, 62.5, 55.0, 53.8, 52.4, 44.5, 42.2, 50.1, 61.3, 49.6, 38.2, 51.1, 44.7, 40.8, 46.1, 53.5, 54.7, 50.3, 48.8, 53.7, 52.0)
X <- c(-12.1, 30.0, 13.5, 30.0, -3.8, -24.3, 30.0, 30.0, 30.0, 30.0, -21.6, 30.0, 0.0, 26.5, -30.0, 20.5, -4.8, -9.2, 22.2, -7.3, 15.9, 16.0, 13.7, 5.6, 5.7, 1.8)
y_1 <- lag(Y)
y_2 <- lag(Y,2)
lm(Y~X+y_1+y_2)
```

You could also do the lag transformations directly in the regression equation:

```
lm(Y ~ X + I(lag(Y)) + I(lag(Y, 2)))
```

Finally, the difference is just a change of the operator to this:

```
lm(Y ~ X + I(lag(Y)) - I(lag(Y, 2)))
```

`Call: lm(formula = Y ~ X + I(lag(Y)) - I(lag(Y, 2))) Coefficients: (Intercept) X I(lag(Y)) 3.906e-14 -4.693e-18 1.000e+00`