Dynamic regression is like ordinary regression with explanatory variables, but the error term is now an ARIMA process instead of white noise.
Dataset uschange
from the fpp2
package contains growth rates of personal consumption and personal income in the US. You might want to model consumption as a function of income. Add parameter xreq
to the auto.arima()
function. xreg
is a matrix of predictor variables.
::uschange %>% head() fpp2
## Consumption Income Production Savings Unemployment
## 1970 Q1 0.6159862 0.9722610 -2.4527003 4.8103115 0.9
## 1970 Q2 0.4603757 1.1690847 -0.5515251 7.2879923 0.5
## 1970 Q3 0.8767914 1.5532705 -0.3587079 7.2890131 0.5
## 1970 Q4 -0.2742451 -0.2552724 -2.1854549 0.9852296 0.7
## 1971 Q1 1.8973708 1.9871536 1.9097341 3.6577706 -0.1
## 1971 Q2 0.9119929 1.4473342 0.9015358 6.0513418 -0.1
# (uschange.arima <- auto.arima(uschange[, "Consumption"],
# xreg = uschange[, "Income"]))
# # Forecast fit as fc
# uschange.fc <- forecast(uschange.arima, xreg = rep(10, 6))
#
# # Plot fc with x and y labels
# autoplot(uschange.fc) + xlab("Month") + ylab("Sales")
# Time plots of demand and temperatures
#autoplot(elec[, c("Demand", "Temperature")], facets = TRUE)
# Matrix of regressors
#xreg <- cbind(MaxTemp = elec[, "Temperature"],
# MaxTempSq = elec[, "Temperature"]^2,
# Workday = elec[, "Workday"])
# Fit model
#fit <- auto.arima(elec[, "Demand"], xreg = xreg)
# Forecast fit one day ahead
#forecast(fit, xreg = cbind(20, 20^2, 1))