-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_Importaciones.R
421 lines (335 loc) · 12.7 KB
/
03_Importaciones.R
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# Inicialización ####
rm(list = ls())
library(tidyverse) #Para manejar bases de datos
library(tsibble) #Para manejar bases para series de tiempo
library(ggplot2) #Para graficar
library(knitr) #Para tablas
library(fpp3) #No recuerdo para que era
library(vars)
library(tseries)
library(urca) #para el adf.test
library(forecast)#para el auto.arima
library(aTSA)
library(fable) #Para correr modelos de series de tiempo
library(strucchange) #Para el test de Chow
df <- readRDS("bases/base_tp2.RDS")
dfh2003 <- readRDS("bases/base_tp2_h2003.RDS")
dfd2004 <- readRDS("bases/base_tp2_d2004.RDS")
# Adfs ####
#---------------#
# BASE COMPLETA #
#---------------#
adf_log_M <- ur.df(df$log_M,
type = c("none"),
lags = 4,
selectlags = "AIC")
summary(adf_log_M)
adf_log_demandaGlobal <- ur.df(df$log_demandaGlobal,
type = c("none"),
lags = 4,
selectlags = "AIC")
summary(adf_log_demandaGlobal)
adf_log_TCRM <- ur.df(df$log_TCRM,
type = c("none"),
lags = 4,
selectlags = "AIC")
summary(adf_log_TCRM)
#------------------------------#
# BASE COMPLETA EN DIFERENCIAS #
#------------------------------#
adf_dlog_M <- ur.df(diff(df$log_M),
type = c("none"),
lags = 4,
selectlags = "AIC")
summary(adf_dlog_M)
adf_dlog_demandaGlobal <- ur.df(diff(df$log_demandaGlobal),
type = c("none"),
lags = 4,
selectlags = "AIC")
summary(adf_dlog_demandaGlobal)
adf_dlog_TCRM <- ur.df(diff(df$log_TCRM),
type = c("none"),
lags = 4,
selectlags = "AIC")
summary(adf_dlog_TCRM)
#-----------------#
# BASE HASTA 2003 #
#-----------------#
adf_log_M_h2003 <- ur.df(dfh2003$log_M,
type = c("none"),
lags = 4,
selectlags = "AIC")
summary(adf_log_M_h2003)
adf_log_demandaGlobal_h2003 <- ur.df(dfh2003$log_demandaGlobal,
type = c("none"),
lags = 4,
selectlags = "AIC")
summary(adf_log_demandaGlobal_h2003)
adf_log_TCRM_h2003 <- ur.df(dfh2003$log_TCRM,
type = c("none"),
lags = 4,
selectlags = "AIC")
summary(adf_log_TCRM_h2003)
#------------------------------#
# BASE HASTA 2003 DIFERENCIADA #
#------------------------------#
adf_dlog_M_h2003 <- ur.df(diff(dfh2003$log_M),
type = c("none"),
lags = 4,
selectlags = "AIC"
)
summary(adf_dlog_M_h2003)
adf_dlog_demandaGlobal_h2003 <- ur.df(diff(dfh2003$log_demandaGlobal),
type = c("none"),
lags = 4,
selectlags = "AIC")
summary(adf_dlog_demandaGlobal_h2003)
adf_dlog_TCRM_h2003 <- ur.df(diff(dfh2003$log_TCRM),
type = c("none"),
lags = 4,
selectlags = "AIC")
summary(adf_dlog_TCRM_h2003)
#-----------------#
# BASE DESDE 2004 #
#-----------------#
adf_log_M_d2004 <- ur.df(dfd2004$log_M,
type = c("none"),
lags = 4,
selectlags = "AIC"
)
summary(adf_log_M_d2004)
adf_log_demandaGlobal_d2004 <- ur.df(dfd2004$log_demandaGlobal,
type = c("none"),
lags = 4,
selectlags = "AIC")
summary(adf_log_demandaGlobal_d2004)
adf_log_TCRM_d2004 <- ur.df(dfd2004$log_TCRM,
type = c("none"),
lags = 4,
selectlags = "AIC")
summary(adf_log_TCRM_d2004)
#------------------------------#
# BASE DESDE 2004 DIFERENCIADA #
#------------------------------#
adf_dlog_M_d2004 <- ur.df(diff(dfd2004$log_M),
type = c("none"),
lags = 4,
selectlags = "AIC"
)
summary(adf_dlog_M_d2004)
adf_dlog_demandaGlobal_d2004 <- ur.df(diff(dfd2004$log_demandaGlobal),
type = c("none"),
lags = 4,
selectlags = "AIC"
)
summary(adf_dlog_demandaGlobal_d2004)
adf_dlog_TCRM_d2004 <- ur.df(diff(dfd2004$log_TCRM),
type = c("none"),
lags = 4,
selectlags = "AIC")
summary(adf_dlog_TCRM_d2004)
rm(list = ls(pattern = "^adf"))
# Estacionalidad ####
## Estacionalidad en toda la base ####
### Estacionalidad importaciones
x11_log_M <- df |>
model(x11 = X_13ARIMA_SEATS(log_M ~ x11())) |>
components()
x11_log_M |>
ggplot(aes(x = Q)) +
geom_line(aes(y = log_M, colour = "Data")) +
geom_line(aes(y = season_adjust,
colour = "Seasonally Adjusted")) +
geom_line(aes(y = trend, colour = "Trend")) +
labs(y = "Persons (thousands)",
title = "Ajuste Estacional - Importaciones") +
scale_colour_manual(
values = c("gray", "#0072B2", "#D55E00"),
breaks = c("Data", "Seasonally Adjusted", "Trend")
)
### Estacionalidad demanda global
x11_log_demandaGlobal <- df |>
model(x11 = X_13ARIMA_SEATS(log_demandaGlobal ~ x11())) |>
components()
### Estacionalidad TCRM
x11_log_TCRM <- df |>
model(x11 = X_13ARIMA_SEATS(log_TCRM ~ x11())) |>
components()
## Estacionalidad hasta 2003 ####
### Estacionalidad importaciones
x11_log_M_h2003 <- dfh2003 |>
model(x11 = X_13ARIMA_SEATS(log_M ~ x11())) |>
components()
x11_log_M_h2003 |>
ggplot(aes(x = Q)) +
geom_line(aes(y = log_M, colour = "Data")) +
geom_line(aes(y = season_adjust,
colour = "Seasonally Adjusted")) +
geom_line(aes(y = trend, colour = "Trend")) +
labs(y = "Persons (thousands)",
title = "Ajuste Estacional - Importaciones hasta 2003") +
scale_colour_manual(
values = c("gray", "#0072B2", "#D55E00"),
breaks = c("Data", "Seasonally Adjusted", "Trend")
)
### Estacionalidad demanda global
x11_log_demandaGlobal_h2003 <- dfh2003 |>
model(x11 = X_13ARIMA_SEATS(log_demandaGlobal ~ x11())) |>
components()
### Estacionalidad TCRM
x11_log_TCRM_h2003 <- dfh2003 |>
model(x11 = X_13ARIMA_SEATS(log_TCRM ~ x11())) |>
components()
## Estacionalidad desde 2004 ####
x11_log_M_d2004 <- dfd2004 |>
model(x11 = X_13ARIMA_SEATS(log_M ~ x11())) |>
components()
x11_log_M_d2004 |>
ggplot(aes(x = Q)) +
geom_line(aes(y = log_M, colour = "Data")) +
geom_line(aes(y = season_adjust,
colour = "Seasonally Adjusted")) +
geom_line(aes(y = trend, colour = "Trend")) +
labs(y = "Persons (thousands)",
title = "Ajuste Estacional - Importaciones hasta 2003") +
scale_colour_manual(
values = c("gray", "#0072B2", "#D55E00"),
breaks = c("Data", "Seasonally Adjusted", "Trend")
)
### Estacionalidad demanda global
x11_log_demandaGlobal_d2004 <- dfd2004 |>
model(x11 = X_13ARIMA_SEATS(log_demandaGlobal ~ x11())) |>
components()
### Estacionalidad TCRM
x11_log_TCRM_d2004 <- dfd2004 |>
model(x11 = X_13ARIMA_SEATS(log_TCRM ~ x11())) |>
components()
# Desestacionalizando ####
df <- df %>% mutate(log_M = x11_log_M$season_adjust)
df <- df %>% mutate(log_demandaGlobal = x11_log_demandaGlobal$season_adjust)
df <- df %>% mutate(log_TCRM = x11_log_TCRM$season_adjust)
dfh2003 <- dfh2003 %>% mutate(log_M = x11_log_M_h2003$season_adjust)
dfh2003 <- dfh2003 %>% mutate(log_demandaGlobal = x11_log_demandaGlobal_h2003$season_adjust)
dfh2003 <- dfh2003 %>% mutate(log_TCRM = x11_log_TCRM_h2003$season_adjust)
dfd2004 <- dfd2004 %>% mutate(log_M = x11_log_M_d2004$season_adjust)
dfd2004 <- dfd2004 %>% mutate(log_demandaGlobal = x11_log_demandaGlobal_d2004$season_adjust)
dfd2004 <- dfd2004 %>% mutate(log_TCRM = x11_log_TCRM_d2004$season_adjust)
# Tests de Engle y Granger ####
## Base completa
reg_coint <- lm(log_M ~ log_demandaGlobal + log_TCRM, data = df)
residuos_coint <- reg_coint$residuals
test_eg <- ur.df(residuos_coint,
type = c("none"),
lags = 4,
selectlags = "AIC"
)
summary(test_eg)
## Base hasta 2003
reg_coint_h2003 <- lm(log_M ~ log_demandaGlobal + log_TCRM, data = dfh2003)
residuos_coint_h2003 <- reg_coint_h2003$residuals
test_eg_h2003 <- ur.df(residuos_coint_h2003,
type = c("none"),
lags = 4,
selectlags = "AIC"
)
summary(test_eg_h2003)
## Base desde 2004
reg_coint_d2004 <- lm(log_M ~ log_demandaGlobal, data = dfd2004)
residuos_coint_d2004 <- reg_coint_d2004$residuals
test_eg_d2004 <- ur.df(residuos_coint_d2004,
type = c("none"),
lags = 4,
selectlags = "AIC"
)
summary(test_eg_d2004)
# ECM (trivariado, uniecuacional) ####
## Base Completa
residuos_coint_lag <- lag(residuos_coint)[-1]
ecm <- lm(diff(log_M) ~ residuos_coint_lag + diff(log_demandaGlobal) + diff(log_TCRM), data = df)
summary(reg_coint)
summary(ecm)
## Base hasta 2003
residuos_coint_h2003_lag <- lag(residuos_coint_h2003)[-1]
ecm_h2003 <- lm(diff(log_M) ~ residuos_coint_h2003_lag + diff(log_demandaGlobal) + diff(log_TCRM), data = dfh2003)
summary(reg_coint_h2003)
summary(ecm_h2003)
## Base desde 2004
residuos_coint_d2004_lag <- lag(residuos_coint_d2004)[-1]
ecm_d2004 <- lm(diff(log_M) ~ residuos_coint_d2004_lag + diff(log_demandaGlobal), data = dfd2004)
summary(reg_coint_d2004)
summary(ecm_d2004)
# Identificación siguiendo a Wicken y Breusch ####
## WB con base completa
log_M_lag <- lag(df$log_M)[-1]
log_demandaGlobal_lag <- lag(df$log_demandaGlobal)[-1]
log_TCRM_lag <- lag(df$log_TCRM)[-1]
wb <- lm(diff(log_M) ~ diff(log_demandaGlobal)+ diff(log_TCRM) + log_M_lag + log_demandaGlobal_lag, data = df)
summary(wb)
## WB con base hasta 2003
log_M_lag_h2003 <- lag(dfh2003$log_M)[-1]
log_demandaGlobal_lag_h2003 <- lag(dfh2003$log_demandaGlobal)[-1]
log_TCRM_lag_h2003 <- lag(dfh2003$log_TCRM)[-1]
wb_h2003 <- lm(diff(log_M) ~ diff(log_demandaGlobal)+ diff(log_TCRM) + log_M_lag_h2003 + log_demandaGlobal_lag_h2003 + log_TCRM_lag_h2003, data = dfh2003)
summary(wb_h2003)
## WB con base desde 2004
log_M_lag_d2004 <- lag(dfd2004$log_M)[-1]
log_demandaGlobal_lag_d2004 <- lag(dfd2004$log_demandaGlobal)[-1]
log_TCRM_lag_d2004 <- lag(dfd2004$log_TCRM)[-1]
wb_d2004 <- lm(diff(log_M) ~ diff(log_demandaGlobal) + log_M_lag_d2004 + log_demandaGlobal_lag_d2004 + log_TCRM_lag_d2004, data = dfd2004)
summary(wb_d2004)
# VEC ####
## Base completa:
### Test de Johansen
data_vecm <- df[, c("log_M", "log_TCRM", "log_demandaGlobal")]
VARselect(data_vecm, lag.max = 4, type = "both")
# AIC indica 4 rezagos, pero hay que ver si
# absorben toda la autocorrleación:
varm <- VAR(data_vecm, p = 4,
type = "both",
season = NULL,
exogen = NULL)
autocorr_serial <- serial.test(varm,
lags.pt = 16,
type = "PT.asymptotic")
autocorr_serial
johansen <- ca.jo(data_vecm, type="trace", ecdet="const", K=4, spec="longrun")
summary(johansen)
vecm <- cajorls(johansen, r = 1)
vecm
## Base hasta 2003:
### Test de Johansen
data_vecm_h2003 <- dfh2003[, c("log_M", "log_TCRM", "log_demandaGlobal")]
VARselect(data_vecm_h2003, lag.max = 4, type = "both")
# AIC indica 4 rezagos
# AIC indica 4 rezagos, pero hay que ver si
# absorben toda la autocorrleación:
varm_h2003 <- VAR(data_vecm_h2003, p = 4,
type = "both",
season = NULL,
exogen = NULL)
autocorr_serial_h2003 <- serial.test(varm_h2003,
lags.pt = 16,
type = "PT.asymptotic")
autocorr_serial_h2003
johansen_h2003 <- ca.jo(data_vecm_h2003, type="trace", ecdet="const", K=4, spec="longrun")
summary(johansen_h2003)
vecm_h2003 <- cajorls(johansen_h2003, r = 1)
vecm_h2003
## Base desde 2004:
### Test de Johansen
data_vecm_d2004 <- dfd2004[, c("log_M", "log_TCRM", "log_demandaGlobal")]
VARselect(data_vecm_d2004, lag.max = 4, type = "both")
# AIC indica 4 rezagos, pero hay que ver si
# absorben toda la autocorrleación:
varm_d2004 <- VAR(data_vecm_d2004, p = 4,
type = "both",
season = NULL,
exogen = NULL)
autocorr_serial_d2004 <- serial.test(varm_d2004,
lags.pt = 16,
type = "PT.asymptotic")
autocorr_serial_d2004
johansen_d2004 <- ca.jo(data_vecm_d2004, type="trace", ecdet="const", K=3, spec="longrun")
summary(johansen_d2004)
vecm_d2004 <- cajorls(johansen_d2004, r = 1)
vecm_d2004