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
series<-uschange
autoplot(uschange[,1:3])

ts.plot(series[,1:3], xlab="Tiempo",col=c(1,3))

a <- VARselect(uschange[,1:3], lag.max=15,type="const")
a$selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      3      1      1      3
modelo1<-VAR(uschange[,1:3],p=1,type=c("const"))

GrangerIncome <-causality(modelo1, cause = 'Income')
GrangerIncome
## $Granger
## 
##  Granger causality H0: Income do not Granger-cause Consumption
##  Production
## 
## data:  VAR object modelo1
## F-Test = 2.7691, df1 = 2, df2 = 546, p-value = 0.0636
## 
## 
## $Instant
## 
##  H0: No instantaneous causality between: Income and Consumption
##  Production
## 
## data:  VAR object modelo1
## Chi-squared = 24.678, df = 2, p-value = 4.377e-06
GrangerConsumptions <-causality(modelo1, cause = 'Consumption')
GrangerConsumptions
## $Granger
## 
##  Granger causality H0: Consumption do not Granger-cause Income
##  Production
## 
## data:  VAR object modelo1
## F-Test = 19.441, df1 = 2, df2 = 546, p-value = 6.982e-09
## 
## 
## $Instant
## 
##  H0: No instantaneous causality between: Consumption and Income
##  Production
## 
## data:  VAR object modelo1
## Chi-squared = 46.671, df = 2, p-value = 7.336e-11
IncomeIRF <- irf(modelo1,  impulse = "Consumption", response="Income", n.ahead = 20, boot = T )
plot(IncomeIRF, ylab = "Income", main = "Shock desde Consumptions")

ConsumptionIRF <- irf(modelo1,  impulse = "Income", response="Consumption", n.ahead = 20, boot = T )
plot(ConsumptionIRF, ylab = "Consumption", main = "Shock desde Income")

El P-valor de las variables son menores a 0.05 y eso hace que sean buenas para el modelo.

serial.test(modelo1, lags.pt=10, type="PT.asymptotic")
## 
##  Portmanteau Test (asymptotic)
## 
## data:  Residuals of VAR object modelo1
## Chi-squared = 123.64, df = 81, p-value = 0.001611

Se utiliza la prueba PortManteu y se mira que el modelo no tiene autocorrelacion ya que el P-valor es menor a 0.05.

roots(modelo1)
## [1] 0.4179594 0.4179594 0.3474463
normality.test(modelo1, multivariate.only=FALSE)
## $Consumption
## 
##  JB-Test (univariate)
## 
## data:  Residual of Consumption equation
## Chi-squared = 46.55, df = 2, p-value = 7.794e-11
## 
## 
## $Income
## 
##  JB-Test (univariate)
## 
## data:  Residual of Income equation
## Chi-squared = 107.92, df = 2, p-value < 2.2e-16
## 
## 
## $Production
## 
##  JB-Test (univariate)
## 
## data:  Residual of Production equation
## Chi-squared = 54.543, df = 2, p-value = 1.432e-12
## 
## 
## $JB
## 
##  JB-Test (multivariate)
## 
## data:  Residuals of VAR object modelo1
## Chi-squared = 206.76, df = 6, p-value < 2.2e-16
## 
## 
## $Skewness
## 
##  Skewness only (multivariate)
## 
## data:  Residuals of VAR object modelo1
## Chi-squared = 14.611, df = 3, p-value = 0.002181
## 
## 
## $Kurtosis
## 
##  Kurtosis only (multivariate)
## 
## data:  Residuals of VAR object modelo1
## Chi-squared = 192.15, df = 3, p-value < 2.2e-16

Las raices unitarias tienen un valor menor a 1.

stab<-stability(modelo1, type = "OLS-CUSUM")
par(mar=c(1,1,1,1))
plot(stab)

Debido a su P-valor es menor a 0.05 pasa la prueba de Jarque Bera

autoplot(forecast(modelo1))