library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(seasonal)
library(tseries)
library(readxl)
data <- read_excel(file.choose(), sheet = 1)
data <- data.frame(data)
str(data)
## 'data.frame': 35 obs. of 2 variables:
## $ DATE: chr "1987" "1988" "1989" "1990" ...
## $ GDP : num 16.08 18.65 12.13 3.96 7.96 ...
head(data)
## DATE GDP
## 1 1987 16.078431
## 2 1988 18.648649
## 3 1989 12.129841
## 4 1990 3.961402
## 5 1991 7.962872
## 6 1992 5.882354
data <- ts(data$GDP,frequency =1 )
plot(data)

#Paso 1 Diferenciar hasta que p-value < 0.05 o que salga estacionario
tseries::adf.test(data)
##
## Augmented Dickey-Fuller Test
##
## data: data
## Dickey-Fuller = -2.7934, Lag order = 3, p-value = 0.265
## alternative hypothesis: stationary
diff_ts<-diff(data)
tseries::adf.test(data)
##
## Augmented Dickey-Fuller Test
##
## data: data
## Dickey-Fuller = -2.7934, Lag order = 3, p-value = 0.265
## alternative hypothesis: stationary
plot(diff_ts)

#paso 2
acf(diff_ts) #q

pacf(diff_ts) #p

#Paso 3 Setear Diferenciacion en d, poner numero en q y p de cuentos se salen del blue area
# p d q
# 0 0 1
# 1 0 2
modelo1 = Arima(data, order=c(0,0,1), include.drift = T)
modelo2 = Arima(data, order=c(1,0,2), include.drift = T)
#Se compara el aic para determinar el mejor modelo (mas pequeño)
modelo1$aic
## [1] 233.7134
modelo2$aic
## [1] 237.6816
#Paso 4 metemos los datos del mejor modelo
modelo1 = Arima(data, order=c(0,0,1), include.drift = T)
a<-forecast::forecast(modelo1, h=20)
plot(a)

checkresiduals(modelo1$residuals)
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.

#Paso 5 Si todo esta dentro del Blue Area todo esta bien.
acf(modelo1$residuals) #q

pacf(modelo1$residuals)#p

# p d q
# 0 0 1
#Paso 6 Meter analis anterior en el primer set de datos y en el segundo meter nuevo analisis de resifuals.
modelo2 = Arima(data, order=c(0,0,1), seasonal= list(order=c(0,0,1),period=1), include.drift = T)
a<-forecast::forecast(modelo2, h=20)
plot(a)

#Segun este modelo arima compuesto por pdq:0,0,1, el PIB de ARUBA se comportora de manera bajista.
#Paso 7
checkresiduals(modelo2$residuals)
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.

a
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 36 -4.591949 -12.87257 3.6886717 -17.25606 8.072167
## 37 -2.073388 -10.46327 6.3164904 -14.90460 10.757824
## 38 -2.482166 -10.87222 5.9078931 -15.31365 10.349322
## 39 -2.784890 -11.17495 5.6051690 -15.61638 10.046598
## 40 -3.087614 -11.47767 5.3024450 -15.91910 9.743874
## 41 -3.390338 -11.78040 4.9997210 -16.22183 9.441150
## 42 -3.693062 -12.08312 4.6969969 -16.52455 9.138426
## 43 -3.995786 -12.38584 4.3942729 -16.82727 8.835702
## 44 -4.298510 -12.68857 4.0915488 -17.13000 8.532978
## 45 -4.601234 -12.99129 3.7888248 -17.43272 8.230254
## 46 -4.903958 -13.29402 3.4861008 -17.73545 7.927530
## 47 -5.206682 -13.59674 3.1833767 -18.03817 7.624806
## 48 -5.509406 -13.89946 2.8806527 -18.34089 7.322082
## 49 -5.812130 -14.20219 2.5779286 -18.64362 7.019357
## 50 -6.114854 -14.50491 2.2752046 -18.94634 6.716633
## 51 -6.417578 -14.80764 1.9724806 -19.24907 6.413909
## 52 -6.720302 -15.11036 1.6697565 -19.55179 6.111185
## 53 -7.023026 -15.41309 1.3670325 -19.85451 5.808461
## 54 -7.325750 -15.71581 1.0643084 -20.15724 5.505737
## 55 -7.628474 -16.01853 0.7615844 -20.45996 5.203013