comexstat_download()
## Downloading data from Comexstat...
## Increasing timeout limit ...
library(dplyr)
# Get CPI data
path<-"https://fred.stlouisfed.org/graph/fredgraph.csv?id=CPIAUCSL"
cpi <- read.csv(path)%>%
transmute(date=as.Date(DATE), cpi=CPIAUCSL)
Utilizando o pacote rbcb, de Wilson Freitas.
lastmonth <- {
m <- Sys.Date()
lubridate::day(m) <- 1
m-1
}
library(rbcb)
ipca0 <- rbcb::get_series(433, start_date="1997-01-01")
brlusd0 <- get_currency("USD", "1997-01-01", lastmonth)%>%
rename(brlusd_bid=bid,brlusd_ask=ask)
ipca <- ipca0%>%
rename(ipca="433")%>%
arrange(date)%>%
mutate(ipca_i=cumprod(1+ipca/100))
brlusd <- brlusd0%>%
group_by(date=lubridate::make_date(lubridate::year(date),lubridate::month(date)))%>%
summarise(brlusd=(mean(brlusd_bid) + mean(brlusd_ask))/2)
Utilizando o meu pacote comexstatr.
## value in BRL -> adjust for inflation
trade0 <- comexstat()%>%
## filtra país (EUA é 249)
filter(co_pais=="249")%>%
## agrupa por ano, mes, e fluxo
group_by(co_ano, co_mes, fluxo)%>%
## soma o valor fob
summarise(vl_fob=sum(vl_fob))%>%
ungroup%>%
mutate(date=lubridate::make_date(co_ano, co_mes), co_ano=NULL, co_mes=NULL)%>%
collect()
head(trade0)
## # A tibble: 6 × 3
## fluxo vl_fob date
## <chr> <dbl> <date>
## 1 exp 732350836 1998-11-01
## 2 exp 982281694 1998-07-01
## 3 exp 824170540 1998-10-01
## 4 exp 852638547 1998-12-01
## 5 exp 815945399 1998-03-01
## 6 exp 899336275 1998-06-01
Junta (join) dos dados de comércio com os de câmbio e inflação
trade1 <- trade0%>%
## joins com dados de inflação e câmbio
left_join(ipca)%>%
left_join(brlusd)%>%
left_join(cpi)
## Joining with `by = join_by(date)`
## Joining with `by = join_by(date)`
## Joining with `by = join_by(date)`
head(trade1)
## # A tibble: 6 × 7
## fluxo vl_fob date ipca ipca_i brlusd cpi
## <chr> <dbl> <date> <dbl> <dbl> <dbl> <dbl>
## 1 exp 732350836 1998-11-01 -0.12 1.07 1.20 164.
## 2 exp 982281694 1998-07-01 -0.12 1.08 1.16 163.
## 3 exp 824170540 1998-10-01 0.02 1.07 1.19 164.
## 4 exp 852638547 1998-12-01 0.33 1.07 1.21 164.
## 5 exp 815945399 1998-03-01 0.34 1.07 1.14 162
## 6 exp 899336275 1998-06-01 0.02 1.08 1.16 163.
Agora colocamos o fluxo (imp - importações e exp - exportações) nas colunas (long-wide) usando o pacote tydir, pra poder calcular corrente de comércio (imp+exp) e deficit/superávit (exp-imp)
trade2 <- trade1%>%
tidyr::pivot_wider(names_from="fluxo", values_from=c("vl_fob"), names_glue = "{fluxo}")%>%
mutate(sum=imp+exp,
dif=exp-imp
)
head(trade2)
## # A tibble: 6 × 9
## date ipca ipca_i brlusd cpi exp imp sum dif
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1998-11-01 -0.12 1.07 1.20 164. 732350836 1064964185 1797315021 -3.33e8
## 2 1998-07-01 -0.12 1.08 1.16 163. 982281694 1276364238 2258645932 -2.94e8
## 3 1998-10-01 0.02 1.07 1.19 164. 824170540 1365002867 2189173407 -5.41e8
## 4 1998-12-01 0.33 1.07 1.21 164. 852638547 1103531134 1956169681 -2.51e8
## 5 1998-03-01 0.34 1.07 1.14 162 815945399 1180793751 1996739150 -3.65e8
## 6 1998-06-01 0.02 1.08 1.16 163. 899336275 1107359375 2006695650 -2.08e8
Transforma pra long, para facilitar os cálculos de ajuste com dados de inflação e câmbio.
trade3 <- trade2%>%
tidyr::pivot_longer(cols = c("sum", "dif", "imp", "exp"), values_to = "nominal_usd", names_to="direction")%>%
ungroup%>%
arrange(date)%>%
## ajustes
mutate(
## valor corrente usd usando cpi
current_usd=nominal_usd*(dplyr::last(cpi)/cpi),
## valor nomianl brl convertendo o valor nominal em usd usando média do cambio mensal
nominal_brl=nominal_usd*brlusd,
## valor corrente brl corrigindo o valor nominal pelo ipca
current_brl=nominal_brl*(dplyr::last(ipca_i)/ipca_i)
)
head(trade3)
## # A tibble: 6 × 10
## date ipca ipca_i brlusd cpi direction nomin…¹ curre…² nomin…³ curre…⁴
## <date> <dbl> <dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 1997-01-01 1.18 1.01 1.05 159. sum 1.33e9 2.52e9 1.39e9 6.65e9
## 2 1997-01-01 1.18 1.01 1.05 159. dif 2.11e8 4.00e8 2.21e8 1.06e9
## 3 1997-01-01 1.18 1.01 1.05 159. imp 5.59e8 1.06e9 5.84e8 2.80e9
## 4 1997-01-01 1.18 1.01 1.05 159. exp 7.70e8 1.46e9 8.05e8 3.86e9
## 5 1997-02-01 0.5 1.02 1.05 160. sum 1.57e9 2.97e9 1.65e9 7.88e9
## 6 1997-02-01 0.5 1.02 1.05 160. dif -1.91e8 -3.61e8 -2.01e8 -9.57e8
## # … with abbreviated variable names ¹nominal_usd, ²current_usd, ³nominal_brl,
## # ⁴current_brl
Mais uma transformação para long agora para facilitar o cálculo de soma parcial/running sum.
trade4 <- trade3%>%
tidyr::pivot_longer(cols=matches("nominal|current"), names_to = "adjustment")%>%
tidyr::separate(adjustment, c("type", "currency"))%>%
group_by(currency, type,direction)%>%
arrange(date)%>%
mutate(
value_bi=value/1e9,
value_bi_12=zoo::rollsum(value_bi,12, fill=NA, align = "right"),
value_bi_3=zoo::rollsum(value_bi,3, fill=NA, align = "right"))
head(trade4)
## # A tibble: 6 × 12
## # Groups: currency, type, direction [6]
## date ipca ipca_i brlusd cpi direction type currency value value…¹
## <date> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <chr> <dbl> <dbl>
## 1 1997-01-01 1.18 1.01 1.05 159. sum nominal usd 1.33e9 1.33
## 2 1997-01-01 1.18 1.01 1.05 159. sum current usd 2.52e9 2.52
## 3 1997-01-01 1.18 1.01 1.05 159. sum nominal brl 1.39e9 1.39
## 4 1997-01-01 1.18 1.01 1.05 159. sum current brl 6.65e9 6.65
## 5 1997-01-01 1.18 1.01 1.05 159. dif nominal usd 2.11e8 0.211
## 6 1997-01-01 1.18 1.01 1.05 159. dif current usd 4.00e8 0.400
## # … with 2 more variables: value_bi_12 <dbl>, value_bi_3 <dbl>, and abbreviated
## # variable name ¹value_bi
library(ggplot2)
library(hrbrthemes)
##hrbrthemes::import_roboto_condensed()
ggplot(aes(x=date, y=value_bi_3),
data=trade4%>%
filter(currency=="usd",
direction%in%c("exp", "imp", "dif"),
type=="nominal",
grepl("03-01$", date)
, date>="2014-01-01"
)) +
geom_col(aes(fill=direction), position="dodge") +
#facet_wrap(.~direction) +
labs(x="", y="USD Bilhões", color="", fill="")+
theme_ipsum()+
theme(legend.position = "bottom") +
scale_fill_viridis_d()
ggplot(aes(x=date, y=value_bi_3),
data=trade4%>%
filter(currency=="usd",
direction%in%c("exp", "imp", "dif"),
type=="current",
grepl("03-01$", date)
, date>="2014-01-01"
)) +
geom_col(aes(fill=direction), position="dodge") +
#facet_wrap(.~direction) +
labs(x="", y="USD Bilhões", color="", fill="")+
theme_ipsum()+
theme(legend.position = "bottom") +
scale_fill_viridis_d()
ggplot(aes(x=date, y=value_bi_3),
data=trade4%>%
filter(currency=="usd",
grepl("03-01$", date),
direction%in%c("exp", "imp", "dif"),
type=="current"
)) +
geom_line(aes(color=direction)) +
#facet_wrap(.~direction) +
labs(x="", y="USD Bilhões", color="", fill="")+
theme_ipsum()+
theme(legend.position = "bottom") +
scale_fill_viridis_c()
Portanto, ajustando pela inflação dos EUA, o valor exportado pelo Brasil no primeiro trimestre de 2023 é (somente) o quarto maior da série desde 1997.
trade4%>%
filter(currency=="usd",
grepl("03-01$", date),
direction%in%c("exp"),
type=="current"
)%>%arrange(-value_bi_3)%>%head%>%ungroup%>%transmute(Trimestre=paste(1, lubridate::year(date), sep="/"), "Valor Corrente (bilhões USD)"=value_bi_3)%>%knitr::kable(format = "html")
Trimestre | Valor Corrente (bilhões USD) |
---|---|
1/2012 | 9.136624 |
1/2006 | 8.374752 |
1/2005 | 8.307939 |
1/2023 | 8.234476 |
1/2007 | 8.168816 |
1/2008 | 8.078893 |
ggplot(aes(x=date, y=value_bi_12),
data=trade4%>%
filter(currency=="brl",
grepl("03-01$", date),
direction%in%c("exp", "imp", "dif"),
type=="current"
)) +
geom_line(aes(color=direction)) +
#facet_wrap(.~direction) +
labs(x="", y="BRL Bilhões Correntes (IPCA)", color="", fill="")+
theme_ipsum()+
theme(legend.position = "bottom") +
scale_fill_viridis_c()
## Warning: Removed 3 row(s) containing missing values (geom_path).