This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(vars)
## Warning: package 'vars' was built under R version 4.2.2
## Loading required package: MASS
## Loading required package: strucchange
## Warning: package 'strucchange' was built under R version 4.2.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.2.2
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: sandwich
## Warning: package 'sandwich' was built under R version 4.2.2
## Loading required package: urca
## Warning: package 'urca' was built under R version 4.2.2
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 4.2.2
library(fpp2)
## Warning: package 'fpp2' was built under R version 4.2.2
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## ── Attaching packages ────────────────────────────────────────────── fpp2 2.5 ──
## ✔ ggplot2 3.3.6 ✔ fma 2.5
## ✔ forecast 8.20 ✔ expsmooth 2.3
## Warning: package 'forecast' was built under R version 4.2.2
## Warning: package 'fma' was built under R version 4.2.2
## Warning: package 'expsmooth' was built under R version 4.2.2
##
#Cargar datos
series<-uschange
autoplot(uschange[,1:2])
#plot de serie de datos
ts.plot(series[,1:2], xlab="Tiempo",col=c(1,2))
#Búsqueda de parámetros
a <- VARselect(uschange[,1:2], lag.max=15,type="const")
a$selection
## AIC(n) HQ(n) SC(n) FPE(n)
## 5 1 1 5
#Creación de modelo
modelo1<-VAR(uschange[,1:2],p=1,type=c("const"))
modelo5<-VAR(uschange[,1:2],p=5,type=c("const"))
aic1<-summary(modelo1)$logLik
aic2<-summary(modelo5)$logLik
summary(modelo1,equation="Consumption")
##
## VAR Estimation Results:
## =========================
## Endogenous variables: Consumption, Income
## Deterministic variables: const
## Sample size: 186
## Log Likelihood: -394.933
## Roots of the characteristic polynomial:
## 0.3738 0.3321
## Call:
## VAR(y = uschange[, 1:2], p = 1, type = c("const"))
##
##
## Estimation results for equation Consumption:
## ============================================
## Consumption = Consumption.l1 + Income.l1 + const
##
## Estimate Std. Error t value Pr(>|t|)
## Consumption.l1 0.29646 0.07485 3.961 0.000107 ***
## Income.l1 0.09434 0.05267 1.791 0.074908 .
## const 0.45811 0.06993 6.551 5.63e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.6135 on 183 degrees of freedom
## Multiple R-Squared: 0.1376, Adjusted R-squared: 0.1282
## F-statistic: 14.6 on 2 and 183 DF, p-value: 1.311e-06
##
##
##
## Covariance matrix of residuals:
## Consumption Income
## Consumption 0.3764 0.2088
## Income 0.2088 0.7732
##
## Correlation matrix of residuals:
## Consumption Income
## Consumption 1.000 0.387
## Income 0.387 1.000
summary(modelo1,equation="Income")
##
## VAR Estimation Results:
## =========================
## Endogenous variables: Consumption, Income
## Deterministic variables: const
## Sample size: 186
## Log Likelihood: -394.933
## Roots of the characteristic polynomial:
## 0.3738 0.3321
## Call:
## VAR(y = uschange[, 1:2], p = 1, type = c("const"))
##
##
## Estimation results for equation Income:
## =======================================
## Income = Consumption.l1 + Income.l1 + const
##
## Estimate Std. Error t value Pr(>|t|)
## Consumption.l1 0.51509 0.10728 4.801 3.27e-06 ***
## Income.l1 -0.25474 0.07549 -3.375 0.000903 ***
## const 0.51462 0.10024 5.134 7.21e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.8793 on 183 degrees of freedom
## Multiple R-Squared: 0.1227, Adjusted R-squared: 0.1131
## F-statistic: 12.79 on 2 and 183 DF, p-value: 6.301e-06
##
##
##
## Covariance matrix of residuals:
## Consumption Income
## Consumption 0.3764 0.2088
## Income 0.2088 0.7732
##
## Correlation matrix of residuals:
## Consumption Income
## Consumption 1.000 0.387
## Income 0.387 1.000
#Validación del modelo
#>PortManteu Test > 0.05 Autocorrelación
serial.test(modelo1, lags.pt=10, type="PT.asymptotic")
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object modelo1
## Chi-squared = 49.102, df = 36, p-value = 0.07144
#RaÃz unitaria > 0.05
roots(modelo1)
## [1] 0.3737744 0.3320591
#normalidad Jarque Bera < 0.05
normality.test(modelo1, multivariate.only=FALSE)
## $Consumption
##
## JB-Test (univariate)
##
## data: Residual of Consumption equation
## Chi-squared = 47.401, df = 2, p-value = 5.094e-11
##
##
## $Income
##
## JB-Test (univariate)
##
## data: Residual of Income equation
## Chi-squared = 141.19, df = 2, p-value < 2.2e-16
##
##
## $JB
##
## JB-Test (multivariate)
##
## data: Residuals of VAR object modelo1
## Chi-squared = 178.11, df = 4, p-value < 2.2e-16
##
##
## $Skewness
##
## Skewness only (multivariate)
##
## data: Residuals of VAR object modelo1
## Chi-squared = 11.44, df = 2, p-value = 0.00328
##
##
## $Kurtosis
##
## Kurtosis only (multivariate)
##
## data: Residuals of VAR object modelo1
## Chi-squared = 166.67, df = 2, p-value < 2.2e-16
#heteroscedasticity >0.05 NO HAY
arch<-arch.test(modelo1, lags.multi = 12, multivariate.only = TRUE)
arch
##
## ARCH (multivariate)
##
## data: Residuals of VAR object modelo1
## Chi-squared = 117.17, df = 108, p-value = 0.2571
#Structural breaks
stab<-stability(modelo1, type = "OLS-CUSUM")
plot(stab)
#Prueba gráfica
plot(modelo1, names="Income")
dev.off()
## null device
## 1
par(mar=c(1,1,1,1))
acf(residuals(modelo1)[,1])
pacf(residuals(modelo1)[,1])
#Formula
modelo1$varresult$Income$coefficients
## Consumption.l1 Income.l1 const
## 0.5150933 -0.2547414 0.5146181
modelo1$varresult$Consumption$coefficients
## Consumption.l1 Income.l1 const
## 0.29645664 0.09434292 0.45810920
autoplot(forecast(modelo1))
#comparación con otros modelos
modelo2<-VAR(uschange[,1:2],p=2,type=c("const"))
modelo3<-VAR(uschange[,1:2],p=3,type=c("const"))
aic1<-summary(modelo1)$logLik
aic2<-summary(modelo2)$logLik
aic3<-summary(modelo3)$logLik
autoplot(uschange)
autoplot(prison[,c(3,10)])
autoplot(melsyd)
autoplot(insurance)
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.