In previous posts I showed how to retrieve and use data from Twitter (Twitter), Yahoo Finance (quantmod), The Guardian (guardianR) and OECD (oecd R-package). In this post I show how to access FRED data with an API key, using the R-package available: fredr. A good documentation for getting started with the fredr package has also been published by the authors: http://sboysel.github.io/fredr/articles/fredr.html
I demonstrate the data retrieval process for FRED time series data using US domestic car production and imported car sales as an example.
First step is to setup the FRED API-key in the R script. For this you must register at FRED and copy-paste the API-key. You can do this here: https://research.stlouisfed.org/useraccount/apikey
# 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
Once the FRED API-key has been setup using the fredr_set_key function the fredr_series_search_text function will enable a time series search in the FRED database:
# 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)
##  "id" "realtime_start" ##  "realtime_end" "title" ##  "observation_start" "observation_end" ##  "frequency" "frequency_short" ##  "units" "units_short" ##  "seasonal_adjustment" "seasonal_adjustment_short" ##  "last_updated" "popularity" ##  "group_popularity" "notes"
The fredr_series_search_text function returns a list that summarizes the search results, providing an overview of relevant IDs and important dataset characteristics, such as e.g. seasonal adjustment.
Using the fredr_series_observations one can retrieve the desired time series data by specifying the relevant series id. With the do.call function one can convert the returned time series list into a data frame, plotable in ggplot2:
# 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]")
Above chart summarizes US domestic car production in units, since 1993 (monthly figures, seasonally adjusted). As can be seen historical production volume has declinded.
But how about new car imports? Below I repeat above work flow for imported new car sales in billion USD, quarterly reported and seasonally adjusted:
# 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]")
Data scientist focusing on simulation, optimization and modeling in R, SQL, VBA and Python