Skip to content

Instantly share code, notes, and snippets.

@jkr216
Created November 11, 2023 18:12
Show Gist options
  • Select an option

  • Save jkr216/38536818aa7bd3fb2fe4c1284c1f5de8 to your computer and use it in GitHub Desktop.

Select an option

Save jkr216/38536818aa7bd3fb2fe4c1284c1f5de8 to your computer and use it in GitHub Desktop.

Revisions

  1. jkr216 created this gist Nov 11, 2023.
    65 changes: 65 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    # Inspired by the work of Rich Falk-Wallace

    library(tidyverse)
    library(tidyquant)
    library(timetk)
    library(readxl)
    library(plotly)
    library(scales)
    library(formattable)


    url <-
    "https://www.atlantafed.org/-/media/documents/cqer/researchcq/gdpnow/GDPTrackingModelDataAndForecasts.xlsx"
    destfile <- "GDPTrackingModelDataAndForecasts.xlsx"
    curl::curl_download(url, destfile)

    treasury_fred_symbols <-
    tibble(
    symbol = c("DGS1MO","DGS3MO",
    "DGS1", "DGS2",
    "DGS5", "DGS10",
    "DGS30" ),
    name = c("one_month","three_month",
    "one_year", "two_year",
    "five_year", "ten_year",
    "thirty_year") %>%
    as_factor()
    )

    treasury_data <-
    treasury_fred_symbols %>%
    tq_get(get = "economic.data", from = "1979-01-01") %>%
    rename(yield = price) %>%
    mutate(yield = yield/100)


    read_excel(destfile, sheet = "Contributions",
    skip = 1) %>%
    clean_names() %>%
    select(date, `GDP Nowcast`= gdp_forecast) %>%
    mutate(`GDP Nowcast` = `GDP Nowcast`/100,
    date = ymd(date)) %>%
    left_join(
    treasury_data %>%
    filter(str_detect(name, "ten")) %>%
    select(date, `10-YR Yield` = yield)
    ) %>%
    fill(`10-YR Yield`, .direction = c("down")) %>%
    pivot_longer(-date) %>%
    filter(date >= "2013-01-01") %>%
    mutate(value = case_when(date <= "2022-02-01" & date >= "2020-01-01" ~ NA_real_,
    TRUE ~ value)
    ) %>%
    ggplot(aes(x = date, y = value, color = name)) +
    geom_line() +
    scale_color_manual(values = c("orange", "steelblue")) +
    theme_minimal() +
    scale_y_continuous(labels = percent_format(accuracy = 1),
    breaks = pretty_breaks(n = 10)) +
    scale_x_date(date_labels = "%b %Y",
    date_breaks = "13 months") +
    labs(x = "", y = "", title = "GDP Now v. 10-yr (2014-Present)",
    color = "", caption = "Source: Rich Falk-Wallace") +
    theme(plot.title = element_text(hjust = .5),
    legend.position = c(.68, .88))