FRED data in R
Published:
Introduction:
In this blog post, we will learn how to use FRED data [Federal Reserve Economic Data FRED St. Louis Fed](https://fred.stlouisfed.org/) - In this example, we will graph male and female unemployment rates over time.
Setting Up:
- We will need to install the
fredr
andggplot2
packages if we do not have them installed. - You will als0 need to get your own personal API key. An API is a method for your R program to interface with FRED website directly without the use of a browser and manually downloading the data. You can get a FRED API for fre at the following website https://fred.stlouisfed.org/account/manage/api_key
# Install the package if you haven't already
if(!require(fredr)){install.packages("fredr")}
if(!require(ggplot2)){install.packages("ggplot2")}
# Load the package
library(fredr)
library(ggplot2)
# You'll need an API key from FRED. Get one for free at:
# https://fred.stlouisfed.org/docs/api/fred/
# Set your API key (replace with your actual key)
fredr_set_key("YOUR_API_KEY") # You only need to do this once per session
# or set it as an environment variable
Sys.setenv(FRED_API_KEY = "YOUR_API_KEY")
fredr_set_key(Sys.getenv("FRED_API_KEY"))
Data:
- Each FRED dataset has an individual ID. The easiest way to find this ID is to go to FRED website, enter in the series you are interested in within the search bar, then select the appropriate series. At the very bottom of the webpage, you will find
Source: U.S. Bureau of Labor Statistics Release: Employment Situation
Units: Percent, Seasonally Adjusted
Frequency: Monthly
Notes: The series comes from the ‘Current Population Survey (Household Survey)’
The source code is: LNS14000001
Suggested Citation: U.S. Bureau of Labor Statistics, Unemployment Rate - Men [LNS14000001], retrieved from FRED, Federal Reserve Bank of St. Louis; https://fred.stlouisfed.org/series/LNS14000001, January 31, 2025.
Notice the code LNS14000001
. This code is the series ID. We will need that.
# --- Download Data ---
# You'll need to find the specific FRED series IDs for male and female unemployment.
# Search on the FRED website (fred.stlouisfed.org).
# Common series IDs (but verify these are what you want!):
# LNS14000002 for Female Unemployment Rate
# LNS14000001 for Male Unemployment Rate
female_unemp <- fredr(series_id = "LNS14000002", observation_start = as.Date("2000-01-01"), observation_end = as.Date("2023-10-01"))
male_unemp <- fredr(series_id = "LNS14000001", observation_start = as.Date("2000-01-01"), observation_end = as.Date("2023-10-01"))
# --- Data Preparation ---
# Merge the data (important: make sure the dates align)
unemp_data <- merge(female_unemp, male_unemp, by = "date", suffixes = c("_female", "_male"))
# Female to Male Unemployment Gap Variable
unemp_data$diff_value <- unemp_data$value_female-unemp_data$value_male
ggplot(data = unemp_data, aes(x=date, y=diff_value))+geom_line()+ylab("Diff. in Unemployment Rate")+theme_classic()+labs(title = "Female - Male Gender Gap in Unemployment Rates")
Interpreting Results in an Economic Context:
- We can see from the figure that the great recession (mid-2007 to June of 2009) affected male unemployment greater than female unemployment, but the COVID recession affected female unemployment rated more than male unemployment rates.
- The mean difference in female - male unemployment rates over the time period considered is -0.366. That is, the unemployment rate for men is slightly higher than the unemployment rate for women between the years 2000 to 2023.
Conclusion:
- the FREDR package allows us to quickly obtain data available from the Federal Reserve Economic Data website and use it in R.