11 April 2017

IN THIS POST I AM going to share some useful code to create some custom plots using the St Louis Federal Reserve Economic Database (FRED). While the FRED page has some nice chart customization options, I’m going to import the data into R with the quantmod package and draw the plots.

I find myself doing these types of things often enough that I thought you might find these bits o’ code useful.

The example I’m going to use is a comparison of house prices, rents, income and other prices. We talked more about these data in a post last year.

Here goes. Execute the code below and get the plot at bottom.

### Load libraries
library(viridis)  # for the colors
library(htmlTable)  # for the table

#  Here are some variables and a brief description, for more see FRED

my.names <- data.table(var=c("A229RC0","CUSR0000SA0L2","CUUR0000SEHA","HPIPONM226S"),
                       Description=c("Per capita disposable income",
                                     "CPI-U All items less shelter",
                                     "CPI-U Rent of primary residences",
                                     "FHFA Purchase-only house price index"),
                       Source=c("U.S. Bureau of Economic Analysis",
                                "U.S. Bureau of Labor Statistics",
                                "U.S. Bureau of Labor Statistics",
                                "Federal Housing Finance Agency (FHFA)"))

htmlTable(my.names, caption="Data description",tfoot="Accessed via: St Louis Federal Reserve Economic Database (FRED)")
Data description
var name Description Source
1 A229RC0 income Per capita disposable income U.S. Bureau of Economic Analysis
2 CUSR0000SA0L2 cpi.less.shelter CPI-U All items less shelter U.S. Bureau of Labor Statistics
3 CUUR0000SEHA cpi.rent CPI-U Rent of primary residences U.S. Bureau of Labor Statistics
4 HPIPONM226S hpi FHFA Purchase-only house price index Federal Housing Finance Agency (FHFA)
Accessed via: St Louis Federal Reserve Economic Database (FRED)

Load data

# Use Quantmod to load data
# helpful reference https://jeffreybreen.wordpress.com/tag/quantmod/

df= getSymbols('CUUR0000SEHA',src='FRED', auto.assign=F) 
df = data.frame(date=time(df), coredata(df) )

df.hpi =getSymbols('HPIPONM226S',src='FRED', auto.assign=F) 
df.hpi = data.frame(date=time(df.hpi), coredata(df.hpi) )

df.inc=getSymbols('A229RC0',src='FRED', auto.assign=F) 
df.inc = data.frame(date=time(df.inc), coredata(df.inc) )

df2= getSymbols('CUSR0000SA0L2',src='FRED', auto.assign=F) 
df2 = data.frame(date=time(df2), coredata(df2) )

Now we’ll want to merge all our data together

# merge the data

# Gather the data to make it tidy

dt %>% gather(var,value,-date) %>% data.table() ->dt2

# Merge on variable names


# Create and index with January 1991 = 100
# Count by var
dt2=dt2[,id:=1:.N, by=var]  # Index running from 1:N by group (var)
dt2=dt2[,var0:=100*value/sum(ifelse(id==1,value,0)),by=var] #create index

Make the plot

# Create caption
mycaption<- "@lenkiefer Source: House Prices: FHFA purchase-only house price index. Rent: U.S. Bureau of Labor Statistics (BLS) consumer price index rent of primary residence. Other Prices: BLS consumer price index all items less shelter. Income: U.S. Bureau of Economic Analysis per capita disposable personal income (Table 2.6.). All are seasonally adjusted."

# Wrap caption 120 characters:
mycaption <- paste0(strwrap(mycaption, 120), sep="", collapse="\n")

# Create Plot
  labs(x="",y="Index, January 1991=100 (log scale)",
       title="Comparing house prices to rent, income and other prices",
       caption=mycaption       )

plot of chunk fig-04-011-2017-4

This plot shows that while house prices are growing faster that incomes and other prices in recent years, if we look at things over a 26 year period (1991-2016) house prices haven’t risen quite as fast as incomes. Though the story can vary a lot based on specific regions.


This little bit of code demonstrates how to get data from FRED into R. It also shows how to use a mixture of tidyr and data.table() to manipulate data. And finally, we used some ggplot2 to create some plots.

I use parts of these steps on a regular basis to make graphs charting trends in the economy and housing markets. Maybe you can find it useful too.