FRED - Federal Reserve Economic Data
Since Stata 15, we can search, browse and import almost a million U.S. and international economic and financial time series made available by the St. Louis Federal Reserve’s Federal Research Economic Data. This post briefly explains this great feature.
Prerequisite
Before you start, you will need an API Key from FRED. Register one here
Then in Stata, you can store this key permanently so you don’t need to provide again.1
1 Replace _key_
with your actual API Key obtained.
set fredkey _key_, permanently
GUI is always a good start
Alternatively, click on menu File>Import>Fedearl Reserve Economic Data (FRED)
will bring up the dialog as shown below.
Enter API Key and you’ll be free to explore all the data series available on FRED.
For example, let’s see the CPI of Australia…
Describe the data series, we can find many useful meta info.
Note that “vintage” section lists a number of dates, with each vintage referring a particular version of the data series at that point of time.
It may sound strange but an economic data series may be revised multiple times after it has been published. Potential reasons may be that later people collect more accurate information, or that there is a change of estimation method, etc.2
2 For example, the CPI from 2005 to 2010 collected by a research as at 2011 may be different from the one collected as at 2023. Without specifying the data vintage, replicating a prior work can be hard.
3 For example, a trading strategy using the revised GDP accessed today, instead of the vintage GDP, implicitly uses hindsight as the GDP series may have been revised to accommodate more accurate data obtained after release.
Another tricky part is that ignoring vintages introduces look-ahead bias in analysis.3
Let’s close the description, double click on the series and click on import. Another dialog will be shown to confirm some final details.
The outputs will be like the following:
aggregate(quarterly,avg)
. import fred AUSCPIALLQINMEI, daterange(2010-01-01 2023-08-08)
Summary
--------------------------------------------------------------------------range Frequency
Series ID Nobs Date
--------------------------------------------------------------------------
AUSCPIALLQINMEI 53 2010-01-01 to 2023-01-01 Quarterly
--------------------------------------------------------------------------of series imported: 1
# frequency: Quarterly
highest frequency: Quarterly lowest
Programmatical is recipe to reproducibility
We don’t need to go through the GUI process every time. In fact, Stata already told us what the corresponding command is:
aggregate(quarterly,avg) import fred AUSCPIALLQINMEI, daterange(2010-01-01 2023-08-08)
We can simply put this line of code into our program.
For example, the code below generates a time-series chart for Australia’s CPI.
// Import
aggregate(quarterly,avg) clear
import fred AUSCPIALLQINMEI, daterange(2010-01-01 2023-03-31) vintage(2023-05-10) rename AUSCPIALLQINMEI_20230510 cpi_australia
// Time format
gen yrqtr = yq(year(daten),quarter(daten))
format yrqtr %tq
tsset yrqtr
// Set start of the period to 100
gen cpi_ret = cpi_australia/L.cpi_australia - 1
replace cpi_australia = 100 if _n==1
replace cpi_australia = L.cpi_australia * (1+cpi_ret) if _n>1
// Plotting
twoway (tsline cpi_australia), title("Quarterly CPI of Australia 2010Q1-2023Q1") ytitle("") ttitle("") note("Index 2010Q1=100. Source: FRED, 2023-05-10 vintage.")
The code snippet above specifies the data vintage. Therefore, even if someone runs it 30 years from now, they will still get exactly the same data and plot as I do in 2023.