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(fpp2)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## ── Attaching packages ────────────────────────────────────────────── fpp2 2.5 ──
## ✔ ggplot2 3.4.1 ✔ fma 2.5
## ✔ forecast 8.20 ✔ expsmooth 2.3
##
library(TSA)
## Registered S3 methods overwritten by 'TSA':
## method from
## fitted.Arima forecast
## plot.Arima forecast
##
## Attaching package: 'TSA'
## The following objects are masked from 'package:stats':
##
## acf, arima
## The following object is masked from 'package:utils':
##
## tar
#Cargar datos
series<-autoplot(uschange)
autoplot(uschange[,2:3])

autoplot(uschange[,c(2,3)])

a <- VARselect(uschange[,2:3], lag.max=15,type="trend")
a$selection
## AIC(n) HQ(n) SC(n) FPE(n)
## 3 3 1 3
Los valores que se recomiendan para P son 2 y 3 en los cuales se van
a evaluar dos modelos
modelo1<-VAR(uschange[,2:3],p=1,type=c("trend"))
modelo3<-VAR(uschange[,2:3],p=3,type=c("trend"))
aic1<-summary(modelo1)$logLik
aic2<-summary(modelo3)$logLik
serial.test(modelo1, lags.pt=10, type="PT.asymptotic")
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object modelo1
## Chi-squared = 93.928, df = 36, p-value = 4.576e-07
serial.test(modelo3, lags.pt=10, type="PT.asymptotic")
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object modelo3
## Chi-squared = 42.829, df = 28, p-value = 0.03615
roots(modelo1)
## [1] 0.59312481 0.01858652
roots(modelo3)
## [1] 0.8150773 0.5561896 0.5561896 0.5199636 0.5199636 0.4162769
Todas las raices unitarias estan por 0.05
normality.test(modelo1, multivariate.only=FALSE)
## $Income
##
## JB-Test (univariate)
##
## data: Residual of Income equation
## Chi-squared = 410.85, df = 2, p-value < 2.2e-16
##
##
## $Production
##
## JB-Test (univariate)
##
## data: Residual of Production equation
## Chi-squared = 83.075, df = 2, p-value < 2.2e-16
##
##
## $JB
##
## JB-Test (multivariate)
##
## data: Residuals of VAR object modelo1
## Chi-squared = 475.68, df = 4, p-value < 2.2e-16
##
##
## $Skewness
##
## Skewness only (multivariate)
##
## data: Residuals of VAR object modelo1
## Chi-squared = 4.8279, df = 2, p-value = 0.08946
##
##
## $Kurtosis
##
## Kurtosis only (multivariate)
##
## data: Residuals of VAR object modelo1
## Chi-squared = 470.85, df = 2, p-value < 2.2e-16
normality.test(modelo3, multivariate.only=FALSE)
## $Income
##
## JB-Test (univariate)
##
## data: Residual of Income equation
## Chi-squared = 344.47, df = 2, p-value < 2.2e-16
##
##
## $Production
##
## JB-Test (univariate)
##
## data: Residual of Production equation
## Chi-squared = 47.534, df = 2, p-value = 4.766e-11
##
##
## $JB
##
## JB-Test (multivariate)
##
## data: Residuals of VAR object modelo3
## Chi-squared = 384.21, df = 4, p-value < 2.2e-16
##
##
## $Skewness
##
## Skewness only (multivariate)
##
## data: Residuals of VAR object modelo3
## Chi-squared = 10.651, df = 2, p-value = 0.004865
##
##
## $Kurtosis
##
## Kurtosis only (multivariate)
##
## data: Residuals of VAR object modelo3
## Chi-squared = 373.56, df = 2, p-value < 2.2e-16
arch<-arch.test(modelo1, lags.multi = 12, multivariate.only = TRUE)
arch<-arch.test(modelo3, lags.multi = 12, multivariate.only = TRUE)
arch
##
## ARCH (multivariate)
##
## data: Residuals of VAR object modelo3
## Chi-squared = 122.36, df = 108, p-value = 0.1631
stab<-stability(modelo3, type = "OLS-CUSUM")
plot(stab)

plot(modelo3, names="Income")
