library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.6 v purrr 0.3.4
## v tibble 3.1.8 v dplyr 1.0.7
## v tidyr 1.1.4 v stringr 1.4.0
## v readr 2.0.2 v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.1.3
## Warning: package 'tibble' was built under R version 4.1.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x lubridate::as.difftime() masks base::as.difftime()
## x lubridate::date() masks base::date()
## x dplyr::filter() masks stats::filter()
## x lubridate::intersect() masks base::intersect()
## x dplyr::lag() masks stats::lag()
## x lubridate::setdiff() masks base::setdiff()
## x lubridate::union() masks base::union()
library(tseries)
## Warning: package 'tseries' was built under R version 4.1.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(forecast)
## Warning: package 'forecast' was built under R version 4.1.3
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(quantmod)
## Warning: package 'quantmod' was built under R version 4.1.3
## Loading required package: xts
##
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
##
## first, last
## Loading required package: TTR
library(rugarch)
## Warning: package 'rugarch' was built under R version 4.1.3
## Loading required package: parallel
##
## Attaching package: 'rugarch'
## The following object is masked from 'package:purrr':
##
## reduce
## The following object is masked from 'package:stats':
##
## sigma
library(fGarch)
## Warning: package 'fGarch' was built under R version 4.1.3
## NOTE: Packages 'fBasics', 'timeDate', and 'timeSeries' are no longer
## attached to the search() path when 'fGarch' is attached.
##
## If needed attach them yourself in your R script by e.g.,
## require("timeSeries")
##
## Attaching package: 'fGarch'
## The following object is masked from 'package:TTR':
##
## volatility
library(timsac)
library(FinTS)
## Warning: package 'FinTS' was built under R version 4.1.3
##
## Attaching package: 'FinTS'
## The following object is masked from 'package:forecast':
##
## Acf
library(dynlm)
## Warning: package 'dynlm' was built under R version 4.1.3
library(tidyr)
library(dplyr)
library(readxl)
Data_base_research <- read_excel("C:/Users/rchang/Downloads/Data_base_research.xlsx")
head(Data_base_research)
inciso1 <- ts(Data_base_research$CO2_Emission_Percapita, start = c(1966, 1), end = c(2013, 1), frequency = 1 )
plot(inciso1)
acf(inciso1)
pacf(inciso1)
adf.test(inciso1)
## Warning in adf.test(inciso1): p-value greater than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: inciso1
## Dickey-Fuller = -0.10263, Lag order = 3, p-value = 0.99
## alternative hypothesis: stationary
La serie de emisiones de CO2 percapita de Honduras No es estacionaria.
modelosarima1=auto.arima(inciso1,stepwise = FALSE,approximation = FALSE)
modelosarima1
## Series: inciso1
## ARIMA(1,1,0) with drift
##
## Coefficients:
## ar1 drift
## -0.2691 0.0185
## s.e. 0.1682 0.0063
##
## sigma^2 = 0.003131: log likelihood = 69.8
## AIC=-133.6 AICc=-133.05 BIC=-128.05
El mejor modelo es un autoregresivo 0, integrado 1 y media móvil 1.
residuossarima1=residuals(modelosarima1)
plot(residuossarima1)
Box.test(residuossarima1,type="Ljung-Box")
##
## Box-Ljung test
##
## data: residuossarima1
## X-squared = 0.0003934, df = 1, p-value = 0.9842
LA hipótesis nula no se rechaza y nos muestra ausencia de autocorrelación. EN otras palabras, los residuos se distribuyen independientemente.
Proyección a 10 datos
forecast_data <- forecast(modelosarima1, 10, level=c(80,95) )
print(forecast_data)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 2014 1.191553 1.119839 1.263267 1.081876 1.301230
## 2015 1.225350 1.136524 1.314177 1.089502 1.361199
## 2016 1.239705 1.133833 1.345577 1.077788 1.401622
## 2017 1.259292 1.139424 1.379161 1.075969 1.442616
## 2018 1.277471 1.144918 1.410025 1.074748 1.480195
## 2019 1.296030 1.151942 1.440117 1.075667 1.516392
## 2020 1.314486 1.159712 1.469259 1.077780 1.551191
## 2021 1.332969 1.168203 1.497735 1.080982 1.584956
## 2022 1.351445 1.177259 1.525631 1.085051 1.617840
## 2023 1.369923 1.186801 1.553046 1.089862 1.649985
plot(forecast_data)
dataframe<-data.frame(forecast_data)
chartSeries(inciso1)
model1=auto.arima(inciso1,stepwise = FALSE,approximation = FALSE)
summary(model1)
## Series: inciso1
## ARIMA(1,1,0) with drift
##
## Coefficients:
## ar1 drift
## -0.2691 0.0185
## s.e. 0.1682 0.0063
##
## sigma^2 = 0.003131: log likelihood = 69.8
## AIC=-133.6 AICc=-133.05 BIC=-128.05
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.0001588804 0.05418177 0.04110976 -1.250726 7.132555 0.9379411
## ACF1
## Training set -0.002775629
Claramente observamos que estamos ante un modelo ARIMA (0,1,1).
Revisar si hay efectos ARCH El primer paso es conocer el orden de nuestro modelo ARMA, de esta forma crearemos nuestro mejor ARIMA y checar el orden de nuestro modelo.
checkresiduals(model1)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(1,1,0) with drift
## Q* = 5.0716, df = 8, p-value = 0.7499
##
## Model df: 2. Total lags used: 10
residuosmodel1=residuals(model1)
chartSeries(residuosmodel1)
acf(residuosmodel1)
pacf(residuosmodel1)
La serie no es estacionaria, lo cual nos indica el test de Dickey-Fuller
adf.test(residuosmodel1)
##
## Augmented Dickey-Fuller Test
##
## data: residuosmodel1
## Dickey-Fuller = -2.6737, Lag order = 3, p-value = 0.3051
## alternative hypothesis: stationary
Observamos que nuestra varianza no es constante, ya que los errores al cuadrado muestran que al pasar los días la varianza llega a ser heterocedastica, por lo que ahora haremos nuestra regresión. Esto para observar si nos enfrentamos a un modelo ARCH o no.
residuosmodel1cuad=residuosmodel1*residuosmodel1
chartSeries(residuosmodel1cuad)
acf(residuosmodel1cuad)
pacf(residuosmodel1cuad)
adf.test(residuosmodel1cuad)
##
## Augmented Dickey-Fuller Test
##
## data: residuosmodel1cuad
## Dickey-Fuller = -0.30417, Lag order = 3, p-value = 0.9867
## alternative hypothesis: stationary
No es estacionaria la serie de tiempo con los residuos al cuadrado.
La siguiente prueba nos indica si hay efectos ARCH, por lo que corraboramos de que no hay efectos ARCH.
#model1reg=dynlm(residuosmodel1cuad~L(residuosmodel1cuad),data=retornosdiaWMT)
#summary(model1reg)