Federal Reserve Economic Data

The FRED® Blog

Leveraging R for powerful data analysis

FRED is a treasure trove of economic data, offering a vast array of time series data on everything from GDP and unemployment rates to interest rates and housing prices.

R is a powerful and flexible open-source programming language, widely used in data science, statistics, and machine learning. Its extensive ecosystem of packages allows users to perform complex data analysis, visualization, and modeling tasks.

FRED is user friendly and a great starting point for many. Those looking for more advanced analysis and customization often turn to programming languages like R. Today we describe one R package that works especially well with FRED data: fredo

In general, the fredo R package streamlines the interaction with the FRED API, provides an accessible interface, and allows for much customization. 

Key features of fredo

  • Automated data retrieval: Retrieve FRED data (including historical data) directly from the API using the fredo package, simplifying the process of downloading and processing the data.
  • Flexible plotting: With plot_fredo, create individual or combined plots with extensive customization, including adding recession bars, changing plot dimensions, and generating LaTeX code.
  • Data filtering: Filter data based on start and end dates for focused analysis.
  • Export options: Save plots as PDFs for use in reports, papers, or presentations.

Installation

To install fredo, use the following command to get it directly from GitHub:

# Install devtools if you haven't already
install.packages("devtools")

# Install fredo package from GitHub
devtools::install_github("manutzn/fredo")

Once installed, load the packages into your R session:

library(fredo)
library(jsonlite)

Setting up your FRED API Key

To use fredo to fetch data from the FRED API, you need to obtain an API key:

  1. Register for an API Key:
  2. Store Your API Key Securely:
    • Option 1: Assign it directly in your R script (not recommended for shared code).
      api_key <- "your_api_key_here"
    • Option 2: Set it as an environment variable in your .Renviron file. Add the following line to your .Renviron file:
      FRED_API_KEY=your_api_key_here

      Then access it in R using:

      api_key <- Sys.getenv("FRED_API_KEY")

The fredo function

The main function in the package is fredo(), which allows you to fetch data from the FRED API. The function retrieves data for specified series IDs over a date range and returns a data frame for easy analysis.

Function Signature

fredo(api_key, series_ids, start_date, end_date)

Parameters
api_key: Your FRED API key as a string.
series_ids: A character vector of FRED series IDs to retrieve (e.g., c(“GNPCA”, “UNRATE”)).
start_date: The start date for data retrieval in ‘YYYY-MM-DD’ format.
end_date: The end date for data retrieval in ‘YYYY-MM-DD’ format.

Returns
A data frame containing the combined data for all specified series, with columns including ‘date’, ‘value’, ‘id’, and ‘title’.

Basic Usage of fredo
Here’s how to use fredo to fetch data from the FRED API:

# Set your FRED API key
api_key <- Sys.getenv("FRED_API_KEY")  # Recommended method

# Define the series IDs and date range
series_ids <- c("GNPCA", "UNRATE")
start_date <- "1950-01-01"
end_date <- "2024-12-31"

# Fetch the data

Sample Output:

        date    value    id                        title
1 1950-01-01  2198.50 GNPCA Real Gross National Product
2 1951-01-01  2373.70 GNPCA Real Gross National Product
3 1952-01-01  2474.70 GNPCA Real Gross National Product
4 1953-01-01  2607.70 GNPCA Real Gross National Product
5 1954-01-01  2635.10 GNPCA Real Gross National Product
6 1955-01-01  2808.50 GNPCA Real Gross National Product

Advanced features and customization with plot_fredo
The plot_fredo function offers several parameters to customize the generated plots:

Combining multiple series
To combine multiple series into one plot, set the combine parameter to TRUE:

plot_fredo(
  dataset = fred_data,
  path = "output_directory/",
  combine = TRUE
)

Adding or removing recession bars
You can include recession bars in your plots by default. To remove them, set show_recessions to FALSE:

plot_fredo(
  dataset = fred_data,
  path = "output_directory/",
  show_recessions = FALSE
)

Adjusting plot size
Customize the dimensions of your plot by setting plot_width and plot_height in centimeters:

plot_fredo(
  dataset = fred_data,
  path = "output_directory/",
  plot_width = 20,
  plot_height = 15
)

Example workflow

# Define the series IDs and date range
series_ids <- c("GNPCA", "UNRATE")
start_date <- "1950-01-01"
end_date <- "2024-12-31"

# Fetch the data
data <- fredo(api_key, series_ids, start_date, end_date)

plot_fredo(
  dataset = data,
  path = "output_directory/",
  plot_width = 20,
  plot_height = 15
)

For more information, examples, and updates, visit the GitHub repository.

Suggested by Manu Garcia and Carlos Garriga.

Unemployment flows and labor dynamics

Recent research from the St Louis Fed

The FRED Blog has discussed the four possible reasons for being (and being categorized as) unemployed: From most to least prevalent, they are involuntarily losing a job, voluntarily leaving a job, looking for work after being out of the labor force for a while, and looking for work for the first time.

The FRED graph above shows data from the US Bureau of Labor Statistics breaking down the headline unemployment rate into these four reasons. The data are stacked to easily observe the change in the unemployment rate, and we can see a gradual rising trend between April 2023 and August 2024.

Recent research by Maximiliano A. Dvorkin and Serdar Ozkan at the St. Louis Fed offers new insights into unemployment flows during that period. They find that more-frequent job separations are the main reason behind the rising unemployment rate. Moreover, their research puts this trend in context by comparing inflows and outflows in the labor market during three different recessions: 1990-91, 2001, and 2007-09. They conclude that upticks in the proportion of people losing their jobs have been observed before the start of past recessions and that keeping an eye on labor dynamics may provide an early indication of a change in broad economic conditions.

For more about this and other research, visit the publications page of the St. Louis Fed’s website, which offers an array of economic analysis and expertise provided by our staff.

How the graph was created: Browse FRED data by Release and navigate to “Employment Situation > Release Tables > Current Population Survey (Household Data) > Table A-11. Unemployed persons by reason for unemployment.” Next, click on the boxes to the left of three categories of “Unemployed as a Percent of the Civilian Labor Force” at the bottom of the page. Last, click on the “Add to Graph” button.

Suggested by Diego Mendez-Carbajo.

Online dating and marriage

Recent insights from the Research Division

The FRED Blog has examined how the internet has affected our choices of where to shop for certain goods and services. And recent research from Paulina Restrepo-Echavarría at the St. Louis Fed has examined how the internet may have affected our choices related to dating and marriage.

New trends in dating and marriage aside, most households are still occupied by married couples, although the fraction of these households dropped from a peak of 87.8% in 1953 to 73.7% in 2023.

The FRED graph above shows data from the US Census Bureau on types of households, which are categorized by the person or persons in whose name the housing unit is rented or owned: married couples in blue, male householders in red, and female householders in green. These annual data are displayed in a stacked area graph to easily compare the relative amounts of these family household types.

Restrepo-Echavarría’s research compares married couples in the 2008-2021 period, as the use of online dating was rising, with those from the pre-internet years of 1960-1980. She and her coauthors find that people have increasingly been marrying someone more like themselves, with the same income, education, and skill levels.

They also look at some of the economic effects of this homogeneity, which they find has contributed to the increase in household income inequality between 1980 and 2020. For more about this and other research, visit the publications page of the St. Louis Fed’s website, which offers an array of economic analysis and expertise provided by our staff.

How this graph was created: Search FRED for and select “Family Households with Married Couples.” Click on the “Edit Graph” button, select the “Add Line” tab, and search for “Family Households with Male Householder.” Don’t forget to click “Add data series.” Repeat the last search step to add “Family Households with Female Householder” to the graph. Last, use the “Format” tab to select “Graph type: Area” and “Stacking: Percent.”

Suggested by Diego Mendez-Carbajo.



Subscribe to the FRED newsletter


Follow us

Back to Top