Skip to content

Instantly share code, notes, and snippets.

@msjgriffiths
Created August 7, 2017 17:25
Show Gist options
  • Save msjgriffiths/9fc082e0ad78cf2bfb0b5ff6a1977028 to your computer and use it in GitHub Desktop.
Save msjgriffiths/9fc082e0ad78cf2bfb0b5ff6a1977028 to your computer and use it in GitHub Desktop.
FRED: Layoffs + Leaves
library(tidyverse)
library(alfred)
series <- tribble(
~Series, ~Name,
"CEU3000000001", "All Employees - Manufacturing",
"JTU3000HIR", "Hires",
"JTU3000LDR", "Layoffs",
"JTU3000QUR", "Quits"
)
series %>%
mutate(data = map(Series, ~ get_fred_series(.x, "Value", "2000-12-01"))) %>%
unnest(data) %>%
select(-Series) %>%
spread(Name, Value) ->
data
data %>%
mutate(Leaves = Layoffs + Quits) %>%
pull(Leaves) ->
leaves
h <- stl(ts(na.omit(data$Hires), frequency = 12), "periodic")
l <- stl(ts(na.omit(leaves), frequency = 12), s.window = "periodic")
data_frame(
date = data$date[1:198],
Hires = h$time.series[, 2] + h$time.series[, 3],
Leaves = l$time.series[, 2] + l$time.series[, 3]
) %>%
gather(series, value, -date) %>%
ggplot(aes(date, value, color = series)) +
geom_rect(aes(xmin = as.Date("2007-12-01"), xmax = as.Date("2009-06-01"), ymin = 1, ymax = 5), fill = "#efefef", color = "#efefef", alpha = .4) +
geom_rect(aes(xmin = as.Date("2001-05-01"), xmax = as.Date("2001-11-01"), ymin = 1, ymax = 5), fill = "#efefef", color = "#efefef", alpha = .4) +
geom_line(alpha = .4) +
geom_smooth(span = .5, se = FALSE) +
scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
theme_annalect() +
coord_cartesian(ylim = c(1.4, 4)) +
labs(
x = "Month",
y = "Rate",
title = "Layoffs + Quits vs. Hires",
caption = "Using alfred to query FRED data"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment