Serie de tiempo ARMA
Serie 1
set.seed(300)
timeseries=ts(arima.sim(list(order = c(1,1,2), ma=c(0.32,0.47), ar=0.8), n = 50)+20, start=2010, frequency = 4)
plot(timeseries)

plot(decompose(timeseries))

adf.test(timeseries)
##
## Augmented Dickey-Fuller Test
##
## data: timeseries
## Dickey-Fuller = -0.58049, Lag order = 3, p-value = 0.975
## alternative hypothesis: stationary
diff_ts<-diff(diff(timeseries))
plot(diff_ts)

plot(decompose(diff_ts))

adf.test(diff_ts)
##
## Augmented Dickey-Fuller Test
##
## data: diff_ts
## Dickey-Fuller = -3.5551, Lag order = 3, p-value = 0.04613
## alternative hypothesis: stationary
acf(timeseries)

pacf(timeseries)

modelo1 = arima(timeseries, order=c(1,2,0))
modelo2 = arima(timeseries, order=c(1,2,1))
modelo1$aic
## [1] 153.4622
modelo2$aic
## [1] 154.7564
checkresiduals(modelo1$residuals)
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.

Box.test(modelo1$residuals, type="Ljung-Box")
##
## Box-Ljung test
##
## data: modelo1$residuals
## X-squared = 0.24269, df = 1, p-value = 0.6223
autoplot(forecast(modelo1))

——————————————————————————————–
Serie 2
set.seed(400)
timeseries=ts(arima.sim(list(order = c(1,1,2), ma=c(0.32,0.47), ar=0.8), n = 50)+20, start=2010, frequency = 4)
plot(timeseries)

plot(decompose(timeseries))

adf.test(timeseries)
##
## Augmented Dickey-Fuller Test
##
## data: timeseries
## Dickey-Fuller = -1.9818, Lag order = 3, p-value = 0.5817
## alternative hypothesis: stationary
diff_ts<-diff(diff(timeseries))
plot(diff_ts)

plot(decompose(diff_ts))

adf.test(diff_ts)
##
## Augmented Dickey-Fuller Test
##
## data: diff_ts
## Dickey-Fuller = -3.2214, Lag order = 3, p-value = 0.09434
## alternative hypothesis: stationary
acf(timeseries)

pacf(timeseries)

modelo1 = arima(timeseries, order=c(1,2,0))
modelo2 = arima(timeseries, order=c(0,2,1))
modelo3 = arima(timeseries, order=c(1,2,1))
modelo1$aic
## [1] 128.2366
modelo2$aic
## [1] 129.11
modelo3$aic
## [1] 129.6608
checkresiduals(modelo1$residuals)
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.

Box.test(modelo1$residuals, type="Ljung-Box")
##
## Box-Ljung test
##
## data: modelo1$residuals
## X-squared = 0.090555, df = 1, p-value = 0.7635
autoplot(forecast(modelo1))

——————————————————————————————–
Serie 3
set.seed(500)
timeseries=ts(arima.sim(list(order = c(1,1,2), ma=c(0.32,0.47), ar=0.8), n = 50)+20, start=2010, frequency = 4)
plot(timeseries)

plot(decompose(timeseries))

adf.test(timeseries)
##
## Augmented Dickey-Fuller Test
##
## data: timeseries
## Dickey-Fuller = -1.0237, Lag order = 3, p-value = 0.926
## alternative hypothesis: stationary
diff_ts<-diff(diff(timeseries))
plot(diff_ts)

plot(decompose(diff_ts))

adf.test(diff_ts)
##
## Augmented Dickey-Fuller Test
##
## data: diff_ts
## Dickey-Fuller = -4.032, Lag order = 3, p-value = 0.01574
## alternative hypothesis: stationary
acf(timeseries)

pacf(timeseries)

modelo1 = arima(timeseries, order=c(1,2,0))
modelo2 = arima(timeseries, order=c(0,2,1))
modelo3 = arima(timeseries, order=c(1,2,1))
modelo4 = arima(timeseries, order=c(1,2,2))
modelo1$aic
## [1] 165.4344
modelo2$aic
## [1] 165.6711
modelo3$aic
## [1] 159.0981
modelo4$aic
## [1] 151.5689
checkresiduals(modelo4$residuals)
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.

Box.test(modelo4$residuals, type="Ljung-Box")
##
## Box-Ljung test
##
## data: modelo4$residuals
## X-squared = 0.00065946, df = 1, p-value = 0.9795
autoplot(forecast(modelo4))
