Last updated: August 11, 2020.

This R notebook highlights the diverse economic datasets integrated into the C3.ai COVID-19 Data Lake. See the API documentation for more details.

Please contribute your questions, answers and insights on Stack Overflow. Tag c3ai-datalake so that others can view and help build on your contributions. For support, please send email to: covid@c3.ai.

if (!require(tidyverse)) install.packages('tidyverse')
if (!require(jsonlite)) install.packages('jsonlite')
if (!require(maps)) install.packages('maps')
if (!require(mapproj)) install.packages('mapproj')

library(tidyverse)
library(jsonlite)
library(maps)
library(mapproj)

options(scipen = 999)

The following code adjusts the theme for plotting and knitting this notebook. Comment it out if you would prefer to use the standard theme or if you encounter errors.

if (!require(hrbrthemes)) install.packages('hrbrthemes')
library(hrbrthemes)
theme_set(theme_ipsum())

A number of helper functions have been created in c3aidatalake.R for easier API calls.

source("c3aidatalake.R")

API Calls

We will investigate how the pandemic has affected US state economies.

states <- list(
  'Alabama_UnitedStates','Alaska_UnitedStates','Arizona_UnitedStates',
  'Arkansas_UnitedStates','California_UnitedStates','Colorado_UnitedStates',
  'Connecticut_UnitedStates','Delaware_UnitedStates','DistrictofColumbia_UnitedStates',
  'Florida_UnitedStates','Georgia_UnitedStates','Hawaii_UnitedStates',
  'Idaho_UnitedStates','Illinois_UnitedStates','Indiana_UnitedStates',
  'Iowa_UnitedStates','Kansas_UnitedStates','Kentucky_UnitedStates',
  'Louisiana_UnitedStates','Maine_UnitedStates','Maryland_UnitedStates',
  'Massachusetts_UnitedStates','Michigan_UnitedStates','Minnesota_UnitedStates',
  'Mississippi_UnitedStates','Missouri_UnitedStates','Montana_UnitedStates',
  'Nebraska_UnitedStates','Nevada_UnitedStates','NewHampshire_UnitedStates',
  'NewJersey_UnitedStates','NewMexico_UnitedStates','NewYork_UnitedStates',
  'NorthCarolina_UnitedStates','NorthDakota_UnitedStates','Ohio_UnitedStates',
  'Oklahoma_UnitedStates','Oregon_UnitedStates','Pennsylvania_UnitedStates',
  'PuertoRico_UnitedStates','RhodeIsland_UnitedStates','SouthCarolina_UnitedStates',
  'SouthDakota_UnitedStates','Tennessee_UnitedStates','Texas_UnitedStates',
  'Utah_UnitedStates','Vermont_UnitedStates','Virginia_UnitedStates',
  'Washington_UnitedStates','WestVirginia_UnitedStates','Wisconsin_UnitedStates',
  'Wyoming_UnitedStates'
)

Business Sectors

The US Bureau of Economic Analysis (BEA) reports detailed data on the makeup of the US economy. Here, we will pull employment by various business sectors along with total employment. This data is reported annually.

end_date <- "2020-07-23"

employment_states <- evalmetrics(
  "outbreaklocation",
  list(
    spec = list(
      ids = states,
      expressions = list(
        "BEA_Employment_AccommodationAndFoodServices_Jobs",
        "BEA_Employment_Accommodation_Jobs",
        "BEA_Employment_AirTransportation_Jobs",
        "BEA_Employment_ArtsEntertainmentAndRecreation_Jobs",
        "BEA_Employment_CreditIntermediationAndRelatedActivities_Jobs",
        "BEA_Employment_DataProcessingHostingAndRelatedServices_Jobs",
        "BEA_Employment_EducationalServices_Jobs",
        "BEA_Employment_FarmEmployment_Jobs",
        "BEA_Employment_FinanceAndInsurance_Jobs",
        "BEA_Employment_FoodServicesAndDrinkingPlaces_Jobs",
        "BEA_Employment_GovernmentAndGovernmentEnterprises_Jobs",
        "BEA_Employment_HealthCareAndSocialAssistance_Jobs",
        "BEA_Employment_Hospitals_Jobs",
        "BEA_Employment_Information_Jobs",
        "BEA_Employment_InsuranceCarriersAndRelatedActivities_Jobs",
        "BEA_Employment_Manufacturing_Jobs",
        "BEA_Employment_OilAndGasExtraction_Jobs",
        "BEA_Employment_RealEstateAndRentalAndLeasing_Jobs",
        "BEA_Employment_RetailTrade_Jobs",
        "BEA_Employment_SocialAssistance_Jobs",
        "BEA_Employment_StateAndLocal_Jobs",
        "BEA_Employment_TotalEmployment_Jobs",
        "BEA_Employment_TransitAndGroundPassengerTransportation_Jobs",
        "BEA_Employment_TransportationAndWarehousing_Jobs",
        "BEA_Employment_TruckTransportation_Jobs",
        "BEA_Employment_WarehousingAndStorage_Jobs"
      ),
      start = "2015-01-01",
      end = end_date,
      interval = "YEAR"
    )
  ),
  get_all = TRUE
) %>% 
  filter(missing == 0) %>%
  select(-missing) %>%
  mutate(state = str_replace(name, '_UnitedStates', ''))

Unemployment Statistics

The US Bureau of Labor Statistics reports measures including the size of the labor force and the unemployment rate monthly. Let’s request unemployment rates at a state level along with confirmed COVID-19 case counts provided by Johns Hopkins University.

unemployment_states <- evalmetrics(
  "outbreaklocation",
  list(
    spec = list(
      ids = states,
      expressions = list(
        "BLS_UnemploymentRate",
        "JHU_ConfirmedCases"
      ),
      start = "2018-01-01",
      end = end_date,
      interval = "MONTH"
    )
  ),
  get_all = TRUE
) %>% 
  filter(missing == 0) %>%
  select(-missing) %>%
  mutate(state = str_replace(name, '_UnitedStates', ''))

Case Counts

We can begin by exploring how COVID-19 case counts have grown in US states.

high_case_states <- unemployment_states %>% 
  filter(dates == '2020-07-01') %>% 
  filter(value_id == 'JHU_ConfirmedCases') %>%
  filter(data > 1e5) %>%
  pull(name)

unemployment_states %>%
  filter(value_id == 'JHU_ConfirmedCases') %>%
  filter(dates >= '2020-02-01') %>%
  filter(name %in% high_case_states) %>%
  ggplot() + geom_line(aes(x = dates, y = data, colour = state)) + 
  labs(
    x='Date', 
    y='Cumulative Case Count', 
    title='Cumulative Case Counts by State',
    colour='State'
  ) + 
  theme(legend.position = "bottom")

Among the states with high case counts, it appears that there are two distinct trends.

outbreak_states <- c(
  "NewYork_UnitedStates",
  "NewJersey_UnitedStates",
  "Massachusetts_UnitedStates",
  "Illinois_UnitedStates",
  "Georgia_UnitedStates",
  "California_UnitedStates",
  "Arizona_UnitedStates",
  "Florida_UnitedStates",
  "Texas_UnitedStates"
)

early_states <- c(
  "NewYork_UnitedStates",
  "NewJersey_UnitedStates",
  "Massachusetts_UnitedStates",
  "Illinois_UnitedStates"
)

unemployment_states %>%
  filter(value_id == 'JHU_ConfirmedCases') %>%
  filter(dates >= '2020-02-01') %>%
  filter(name %in% outbreak_states) %>%
  mutate(early = ifelse(
    name %in% early_states, 
    'Early Outbreak', 
    'Late Outbreak'
  )) %>%
  ggplot() + 
  geom_line(aes(x=dates, y=data, colour=state)) +
  facet_grid(cols=vars(early)) +
  labs(
    x='Date', 
    y='Cumulative Case Count', 
    title='State Case Counts by Stage of Outbreak',
    colour='State'
  ) +
  theme(legend.position="bottom")

States like Massachusetts, New Jersey, and especially New York show a sharp rise in cases early in March before their growth slows considerably. On the other hand, states like Florida, California, and Arizona show a large spike beginning in May.

Unemployment

Outbreak Timing

Since states had severe outbreaks at different times, we can investigate whether there were differential effects on their economies.

unemployment_states %>%
  filter(value_id == 'BLS_UnemploymentRate') %>%
  filter((dates >= '2020-01-01') & (dates < '2020-07-01')) %>%
  filter(name %in% outbreak_states) %>%
  mutate(early = ifelse(name %in% early_states, 'Early Outbreak', 'Late Outbreak')) %>%
  ggplot() + 
  geom_line(aes(x=dates, y=data, colour=state)) +
  geom_point(aes(x=dates, y=data, colour=state)) +
  facet_grid(cols=vars(early)) +
  labs(
    x='Date', 
    y='Unemployment Rate (%)', 
    title='State Unemployment Rates by Stage of Outbreak',
    colour='State'
  ) +
  theme(legend.position="bottom")

The early outbreak states had immediate and long lasting impacts to their labor forces. The late outbreak states saw unemployment begin to spike at the same time, but their unemployment rates had lower peaks and have declined much more quickly in May and June.

unemployment_states %>%
  filter(value_id %in% c('JHU_ConfirmedCases', 'BLS_UnemploymentRate')) %>%
  filter((dates >= '2020-01-01') & (dates < '2020-07-01')) %>%
  filter(name %in% early_states) %>%
  mutate(early = ifelse(name %in% early_states, 'Early Outbreak', 'Late Outbreak')) %>%
  mutate(label = ifelse(value_id == 'JHU_ConfirmedCases', 'Case Count', 'Unemp. Rate (%)')) %>%
  ggplot() + geom_line(aes(x = dates, y = data, colour = state)) +
  facet_grid(rows=vars(label), scales = 'free') +
  labs(
    x='Date',
    y='',
    title='Cases vs Unemployment for Early Outbreak States',
    colour='State'
  ) +
  theme(
    legend.position="bottom",
    strip.text.y = element_text(size = 10)
  )

Zeroing in on the early outbreak states, it appears that differences in the timing and severity of their outbreaks did not lead to differences in the shock to their labor markets.

State Labor Markets

The pandemic and business closures have had a significant effect on unemployment across states, but digging into the data, we can see that not all states were affected equally. Some states like Nevada saw over 1 in 4 workers categorized as unemployed, while other states have seen much more modest economic shocks for now.

us_states <- map_data("state") %>% 
  mutate(state_name = str_replace_all(region, " ", ""))

map_date <- '2020-04-01'

us_states %>%
  left_join(
    unemployment_states %>%
      mutate(state_name = str_to_lower(str_replace(name, "_UnitedStates", ""))) %>%
      filter(dates == map_date) %>%
      filter(value_id == 'BLS_UnemploymentRate') %>%
      filter(!is.na(data)),
    by = "state_name"
  ) %>% 
  ggplot(aes(x=long, y=lat, group=group, fill=data)) + 
  geom_polygon(color="black", size=0.3) +
  coord_map(projection="albers", lat0=39, lat1=45) +
  labs(
    title=str_interp('Unemployment Rate by State on ${map_date}'),
    fill='Unemp. Rate (%)'
  ) + 
  theme(
    legend.position = "bottom",
    strip.background = element_blank()
  ) +
  scale_fill_viridis_c(
    option = "plasma",
    values = seq(0, 30, 1) %>% scales::rescale(),
    breaks = seq(0, 30, 5),
    limits = c(0, 30)
  ) +
  guides(
    fill = guide_colorbar(
      title.position = "top",
      title.hjust = 0.5,
      barwidth = 5,
      barheight = 1.5,
      nbin = 6,
      raster = FALSE,
      ticks = FALSE,
      direction = "horizontal"
    )
  )

us_states %>%
  select(long, lat, group, state_name) %>%
  left_join(
    unemployment_states %>%
      mutate(state_name = str_to_lower(str_replace(name, "_UnitedStates", ""))) %>%
      filter((dates >= '2020-01-01') & (dates < '2020-07-01')) %>%
      filter(value_id == 'BLS_UnemploymentRate') %>%
      filter(!is.na(data)),
    by = "state_name"
  ) %>%
  ggplot(aes(x = long, y = lat, group = group, fill=data)) + 
  geom_polygon(color = "black", size = 0.3) +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
  facet_wrap(. ~ dates, ncol = 2, scales = "fixed", drop=T) + 
  labs(
    title='US Unemployment Rates, Jan - June 2020',
    fill='Unemp. Rate (%)'
  ) +
  theme(
    legend.position = "bottom",
    strip.background = element_blank()
  ) +
  scale_fill_viridis_c(
    option = "plasma",
    values = seq(0, 30, 1) %>% scales::rescale(),
    breaks = seq(0, 30, 5),
    limits = c(0, 30)
  ) +
  guides(
    fill = guide_colorbar(
      title.position = "top",
      title.hjust = 0.5,
      barwidth = 5,
      barheight = 1.5,
      nbin = 6,
      raster = FALSE,
      ticks = FALSE,
      direction = "horizontal"
    )
  )

While unemployment rates have declined from their peaks in many states, the US is still far from pre-pandemic economic conditions.

State Industries

The differences in labor market impact could in part be a reflection of industry specializations in each state. Lockdowns and safety concerns have altered how consumers spend their time and money, and some states are less prepared for a distanced economy than others.

unemp_by_state <- unemployment_states %>% 
  filter((value_id == 'BLS_UnemploymentRate') & (dates == '2020-06-01')) %>%
  pivot_wider(names_from=value_id, values_from=data) %>%
  select(-dates)

employment_compare <- employment_states %>%
  filter(dates == '2018-01-01') %>%
  filter(value_id != "BEA_Employment_TotalEmployment_Jobs") %>%
  inner_join(
    employment_states %>%
      filter(dates == '2018-01-01') %>%
      filter(value_id == "BEA_Employment_TotalEmployment_Jobs") %>%
      mutate(totalEmployment = data) %>%
      select(name, totalEmployment),
    by='name'
  ) %>%
  mutate(percentEmployment = data * 100.0 / totalEmployment) %>%
  mutate(sector = str_replace_all(value_id, c('BEA_Employment_|_Jobs'), ''))

grid_data <- employment_compare %>%
  filter(name %in% c(
    'Nevada_UnitedStates', 
    'Michigan_UnitedStates', 
    'Washington_UnitedStates'
  )) %>% 
  group_by(state) %>%
  top_n(5, percentEmployment) %>% 
  ungroup() %>%
  arrange(state, desc(percentEmployment)) %>%
  mutate(order = row_number()) 

grid_data %>%
  ggplot(aes(x=order, y=percentEmployment, fill=sector)) +
  geom_bar(stat='identity') +
  facet_grid(cols=vars(state), scales="free") +
  scale_x_continuous(
    breaks = grid_data$order,
    labels = grid_data$sector,
    expand = c(0,0)
  ) +
  labs(
    x="Business Sector", 
    y='% of Total Employment', 
    title="2018 Biggest Employment Sectors"
  ) +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1, size=9),
    legend.position = "none"
  )

Nevada’s extreme unemployment rate makes sense in a world where travelers are scarce and casinos are closed. Michigan has an outsized exposure to manufacturing industries while Washington has large numbers of office workers.

On the other hand, there are some industries that every state nurtures.

employment_compare %>%
  inner_join(unemp_by_state, by='state') %>%
  filter(value_id == 'BEA_Employment_FoodServicesAndDrinkingPlaces_Jobs') %>%
  ggplot(aes(x = reorder(state, percentEmployment))) + 
  geom_bar(
    aes(y=percentEmployment), 
    stat='identity', 
    alpha=.8, 
    width = 0.7, 
    fill='navy'
  ) +
  labs(
    x='State',
    y='% of Total Employment',
    title='Food Service Work as % of State Employment'
  ) +
  theme(axis.text.x = element_text(angle = 55, hjust = 1, size=10)) 

All states have food service workers, and while travel destinations like Hawaii or Nevada might have somewhat more, the impact of closing restaurants is felt throughout the US economy.

employment_compare %>%
  filter(sector %in% c(
    'Manufacturing',
    'FarmEmployment',
    "OilAndGasExtraction",
    "TransitAndGroundPassengerTransportation"
  )) %>%
  mutate(shortName = case_when(
    sector == 'FarmEmployment' ~ "Farms",
    sector == 'OilAndGasExtraction' ~ "Oil&Gas",
    sector == 'TransitAndGroundPassengerTransportation' ~ "Ground Transit",
    TRUE ~ sector
  )) %>%
  ggplot() + 
  geom_bar(aes(x = state, y=percentEmployment, fill=sector), stat='identity') +
  facet_grid(rows=vars(shortName), scale='free_y') +
  labs(
    x='State',
    y='% of Total Employment',
    title='Percent of State Employment by Industry'
  ) +
  theme(
    legend.position = "none",
    axis.text.x = element_text(angle = 45, hjust = 1, size = 9),
    axis.text.y = element_text(hjust = 1, size = 9),
    strip.text.y = element_text(size = 10, colour = "black")
  ) 

States have specialized in certain industries and tend to have distinct profiles when compared to one another. We can see farming states tend to differ from those that have more manufacturing, oil, or transit workers.