1- INTRODUÇÃO:

1.1 - BASES DE DADOS:

library(readxl)

setwd("C:/Users/Giordano Bruno Boff/Desktop/FGV/EESP - Ciências Econômicas/4º SEMESTRE/Econometria II/Trabalho")

#EXPORTAÇÔES:
exp = read_excel("exportacoes.xls")
colnames(exp) = c("Período", "Exportações")
exp = data.frame(exp, row.names = 1)
exp = ts(exp, start = 1995, frequency = 12)

#IMPORTAÇÕES:
imp = read_excel("importacoes.xls")
colnames(imp) = c("Período", "Importações")
imp = data.frame(imp, row.names = 1)
imp = ts(imp, start = 1995, frequency = 12)

#BALANÇA COMERCIAL:
bc = log(exp/imp)
colnames(bc) = c("Balança Comercial")

#TAXA DE CÂMBIO REAL:
tx_cambio_real = read_excel("tx_cambio_real.xls")
tx_cambio_real = tx_cambio_real[181:525,]
colnames(tx_cambio_real) = c("Período", "Taxa de Câmbio Real", "")
tx_cambio_real = data.frame(tx_cambio_real, row.names = 1)
tx_cambio_real = tx_cambio_real$Taxa.de.Câmbio.Real
tx_cambio_real = log(tx_cambio_real)
tx_cambio_real = ts(tx_cambio_real, start = 1995, frequency = 12)

2 - ANÁLISE GRÁFICA

2.1 - EXPORTAÇÕES:

par(mfrow = c(2, 2))
plot(exp, type = 'line')
plot(diff(exp), type = 'line')
acf(exp)
pacf(exp)

2.2 - IMPORTAÇÕES:

par(mfrow = c(2, 2))
plot(imp, type = 'line')
plot(diff(imp), type = 'line')
acf(imp)
pacf(imp)

2.3 - BALANÇA COMERCIAL:

par(mfrow = c(2, 2))
plot(bc, type = 'line')
plot(diff(bc), type = 'line')
acf(bc)
pacf(bc)

2.5 - TAXA DE CÂMBIO REAL:

par(mfrow = c(2, 2))
plot(tx_cambio_real, type = 'line')
plot(diff(tx_cambio_real), type = 'line')
acf(tx_cambio_real)
pacf(tx_cambio_real)

3 - ESTACIONARIEDADE E RAIZ UNITÁRIA

3.1 - AUGMENTED DICKEY–FULLER TEST:

library(aTSA)
adf_bc = adf.test(bc, nlag = 21); adf_bc
## Augmented Dickey-Fuller Test 
## alternative: stationary 
##  
## Type 1: no drift no trend 
##       lag    ADF p.value
##  [1,]   0 -5.159  0.0100
##  [2,]   1 -3.098  0.0100
##  [3,]   2 -2.724  0.0100
##  [4,]   3 -2.600  0.0100
##  [5,]   4 -2.560  0.0109
##  [6,]   5 -2.069  0.0396
##  [7,]   6 -1.726  0.0840
##  [8,]   7 -1.443  0.1631
##  [9,]   8 -1.086  0.2906
## [10,]   9 -0.993  0.3240
## [11,]  10 -0.931  0.3461
## [12,]  11 -0.633  0.4526
## [13,]  12 -0.861  0.3711
## [14,]  13 -1.089  0.2898
## [15,]  14 -1.185  0.2555
## [16,]  15 -1.098  0.2865
## [17,]  16 -0.994  0.3237
## [18,]  17 -1.054  0.3020
## [19,]  18 -1.183  0.2561
## [20,]  19 -1.245  0.2339
## [21,]  20 -1.281  0.2211
## Type 2: with drift no trend 
##       lag   ADF p.value
##  [1,]   0 -6.43  0.0100
##  [2,]   1 -4.10  0.0100
##  [3,]   2 -3.71  0.0100
##  [4,]   3 -3.56  0.0100
##  [5,]   4 -3.53  0.0100
##  [6,]   5 -3.01  0.0371
##  [7,]   6 -2.60  0.0960
##  [8,]   7 -2.26  0.2267
##  [9,]   8 -1.83  0.3952
## [10,]   9 -1.72  0.4373
## [11,]  10 -1.69  0.4515
## [12,]  11 -1.44  0.5435
## [13,]  12 -1.60  0.4860
## [14,]  13 -1.77  0.4166
## [15,]  14 -1.92  0.3587
## [16,]  15 -1.82  0.4001
## [17,]  16 -1.68  0.4540
## [18,]  17 -1.79  0.4089
## [19,]  18 -1.99  0.3312
## [20,]  19 -2.10  0.2887
## [21,]  20 -2.22  0.2401
## Type 3: with drift and trend 
##       lag   ADF p.value
##  [1,]   0 -6.72  0.0100
##  [2,]   1 -4.21  0.0100
##  [3,]   2 -3.79  0.0197
##  [4,]   3 -3.65  0.0280
##  [5,]   4 -3.63  0.0302
##  [6,]   5 -3.06  0.1286
##  [7,]   6 -2.65  0.3012
##  [8,]   7 -2.30  0.4476
##  [9,]   8 -1.86  0.6361
## [10,]   9 -1.75  0.6836
## [11,]  10 -1.69  0.7086
## [12,]  11 -1.37  0.8417
## [13,]  12 -1.62  0.7384
## [14,]  13 -1.89  0.6226
## [15,]  14 -2.03  0.5617
## [16,]  15 -1.92  0.6087
## [17,]  16 -1.78  0.6682
## [18,]  17 -1.88  0.6270
## [19,]  18 -2.07  0.5471
## [20,]  19 -2.17  0.5049
## [21,]  20 -2.26  0.4679
## ---- 
## Note: in fact, p.value = 0.01 means p.value <= 0.01
## $type1
##       lag        ADF    p.value
##  [1,]   0 -5.1587840 0.01000000
##  [2,]   1 -3.0975257 0.01000000
##  [3,]   2 -2.7242860 0.01000000
##  [4,]   3 -2.5999320 0.01000000
##  [5,]   4 -2.5595012 0.01089447
##  [6,]   5 -2.0689722 0.03960906
##  [7,]   6 -1.7256477 0.08399278
##  [8,]   7 -1.4432171 0.16313675
##  [9,]   8 -1.0863504 0.29058914
## [10,]   9 -0.9927497 0.32401796
## [11,]  10 -0.9309451 0.34609103
## [12,]  11 -0.6328490 0.45255391
## [13,]  12 -0.8608900 0.37111070
## [14,]  13 -1.0886972 0.28975099
## [15,]  14 -1.1847133 0.25545955
## [16,]  15 -1.0977115 0.28653159
## [17,]  16 -0.9936210 0.32370679
## [18,]  17 -1.0544459 0.30198360
## [19,]  18 -1.1828494 0.25612523
## [20,]  19 -1.2451048 0.23389116
## [21,]  20 -1.2810144 0.22106630
## 
## $type2
##       lag       ADF    p.value
##  [1,]   0 -6.425643 0.01000000
##  [2,]   1 -4.100427 0.01000000
##  [3,]   2 -3.710530 0.01000000
##  [4,]   3 -3.557471 0.01000000
##  [5,]   4 -3.527904 0.01000000
##  [6,]   5 -3.010727 0.03706855
##  [7,]   6 -2.600147 0.09601545
##  [8,]   7 -2.255420 0.22674607
##  [9,]   8 -1.829090 0.39517621
## [10,]   9 -1.722515 0.43728074
## [11,]  10 -1.686597 0.45147082
## [12,]  11 -1.440312 0.54345834
## [13,]  12 -1.599264 0.48597350
## [14,]  13 -1.774803 0.41662339
## [15,]  14 -1.921368 0.35871988
## [16,]  15 -1.816625 0.40010089
## [17,]  16 -1.680225 0.45398822
## [18,]  17 -1.794330 0.40890896
## [19,]  18 -1.991132 0.33115824
## [20,]  19 -2.098543 0.28872340
## [21,]  20 -2.221648 0.24008839
## 
## $type3
##       lag       ADF    p.value
##  [1,]   0 -6.721266 0.01000000
##  [2,]   1 -4.208962 0.01000000
##  [3,]   2 -3.785736 0.01972753
##  [4,]   3 -3.651013 0.02799704
##  [5,]   4 -3.628089 0.03018047
##  [6,]   5 -3.061965 0.12864647
##  [7,]   6 -2.652092 0.30122430
##  [8,]   7 -2.304440 0.44760406
##  [9,]   8 -1.858127 0.63606411
## [10,]   9 -1.745730 0.68357731
## [11,]  10 -1.686551 0.70859365
## [12,]  11 -1.371784 0.84165383
## [13,]  12 -1.616082 0.73838265
## [14,]  13 -1.889910 0.62262852
## [15,]  14 -2.034097 0.56167709
## [16,]  15 -1.922829 0.60871259
## [17,]  16 -1.782223 0.66815041
## [18,]  17 -1.879633 0.62697293
## [19,]  18 -2.068552 0.54711180
## [20,]  19 -2.168330 0.50493305
## [21,]  20 -2.256347 0.46785389
adf_cambio = adf.test(tx_cambio_real, nlag = 6); adf_cambio
## Augmented Dickey-Fuller Test 
## alternative: stationary 
##  
## Type 1: no drift no trend 
##      lag   ADF p.value
## [1,]   0 0.291   0.728
## [2,]   1 0.169   0.692
## [3,]   2 0.122   0.679
## [4,]   3 0.121   0.679
## [5,]   4 0.155   0.689
## [6,]   5 0.118   0.678
## Type 2: with drift no trend 
##      lag   ADF p.value
## [1,]   0 -1.69   0.449
## [2,]   1 -2.35   0.191
## [3,]   2 -2.00   0.328
## [4,]   3 -2.06   0.304
## [5,]   4 -2.14   0.274
## [6,]   5 -2.22   0.239
## Type 3: with drift and trend 
##      lag   ADF p.value
## [1,]   0 -1.71   0.698
## [2,]   1 -2.38   0.415
## [3,]   2 -2.06   0.549
## [4,]   3 -2.12   0.524
## [5,]   4 -2.18   0.499
## [6,]   5 -2.28   0.457
## ---- 
## Note: in fact, p.value = 0.01 means p.value <= 0.01
## $type1
##      lag       ADF   p.value
## [1,]   0 0.2911587 0.7276716
## [2,]   1 0.1685321 0.6923833
## [3,]   2 0.1220448 0.6790057
## [4,]   3 0.1213850 0.6788158
## [5,]   4 0.1552506 0.6885613
## [6,]   5 0.1177732 0.6777765
## 
## $type2
##      lag       ADF   p.value
## [1,]   0 -1.693404 0.4487815
## [2,]   1 -2.345135 0.1913026
## [3,]   2 -1.998813 0.3281237
## [4,]   3 -2.059974 0.3039610
## [5,]   4 -2.136098 0.2738867
## [6,]   5 -2.224931 0.2387913
## 
## $type3
##      lag       ADF   p.value
## [1,]   0 -1.710459 0.6984870
## [2,]   1 -2.381601 0.4151154
## [3,]   2 -2.063711 0.5491583
## [4,]   3 -2.123270 0.5239814
## [5,]   4 -2.182922 0.4987698
## [6,]   5 -2.281750 0.4571579

4 - ESPECIFICAÇÂO DO MODELO:

4.1 - CRITÉRIOS DE INFORMAÇÃO

library(vars)
VAR = cbind(bc, tx_cambio_real)
espec = VARselect(VAR); espec
## $selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      9      2      2      9 
## 
## $criteria
##                    1             2             3             4             5
## AIC(n) -1.074526e+01 -1.101276e+01 -1.102140e+01 -1.101931e+01 -1.100125e+01
## HQ(n)  -1.071803e+01 -1.096737e+01 -1.095785e+01 -1.093761e+01 -1.090139e+01
## SC(n)  -1.067695e+01 -1.089890e+01 -1.086200e+01 -1.081437e+01 -1.075077e+01
## FPE(n)  2.154732e-05  1.649005e-05  1.634828e-05  1.638270e-05  1.668166e-05
##                    6             7             8             9            10
## AIC(n) -1.102300e+01 -1.104084e+01 -1.104077e+01 -1.106013e+01 -1.104785e+01
## HQ(n)  -1.090498e+01 -1.090467e+01 -1.088644e+01 -1.088765e+01 -1.085721e+01
## SC(n)  -1.072698e+01 -1.069928e+01 -1.065366e+01 -1.062749e+01 -1.056966e+01
## FPE(n)  1.632323e-05  1.603522e-05  1.603732e-05  1.573079e-05  1.592653e-05

5 - RELAÇÃO DE COINTEGRAÇÃO:

5.1 - JOHANSEN TEST:

j_test = ca.jo(VAR, type = 'trace',ecdet = 'const', K = 2, season = 12)
summary(j_test)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: trace statistic , without linear trend and constant in cointegration 
## 
## Eigenvalues (lambda):
## [1] 0.06275245 0.02012530 0.00000000
## 
## Values of teststatistic and critical values of test:
## 
##           test 10pct  5pct  1pct
## r <= 1 |  6.97  7.52  9.24 12.97
## r = 0  | 29.20 17.85 19.96 24.60
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##                        bc.l2 tx_cambio_real.l2    constant
## bc.l2              1.0000000          1.000000  1.00000000
## tx_cambio_real.l2 -0.4329849          7.718752 -0.01499546
## constant           1.9733131        -37.993691  1.65446668
## 
## Weights W:
## (This is the loading matrix)
## 
##                        bc.l2 tx_cambio_real.l2      constant
## bc.d             -0.14008268       0.002643939  1.029397e-17
## tx_cambio_real.d -0.01749186      -0.002750435 -2.834325e-18

6 - MODELAGEM:

6.1 - VECTOR ERROR CORRECTION MODEL (VECM):

library(tsDyn)
modelo = VECM(VAR, lag = 2, r = 1,
              include = "const",
              estim = 'ML')
summary(modelo)
## #############
## ###Model VECM 
## #############
## Full sample size: 345    End sample size: 342
## Number of variables: 2   Number of estimated slope parameters 12
## AIC -3776.654    BIC -3726.802   SSR 4.398904
## Cointegrating vector (estimated by ML):
##    bc tx_cambio_real
## r1  1     -0.4779439
## 
## 
##                         ECT                 Intercept          
## Equation bc             -0.1707(0.0360)***  -0.3741(0.0796)*** 
## Equation tx_cambio_real -0.0114(0.0120)     -0.0248(0.0265)    
##                         bc -1               tx_cambio_real -1 
## Equation bc             -0.4455(0.0566)***  0.0141(0.1630)    
## Equation tx_cambio_real -0.0072(0.0188)     0.3502(0.0543)*** 
##                         bc -2               tx_cambio_real -2  
## Equation bc             -0.0883(0.0530).    -0.1095(0.1641)    
## Equation tx_cambio_real -0.0223(0.0176)     -0.1423(0.0546)**

7 - DIAGNÓSTICO DOS RESÍDUOS:

7.1 - ANÁLISE GRÁFICA:

par(mfrow = c(1, 2))
res = modelo$residuals
res = data.frame(res)

res_bc = res$bc
hist(res_bc)
plot(res_bc, type = 'line')

res_cambio = res$tx_cambio_real
hist(res_cambio)
plot(res_cambio, type = 'line')

7.2 - JARQUE-BERA TEST - NORMALIDADE:

library(tseries)
n_bc = jarque.bera.test(res_bc); n_bc
## 
##  Jarque Bera Test
## 
## data:  res_bc
## X-squared = 146.49, df = 2, p-value < 2.2e-16
n_cambio = jarque.bera.test(res_cambio); n_cambio
## 
##  Jarque Bera Test
## 
## data:  res_cambio
## X-squared = 281.79, df = 2, p-value < 2.2e-16

7.3 - LJUNG-BOX TEST - AUTOCORRELAÇÃO:

library(tseries)
ac_bc = Box.test(res_bc, lag = 1, type = c("Ljung-Box")); ac_bc
## 
##  Box-Ljung test
## 
## data:  res_bc
## X-squared = 0.0081376, df = 1, p-value = 0.9281
ac_cambio = Box.test(res_cambio, lag = 1, type = c("Ljung-Box")); ac_cambio
## 
##  Box-Ljung test
## 
## data:  res_cambio
## X-squared = 0.035651, df = 1, p-value = 0.8502

8 - CAUSALIDADE DE GRANGER:

8.1 - CURJA J:

library(lmtest)
gc_bc = grangertest(bc ~ tx_cambio_real, order = 2); gc_bc
## Granger causality test
## 
## Model 1: bc ~ Lags(bc, 1:2) + Lags(tx_cambio_real, 1:2)
## Model 2: bc ~ Lags(bc, 1:2)
##   Res.Df Df      F   Pr(>F)   
## 1    338                      
## 2    340 -2 4.6837 0.009853 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
gc_cambio = grangertest(tx_cambio_real ~ bc, order = 2); gc_cambio
## Granger causality test
## 
## Model 1: tx_cambio_real ~ Lags(tx_cambio_real, 1:2) + Lags(bc, 1:2)
## Model 2: tx_cambio_real ~ Lags(tx_cambio_real, 1:2)
##   Res.Df Df     F Pr(>F)
## 1    338                
## 2    340 -2 0.857 0.4253