Quandl is one of the best platforms for finding and downloading financial and economic time series. The collection of free databases is comprehensive and I’ve used it intensively in my research and class material.
But, a couple of things from the native package Quandl
always bothered me:
As you suspect, I decided to tackle the problem over the weekend. The
result is package GetQuandlData
. This is what it does
differently:
ggplot
or making tables;memoise
to set a local caching system.
This means that the second time you ask for a particular time series, it
will grab it from your hard drive (and not the internet);# not in CRAN yet (need to test it further)
#install.packages('GetQuandlData')
# from github
::install_github('msperlin/GetQuandlData') devtools
Let’s download and plot information about inflation in the US:
library(GetQuandlData)
<- c('Inflation USA' = 'RATEINF/INFLATION_USA')
my_id <- readLines('YOURAPIHERE') # you need your own API (get it at https://www.quandl.com/sign-up-modal?defaultModal=showSignUp>)
my_api <- '2000-01-01'
first_date <- Sys.Date()
last_date
<- get_Quandl_series(id_in = my_id,
df api_key = my_api,
first_date = first_date,
last_date = last_date,
cache_folder = tempdir())
::glimpse(df) dplyr
As you can see, the data is in the long format. Let’s plot it:
<- ggplot(df, aes(x = ref_date, y = value/100)) +
p geom_col() +
labs(y = 'Inflation (%)',
x = '',
title = 'Inflation in the US') +
scale_y_continuous(labels = scales::percent)
p
Beautiful!
Next, lets have a look into a more realistic case, where we need inflation data for several countries:
First, we need to see what are the available datasets from database
RATEINF
:
library(GetQuandlData)
<- 'RATEINF'
db_id <- readLines('YOURAPIHERE') # you need your own API
my_api
<- get_database_info(db_id, my_api)
df
head(df)
Nice. Now we only need to filter the series with YOY inflation:
<- stringr::str_detect(df$name, 'Inflation YOY')
idx
<- df[idx, ] df_series
and grab the data:
<- df_series$quandl_code
my_id names(my_id) <- df_series$name
<- '2010-01-01'
first_date <- Sys.Date()
last_date
<- get_Quandl_series(id_in = my_id,
df_inflation api_key = my_api,
first_date = first_date,
last_date = last_date,
cache_folder = tempdir())
glimpse(df_inflation)
And, an elegant plot:
<- ggplot(df_inflation, aes(x = ref_date, y = value/100)) +
p geom_col() +
labs(y = 'Inflation (%)',
x = '',
title = 'Inflation in the World',
subtitle = paste0(first_date, ' to ', last_date)) +
scale_y_continuous(labels = scales::percent) +
facet_wrap(~series_name)
p