Forecasting with R’s arima function
You can do a lot of univariate forecasting using only base R. Here are a few examples. I’ve made use of a few convenience functions from the tstools package, but only to create the trend and seasonal dummies, and you could easily remove that dependency if you want.
We’ll generate a time series with 100 quarterly observations starting in 2000Q1.
set.seed(100)
y <- ts(rnorm(100), frequency=4, start=c(2000,1))
library(tstools)
ARMA models
Fitting an ARMA(1,1) model and predicting the next four quarters is straightforward:
fit.arma11 <- arima(y, order=c(1,0,1))
predict(fit.arma11, 4)
ARMA models with a linear time trend
What if we want a linear time trend in the model? We have to add the time trend as the xreg
argument for estimation, and then supply the value of the trend for the predictions. tstools functions make.trend
and trend.after
come to the rescue:
tr <- make.trend(y)
fit.tr <- arima(y, order=c(1,0,1), xreg=tr)
predict(fit.tr, 4, newxreg=trend.after(y, 4))
ARMA models with linear time trend and seasonal component
If we want to add multiple variables (a time trend plus three quarter dummies) we can pass a matrix to xreg
and newxreg
:
seasonal <- quarter.dummy(y)
fit.seasonal <- arima(y, order=c(1,0,1), xreg=tr %~% seasonal)
predict(fit.seasonal, 4, newxreg=trend.after(y, 4) %~% dummy.after(y, 4))
ARX models
The trend and seasonal models are special cases of the ARX model, for particular choices of exogenous variables. You can use any variables for xreg
and newxreg
as long as you know their values in the sample and after the sample (perhaps predicting their values after the sample).
Last updated August 24, 2024