Series de Tiempo
Problema No. 1
library(vars)
## Loading required package: MASS
## Loading required package: strucchange
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: sandwich
## Loading required package: urca
## Loading required package: lmtest
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.7
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x stringr::boundary() masks strucchange::boundary()
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## x dplyr::select() masks MASS::select()
library(ggfortify)
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Registered S3 methods overwritten by 'forecast':
## method from
## autoplot.Arima ggfortify
## autoplot.acf ggfortify
## autoplot.ar ggfortify
## autoplot.bats ggfortify
## autoplot.decomposed.ts ggfortify
## autoplot.ets ggfortify
## autoplot.forecast ggfortify
## autoplot.stl ggfortify
## autoplot.ts ggfortify
## fitted.ar ggfortify
## fortify.ts ggfortify
## residuals.ar ggfortify
library(seasonal)
##
## Attaching package: 'seasonal'
## The following object is masked from 'package:tibble':
##
## view
library(tseries)
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))
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
plot(diff_ts)

plot(decompose(diff_ts))

acf(timeseries)

pacf(timeseries)

par(mfrow=c(1,2))
modelo1 = arima(timeseries, order=c(1,2,0))
modelo2 = arima(timeseries, order=c(1,2,1))
modelo3 = arima(timeseries, order=c(3,0,1))
modelo1$aic
## [1] 153.4622
modelo2$aic
## [1] 154.7564
modelo3$aic
## [1] 165.0761
El modelo 1 es el mejor modelo porque el aic es el menor (153.4622)
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))

Problema No. 2
library(vars)
library(tidyverse)
library(ggfortify)
library(forecast)
library(seasonal)
library(tseries)
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(diff(timeseries)))
plot(diff_ts)

plot(decompose(diff_ts))

adf.test(diff_ts)
## Warning in adf.test(diff_ts): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: diff_ts
## Dickey-Fuller = -4.2759, Lag order = 3, p-value = 0.01
## alternative hypothesis: stationary
acf(timeseries)

pacf(timeseries)

modelo1 = arima(timeseries, order=c(1,3,0))
modelo2 = arima(timeseries, order=c(1,3,1))
modelo3 = arima(timeseries, order=c(3,3,1))
modelo1$aic
## [1] 137.1494
modelo2$aic
## [1] 131.8348
modelo3$aic
## [1] 134.6631
El mejor modelo es el 2 porque el aic es menor (131.8348)
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.42557, df = 1, p-value = 0.5142
autoplot(forecast(modelo1))

Problema 3
library(vars)
library(tidyverse)
library(ggfortify)
library(forecast)
library(seasonal)
library(tseries)
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(1,2,1))
modelo3 = arima(timeseries, order=c(3,2,1))
modelo1$aic
## [1] 165.4344
modelo2$aic
## [1] 159.0981
modelo3$aic
## [1] 147.5952
El mejor modelo es el 3 porque el aic es menor (147.5952)
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.096137, df = 1, p-value = 0.7565
autoplot(forecast(modelo1))
