fred: Fetch Data Series from FRED

View source: R/fred.R

fredR Documentation

Fetch Data Series from FRED

Description

Function that fetches economic data from the FRED based on the series ids.

Usage

fred(
  ...,
  key = NULL,
  all = TRUE,
  info = TRUE,
  realtime_start = NULL,
  realtime_end = NULL,
  long = FALSE
)

Arguments

...

character vectors of series ids to search. Any names will be used to label the series in the resulting data.frame

key

32 character lower-cased alpha-numeric character string. If NULL, the key registered with set_fred_key is used.

all

logical; should all observations be used? Defaults to TRUE. If FALSE, only observations existing in all series will be kept.

info

logical; should information about each series, such as units and title, be kept? If so, the information will be stored in a data.frame under the attribute "info".

realtime_start, realtime_end

character date strings of format "YYY-MM-DD" used obtain information that was known during the specified time period. If empty, the latest update of the information is used.

long

logical; should the resulting data.frame be long or wide? Defaults to FALSE so that a new column is created for each series

Details

The fred function search for series ids from the FRED and returns the values in a data.frame. Unless long=TRUE, a new column will be created for each series. The dates for each observation will be included in the "date" column and will be of class "Date". Each series will be a numeric value.

An API key is required. Request a free key at https://fred.stlouisfed.org/docs/api/api_key.html and register it for the session with set_fred_key, or pass it through key.

If a series id cannot be found, then it will be skipped and R will issue a warning. If the FRED API itself cannot be reached, or returns a response that cannot be parsed, an informative message is printed and no data are returned. In either case the remaining series are still returned; if no series at all could be retrieved, the function returns NULL.

To access ALFRED, or the archived Federal Reserve Economic Data, use the realtime_start and realtime_end arguments to specify a time frame. More details about real time periods can be found at https://fred.stlouisfed.org/docs/api/fred/realtime_period.html.

Value

data.frame

Examples

## Not run: 
api_key <- "abcdefghijklmnopqrstuvwxyz123456"

# Naive Phillips Curve Estimation
df <- fred(p = "CPIAUCSL", u = "u6rate", key=api_key)
head(df)

df$pi  <- log(df$p) - log(c(NA, df$p[2:nrow(df)]))
reg_pc <- lm(pi ~ u, data = df)
summary(reg_pc)

# Different variations of the CPI
cpis <- c("CPIMEDSL", "CPIFABSL", "CPIHOSSL")
names(cpis) <- c("P_Med", "P_Food", "P_House")

df_cpi <- fred(cpis, key=api_key, long=TRUE)
head(df)

## End(Not run)

eFRED documentation built on Oct. 22, 2026, 5:23 p.m.