##IMPORTACION DE DATOS

library(tseries)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(forecast)
library(writexl)
uf=AirPassengers
plot(uf)

url=
"https://raw.githubusercontent.com/vmoprojs/DataLectures/master/pib_ec_const.csv"
datos=read.delim(url,sep = ",")
uf_s=AirPassengers
plot(uf)

uf_s=log(uf)
plot(uf_s)

adf.test(uf_s)
## Warning in adf.test(uf_s): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  uf_s
## Dickey-Fuller = -6.4215, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary

SERIE ESTACIONARIA

uf_sl=diff(uf_s)
uf_sld=diff(uf_sl,4)
adf.test(uf_sld)
## Warning in adf.test(uf_sld): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  uf_sld
## Dickey-Fuller = -5.5915, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
acf(uf_sld,lag.max = 12,main= "serie uf en diferencias")

## funcion de autocorrelacion parcial

pacf(uf_sld,lag.max = 12,main="serie uf en diferencias")

modelo sarima

mod1=arima(uf_sl,order = c(1,1,1),seasonal = c(0,1,1))
mod1
## 
## Call:
## arima(x = uf_sl, order = c(1, 1, 1), seasonal = c(0, 1, 1))
## 
## Coefficients:
##           ar1      ma1     sma1
##       -0.3356  -1.0000  -0.5530
## s.e.   0.0826   0.0271   0.0762
## 
## sigma^2 estimated as 0.00138:  log likelihood = 237.97,  aic = -467.93
mod=auto.arima(uf_sl,trace = T)
## 
##  ARIMA(2,0,2)(1,1,1)[12] with drift         : Inf
##  ARIMA(0,0,0)(0,1,0)[12] with drift         : -432.7415
##  ARIMA(1,0,0)(1,1,0)[12] with drift         : -472.4967
##  ARIMA(0,0,1)(0,1,1)[12] with drift         : -481.1033
##  ARIMA(0,0,0)(0,1,0)[12]                    : -434.799
##  ARIMA(0,0,1)(0,1,0)[12] with drift         : -447.8018
##  ARIMA(0,0,1)(1,1,1)[12] with drift         : -479.454
##  ARIMA(0,0,1)(0,1,2)[12] with drift         : -479.5049
##  ARIMA(0,0,1)(1,1,0)[12] with drift         : -475.0825
##  ARIMA(0,0,1)(1,1,2)[12] with drift         : Inf
##  ARIMA(0,0,0)(0,1,1)[12] with drift         : -465.3811
##  ARIMA(1,0,1)(0,1,1)[12] with drift         : -479.4557
##  ARIMA(0,0,2)(0,1,1)[12] with drift         : -479.1627
##  ARIMA(1,0,0)(0,1,1)[12] with drift         : -479.1871
##  ARIMA(1,0,2)(0,1,1)[12] with drift         : Inf
##  ARIMA(0,0,1)(0,1,1)[12]                    : -483.204
##  ARIMA(0,0,1)(0,1,0)[12]                    : -449.8846
##  ARIMA(0,0,1)(1,1,1)[12]                    : -481.5888
##  ARIMA(0,0,1)(0,1,2)[12]                    : -481.6381
##  ARIMA(0,0,1)(1,1,0)[12]                    : -477.2096
##  ARIMA(0,0,1)(1,1,2)[12]                    : Inf
##  ARIMA(0,0,0)(0,1,1)[12]                    : -467.459
##  ARIMA(1,0,1)(0,1,1)[12]                    : -481.5755
##  ARIMA(0,0,2)(0,1,1)[12]                    : -481.2929
##  ARIMA(1,0,0)(0,1,1)[12]                    : -481.2948
##  ARIMA(1,0,2)(0,1,1)[12]                    : -481.5558
## 
##  Best model: ARIMA(0,0,1)(0,1,1)[12]
mod
## Series: uf_sl 
## ARIMA(0,0,1)(0,1,1)[12] 
## 
## Coefficients:
##           ma1     sma1
##       -0.4018  -0.5569
## s.e.   0.0896   0.0731
## 
## sigma^2 = 0.001369:  log likelihood = 244.7
## AIC=-483.39   AICc=-483.2   BIC=-474.77

\((y_t-y_{t-1})-(y_{t-12}-y_{t-13})=-0.4018 E_{t-1}-0.5569/epsilon_{t-12}\) ##EVALUACION DEL MODELO 1

Box.test(mod1$residuals)
## 
##  Box-Pierce test
## 
## data:  mod1$residuals
## X-squared = 0.12585, df = 1, p-value = 0.7228
uf_sld=predict(mod1,4)
uf_sl=exp(uf_sld$pred)
uf_sl
##            Jan       Feb       Mar       Apr
## 1961 1.0372908 0.9442893 1.1245567 1.0280120
ts.plot(uf,uf_sl,col=c("blue","red"))