API - FRED

remove(list=ls())

FRED_API <- "e409023add8fd4542636146cae3656d2"

Install fredr package

# install.packages("fredr")

set your key

library(fredr)

fredr_set_key(FRED_API)

Search for data

fredr_series_search_text(search_text = "gdp", 
                         order_by = "popularity",
                         limit = 20)
# A tibble: 20 × 16
   id        realtime_start realtime_end title observation_start observation_end
   <chr>     <chr>          <chr>        <chr> <chr>             <chr>          
 1 GDP       2024-07-31     2024-07-31   Gros… 1947-01-01        2024-04-01     
 2 GDPC1     2024-07-31     2024-07-31   Real… 1947-01-01        2024-04-01     
 3 GFDEGDQ1… 2024-07-31     2024-07-31   Fede… 1966-01-01        2024-01-01     
 4 PAYEMS    2024-07-31     2024-07-31   All … 1939-01-01        2024-06-01     
 5 M2V       2024-07-31     2024-07-31   Velo… 1959-01-01        2024-04-01     
 6 A939RX0Q… 2024-07-31     2024-07-31   Real… 1947-01-01        2024-04-01     
 7 A091RC1Q… 2024-07-31     2024-07-31   Fede… 1947-01-01        2024-04-01     
 8 FYFSGDA1… 2024-07-31     2024-07-31   Fede… 1929-01-01        2023-01-01     
 9 GDPPOT    2024-07-31     2024-07-31   Real… 1949-01-01        2034-10-01     
10 A191RL1Q… 2024-07-31     2024-07-31   Real… 1947-04-01        2024-04-01     
11 GDPDEF    2024-07-31     2024-07-31   Gros… 1947-01-01        2024-04-01     
12 GDPNOW    2024-07-31     2024-07-31   GDPN… 2011-07-01        2024-07-01     
13 CP        2024-07-31     2024-07-31   Corp… 1947-01-01        2024-01-01     
14 NROU      2024-07-31     2024-07-31   Nonc… 1949-01-01        2034-10-01     
15 JHDUSRGD… 2024-07-31     2024-07-31   Date… 1967-10-01        2024-01-01     
16 DPCCRV1Q… 2024-07-31     2024-07-31   Pers… 1959-04-01        2024-04-01     
17 W006RC1Q… 2024-07-31     2024-07-31   Fede… 1947-01-01        2024-01-01     
18 HDTGPDUS… 2024-07-31     2024-07-31   Hous… 2005-01-01        2023-10-01     
19 FGEXPND   2024-07-31     2024-07-31   Fede… 1947-01-01        2024-04-01     
20 M1V       2024-07-31     2024-07-31   Velo… 1959-01-01        2024-04-01     
# ℹ 10 more variables: frequency <chr>, frequency_short <chr>, units <chr>,
#   units_short <chr>, seasonal_adjustment <chr>,
#   seasonal_adjustment_short <chr>, last_updated <chr>, popularity <int>,
#   group_popularity <int>, notes <chr>

Load the Data

?fredr

fredr(series_id = "GDPC1")
# A tibble: 310 × 5
   date       series_id value realtime_start realtime_end
   <date>     <chr>     <dbl> <date>         <date>      
 1 1947-01-01 GDPC1     2183. 2024-07-25     2024-07-25  
 2 1947-04-01 GDPC1     2177. 2024-07-25     2024-07-25  
 3 1947-07-01 GDPC1     2172. 2024-07-25     2024-07-25  
 4 1947-10-01 GDPC1     2206. 2024-07-25     2024-07-25  
 5 1948-01-01 GDPC1     2240. 2024-07-25     2024-07-25  
 6 1948-04-01 GDPC1     2277. 2024-07-25     2024-07-25  
 7 1948-07-01 GDPC1     2290. 2024-07-25     2024-07-25  
 8 1948-10-01 GDPC1     2292. 2024-07-25     2024-07-25  
 9 1949-01-01 GDPC1     2261. 2024-07-25     2024-07-25  
10 1949-04-01 GDPC1     2253. 2024-07-25     2024-07-25  
# ℹ 300 more rows
GDP<-
fredr(series_id = "GDPC1",
      observation_start = as.Date("1947-01-01"),
      observation_end = as.Date("2024-04-01")
      )

Plot

library(ggplot2)

ggplot(data = GDP, 
       mapping = aes(x = date, 
                     y = value)
       ) + geom_line() + geom_line(colour = "deeppink") + labs(title = "US Real GDP over time",
                                                               x = "Time",
                                                               y = "In Billions of Dollars")