Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/akai01/caretforecast

Conformal Time Series Forecasting Using State of Art Machine Learning Algorithms
https://github.com/akai01/caretforecast

caret conformal-prediction data-science econometrics forecast forecasting forecasting-models machine-learning macroeconometrics microeconometrics r time-series time-series-forcasting time-series-prediction

Last synced: 2 days ago
JSON representation

Conformal Time Series Forecasting Using State of Art Machine Learning Algorithms

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# caretForecast

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

## Installation

The CRAN version with:

``` r
install.packages("caretForecast")

```

The development version from [GitHub](https://github.com/) with:

``` r
# install.packages("devtools")
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

```{r example1}
library(caretForecast)

data(retail_wide, package = "caretForecast")

```

### Forecasting with glmboost
```{r, example2}
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")

```

### Forecasting with cubist regression

```{r, example3}
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")

```

### Forecasting using Support Vector Machines with Linear Kernel

```{r, example4}

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)

```

### Forecasting using Ridge Regression

```{r, example5}

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)
```

## 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.

```{r, example6}

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)

```

# Forecasting Hierarchical or grouped time series

```{r, example7, warning=FALSE, message=FALSE}
library(hts)

data("htseg1", package = "hts")

fc <- forecast(htseg1, h = 4, FUN = caretForecast::ARml,
caret_method = "ridge", verbose = FALSE)

plot(fc)

```