knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
caretForecast aspires to equip users with the means of using machine learning algorithms for time series data forecasting.
The CRAN version with:
install.packages("caretForecast")
The development version from GitHub with:
# install.packages("devtools") devtools::install_github("Akai01/caretForecast")
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.
library(caretForecast) data(retail_wide, package = "caretForecast")
i <- 8 dtlist <- caretForecast::split_ts(retail_wide[,i], test_size = 12) training_data <- dtlist$train testing_data <- dtlist$test fit <- ARml(training_data, max_lag = 12, caret_method = "glmboost", verbose = FALSE) forecast(fit, h = length(testing_data), level = c(80,95))-> fc accuracy(fc, testing_data) autoplot(fc) + autolayer(testing_data, series = "testing_data")
i <- 9 dtlist <- caretForecast::split_ts(retail_wide[,i], test_size = 12) training_data <- dtlist$train testing_data <- dtlist$test fit <- ARml(training_data, max_lag = 12, caret_method = "cubist", verbose = FALSE) forecast(fit, h = length(testing_data), level = c(80,95), PI = TRUE)-> fc accuracy(fc, testing_data) autoplot(fc) + autolayer(testing_data, series = "testing_data")
i <- 9 dtlist <- caretForecast::split_ts(retail_wide[,i], test_size = 12) training_data <- dtlist$train testing_data <- dtlist$test fit <- ARml(training_data, max_lag = 12, caret_method = "svmLinear2", verbose = FALSE, pre_process = c("scale", "center")) forecast(fit, h = length(testing_data), level = c(80,95), PI = TRUE)-> fc accuracy(fc, testing_data) autoplot(fc) + autolayer(testing_data, series = "testing_data") get_var_imp(fc) get_var_imp(fc, plot = F)
i <- 8 dtlist <- caretForecast::split_ts(retail_wide[,i], test_size = 12) training_data <- dtlist$train testing_data <- dtlist$test fit <- ARml(training_data, max_lag = 12, caret_method = "ridge", verbose = FALSE) forecast(fit, h = length(testing_data), level = c(80,95), PI = TRUE)-> fc accuracy(fc, testing_data) autoplot(fc) + autolayer(testing_data, series = "testing_data") get_var_imp(fc) get_var_imp(fc, plot = F)
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.
xreg_train <- forecast::seasonaldummy(AirPassengers) newxreg <- forecast::seasonaldummy(AirPassengers, h = 21) fit <- ARml(AirPassengers, max_lag = 4, caret_method = "cubist", seasonal = FALSE, xreg = xreg_train, verbose = FALSE) fc <- forecast(fit, h = 12, level = c(80, 95, 99), xreg = newxreg) autoplot(fc) get_var_imp(fc)
library(hts) data("htseg1", package = "hts") fc <- forecast(htseg1, h = 4, FUN = caretForecast::ARml, caret_method = "ridge", verbose = FALSE) plot(fc)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.