-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseasonadjust.r
212 lines (159 loc) · 6.05 KB
/
seasonadjust.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
# Dependencias para graficas y formato
library("ggplot2")
library("ggfortify")
library("cowplot")
library("ggseas")
library("extrafont")
library("grid")
library("scales")
library("gt")
library("ggthemes")
source("script.r")
# Dependencias para datos
library("lubridate")
library("forecast")
library("tseries")
# importar fonts (usar una vez)
font_import()
loadfonts(device = "win")
windowsFonts()
db <- readxl::read_excel("Datos\\indicadores2020.xls", sheet = 1)
### leyendas de las graficas
fuente <- function(x, y) {
grid.text("Fuente: Elaboración propia con datos del INEGI", x, y,
gp = gpar(fontfamily = "Arial", fontface = "italic", cex = 0.7))
}
#Convertir columna periodos en date
db$periodos <- ym(db$periodos)
# Serie original
ggplot(data = db, mapping = aes(x = periodos, y = datos)) +
geom_line() + theme_cowplot(12, "Times New Roman") +
labs(x = "Año", y = "IMAI") +
theme(plot.title = element_text(hjust = 0.5) + theme.fxdat)
fuente(0.8, 0.01)
# Ajustar serie
## convertimos db en time series
db_ts <- ts(db$datos, start = c(1993, 1), frequency = 12)
plot(decompose(db_ts),
xlab = "Año",
cex.lab = 0.58,
cex.axis = 0.92)
#######ggseas (misma grafica pero en ggplot)
ggsdc(data = db, aes(x = periodos, y = datos),
method = "seas", start = c(1993, 1), frequency = 12) +
geom_line() +
labs(x = "Año", colour = "", y = " \n") +
theme_cowplot(12, "Times New Roman") +
theme(legend.position = c(0.17, 0.92))
fuente(0.8, 0.03)
## Para suavizar la serie y estabilizar la varianza aplicamos logaritmos
db_log <- ts(log(db$datos), start = c(1993, 1), frequency = 12)
db_log_plot <- autoplot(db_log) +
labs(y = "log(datos)", x = "Año") +
theme_cowplot(12, "Times New Roman")
db_plot <- autoplot(db_ts) +
labs(y = "datos", x = "Año") +
theme_cowplot(12, "Times New Roman")
### serie log
autoplot(db_log) +
labs(y = "log(datos)", x = "Año", title = "Transformación logarítmica") +
theme_cowplot(12, "Times New Roman") +
theme(plot.title = element_text(hjust = 0.5))
fuente(0.8, 0.01)
#### Serie original y log
plot_grid(db_plot, db_log_plot, ncol = 1, align = "v")
fuente(0.8, 0.01)
## Para eliminar tendencia, utilizamos primeras diferencias
first_difference <- diff(db_log)
autoplot(first_difference) + theme_cowplot(12, "Times New Roman") +
labs(x = "Año", y = "Dlog(db_ts)", title = "Primeras diferencias") +
theme(plot.title = element_text(hjust = 0.5))
fuente(0.8, 0.01)
## Eliminar estacionalidad, diferencias estacionales
seasonal_difference <- diff(first_difference, lag = 12)
autoplot(seasonal_difference) +
labs(x = "Año", y = "IMAI ajustado",
title = "Diferencias estacionales") +
theme_cowplot(12, "Times New Roman") +
theme(plot.title = element_text(hjust = 0.5))
fuente(0.8, 0.01)
comparassion <- cbind(seasonal_difference, first_difference)
autoplot(comparassion, facets = FALSE, xlab = "Año", ylab = "IMAI")
plot(seasonal_difference)
## Pruebas de estacionariedad
### Dickey fuller aumentada ADF
#### Testeando serie original
adf.test(db_ts, k = 12)# p-value > .05, no rechazas H0, no es estacionaria
pp.test(db_ts) # p-value < .05, rechazas H0, es estacionaria?
kpss.test(db_ts) # p-value < .05, rechazas H0 no es estacionaria
plot(db_ts)
#### Testeando serie transformada
adf.test(seasonal_difference, k = 12)#p-value < .05, rechazas H0 es estacionaria
pp.test(seasonal_difference) # p-value < .05, rechazas H0, es estacionaria
kpss.test(seasonal_difference) # p-value > .05, no rechazas H0, es estacionaria
# Graficar ambas
db_sd_plot <- autoplot(seasonal_difference) +
labs(y = "IMAI ajustado", x = "Año") +
theme_cowplot(12, "Times New Roman")
db_ts_plot <- autoplot(db_ts) +
labs(y = "IMAI", x = "Año") +
theme_cowplot(12, "Times New Roman")
plot_grid(db_ts_plot, db_sd_plot, ncol = 1, align = "v")
fuente(0.8, 0.02)
## FORECAST SIMPLE MODEL ######
n_training <- ((2020 - 1994) * 12) - 1 #Datos de 1993 a 2019
n_test <- 13 #Datos de enero 2020 a enero 2021
training_set <- head(seasonal_difference, n_training)
test_set <- tail(seasonal_difference, n_test)
### Mean forecast
mean_forecast <- meanf(training_set, h = 13, level = c(80, 95, 99))
plot_fx(mean_forecast, date.breaks = "3 years", date.format = "%Y",
x.title = "Año")
accuracy(mean_forecast, test_set)
### Random walk (= naive)
naive_forecast <- naive(training_set, h = 13, level = c(80, 95, 99))
plot_fx(naive_forecast, date.breaks = "3 years", date.format = "%Y",
x.title = "Año")
accuracy(naive_forecast, test_set)
## Random walk estacional
snaive_forecast <- snaive(training_set, h = 13, level = c(80, 95, 99))
plot_fx(snaive_forecast, date.breaks = "3 years", date.format = "%Y",
x.title = "Año")
accuracy(snaive_forecast, test_set)
### Comparar modelos con test set
## Modelo de Pronosticos
autoplot(mean_forecast) +
labs(y = "IMAI", x = "Año", title = NULL) +
autolayer(test_set) +
theme_cowplot(12, "Times New Roman") +
theme(legend.position = "none")
## Modelo naive
autoplot(naive_forecast) +
labs(y = "IMAI", x = "Año", title = NULL) +
autolayer(test_set) +
theme_cowplot(12, "Times New Roman") +
theme(legend.position = "none")
## Modelo snaive
autoplot(snaive_forecast) +
autolayer(test_set) +
labs(y = "IMAI", x = "Año", title = NULL) +
theme_cowplot(12, "Times New Roman") +
theme(legend.position = "none")
#Holt Winter model
n_training <- 311 + 13
training_set_hw <- head(db_ts, n_training)
test_set_hw <- tail(db_ts, n_test)
hw_forecast <- hw(training_set_hw, seasonal = "multiplicative")
hw_forecast <- forecast(hw_forecast, h = 13)
autoplot(hw_forecast) + autolayer(test_set_hw)
#ARIMA model
arima_forecast <- arima(training_set_hw, order = c(2, 1, 2))
arima_forecast <- forecast(arima_forecast, h = 13)
autoplot(arima_forecast) + autolayer(test_set_hw)
#Diebold-Mariano test
###Modelos basicos
snf_res <- na.omit(snaive_forecast$residuals)
nf_res <- na.omit(naive_forecast$residuals)
meanf_res <- na.omit(mean_forecast$residuals)
hwf_res <- tail(hw_forecast$residuals, 299)
dm.test(hwf_res, snf_res, h = 1, alternative = "greater")