rm(list=ls())
if (!require("pacman")) install.packages("pacman")
## Carregando pacotes exigidos: pacman
## Warning: package 'pacman' was built under R version 4.2.3
library(pacman)
carregar_pacotes <- function() {
  pacman::p_load(
    ggplot2,
    gridExtra,
    ipeadatar,
    tseries,
    dplyr,
    forecast,
    lmtest,
    FinTS,
    tidyverse,
    lubridate,
    urca,
    vars
  )
}
carregar_pacotes()

## Importação dos dados
seriesbr <- ipeadatar::available_series("br")
preço_grão_inter <- ipeadata("IFS_SOJAGP")
exp_grão_quant <- ipeadata("HIST_XSOJAQ")
preço_saca_br <- ipeadata("DERAL12_PRSO12")
tax_câmbio <- ipeadata("BM_ERC")   

## Transformando as periodicidades das séries
preço_saca_br <- preço_saca_br %>%
  mutate(date = format(date, "%Y")) %>%
  group_by(date) %>%
  summarize(cot_br = mean(value))

preço_grão_inter <- preço_grão_inter %>%
  mutate(date = format(date, "%Y")) %>%
  group_by(date)
preço_grão_inter <- preço_grão_inter %>%
  summarize(cot_inter = value*0.06)

exp_grão_quant <- exp_grão_quant %>%
  mutate(date = format(date, "%Y")) %>%
  group_by(date)
exp_grão_quant <- exp_grão_quant %>%
  rename(exp_quant = value)

tax_câmbio <- tax_câmbio %>%
  mutate(date = format(date, "%Y")) %>%
  group_by(date)
tax_câmbio <- tax_câmbio %>%
  rename(tx_câmbio = value)

## Filtrando os períodos
preço_saca_br <- preço_saca_br %>%
  filter(date>=1990) %>%
  filter(date<=2023)

preço_grão_inter <- preço_grão_inter %>%
  filter(date>=1990) %>%
  filter(date<=2023)

exp_grão_quant <- exp_grão_quant %>%
  filter(date>=1990) %>%
  filter(date<=2023)

tax_câmbio <- tax_câmbio %>%
  filter(date>=1990) %>%
  filter(date<=2023)

## Filtrando as variáveis
preço_grão_inter_subset <- preço_grão_inter[, c("cot_inter")]

exp_grão_quant_subset <- exp_grão_quant[, c("exp_quant", "date")]

tax_câmbio_subset <- tax_câmbio[, c("tx_câmbio")]

preço_saca_br <- preço_saca_br[, c("cot_br")]

## Construindo a base de dados
dados <-cbind(tax_câmbio_subset, preço_grão_inter_subset, preço_saca_br, exp_grão_quant_subset)
view(dados)

## Plotando variação da cotação doméstica contra cotação internacional (ambos em reais por saca por meio da taxa de câmbio)
cot_inter_reais <- dados$cot_inter*dados$tx_câmbio
dados_gráfico <- cbind(dados, cot_inter_reais)
plot_inter <- ggplot(dados_gráfico, aes(date, cot_inter_reais)) + geom_point()
plot_br <- ggplot(dados_gráfico, aes(date, cot_br)) + geom_point()
grid.arrange(plot_br, plot_inter, ncol=2, nrow=2)

## Transformação das variáveis em log e declaração de séries temporais
lpgi <- as.vector(log(dados$cot_inter))
legq <- as.vector(log(dados$exp_quant))
lpsb <- as.vector(log(dados$cot_br))
ltxc <- as.vector(log(dados$tx_câmbio))

lpgi <- ts(lpgi, start = c(1990), frequency = 1)
legq <- ts(legq, start = c(1990), frequency = 1)
lpsb <- ts(lpsb, start = c(1990), frequency = 1)
ltxc <- ts(ltxc, start = c(1990), frequency = 1)

## TESTE ADF: Adotando um alfa = 5%
#ADF lpgi 
#M3
M3_lpgi<-ur.df(lpgi, type = "trend", selectlags = c("BIC"))
summary(M3_lpgi) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária (M2)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression trend 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.302864 -0.093277 -0.002279  0.098564  0.260382 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  0.905341   0.306256   2.956  0.00626 **
## z.lag.1     -0.374983   0.126450  -2.965  0.00612 **
## tt           0.011160   0.004659   2.396  0.02352 * 
## z.diff.lag   0.402181   0.175068   2.297  0.02929 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1535 on 28 degrees of freedom
## Multiple R-squared:  0.2742, Adjusted R-squared:  0.1965 
## F-statistic: 3.526 on 3 and 28 DF,  p-value: 0.02764
## 
## 
## Value of test-statistic is: -2.9655 3.1555 4.4107 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.15 -3.50 -3.18
## phi2  7.02  5.13  4.31
## phi3  9.31  6.73  5.61
#M2
M2_lpgi<-ur.df(lpgi, type = "drift", selectlags = c("BIC"))
summary(M2_lpgi) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária (M1)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.29885 -0.08180 -0.01275  0.08265  0.34846 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  0.42690    0.25042   1.705   0.0989 .
## z.lag.1     -0.14100    0.08662  -1.628   0.1144  
## z.diff.lag   0.31154    0.18437   1.690   0.1018  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1656 on 29 degrees of freedom
## Multiple R-squared:  0.1255, Adjusted R-squared:  0.06516 
## F-statistic:  2.08 on 2 and 29 DF,  p-value: 0.1431
## 
## 
## Value of test-statistic is: -1.6278 1.6022 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.58 -2.93 -2.60
## phi1  7.06  4.86  3.94
#M1
M1_lpgi<-ur.df(lpgi, type = "none", selectlags = c("BIC"))
summary(M1_lpgi) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária (M1)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression none 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.27512 -0.08707  0.00365  0.09211  0.36910 
## 
## Coefficients:
##            Estimate Std. Error t value Pr(>|t|)
## z.lag.1    0.005617   0.010606   0.530    0.600
## z.diff.lag 0.216769   0.181283   1.196    0.241
## 
## Residual standard error: 0.1708 on 30 degrees of freedom
## Multiple R-squared:  0.06467,    Adjusted R-squared:  0.002312 
## F-statistic: 1.037 on 2 and 30 DF,  p-value: 0.3669
## 
## 
## Value of test-statistic is: 0.5297 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau1 -2.62 -1.95 -1.61
#ADF legq
#M3
M3_legq<-ur.df(legq, type = "trend", selectlags = c("BIC"))
summary(M3_legq) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária (M2)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression trend 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.57387 -0.07054  0.01538  0.10316  0.39524 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  7.30254    2.56768   2.844  0.00823 **
## z.lag.1     -0.47711    0.17334  -2.753  0.01026 * 
## tt           0.04861    0.01981   2.454  0.02059 * 
## z.diff.lag  -0.04670    0.15693  -0.298  0.76821   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1861 on 28 degrees of freedom
## Multiple R-squared:  0.3442, Adjusted R-squared:  0.2739 
## F-statistic: 4.898 on 3 and 28 DF,  p-value: 0.00734
## 
## 
## Value of test-statistic is: -2.7526 9.2674 4.7771 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.15 -3.50 -3.18
## phi2  7.02  5.13  4.31
## phi3  9.31  6.73  5.61
#M2
M2_legq<-ur.df(legq, type = "drift", selectlags = c("BIC"))
summary(M2_legq) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária (M1)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.59431 -0.10039  0.00986  0.10068  0.59101 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  1.13336    0.56856   1.993   0.0557 .
## z.lag.1     -0.05862    0.03380  -1.735   0.0934 .
## z.diff.lag  -0.26724    0.13936  -1.918   0.0651 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2016 on 29 degrees of freedom
## Multiple R-squared:  0.2031, Adjusted R-squared:  0.1481 
## F-statistic: 3.695 on 2 and 29 DF,  p-value: 0.0372
## 
## 
## Value of test-statistic is: -1.7345 9.2809 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.58 -2.93 -2.60
## phi1  7.06  4.86  3.94
#M1
M1_legq<-ur.df(legq, type = "none", selectlags = c("BIC"))
summary(M1_legq) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária (M2)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression none 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.4979 -0.1193 -0.0069  0.1116  0.7096 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## z.lag.1     0.008599   0.002360   3.643  0.00101 **
## z.diff.lag -0.287511   0.145714  -1.973  0.05776 . 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2113 on 30 degrees of freedom
## Multiple R-squared:  0.316,  Adjusted R-squared:  0.2704 
## F-statistic: 6.931 on 2 and 30 DF,  p-value: 0.003354
## 
## 
## Value of test-statistic is: 3.6432 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau1 -2.62 -1.95 -1.61
#ADF lpsb
#M3
M3_lpsb<-ur.df(lpsb, type = "trend", selectlags = c("BIC"))
summary(M3_lpsb) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression trend 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.88193 -0.14589  0.02437  0.13878  0.96321 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.611100   0.170761   3.579  0.00128 ** 
## z.lag.1     -0.281425   0.041386  -6.800 2.19e-07 ***
## tt           0.023800   0.009034   2.634  0.01358 *  
## z.diff.lag   0.343889   0.096300   3.571  0.00131 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3268 on 28 degrees of freedom
## Multiple R-squared:  0.8613, Adjusted R-squared:  0.8465 
## F-statistic: 57.98 on 3 and 28 DF,  p-value: 3.928e-12
## 
## 
## Value of test-statistic is: -6.8 16.2166 24.1848 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.15 -3.50 -3.18
## phi2  7.02  5.13  4.31
## phi3  9.31  6.73  5.61
#M2
M2_lpsb<-ur.df(lpsb, type = "drift", selectlags = c("BIC"))
summary(M2_lpsb) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.0752 -0.1086 -0.0243  0.1511  0.9304 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   0.8565     0.1571   5.453 7.21e-06 ***
## z.lag.1      -0.2199     0.0375  -5.864 2.31e-06 ***
## z.diff.lag    0.3253     0.1054   3.086  0.00443 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3587 on 29 degrees of freedom
## Multiple R-squared:  0.827,  Adjusted R-squared:  0.815 
## F-statistic:  69.3 on 2 and 29 DF,  p-value: 8.97e-12
## 
## 
## Value of test-statistic is: -5.864 17.3092 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.58 -2.93 -2.60
## phi1  7.06  4.86  3.94
#M1
M1_lpsb<-ur.df(lpsb, type = "none", selectlags = c("BIC"))
summary(M1_lpsb) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression none 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.90644 -0.03251  0.18689  0.37081  0.95427 
## 
## Coefficients:
##            Estimate Std. Error t value Pr(>|t|)    
## z.lag.1    -0.03732    0.02363  -1.580    0.125    
## z.diff.lag  0.76908    0.09377   8.202 3.72e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5019 on 30 degrees of freedom
## Multiple R-squared:  0.7046, Adjusted R-squared:  0.6849 
## F-statistic: 35.78 on 2 and 30 DF,  p-value: 1.138e-08
## 
## 
## Value of test-statistic is: -1.5796 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau1 -2.62 -1.95 -1.61
#ADF ltxc
#M3
M3_ltxc<-ur.df(ltxc, type = "trend", selectlags = c("BIC"))
summary(M3_ltxc) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression trend 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.89884 -0.08791 -0.02490  0.09449  0.95679 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.001385   0.158835  -0.009 0.993106    
## z.lag.1     -0.263185   0.038898  -6.766 2.39e-07 ***
## tt           0.012959   0.007899   1.641 0.112072    
## z.diff.lag   0.361302   0.095378   3.788 0.000739 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3115 on 28 degrees of freedom
## Multiple R-squared:  0.8706, Adjusted R-squared:  0.8568 
## F-statistic: 62.81 on 3 and 28 DF,  p-value: 1.496e-12
## 
## 
## Value of test-statistic is: -6.766 15.7797 23.5489 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.15 -3.50 -3.18
## phi2  7.02  5.13  4.31
## phi3  9.31  6.73  5.61
#M2
M2_ltxc<-ur.df(ltxc, type = "drift", selectlags = c("BIC"))
summary(M2_ltxc) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.96051 -0.12902 -0.00717  0.11906  0.95768 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.23427    0.06974   3.359  0.00220 ** 
## z.lag.1     -0.23778    0.03671  -6.478 4.34e-07 ***
## z.diff.lag   0.32857    0.09595   3.424  0.00186 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3205 on 29 degrees of freedom
## Multiple R-squared:  0.8582, Adjusted R-squared:  0.8484 
## F-statistic: 87.75 on 2 and 29 DF,  p-value: 5.011e-13
## 
## 
## Value of test-statistic is: -6.4776 21.0934 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.58 -2.93 -2.60
## phi1  7.06  4.86  3.94
#M1
M1_ltxc<-ur.df(ltxc, type = "none", selectlags = c("BIC"))
summary(M1_ltxc) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression none 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.26028  0.07753  0.14255  0.27737  0.82682 
## 
## Coefficients:
##            Estimate Std. Error t value Pr(>|t|)    
## z.lag.1    -0.18194    0.03792  -4.797 4.13e-05 ***
## z.diff.lag  0.51578    0.09051   5.699 3.24e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3714 on 30 degrees of freedom
## Multiple R-squared:  0.8304, Adjusted R-squared:  0.8191 
## F-statistic: 73.47 on 2 and 30 DF,  p-value: 2.751e-12
## 
## 
## Value of test-statistic is: -4.7974 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau1 -2.62 -1.95 -1.61
## Testes ADF com uma diferença 
#ADF lpgi *com diferença*
#M3
M3_lpgi_d<-ur.df(diff(lpgi), type = "trend", selectlags = c("BIC"))
summary(M3_lpgi_d) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression trend 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.296322 -0.092914  0.002373  0.104299  0.275749 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.012846   0.060483   0.212  0.83340    
## z.lag.1     -1.171093   0.213112  -5.495 8.08e-06 ***
## tt           0.001137   0.003171   0.359  0.72265    
## z.diff.lag   0.491534   0.170920   2.876  0.00777 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1568 on 27 degrees of freedom
## Multiple R-squared:  0.5327, Adjusted R-squared:  0.4808 
## F-statistic: 10.26 on 3 and 27 DF,  p-value: 0.0001107
## 
## 
## Value of test-statistic is: -5.4952 10.0965 15.1406 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.15 -3.50 -3.18
## phi2  7.02  5.13  4.31
## phi3  9.31  6.73  5.61
#M2
M2_lpgi_d<-ur.df(diff(lpgi), type = "drift", selectlags = c("BIC"))
summary(M2_lpgi_d) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.287359 -0.096071  0.006476  0.103695  0.290685 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.03191    0.02841   1.123  0.27089    
## z.lag.1     -1.16210    0.20831  -5.579 5.72e-06 ***
## z.diff.lag   0.48719    0.16782   2.903  0.00713 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1543 on 28 degrees of freedom
## Multiple R-squared:  0.5305, Adjusted R-squared:  0.497 
## F-statistic: 15.82 on 2 and 28 DF,  p-value: 2.527e-05
## 
## 
## Value of test-statistic is: -5.5786 15.5648 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.58 -2.93 -2.60
## phi1  7.06  4.86  3.94
#M1
M1_lpgi_d<-ur.df(diff(lpgi), type = "none", selectlags = c("BIC"))
summary(M1_lpgi_d) #Valor do teste < Valor crítico, portanto, rejeita-se a hipótese nula, não há raiz unitária e a série é estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression none 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.25105 -0.06759  0.03070  0.13656  0.32163 
## 
## Coefficients:
##            Estimate Std. Error t value Pr(>|t|)    
## z.lag.1     -1.1112     0.2042  -5.441 7.46e-06 ***
## z.diff.lag   0.4654     0.1674   2.780  0.00945 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.155 on 29 degrees of freedom
## Multiple R-squared:  0.5095, Adjusted R-squared:  0.4756 
## F-statistic: 15.06 on 2 and 29 DF,  p-value: 3.271e-05
## 
## 
## Value of test-statistic is: -5.4407 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau1 -2.62 -1.95 -1.61
#ADF legq *com diferença*
#M3
M3_legq_d<-ur.df(diff(legq), type = "trend", selectlags = c("BIC"))
summary(M3_legq_d) #Valor do teste < Valor crítico, portanto, rejeita-se a hipótese nula, não há raiz unitária e a série é estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression trend 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.54630 -0.11814 -0.00415  0.12790  0.48888 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.239398   0.087028   2.751   0.0105 *  
## z.lag.1     -1.618745   0.261505  -6.190 1.28e-06 ***
## tt          -0.003870   0.004007  -0.966   0.3428    
## z.diff.lag   0.310275   0.142368   2.179   0.0382 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1925 on 27 degrees of freedom
## Multiple R-squared:  0.6782, Adjusted R-squared:  0.6424 
## F-statistic: 18.97 on 3 and 27 DF,  p-value: 8.032e-07
## 
## 
## Value of test-statistic is: -6.1901 12.8391 19.2519 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.15 -3.50 -3.18
## phi2  7.02  5.13  4.31
## phi3  9.31  6.73  5.61
#M2
M2_legq_d<-ur.df(diff(legq), type = "drift", selectlags = c("BIC"))
summary(M2_legq_d) #Valor do teste < Valor crítico, portanto, rejeita-se a hipótese nula, não há raiz unitária e a série é estacionária
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.50214 -0.13290 -0.01961  0.10611  0.54108 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.16716    0.04442   3.763  0.00079 ***
## z.lag.1     -1.56218    0.25455  -6.137 1.27e-06 ***
## z.diff.lag   0.29853    0.14168   2.107  0.04419 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1922 on 28 degrees of freedom
## Multiple R-squared:  0.6671, Adjusted R-squared:  0.6433 
## F-statistic: 28.05 on 2 and 28 DF,  p-value: 2.056e-07
## 
## 
## Value of test-statistic is: -6.1369 18.8378 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.58 -2.93 -2.60
## phi1  7.06  4.86  3.94
#M1
M1_legq_d<-ur.df(diff(legq), type = "none", selectlags = c("BIC"))
summary(M1_legq_d) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression none 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.45284 -0.00311  0.09461  0.20679  0.80130 
## 
## Coefficients:
##            Estimate Std. Error t value Pr(>|t|)    
## z.lag.1     -0.9606     0.2389  -4.021 0.000377 ***
## z.diff.lag   0.0503     0.1512   0.333 0.741758    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2318 on 29 degrees of freedom
## Multiple R-squared:  0.4993, Adjusted R-squared:  0.4648 
## F-statistic: 14.46 on 2 and 29 DF,  p-value: 4.402e-05
## 
## 
## Value of test-statistic is: -4.0214 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau1 -2.62 -1.95 -1.61
#ADF lpsb *com diferença*
#M3
M3_lpsb_d<-ur.df(diff(lpsb), type = "trend", selectlags = c("BIC"))
summary(M3_lpsb_d)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression trend 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.68176 -0.21951  0.04139  0.14825  1.17516 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  0.23960    0.25364   0.945  0.35321   
## z.lag.1     -0.39629    0.13333  -2.972  0.00615 **
## tt          -0.01005    0.01194  -0.842  0.40714   
## z.diff.lag   0.17715    0.17474   1.014  0.31966   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.492 on 27 degrees of freedom
## Multiple R-squared:  0.2655, Adjusted R-squared:  0.1839 
## F-statistic: 3.254 on 3 and 27 DF,  p-value: 0.03705
## 
## 
## Value of test-statistic is: -2.9722 3.5948 4.8811 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.15 -3.50 -3.18
## phi2  7.02  5.13  4.31
## phi3  9.31  6.73  5.61
#M2
M2_lpsb_d<-ur.df(diff(lpsb), type = "drift", selectlags = c("BIC"))
summary(M2_lpsb_d) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.71197 -0.21846  0.00865  0.17459  1.22679 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  0.04302    0.09865   0.436  0.66611   
## z.lag.1     -0.33416    0.11048  -3.025  0.00529 **
## z.diff.lag   0.13384    0.16613   0.806  0.42722   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4894 on 28 degrees of freedom
## Multiple R-squared:  0.2463, Adjusted R-squared:  0.1924 
## F-statistic: 4.574 on 2 and 28 DF,  p-value: 0.0191
## 
## 
## Value of test-statistic is: -3.0246 5.0905 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.58 -2.93 -2.60
## phi1  7.06  4.86  3.94
#M1
M1_lpsb_d<-ur.df(diff(lpsb), type = "none", selectlags = c("BIC"))
summary(M1_lpsb_d) #Valor do teste < Valor crítico, portanto, rejeita-se a hipótese nula, não há raiz unitária e a série é estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression none 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.73335 -0.17772  0.04662  0.21899  1.22659 
## 
## Coefficients:
##            Estimate Std. Error t value Pr(>|t|)   
## z.lag.1    -0.31272    0.09754  -3.206  0.00327 **
## z.diff.lag  0.11908    0.16035   0.743  0.46370   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4826 on 29 degrees of freedom
## Multiple R-squared:  0.2619, Adjusted R-squared:  0.211 
## F-statistic: 5.144 on 2 and 29 DF,  p-value: 0.01224
## 
## 
## Value of test-statistic is: -3.2059 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau1 -2.62 -1.95 -1.61
#ADF ltxc *com diferença*
#M3
M3_ltxc_d<-ur.df(diff(ltxc), type = "trend", selectlags = c("BIC"))
summary(M3_ltxc_d) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression trend 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.66687 -0.12118 -0.03632  0.17996  1.15244 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  0.20866    0.23960   0.871  0.39150   
## z.lag.1     -0.37987    0.12752  -2.979  0.00605 **
## tt          -0.00859    0.01140  -0.754  0.45755   
## z.diff.lag   0.22296    0.17150   1.300  0.20457   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4623 on 27 degrees of freedom
## Multiple R-squared:  0.2774, Adjusted R-squared:  0.1971 
## F-statistic: 3.455 on 3 and 27 DF,  p-value: 0.03026
## 
## 
## Value of test-statistic is: -2.9789 3.6803 5.1145 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.15 -3.50 -3.18
## phi2  7.02  5.13  4.31
## phi3  9.31  6.73  5.61
#M2
M2_ltxc_d<-ur.df(diff(ltxc), type = "drift", selectlags = c("BIC"))
summary(M2_ltxc_d) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.69854 -0.10243 -0.02721  0.15547  1.19263 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  0.04181    0.09096   0.460  0.64931   
## z.lag.1     -0.32477    0.10368  -3.132  0.00404 **
## z.diff.lag   0.18553    0.16288   1.139  0.26434   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4588 on 28 degrees of freedom
## Multiple R-squared:  0.2622, Adjusted R-squared:  0.2095 
## F-statistic: 4.975 on 2 and 28 DF,  p-value: 0.01416
## 
## 
## Value of test-statistic is: -3.1325 5.3184 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.58 -2.93 -2.60
## phi1  7.06  4.86  3.94
#M1
M1_ltxc_d<-ur.df(diff(ltxc), type = "none", selectlags = c("BIC"))
summary(M1_ltxc_d) #Valor do teste > Valor crítico, portanto, não rejeita-se a hipótese nula, há raiz unitária e a série é não-estacionária 
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression none 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.71506 -0.06468  0.01680  0.19820  1.19662 
## 
## Coefficients:
##            Estimate Std. Error t value Pr(>|t|)   
## z.lag.1    -0.30519    0.09322  -3.274  0.00275 **
## z.diff.lag  0.17044    0.15736   1.083  0.28766   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4525 on 29 degrees of freedom
## Multiple R-squared:  0.2741, Adjusted R-squared:  0.224 
## F-statistic: 5.475 on 2 and 29 DF,  p-value: 0.009608
## 
## 
## Value of test-statistic is: -3.2737 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau1 -2.62 -1.95 -1.61
## Diferenciando variáveis e agregando em dataframes
dlpgi <- diff(lpgi)
dlegq <- diff(legq)
dlpsb <- diff(lpsb)
dltxc <- diff(ltxc)

dados_ts <- ts(as.data.frame(cbind(ltxc, lpgi, lpsb, legq),
                             start = c(1990, frequency = 1)))
ddados_ts <- ts(as.data.frame(cbind(dltxc, dlpgi, dlpsb, dlegq), 
                              start = c(1990), frequency = 1))

## TESTE DE COINTEGRAÇÃO: utilizando critério de Schwarz (4 defasagens)
#Ordem do teste traço
VARselect((dados_ts), lag.max =  4, type="const", season = NULL, exogen = NULL)
## $selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      3      3      3      3 
## 
## $criteria
##                    1             2             3             4
## AIC(n) -1.565266e+01 -1.645785e+01 -1.799217e+01 -1.787162e+01
## HQ(n)  -1.535382e+01 -1.591995e+01 -1.721519e+01 -1.685557e+01
## SC(n)  -1.471852e+01 -1.477642e+01 -1.556343e+01 -1.469557e+01
## FPE(n)  1.612839e-07  7.682802e-08  1.961659e-08  3.178858e-08
#Traço transitório
jotest_traco_tr <- ca.jo((dados_ts), type="trace", K=3, ecdet = "const", spec="transitory",
                         season = NULL, dumvar = NULL) # Há até três níveis de cointegração
summary(jotest_traco_tr)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: trace statistic , without linear trend and constant in cointegration 
## 
## Eigenvalues (lambda):
## [1] 9.359966e-01 7.015766e-01 3.490419e-01 2.317578e-01 2.070216e-16
## 
## Values of teststatistic and critical values of test:
## 
##            test 10pct  5pct  1pct
## r <= 3 |   8.17  7.52  9.24 12.97
## r <= 2 |  21.48 17.85 19.96 24.60
## r <= 1 |  58.97 32.00 34.91 41.07
## r = 0  | 144.18 49.65 53.12 60.16
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##             ltxc.l1     lpgi.l1     lpsb.l1     legq.l1  constant
## ltxc.l1   1.0000000  1.00000000  1.00000000  1.00000000  1.000000
## lpgi.l1   0.9641571  1.02136550  0.98695687  1.17866752  1.430740
## lpsb.l1  -0.8624352 -0.97270279 -1.00371557 -0.99276683 -1.022931
## legq.l1  -0.0851317  0.04262155  0.04923599 -0.03428208  0.161438
## constant  1.0025091 -1.08181690 -0.91181119 -0.09484798 -3.986988
## 
## Weights W:
## (This is the loading matrix)
## 
##           ltxc.l1    lpgi.l1    lpsb.l1    legq.l1     constant
## ltxc.d -3.1444446 -3.6271234  1.6404286  1.2011785 2.970897e-11
## lpgi.d  0.4023547 -0.4982545  1.3953772 -1.3133013 1.390835e-12
## lpsb.d -2.6926051 -4.1235637  4.3242985  0.1689927 2.041034e-11
## legq.d  0.3318604 -3.0523679 -0.6347919  1.3235401 8.802178e-12
#Traço longo prazo
jotest_traco_lp <- ca.jo((dados_ts), type="trace", K=3, ecdet = "const", spec="longrun",
                         season = NULL, dumvar = NULL) # Há até três níveis de cointegração
summary(jotest_traco_lp)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: trace statistic , without linear trend and constant in cointegration 
## 
## Eigenvalues (lambda):
## [1]  9.359966e-01  7.015766e-01  3.490419e-01  2.317578e-01 -9.264453e-16
## 
## Values of teststatistic and critical values of test:
## 
##            test 10pct  5pct  1pct
## r <= 3 |   8.17  7.52  9.24 12.97
## r <= 2 |  21.48 17.85 19.96 24.60
## r <= 1 |  58.97 32.00 34.91 41.07
## r = 0  | 144.18 49.65 53.12 60.16
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##             ltxc.l3     lpgi.l3     lpsb.l3     legq.l3  constant
## ltxc.l3   1.0000000  1.00000000  1.00000000  1.00000000  1.000000
## lpgi.l3   0.9641571  1.02136550  0.98695687  1.17866752  1.430740
## lpsb.l3  -0.8624352 -0.97270279 -1.00371557 -0.99276683 -1.022931
## legq.l3  -0.0851317  0.04262155  0.04923599 -0.03428208  0.161438
## constant  1.0025091 -1.08181690 -0.91181119 -0.09484798 -3.986988
## 
## Weights W:
## (This is the loading matrix)
## 
##           ltxc.l3    lpgi.l3    lpsb.l3    legq.l3     constant
## ltxc.d -3.1444446 -3.6271234  1.6404286  1.2011785 7.550175e-11
## lpgi.d  0.4023547 -0.4982545  1.3953772 -1.3133013 5.459337e-12
## lpsb.d -2.6926052 -4.1235637  4.3242985  0.1689927 5.089868e-11
## legq.d  0.3318604 -3.0523679 -0.6347919  1.3235401 2.880411e-11
#Raíz máxima transitório
jotest_max_tr <- ca.jo((dados_ts), type="eigen", K=3, ecdet = "const", spec="transitory",
                       season = NULL, dumvar = NULL) # Há até dois níveis de cointegração
summary(jotest_max_tr)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: maximal eigenvalue statistic (lambda max) , without linear trend and constant in cointegration 
## 
## Eigenvalues (lambda):
## [1] 9.359966e-01 7.015766e-01 3.490419e-01 2.317578e-01 2.070216e-16
## 
## Values of teststatistic and critical values of test:
## 
##           test 10pct  5pct  1pct
## r <= 3 |  8.17  7.52  9.24 12.97
## r <= 2 | 13.31 13.75 15.67 20.20
## r <= 1 | 37.49 19.77 22.00 26.81
## r = 0  | 85.21 25.56 28.14 33.24
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##             ltxc.l1     lpgi.l1     lpsb.l1     legq.l1  constant
## ltxc.l1   1.0000000  1.00000000  1.00000000  1.00000000  1.000000
## lpgi.l1   0.9641571  1.02136550  0.98695687  1.17866752  1.430740
## lpsb.l1  -0.8624352 -0.97270279 -1.00371557 -0.99276683 -1.022931
## legq.l1  -0.0851317  0.04262155  0.04923599 -0.03428208  0.161438
## constant  1.0025091 -1.08181690 -0.91181119 -0.09484798 -3.986988
## 
## Weights W:
## (This is the loading matrix)
## 
##           ltxc.l1    lpgi.l1    lpsb.l1    legq.l1     constant
## ltxc.d -3.1444446 -3.6271234  1.6404286  1.2011785 2.970897e-11
## lpgi.d  0.4023547 -0.4982545  1.3953772 -1.3133013 1.390835e-12
## lpsb.d -2.6926051 -4.1235637  4.3242985  0.1689927 2.041034e-11
## legq.d  0.3318604 -3.0523679 -0.6347919  1.3235401 8.802178e-12
#Raíz máxima longo prazo
jotest_max_lp <- ca.jo((dados_ts), type="eigen", K=3, ecdet = "const", spec="longrun",
                       season = NULL, dumvar = NULL) # Há até dois níveis de cointegração
summary(jotest_max_lp)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: maximal eigenvalue statistic (lambda max) , without linear trend and constant in cointegration 
## 
## Eigenvalues (lambda):
## [1]  9.359966e-01  7.015766e-01  3.490419e-01  2.317578e-01 -9.264453e-16
## 
## Values of teststatistic and critical values of test:
## 
##           test 10pct  5pct  1pct
## r <= 3 |  8.17  7.52  9.24 12.97
## r <= 2 | 13.31 13.75 15.67 20.20
## r <= 1 | 37.49 19.77 22.00 26.81
## r = 0  | 85.21 25.56 28.14 33.24
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##             ltxc.l3     lpgi.l3     lpsb.l3     legq.l3  constant
## ltxc.l3   1.0000000  1.00000000  1.00000000  1.00000000  1.000000
## lpgi.l3   0.9641571  1.02136550  0.98695687  1.17866752  1.430740
## lpsb.l3  -0.8624352 -0.97270279 -1.00371557 -0.99276683 -1.022931
## legq.l3  -0.0851317  0.04262155  0.04923599 -0.03428208  0.161438
## constant  1.0025091 -1.08181690 -0.91181119 -0.09484798 -3.986988
## 
## Weights W:
## (This is the loading matrix)
## 
##           ltxc.l3    lpgi.l3    lpsb.l3    legq.l3     constant
## ltxc.d -3.1444446 -3.6271234  1.6404286  1.2011785 7.550175e-11
## lpgi.d  0.4023547 -0.4982545  1.3953772 -1.3133013 5.459337e-12
## lpsb.d -2.6926052 -4.1235637  4.3242985  0.1689927 5.089868e-11
## legq.d  0.3318604 -3.0523679 -0.6347919  1.3235401 2.880411e-11
##Modelo VEC é mais adequado
#VEC
vec <- cajorls(jotest_traco_lp, r=3, reg.number = NULL)
vec
## $rlm
## 
## Call:
## lm(formula = substitute(form1), data = data.mat)
## 
## Coefficients:
##           ltxc.d   lpgi.d   lpsb.d   legq.d 
## ect1      -5.1311   1.2995  -2.4919  -3.3553
## ect2      -5.1173   1.2562  -2.5399  -3.4241
## ect3       4.5935  -1.2629   1.9928   3.3200
## ltxc.dl1  -1.3230  -0.6540  -0.8056  -1.2707
## lpgi.dl1  -1.1616  -0.4717  -0.4578  -1.2561
## lpsb.dl1   0.8892   0.7724   0.4895   1.1834
## legq.dl1   0.1477  -0.3680  -0.1557  -0.5555
## ltxc.dl2  -3.1351  -0.7865  -2.6427  -2.2617
## lpgi.dl2  -2.9379  -1.1680  -2.8733  -2.2704
## lpsb.dl2   2.5760   0.7911   2.1022   2.0932
## legq.dl2   0.6551  -0.1728   0.4915  -0.4717
## 
## 
## $beta
##                   ect1        ect2          ect3
## ltxc.l3   1.000000e+00   0.0000000  0.000000e+00
## lpgi.l3   1.776357e-15   1.0000000  1.776357e-15
## lpsb.l3   1.776357e-15   0.0000000  1.000000e+00
## legq.l3  -1.384298e+00   0.5805358 -8.573848e-01
## constant  2.504250e+01 -14.9751523  1.113311e+01
summary(vec$rlm)
## Response ltxc.d :
## 
## Call:
## lm(formula = ltxc.d ~ ect1 + ect2 + ect3 + ltxc.dl1 + lpgi.dl1 + 
##     lpsb.dl1 + legq.dl1 + ltxc.dl2 + lpgi.dl2 + lpsb.dl2 + legq.dl2 - 
##     1, data = data.mat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.3406 -0.1231 -0.0420  0.1002  0.5124 
## 
## Coefficients:
##          Estimate Std. Error t value Pr(>|t|)  
## ect1      -5.1311     2.1482  -2.389   0.0269 *
## ect2      -5.1173     2.1295  -2.403   0.0261 *
## ect3       4.5935     2.1365   2.150   0.0440 *
## ltxc.dl1  -1.3230     1.1221  -1.179   0.2522  
## lpgi.dl1  -1.1616     1.1077  -1.049   0.3068  
## lpsb.dl1   0.8892     1.1041   0.805   0.4301  
## legq.dl1   0.1477     0.2874   0.514   0.6130  
## ltxc.dl2  -3.1351     1.6297  -1.924   0.0687 .
## lpgi.dl2  -2.9379     1.6089  -1.826   0.0828 .
## lpsb.dl2   2.5760     1.6116   1.598   0.1256  
## legq.dl2   0.6551     0.2491   2.630   0.0161 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.223 on 20 degrees of freedom
## Multiple R-squared:  0.9466, Adjusted R-squared:  0.9172 
## F-statistic:  32.2 on 11 and 20 DF,  p-value: 2.679e-10
## 
## 
## Response lpgi.d :
## 
## Call:
## lm(formula = lpgi.d ~ ect1 + ect2 + ect3 + ltxc.dl1 + lpgi.dl1 + 
##     lpsb.dl1 + legq.dl1 + ltxc.dl2 + lpgi.dl2 + lpsb.dl2 + legq.dl2 - 
##     1, data = data.mat)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.21839 -0.08084  0.00095  0.06965  0.32607 
## 
## Coefficients:
##          Estimate Std. Error t value Pr(>|t|)  
## ect1       1.2995     1.4141   0.919    0.369  
## ect2       1.2562     1.4019   0.896    0.381  
## ect3      -1.2629     1.4065  -0.898    0.380  
## ltxc.dl1  -0.6540     0.7387  -0.885    0.386  
## lpgi.dl1  -0.4717     0.7292  -0.647    0.525  
## lpsb.dl1   0.7724     0.7268   1.063    0.301  
## legq.dl1  -0.3680     0.1892  -1.945    0.066 .
## ltxc.dl2  -0.7865     1.0728  -0.733    0.472  
## lpgi.dl2  -1.1680     1.0591  -1.103    0.283  
## lpsb.dl2   0.7911     1.0609   0.746    0.465  
## legq.dl2  -0.1728     0.1640  -1.053    0.305  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1468 on 20 degrees of freedom
## Multiple R-squared:  0.5393, Adjusted R-squared:  0.286 
## F-statistic: 2.129 on 11 and 20 DF,  p-value: 0.06845
## 
## 
## Response lpsb.d :
## 
## Call:
## lm(formula = lpsb.d ~ ect1 + ect2 + ect3 + ltxc.dl1 + lpgi.dl1 + 
##     lpsb.dl1 + legq.dl1 + ltxc.dl2 + lpgi.dl2 + lpsb.dl2 + legq.dl2 - 
##     1, data = data.mat)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.36190 -0.12161 -0.01369  0.12101  0.52469 
## 
## Coefficients:
##          Estimate Std. Error t value Pr(>|t|)  
## ect1      -2.4919     2.3072  -1.080   0.2930  
## ect2      -2.5399     2.2872  -1.110   0.2800  
## ect3       1.9928     2.2947   0.868   0.3954  
## ltxc.dl1  -0.8056     1.2051  -0.668   0.5115  
## lpgi.dl1  -0.4578     1.1897  -0.385   0.7045  
## lpsb.dl1   0.4895     1.1858   0.413   0.6842  
## legq.dl1  -0.1557     0.3087  -0.504   0.6195  
## ltxc.dl2  -2.6427     1.7503  -1.510   0.1467  
## lpgi.dl2  -2.8733     1.7280  -1.663   0.1120  
## lpsb.dl2   2.1022     1.7309   1.214   0.2387  
## legq.dl2   0.4915     0.2676   1.837   0.0812 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2395 on 20 degrees of freedom
## Multiple R-squared:  0.941,  Adjusted R-squared:  0.9085 
## F-statistic: 28.99 on 11 and 20 DF,  p-value: 7.054e-10
## 
## 
## Response legq.d :
## 
## Call:
## lm(formula = legq.d ~ ect1 + ect2 + ect3 + ltxc.dl1 + lpgi.dl1 + 
##     lpsb.dl1 + legq.dl1 + ltxc.dl2 + lpgi.dl2 + lpsb.dl2 + legq.dl2 - 
##     1, data = data.mat)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.22271 -0.12129 -0.03810  0.08488  0.35704 
## 
## Coefficients:
##          Estimate Std. Error t value Pr(>|t|)  
## ect1      -3.3553     1.7762  -1.889   0.0735 .
## ect2      -3.4241     1.7607  -1.945   0.0660 .
## ect3       3.3200     1.7665   1.879   0.0748 .
## ltxc.dl1  -1.2707     0.9277  -1.370   0.1860  
## lpgi.dl1  -1.2561     0.9159  -1.371   0.1854  
## lpsb.dl1   1.1834     0.9129   1.296   0.2096  
## legq.dl1  -0.5555     0.2377  -2.337   0.0299 *
## ltxc.dl2  -2.2617     1.3474  -1.679   0.1088  
## lpgi.dl2  -2.2704     1.3303  -1.707   0.1034  
## lpsb.dl2   2.0932     1.3325   1.571   0.1319  
## legq.dl2  -0.4717     0.2060  -2.290   0.0330 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1843 on 20 degrees of freedom
## Multiple R-squared:  0.5702, Adjusted R-squared:  0.3338 
## F-statistic: 2.412 on 11 and 20 DF,  p-value: 0.04199
##Função impulso resposta
varvec <- vars::vec2var(jotest_traco_lp, r=3)

##Estabilidade: todas as a raizes dentro do circulo unitário
eigen <- eigen(varvec$A$A1)
eigen
## eigen() decomposition
## $values
## [1]  1.2710382+0.0000000i  0.5797077+0.0790627i  0.5797077-0.0790627i
## [4] -0.2911435+0.0000000i
## 
## $vectors
##                [,1]                  [,2]                  [,3]         [,4]
## [1,]  0.32015137+0i  0.6911545+0.0000000i  0.6911545+0.0000000i 0.4178379+0i
## [2,] -0.80985948+0i -0.2385976+0.1391077i -0.2385976-0.1391077i 0.3503888+0i
## [3,] -0.49018584+0i  0.4326654+0.1764031i  0.4326654-0.1764031i 0.3458903+0i
## [4,]  0.03672281+0i -0.2570265+0.4020519i -0.2570265-0.4020519i 0.7635438+0i
#Impacto da taxa de câmbio no volume de exportações
impulso_ltxc_legq = irf(varvec, impulse = "ltxc", response = c('legq'), n.ahead = 5,
                        ortho = FALSE, cumulative = FALSE, boot = TRUE, ci = 0.95,
                        runs = 100, seed = NULL)
plot(impulso_ltxc_legq)

#Impacto do preço internacional da soja em grão no volume de exportações
impulso_lpgi_legq = irf(varvec, impulse = "lpgi", response = c('legq'), n.ahead = 5,
                        ortho = FALSE, cumulative = FALSE, boot = TRUE, ci = 0.95,
                        runs = 100, seed = NULL)
plot(impulso_lpgi_legq)

#Impacto do preço doméstico da soja em grão no volume de exportações
impulso_lpsb_legq=irf(varvec, impulse = "lpsb", response = c('legq'), n.ahead = 5,
                      ortho = FALSE, cumulative = FALSE, boot = TRUE, ci = 0.95,
                      runs = 100, seed = NULL)
plot(impulso_lpsb_legq)

##Decomposição do erro da variância
fevd(varvec, n.ahead = 5)$legq*100
##          ltxc     lpgi     lpsb     legq
## [1,] 60.53591 13.44869 1.329510 24.68589
## [2,] 58.92819 13.72240 5.480429 21.86898
## [3,] 54.78915 16.84394 7.961335 20.40557
## [4,] 56.08863 17.25873 9.193670 17.45897
## [5,] 56.67688 18.11762 8.296811 16.90869
fevd(varvec, n.ahead = 5)$ltxc*100
##           ltxc      lpgi      lpsb      legq
## [1,] 100.00000  0.000000  0.000000 0.0000000
## [2,]  95.54069  2.615012  1.637906 0.2063868
## [3,]  80.30197 12.394109  4.051867 3.2520571
## [4,]  67.96598 18.700156 10.455921 2.8779439
## [5,]  60.83345 26.253177 10.072762 2.8406158
fevd(varvec, n.ahead = 5)$lpgi*100
##          ltxc     lpgi      lpsb     legq
## [1,] 15.09406 84.90594  0.000000 0.000000
## [2,] 13.53913 82.16223  2.693134 1.605502
## [3,] 13.44498 81.33231  2.959053 2.263660
## [4,] 14.90590 75.19636  8.064458 1.833281
## [5,] 17.89786 68.47191 12.109412 1.520818
fevd(varvec, n.ahead = 5)$lpsb*100
##          ltxc     lpgi      lpsb      legq
## [1,] 65.04611 31.20022  3.753671 0.0000000
## [2,] 51.26181 40.91575  7.613079 0.2093539
## [3,] 56.39765 28.49798 14.027955 1.0764222
## [4,] 57.82171 26.47349 13.629626 2.0751734
## [5,] 55.67555 28.72184 12.906914 2.6956962
##Ajustamento
#Heterocedasticia
ARCH<-arch.test(varvec,lags.single = 4,lags.multi=2,multivariate.only=FALSE)
ARCH
## $`resids of ltxc`
## 
##  ARCH test (univariate)
## 
## data:  Residual of resids of ltxc equation
## Chi-squared = 14.988, df = 4, p-value = 0.004727
## 
## 
## $`resids of lpgi`
## 
##  ARCH test (univariate)
## 
## data:  Residual of resids of lpgi equation
## Chi-squared = 8.4084, df = 4, p-value = 0.07771
## 
## 
## $`resids of lpsb`
## 
##  ARCH test (univariate)
## 
## data:  Residual of resids of lpsb equation
## Chi-squared = 1.1823, df = 4, p-value = 0.881
## 
## 
## $`resids of legq`
## 
##  ARCH test (univariate)
## 
## data:  Residual of resids of legq equation
## Chi-squared = 7.066, df = 4, p-value = 0.1324
## 
## 
## 
##  ARCH (multivariate)
## 
## data:  Residuals of VAR object varvec
## Chi-squared = 189.13, df = 200, p-value = 0.6986
#Autocorrelação
vars::serial.test(varvec, type=c('PT.asymptotic'))
## 
##  Portmanteau Test (asymptotic)
## 
## data:  Residuals of VAR object varvec
## Chi-squared = 171.81, df = 212, p-value = 0.9802
#Normalidade
vars::normality.test(varvec, multivariate.only=TRUE)
## $JB
## 
##  JB-Test (multivariate)
## 
## data:  Residuals of VAR object varvec
## Chi-squared = 13.011, df = 8, p-value = 0.1115
## 
## 
## $Skewness
## 
##  Skewness only (multivariate)
## 
## data:  Residuals of VAR object varvec
## Chi-squared = 10.717, df = 4, p-value = 0.02993
## 
## 
## $Kurtosis
## 
##  Kurtosis only (multivariate)
## 
## data:  Residuals of VAR object varvec
## Chi-squared = 2.2943, df = 4, p-value = 0.6818
##Exogeneidade
#Fraca --> Maioria dos alfas estatisticamente não-nulos
#Forte --> Um alfa estatisticamente nulo --> Teste de Granger-Causalidade:
grangertest(ltxc,legq,order=1)
## Granger causality test
## 
## Model 1: legq ~ Lags(legq, 1:1) + Lags(ltxc, 1:1)
## Model 2: legq ~ Lags(legq, 1:1)
##   Res.Df Df     F  Pr(>F)  
## 1     30                   
## 2     31 -1 3.066 0.09016 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
grangertest(lpgi,legq,order=1)
## Granger causality test
## 
## Model 1: legq ~ Lags(legq, 1:1) + Lags(lpgi, 1:1)
## Model 2: legq ~ Lags(legq, 1:1)
##   Res.Df Df      F Pr(>F)
## 1     30                 
## 2     31 -1 0.7683 0.3877
grangertest(lpsb,legq,order=1)
## Granger causality test
## 
## Model 1: legq ~ Lags(legq, 1:1) + Lags(lpsb, 1:1)
## Model 2: legq ~ Lags(legq, 1:1)
##   Res.Df Df      F  Pr(>F)  
## 1     30                    
## 2     31 -1 3.7747 0.06147 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
var <- VAR(dados_ts, p = 1, type = "const", season = NULL, exogen = NULL)
causality(var, cause = "ltxc", vcov. = vcovHC(var))
## $Granger
## 
##  Granger causality H0: ltxc do not Granger-cause lpgi lpsb legq
## 
## data:  VAR object var
## F-Test = 0.71164, df1 = 3, df2 = 112, p-value = 0.547
## 
## 
## $Instant
## 
##  H0: No instantaneous causality between: ltxc and lpgi lpsb legq
## 
## data:  VAR object var
## Chi-squared = 16.419, df = 3, p-value = 0.0009302
causality(var, cause = "lpgi", vcov. = vcovHC(var))
## $Granger
## 
##  Granger causality H0: lpgi do not Granger-cause ltxc lpsb legq
## 
## data:  VAR object var
## F-Test = 0.45772, df1 = 3, df2 = 112, p-value = 0.7124
## 
## 
## $Instant
## 
##  H0: No instantaneous causality between: lpgi and ltxc lpsb legq
## 
## data:  VAR object var
## Chi-squared = 15.928, df = 3, p-value = 0.001173
causality(var, cause = "lpsb", vcov. = vcovHC(var))
## $Granger
## 
##  Granger causality H0: lpsb do not Granger-cause ltxc lpgi legq
## 
## data:  VAR object var
## F-Test = 0.74059, df1 = 3, df2 = 112, p-value = 0.53
## 
## 
## $Instant
## 
##  H0: No instantaneous causality between: lpsb and ltxc lpgi legq
## 
## data:  VAR object var
## Chi-squared = 16.425, df = 3, p-value = 0.0009276
##PREVISÃO 
predict(varvec, n.ahead = 5, ci = 0.95, dumvar = NULL)
## $ltxc
##          fcst     lower    upper        CI
## [1,] 1.406332 1.0553447 1.757320 0.3509877
## [2,] 1.411509 0.9427035 1.880314 0.4688054
## [3,] 1.223078 0.5612847 1.884871 0.6617932
## [4,] 1.437568 0.6276292 2.247507 0.8099388
## [5,] 1.510488 0.6077739 2.413202 0.9027138
## 
## $lpgi
##          fcst    lower    upper        CI
## [1,] 3.190535 2.959480 3.421590 0.2310548
## [2,] 3.068945 2.650155 3.487735 0.4187902
## [3,] 3.422752 2.916033 3.929472 0.5067196
## [4,] 3.531599 2.965157 4.098040 0.5664414
## [5,] 3.454026 2.822880 4.085173 0.6311464
## 
## $lpsb
##          fcst    lower    upper        CI
## [1,] 4.552338 4.175367 4.929309 0.3769713
## [2,] 4.402563 3.911890 4.893235 0.4906721
## [3,] 4.599371 4.008663 5.190079 0.5907080
## [4,] 4.905638 4.265407 5.545869 0.6402313
## [5,] 4.898010 4.235884 5.560135 0.6621254
## 
## $legq
##          fcst    lower    upper        CI
## [1,] 18.34261 18.05241 18.63282 0.2902052
## [2,] 18.40954 18.07211 18.74696 0.3374223
## [3,] 18.42615 18.05724 18.79506 0.3689129
## [4,] 18.57016 18.13832 19.00200 0.4318390
## [5,] 18.56540 18.10274 19.02805 0.4626558