Limpando a base de dados

rm(list=ls())
install.packages("readxl", repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'readxl' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'readxl'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying C:
## \Users\Thomas\AppData\Local\R\win-library\4.2\00LOCK\readxl\libs\x64\readxl.dll
## to C:\Users\Thomas\AppData\Local\R\win-library\4.2\readxl\libs\x64\readxl.dll:
## Permission denied
## Warning: restored 'readxl'
## 
## The downloaded binary packages are in
##  C:\Users\Thomas\AppData\Local\Temp\Rtmp2FdEYf\downloaded_packages
install.packages("dplyr", repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'dplyr' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'dplyr'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying C:
## \Users\Thomas\AppData\Local\R\win-library\4.2\00LOCK\dplyr\libs\x64\dplyr.dll
## to C:\Users\Thomas\AppData\Local\R\win-library\4.2\dplyr\libs\x64\dplyr.dll:
## Permission denied
## Warning: restored 'dplyr'
## 
## The downloaded binary packages are in
##  C:\Users\Thomas\AppData\Local\Temp\Rtmp2FdEYf\downloaded_packages
install.packages('tidyverse', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'tidyverse' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\Thomas\AppData\Local\Temp\Rtmp2FdEYf\downloaded_packages
install.packages('BETS', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'BETS' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\Thomas\AppData\Local\Temp\Rtmp2FdEYf\downloaded_packages
install.packages('urca', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'urca' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'urca'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying C:
## \Users\Thomas\AppData\Local\R\win-library\4.2\00LOCK\urca\libs\x64\urca.dll
## to C:\Users\Thomas\AppData\Local\R\win-library\4.2\urca\libs\x64\urca.dll:
## Permission denied
## Warning: restored 'urca'
## 
## The downloaded binary packages are in
##  C:\Users\Thomas\AppData\Local\Temp\Rtmp2FdEYf\downloaded_packages
install.packages('TSA', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'TSA' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\Thomas\AppData\Local\Temp\Rtmp2FdEYf\downloaded_packages
install.packages('lmtest', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'lmtest' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'lmtest'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying C:
## \Users\Thomas\AppData\Local\R\win-library\4.2\00LOCK\lmtest\libs\x64\lmtest.dll
## to C:\Users\Thomas\AppData\Local\R\win-library\4.2\lmtest\libs\x64\lmtest.dll:
## Permission denied
## Warning: restored 'lmtest'
## 
## The downloaded binary packages are in
##  C:\Users\Thomas\AppData\Local\Temp\Rtmp2FdEYf\downloaded_packages
install.packages('aTSA', repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'aTSA' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\Thomas\AppData\Local\Temp\Rtmp2FdEYf\downloaded_packages
#install.packages('forecast', repos = "http://cran.us.r-project.org")
install.packages("vars", repos = "http://cran.us.r-project.org")
## Installing package into 'C:/Users/Thomas/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'vars' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\Thomas\AppData\Local\Temp\Rtmp2FdEYf\downloaded_packages
library(readxl)
## Warning: package 'readxl' was built under R version 4.2.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.2
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ stringr 1.4.1
## ✔ tidyr   1.2.1     ✔ forcats 0.5.2
## ✔ readr   2.1.3
## Warning: package 'readr' was built under R version 4.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(BETS)
## Warning: package 'BETS' was built under R version 4.2.2
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo 
## 
## Attaching package: 'BETS'
## 
## The following object is masked from 'package:stats':
## 
##     predict
library(urca)
## Warning: package 'urca' was built under R version 4.2.2
library(TSA)
## Warning: package 'TSA' was built under R version 4.2.2
## Registered S3 methods overwritten by 'TSA':
##   method       from    
##   fitted.Arima forecast
##   plot.Arima   forecast
## 
## Attaching package: 'TSA'
## 
## The following object is masked from 'package:readr':
## 
##     spec
## 
## The following objects are masked from 'package:stats':
## 
##     acf, arima
## 
## The following object is masked from 'package:utils':
## 
##     tar
library(lmtest)
## Warning: package 'lmtest' was built under R version 4.2.2
## Carregando pacotes exigidos: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(aTSA)
## 
## Attaching package: 'aTSA'
## 
## The following object is masked from 'package:graphics':
## 
##     identify
#library(forecast)
library(vars)
## Warning: package 'vars' was built under R version 4.2.2
## Carregando pacotes exigidos: MASS
## 
## Attaching package: 'MASS'
## 
## The following object is masked from 'package:dplyr':
## 
##     select
## 
## Carregando pacotes exigidos: strucchange
## Carregando pacotes exigidos: sandwich
## 
## Attaching package: 'strucchange'
## 
## The following object is masked from 'package:stringr':
## 
##     boundary
## 
## 
## Attaching package: 'vars'
## 
## The following object is masked from 'package:aTSA':
## 
##     arch.test

Base de dados.

Importação da base e ajuste de linhas e cabeçalho.

df_projeto <- read_excel("C:/Users/Thomas/Desktop/df projeto.xlsx")
## New names:
## • `` -> `...1`
## • `` -> `...5`
## • `` -> `...6`
## • `` -> `...7`
## • `` -> `...8`
## • `` -> `...9`
## • `` -> `...10`
## • `` -> `...11`
## • `` -> `...12`
## • `` -> `...13`
## • `` -> `...14`
colnames(df_projeto) <- as.character(df_projeto[1,])
df_co <- df_projeto %>% slice(-1)
tsco <- ts(df_co, start=c(2012, 03), end=c(2022, 08), frequency=12)

(tsco) %>%
  as_tibble() %>%
  mutate(date = seq(as.Date('2012-03-01'), as.Date('2022-08-01'),
                    by = '1 month')) %>%
  gather(variavel, valor, -date) %>%
  ggplot(aes(x=date, y=valor, colour=variavel))+
  geom_line()+
  facet_wrap(~variavel, scales = 'free')+
  theme(legend.position = 'none')

Graficamente temos o indicativo de que Taxa de desemprego esta correlacionada com Selic e Crédito, vou analisar a estacionariedade destas.

Vou filtrar os fatores que vou analizar, taxa de desemprego, Selic, Crédito e Anfavea.

df_co2 <- df_co %>%
            dplyr::select("Taxa Desemprego", "Selic", 6, 13) %>%
            as_tibble()

tsco2 <- ts(df_co2, start=c(2012, 03), end=c(2022, 08), frequency=12)

Teste de estacionariedade adf para desemprego.

adf.tred_desemprego <- ur.df(tsco2 [, 'Taxa Desemprego'], type = c('trend'), lags = 12, selectlags = "AIC")

adf.tred_desemprego@teststat
##                tau3     phi2     phi3
## statistic -2.672541 2.720885 4.080891
adf.tred_desemprego@cval
##       1pct  5pct 10pct
## tau3 -3.99 -3.43 -3.13
## phi2  6.22  4.75  4.07
## phi3  8.43  6.49  5.47

Teste de estacionariedade adf para Selic.

adf.tred_Selic <- ur.df(tsco2 [, 'Selic'], type = c('trend'), lags = 12, selectlags = "AIC")

adf.tred_Selic@teststat
##               tau3     phi2     phi3
## statistic -3.29203 3.656447 5.431711
adf.tred_Selic@cval
##       1pct  5pct 10pct
## tau3 -3.99 -3.43 -3.13
## phi2  6.22  4.75  4.07
## phi3  8.43  6.49  5.47

Teste de estacionariedade adf para para crédito.

adf.tred_credito <- ur.df(tsco2 [, 3], type = c('trend'), lags = 12, selectlags = "AIC")

adf.tred_credito@teststat
##                tau3     phi2     phi3
## statistic -2.557338 2.659974 3.985324
adf.tred_credito@cval
##       1pct  5pct 10pct
## tau3 -3.99 -3.43 -3.13
## phi2  6.22  4.75  4.07
## phi3  8.43  6.49  5.47

Teste de estacionariedade adf para para Anfavea.

adf.tred_anfavea <- ur.df(tsco2 [, 4], type = c('trend'), lags = 12, selectlags = "AIC")

adf.tred_anfavea@teststat
##                tau3     phi2     phi3
## statistic -2.873489 2.904869 4.310253
adf.tred_anfavea@cval
##       1pct  5pct 10pct
## tau3 -3.99 -3.43 -3.13
## phi2  6.22  4.75  4.07
## phi3  8.43  6.49  5.47

Não é estacionária.

Somente Selic é estacionária a 10% de confiança, se fossemos estimar um VAR eu teria de deixar tudo estacionário. Mas como quero analisar se há cointegração não é preciso, entretanto não posso analisar uma série estacionária com series não estacionárias, pois com certeza o erro será não estacionário. Portanto não vou testar a cointegração para SElic mas sim só para Taxa de desemprego e crádito.

plot(tsco2, main = "Variáveis")

df_co3 <- df_co %>%
            dplyr::select("Taxa Desemprego", 6) %>%
            as_tibble()
tsco3 <- ts(df_co3, start=c(2012, 03), end=c(2022, 08), frequency=12)

lagselect1 <- VARselect(tsco3, lag.max = 12, type = "const")
lagselect1$selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      4      1      1      4

Já que 3 foi escolhido, vou usar 4 - 1 = 3 como lag.

Johansen test

ctest1 <- ca.jo(tsco3, type = "trace", ecdet = "const", K = 3)
summary(ctest1)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: trace statistic , without linear trend and constant in cointegration 
## 
## Eigenvalues (lambda):
## [1] 1.002926e-01 2.737834e-02 4.163336e-17
## 
## Values of teststatistic and critical values of test:
## 
##           test 10pct  5pct  1pct
## r <= 1 |  3.41  7.52  9.24 12.97
## r = 0  | 16.41 17.85 19.96 24.60
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##                    Taxa.Desemprego.l3   Crédito.l3    constant
## Taxa.Desemprego.l3          1.0000000    1.0000000    1.000000
## Crédito.l3                 -0.5282064    0.6452439    4.469032
## constant                  -19.9364403 -197.9820988 -273.347354
## 
## Weights W:
## (This is the loading matrix)
## 
##                   Taxa.Desemprego.l3   Crédito.l3     constant
## Taxa.Desemprego.d        -0.14017819 -0.011763973 1.515794e-17
## Crédito.d                 0.02379119 -0.007855384 5.980424e-18

Utilizando o lag 3, vimos que não há raiz de cointegração para Taxa de desemprego e crédito, ou seja, não há uma relação linar entre as duas e uma regressão entre elas será espúria. Decido fazer tambem para anfavea pois ela aparenta possuir alguma relação com taxa de desemprego.

df_co4 <- df_co %>%
            dplyr::select("Taxa Desemprego", 13) %>%
            as_tibble()
tsco4 <- ts(df_co4, start=c(2012, 03), end=c(2022, 08), frequency=12)

lagselect2 <- VARselect(tsco4, lag.max = 12, type = "const")
lagselect2$selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      3      1      1      3

Vou usar lag 3 - 1 = 2.q

ctest2 <- ca.jo(tsco4, type = "trace", ecdet = "const", K = 2)
summary(ctest2)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: trace statistic , without linear trend and constant in cointegration 
## 
## Eigenvalues (lambda):
## [1] 1.190861e-01 5.693635e-02 1.387779e-17
## 
## Values of teststatistic and critical values of test:
## 
##           test 10pct  5pct  1pct
## r <= 1 |  7.27  7.52  9.24 12.97
## r = 0  | 22.99 17.85 19.96 24.60
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##                    Taxa.Desemprego.l2  Anfavea.l2    constant
## Taxa.Desemprego.l2           1.000000   1.0000000    1.000000
## Anfavea.l2                  -1.645406   0.3556583   -1.669615
## constant                    39.726877 -85.1233540 1771.466654
## 
## Weights W:
## (This is the loading matrix)
## 
##                   Taxa.Desemprego.l2  Anfavea.l2      constant
## Taxa.Desemprego.d        -0.03637831 -0.09298447 -1.241200e-18
## Anfavea.d                 0.15065517 -0.06098640  2.980365e-18

Obtemos o mesmo resultado, não há cointegração.

Vou agora testar para todas as variáveis

df_co5 <- df_co %>%
            dplyr::select("Taxa Desemprego", 6, 13) %>%
            as_tibble()
tsco5 <- ts(df_co5, start=c(2012, 03), end=c(2022, 08), frequency=12)

lagselect3 <- VARselect(tsco5, lag.max = 12, type = "const")
lagselect3$selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      1      1      1      1

Como sugerido lag = 1 vou usar 2 pois o comando determina no minimo 2 lags.

ctest3 <- ca.jo(tsco5, type = "trace", ecdet = "const", K = 2)
summary(ctest3)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: trace statistic , without linear trend and constant in cointegration 
## 
## Eigenvalues (lambda):
## [1] 2.198610e-01 9.609046e-02 1.904313e-02 6.071532e-18
## 
## Values of teststatistic and critical values of test:
## 
##           test 10pct  5pct  1pct
## r <= 2 |  2.38  7.52  9.24 12.97
## r <= 1 | 14.91 17.85 19.96 24.60
## r = 0  | 45.70 32.00 34.91 41.07
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##                    Taxa.Desemprego.l2  Crédito.l2   Anfavea.l2     constant
## Taxa.Desemprego.l2          1.0000000   1.0000000    1.0000000    1.0000000
## Crédito.l2                 -0.9251317  -0.4955454    1.1675076    5.2710109
## Anfavea.l2                 -1.6379626   0.2764560    0.6628448   -0.4046007
## constant                   92.6693809 -33.3869695 -266.9388669 -261.8325892
## 
## Weights W:
## (This is the loading matrix)
## 
##                   Taxa.Desemprego.l2  Crédito.l2   Anfavea.l2      constant
## Taxa.Desemprego.d        -0.06634659 -0.09720314 -0.010610520 -1.727542e-17
## Crédito.d                -0.03128631  0.02373821 -0.004065809 -1.895841e-18
## Anfavea.d                 0.23293345 -0.01598534 -0.019114583  4.248215e-17