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