MetricT

Velocity of M2 Money Stock, 1869 - Present

May 27th, 2020
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 6.75 KB | None | 0 0
  1. ################################################################################
  2. ### Velocity of M2 Money Stock, 1869 to present
  3. ###    by /u/MetricT
  4. ################################################################################
  5.  
  6. library("tidyverse")
  7. library("fredr")
  8.  
  9. ### Set your FRED API key to access the FRED database.
  10. ### You may request an API key at:
  11. ### https://research.stlouisfed.org/useraccount/apikeys
  12. api_key_fred <- "PUT_YOUR_FRED_API_KEY_HERE"
  13. fredr_set_key(api_key_fred)
  14.  
  15. ################################################################################
  16. ### Add recession bars to our graph using NBER dates
  17. ################################################################################
  18. geom_recession_bars <- function(date_start, date_end, fill = "darkgray") {
  19.  
  20.   date_start <- as.Date(date_start, origin = "1970-01-01")
  21.   date_end   <- as.Date(date_end,   origin = "1970-01-01")
  22.  
  23.   recessions_tibble <- tibble(
  24.  
  25.     peak = as.Date(
  26.       c("1857-06-01", "1860-10-01", "1865-04-01", "1869-06-01",
  27.         "1873-10-01", "1882-03-01", "1887-03-01", "1890-07-01",
  28.         "1893-01-01", "1895-12-01", "1899-06-01", "1902-09-01",
  29.         "1907-05-01", "1910-01-01", "1913-01-01", "1918-08-01",
  30.         "1920-01-01", "1923-05-01", "1926-10-01", "1929-08-01",
  31.         "1937-05-01", "1945-02-01", "1948-11-01", "1953-07-01",
  32.         "1957-08-01", "1960-04-01", "1969-12-01", "1973-11-01",
  33.         "1980-01-01", "1981-07-01", "1990-07-01", "2001-03-01",
  34.         "2007-12-01")),
  35.  
  36.     trough = as.Date(
  37.       c("1858-12-01", "1861-06-01", "1867-12-01", "1870-12-01",
  38.         "1879-03-01", "1885-05-01", "1888-04-01", "1891-05-01",
  39.         "1894-06-01", "1897-06-01", "1900-12-01", "1904-08-01",
  40.         "1908-06-01", "1912-01-01", "1914-12-01", "1919-03-01",
  41.         "1921-07-01", "1924-07-01", "1927-11-01", "1933-03-01",
  42.         "1938-06-01", "1945-10-01", "1949-10-01", "1954-05-01",
  43.         "1958-04-01", "1961-02-01", "1970-11-01", "1975-03-01",
  44.         "1980-07-01", "1982-11-01", "1991-03-01", "2001-11-01",
  45.         "2009-06-01")
  46.     )
  47.   )
  48.  
  49.   recessions_trim <- recessions_tibble %>%
  50.     filter(peak   >= min(date_start) &
  51.              trough <= max(date_end))
  52.  
  53.   if (nrow(recessions_trim) > 0) {
  54.  
  55.     recession_bars <- geom_rect(data        = recessions_trim,
  56.                                 inherit.aes = F,
  57.                                 fill        = fill,
  58.                                 alpha       = 0.25,
  59.                               aes(xmin = as.Date(peak,   origin = "1970-01-01"),
  60.                                   xmax = as.Date(trough, origin = "1970-01-01"),
  61.                                   ymin = -Inf, ymax = +Inf))
  62.   } else {
  63.  
  64.     recession_bars <- geom_blank()
  65.   }
  66.  
  67. }
  68.  
  69. ################################################################################
  70. ### Data from 1869 to 1960 is from Friedman & Schwartz
  71. ### "A Monetary History of the United States, 1867-1960", table A-5
  72. ################################################################################
  73.  
  74. money_velocity_before_1960 <- tibble(
  75.  
  76.   date = as.Date(c(
  77.  
  78.     "1869-01-01", "1870-01-01", "1871-01-01", "1872-01-01",
  79.     "1873-01-01", "1874-01-01", "1875-01-01", "1876-01-01",
  80.     "1877-01-01", "1878-01-01", "1879-01-01", "1880-01-01",
  81.     "1881-01-01", "1882-01-01", "1883-01-01", "1884-01-01",
  82.     "1885-01-01", "1886-01-01", "1887-01-01", "1888-01-01",
  83.     "1889-01-01", "1890-01-01", "1891-01-01", "1892-01-01",
  84.     "1893-01-01", "1894-01-01", "1895-01-01", "1896-01-01",
  85.     "1897-01-01", "1898-01-01", "1899-01-01", "1900-01-01",
  86.     "1901-01-01", "1902-01-01", "1903-01-01", "1904-01-01",
  87.     "1905-01-01", "1906-01-01", "1907-01-01", "1908-01-01",
  88.     "1909-01-01", "1910-01-01", "1911-01-01", "1912-01-01",
  89.     "1913-01-01", "1914-01-01", "1915-01-01", "1916-01-01",
  90.     "1917-01-01", "1918-01-01", "1919-01-01", "1920-01-01",
  91.     "1921-01-01", "1922-01-01", "1923-01-01", "1924-01-01",
  92.     "1925-01-01", "1926-01-01", "1927-01-01", "1928-01-01",
  93.     "1929-01-01", "1930-01-01", "1931-01-01", "1932-01-01",
  94.     "1933-01-01", "1934-01-01", "1935-01-01", "1936-01-01",
  95.     "1937-01-01", "1938-01-01", "1939-01-01", "1940-01-01",
  96.     "1941-01-01", "1942-01-01", "1943-01-01", "1944-01-01",
  97.     "1945-01-01", "1946-01-01", "1947-01-01", "1948-01-01",
  98.     "1949-01-01", "1950-01-01", "1951-01-01", "1952-01-01",
  99.     "1953-01-01", "1954-01-01", "1955-01-01", "1956-01-01",
  100.     "1957-01-01", "1958-01-01", "1959-01-01", "1960-01-01")),
  101.  
  102.   value = c(
  103.     4.57, 4.12, 3.91, 4.34,
  104.     4.35, 4.23, 3.99, 4.19,
  105.     4.48, 4.70, 4.67, 4.97,
  106.     4.10, 4.16, 3.76, 3.75,
  107.     3.43, 3.30, 3.22, 3.10,
  108.     3.06, 2.93, 2.94, 2.81,
  109.     2.87, 2.55, 2.71, 2.67,
  110.     2.81, 2.55, 2.48, 2.53,
  111.     2.47, 2.35, 2.34, 2.21,
  112.     2.18, 2.32, 2.30, 2.08,
  113.     2.23, 2.20, 2.09, 2.15,
  114.     2.17, 1.91, 1.90, 2.12,
  115.     2.18, 2.51, 2.28, 2.20,
  116.     1.90, 1.88, 2.04, 1.97,
  117.     1.88, 1.95, 1.87, 1.84,
  118.     1.95, 1.70, 1.47, 1.28,
  119.     1.38, 1.52, 1.52, 1.60,
  120.     1.67, 1.53, 1.52, 1.51,
  121.     1.61, 1.84, 1.77, 1.61,
  122.     1.37, 1.16, 1.23, 1.31,
  123.     1.27, 1.43, 1.53, 1.50,
  124.     1.51, 1.49, 1.58, 1.61,
  125.     1.63, 1.56, 1.63, 1.69)
  126.  
  127. )
  128.  
  129. ################################################################################
  130. ### Add/combine our data
  131. ################################################################################
  132.  
  133. ### Add data from 1859 - 1960 from the tibble above
  134. data0 <- money_velocity_before_1960
  135.  
  136. ### Fetch data for 1959 - present from FRED
  137. data1 <-
  138.   fredr(series_id = "M2V", frequency = "q") %>%
  139.   select(date, value) %>%
  140.   as_tibble()
  141.  
  142. ### Exclude data from Friedman when we have Fed data to go on
  143. data0 <- data0 %>% filter(date < (min(data1$date)))
  144.  
  145. ### Combine the two data sets
  146. data <- bind_rows(data0, data1) %>% filter(!is.na(value))
  147.  
  148. ################################################################################
  149. ### Graph:  Velocity of M2 Money Stock, 1869 - present
  150. ################################################################################
  151. p_m2_velocity <-
  152.   ggplot(data = data) +
  153.   theme_classic() +
  154.   theme(legend.position = "none") +
  155.   geom_line(aes(x = as.Date(date), y = value),
  156.             color = "black", alpha = 0.8) +
  157.   geom_recession_bars(min(data$date), max(data$date)) +
  158.  
  159.   scale_x_date(breaks = as.Date(c("1870-01-01", "1880-01-01", "1890-01-01",
  160.                                   "1900-01-01", "1910-01-01", "1920-01-01",
  161.                                   "1930-01-01", "1940-01-01", "1950-01-01",
  162.                                   "1960-01-01", "1970-01-01", "1980-01-01",
  163.                                   "1990-01-01", "2000-01-01", "2010-01-01",
  164.                                   "2020-01-01")),
  165.                date_labels = "%Y") +
  166.   labs(title = "Velocity of M2 Money Stock, 1869 - Present", x = "", y = "")
  167. print(p_m2_velocity)
Add Comment
Please, Sign In to add comment