-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdp_modeling.Rmd
345 lines (269 loc) · 9.45 KB
/
gdp_modeling.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
---
output:
github_document:
toc: yes
toc_depth: 6
html_document:
toc: yes
toc_depth: 6
toc_float: yes
---
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #333;
}
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}
</style>
<div class="topnav">
<a class="active" href="./index">Home</a>
<a href="./data">Data</a>
<a href="./EDA.html">Exploratory Data Analysis</a>
<a href="./gdp_modeling.html">Modeling of GDP</a>
<a href="https://github.com/sjmiller8182/gdp-pred/blob/master/analysis/gdp_modeling.Rmd">Veiw on GitHub</a>
</div>
# GDP Modeling
**Stuart Miller**
**`r Sys.time()`**
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(tswge)
library(gridExtra)
```
```{r, read-data, include=FALSE}
#read data with set columns
data <- read_csv('../data/economic_indicators_all_ex_3mo_china.csv',
col_types = cols(
date = col_character(),
gdp_change = col_double(),
unrate = col_double(),
nfjobs = col_double(),
treas10yr = col_double(),
treas3mo = col_double(),
treas10yr3mo = col_double(),
fedintrate = col_double(),
libor3mo = col_double(),
personincomechg = col_double(),
corpprofitchg = col_double()
)
)
gdp <- data$gdp_change
```
# Univariate Modeling
## Plots
**Assess Constant Mean**
Based on the realization, it looks like there may be a slight downward trend.
This appears to be more pronounced prior to time step 75.
After time step 75, the movement of the realization appears to be more flat.
We will assume that the assumption of constant mean is not met.
**Assess Constant Variance**
The variance of the realization appears to be larger eariler in the realization (around and before 75).
Later in time, the variance of the realization appears to decrease.
Based on these observations and because only one realization is possible,
we will assume that the assumption of constant variance is not met.
**Assess Constant ACF**
The ACF of teh first half and the ACF of the second half appear to be different.
Both show expotentially dampped autocorrelations from 0,
but these autocorrelations fall off slower in the first half.
Additionally, the ACFs show different oscillator behavior.
**Conclusion**
Given the assessment of stationary above,
we propose that this realization does not meet the requirements of a stationary process.
```{r}
vals <- plotts.sample.wge(gdp, lag.max = 50)
size = length(gdp)
acf(gdp[1:size/2], lag.max = 40)
acf(gdp[(size/2+1):(size-1)], lag.max = 40)
```
## ARMA Fit
### ARMA Order Selection by AIC/BIC
The following models are suggested by both AIC and BIC
* ARMA(2, 1)
* ARMA(1, 2)
* ARMA(3, 1)
```{r}
# should fits for AIC and BIC
bic_vals <- aic5.wge(gdp, type = 'bic')
aic_vals <- aic5.wge(gdp, type = 'aic')
cbind(bic_vals,aic_vals)
```
### Fit ARMA Model
A fit of the ARMA(2,1), which selected by AIC and BIC, as a root very close to one.
This is also a suggestion of a non-stationary series.
```{r}
est.arma <- est.arma.wge(gdp, 2, 1)
factor.wge(phi = est.arma$phi)
factor.wge(phi = est.arma$theta)
```
### Comparison of True Plots
The true plots of the model fit are shown below.
It is notable that the evidence of oscillation seen in the sample autocorrelation is not present in the true plots of the model fit.
```{r}
vals <- plotts.true.wge(n = length(gdp), phi = est.arma$phi, theta = est.arma$theta, lag.max = 50)
```
## ARIMA Fit
After taking the first difference, the only first autocorrelation appears to be significant.
This suggests that an MA(1) may be sufficient to explain the noise after the first difference.
```{r}
diff <- artrans.wge(data$gdp_change, phi.tr = 1, plottr = TRUE)
bic_vals <- aic5.wge(diff, type = 'bic')
aic_vals <- aic5.wge(diff, type = 'aic')
cbind(bic_vals,aic_vals)
```
```{r}
est.arima <- est.arma.wge(diff, 0, 1)
```
# Forecast The Models
## Forecast with a Sliding Window
* Window size: 12 (3 years)
* Validation size: 6 (1.5 years)
* Window step size: 6 (1.5 years)
```{r}
# control loop variables
n.head <- 6 # size of the validation forecast
window.size <- 12 # size of the rolling window (train and test)
# training size is `window.size - n.head + 1`
window.start.step <- 6 # size of the step to take between each iteration
# create dummy vactors ot hold ASE values
f1.mse.values <- c(1:19)
f2.mse.values <- c(1:19)
# collect the overall forecasts and limits
f1.fcast <- c(rep(NA,135))
f1.fcast.ul <- c(rep(NA,135))
f1.fcast.ll <- c(rep(NA,135))
f2.fcast <- c(rep(NA,135))
f2.fcast.ul <- c(rep(NA,135))
f2.fcast.ll <- c(rep(NA,135))
# roll over the realization
for (i in seq(0, 30)){
idx.start <- i * window.start.step + 1
idx.end <- idx.start + window.size
# forecast with f1
f1 <- fore.aruma.wge(gdp[ idx.start : idx.end ],
phi = est.arma$phi,
theta = est.arma$theta,
plot = F,
n.ahead = n.head,
lastn = T)
# get the rolling forecast and limits
for (j in seq(1, n.head)){
f1.fcast[idx.end - n.head + j] <- f1$f[j]
f1.fcast.ul[idx.end - n.head + j] <- f1$ul[j]
f1.fcast.ll[idx.end - n.head + j] <- f1$ll[j]
}
# calculate ASE from f1 forecast
f1.mse.values[i+1] <- mean( ( gdp[ (idx.end - n.head + 1) : idx.end ] - f1$f )^2 )
# forecast with f2
f2 <- fore.aruma.wge(gdp[ idx.start : idx.end ],
d = 1,
phi = 0,
theta = est.arima$theta,
plot = F,
n.ahead = n.head,
lastn = T)
# get the rolling forecast and limits
for (j in seq(1, n.head)){
f2.fcast[idx.end - n.head + j] <- f2$f[j]
f2.fcast.ul[idx.end - n.head + j] <- f2$ul[j]
f2.fcast.ll[idx.end - n.head + j] <- f2$ll[j]
}
# calculate ASE from f2 forecast
f2.mse.values[i+1] <- mean( ( gdp[ (idx.end - n.head + 1) : idx.end ] - f2$f )^2 )
}
# put the ASE values in a dataframe for plotting
model.res <- data.frame(
ASE = c(f1.mse.values, f2.mse.values),
Model = factor(c(rep('Model 1',length(f1.mse.values)),rep('Model 2',length(f2.mse.values))))
)
# just formatting the data to make the plot easier
f1.data <- data.frame(
Realization = gdp[seq_along(f1.fcast)],
Forecast = f1.fcast,
UpperLimit = f1.fcast.ul,
LowerLimit = f1.fcast.ll,
Time = seq_along(f1.fcast),
Model = rep('Model 1',length(f1.fcast))
)
f1.data <- gather(f1.data, Series, Value, Realization:LowerLimit, factor_key = T)
f2.data <- data.frame(
Realization = gdp[seq_along(f2.fcast)],
Forecast = f2.fcast,
UpperLimit = f2.fcast.ul,
LowerLimit = f2.fcast.ll,
Time = seq_along(f2.fcast),
Model = rep('Model 2',length(f2.fcast))
)
f2.data <- gather(f2.data, Series, Value, Realization:LowerLimit, factor_key = T)
f1f2 <- rbind(f1.data, f2.data)
print(paste('The mean ASE for model 1:', mean(f1.mse.values)))
print(paste('The mean ASE for model 2:', mean(f2.mse.values)))
```
## Compare ASE Values
```{r}
p1 <- data.frame(
Model = as.factor(c(rep('Model.1', length(f1.mse.values)),
rep('Model.2', length(f2.mse.values)))),
ASE = c(f1.mse.values, f2.mse.values)
) %>%
ggplot(aes(y = ASE)) +
geom_boxplot() +
facet_wrap(~ Model) +
ggtitle('Boxplots of ASEs') +
xlab('Model')
p2 <- data.frame(
Model.1.ASE = f1.mse.values,
Model.2.ASE = f2.mse.values
) %>%
ggplot(aes(y = f1.mse.values, x = f2.mse.values)) +
geom_point() +
geom_smooth(method = 'lm') +
ggtitle('Model ASE Correlation') +
xlab('Model 2 ASE Values') +
ylab('Model 1 ASE Values')
grid.arrange(p1, p2, ncol = 2, top = 'Model ASE Plots')
```
## Forecast Plots
In the plots below, model 1 is the ARMA(2,1) model and model 2 is the ARIMA(0,1,1) model.
The forecasts begin at the vertical line `t =` `r window.size - n.head + 2`.
```{r, univariate-forecast, fig.width=15, message=FALSE, warning=FALSE}
ggplot(f1f2, aes(y = Value, x = Time, col = Series)) +
geom_line() +
facet_wrap(~ Model, dir = 'v') +
geom_vline(xintercept = window.size - n.head + 2) +
#geom_text(aes(x=27, label="\nForecast", y=7000), colour="blue", angle=0, size = 6) +
#geom_text(aes(x=13, label="\nInitial\nTraining", y=7000), colour="blue", angle=0, size = 6) +
labs(title = 'Forecasts and Prediction Limits for Model 1 & 2') +
theme(plot.title = element_text(hjust = 0.5))
```
### Forecast Conclusion
The ARIMA(0,1,1) model generally appears to capture the movement of the realization better than the ARMA(2,1) model.
The ARMA model appear to have forecast jumps that are not consistent with the realization;
the ARIMA model does not exhibit this behavior.
The prediction bounds of the ARIMA model also appear to show more confidence on the movement of the realization.
**Rolling Window Validation Results**
| Model | ASE |
|-------|-----|
| ARMA(2,1) | 18.35 |
| ARIMA(0,1,1) | 13.24 |
The ARIMA model appears to be a better model of the realization than the ARMA model.