February 13, 2023

How to Forecast Time Series in R

I was pointed to caretForecast to machine learning algorithms for time series data forecasting and decided to post this as a tutorial on how I did it (and before I forget). We'll use the UK Unemployment rate from FRED as our data. Without further ado, then:


require(caretForecast)
require(caret)
require( quantmod)
i <- 8
retail.wide <- getSymbols('UNRTUKA', auto.assign=F, src='FRED')
dtlist <- caretForecast::split_ts(as.ts(retail.wide), test_size = 12)
training_data <- dtlist$train
testing_data <- dtlist$test
fit <- caretForecast::ARml(training_data, max_lag = 12, caret_method = "glmboost", 
            verbose = FALSE)
fc <- caretForecast::forecast(fit, h = length(testing_data), level = c(80,95))
caretForecast::accuracy(fc, testing_data)

  Your output will look like:
  
initial_window = NULL. Setting initial_window = 225 > > ME RMSE MAE MPE MAPE MASE Training set 7.265863e-16 1.353875 0.9509708 -12.93998 25.39738 0.9920345 Test set 1.349505e+00 1.832183 1.4355957 18.10500 19.86195 1.4975859 ACF1 Theil's U Training set 0.1524037 NA Test set 0.6623072 1.997636

But, what we're really interested in are the forecasted values, which are embedded in fc, so let's have a look:

> fc
    Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
246       4.745729 3.340558 6.150899 2.168297 7.323160
247       4.766436 3.361266 6.171607 2.189005 7.343867
248       4.797869 3.392698 6.203040 2.220438 7.375300
249       4.860259 3.455089 6.265430 2.282828 7.437690
250       4.937558 3.532388 6.342729 2.360127 7.514990
251       5.013973 3.608803 6.419144 2.436542 7.591405
252       5.094964 3.689794 6.500135 2.517533 7.672395
253       5.174285 3.769114 6.579456 2.596854 7.751716
254       5.237659 3.832488 6.642829 2.660228 7.815090
255       5.298847 3.893677 6.704018 2.721416 7.876279
256       5.361814 3.956644 6.766985 2.784383 7.939245
257       5.416543 4.011372 6.821714 2.839112 7.993974
  

No comments:

Post a Comment