Analyzing US automotive industry using fredr, a FRED package in R

Linnart Felkl M.Sc. ETH
Analytics Vidhya
Published in
3 min readJan 2, 2020
Photo by Carlo D'Agnolo on Unsplash

Using public FRED data I analyze historical US vehicle market data, showing historical trends in US automotive market.

FRED is an abbreviation for the US Federal Reserve System and it provides public data related to US economy in an online database. This database comprises various indicators relevant to economic research. E.g. time series data on US domestic car production, vehicle sales, vehicle imports and much more related to US automotive market.

Below I set up the FRED API key for retrieving FRED data in R, using the fredr package available. I proceed by searching the database for “car production”. Using this search-term the fredr_series_search_text function returns a list with dataset summaries related to FRED US car production data.

# install fredr package directly from CRAN
#install.packages("fredr")
# load package
library(fredr)
## Warning: package 'fredr' was built under R version 3.6.2# set fredr API key
fredr_set_key(api_key) # api_key string must be retrieved from https://research.stlouisfed.org/useraccount/apikey
# search database for a car production series
search_ls <- fredr_series_search_text("car production")
# view column names of series search result list
colnames(search_ls)
## [1] "id" "realtime_start"
## [3] "realtime_end" "title"
## [5] "observation_start" "observation_end"
## [7] "frequency" "frequency_short"
## [9] "units" "units_short"
## [11] "seasonal_adjustment" "seasonal_adjustment_short"
## [13] "last_updated" "popularity"
## [15] "group_popularity" "notes"

Below chart is generated based on FRED time series data, accessing one of the datasets comprised by the search result. The chart summarizes historical monthly development of seasonally adjusted US domestic car production in thousands of units. Visualization is done in R, using the ggplot2 package.

# loading ggplot2 R-package
library(ggplot2)
# DAUPSA is id for seasonlly adjusted monthly domestic car production in units
series_ls <-fredr_series_observations(series_id = "DAUPSA")
# convert series list to dataframe
series_df <- do.call(cbind.data.frame, series_ls)
# plotting data
ggplot(series_df) + geom_line(mapping = aes(x=date,y=value),
color = "red") +
ggtitle("Monthly US car production, seasonally adjusted [in thousands]") +
xlab("time") +
ylab("monthly cars produced [thousands of units]")
Fig. 1: Monthly US domestic car production, seasonally adjusted [source: FRED database]

Above time series data starts in year 1993. Since then, domestic car production has declined.

The public FRED database also comprises time series data on quarterly US sales of new imported cars. Below chart displays historical sales trends in billion USD, considering quarterly sales of new vehicles imported to the US.

# B149RC1Q027SBEA is id for US domestic sales of imported new cars, seasonally adjusted and in billions of USD
series_df <-do.call(cbind.data.frame,
fredr_series_observations(series_id = "B149RC1Q027SBEA"))
# plotting data
ggplot(series_df) + geom_line(mapping = aes(x=date,y=value),
color = "red") +
ggtitle("Quarterly US imported new car sales, seasonally adjusted [in billion USD]") +
xlab("time") +
ylab("quarterly new imported car sales [billion USD]")
Fig. 2: Quarterly US sales of new imported vehicles [source: FRED database]

Sales of imported vehicles has long been growing. The trend only recently turned negative (2009 — global financial crisis, last 2–3 years — negative trend in US vehicle demand).

This simple example illustrats how FRED data can be retrieved and processed directly in R, e.g. as part of some major work flow or analysis. Such an approach will come in handy for some complex multi-step analysis procedures in which multiple sources of data must be considered. The possibility of accessing FRED data directly from the R script simplifies such tasks.

For more coding examples on how to use FRED (or e.g. OECD) data in R visit SupplyChainDataAnalytics.com

--

--