· 4 years ago · Sep 23, 2021, 02:10 PM
1library(tidyverse)
2library(fredr)
3library(purrr)
4library(scales)
5
6# Set FRED API key to access the FRED database.
7# You may request an API key at:
8# https://research.stlouisfed.org/useraccount/apikeys
9fredr_set_key(api_key_fred)
10
11### What are the start/end dates you wish to graph
12date_start <- as.Date("1949-01-01") # <- Earliest date
13date_start <- as.Date("2000-01-01")
14date_end <- as.Date(Sys.Date())
15
16### Data to load from FRED
17fred_series <-
18 tribble(
19 ~ series_id, ~frequency, ~units,
20 "GDP", "q", "lin", # Nominal GDP
21 "GDPC1", "q", "lin", # Real GDP
22 "GDPPOT", "q", "lin", # Potential GDP
23 "GDPDEF", "q", "pc1", # Gross Domestic Product: Implicit Price Deflator
24 "CPILFESL", "m", "pc1", # Consumer Price Index for All Urban Consumers: All Items Less Food and Energy in U.S. City Average
25 "UNRATE", "m", "lin", # Unemployment Rate
26 "FEDFUNDS", "m", "lin", # Federal Funds Rate
27 )
28
29# Use purrr to pull all of the series from FRED
30data_fred <-
31 purrr::pmap_dfr(.l = fred_series, .f = fredr) %>%
32 select(date, series_id, value) %>%
33 pivot_wider(id_cols = "date", names_from = "series_id", values_from = "value") %>%
34 arrange(date)
35
36# Filter data to the time window specified
37data_fred <- data_fred %>% filter(date >= date_start & date <= date_end)
38
39# Compute the Taylor/Mankiw Rule estimates
40data_fred <-
41 data_fred %>%
42 mutate(Taylor_Rule = GDPDEF + 2 + 0.5 * (GDPDEF - 2) + 0.5 * ((GDPC1 - GDPPOT) / GDPPOT) * 100) %>%
43
44 # Compute Mankiw rule using constants from Mankiw's original paper at
45 # http://scholar.harvard.edu/files/mankiw/files/us_monetary_policy_during_the_1990s.pdf
46 # mutate(Mankiw_Rule = 8.5 + 1.4 * (CPILFESL - UNRATE))
47
48 # Compute Mankiw rule using updated constants by Lars Christensen
49 # at https://marketmonetarist.com/2014/09/16/mankiw-rule-tells-the-fed-to-tighten/
50 mutate(Mankiw_Rule = 9.1 + 2.1 * (CPILFESL - UNRATE))
51
52##########################################################################
53### Graph: Taylor Rule, Mankiw Rule, and Fed Rate
54##########################################################################
55c1 <- "U.S. Bureau of Economic Analysis, Real Gross Domestic Product [GDPC1]\n"
56c2 <- "U.S. Congressional Budget Office, Real Potential Gross Domestic Product [GDPPOT]\n"
57c3 <- "U.S. Bureau of Economic Analysis, Gross Domestic Product: Implicit Price Deflator [GDPDEF]\n"
58c4 <- "U.S. Bureau of Labor Statistics, Consumer Price Index for All Urban Consumers: All Items Less Food and Energy in U.S. City Average [CPILFESL]\n"
59c5 <- "U.S. Bureau of Labor Statistics, Unemployment Rate [UNRATE]\n"
60c6 <- "Board of Governors of the Federal Reserve System (US), Effective Federal Funds Rate [FEDFUNDS]\n"
61c7 <- "retrieved from FRED, Federal Reserve Bank of St. Louis; https://fred.stlouisfed.org\n"
62c8 <- paste("Data retrieved on", format(Sys.time(), "%B %d, %Y at %I:%M %p %Z"))
63
64caption <- "" #paste(c1, c2, c3, c4, c5, c6, c7, c8)
65
66# Initialize the plot
67p_mankiw_taylor <-
68 ggplot() +
69 theme_bw() +
70 theme(legend.title = element_blank()) +
71 theme(legend.position = "bottom") +
72 # Plot the Fed Rate
73 geom_line(data = data_fred, size = 1.3, aes(x = date, y = FEDFUNDS/ 100, color = "Federal Funds Rate")) +
74
75 # Plot the Taylor rule
76 geom_line(data = data_fred %>% filter(!is.na(Taylor_Rule)), size = 1.3, aes(x = date, y = Taylor_Rule / 100, color = "Taylor Rule")) +
77
78 # Plot the Mankiw rule
79 geom_line(data = data_fred %>% filter(!is.na(Mankiw_Rule)), size = 1.3, aes(x = date, y = Mankiw_Rule / 100, color = "Mankiw Rule")) +
80
81 geom_hline(yintercept = 0, linetype = "dotted") +
82
83 # Add recession bars
84 geom_recession_bars(min(data_fred$date), max(data_fred$date)) +
85
86 scale_y_continuous(breaks = pretty_breaks(7), labels = scales::percent_format(accuracy = 1)) +
87 scale_x_date(breaks = pretty_breaks(8)) +
88 scale_colour_manual(values = c("Federal Funds Rate" = "steelblue2",
89 "Taylor Rule" = "goldenrod",
90 "Mankiw Rule" = "red3"
91 )) +
92
93 # Set the title, subtitle, captions, xlab, and ylab
94 labs(title = "Taylor Rule, Mankiw Rule, and Fed Rate",
95 subtitle = "Recessions marked with vertical bars",
96 caption = caption, x = "", y = "")
97
98print(p_mankiw_taylor)