DESARROLLO DEL TALLER 5 DE ECONOMETRÍA 2

A continuación se muestran los códigos necesarios para la estimación de los modelos del taller.

Código del Desarrollo del Punto 1

library(xts)
library(tseries)
library(forecast)
library(lmtest)
library(readxl)
library(stargazer)

DATA <- read_excel("C:/Programacion en R/5. ECONOMETRIA 2/ECO-EJ5/euretails.xlsx")

ST <- ts(DATA, start = c(1996), frequency = 4)
y <- ST[,3]
y

summary(y)   #| Media (Mean)
frequency(y) #| Trimestral -> 4
start(y)     #| Comienza en el Trimestre 1 de 1996
end(y)       #| Termina en el Trimestre 4 de 2011


par(mfrow=c(1,1))
plot(y, main="Serie de Tiempo - euretails", col = "red") 

boxplot(y ~ cycle(y)) 
ggseasonplot(log(y), col=rainbow(4), year.labels=TRUE) 
ggseasonplot(log(y), year.labels=TRUE, continuous=TRUE)

dy = diff(y, differences = 1)       #| Diferencia regular del logaritmo
s4y = diff(y, lag=4)                #| Diferencia estacional del logaritmo
d1s4y = diff(s4y, differences = 1)  #| Diferencia regular y estacional del logaritmo

dly = diff(log(y), differences = 1)   #| Diferencia regular del logaritmo
s4ly = diff(log(y), lag=4)            #| Diferencia estacional del logaritmo
d1s4ly = diff(s4ly, differences = 1)  #| Diferencia regular y estacional del logaritmo

#| GRÁFICAS

#| Gráficas de las series normales
par(mfrow=c(1,2))
ts.plot(y, main="Serie Original (Y)")          
ts.plot(log(y), main="Log(Y)") 

#| Gráficas de las series diferenciadas regulares
par(mfrow=c(1,2))
ts.plot(dy, main="Diferencia Regular de (Y)") 
ts.plot(dly, main="Diferencia Regular del Log(Y)") 

#| Gráficas de las series diferenciadas estacionales
par(mfrow=c(1,2))
ts.plot(s4y, main="Diferencia Estacional de (Y)")
ts.plot(s4ly, main="Diferencia Estacional del Log(Y)")

#| Gráficas de las series con diferencias regulares y estacionales 
par(mfrow=c(1,2))
ts.plot(d1s4y, main="Diferencia Regular y Estacional de (Y)")
ts.plot(d1s4ly, main="Diferencia Regular y Estacional del Log(Y)")

#| Gráfico de FACS y FACP de (y)
par(mfrow=c(1,2))
FACS <- acf(y, main="FACS", lag.max = 16, ylim=c(-1,1))
FACP <- pacf(y, main="FACP", lag.max = 16,ylim=c(-1,1))
FACS
FACP

#| Gráfico de FACS y FACP de (y) con diferencia regular
FACS <- acf(dy, main="FACS", lag.max = 16, ylim=c(-1,1))
FACP <- pacf(dy, main="FACP", lag.max = 16,ylim=c(-1,1))
FACS
FACP

#| Gráfico de FACS y FACP de (y) con diferencia estacional
FACS <- acf(s4y, main="FACS", lag.max = 16, ylim=c(-1,1))
FACP <- pacf(s4y, main="FACP", lag.max = 16,ylim=c(-1,1))
FACS
FACP

#| Gráfico de FACS y FACP de (y) con diferencia regular y estacional
FACS <- acf(d1s4y, main="FACS", lag.max = 16, ylim=c(-1,1))
FACP <- pacf(d1s4y, main="FACP", lag.max = 16,ylim=c(-1,1))
FACS
FACP


rpdqParam1 = c(0,1,1)
SPDQParam1 = c(0,1,1)
MODELO1 <- arima(y, rpdqParam1, seasonal = list(order = SPDQParam1, period = 4))
coeftest(MODELO1)

rpdqParam2 = c(1,1,0)
SPDQParam2 = c(1,1,0)
MODELO2 <- arima(y, rpdqParam2, seasonal = list(order = SPDQParam2, period = 4))
coeftest(MODELO2)

rpdqParam3 = c(0,1,2)
SPDQParam3 = c(0,1,1)
MODELO3 <- arima(y, rpdqParam3, seasonal = list(order = SPDQParam3, period = 4))
coeftest(MODELO3)

rpdqParam4 = c(0,1,3)
SPDQParam4 = c(0,1,1)
MODELO4 <- arima(y, rpdqParam4, seasonal = list(order = SPDQParam4, period = 4))
coeftest(MODELO4)

stargazer(MODELO4, type="text", out="mod1.doc") 

#| Supuesto de NO AC
tsdiag(MODELO1) 
tsdiag(MODELO2) 
tsdiag(MODELO3) 
tsdiag(MODELO4) 

#| Supuesto de NORMALIDAD
jarque.bera.test(MODELO4$residuals) 

#| Prueba de raíces inversas
autoplot(MODELO4) 

#| Criterio Akaike y Bayesiano 

AIC(MODELO1)  
AIC(MODELO2)
AIC(MODELO3)
AIC(MODELO4)

BIC(MODELO1) 
BIC(MODELO2)
BIC(MODELO3)  
BIC(MODELO4

#| Se pronostican 2 años, 8 trimestres por ser la serie trimestral
par(mfrow=c(1,1))
MODELO4_PRED <- forecast(MODELO4, h=8)
MODELO4_PRED

plot(MODELO4_PRED)

Código del Desarrollo del Punto 2

library(xts)
library(tseries)
library(forecast)
library(lmtest)
library(readxl)
library(stargazer)

DATA <- read_excel("C:/Programacion en R/5. ECONOMETRIA 2/ECO-EJ5/M1 2006-2019.xlsx")

ST <- ts(DATA, start = c(2006), frequency = 12)
y <- ST[,3]
y

summary(y)   #| Media (Mean)
frequency(y) #| Mensual -> 12
start(y)     #| Comienza en el Mes 1 de 2006
end(y)       #| Termina en el Mes 12 de 2019


par(mfrow=c(1,1))
plot(y, main="Serie de Tiempo - euretails", col = "red") 

boxplot(y ~ cycle(y)) 
ggseasonplot(log(y), col=rainbow(4), year.labels=TRUE) 
ggseasonplot(log(y), year.labels=TRUE, continuous=TRUE)


dy = diff(y, differences = 1)       #| Diferencia regular del logaritmo
s12y = diff(y, lag=12)                #| Diferencia estacional del logaritmo
d1s12y = diff(s12y, differences = 1)  #| Diferencia regular y estacional del logaritmo

dly = diff(log(y), differences = 1)   #| Diferencia regular del logaritmo
s12ly = diff(log(y), lag=12)            #| Diferencia estacional del logaritmo
d1s12ly = diff(s12ly, differences = 1)  #| Diferencia regular y estacional del logaritmo

#| Gráficas de las series normales
par(mfrow=c(1,2))
ts.plot(y, main="Serie Original (Y)")          
ts.plot(log(y), main="Log(Y)") 

#| Gráficas de las series diferenciadas regulares
par(mfrow=c(1,2))
ts.plot(dy, main="Diferencia Regular de (Y)") #| Varianza creciente
ts.plot(dly, main="Diferencia Regular del Log(Y)") #| Varianza estable

#| Gráficas de las series diferenciadas estacionales
par(mfrow=c(1,2))
ts.plot(s12y, main="Diferencia Estacional de (Y)")
ts.plot(s12ly, main="Diferencia Estacional del Log(Y)")

#| Gráficas de las series con diferencias regulares y estacionales 
par(mfrow=c(1,2))
ts.plot(d1s12y, main="Diferencia Regular y Estacional de (Y)")
ts.plot(d1s12ly, main="Diferencia Regular y Estacional del Log(Y)")

#| Gráfico de FACS y FACP de (y)
par(mfrow=c(1,2))
FACS <- acf(y, main="FACS", lag.max = 36, ylim=c(-1,1))
FACP <- pacf(y, main="FACP", lag.max = 36,ylim=c(-1,1))
FACS
FACP

#| Gráfico de FACS y FACP de (y) con diferencia regular
FACS <- acf(dy, main="FACS", lag.max = 36, ylim=c(-1,1))
FACP <- pacf(dy, main="FACP", lag.max = 36,ylim=c(-1,1))
FACS
FACP

#| Gráfico de FACS y FACP de (y) con diferencia estacional
FACS <- acf(s12y, main="FACS", lag.max = 36, ylim=c(-1,1))
FACP <- pacf(s12y, main="FACP", lag.max = 36,ylim=c(-1,1))
FACS
FACP

#| Gráfico de FACS y FACP de (y) con diferencia regular y estacional
FACS <- acf(d1s12y, main="FACS", lag.max = 36, ylim=c(-1,1))
FACP <- pacf(d1s12y, main="FACP", lag.max = 36,ylim=c(-1,1))
FACS
FACP

SPDQParam1 = c(1,1,0)
MODELO1 <- arima(y, c(3,1,2),fixed=c(NA,NA,NA,rep(0,1),NA,NA), 
                 seasonal = list(order = SPDQParam1, period = 12))
coeftest(MODELO1)

rpdqParam2 = c(2,1,2)
SPDQParam2 = c(0,1,1)
MODELO2 <- arima(y, rpdqParam2, seasonal = list(order = SPDQParam2, period = 12))
coeftest(MODELO2)

rpdqParam3 = c(1,1,2)
SPDQParam3 = c(1,1,0)
MODELO3 <- arima(y, rpdqParam3, seasonal = list(order = SPDQParam3, period = 12))
coeftest(MODELO3)

rpdqParam4 = c(1,1,2)
SPDQParam4 = c(0,1,1)
MODELO4 <- arima(y, rpdqParam4, seasonal = list(order = SPDQParam4, period = 12))
coeftest(MODELO4)

stargazer(MODELO4, type="text", out="mod4.doc") 

tsdiag(MODELO1)     
tsdiag(MODELO2) 
tsdiag(MODELO3) 
tsdiag(MODELO4) 

#| Supuesto de NORMALIDAD
jarque.bera.test(MODELO1$residuals) 
jarque.bera.test(MODELO2$residuals) 
jarque.bera.test(MODELO3$residuals) 
jarque.bera.test(MODELO4$residuals) 

#| Prueba de raíces inversas
autoplot(MODELO1) 
autoplot(MODELO2) 
autoplot(MODELO3) 
autoplot(MODELO4) 

#| Criterio Akaike y Bayesiano 

AIC(MODELO1)  
AIC(MODELO2)
AIC(MODELO3)
AIC(MODELO4)  

BIC(MODELO1) 
BIC(MODELO2)
BIC(MODELO3)  
BIC(MODELO4)  

par(mfrow=c(1,1))
MODELO4_PRED <- forecast(MODELO4, h=12)
MODELO4_PRED

plot(MODELO4_PRED)