Explicando o comportamento de depósitos totais (Depósito à vista, Depósito a Prazo e Poupança) do Brasil nos ultimos cinco anos uitlizando o PIB, Selic, Inflação e Desemprego

Origem dos dados utilizados

Neste relatório, os dados utilizados foram obtidos a partir das bibliotecas sidrar , ipeaDataR, GetBCBData e rvest

Caso não tenha instalado a biblioteca, será necessário descomentar comando comando install.packages(“sidrar”) e install.packages(“ipeadatar”) no código R abaixo:

# install.packages("sidrar")
# install.packages("ipeadatar")
# install.packages("GetBCBData")
# install.packages("lubridate")
# install.packages("ts")
# install.packages("XML")
# install.packages("rvest")
# install.packages("bcb")
# library(readxl)
# library(tseries)
# library(urca)
# library(vars)
# library(lmtest)
# library(tsDyn)
# rm(list = ls()) # código limpeza do ambiente

Definindo o timeframe da série de dados

# Definir as datas inicial e hoje dinâmicamente sempre que o código for atualizado

rm(list=ls());
DataInicial <- Sys.Date() - (365*8)  # calculada a data de  anos atrás 
DataHoje <- Sys.Date()  # Recebe do sistema do dia atual
DataHoje
## [1] "2023-05-16"
DataFiltro <- "2022-12-01" #  necessário porque o PIB esta apenas disponível até o ano de 2022 com dados mensais completos. 

Consultando tabelas de variáveis do pacote Sidrar

Para utilização do pacote Sidrar é necessário passar como parâmetro uma tabela de referência.

Esta pode ser obtida pelo comando search_sidrar(c(Busca)). O método utilizado permite ao pesquisador produzir uma lista de tabelas para extrair os dados que comporão as séries históricas.

De modo análogo, __search_sidra c((“Busca”)) pode ser utilizado para o pacote sidrar.

Obtendo e tratando os dados da SELIC

library(ipeadatar)
#ls('package:ipeadatar')

# Procurando tabela com dados de inflação. 
search_series(c('Taxa de juros - Over / Selic - acumulada no mês'))
## Warning: `filter_()` was deprecated in dplyr 0.7.0.
## ℹ Please use `filter()` instead.
## ℹ See vignette('programming') for more help
## ℹ The deprecated feature was likely used in the ipeadatar package.
##   Please report the issue at
##   <https://github.com/gomesleduardo/ipeadatar/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## # A tibble: 1 × 7
##   code          name                        theme source freq  lastupdate status
##   <chr>         <chr>                       <fct> <fct>  <fct> <date>     <fct> 
## 1 BM12_TJOVER12 Taxa de juros - Over / Sel… Macr… Bacen… Mont… 2023-05-16 Active
#  selecionando os dados do ipeadata
SELIC <- ipeadata("BM12_TJOVER12")

# aplicando o timeframe desejado
SELIC <- subset(SELIC, date >= DataInicial & date <= DataFiltro)

# Adicionar uma coluna com a marcação do trimestre
SELIC$Tri <- quarters(SELIC$date)

# Adicionar uma coluna com a marcação do ano
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
SELIC$Ano <- year(as.Date(SELIC$date))

# Utilizando a mediana das médias mensais, agregando e ordenando
SELIC <- aggregate(value ~ Ano + Tri, data = SELIC, FUN = mean)
SELIC <- SELIC[order(SELIC$Ano, SELIC$Tri), ]
data_SELIC_in <- as.Date("2015-04-01")
data_SELIC_fim <-  as.Date("2022-10-01")

# Adicionando a sequência trimestral ao dataframe. 
seq <- seq(data_SELIC_in, data_SELIC_fim, by = "3 months")
SELIC$date <- c(seq)


# Formantando SELIC para tamanho de dados desejados.
SELIC <- subset(SELIC, date >= "2016-07-01" & date <= DataFiltro)
head(SELIC, 1)
##     Ano Tri    value       date
## 17 2016  Q3 1.146667 2016-07-01
tail(SELIC,1)
##     Ano Tri    value       date
## 31 2022  Q4 1.053333 2022-10-01
summary(SELIC)
##       Ano           Tri                value             date           
##  Min.   :2016   Length:26          Min.   :0.1567   Min.   :2016-07-01  
##  1st Qu.:2018   Class :character   1st Qu.:0.4100   1st Qu.:2018-01-23  
##  Median :2019   Mode  :character   Median :0.5233   Median :2019-08-16  
##  Mean   :2019                      Mean   :0.6027   Mean   :2019-08-16  
##  3rd Qu.:2021                      3rd Qu.:0.8342   3rd Qu.:2021-03-09  
##  Max.   :2022                      Max.   :1.1467   Max.   :2022-10-01
nrow(SELIC)
## [1] 26

Baixando os Dados da Inflação

library(ipeadatar)
# Procurando tabela com dados de inflação. 
# search_series(c('IPCA'))
# Dentre as possíveis tabelas, escolhi BM12_IPCA20N12

#  selecionando os dados do ipeadata
IPCA <- ipeadata("BM12_IPCA20N12")

# aplicando o timeframe desejado
IPCA <- subset(IPCA, date >= DataInicial & date <= DataFiltro)

# Adicionar uma coluna com a marcação do trimestre
IPCA$Tri <- quarters(IPCA$date)

# Adicionar uma coluna com a marcação do ano
library(lubridate)
IPCA$Ano <- year(as.Date(IPCA$date))

# Utilizando a mediana das médias mensais, agregando e ordenando
IPCA <- aggregate(value ~ Ano + Tri, data = IPCA, FUN = mean)
IPCA <- IPCA[order(IPCA$Ano, IPCA$Tri), ]
data_IPCA_in <- as.Date("2015-04-01")
data_IPCA_fim <-  as.Date("2022-10-01")

# Adicionando a sequência trimestral ao dataframe. 
seq <- seq(data_IPCA_in, data_IPCA_fim, by = "3 months")
IPCA$date <- c(seq)

# Formantando IPCA para tamanho de dados desejados.
IPCA <- subset(IPCA, date >= "2016-07-01" & date <= DataFiltro)
head(IPCA, 1)
##     Ano Tri value       date
## 17 2016  Q3  0.33 2016-07-01
tail(IPCA, 1)
##     Ano Tri     value       date
## 31 2022  Q4 0.4166667 2022-10-01
summary(IPCA)
##       Ano           Tri                value              date           
##  Min.   :2016   Length:26          Min.   :0.04667   Min.   :2016-07-01  
##  1st Qu.:2018   Class :character   1st Qu.:0.22167   1st Qu.:2018-01-23  
##  Median :2019   Mode  :character   Median :0.30000   Median :2019-08-16  
##  Mean   :2019                      Mean   :0.35987   Mean   :2019-08-16  
##  3rd Qu.:2021                      3rd Qu.:0.46000   3rd Qu.:2021-03-09  
##  Max.   :2022                      Max.   :0.86000   Max.   :2022-10-01
nrow(IPCA)
## [1] 26

Baixando dados para o nível de Desemprego

# Baixando os dados com início em 2016
library(sidrar)

# Caminho da tabela definida em 'sidra.ibge.gov.br/tabela/6379'
cod_sidraDes <- '/t/6397/n1/all/v/4099/p/last%2024/c58/95253/d/v4099%201'

# Baixando as infos da tabela pela API 
IBGE <- sidrar::get_sidra(api = cod_sidraDes)
## All others arguments are desconsidered when 'api' is informed
tail(IBGE,1)
##    Nível Territorial (Código) Nível Territorial Unidade de Medida (Código)
## 25                          1            Brasil                          2
##    Unidade de Medida Valor Brasil (Código) Brasil Variável (Código)
## 25                 %   7.9               1 Brasil              4099
##                                                                                 Variável
## 25 Taxa de desocupação, na semana de referência, das pessoas de 14 anos ou mais de idade
##    Trimestre (Código)         Trimestre Grupo de idade (Código) Grupo de idade
## 25             202204 4º trimestre 2022                   95253          Total
# convertendo a coluna Trimestre para formato data válido
library(dplyr)
## 
## 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
#Obtendo os anos
ano <- substr(IBGE$Trimestre, nchar(IBGE$Trimestre) - 3, nchar(IBGE$Trimestre))

# Obtendo os trimestres
trimestre <- substr(IBGE$Trimestre, 1, 1)


# Substituindo strings 1, 2, 3 4, respectivamente, pelos meses 01, 04, 07 e 10
mes <- ifelse(trimestre == "1", "01",
              ifelse(trimestre == "2", "04",
                     ifelse(trimestre == "3", "07",
                            ifelse(trimestre == "4", "10", NA))))

# Criando datas no padrão trimestral
dataDES <- paste(ano, mes, "01", sep = "-")

# Adicionando datas válidas ao dataframe
IBGE$Trimestre <- as.Date(dataDES)
tail(IBGE$Trimestre)
## [1] "2019-07-01" "2019-10-01" "2020-01-01" "2022-04-01" "2022-07-01"
## [6] "2022-10-01"
# Criando dataframe somente com info necessária
DES <- data.frame(Date = IBGE$Trimestre, Des = IBGE$Valor)

# Formatando série trimestral para mesmo tamanho de dados
DES <- subset(DES, Date >= '2016-07-01' & Date <= DataFiltro)
head(DES,1)
##         Date  Des
## 7 2016-07-01 11.9
tail(DES, 1)
##          Date Des
## 24 2022-10-01 7.9
nrow(DES)
## [1] 18
# Aparantemente os daods trimestrais de 2020  e 2021 estão faltantes na base do IBGE. 
# Assim foram manualmente substituidos pelos dados disponívels eim: https://agenciabrasil.ebc.com.br/economia/noticia/2021-03/desemprego-registrou-taxa-media-de-135-em-2020#:~:text=No%20%C3%BAltimo%20trimestre%20de%202020,em%20apenas%20em%20cinco%20estados.

#https://agenciabrasil.ebc.com.br/economia/noticia/2020-08/pnad-desemprego-vai-133-no-segundo-trimestre

# Preenchendo os dados faltantes de 2020
DES <- rbind(DES,c('2020-04-01', 13.3))
DES <- rbind(DES,c('2020-07-01', 14.6))
DES <- rbind(DES,c('2020-10-01', 13.9))
# Preenchendo os dados faltantes de 2021
DES <- rbind(DES,c('2021-01-01',14.9))
DES <- rbind(DES,c('2021-04-01',12.85))
DES <- rbind(DES,c('2021-07-01',12.6))
DES <- rbind(DES,c('2021-10-01', 11.1))
# Preenchendo primeiro trimestre de 2020
DES <- rbind(DES,c('2022-01-01', 11.1))
# Ficou estável ao tri do ano anterior.

# ordendando novamente a série temporal
DES <- DES[order(DES$Date), ]
nrow(DES)
## [1] 26
tail(DES,5)
##          Date  Des
## 25 2021-10-01 11.1
## 26 2022-01-01 11.1
## 22 2022-04-01  9.3
## 23 2022-07-01  8.7
## 24 2022-10-01  7.9

Baixando dados do PIB direto da tabela do IPEAData via Webscrapping

library(rvest)

# Local dos quais dados serão baixados
url <- "http://www.ipeadata.gov.br/ExibeSerie.aspx?serid=38415"

# Realizar o scraping dos dados
page <- read_html(url)
dados_html <- page %>%
  html_nodes(xpath = '//table[@class="dxgvTable"]') %>%
  html_table(fill = TRUE)
# Converter os dados em um dataframe
PIB <- data.frame(dados_html[[1]])

# Alterando nomes das colunas
PIB <- data.frame(Data = PIB$X1, PIB = PIB$X2)

# Limpando dataframe para somente valores numéricos
PIB <- PIB[4:nrow(PIB), ]

# Convertendo String do tipo XXXX - XT para dado trimestral do tipo data.  
# Exemplo de dado na coluna

# Extrair o ano e o trimestre do dado
anos <- substring(PIB$Data, 1, 4)
trimestre <- substring(PIB$Data, 6, 8)

# Definir o mês baseado no trimestre
meses <- ifelse(trimestre == "T1", "01",
                ifelse(trimestre == "T2", "04",
                       ifelse(trimestre == "T3", "07",
                              ifelse(trimestre == "T4", "10", NA))))

# Criar as datas no formato "yyyy-mm-dd"
datas <- paste(anos, meses, "01", sep = "-")

# Converter coluna de datas para o formato de data trimestral
PIB$Data <- as.Date(datas)

# Aplicando o filtro para os ultimos 5 anos
PIB <- subset(PIB, Data >= '2016-07-01' & Data <= DataFiltro)

# Por fim, convertendo os valores String para strings capazes de serem posteiormente interpretados como numéricos

PIB$PIB <- as.numeric(gsub("\\.", "", PIB$PIB))

tail(PIB, 5)
##           Data     PIB
## 107 2021-10-01 2309564
## 108 2022-01-01 2315709
## 109 2022-04-01 2471837
## 110 2022-07-01 2543645
## 111 2022-10-01 2584126
nrow(PIB)
## [1] 26

Juntando os dados em um único dataframe

#adicionando as variáveis disponíveis para o dataframe dado
dados <- data.frame(Date = as.Date(DES$Date, format = "%Y-%m-%d"),
                    PIBmi = as.numeric(PIB$PIB),
                    IPCA = as.numeric(IPCA$value),
                    DES = as.numeric(DES$Des),
                    SELIC = as.numeric(SELIC$value))
tail(dados, 10)
##          Date   PIBmi      IPCA   DES     SELIC
## 17 2020-07-01 1929703 0.2266667 14.60 0.1700000
## 18 2020-10-01 2048979 0.5366667 13.90 0.1566667
## 19 2021-01-01 2152622 0.4200000 14.90 0.1600000
## 20 2021-04-01 2182049 0.4733333 12.85 0.2633333
## 21 2021-07-01 2254492 0.6600000 12.60 0.4100000
## 22 2021-10-01 2309564 0.7333333 11.10 0.6166667
## 23 2022-01-01 2315709 0.7800000 11.10 0.8066667
## 24 2022-04-01 2471837 0.8600000  9.30 0.9600000
## 25 2022-07-01 2543645 0.4800000  8.70 1.0900000
## 26 2022-10-01 2584126 0.4166667  7.90 1.0533333

Extraindo rendimentos da poupança

library(GetBCBData)

cod_poup <- "7828"
# tabela para a rentabilidade da poupança
rentpoup <- gbcbd_get_series(cod_poup)
## 
## Fetching id = 7828 [7828] from BCB-SGS with cache 
##   Found 121 observations
# Filtrando rendimentos da poupança
rentpoup <- subset(rentpoup, ref.date >= '2016-07-01' & ref.date <= DataFiltro)
rentpoup <- data.frame(data = rentpoup$ref.date, valor = rentpoup$value)

# Extraindo o trimestre e o ano das datas

library(lubridate)

# Converter a coluna de data para o formato de data
rentpoup$data <- as.Date(rentpoup$data)

# Adicionar a coluna de trimestre
rentpoup$trimestre <- quarter(rentpoup$data)

# Adicionar a coluna de ano
rentpoup$ano <- year(rentpoup$data)
head(rentpoup, 5)
##         data  valor trimestre  ano
## 1 2016-07-01 0.6629         3 2016
## 2 2016-08-01 0.7558         3 2016
## 3 2016-09-01 0.6583         3 2016
## 4 2016-10-01 0.6609         4 2016
## 5 2016-11-01 0.6435         4 2016
anos <-  rentpoup$ano
trimestre <- rentpoup$trimestre

# Definir o mês baseado no trimestre
meses <- ifelse(trimestre == "1", "01",
                ifelse(trimestre == "2", "04",
                       ifelse(trimestre == "3", "07",
                              ifelse(trimestre == "4", "10", NA))))

# Criar as datas no formato "yyyy-mm-dd"
datastri <- paste(anos, meses, "01", sep = "-")

# Converter coluna de datas para o formato de data trimestral
rentpoup$DatasTRI <- c(datastri)

# Agrupando pela média
rentpoup <- aggregate(valor ~ datastri, data = rentpoup, FUN = mean)

# juntando rentabilidade da poupança ao dataframe de dados

dados$SavI = c(rentpoup$valor)
tail(dados, 5)
##          Date   PIBmi      IPCA  DES     SELIC      SavI
## 22 2021-10-01 2309564 0.7333333 11.1 0.6166667 0.5163333
## 23 2022-01-01 2315709 0.7800000 11.1 0.8066667 0.5528000
## 24 2022-04-01 2471837 0.8600000  9.3 0.9600000 0.6240000
## 25 2022-07-01 2543645 0.4800000  8.7 1.0900000 0.6958000
## 26 2022-10-01 2584126 0.4166667  7.9 1.0533333 0.6699333

Baixando dados para depósitos totais

library(GetBCBData)

cod_prazo <- "27805" # Depósitos a prazo
#Meios de pagamento amplos - Depósitos a prazo (saldo em final de período) - Novo   u.m.c. (mil)

dep_prazo <- gbcbd_get_series(cod_prazo)
## 
## Fetching id = 27805 [27805] from BCB-SGS with cache 
##   Found 119 observations
head(dep_prazo, 10)
##      ref.date     value id.num series.name
## 1  2013-05-01 654729170  27805  id = 27805
## 2  2013-06-01 661146880  27805  id = 27805
## 3  2013-07-01 648627183  27805  id = 27805
## 4  2013-08-01 640591070  27805  id = 27805
## 5  2013-09-01 636467388  27805  id = 27805
## 6  2013-10-01 632614042  27805  id = 27805
## 7  2013-11-01 629290367  27805  id = 27805
## 8  2013-12-01 631550861  27805  id = 27805
## 9  2014-01-01 627715639  27805  id = 27805
## 10 2014-02-01 623862192  27805  id = 27805
cod_vista <- "27787"    
# Meios de pagamento - Depósitos à vista (saldo em final de período) - Novo u.m.c. (mil)
dep_vista <-  gbcbd_get_series(cod_vista)
## 
## Fetching id = 27787 [27787] from BCB-SGS with cache 
##   Found 119 observations
head(dep_vista,10)
##      ref.date     value id.num series.name
## 1  2013-05-01 164917324  27787  id = 27787
## 2  2013-06-01 170053202  27787  id = 27787
## 3  2013-07-01 172438147  27787  id = 27787
## 4  2013-08-01 167912251  27787  id = 27787
## 5  2013-09-01 169761564  27787  id = 27787
## 6  2013-10-01 168983698  27787  id = 27787
## 7  2013-11-01 171308235  27787  id = 27787
## 8  2013-12-01 187720429  27787  id = 27787
## 9  2014-01-01 178135098  27787  id = 27787
## 10 2014-02-01 170502813  27787  id = 27787
cod_savings <- "1835"
# 1835  Meios de pagamento amplos - Depósito de Poupança (saldo em final de #período)   u.m.c. (mil)
savings <- gbcbd_get_series(cod_savings)
## 
## Fetching id = 1835 [1835] from BCB-SGS with cache 
##   Found 119 observations
head(savings,10)
##      ref.date     value id.num series.name
## 1  2013-05-01 527860458   1835   id = 1835
## 2  2013-06-01 539315239   1835   id = 1835
## 3  2013-07-01 551158753   1835   id = 1835
## 4  2013-08-01 558449217   1835   id = 1835
## 5  2013-09-01 567882037   1835   id = 1835
## 6  2013-10-01 575369015   1835   id = 1835
## 7  2013-11-01 584781093   1835   id = 1835
## 8  2013-12-01 599825987   1835   id = 1835
## 9  2014-01-01 604824604   1835   id = 1835
## 10 2014-02-01 609877474   1835   id = 1835
# aplicando o filtro desejado

savings <- subset(savings, ref.date >= '2016-07-01' & ref.date <= DataFiltro)
dep_vista <- subset(dep_vista, ref.date >= '2016-07-01' & ref.date <= DataFiltro)
dep_prazo <- subset(dep_prazo, ref.date >= '2016-07-01' & ref.date <= DataFiltro)

# verificando a quantidade de linhas nas trÊs tabelas filtradas 
nrow(dep_prazo)
## [1] 78
nrow(savings)
## [1] 78
nrow(dep_vista)
## [1] 78
# Formatando para série trimestral
library(lubridate)

anos <- year(savings$ref.date)
trimestre <- quarters(savings$ref.date)

# Definir o mês baseado no trimestre
meses <- ifelse(trimestre == "Q1", "01",
                ifelse(trimestre == "Q2", "04",
                       ifelse(trimestre == "Q3", "07",
                              ifelse(trimestre == "Q4", "10", NA))))

# Criar as datas no formato "yyyy-mm-dd"
datastri <- paste(anos, meses, "01", sep = "-")

# Converter coluna de datas para o formato de data trimestral
savings$DatasTRI <- c(datastri)
dep_prazo$DatasTRI <- c(datastri)
dep_vista$DatasTRI <- c(datastri)

savings <- aggregate(value ~ DatasTRI, data = savings, FUN = mean)
dep_prazo <- aggregate(value ~ DatasTRI, data = dep_prazo, FUN = mean)
dep_vista <- aggregate(value ~ DatasTRI, data = dep_vista, FUN = mean)

dados <- cbind(dados, Savings = savings$value, DepVista = dep_vista$value, DepPrazo = dep_prazo$value)

dados$DepTotais <- rowSums(data.frame(dados$DepPrazo, dados$DepVista, dados$Savings))
head(dados, 10)
##          Date   PIBmi      IPCA  DES     SELIC      SavI   Savings  DepVista
## 1  2016-07-01 1577172 0.3300000 11.9 1.1466667 0.6923333 644299807 148085053
## 2  2016-10-01 1632766 0.3566667 12.2 1.0700000 0.6634000 656256520 156864085
## 3  2017-01-01 1585585 0.3066667 13.9 1.0033333 0.6179667 663075626 153017312
## 4  2017-04-01 1630638 0.2133333 13.1 0.8433333 0.5435667 670974684 153672040
## 5  2017-07-01 1648631 0.1800000 12.5 0.7466667 0.5379333 690841486 155032100
## 6  2017-10-01 1720626 0.2966667 11.9 0.5833333 0.5000000 710506319 163379596
## 7  2018-01-01 1682460 0.1733333 13.2 0.5266667 0.5000000 729469370 163855257
## 8  2018-04-01 1734454 0.2133333 12.6 0.5200000 0.5000000 745119657 168568061
## 9  2018-07-01 1767868 0.2433333 12.0 0.5266667 0.5000000 768784127 173730991
## 10 2018-10-01 1819359 0.2266667 11.7 0.5066667 0.5000000 787989308 177216453
##     DepPrazo  DepTotais
## 1  659675119 1452059979
## 2  684942790 1498063395
## 3  711482590 1527575528
## 4  789986433 1614633157
## 5  826600944 1672474529
## 6  838968408 1712854323
## 7  843486825 1736811452
## 8  895058350 1808746067
## 9  942237342 1884752460
## 10 988249872 1953455632

Colocando variáveis monetárias em índices

# Calculando índices para linhas.
PIBper <-  (dados$PIBmi[-1]/dados$PIBmi[-length(dados$PIBmi)])
Savingsper <- (dados$Savings[-1]/dados$Savings[-length(dados$Savings)])
DepVistaper <- (dados$DepVista[-1]/dados$DepVista[-length(dados$DepVista)])
DepPrazoper<- (dados$DepPrazo[-1]/dados$DepPrazo[-length(dados$DepPrazo)]) 
DepTotaisPer<- (dados$DepTotais[-1]/dados$DepTotais[-length(dados$DepTotais)])  

# Preenchendo linha 1 com NA e demais com valores calculados.

dados$PIBper <- c(NA, PIBper)
dados$Savingsper <- c(NA, Savingsper)
dados$DepVistaper <- c(NA, DepVistaper)
dados$DepPrazoPer <- c(NA, DepPrazoper)
dados$DepTotaisPer <- c(NA, DepTotaisPer)
# remover NA

tail(dados)
##          Date   PIBmi      IPCA  DES     SELIC      SavI    Savings  DepVista
## 21 2021-07-01 2254492 0.6600000 12.6 0.4100000 0.5000000 1041261108 336213904
## 22 2021-10-01 2309564 0.7333333 11.1 0.6166667 0.5163333 1031859757 327375110
## 23 2022-01-01 2315709 0.7800000 11.1 0.8066667 0.5528000 1019602095 317914538
## 24 2022-04-01 2471837 0.8600000  9.3 0.9600000 0.6240000 1012782008 325542610
## 25 2022-07-01 2543645 0.4800000  8.7 1.0900000 0.6958000  999654435 323934234
## 26 2022-10-01 2584126 0.4166667  7.9 1.0533333 0.6699333  992574633 320599942
##      DepPrazo  DepTotais   PIBper Savingsper DepVistaper DepPrazoPer
## 21 1856329988 3233805001 1.033200  1.0131938   1.1079276   1.0486938
## 22 1886302951 3245537818 1.024428  0.9909712   0.9737108   1.0161464
## 23 1886285508 3223802141 1.002661  0.9881208   0.9711017   0.9999908
## 24 2013643228 3351967846 1.067421  0.9933110   1.0239941   1.0675177
## 25 2162789487 3486378156 1.029050  0.9870381   0.9950594   1.0740679
## 26 2263481742 3576656317 1.015915  0.9929178   0.9897069   1.0465567
##    DepTotaisPer
## 21    1.0427259
## 22    1.0036282
## 23    0.9933029
## 24    1.0397561
## 25    1.0400989
## 26    1.0258945
dados <- na.omit(dados)
head(dados, 5)
##         Date   PIBmi      IPCA  DES     SELIC      SavI   Savings  DepVista
## 2 2016-10-01 1632766 0.3566667 12.2 1.0700000 0.6634000 656256520 156864085
## 3 2017-01-01 1585585 0.3066667 13.9 1.0033333 0.6179667 663075626 153017312
## 4 2017-04-01 1630638 0.2133333 13.1 0.8433333 0.5435667 670974684 153672040
## 5 2017-07-01 1648631 0.1800000 12.5 0.7466667 0.5379333 690841486 155032100
## 6 2017-10-01 1720626 0.2966667 11.9 0.5833333 0.5000000 710506319 163379596
##    DepPrazo  DepTotais    PIBper Savingsper DepVistaper DepPrazoPer
## 2 684942790 1498063395 1.0352492   1.018558    1.059284    1.038303
## 3 711482590 1527575528 0.9711036   1.010391    0.975477    1.038747
## 4 789986433 1614633157 1.0284141   1.011913    1.004279    1.110338
## 5 826600944 1672474529 1.0110343   1.029609    1.008850    1.046348
## 6 838968408 1712854323 1.0436696   1.028465    1.053844    1.014962
##   DepTotaisPer
## 2     1.031681
## 3     1.019700
## 4     1.056991
## 5     1.035823
## 6     1.024144

Inspeção visual dos dados!

par(mfrow=c(2,3))

plot(dados$Date, dados$PIBmi, col = 'black', type = 'l', 
     ylim = c(min(dados$PIBmi), max(dados$PIBmi)), 
     main = "PIB", xlab = "Data", ylab = "PIB")
     
plot(dados$Date, dados$SELIC, col = 'black', type = 'l', 
     ylim = c(min(dados$SELIC), max(dados$SELIC)), 
     main = "Taxa de juros - Selic", xlab = "Data", ylab = "SELIC")

plot(dados$Date, dados$DES, col = 'black', type = 'l', 
     ylim = c(min(dados$DES), max(dados$DES)), 
     main = "Desemprego", xlab = "Data", ylab = "Desemprego")

plot(dados$Date, dados$IPCA, col = 'black', type = 'l', 
     ylim = c(min(dados$IPCA), max(dados$IPCA)), 
     main = "IPCA", xlab = "Data", ylab = "IPCA")

plot(dados$Date, dados$DepTotais, col = 'black', type = 'l', 
        ylim = c(min(dados$DepTotais), max(dados$DepTotais)), 
        main = "Depósitos totais", xlab = "Data", ylab = "Depósitos totais")

Inspeção visuais dos dados transformados em taxas

par(mfrow=c(2,3))

plot(dados$Date, dados$PIBper, col = 'black', type = 'l', 
     ylim = c(min(dados$PIBper), max(dados$PIBper)), 
     main = "PIB", xlab = "Data", ylab = "Taxa crescimento PIB")
     
plot(dados$Date, dados$SELIC, col = 'black', type = 'l', 
     ylim = c(min(dados$SELIC), max(dados$SELIC)), 
     main = "Taxa de juros - Selic", xlab = "Data", ylab = "SELIC")

plot(dados$Date, dados$DES, col = 'black', type = 'l', 
     ylim = c(min(dados$DES), max(dados$DES)), 
     main = "Desemprego", xlab = "Data", ylab = "Taxa de Desemprego")

plot(dados$Date, dados$IPCA, col = 'black', type = 'l', 
     ylim = c(min(dados$IPCA), max(dados$IPCA)), 
     main = "IPCA", xlab = "Data", ylab = "IPCA")

plot(dados$Date, dados$DepTotaisPer, col = 'black', type = 'l', 
        ylim = c(min(dados$DepTotaisPer), max(dados$DepTotaisPer)), 
        main = "Depósitos totais", xlab = "Data", ylab = "Varição Depósitos totais")

## Inspecionando a correlação entre os dados

# Criando a matriz de correlação para as taxas
matriz_taxas <- data.frame(PIBper = dados$PIBper, DepTotaisPer = dados$DepTotaisPer, SELIC = dados$SELIC, IPCA = dados$IPCA, DES = dados$DES)
matriz_correl <- cor(matriz_taxas)

# exibindo a relação gráfica 
matriz_correl
##                   PIBper DepTotaisPer       SELIC       IPCA         DES
## PIBper        1.00000000   -0.1332221 -0.06692552  0.3292171 -0.04673026
## DepTotaisPer -0.13322211    1.0000000 -0.28863352 -0.3825173  0.19604120
## SELIC        -0.06692552   -0.2886335  1.00000000  0.2507168 -0.64675624
## IPCA          0.32921707   -0.3825173  0.25071682  1.0000000 -0.39469378
## DES          -0.04673026    0.1960412 -0.64675624 -0.3946938  1.00000000
pairs(matriz_correl)

# Classificando as correlações entre as variáveis
# Definir os limiares para classificação das correlações
limiar_fraco <- 0.3
limiar_moderado <- 0.7

# Inicializar uma lista para armazenar os resultados
resultados <- list()

# Percorrer as células da matriz de correlação
for (i in 1:nrow(matriz_correl)) {
  for (j in 1:ncol(matriz_correl)) {
    # Obter o valor da correlação na célula (i, j)
    correlacao <- matriz_correl[i, j]
    
    # Classificar a correlação com base nos limiares
    if (abs(correlacao) < limiar_fraco) {
      classificacao <- "Fraca"
    } else if (abs(correlacao) < limiar_moderado) {
      classificacao <- "Moderada"
    } else {
      classificacao <- "Forte"
    }
    
    # Armazenar o resultado na lista
    resultado <- paste("Correlação entre", colnames(matriz_correl)[i], "e", colnames(matriz_correl)[j], "é", classificacao)
    resultados <- c(resultados, resultado)
  }
}

# Atribuir nomes aos elementos da lista com base na posição da célula na matriz
names(resultados) <- c(outer(colnames(matriz_correl), colnames(matriz_correl), paste, sep = "_"))

# atribuindo classificação ao dataframe resultados
resultados <- data.frame(Resultado = unlist(resultados))

# dataframe resultante
resultados
##                                                                      Resultado
## PIBper_PIBper                         Correlação entre PIBper e PIBper é Forte
## DepTotaisPer_PIBper             Correlação entre PIBper e DepTotaisPer é Fraca
## SELIC_PIBper                           Correlação entre PIBper e SELIC é Fraca
## IPCA_PIBper                          Correlação entre PIBper e IPCA é Moderada
## DES_PIBper                               Correlação entre PIBper e DES é Fraca
## PIBper_DepTotaisPer             Correlação entre DepTotaisPer e PIBper é Fraca
## DepTotaisPer_DepTotaisPer Correlação entre DepTotaisPer e DepTotaisPer é Forte
## SELIC_DepTotaisPer               Correlação entre DepTotaisPer e SELIC é Fraca
## IPCA_DepTotaisPer              Correlação entre DepTotaisPer e IPCA é Moderada
## DES_DepTotaisPer                   Correlação entre DepTotaisPer e DES é Fraca
## PIBper_SELIC                           Correlação entre SELIC e PIBper é Fraca
## DepTotaisPer_SELIC               Correlação entre SELIC e DepTotaisPer é Fraca
## SELIC_SELIC                             Correlação entre SELIC e SELIC é Forte
## IPCA_SELIC                               Correlação entre SELIC e IPCA é Fraca
## DES_SELIC                              Correlação entre SELIC e DES é Moderada
## PIBper_IPCA                          Correlação entre IPCA e PIBper é Moderada
## DepTotaisPer_IPCA              Correlação entre IPCA e DepTotaisPer é Moderada
## SELIC_IPCA                               Correlação entre IPCA e SELIC é Fraca
## IPCA_IPCA                                 Correlação entre IPCA e IPCA é Forte
## DES_IPCA                                Correlação entre IPCA e DES é Moderada
## PIBper_DES                               Correlação entre DES e PIBper é Fraca
## DepTotaisPer_DES                   Correlação entre DES e DepTotaisPer é Fraca
## SELIC_DES                              Correlação entre DES e SELIC é Moderada
## IPCA_DES                                Correlação entre DES e IPCA é Moderada
## DES_DES                                     Correlação entre DES e DES é Forte

Criando Modelo de Série Temporal

# Excluir a coluna de data

logdados <- subset(dados, Date >= "2017-01-01")
logdados <- subset(dados, select = -1)
head(logdados)
##     PIBmi      IPCA  DES     SELIC      SavI   Savings  DepVista  DepPrazo
## 2 1632766 0.3566667 12.2 1.0700000 0.6634000 656256520 156864085 684942790
## 3 1585585 0.3066667 13.9 1.0033333 0.6179667 663075626 153017312 711482590
## 4 1630638 0.2133333 13.1 0.8433333 0.5435667 670974684 153672040 789986433
## 5 1648631 0.1800000 12.5 0.7466667 0.5379333 690841486 155032100 826600944
## 6 1720626 0.2966667 11.9 0.5833333 0.5000000 710506319 163379596 838968408
## 7 1682460 0.1733333 13.2 0.5266667 0.5000000 729469370 163855257 843486825
##    DepTotais    PIBper Savingsper DepVistaper DepPrazoPer DepTotaisPer
## 2 1498063395 1.0352492   1.018558    1.059284    1.038303     1.031681
## 3 1527575528 0.9711036   1.010391    0.975477    1.038747     1.019700
## 4 1614633157 1.0284141   1.011913    1.004279    1.110338     1.056991
## 5 1672474529 1.0110343   1.029609    1.008850    1.046348     1.035823
## 6 1712854323 1.0436696   1.028465    1.053844    1.014962     1.024144
## 7 1736811452 0.9778185   1.026689    1.002911    1.005386     1.013987
# converter para log
logdados <- log(logdados)
sum(is.na(logdados))
## [1] 0
dadosts<-ts(as.data.frame(cbind(logdados$DepTotaisPer,logdados$PIBper,logdados$DES, logdados$IPCA,logdados$SELIC),start=c(1997,1), frequency = 4))

# Realizar testes 

Teste ADF para determinar presença de raiz unitária

Para Variável PIB nominal em taxas percentuais

library(urca)

colnames(logdados)
##  [1] "PIBmi"        "IPCA"         "DES"          "SELIC"        "SavI"        
##  [6] "Savings"      "DepVista"     "DepPrazo"     "DepTotais"    "PIBper"      
## [11] "Savingsper"   "DepVistaper"  "DepPrazoPer"  "DepTotaisPer"
# M3
DepTotaisPer_adf3 <-ur.df(logdados$DepPrazoPer, type = "trend", selectlags = c("BIC"))
summary(DepTotaisPer_adf3)
## 
## ############################################### 
## # 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.050148 -0.024847 -0.008763  0.011179  0.174776 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  4.254e-02  2.595e-02   1.639  0.11761   
## z.lag.1     -8.427e-01  2.679e-01  -3.145  0.00533 **
## tt          -2.779e-05  1.553e-03  -0.018  0.98592   
## z.diff.lag   1.896e-01  2.262e-01   0.838  0.41233   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.0494 on 19 degrees of freedom
## Multiple R-squared:  0.3782, Adjusted R-squared:   0.28 
## F-statistic: 3.853 on 3 and 19 DF,  p-value: 0.02613
## 
## 
## Value of test-statistic is: -3.1451 3.3023 4.9514 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.38 -3.60 -3.24
## phi2  8.21  5.68  4.67
## phi3 10.61  7.24  5.91
# Se valor a 5% menor que tau3, não há raiz unitária, caso contrário testar M3 X M2 
# Se phi 3 maior phi calculado, M3 melhor que M2, caso contrário M2 melhor que 
# Se phi 2 maior que phi2 calculado, modelo M3 é melhor que M1.
# Obtendo os valores críticos a partir do summary
ADF3 <- summary(DepTotaisPer_adf3)

# Obtendo os valores do resumo
value_tau3 <- ADF3@teststat[1]
value_phi2 <- ADF3@teststat[2]
value_phi3 <- ADF3@teststat[3]
summary_values <- ADF3@cval

# Valores críticos
critical_values <- data.frame(
  row.names = c("tau3", "phi2", "phi3"),
  "1pct" = c(summary_values["tau3", "1pct"], summary_values["phi2", "1pct"], summary_values["phi3", "1pct"]),
  "5pct" = c(summary_values["tau3", "5pct"], summary_values["phi2", "5pct"], summary_values["phi3", "5pct"]),
  "10pct" = c(summary_values["tau3", "10pct"], summary_values["phi2", "10pct"], summary_values["phi3", "10pct"])
)

# Função para verificar a melhor modelagem para ADF3.
verificar_modelagem <- function(value_tau3, value_phi2, value_phi3, pct, critical_values) {
  # Verificar se não há raiz unitária (tau3)
  if (value_tau3 < critical_values["tau3", pct]) {
    return("Não há raiz unitária.")
  } else {
    # Verificar a melhor modelagem (phi3)
    if (value_phi3 > critical_values["phi3", pct]) {
      return("M3 é melhor que M2, Trend.")
    } else {
      # Verificar a melhor modelagem (phi2)
      if (value_phi2 > critical_values["phi2", pct]) {
        return("M3 é melhor que M1, Trend.")
      } else {
        return("M2 é melhor que M3, Testar ADF2")
      }
    }
  }
}

# Chamada da função para verificar a modelagem
pct <- "X5pct"  # Substitua pelo valor correto ("1pct", "5pct", ou "10pct")
resultado <- verificar_modelagem(value_tau3, value_phi2, value_phi3, pct, critical_values)
resultado
## [1] "M2 é melhor que M3, Testar ADF2"
DepTotaisPer_adf2<-ur.df(DepTotaisPer, type = "drift", selectlags = c("BIC"))
summary(DepTotaisPer_adf2)
## 
## ############################################### 
## # 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.040039 -0.012725 -0.002984  0.004225  0.150171 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)   0.7651     0.2612   2.929   0.0083 **
## z.lag.1      -0.7368     0.2516  -2.929   0.0083 **
## z.diff.lag    0.1334     0.2211   0.603   0.5530   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.03884 on 20 degrees of freedom
## Multiple R-squared:  0.3387, Adjusted R-squared:  0.2725 
## F-statistic: 5.121 on 2 and 20 DF,  p-value: 0.016
## 
## 
## Value of test-statistic is: -2.9289 4.2902 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.75 -3.00 -2.63
## phi1  7.88  5.18  4.12
# se Tau2 calculado > Tau2 est -> Tem RU
# Se Phi1 calc > Phi 1 est -> M2 melhor que M1, caso contrário M1. 
# M1 melhor que M2, fazer ADF1

DepTotaisPer_adf1<-ur.df(DepTotaisPer, type = "none", selectlags = c("BIC"))
summary(DepTotaisPer_adf1)
## 
## ############################################### 
## # 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.067724 -0.017691  0.006493  0.013286  0.149999 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)
## z.lag.1    -0.0003662  0.0090973  -0.040    0.968
## z.diff.lag -0.2343525  0.2123559  -1.104    0.282
## 
## Residual standard error: 0.0453 on 21 degrees of freedom
## Multiple R-squared:  0.05504,    Adjusted R-squared:  -0.03495 
## F-statistic: 0.6116 on 2 and 21 DF,  p-value: 0.5519
## 
## 
## Value of test-statistic is: -0.0403 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau1 -2.66 -1.95  -1.6
# Se  tau1 cal > tau estat, Tem RU

Para variável SELIC

library(urca)
SELIC_adf3 <-ur.df(dados$SELIC, type = "trend", selectlags = c("BIC"))
summary(SELIC_adf3)
## 
## ############################################### 
## # 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.106875 -0.036369  0.001536  0.028857  0.093738 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.096943   0.057479   1.687 0.108035    
## z.lag.1     -0.148089   0.048984  -3.023 0.006993 ** 
## tt          -0.001212   0.003027  -0.400 0.693395    
## z.diff.lag   0.948387   0.195738   4.845 0.000112 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.05349 on 19 degrees of freedom
## Multiple R-squared:  0.7806, Adjusted R-squared:  0.7459 
## F-statistic: 22.53 on 3 and 19 DF,  p-value: 1.788e-06
## 
## 
## Value of test-statistic is: -3.0232 3.4561 5.1755 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.38 -3.60 -3.24
## phi2  8.21  5.68  4.67
## phi3 10.61  7.24  5.91
# Tem RU, M2 melhor que M3 e M1 melhor que M3. 

SELIC_adf2<-ur.df(dados$SELIC, type = "drift", selectlags = c("BIC"))
summary(SELIC_adf2)
## 
## ############################################### 
## # 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.105459 -0.035684  0.005079  0.030779  0.092368 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.07642    0.02544   3.004  0.00702 ** 
## z.lag.1     -0.13914    0.04266  -3.261  0.00391 ** 
## z.diff.lag   0.88341    0.10706   8.252  7.2e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.05236 on 20 degrees of freedom
## Multiple R-squared:  0.7787, Adjusted R-squared:  0.7566 
## F-statistic: 35.19 on 2 and 20 DF,  p-value: 2.817e-07
## 
## 
## Value of test-statistic is: -3.2615 5.3278 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.75 -3.00 -2.63
## phi1  7.88  5.18  4.12
# Tem RU, M2 melhor M1

SELIC_adf1<-ur.df(dados$SELIC, type = "none", selectlags = c("BIC"))
summary(SELIC_adf1)
## 
## ############################################### 
## # 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.11798 -0.00472  0.02162  0.05156  0.10434 
## 
## Coefficients:
##            Estimate Std. Error t value Pr(>|t|)    
## z.lag.1    -0.02340    0.02152  -1.087    0.289    
## z.diff.lag  0.82166    0.12351   6.652 1.38e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.06155 on 21 degrees of freedom
## Multiple R-squared:  0.679,  Adjusted R-squared:  0.6484 
## F-statistic: 22.21 on 2 and 21 DF,  p-value: 6.579e-06
## 
## 
## Value of test-statistic is: -1.087 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau1 -2.66 -1.95  -1.6
# Tem Ru,
# M2 vence!

Para variável IPCA

DepTotaisPer_adf3 <- ur.df(logdados$DepTotaisPer, type = "trend", selectlags = c("BIC"))
summary(DepTotaisPer_adf3)
## 
## ############################################### 
## # 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.037467 -0.013380 -0.002297  0.006319  0.135662 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  0.0307432  0.0190632   1.613  0.12330   
## z.lag.1     -0.7359481  0.2558418  -2.877  0.00966 **
## tt          -0.0002695  0.0011491  -0.235  0.81710   
## z.diff.lag   0.1456260  0.2261724   0.644  0.52736   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.03655 on 19 degrees of freedom
## Multiple R-squared:  0.3397, Adjusted R-squared:  0.2354 
## F-statistic: 3.258 on 3 and 19 DF,  p-value: 0.04436
## 
## 
## Value of test-statistic is: -2.8766 2.7867 4.1791 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.38 -3.60 -3.24
## phi2  8.21  5.68  4.67
## phi3 10.61  7.24  5.91
IPCA_adf3 <-ur.df(logdados$IPCA, type = "trend", selectlags = c("BIC"))
summary(IPCA_adf3)
## 
## ############################################### 
## # 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.7833 -0.1933  0.1419  0.3225  0.5344 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) -1.42832    0.51419  -2.778  0.01199 * 
## z.lag.1     -0.72458    0.24035  -3.015  0.00713 **
## tt           0.04323    0.02064   2.095  0.04985 * 
## z.diff.lag   0.12857    0.21588   0.596  0.55849   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5208 on 19 degrees of freedom
## Multiple R-squared:  0.3535, Adjusted R-squared:  0.2515 
## F-statistic: 3.463 on 3 and 19 DF,  p-value: 0.03684
## 
## 
## Value of test-statistic is: -3.0146 3.075 4.6017 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.38 -3.60 -3.24
## phi2  8.21  5.68  4.67
## phi3 10.61  7.24  5.91
# Tem RU, M2 melhor que M3 e M1 melhor que M3

IPCA_adf2<-ur.df(logdados$IPCA, type = "drift", selectlags = c("BIC"))
summary(IPCA_adf2) 
## 
## ############################################### 
## # 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.6861 -0.2959 -0.0231  0.4237  0.8113 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept) -0.49572    0.27813  -1.782   0.0899 .
## z.lag.1     -0.41995    0.20693  -2.029   0.0559 .
## z.diff.lag   0.01431    0.22587   0.063   0.9501  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5632 on 20 degrees of freedom
## Multiple R-squared:  0.2043, Adjusted R-squared:  0.1247 
## F-statistic: 2.567 on 2 and 20 DF,  p-value: 0.1018
## 
## 
## Value of test-statistic is: -2.0294 2.0685 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.75 -3.00 -2.63
## phi1  7.88  5.18  4.12
# Tem RU, M1

dif_IPCA_adf1<-ur.df(diff(logdados$IPCA), type = "none", selectlags = c("BIC"))
summary(dif_IPCA_adf1)
## 
## ############################################### 
## # 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.2546 -0.3128  0.2069  0.2995  0.9131 
## 
## Coefficients:
##            Estimate Std. Error t value Pr(>|t|)    
## z.lag.1     -1.8522     0.2983  -6.210 4.58e-06 ***
## z.diff.lag   0.5320     0.1932   2.753   0.0123 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5215 on 20 degrees of freedom
## Multiple R-squared:  0.7153, Adjusted R-squared:  0.6868 
## F-statistic: 25.13 on 2 and 20 DF,  p-value: 3.497e-06
## 
## 
## Value of test-statistic is: -6.2096 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau1 -2.66 -1.95  -1.6
# Tem RU

Para variável desemprego

DES_adf3 <-ur.df(dados$DES, type = "trend", selectlags = c("BIC"))
summary(DES_adf3)
## 
## ############################################### 
## # 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.2259 -0.6306 -0.3436  0.7146  1.8758 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  1.18563    2.45743   0.482    0.635
## z.lag.1     -0.07444    0.18365  -0.405    0.690
## tt          -0.04243    0.03441  -1.233    0.233
## z.diff.lag  -0.14986    0.24631  -0.608    0.550
## 
## Residual standard error: 1.004 on 19 degrees of freedom
## Multiple R-squared:  0.09223,    Adjusted R-squared:  -0.0511 
## F-statistic: 0.6435 on 3 and 19 DF,  p-value: 0.5965
## 
## 
## Value of test-statistic is: -0.4054 1.0935 0.7615 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.38 -3.60 -3.24
## phi2  8.21  5.68  4.67
## phi3 10.61  7.24  5.91
# TEm RU, M1 melhor que M3 e M2 melhor que M3


DES_adf2<-ur.df(dados$DES, type = "drift", selectlags = c("BIC"))
summary(DES_adf2)
## 
## ############################################### 
## # 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.6243 -0.4802 -0.2403  0.6462  1.6988 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.16710    2.22737  -0.075    0.941
## z.lag.1     -0.00912    0.17812  -0.051    0.960
## z.diff.lag  -0.12266    0.24849  -0.494    0.627
## 
## Residual standard error: 1.017 on 20 degrees of freedom
## Multiple R-squared:  0.01959,    Adjusted R-squared:  -0.07845 
## F-statistic: 0.1998 on 2 and 20 DF,  p-value: 0.8205
## 
## 
## Value of test-statistic is: -0.0512 0.8577 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.75 -3.00 -2.63
## phi1  7.88  5.18  4.12
# Tem RU, M2 melhor que M1


DES_adf1<-ur.df(dados$DES, type = "none", selectlags = c("BIC"))
summary(DES_adf1)
## 
## ############################################### 
## # 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.6039 -0.4758 -0.2270  0.6570  1.6990 
## 
## Coefficients:
##            Estimate Std. Error t value Pr(>|t|)
## z.lag.1    -0.02242    0.01674  -1.340    0.195
## z.diff.lag -0.11206    0.19953  -0.562    0.580
## 
## Residual standard error: 0.9929 on 21 degrees of freedom
## Multiple R-squared:  0.08701,    Adjusted R-squared:  5.689e-05 
## F-statistic: 1.001 on 2 and 21 DF,  p-value: 0.3845
## 
## 
## Value of test-statistic is: -1.3397 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau1 -2.66 -1.95  -1.6
# TEM RU

Repetindo ADF 3 para variáveis na primeira diferença

dSELIC_adf3 <-ur.df(diff(logdados$SELIC), type = "trend", selectlags = c("BIC"))
summary(dSELIC_adf3) # ainda há raiz unitá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.21113 -0.04516 -0.00295  0.04055  0.43009 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)  
## (Intercept) -0.048894   0.083544  -0.585    0.566  
## z.lag.1     -0.351446   0.179558  -1.957    0.066 .
## tt           0.004322   0.006186   0.699    0.494  
## z.diff.lag   0.291248   0.231242   1.259    0.224  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1482 on 18 degrees of freedom
## Multiple R-squared:  0.1985, Adjusted R-squared:  0.06495 
## F-statistic: 1.486 on 3 and 18 DF,  p-value: 0.2519
## 
## 
## Value of test-statistic is: -1.9573 1.3956 2.0793 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.38 -3.60 -3.24
## phi2  8.21  5.68  4.67
## phi3 10.61  7.24  5.91
d2SELIC_adf3 <-ur.df(diff(logdados$SELIC, 2), type = "trend", selectlags = c("BIC"))
summary(d2SELIC_adf3) # Não há mais raiz unitá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.257636 -0.060115 -0.002581  0.105164  0.195437 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.126616   0.079802  -1.587  0.13102    
## z.lag.1     -0.336420   0.086983  -3.868  0.00124 ** 
## tt           0.010563   0.006164   1.714  0.10474    
## z.diff.lag   0.847274   0.140264   6.041 1.32e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1358 on 17 degrees of freedom
## Multiple R-squared:  0.7095, Adjusted R-squared:  0.6583 
## F-statistic: 13.84 on 3 and 17 DF,  p-value: 8.068e-05
## 
## 
## Value of test-statistic is: -3.8676 5.2015 7.8016 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.38 -3.60 -3.24
## phi2  8.21  5.68  4.67
## phi3 10.61  7.24  5.91
difIPCA_adf3 <-ur.df(diff(logdados$IPCA), type = "trend", selectlags = c("BIC"))
summary(difIPCA_adf3) # não há mais raiz unitá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.3018 -0.3653  0.1028  0.3207  0.8395 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.08028    0.25752  -0.312   0.7588    
## z.lag.1     -1.90298    0.31646  -6.013  1.1e-05 ***
## tt           0.01121    0.01857   0.604   0.5535    
## z.diff.lag   0.56435    0.20492   2.754   0.0131 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5404 on 18 degrees of freedom
## Multiple R-squared:  0.7248, Adjusted R-squared:  0.6789 
## F-statistic:  15.8 on 3 and 18 DF,  p-value: 2.768e-05
## 
## 
## Value of test-statistic is: -6.0132 12.1746 18.2618 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.38 -3.60 -3.24
## phi2  8.21  5.68  4.67
## phi3 10.61  7.24  5.91
dif2DES_adf3 <-ur.df(diff(logdados$DES, 2), type = "trend", selectlags = c("BIC"))
summary(dif2DES_adf3) # Há raiz unitá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.14022 -0.05888 -0.00092  0.05157  0.12544 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  0.048804   0.036577   1.334  0.19971   
## z.lag.1     -0.595558   0.181928  -3.274  0.00448 **
## tt          -0.005645   0.002868  -1.968  0.06561 . 
## z.diff.lag   0.452572   0.202987   2.230  0.03955 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.07434 on 17 degrees of freedom
## Multiple R-squared:  0.4158, Adjusted R-squared:  0.3127 
## F-statistic: 4.034 on 3 and 17 DF,  p-value: 0.02454
## 
## 
## Value of test-statistic is: -3.2736 3.8385 5.7569 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.38 -3.60 -3.24
## phi2  8.21  5.68  4.67
## phi3 10.61  7.24  5.91
difDepTotaisPer_adf3 <-ur.df(diff(logdados$DepTotaisPer), type = "trend", selectlags = c("BIC")) # não há raiz unitária
summary(difDepTotaisPer_adf3)
## 
## ############################################### 
## # 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.080555 -0.015616  0.002949  0.010146  0.137610 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.531e-03  2.055e-02  -0.075 0.941408    
## z.lag.1     -1.434e+00  3.572e-01  -4.014 0.000815 ***
## tt           3.721e-05  1.465e-03   0.025 0.980019    
## z.diff.lag   1.823e-01  2.282e-01   0.799 0.434678    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.04358 on 18 degrees of freedom
## Multiple R-squared:  0.6263, Adjusted R-squared:  0.564 
## F-statistic: 10.05 on 3 and 18 DF,  p-value: 0.0004083
## 
## 
## Value of test-statistic is: -4.0137 5.391 8.0641 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -4.38 -3.60 -3.24
## phi2  8.21  5.68  4.67
## phi3 10.61  7.24  5.91