Advertisement
binkleym

USA Trend Unemployment Velocity (6 month average)

Jan 7th, 2020
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.33 KB | None | 0 0
  1. ### Create a map showing trend unemployment velocity - Mathew Binkley
  2.  
  3. ### You need a FRED API key in order to pull data from FRED.
  4. ### You may request an API key at:
  5. ### https://research.stlouisfed.org/useraccount/apikeys
  6. api_key_fred   <- "INSERT_YOUR_FRED_API_KEY_HERE"
  7.  
  8. ### We need the following packages for this example.
  9. packages <- c("tidyverse", "lubridate", "fredr", "ggplot2", "maps", "sf",
  10.               "ggthemes", "tsibble", "dplyr", "broom", "ggfortify", "forecast")
  11.  
  12. ### Install packages if needed, then load them quietly
  13. new_packages <- packages[!(packages %in% installed.packages()[, "Package"])]
  14. if (length(new_packages)) install.packages(new_packages, quiet = TRUE)
  15. invisible(lapply(packages, "library",
  16.                  quietly = TRUE,
  17.                  character.only = TRUE,
  18.                  warn.conflicts = FALSE))
  19.  
  20. ### Now set the FRED API key
  21. fredr_set_key(api_key_fred)
  22.  
  23. ### We need several years worth of data in order to filter out
  24. ### the seasonal component.  Ten years is a nice round number...
  25. date_start <- as.Date("2010-01-01")
  26. date_end   <- as.Date(now())
  27.  
  28. ### Create an empty tibble to hold our data
  29. trend_velocity <- tibble(state    = character(),
  30.                          velocity = numeric())
  31.  
  32. for (state in state.abb) {
  33.  
  34.   # Pull data from FRED
  35.   data <- fredr(series_id = paste(state, "URN", sep = ""),
  36.                 observation_start = date_start,
  37.                 observation_end   = date_end,
  38.                 frequency = "m")
  39.  
  40.   date   <- data %>% as_tsibble(index = "date") %>% pull("date")
  41.   values <- data %>% as_tsibble(index = "date") %>% pull("value")
  42.  
  43.   # Decompose the data and pluck out the trend component
  44.   trend <- values %>%
  45.            ts(frequency = 12, start = c(year(min(date)), month(min(date)))) %>%
  46.            mstl() %>%
  47.            trendcycle()
  48.  
  49.   # Take a 6-month average of unemployment velocity.
  50.   velocity <- trend %>% diff() %>% tail(n = 6) %>% mean()
  51.  
  52.   # Append the result to our tibble
  53.   trend_velocity <- trend_velocity %>%
  54.                     add_row(state = state, velocity = velocity)
  55.  
  56. }
  57.  
  58. ### use the "state.name" and "state.abb" databases to convert two-letter state
  59. ### codes to lowercase names (tennessee, california, etc).   Add the resulting
  60. ### lowercase name to the tibble
  61. match <- match(trend_velocity$state, state.abb)
  62. trend_velocity$state_name <- state.name[match] %>% tolower()
  63.  
  64. ### Load a map of states in the USA.   The maps have a list of lowercase
  65. ### state names, which will use to match against "pres" down below
  66. us_map <- maps::map("state", plot = FALSE, fill = TRUE)
  67.  
  68. ### Change the latitude/longitude data to a simple feature object
  69. us_map <- sf::st_as_sf(us_map)
  70.  
  71. ### Change the name of the "ID" column to "state_name"
  72. names(us_map) <- c("geometry", "state_name")
  73.  
  74. ### Remove the District of Colombia from our map
  75. us_map <- us_map %>% filter(state_name != "district of columbia")
  76.  
  77. ### Add our velocity data to the map data
  78. us_map <- us_map %>% left_join(trend_velocity, by = "state_name")
  79.  
  80. ggplot(us_map, aes(fill = velocity <= 0), col = "black") +
  81.    geom_sf(aes(alpha = abs(velocity))) +
  82.    coord_sf(crs = "+proj=aea +lat_1=25 +lat_2=50 +lon_0=-100", ndiscr = 0) +
  83.    scale_fill_manual(values = c("TRUE" = "darkgreen", "FALSE" = "red")) +
  84.    scale_alpha(range = c(0.1, 1)) +
  85.    theme_void() +
  86.    theme(legend.position = "none")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement