-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathREADME.Rmd
executable file
·190 lines (108 loc) · 3.91 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# caretForecast
<!-- badges: start -->
<!-- badges: end -->
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)
```