Skip to content

Instantly share code, notes, and snippets.

@jkr216
Created December 10, 2021 00:02
Show Gist options
  • Save jkr216/05ff495007fe30fbeabdb0edf61bfaa5 to your computer and use it in GitHub Desktop.
Save jkr216/05ff495007fe30fbeabdb0edf61bfaa5 to your computer and use it in GitHub Desktop.
bev curve
```{r}
library(tidyverse)
library(tidyquant)
library(timetk)
library(readxl)
library(plotly)
library(scales)
library(formattable)
```
Let's get job openings rate and unemployment rate data from FRED, and combine into one data frame.
```{r}
job_op_unrate <-
"JTSJOR" %>%
get_series_from_fred(start_date = "2000-01-01") %>%
select(title, date, value = Rate) %>%
mutate(title = str_replace(title, ": Total Nonfarm", " Rate") ) %>%
bind_rows(
"UNRATE" %>%
get_series_from_fred(start_date = "2000-01-01") %>%
select(title, date, value = Percent)
) %>%
pivot_wider(
names_from = title,
values_from = value
) %>%
arrange(date) %>%
drop_na()
job_op_unrate %>%
tail()
```
Next we take `USREC` data from FRED and turn into data ranges. `USREC` is a timeseries from NBER that flags each recessionary month with a 1, else with a 0.
```{r}
recession_range <-
"USREC" %>%
tq_get(get = "economic.data", from = "2000-12-01") %>%
select(date, recession_flag = price) %>%
filter(date <= max(job_op_unrate$date)) %>%
mutate(recession_label =
case_when(recession_flag == 1 & lag(recession_flag == 0) ~ str_glue("{year(date)} recession"),
TRUE ~ NA_character_)) %>%
filter(recession_flag == 1) %>%
fill(recession_label, .direction = "down") %>%
bind_rows(
"USREC" %>%
tq_get(get = "economic.data", from = "2000-12-01") %>%
select(date, recession_flag = price) %>%
filter(date <= max(job_op_unrate$date)) %>%
mutate(recession_label =
case_when(recession_flag == 1 & lag(recession_flag == 0) ~ str_glue("{year(date)} recession"),
TRUE ~ NA_character_)) %>%
filter(recession_flag == 0)
) %>%
arrange(date) %>%
mutate(recession_label =
case_when(recession_flag == 0 & lead(recession_flag == 1) ~ str_glue("{year(date)} non recession"),
TRUE ~ recession_label)) %>%
fill(recession_label, .direction = "up") %>%
group_by(recession_label) %>%
mutate(recession_range =
case_when(recession_flag == 1 ~ str_glue("{month(min(date), label = T, abbr = T)} {year(min(date))} to {month(max(date), label = T, abbr = T)} {year(max(date))}*"),
TRUE ~ str_glue("{month(min(date), label = T, abbr = T)} {year(min(date))} to {month(max(date), label = T, abbr = T)} {year(max(date))}"))
)%>%
ungroup() %>%
select(date, recession_range)
recession_range
```
Have a look at the recession range object we just created and imagine how we could put it to use. It allows us to sort time series into different date ranges, bounded by the beginnings and ends to recessionary periods.
Let's `left_join()` that data to our Beveridge Curve object `job_op_unrate` and take a peek at the results.
```{r}
job_op_unrate %>%
left_join(recession_range) %>%
drop_na() %>%
mutate(recession_range = as_factor(recession_range)) %>%
group_by(recession_range) %>%
slice(1, n())
```
Notice how we have our `date` column if we wish to reference a specific month, and also a `recession_range` column, a factor label for the date range of each observation. Now we can plot the relationship between the `Job Openings Rate` column and the `Unemploymnent Rate` column, and highlight how that relationship, or curve, has changed with different date ranges.
```{r}
job_op_unrate %>%
left_join(recession_range) %>%
drop_na() %>%
mutate(recession_range = as_factor(recession_range)) %>%
ggplot(aes(y = `Job Openings Rate`, x = `Unemployment Rate`, color = recession_range)) +
geom_line() +
geom_point() +
theme_minimal() +
ggrepel::geom_text_repel(aes(label = str_glue("{month(date, label = T, abbr = F)}
{year(date)}")),
data = . %>% ungroup() %>% filter(date == max(date)),
nudge_x = -.1,
nudge_y = -.6,
show.legend = F,
color = "black") +
scale_y_continuous(labels = function(x) str_glue("{x}%")) +
scale_x_continuous(labels = function(x) str_glue("{x}%"),
breaks = pretty_breaks(10)) +
labs(color = "", caption = "* is recssionary period", title = "Beveridge Curve: Job Openings Rate v. Unemployment Rate")
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment