-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcross_validation_detailedexample_2_UPDATE.qmd
191 lines (155 loc) · 3.91 KB
/
cross_validation_detailedexample_2_UPDATE.qmd
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
```{r}
library(fpp3)
```
```{r}
google_stock <-
gafa_stock %>%
filter(Symbol %in% c("GOOG", "AAPL"), year(Date) >= 2015) %>%
group_by(Symbol) %>%
arrange(Symbol, Date) %>%
mutate(day = row_number()) %>%
ungroup() %>%
update_tsibble(index = day, regular = TRUE)
stocks_2015 <- google_stock %>% filter(year(Date) == 2015)
```
```{r}
stocks_2015_tr <-
stocks_2015 %>%
stretch_tsibble(.init = 20, .step = 1) %>%
select(Symbol, .id, everything())
```
```{r}
# How many training datasets do we have for each stock asset?
stocks_2015_tr %>%
as_tibble() %>%
group_by(Symbol) %>%
summarize(
n_tr_sets = max(.id)
)
# How many observations does each training dataset have?
stocks_2015_tr %>%
as_tibble() %>%
group_by(Symbol, .id) %>%
summarize(
nobs = n()
)
```
```{r}
stocks_2015_fit_cv <-
stocks_2015_tr %>%
model(
Drift = RW(Close ~ drift()),
Mean = MEAN(Close),
SES = ETS(Close ~ error("A") + trend("N") + season("N"))
)
```
```{r}
# Number of forecasts:
# Number of tseries = 466
# Nmodels = 3
# Nforecasts per model = 8
466 * 3 * 8
```
```{r}
stocks_2015_fc <-
stocks_2015_fit_cv %>%
forecast(h=8)
stocks_2015_fc
```
```{r}
stocks_2015
```
```{r}
cv_accuracy <-
stocks_2015_fc %>%
accuracy(stocks_2015) %>%
arrange(Symbol, RMSE)
cv_accuracy
```
```{r}
stocks_2015
```
```{r}
# Step 1. Compute forecasts - Done
stocks_2015_fc
# Step 2 - Bring the test data information to the table containing the forecasts
# LEFT JOIN
stocks_2015_close <-
stocks_2015 %>%
select(Symbol, day, Close) %>%
rename(Close_data = Close)
errors_manual <-
stocks_2015_fc %>%
left_join(stocks_2015_close, by=c("Symbol", "day"))
# Step 3 - Compute the error at each point in time, for each training dataset
errors_manual <-
errors_manual %>%
mutate(
fc_error = Close_data - .mean,
fc_error_pc = fc_error / Close_data
)
# Step 4 - Average depending on the desired output
cv_errors_manual <-
errors_manual %>%
as_tibble() %>% # Cast it as a normal dataframe to have group_by work normally
group_by(Symbol, .model) %>%
summarize(
MAE = mean(abs(fc_error), na.rm=TRUE),
RMSE = sqrt(mean(fc_error^2, na.rm=TRUE)),
MAPE = mean(abs(fc_error_pc), na.rm=TRUE)
) %>%
arrange(Symbol, RMSE)
all.equal(cv_errors_manual$RMSE, cv_accuracy$RMSE)
```
```{r}
# Create the column h and render back to a fable
stocks_2015_fc_fable <-
stocks_2015_fc %>%
group_by(.id, Symbol, .model) %>%
mutate(
h = row_number()
) %>%
select(.id, Symbol, .model, h, day, everything()) %>%
ungroup() %>%
as_fable(response = "Close", distribution = Close)
```
```{r}
cv_accuracy_h <-
stocks_2015_fc_fable %>%
accuracy(stocks_2015, by=c("Symbol", ".model", "h")) %>%
arrange(Symbol, h, RMSE)
cv_accuracy_h %>%
filter(h==4)
```
```{r}
# Step 1. Compute forecasts and add the column h
stocks_2015_fc_fable
# Step 2 - Bring the test data information to the table containing the forecasts
# LEFT JOIN
stocks_2015_close <-
stocks_2015 %>%
select(Symbol, day, Close) %>%
rename(Close_data = Close)
errors_manual <-
stocks_2015_fc_fable %>%
left_join(stocks_2015_close, by=c("Symbol", "day"))
# Step 3 - Compute the error at each point in time, for each training dataset
errors_manual <-
errors_manual %>%
mutate(
fc_error = Close_data - .mean,
fc_error_pc = ((Close_data - .mean) / Close_data)*100
)
# Step 4 - Average depending on the desired output
cv_errors_manual_h <-
errors_manual %>%
as_tibble() %>% # Cast it as a normal dataframe to have group_by work normally
group_by(Symbol, .model, h) %>%
summarize(
MAE = mean(abs(fc_error), na.rm=TRUE),
RMSE = sqrt(mean(fc_error^2, na.rm=TRUE)),
MAPE = mean(abs(fc_error_pc), na.rm=TRUE)
) %>%
arrange(Symbol, h, RMSE)
all.equal(cv_errors_manual_h$MAPE, cv_accuracy_h$MAPE)
```