1

Can someone explain me how to implement a dynamic linear model in R?

The concept is similar to a transfer function, which mathematically is defined as: $$ y_t=c+w(B)x_t + N_t $$ Where $y_t$ is the variable to forecast, $x_t$ is the exogenous variable, $w(B)$ is the backshift operator related to the exogenous variable, and $N_t$ is the error term following an ARMA model.

To implement a transfer function model in R, the function auto.arima with the xreg specification is used. For example, suppose that the goal is to forecast the energy price p using a transfer function model where the exogenous variable is the demand for energy, I can then write:

p <- auto.arima(p.train, xreg=demand.train, stationary=TRUE, seasonal=TRUE)
fcast.p <- forecast(p, h=90, xreg=demand.test)
error <- MAPE(fcast.p$mean, p.test)

But how about a dynamic linear regression model? This is mathematically defined as: $$ y_t=c + u(B)y_t + v(B) x_t + \epsilon_t $$ I know about the function dynlm, but I don't understand how to choose the optimal coefficients for lagged values of my variables.

mhdadk
  • 2,582
  • 1
  • 4
  • 17
  • Hello and welcome to Cross Validated! There seems to be more than one question in your question. To increase the chance of someone answering your question, consider asking a single question here, and then asking your other questions in separate posts. Also, please consider including a minimal working example in your question. For example, could you show us how you can use `auto.arima` and `xreg` to implement a transfer function? – mhdadk Apr 25 '21 at 14:09
  • Hi, thanks for your comment! I've modified the post so that it may sound clearer. – Flaminia Sarrantonio Apr 25 '21 at 16:33
  • By the way, you may find this question helpful: https://stats.stackexchange.com/questions/367856/simple-explanation-of-dynamic-linear-models?r=SearchResults – mhdadk Apr 25 '21 at 17:41
  • Thanks again, but it doesn't seem to answer the question about the lagged coefficients – Flaminia Sarrantonio Apr 25 '21 at 18:29
  • Hi: The last equation that you wrote represents an autoregressive distributed lag in econometrics. I didn't watch it because I don't have time but here's a video that goes through the steps of building an ARDL in R. https://www.youtube.com/watch?v=qoihlzu7CcM – mlofton Apr 26 '21 at 13:18
  • @mlofton this video was really useful! Thank you a lot – Flaminia Sarrantonio Apr 26 '21 at 13:38
  • Hi @Flaminia, would you like to include an answer to your own question based on the video? This would help others who find this question later on. – mhdadk Apr 26 '21 at 15:28
  • Flaminia: I'm glad that it helped. – mlofton Apr 27 '21 at 14:24

0 Answers0