-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_inflation.R
More file actions
334 lines (251 loc) · 8.58 KB
/
01_inflation.R
File metadata and controls
334 lines (251 loc) · 8.58 KB
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
# Libraries
library(tidyverse)
library(tidymodels)
library(forecast)
library(modeltime)
library(timetk)
# Read in Data
cpi_df <- read_csv("data/cpi.csv") |>
filter(date >= "2010-01-01") |>
mutate(lagged_cpi = lag(cpi, n = 1)) |>
mutate(log_dif_cpi = log(cpi) - log(lagged_cpi)) |>
filter(!is.na(log_dif_cpi)) # remove first observation without lag
med_inflation_df <- read_csv("data/med_inflation.csv") |>
filter(date >= min(cpi_df$date)) |> # only need data that cpi has
mutate(lagged_med = lag(med_inflation, n = 1)) |>
mutate(log_dif_med = log(med_inflation) - log(lagged_med)) |>
filter(!is.na(log_dif_med)) # remove first observation without lag
# get difference of the logs of each value
ggplot() +
#geom_line(aes(x = eci$date, y = eci$log_dif_eci, color = "ECI")) +
geom_line(aes(x = cpi_df$date, y = cpi_df$log_dif_cpi, color = "CPI")) +
geom_line(aes(x = med_inflation_df$date, y = med_inflation_df$log_dif_med, color = "Medical Inflation")) +
labs(
x = "Date",
y = "Value",
color = "Legend"
) +
scale_color_manual(values = c("CPI" = "black", "Medical Inflation" = "red"))
ggplot() +
geom_line(aes(x = eci_df$date, y = eci_df$log_dif_eci, color = "ECI")) +
labs(
title = "Log Difference of ECI",
x = "Date",
y = "Value",
color = "Legend"
) +
scale_color_manual(values = c("ECI" = "blue"))
full_inflation <- cpi_df |>
full_join(eci_df, by = "date") |>
full_join(med_inflation_df, by = "date")
GGally::ggpairs(full_inflation |> select(log_dif_eci, log_dif_cpi, log_dif_med))
## Explore lags
lag_df <- full_inflation |>
mutate(lagged = lag(eci, n = 1))
cor(lag_df$lagged, lag_df$cpi, use = "complete.obs")
# ARIMA Model for CPI -------------------------------------------------------
cpi_df |> plot_time_series(date, log_dif_cpi)
cpi_df %>%
pull(log_dif_cpi) %>%
forecast::ggAcf(., lag.max = 2*365) +
ggtitle("CPI")
cpi_recipe <- recipe(log_dif_cpi~date, data = cpi_df) #%>%
#step_date(date, features = c("year", "month"))
ar_model <- arima_reg() |>
set_engine("auto_arima")
cpi_cv_split <- time_series_split(cpi_df, assess = '5 years', cumulative = TRUE)
cpi_ar_wf <- workflow() %>%
add_recipe(cpi_recipe) %>%
add_model(ar_model) %>%
fit(data = training(cpi_cv_split))
cpi_cv_results <- modeltime_calibrate(cpi_ar_wf,
new_data = testing(cpi_cv_split))
# Visualize CV results
p1 <- cpi_cv_results %>%
modeltime_forecast(
new_data = testing(cpi_cv_split),
actual_data = cpi_df) %>%
plot_modeltime_forecast(.interactive = FALSE)
## Evaluate the accuracy
cpi_cv_results %>%
modeltime_accuracy() %>%
table_modeltime_accuracy(
.interactive = FALSE
)
## Refit to all data then forecast
cpi_fullfit <- cpi_cv_results %>%
modeltime_refit(data = cpi_df)
names(cpi_fullfit)
cpi_fullfit$.model
eci_fullfit$.model
cpi_preds <- cpi_fullfit %>%
modeltime_forecast(h = '30 years') %>%
rename(date = .index, log_dif_cpi = .value) %>%
select(date, log_dif_cpi) %>%
full_join(cpi_df, by = 'date')
p2 <- cpi_fullfit %>%
modeltime_forecast(h = '30 years', actual_data = cpi_df) %>%
plot_modeltime_forecast(.interactive = FALSE)
plotly::subplot(p1, p2, nrows = 2)
# Get residuals
cpi_residuals <- cpi_fullfit %>%
modeltime_residuals(new_data = cpi_df) %>%
rename(date = .index, residual = .residuals) %>%
select(date, residual)
# plot residuals
ggplot(data = cpi_residuals) +
geom_line(aes(x = date, y = residual)) +
labs(
title = "CPI Residuals",
x = "Date",
y = "Value",
color = "Legend"
)
cpi_residuals %>%
pull(residual) %>%
forecast::ggAcf(., lag.max = 2*365) +
ggtitle("Residuals")
# ARIMA Model for ECI ----------------------------------------------------
quarterly_cpi <- cpi_df |>
filter(month(date) %in% c(3, 6, 9, 12)) |>
mutate(lagged_cpi = lag(cpi, n = 1)) |>
mutate(log_dif_cpi = log(cpi) - log(lagged_cpi),
lagged_cpi = lag(log_dif_cpi, n = 1)) |>
filter(!is.na(log_dif_cpi) & !is.na(lagged_cpi))
eci_df <- read_csv("data/eci.csv") |>
filter(date >= min(quarterly_cpi$date)) |> # only need data that cpi has
mutate(lagged_eci = lag(eci, n = 1)) |>
mutate(log_dif_eci = log(eci) - log(lagged_eci)) |>
filter(!is.na(log_dif_eci)) |> # remove first observation without lag
left_join(quarterly_cpi |> select(date, log_dif_cpi, lagged_cpi), by = "date")
eci_df |> plot_time_series(date, log_dif_eci)
eci_df %>%
pull(log_dif_eci) %>%
forecast::ggAcf(., lag.max = 5*365) +
ggtitle("ECI")
eci_recipe <- recipe(log_dif_eci~date+log_dif_cpi + lagged_cpi, data = eci_df)
ar_model <- arima_reg() |>
set_engine("auto_arima", ic = "bic")
eci_cv_split <- time_series_split(eci_df, assess = '2 years', cumulative = TRUE)
eci_ar_wf <- workflow() %>%
add_recipe(eci_recipe) %>%
add_model(ar_model) %>%
fit(data = training(eci_cv_split))
eci_cv_results <- modeltime_calibrate(eci_ar_wf,
new_data = testing(eci_cv_split))
# Visualize CV results
p3 <- eci_cv_results %>%
modeltime_forecast(
new_data = testing(eci_cv_split),
actual_data = eci_df) %>%
plot_modeltime_forecast(.interactive = TRUE)
## Evaluate the accuracy
eci_cv_results %>%
modeltime_accuracy() %>%
table_modeltime_accuracy(
.interactive = FALSE
)
## Refit to all data then forecast
eci_fullfit <- eci_cv_results %>%
modeltime_refit(data = eci_df)
eci_preds <- eci_fullfit %>%
modeltime_forecast(h = '5 years') %>%
rename(date = .index, log_dif_eci = .value) %>%
select(date, log_dif_eci) %>%
bind_rows(eci_df)
# plot model output
ggplot(data = eci_preds) +
geom_line(aes(x = date, y = log_dif_eci)) +
labs(
title = "ECI Forecast",
x = "Date",
y = "Value",
color = "Legend"
)
p4 <- eci_fullfit %>%
modeltime_forecast(h = '30 years', actual_data = eci_df) %>%
plot_modeltime_forecast(.interactive = FALSE)
plotly::subplot(p3, p4, nrows = 2)
# Get residuals
eci_residuals <- eci_fullfit %>%
modeltime_residuals(new_data = eci_df) %>%
rename(date = .index, residual = .residuals) %>%
select(date, residual)
# plot residuals
ggplot(data = eci_residuals) +
geom_line(aes(x = date, y = residual)) +
labs(
title = "ECI Residuals",
x = "Date",
y = "Value",
color = "Legend"
)
eci_residuals %>%
pull(residual) %>%
forecast::ggAcf(., lag.max = 2*365) +
ggtitle("Residuals")
# ARIMA Model for Medical Inflation ------------------------------------------------------
med_inflation_df %>%
pull(log_dif_med) %>%
forecast::ggAcf(., lag.max = 3*365) +
ggtitle("Medical Inflation")
med_recipe <- recipe(log_dif_med~date, data = med_inflation_df)
ar_model <- arima_reg() |>
set_engine("auto_arima")
med_cv_split <- time_series_split(med_inflation_df, assess = '20 years', cumulative = TRUE)
med_ar_wf <- workflow() %>%
add_recipe(med_recipe) %>%
add_model(ar_model) %>%
fit(data = training(med_cv_split))
med_cv_results <- modeltime_calibrate(med_ar_wf,
new_data = testing(med_cv_split))
# Visualize CV results
p5 <- med_cv_results %>%
modeltime_forecast(
new_data = testing(med_cv_split),
actual_data = med_inflation_df) %>%
plot_modeltime_forecast(.interactive = TRUE)
## Evaluate the accuracy
med_cv_results %>%
modeltime_accuracy() %>%
table_modeltime_accuracy(
.interactive = FALSE
)
## Refit to all data then forecast
med_fullfit <- med_cv_results %>%
modeltime_refit(data = med_inflation_df)
# get residuals from model
med_fullfit$.model
med_preds <- med_fullfit %>%
modeltime_forecast(h = '30 years') %>%
rename(date = .index, log_dif_med = .value) %>%
select(date, log_dif_med) %>%
bind_rows(med_inflation_df)
p6 <- med_fullfit %>%
modeltime_forecast(h = '30 years', actual_data = med_inflation_df) %>%
plot_modeltime_forecast(.interactive = FALSE)
plotly::subplot(p5, p6, nrows = 2)
# Get residuals
med_residuals <- med_fullfit %>%
modeltime_residuals(new_data = med_inflation_df) %>%
rename(date = .index, residual = .residuals) %>%
select(date, residual)
# plot residuals
ggplot(data = med_residuals) +
geom_line(aes(x = date, y = residual)) +
labs(
title = "Med Residuals",
x = "Date",
y = "Value",
color = "Legend"
)
med_residuals %>%
pull(residual) %>%
forecast::ggAcf(., lag.max = 2*365) +
ggtitle("Residuals")
# Explore Correlations between residuals ----------------------------------
residuals_df <- cpi_residuals |>
rename(cpi_residual = residual) |>
full_join(eci_residuals |> rename(eci_residual = residual), by = "date") |>
full_join(med_residuals |> rename(med_residual = residual), by = "date")
GGally::ggpairs(residuals_df |> select(-date))