Problema 1
library(vars)
## Warning: package 'vars' was built under R version 4.1.3
## 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)
#problema 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))

#H0: La serie tiene raÃces unitarias No es estacionaria > 0.05
#Ha: La serie no tiene raÃces unitarias Es estacionaria < 0.05
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
#el mejor modelo es el 1 porque ....
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 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))

#H0: La serie tiene raÃces unitarias No es estacionaria > 0.05
#Ha: La serie no tiene raÃces unitarias Es estacionaria < 0.05
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))
modelo1$aic
## [1] 137.1494
modelo2$aic
## [1] 131.8348
#el mejor modelo es el 2 porque es la que tiene un valor menor
checkresiduals(modelo2$residuals)
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.

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

# problema 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))

#H0: La serie tiene raÃces unitarias No es estacionaria > 0.05
#Ha: La serie no tiene raÃces unitarias Es estacionaria < 0.05
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))
modelo1$aic
## [1] 165.4344
modelo2$aic
## [1] 159.0981
#el mejor modelo es el 2 porque es la que tiene un valor menor
checkresiduals(modelo2$residuals)
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.

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