Forecasting using model object from ARDL R package

1.4k views Asked by At

I am trying to use forecast function on ardl model like this.

library(ARDL)
data(denmark)

models <- auto_ardl(LRM ~ LRY + IBO + IDE, data = denmark, max_order = 5)
ardl_3132 <- models$best_model

fabletools::forecast(object = ardl_3132, new_data = denmark[1:20, ])
forecast::forecast(object = ardl_3132, new_data = denmark[1:20, ])

However, it's throwing an error Error in L(LRM, 1) : could not find function "L"

How can I make forecast work here?

2

There are 2 answers

1
Peter Thejll On

I am seeting this error too when trying to use predict() on an ardl model.

L function is in tsDyn, I think, but loading that does not eliminate the error.

0
Rokis1990 On

AFAIK, in the "core" of the ARDL model usually lies simple linear regression (unless it is specified as a dynamic model with dynlm), so... My best attempt would be to try building the ARDL model using a different package/function. It takes a bit more writing, but should give you a workable model object, which you could then use to build your prediction/forecast.

So what I would do, would be to estimate the ARDL model using the ARDL package, just like you did in your example above, extract the full formula from your model object, and rewrite it inside the lm() formula, using it's syntax, like so:

> ardl_3132$full_formula

LRM ~ L(LRM, 1) + L(LRM, 2) + L(LRM, 3) + LRY + L(LRY, 1) + IBO + L(IBO, 1) + L(IBO, 2) + L(IBO, 3) + IDE + L(IDE, 1) + L(IDE, 2)

We can specify the formula above inside an lm() model:

ardl_3132_lm <- lm(LRM ~ lag(LRM, 1) + lag(LRM, 2) + lag(LRM, 3) + LRY + lag(LRY, 1) + IBO + lag(IBO, 1) + lag(IBO, 2) + lag(IBO, 3) + IDE + lag(IDE, 1) + lag(IDE, 2), data = denmark)

You can use the predict.lm() function to create the forecast using the linearly specified ARDL model:

predict.lm(ardl_3132_lm)

The ARDL package itself is useful for estimating the model more conveniently than you would, using lm() or glm(). See summary() call on the model object to explore it's properties.

summary(ardl_3132)

Comparing it with the lm() specified model summary shows, however, that they are not identical.

summary(ardl_3132_lm)

The reason for this is that the function ARDL::auto_ardl() for this specific example has selected dynlm::dynlm(formula = full_formula, data = data, start = start, end = end) which specifies Dynamic Linear Models and Time Series Regression, and not a linear model.

To create a forecast from the dynlm model, you would need to use stats::predict() like so:

stats::predict(ardl_3132, 1)

Comparing the dynlm forecasted values with the linear model predicted values,

stats::predict(ardl_3132_lm)

we can see, that the predictions are different.

Update: Probably a better option would be to use another package that is ARDL specific, and not the simple lm() or glm() functions.

So, now I would use a different package dyn, that uses a somewhat peculiar syntax:

library(dyn)

dyn_model_1 <- dyn::dyn$lm(LRM ~ stats::lag(LRM, -1) + stats::lag(LRM, -2) + stats::lag(LRM, -3) + LRY + stats::lag(LRY, -1) + IBO + stats::lag(IBO, -1) + stats::lag(IBO, -2) + stats::lag(IBO, -3) + IDE + stats::lag(IDE, -1) + stats::lag(IDE, -2), data = denmark)

Print summary of the specified model:

summary(dyn_model_1)

Observe model fitted values:

plot(dyn_model_1$fitted.values)

Predict n steps forwart, using the specified model:

n <- 2 # Specify number of steps ahead to predict

prediction <- predict(dyn_model_1, n) # Forecast n steps ahead

plot(prediction) # Plot the forecasted values

Important note: You can't predict y for period t+i, if you have unlagged variables in the equation, that are of time t=0. Additionally, if you want to make t=2 predictions, you should make sure that you have removed the t-1 lagged variables from the equation as well.

General rule: You can't predict further ahead than your independent variables are lagged at in the equation that specifies the model.

So, in this case, the prediction for 2 steps ahead won't work, because we still have variables of t=0 and t=-1 in the model equation. To forecast, we would first have to remove those (underlagged) variables from the equation. For us to be able to predict 2 steps ahead, I would have to respecify the model as such:

dyn_model_2 <- dyn::dyn$lm(LRM ~ stats::lag(LRM, -2) + stats::lag(LRM, -3) + stats::lag(IBO, -2) + stats::lag(IBO, -3) + stats::lag(IDE, -2), data = denmark)

prediction <- predict(dyn_model_2, n)

plot(prediction)