1 Register for FRED API key

https://fred.stlouisfed.org/docs/api/fred/

remove(list=ls())

# install.packages("fredr")
library(fredr)

FRED_API_KEY="8a9ec1330374c1696f05cc8e526233b5"
fredr_set_key(FRED_API_KEY)

2 Search for Data

fredr_series_search_text(
    search_text = "gdp",
    order_by    = "popularity",
    sort_order  = "desc",
    limit       = 10
)
## # A tibble: 10 × 16
##    id        realtime_start realtime_end title observation_start observation_end
##    <chr>     <chr>          <chr>        <chr> <chr>             <chr>          
##  1 GDP       2024-08-03     2024-08-03   Gros… 1947-01-01        2024-04-01     
##  2 GDPC1     2024-08-03     2024-08-03   Real… 1947-01-01        2024-04-01     
##  3 GFDEGDQ1… 2024-08-03     2024-08-03   Fede… 1966-01-01        2024-01-01     
##  4 PAYEMS    2024-08-03     2024-08-03   All … 1939-01-01        2024-07-01     
##  5 M2V       2024-08-03     2024-08-03   Velo… 1959-01-01        2024-04-01     
##  6 A939RX0Q… 2024-08-03     2024-08-03   Real… 1947-01-01        2024-04-01     
##  7 A091RC1Q… 2024-08-03     2024-08-03   Fede… 1947-01-01        2024-04-01     
##  8 FYFSGDA1… 2024-08-03     2024-08-03   Fede… 1929-01-01        2023-01-01     
##  9 GDPPOT    2024-08-03     2024-08-03   Real… 1949-01-01        2034-10-01     
## 10 A191RL1Q… 2024-08-03     2024-08-03   Real… 1947-04-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>

3 Load Data

GDP <-
fredr(
  series_id = "GDP",
  observation_start = as.Date("1950-01-01"),
  observation_end = as.Date("2023-01-01"), 
  frequency = "q" # quarterly
)

4 Plot Data

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)


ggplot(data = GDP, 
       mapping = aes(x = date,
                     y = value, 
                     color = series_id)
       ) +
    geom_line() +
    labs(x = "Observation Date", 
         y = "Rate", 
         color = "Series"
         )

# Install the forecast package if you haven't already
# install.packages("forecast")

# Load the forecast package
library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
# Fit an ARIMA model
fit <- auto.arima(GDP$value)

# Print the model
summary(fit)
## Series: GDP$value 
## ARIMA(0,2,2) 
## 
## Coefficients:
##           ma1     ma2
##       -1.0404  0.1185
## s.e.   0.0555  0.0562
## 
## sigma^2 = 29897:  log likelihood = -1912.37
## AIC=3830.75   AICc=3830.83   BIC=3841.77
## 
## Training set error measures:
##                    ME     RMSE      MAE       MPE      MAPE      MASE
## Training set 15.50246 171.7226 55.01256 0.1957694 0.7915774 0.5075368
##                     ACF1
## Training set -0.01789321
# Forecast the next 12 periods
forecasted_values <- forecast(fit, h = 24)

# Plot the forecast
plot(forecasted_values)