New package: GetBCBData

April 14, 2019
By

(This article was first published on R on msperlin, and kindly contributed to R-bloggers)

The Central Bank of Brazil (BCB) offers access to its SGS system (sistema gerenciador de series temporais) with a official API available here.

With time, I find myself using more and more of the available datasets in my regular research and studies. Over last weekend I decided to write my own API package that would make my life a lot easier.

Package GetBCBData can fetch data efficiently and rapidly:

  • Use of a caching system with package memoise to speed up repeated requests of data;
  • Users can utilize all cores of the machine (parallel computing) when fetching a large batch of time series;
  • Allows the choice for format output: long (row oriented, tidy data) or wide (column oriented)
  • Error handling internally. Even if requested series does not exist, the function will still return all results.

Installation

# CRAN (official release) - IN CHECK
install.packages('GetBCBData')

# Github (dev version)
devtools::install_github('msperlin/GetBCBData')

A simple example

Let’s have a look at unemployment rates around the world. After searching for the ids in the SGS system, we find the ids for 6 countries and set it as input id.

Now, lets download the data with GetBCBData:

#devtools::install_github('msperlin/GetBCBData')

library(GetBCBData)
library(tidyverse)

my.countries <- c('Germany', 'Canada', 'USA', 
                  'France', 'Italy', 'Japan')

my.ids <- c(3785:3790)

names(my.ids) <- paste0('Unemp. rate - ', my.countries)

df.bcb <- gbcbd_get_series(id = my.ids ,
                           first.date = '2000-01-01',
                           last.date = Sys.Date())
## 
## Fetching Unemp. rate - Germany [3785] from BCB-SGS with cache 
##   Found 199 observations
## Fetching Unemp. rate - Canada [3786] from BCB-SGS with cache 
##   Found 200 observations
## Fetching Unemp. rate - USA [3787] from BCB-SGS with cache 
##   Found 200 observations
## Fetching Unemp. rate - France [3788] from BCB-SGS with cache 
##   Found 199 observations
## Fetching Unemp. rate - Italy [3789] from BCB-SGS with cache 
##   Found 199 observations
## Fetching Unemp. rate - Japan [3790] from BCB-SGS with cache 
##   Found 199 observations
glimpse(df.bcb)
## Observations: 1,196
## Variables: 4
## $ ref.date     2000-01-01, 2000-02-01, 2000-03-01, 2000-04-01, 200…
## $ value        8.2, 8.1, 8.1, 8.0, 8.0, 8.0, 7.9, 7.9, 7.9, 7.8, 7.…
## $ id.num       3785, 3785, 3785, 3785, 3785, 3785, 3785, 3785, 3785…
## $ series.name  "Unemp. rate - Germany", "Unemp. rate - Germany", "U…
p <- ggplot(df.bcb, aes(x = ref.date, y = value) ) +
  geom_line() + 
  labs(title = 'Unemploymnent Rates Around the World', 
       subtitle = paste0(min(df.bcb$ref.date), ' to ', max(df.bcb$ref.date)),
       x = '', y = 'Percentage*100') + facet_wrap(~series.name)


print(p)

To leave a comment for the author, please follow the link and comment on their blog: R on msperlin.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)