R Markdown
library(vars)
## Warning: package 'vars' was built under R version 4.1.3
## 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)
## Warning in register(): Can't find generic `scale_type` in package ggplot2 to
## register S3 method.
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## -- Attaching packages ---------------------------------------------- fpp2 2.4 --
## v ggplot2 3.3.5 v fma 2.4
## v forecast 8.16 v expsmooth 2.3
##
#Cargar datos
series<-insurance
?insurance
## starting httpd help server ...
## done
autoplot(insurance[,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(diff(insurance[,1:2]), lag.max=15,type="const")
a$selection
## AIC(n) HQ(n) SC(n) FPE(n)
## 12 12 12 12
#Creación de modelo
modelo1<-VAR(diff(insurance[,1:2]),p=12,type=c("const"))
modelo1
##
## VAR Estimation Results:
## =======================
##
## Estimated coefficients for equation Quotes:
## ===========================================
## Call:
## Quotes = Quotes.l1 + TV.advert.l1 + Quotes.l2 + TV.advert.l2 + Quotes.l3 + TV.advert.l3 + Quotes.l4 + TV.advert.l4 + Quotes.l5 + TV.advert.l5 + Quotes.l6 + TV.advert.l6 + Quotes.l7 + TV.advert.l7 + Quotes.l8 + TV.advert.l8 + Quotes.l9 + TV.advert.l9 + Quotes.l10 + TV.advert.l10 + Quotes.l11 + TV.advert.l11 + Quotes.l12 + TV.advert.l12 + const
##
## Quotes.l1 TV.advert.l1 Quotes.l2 TV.advert.l2 Quotes.l3
## 0.7896773 -2.5214016 0.5406640 -2.3149573 0.1723104
## TV.advert.l3 Quotes.l4 TV.advert.l4 Quotes.l5 TV.advert.l5
## -1.5301008 0.8330785 -3.1631676 1.0849995 -3.5226300
## Quotes.l6 TV.advert.l6 Quotes.l7 TV.advert.l7 Quotes.l8
## 2.5371896 -4.8354452 0.9273798 -2.8193954 1.4692797
## TV.advert.l8 Quotes.l9 TV.advert.l9 Quotes.l10 TV.advert.l10
## -3.2986845 1.1339034 -2.7431205 1.6144396 -3.2853883
## Quotes.l11 TV.advert.l11 Quotes.l12 TV.advert.l12 const
## 0.5557078 -2.2971848 0.1728017 -1.0431363 0.6725315
##
##
## Estimated coefficients for equation TV.advert:
## ==============================================
## Call:
## TV.advert = Quotes.l1 + TV.advert.l1 + Quotes.l2 + TV.advert.l2 + Quotes.l3 + TV.advert.l3 + Quotes.l4 + TV.advert.l4 + Quotes.l5 + TV.advert.l5 + Quotes.l6 + TV.advert.l6 + Quotes.l7 + TV.advert.l7 + Quotes.l8 + TV.advert.l8 + Quotes.l9 + TV.advert.l9 + Quotes.l10 + TV.advert.l10 + Quotes.l11 + TV.advert.l11 + Quotes.l12 + TV.advert.l12 + const
##
## Quotes.l1 TV.advert.l1 Quotes.l2 TV.advert.l2 Quotes.l3
## 0.43497311 -1.76938035 0.44213273 -1.48097472 0.13575295
## TV.advert.l3 Quotes.l4 TV.advert.l4 Quotes.l5 TV.advert.l5
## -1.11478715 0.29499228 -1.65427233 0.87634979 -2.32901278
## Quotes.l6 TV.advert.l6 Quotes.l7 TV.advert.l7 Quotes.l8
## 1.42349233 -2.80595422 0.46798393 -1.61050050 1.00262829
## TV.advert.l8 Quotes.l9 TV.advert.l9 Quotes.l10 TV.advert.l10
## -2.00541202 0.66301969 -1.37659738 0.46043413 -1.52875526
## Quotes.l11 TV.advert.l11 Quotes.l12 TV.advert.l12 const
## 0.86141929 -2.16491707 0.02726438 -0.73576888 0.41834879
aic1<-summary(modelo1)$logLik
summary(modelo1,equation="Quotes")
##
## VAR Estimation Results:
## =========================
## Endogenous variables: Quotes, TV.advert
## Deterministic variables: const
## Sample size: 27
## Log Likelihood: 16.441
## Roots of the characteristic polynomial:
## 1.085 1.035 1.035 1.03 1.03 1.017 1.017 1.014 1.014 0.9969 0.9969 0.9734 0.9734 0.9657 0.9657 0.9603 0.9603 0.9592 0.9592 0.9534 0.815 0.815 0.3994 0.3994
## Call:
## VAR(y = diff(insurance[, 1:2]), p = 12, type = c("const"))
##
##
## Estimation results for equation Quotes:
## =======================================
## Quotes = Quotes.l1 + TV.advert.l1 + Quotes.l2 + TV.advert.l2 + Quotes.l3 + TV.advert.l3 + Quotes.l4 + TV.advert.l4 + Quotes.l5 + TV.advert.l5 + Quotes.l6 + TV.advert.l6 + Quotes.l7 + TV.advert.l7 + Quotes.l8 + TV.advert.l8 + Quotes.l9 + TV.advert.l9 + Quotes.l10 + TV.advert.l10 + Quotes.l11 + TV.advert.l11 + Quotes.l12 + TV.advert.l12 + const
##
## Estimate Std. Error t value Pr(>|t|)
## Quotes.l1 0.7897 0.8312 0.950 0.442
## TV.advert.l1 -2.5214 1.4204 -1.775 0.218
## Quotes.l2 0.5407 0.7598 0.712 0.551
## TV.advert.l2 -2.3150 1.0900 -2.124 0.168
## Quotes.l3 0.1723 0.7605 0.227 0.842
## TV.advert.l3 -1.5301 1.3050 -1.172 0.362
## Quotes.l4 0.8331 0.8905 0.935 0.448
## TV.advert.l4 -3.1632 1.3597 -2.326 0.145
## Quotes.l5 1.0850 0.8722 1.244 0.340
## TV.advert.l5 -3.5226 1.5623 -2.255 0.153
## Quotes.l6 2.5372 0.8814 2.878 0.102
## TV.advert.l6 -4.8354 1.7220 -2.808 0.107
## Quotes.l7 0.9274 1.0057 0.922 0.454
## TV.advert.l7 -2.8194 1.9645 -1.435 0.288
## Quotes.l8 1.4693 0.9058 1.622 0.246
## TV.advert.l8 -3.2987 1.7116 -1.927 0.194
## Quotes.l9 1.1339 1.1149 1.017 0.416
## TV.advert.l9 -2.7431 1.7438 -1.573 0.256
## Quotes.l10 1.6144 1.4435 1.118 0.380
## TV.advert.l10 -3.2854 2.0850 -1.576 0.256
## Quotes.l11 0.5557 1.3935 0.399 0.729
## TV.advert.l11 -2.2972 2.1682 -1.059 0.400
## Quotes.l12 0.1728 0.8035 0.215 0.850
## TV.advert.l12 -1.0431 1.2216 -0.854 0.483
## const 0.6725 0.4110 1.636 0.243
##
##
## Residual standard error: 0.9767 on 2 degrees of freedom
## Multiple R-Squared: 0.9811, Adjusted R-squared: 0.7544
## F-statistic: 4.328 on 24 and 2 DF, p-value: 0.2046
##
##
##
## Covariance matrix of residuals:
## Quotes TV.advert
## Quotes 0.9539 0.2107
## TV.advert 0.2107 0.2403
##
## Correlation matrix of residuals:
## Quotes TV.advert
## Quotes 1.00 0.44
## TV.advert 0.44 1.00
summary(modelo1,equation="TV.advert")
##
## VAR Estimation Results:
## =========================
## Endogenous variables: Quotes, TV.advert
## Deterministic variables: const
## Sample size: 27
## Log Likelihood: 16.441
## Roots of the characteristic polynomial:
## 1.085 1.035 1.035 1.03 1.03 1.017 1.017 1.014 1.014 0.9969 0.9969 0.9734 0.9734 0.9657 0.9657 0.9603 0.9603 0.9592 0.9592 0.9534 0.815 0.815 0.3994 0.3994
## Call:
## VAR(y = diff(insurance[, 1:2]), p = 12, type = c("const"))
##
##
## Estimation results for equation TV.advert:
## ==========================================
## TV.advert = Quotes.l1 + TV.advert.l1 + Quotes.l2 + TV.advert.l2 + Quotes.l3 + TV.advert.l3 + Quotes.l4 + TV.advert.l4 + Quotes.l5 + TV.advert.l5 + Quotes.l6 + TV.advert.l6 + Quotes.l7 + TV.advert.l7 + Quotes.l8 + TV.advert.l8 + Quotes.l9 + TV.advert.l9 + Quotes.l10 + TV.advert.l10 + Quotes.l11 + TV.advert.l11 + Quotes.l12 + TV.advert.l12 + const
##
## Estimate Std. Error t value Pr(>|t|)
## Quotes.l1 0.43497 0.41716 1.043 0.4066
## TV.advert.l1 -1.76938 0.71289 -2.482 0.1311
## Quotes.l2 0.44213 0.38134 1.159 0.3660
## TV.advert.l2 -1.48097 0.54705 -2.707 0.1137
## Quotes.l3 0.13575 0.38168 0.356 0.7561
## TV.advert.l3 -1.11479 0.65498 -1.702 0.2309
## Quotes.l4 0.29499 0.44696 0.660 0.5771
## TV.advert.l4 -1.65427 0.68242 -2.424 0.1362
## Quotes.l5 0.87635 0.43777 2.002 0.1833
## TV.advert.l5 -2.32901 0.78411 -2.970 0.0971 .
## Quotes.l6 1.42349 0.44239 3.218 0.0845 .
## TV.advert.l6 -2.80595 0.86426 -3.247 0.0832 .
## Quotes.l7 0.46798 0.50474 0.927 0.4517
## TV.advert.l7 -1.61050 0.98597 -1.633 0.2440
## Quotes.l8 1.00263 0.45460 2.206 0.1582
## TV.advert.l8 -2.00541 0.85908 -2.334 0.1447
## Quotes.l9 0.66302 0.55959 1.185 0.3578
## TV.advert.l9 -1.37660 0.87522 -1.573 0.2564
## Quotes.l10 0.46043 0.72447 0.636 0.5901
## TV.advert.l10 -1.52876 1.04648 -1.461 0.2815
## Quotes.l11 0.86142 0.69938 1.232 0.3432
## TV.advert.l11 -2.16492 1.08821 -1.989 0.1849
## Quotes.l12 0.02726 0.40328 0.068 0.9522
## TV.advert.l12 -0.73577 0.61313 -1.200 0.3530
## const 0.41835 0.20627 2.028 0.1797
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.4902 on 2 degrees of freedom
## Multiple R-Squared: 0.9895, Adjusted R-squared: 0.8641
## F-statistic: 7.888 on 24 and 2 DF, p-value: 0.1185
##
##
##
## Covariance matrix of residuals:
## Quotes TV.advert
## Quotes 0.9539 0.2107
## TV.advert 0.2107 0.2403
##
## Correlation matrix of residuals:
## Quotes TV.advert
## Quotes 1.00 0.44
## TV.advert 0.44 1.00
#Validación del modelo
#>PortManteu Test > 0.05 Autocorrelación
serial.test(modelo1, lags.pt=10, type="PT.asymptotic")
## Warning in pchisq(STATISTIC, df = PARAMETER): NaNs produced
## Warning in pchisq(STATISTIC, df = PARAMETER): NaNs produced
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object modelo1
## Chi-squared = 79.311, df = -8, p-value = NA
#RaÃz unitaria > 0.05
roots(modelo1)
## [1] 1.0853804 1.0353787 1.0353787 1.0295196 1.0295196 1.0165798 1.0165798
## [8] 1.0143173 1.0143173 0.9969243 0.9969243 0.9734470 0.9734470 0.9656508
## [15] 0.9656508 0.9603069 0.9603069 0.9592251 0.9592251 0.9533892 0.8150357
## [22] 0.8150357 0.3993789 0.3993789
#normalidad Jarque Bera < 0.05
normality.test(modelo1, multivariate.only=FALSE)
## $Quotes
##
## JB-Test (univariate)
##
## data: Residual of Quotes equation
## Chi-squared = 0.50056, df = 2, p-value = 0.7786
##
##
## $TV.advert
##
## JB-Test (univariate)
##
## data: Residual of TV.advert equation
## Chi-squared = 0.91214, df = 2, p-value = 0.6338
##
##
## $JB
##
## JB-Test (multivariate)
##
## data: Residuals of VAR object modelo1
## Chi-squared = 1.4286, df = 4, p-value = 0.8392
##
##
## $Skewness
##
## Skewness only (multivariate)
##
## data: Residuals of VAR object modelo1
## Chi-squared = 0.28431, df = 2, p-value = 0.8675
##
##
## $Kurtosis
##
## Kurtosis only (multivariate)
##
## data: Residuals of VAR object modelo1
## Chi-squared = 1.1443, df = 2, p-value = 0.5643
#Prueba gráfica
plot(modelo1, names="Income")
## Warning in plot.varest(modelo1, names = "Income"):
## Invalid variable name(s) supplied, using first variable.

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
## NULL
modelo1$varresult$Consumption$coefficients
## NULL
autoplot(forecast(modelo1))