Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using ARDL to forecast #5

Open
melville1808 opened this issue Aug 4, 2021 · 8 comments
Open

using ARDL to forecast #5

melville1808 opened this issue Aug 4, 2021 · 8 comments

Comments

@melville1808
Copy link

melville1808 commented Aug 4, 2021

Hi, forgive me if it's covered somewhere, but is there a way to use this to create forecasts?

I have a model looking like this:
models <- auto_ardl(y~ x+ z,data = df, max_order = 5)
and I have x and z going forward about 20 periods. Ideally i would be able to pass a dataframe and solve that way, but i'm not sure how. Is this possible?

Thanks

@crossxwill
Copy link

A function similar to ecm::ecmpredict() would be awesome!

@nipnipj
Copy link

nipnipj commented Dec 11, 2021

Is it possible to use ARDL package to predict/forecast?

@halfgua
Copy link

halfgua commented Feb 16, 2023

same question

@Natsiopoulos
Copy link
Owner

Thank you for mentioning this. I will create a workaround solution in the next update.
The reason you can't directly forecast/predict using a model from the ARDL package is that the d and L operators (from the dynlm package) are not loaded in the object environment and the predict() function can't find it. e.g.:

library(ARDL)
ardl_3132 <- ardl(LRM ~ LRY + IBO + IDE, data = denmark, order = c(3,1,3,2))
predict(ardl_3132, newdata = denmark)

Error in L(LRM, 1) : could not find function "L"

This happens because I mess around with the environments of the objects in the functions ardl(), uecm(), and recm().

Solution 1:

I would consider leaving the environments as exported from the dynlm(). I believe that this should not cause any problems except that comparing with identical() two virtually identical models would return FALSE.
Actually, the only difference is the environment. e.g.:

library(dynlm)
b1 <- dynlm(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), data=denmark)
b2 <- dynlm(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), data=denmark)
identical(b1,b2)

[1] FALSE

environment(terms(b1))
environment(terms(b2))

This is something that already happens with the dynlm models, so it should be no big deal going for solution 1, as it has the benefit that now the special operators would be found in the object's environment, and this may be beneficial combining the ARDL models with other packages that can handle dynlm classes. e.g.:

predict(b1, newdata = denmark)

1974 Q1 1974 Q2 1974 Q3 1974 Q4 1975 Q1 1975 Q2 1975 Q3 1975 Q4 1976 Q1 1976 Q2 1976 Q3 1976 Q4 1977 Q1 1977 Q2 1977 Q3 11.67653 11.60539 11.59129 11.62721 11.64031 11.63374 11.68756 11.78910 11.75967 11.75098 11.74660 11.76952 11.73396 11.74692 11.74065 1977 Q4 1978 Q1 1978 Q2 1978 Q3 1978 Q4 1979 Q1 1979 Q2 1979 Q3 1979 Q4 1980 Q1 1980 Q2 1980 Q3 1980 Q4 1981 Q1 1981 Q2 11.72847 11.70454 11.68242 11.69926 11.67296 11.67613 11.70747 11.68149 11.69794 11.66499 11.64848 11.62359 11.65290 11.62807 11.61625 1981 Q3 1981 Q4 1982 Q1 1982 Q2 1982 Q3 1982 Q4 1983 Q1 1983 Q2 1983 Q3 1983 Q4 1984 Q1 1984 Q2 1984 Q3 1984 Q4 1985 Q1 11.58011 11.61773 11.58204 11.58286 11.59171 11.60937 11.67504 11.74612 11.75458 11.82135 11.82601 11.85045 11.84171 11.89603 11.91905 1985 Q2 1985 Q3 1985 Q4 1986 Q1 1986 Q2 1986 Q3 1986 Q4 1987 Q1 1987 Q2 1987 Q3 11.94830 12.00589 12.05405 12.08219 12.08665 12.05509 12.06025 12.01860 12.02987 12.00582

Solution 2:

On the other hand, a simpler solution would be to create a functionality to convert an ARDL/ECM model into a regular lm model. An ARDL is nothing more than a simple linear model anyway.
This way the predict() function would be used the same way as with any other lm object.

Solution 3:

Combining both of these solutions. The functionality of solution 2 would be used for practical usage such as forecasting, and solution 1 for special cases when someone needs the heritage from the dynlm environment.

Big concern:

In the following example, I show how you can still forecast using the ARDL package.
Basically, you just do manually the two solutions I mentioned above.
Converte the model in dynlm or in lm and then use predict().
But doing this experiment I noticed this!

ardl_3132_dynlm <- dynlm(ardl_3132$full_formula, data = denmark) # the same model in `dynlm` class
ardl_3132_lm <- lm(LRM ~ ., data = ardl_3132$model) # the same model in `lm` class
used_data <- ardl_3132_lm$model # the data used in the model, excluding hte observations used for creating the differences and lags

plot(1:52, as.numeric(ardl_3132_dynlm$fitted.values), type='l') # fitted values from the  `dynlm` model
lines(as.numeric(ardl_3132_lm$fitted.values), col = "blue") # fitted values from the  `lm` model
lines(predict(ardl_3132_lm, newdata = used_data), col="green") # predicted values from the  `lm` model
lines(predict(ardl_3132_dynlm, newdata = used_data), col = "red") # predicted values from the  `dynlm` model

image

These four series, in theory, should be identical.
Here, all series overlap except for the predicted values using the dynlm object, which are wrong for some reason...
At least, this proves that using an lm object to forecast using the predict() function works well!

I think I will go for solution 2 first (the option to convert into lm object), as it should be easy, and then I will try to add also the option to convert into dynlm object (solution 1).
What do you think? Your opinion would be much valuable

@Natsiopoulos
Copy link
Owner

Solution 2 is finally done in 880fae4.
Please, let me know what you think about it.

@nipnipj
Copy link

nipnipj commented May 16, 2023

...
ardl <- model$best_model
predict(ardl, newdata = test_data)
Error in L(Co2pc, 1) : could not find function "L"

I think, it is because of

── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()

or not.

@Natsiopoulos
Copy link
Owner

Natsiopoulos commented May 16, 2023

Yes, this was the problem.
Using the new function to_lm() you can convert the ardl model into a regular lm. Then, predict() should work just fine!
You can download the new version of the package from the dev branch. It's already on its way to CRAN and it should be available online in the next 1 or 2 days.

The conflicts you mention are irrelevant though.

@nipnipj
Copy link

nipnipj commented May 16, 2023

lm <- ardl %>% to_lm()
insample_data <- ardl$model
pp <- predict(lm, newdata = insample_data)

It works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants