February 24, 2020

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: 15 × 2
##    id               title                                                       
##    <chr>            <chr>                                                       
##  1 DDDM04USA156NWDB Outstanding Domestic Public Debt Securities to GDP for Unit…
##  2 DDDM04CNA156NWDB Outstanding Domestic Public Debt Securities to GDP for China
##  3 DDDM04MXA156NWDB Outstanding Domestic Public Debt Securities to GDP for Mexi…
##  4 DDDM04JPA156NWDB Outstanding Domestic Public Debt Securities to GDP for Japan
##  5 WLSFAL           Liabilities: Deposits with F.R. Banks, Other Than Reserve B…
##  6 DDDM04INA156NWDB Outstanding Domestic Public Debt Securities to GDP for India
##  7 DDDM04PLA156NWDB Outstanding Domestic Public Debt Securities to GDP for Pola…
##  8 DDDM04MYA156NWDB Outstanding Domestic Public Debt Securities to GDP for Mala…
##  9 DDDM04GRA156NWDB Outstanding Domestic Public Debt Securities to GDP for Gree…
## 10 DDDM04IDA156NWDB Outstanding Domestic Public Debt Securities to GDP for Indo…
## 11 WDSFAL           Factors Absorbing Reserve Funds: Deposits with Federal Rese…
## 12 DDDM04DEA156NWDB Outstanding Domestic Public Debt Securities to GDP for Germ…
## 13 DDDM04BRA156NWDB Outstanding Domestic Public Debt Securities to GDP for Braz…
## 14 DDDM04THA156NWDB Outstanding Domestic Public Debt Securities to GDP for Thai…
## 15 DDDM04PKA156NWDB Outstanding Domestic Public Debt Securities to GDP for Paki…

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 <- Ctry.DD %>% filter(!str_starts(Country, "Factors")) %>% ggplot(., 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") + guides(color="none")
library(widgetframe)
plotly::ggplotly(plot1)