DATOS USCHANGE

#CARGAR DATOS
series<-uschange

?uschange

#OBTENER LA SERIE
autoplot(uschange)

autoplot(uschange[,1:2])

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

a <- VARselect(uschange[,1:2], lag.max=12,type="const")
a
## $selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      5      1      1      5 
## 
## $criteria
##                 1          2          3          4         5          6
## AIC(n) -1.4387301 -1.4266106 -1.4581505 -1.4688220 -1.484526 -1.4626972
## HQ(n)  -1.3947166 -1.3532547 -1.3554523 -1.3367815 -1.323143 -1.2719720
## SC(n)  -1.3302232 -1.2457657 -1.2049676 -1.1433012 -1.086667 -0.9925004
## FPE(n)  0.2372304  0.2401289  0.2326861  0.2302383  0.226685  0.2317378
##                 7          8          9         10         11         12
## AIC(n) -1.4475670 -1.4291678 -1.3889936 -1.3510597 -1.3180043 -1.2978215
## HQ(n)  -1.2274995 -1.1797580 -1.1102414 -1.0429651 -0.9805674 -0.9310422
## SC(n)  -0.9050323 -0.8142951 -0.7017830 -0.5915111 -0.4861177 -0.3935969
## FPE(n)  0.2353401  0.2398027  0.2497549  0.2595683  0.2684889  0.2742032
a$selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      5      1      1      5
#CREACION DEL MODELO
modelo1<-VAR(uschange[,1:2],p=1,type=c("const"))
modelo2<-VAR(uschange[,1:2],p=5,type=c("const"))
modelo1
## 
## VAR Estimation Results:
## ======================= 
## 
## Estimated coefficients for equation Consumption: 
## ================================================ 
## Call:
## Consumption = Consumption.l1 + Income.l1 + const 
## 
## Consumption.l1      Income.l1          const 
##     0.29645664     0.09434292     0.45810920 
## 
## 
## Estimated coefficients for equation Income: 
## =========================================== 
## Call:
## Income = Consumption.l1 + Income.l1 + const 
## 
## Consumption.l1      Income.l1          const 
##      0.5150933     -0.2547414      0.5146181
modelo2
## 
## VAR Estimation Results:
## ======================= 
## 
## Estimated coefficients for equation Consumption: 
## ================================================ 
## Call:
## Consumption = Consumption.l1 + Income.l1 + Consumption.l2 + Income.l2 + Consumption.l3 + Income.l3 + Consumption.l4 + Income.l4 + Consumption.l5 + Income.l5 + const 
## 
## Consumption.l1      Income.l1 Consumption.l2      Income.l2 Consumption.l3 
##     0.21855796     0.09445354     0.17212699    -0.03278838     0.28849780 
##      Income.l3 Consumption.l4      Income.l4 Consumption.l5      Income.l5 
##    -0.03818433    -0.02828834    -0.07767663    -0.03860699    -0.04324357 
##          const 
##     0.35791445 
## 
## 
## Estimated coefficients for equation Income: 
## =========================================== 
## Call:
## Income = Consumption.l1 + Income.l1 + Consumption.l2 + Income.l2 + Consumption.l3 + Income.l3 + Consumption.l4 + Income.l4 + Consumption.l5 + Income.l5 + const 
## 
## Consumption.l1      Income.l1 Consumption.l2      Income.l2 Consumption.l3 
##     0.43750033    -0.29981380     0.04889127    -0.10080974     0.45234276 
##      Income.l3 Consumption.l4      Income.l4 Consumption.l5      Income.l5 
##    -0.14603080     0.30345938    -0.24189049    -0.07572447    -0.19026612 
##          const 
##     0.53252727
aic1<-summary(modelo1)$logLik
aic1
## [1] -394.9332
aic2<-summary(modelo2)$logLik
aic2
## [1] -362.4965
#VALIDACION DEL MODELO
serial.test(modelo1, lags.pt=12, type="PT.asymptotic")
## 
##  Portmanteau Test (asymptotic)
## 
## data:  Residuals of VAR object modelo1
## Chi-squared = 52.272, df = 44, p-value = 0.1835
roots(modelo1)
## [1] 0.3737744 0.3320591
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
plot(modelo1, names="Income")

dev.off()
## null device 
##           1
par(mar=c(1,1,1,1))
acf(residuals(modelo1)[,1])
pacf(residuals(modelo1)[,1])
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))

DATOS MELSYD

#CARGAR DATOS
series<-melsyd


#OBTENER LA SERIE
autoplot(melsyd)

autoplot(melsyd[,1:3])

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

#HAY MUCHOS DATOS VACIOS POR LO QUE NO SE PUEDE REALIZAR

DATOS INSURANCE

#CARGAR DATOS
series<-insurance

#OBTENER LA SERIEç
autoplot(insurance)

autoplot(insurance[,1:2])

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

a <- VARselect(insurance[,1:2], lag.max=12,type="const")
a
## $selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##     12     12      2      2 
## 
## $criteria
##                 1          2          3          4          5          6
## AIC(n) -0.9415300 -1.2875411 -1.1802611 -1.0288969 -0.7804458 -0.5805128
## HQ(n)  -0.8542583 -1.1420882 -0.9766270 -0.7670816 -0.4604494 -0.2023352
## SC(n)  -0.6560576 -0.8117538 -0.5141588 -0.1724797  0.2662863  0.6565343
## FPE(n)  0.3906752  0.2780929  0.3139223  0.3746911  0.5009857  0.6527168
##                  7           8          9         10         11         12
## AIC(n) -0.34407854 -0.43848191 -0.9023228 -1.3854606 -2.5780266 -2.7452042
## HQ(n)   0.09228024  0.05605805 -0.3496017 -0.7745583 -1.9089431 -2.0179396
## SC(n)   1.08328343  1.17919500  0.9056690  0.6128462 -0.3894049 -0.3662676
## FPE(n)  0.90988763  0.95168717  0.7328879  0.6104003  0.2955358  0.5636772
a$selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##     12     12      2      2
#CREACION DEL MODELO
modelo1<-VAR(insurance[,1:2],p=12,type=c("const"))
modelo2<-VAR(insurance[,1:2],p=2,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 
##     1.2470658    -2.4530452     1.0667824    -2.6162468    -0.3688445 
##  TV.advert.l3     Quotes.l4  TV.advert.l4     Quotes.l5  TV.advert.l5 
##    -0.2609116     0.5154976    -1.8976661     0.3596082    -1.3184915 
##     Quotes.l6  TV.advert.l6     Quotes.l7  TV.advert.l7     Quotes.l8 
##     1.5935403    -2.1877329    -0.6528141     0.5197491     0.6483490 
##  TV.advert.l8     Quotes.l9  TV.advert.l9    Quotes.l10 TV.advert.l10 
##    -0.8583710    -0.2206740     0.0362382     0.7593211    -0.7383296 
##    Quotes.l11 TV.advert.l11    Quotes.l12 TV.advert.l12         const 
##    -1.2756525     1.1328161    -0.7724121     0.9547982    54.2172211 
## 
## 
## 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.2057646    -0.6306077     0.9308638    -1.7131892    -0.5956195 
##  TV.advert.l3     Quotes.l4  TV.advert.l4     Quotes.l5  TV.advert.l5 
##     0.2971025     0.4408534    -1.1668884     0.2126698    -0.6784697 
##     Quotes.l6  TV.advert.l6     Quotes.l7  TV.advert.l7     Quotes.l8 
##     0.8856756    -1.1119076    -0.6958322     0.5984264     0.7444307 
##  TV.advert.l8     Quotes.l9  TV.advert.l9    Quotes.l10 TV.advert.l10 
##    -0.6506325    -0.5900783     0.5505313     0.6171403    -0.7599420 
##    Quotes.l11 TV.advert.l11    Quotes.l12 TV.advert.l12         const 
##    -0.6373814     0.4255538    -0.3550255     0.5535360    27.9166420
modelo2
## 
## VAR Estimation Results:
## ======================= 
## 
## Estimated coefficients for equation Quotes: 
## =========================================== 
## Call:
## Quotes = Quotes.l1 + TV.advert.l1 + Quotes.l2 + TV.advert.l2 + const 
## 
##    Quotes.l1 TV.advert.l1    Quotes.l2 TV.advert.l2        const 
##    2.5587745   -2.7341322   -1.0943511    0.9337613    8.3490139 
## 
## 
## Estimated coefficients for equation TV.advert: 
## ============================================== 
## Call:
## TV.advert = Quotes.l1 + TV.advert.l1 + Quotes.l2 + TV.advert.l2 + const 
## 
##    Quotes.l1 TV.advert.l1    Quotes.l2 TV.advert.l2        const 
##    1.1032431   -1.0161438   -0.6547575    0.6458306    5.0776769
aic1<-summary(modelo1)$logLik
aic1
## [1] 8.972301
aic2<-summary(modelo2)$logLik
aic2
## [1] -74.4654
serial.test(modelo2, lags.pt=12, type="PT.asymptotic")
## 
##  Portmanteau Test (asymptotic)
## 
## data:  Residuals of VAR object modelo2
## Chi-squared = 38.822, df = 40, p-value = 0.5232
roots(modelo2)
## [1] 0.7400142 0.7400142 0.6987953 0.2492407
normality.test(modelo2, multivariate.only=FALSE)
## $Quotes
## 
##  JB-Test (univariate)
## 
## data:  Residual of Quotes equation
## Chi-squared = 0.040082, df = 2, p-value = 0.9802
## 
## 
## $TV.advert
## 
##  JB-Test (univariate)
## 
## data:  Residual of TV.advert equation
## Chi-squared = 0.24206, df = 2, p-value = 0.886
## 
## 
## $JB
## 
##  JB-Test (multivariate)
## 
## data:  Residuals of VAR object modelo2
## Chi-squared = 37.953, df = 4, p-value = 1.146e-07
## 
## 
## $Skewness
## 
##  Skewness only (multivariate)
## 
## data:  Residuals of VAR object modelo2
## Chi-squared = 14.539, df = 2, p-value = 0.0006963
## 
## 
## $Kurtosis
## 
##  Kurtosis only (multivariate)
## 
## data:  Residuals of VAR object modelo2
## Chi-squared = 23.413, df = 2, p-value = 8.238e-06
autoplot(forecast(modelo2))