Quick and Dirty Fredr

Some Data from FREDr

Downloading the FRED data on national debt as a percentage of GDP. I first want to examine the US data and will then turn to some comparisons. fredr makes it markable asy to do! I will use two core tools from fredr. First, fredr_series_search allows one to enter search text and retrieve the responsive series given that search text. They can be sorted in particular ways, two such options are shown below. In the first chunk, I will download the “national debt” data and show the top 6 responsive series.

library(fredr);library(ggthemes)
Debt.Search <- fredr_series_search_text(
  search_text = "national debt",
  order_by = "popularity",
  sort_order = "desc")
Debt.Search <- Debt.Search %>% top_n(6)
Debt.Search %>% select(id, title)
## # A tibble: 18 x 2
##    id             title                                                         
##    <chr>          <chr>                                                         
##  1 DDDM04USA156N… Outstanding Domestic Public Debt Securities to GDP for United…
##  2 DDDM04CNA156N… Outstanding Domestic Public Debt Securities to GDP for China  
##  3 DDDM04JPA156N… Outstanding Domestic Public Debt Securities to GDP for Japan  
##  4 DDDM04DEA156N… Outstanding Domestic Public Debt Securities to GDP for Germany
##  5 WLSFAL         Liabilities: Deposits with F.R. Banks, Other Than Reserve Bal…
##  6 DDDM04ARA156N… Outstanding Domestic Public Debt Securities to GDP for Argent…
##  7 DDDM04HKA156N… Outstanding Domestic Public Debt Securities to GDP for Hong K…
##  8 DDDM04BRA156N… Outstanding Domestic Public Debt Securities to GDP for Brazil 
##  9 DDDM04ITA156N… Outstanding Domestic Public Debt Securities to GDP for Italy  
## 10 DDDM04MXA156N… Outstanding Domestic Public Debt Securities to GDP for Mexico 
## 11 DDDM04CHA156N… Outstanding Domestic Public Debt Securities to GDP for Switze…
## 12 DDDM04THA156N… Outstanding Domestic Public Debt Securities to GDP for Thaila…
## 13 DDDM04AUA156N… Outstanding Domestic Public Debt Securities to GDP for Austra…
## 14 DDDM04GRA156N… Outstanding Domestic Public Debt Securities to GDP for Greece 
## 15 DDDM04INA156N… Outstanding Domestic Public Debt Securities to GDP for India  
## 16 DDDM04NLA156N… Outstanding Domestic Public Debt Securities to GDP for Nether…
## 17 DDDM04RUA156N… Outstanding Domestic Public Debt Securities to GDP for Russia…
## 18 DDDM04TRA156N… Outstanding Domestic Public Debt Securities to GDP for Turkey

Next, I need to acquire the data that I want. I probably should have reversed the order of some of the operations here. For example, I don’t really want the non-domestic public debt but I am not going to filter them before downloading. That’s not great but it isn’t all that much data either. The command fredr aliases the fredr_series_observations command that obtains data directly from FRED. I will use a variant of map to grab the relevant series id above and then join them back to the Search results.

Debt.Data <- map_dfr(Debt.Search$id, fredr) %>% left_join(Debt.Search, by=c("series_id" = "id"))

Now let me splice off the United States and plot it.

US.Debt <- dplyr::filter(Debt.Data, grepl('to GDP for United States', title))
ggplot(US.Debt, aes(x=date, y=value)) + geom_line(size=3) + theme_economist() + theme(plot.title = element_text(color="red", size=11, face="bold.italic")) + labs(title=US.Debt$title[1])

And because I ended up with a bunch of them; multiple time series plots. To automate this, I will first remove everything that represents liabilities above in the FRB system. Then I need to use the series title to select everything that I want and separate off the country names for labels. The only hard-coding hacks for the final plot are the title. Here’s what we get, with color encoding the country names.

library(stringr)
Ctry.DD <- Debt.Data %>% filter(!grepl('Liabilities: Deposits', title))
Ctry.DD <- Ctry.DD %>% mutate(Country = str_remove(title, str_remove(US.Debt$title[1], "United States")))
plot1 <- ggplot(Ctry.DD, aes(x=date, y=value, color=Country)) + geom_line(size=1) + scale_color_viridis_d() + labs(title="Public Debt to GDP", y="Public Debt to GDP")
library(widgetframe)
frameWidget(plotly::ggplotly(plot1))
Avatar
Robert W. Walker
Associate Professor of Quantitative Methods

My research interests include causal inference, statistical computation and data visualization.

Next
Previous

Related