项目作者: Akai01

项目描述 :
Forecasting using caret package infrastructure
高级语言: R
项目地址: git://github.com/Akai01/caretForecast.git
创建时间: 2020-09-21T13:57:10Z
项目社区:https://github.com/Akai01/caretForecast

开源协议:Other

下载


caretForecast

caretForecast aspires to equip users with the means of using machine
learning algorithms for time series data forecasting.

Installation

The CRAN version with:

  1. install.packages("caretForecast")

The development version from GitHub with:

  1. # install.packages("devtools")
  2. devtools::install_github("Akai01/caretForecast")

Example

By using caretForecast, users can train any regression model that is
compatible with the caret package. This allows them to use any machine
learning model they need in order to solve specific problems, as shown
by the examples below.

Load the library

  1. library(caretForecast)
  2. #> Registered S3 method overwritten by 'quantmod':
  3. #> method from
  4. #> as.zoo.data.frame zoo
  5. data(retail_wide, package = "caretForecast")

Forecasting with glmboost

  1. i <- 8
  2. dtlist <- caretForecast::split_ts(retail_wide[,i], test_size = 12)
  3. training_data <- dtlist$train
  4. testing_data <- dtlist$test
  5. fit <- ARml(training_data, max_lag = 12, caret_method = "glmboost",
  6. verbose = FALSE)
  7. #> initial_window = NULL. Setting initial_window = 301
  8. #> Loading required package: ggplot2
  9. #> Loading required package: lattice
  10. forecast(fit, h = length(testing_data), level = c(80,95))-> fc
  11. accuracy(fc, testing_data)
  12. #> ME RMSE MAE MPE MAPE MASE
  13. #> Training set 1.899361e-14 16.55233 11.98019 -1.132477 6.137096 0.777379
  14. #> Test set 7.472114e+00 21.68302 18.33423 2.780723 5.876433 1.189685
  15. #> ACF1 Theil's U
  16. #> Training set 0.6181425 NA
  17. #> Test set 0.3849258 0.8078558
  18. autoplot(fc) +
  19. autolayer(testing_data, series = "testing_data")

Forecasting with cubist regression

  1. i <- 9
  2. dtlist <- caretForecast::split_ts(retail_wide[,i], test_size = 12)
  3. training_data <- dtlist$train
  4. testing_data <- dtlist$test
  5. fit <- ARml(training_data, max_lag = 12, caret_method = "cubist",
  6. verbose = FALSE)
  7. #> initial_window = NULL. Setting initial_window = 301
  8. forecast(fit, h = length(testing_data), level = c(80,95), PI = TRUE)-> fc
  9. accuracy(fc, testing_data)
  10. #> ME RMSE MAE MPE MAPE MASE
  11. #> Training set 0.5498926 13.33516 10.36501 0.0394589 2.223005 0.3454108
  12. #> Test set -17.9231242 47.13871 32.03537 -2.6737257 4.271373 1.0675691
  13. #> ACF1 Theil's U
  14. #> Training set 0.3979153 NA
  15. #> Test set 0.4934659 0.4736043
  16. autoplot(fc) +
  17. autolayer(testing_data, series = "testing_data")

Forecasting using Support Vector Machines with Linear Kernel

  1. i <- 9
  2. dtlist <- caretForecast::split_ts(retail_wide[,i], test_size = 12)
  3. training_data <- dtlist$train
  4. testing_data <- dtlist$test
  5. fit <- ARml(training_data, max_lag = 12, caret_method = "svmLinear2",
  6. verbose = FALSE, pre_process = c("scale", "center"))
  7. #> initial_window = NULL. Setting initial_window = 301
  8. forecast(fit, h = length(testing_data), level = c(80,95), PI = TRUE)-> fc
  9. accuracy(fc, testing_data)
  10. #> ME RMSE MAE MPE MAPE MASE
  11. #> Training set -0.2227708 22.66741 17.16009 -0.2476916 3.712496 0.5718548
  12. #> Test set -1.4732653 28.47932 22.92438 -0.4787467 2.883937 0.7639481
  13. #> ACF1 Theil's U
  14. #> Training set 0.3290937 NA
  15. #> Test set 0.3863955 0.3137822
  16. autoplot(fc) +
  17. autolayer(testing_data, series = "testing_data")

  1. get_var_imp(fc)

  1. get_var_imp(fc, plot = F)
  2. #> loess r-squared variable importance
  3. #>
  4. #> only 20 most important variables shown (out of 23)
  5. #>
  6. #> Overall
  7. #> lag12 100.0000
  8. #> lag1 83.9153
  9. #> lag11 80.4644
  10. #> lag2 79.9170
  11. #> lag3 79.5019
  12. #> lag4 78.3472
  13. #> lag9 78.1634
  14. #> lag5 78.0262
  15. #> lag7 77.9153
  16. #> lag8 76.7721
  17. #> lag10 76.4275
  18. #> lag6 75.7056
  19. #> S1-12 3.8169
  20. #> S3-12 2.4230
  21. #> C2-12 2.1863
  22. #> S5-12 2.1154
  23. #> C4-12 1.9426
  24. #> C1-12 0.5974
  25. #> C6-12 0.3883
  26. #> S2-12 0.2220

Forecasting using Ridge Regression

  1. i <- 8
  2. dtlist <- caretForecast::split_ts(retail_wide[,i], test_size = 12)
  3. training_data <- dtlist$train
  4. testing_data <- dtlist$test
  5. fit <- ARml(training_data, max_lag = 12, caret_method = "ridge",
  6. verbose = FALSE)
  7. #> initial_window = NULL. Setting initial_window = 301
  8. forecast(fit, h = length(testing_data), level = c(80,95), PI = TRUE)-> fc
  9. accuracy(fc, testing_data)
  10. #> ME RMSE MAE MPE MAPE MASE
  11. #> Training set 7.11464e-14 12.69082 9.53269 -0.1991292 5.212444 0.6185639
  12. #> Test set 1.52445e+00 14.45469 12.04357 0.6431543 3.880894 0.7814914
  13. #> ACF1 Theil's U
  14. #> Training set 0.2598784 NA
  15. #> Test set 0.3463574 0.5056792
  16. autoplot(fc) +
  17. autolayer(testing_data, series = "testing_data")

  1. get_var_imp(fc)

  1. get_var_imp(fc, plot = F)
  2. #> loess r-squared variable importance
  3. #>
  4. #> only 20 most important variables shown (out of 23)
  5. #>
  6. #> Overall
  7. #> lag12 100.0000
  8. #> lag1 84.2313
  9. #> lag2 78.9566
  10. #> lag11 78.5354
  11. #> lag3 76.3480
  12. #> lag10 74.4727
  13. #> lag4 74.1330
  14. #> lag7 74.0737
  15. #> lag9 73.9113
  16. #> lag5 73.2040
  17. #> lag8 72.7224
  18. #> lag6 71.5713
  19. #> S1-12 6.3658
  20. #> S3-12 2.7341
  21. #> C2-12 2.7125
  22. #> S5-12 2.4858
  23. #> C4-12 2.2137
  24. #> C6-12 0.6381
  25. #> C1-12 0.5176
  26. #> S2-12 0.4464

Adding external variables

The xreg argument can be used for adding promotions, holidays, and other
external variables to the model. In the example below, we will add
seasonal dummies to the model. We set the ‘seasonal = FALSE’ to avoid
adding the Fourier series to the model together with seasonal dummies.

  1. xreg_train <- forecast::seasonaldummy(AirPassengers)
  2. newxreg <- forecast::seasonaldummy(AirPassengers, h = 21)
  3. fit <- ARml(AirPassengers, max_lag = 4, caret_method = "cubist",
  4. seasonal = FALSE, xreg = xreg_train, verbose = FALSE)
  5. #> initial_window = NULL. Setting initial_window = 132
  6. fc <- forecast(fit, h = 12, level = c(80, 95, 99), xreg = newxreg)
  7. autoplot(fc)

  1. get_var_imp(fc)

Forecasting Hierarchical or grouped time series

  1. library(hts)
  2. data("htseg1", package = "hts")
  3. fc <- forecast(htseg1, h = 4, FUN = caretForecast::ARml,
  4. caret_method = "ridge", verbose = FALSE)
  5. plot(fc)