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.
auto.arima
andxreg
to implement a transfer function? $\endgroup$