---
title: "frontline workers Jenna"
author: "Jenna"
date: "April 22, 2020"
output: rmarkdown::github_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
 

library(tidyverse)
library(kableExtra)
library(ipumsr)
#library(survey) # Some easy weighting functions.
library(srvyr) # Like survey but with syntax like dplyr.
library(labelled) # for working with labelled data
library(tidycensus)
library(ggrepel)
library(readxl)
library(here)

library(scales)

source(here("03_graphics themes.R"))

```

## Objective 

Use IPUMS to recreate similar analyses to the following about the workers vulnerable to COVID-19 exposure. 

We're basing this on a few examples:

- [CEPR](https://cepr.net/a-basic-demographic-profile-of-workers-in-frontline-industries/) -- This also has a [Github repo](https://github.com/ceprdata/frontline-workers). 
- [Boston Indicators](https://www.bostonindicators.org/article-pages/2020/april/frontline_workers)
- [NYC Comptroller](https://comptroller.nyc.gov/reports/new-york-citys-frontline-workers/)
- [ACLU](https://www.aclum.org/en/publications/data-show-covid-19-hitting-essential-workers-and-people-color-hardest)



```{r, include=FALSE}
## IPUMS setup

# Load the raw IPUMS data. 


# Note that you can pass in the loaded DDI into the `read_ipums_micro()`
# NOTE: To load data, you must download both the extract's data and the DDI
# and also set the working directory to the folder with these files (or change the path below).

if (!require("ipumsr")) stop("Reading IPUMS data into R requires the ipumsr package. It can be installed using the following command: install.packages('ipumsr')")

ddi <- read_ipums_ddi("inputs/ipums/usa_00007.xml")
ipums.la <- read_ipums_micro(ddi)
```



```{r, include=FALSE}
# Clean some of the variables. 
# Note this list of variables converted to factors may be incomplete.

# to_factor pulls the labels out and makes them factors.
# This is an alternative to the hybrid int+lbl default. 
ipums.la.fac <- ipums.la %>% 
  mutate_at(vars(NUMPREC:SUBSAMP,
                 HHTYPE,
                 STATEFIP:PUMA,
                 GQ:MORTGAGE,
                 TAXINCL:INSINCL,
                 FOODSTMP,
                 SSMC:MULTGEND,
                 FAMUNIT:SCHOOL,
                 CLASSWKR,
                 EDUC:LABFORCE,
                 WKSWORK2:WORKEDYR,
                 MIGRATE1:MIGPUMA1,
                 DIFFREM:GCRESPON), 
            to_factor) %>% 
  mutate(OCC2010.fac = to_factor(OCC2010), # to have factor and numeric versions of occ and ind codes
         OCCSOC.fac = to_factor(OCCSOC),
         IND.fac = to_factor(IND)) #%>% 
  #mutate_at(vars(INCTOT:INCWAGE), to_character) # this takes the labels for the income variables but makes them character

# clear the original object to save memory.
rm(ipums.la)

```

```{r, include=FALSE}
# Fix income variables using CPI adjustment (available [here](https://cps.ipums.org/cps/cpi99.shtml)). Convert dollar values to numeric and exclude certain codes (Note that it's strange that INCWAGE doesn't have special codes). 
# Use the included CPI99, then bring to 2018
cpi99to2018 <- 1.507

ipums.la.fac <- ipums.la.fac %>% 
  mutate(INCWAGE.cpi = ifelse(INCWAGE %in% c(999999 ,999998), NA, CPI99*as.numeric(INCWAGE)*cpi99to2018)) %>% 
  mutate(INCWAGE.cpi = CPI99*as.numeric(INCWAGE)*cpi99to2018) %>% 
  mutate(INCTOT.cpi = ifelse(INCTOT %in% c(9999999), NA, CPI99*as.numeric(INCTOT)*cpi99to2018)) %>% 
  mutate(FTOTINC.cpi = ifelse(FTOTINC %in% c(9999999), NA, CPI99*as.numeric(FTOTINC)*cpi99to2018)) %>% 
  mutate(HHINCOME.cpi = ifelse(HHINCOME %in% c(9999999), NA, CPI99*as.numeric(HHINCOME)*cpi99to2018)) 
```


```{r, include=FALSE}

# For convenience, define PUMA codes for querying by geography. Maps with the exact definitions for 2010 PUMAs are on the [IPUMS website](https://usa.ipums.org/usa/volii/pumas10.shtml).
# 
## Create filters for PUMA codes -- these probably changed in 1990 and 2000.
# ipums provides the best documentation. The fed websites are becoming unusable. 

super.region.pumacodes.2010 <- c(
  "1400",	# Capital Region Planning Commission 1--Baton Rouge Metropolitan Area (West) PUMA
  "1500",	# Capital Region Planning Commission 2--East Baton Rouge Parish (South) PUMA
  "1501",	# Capital Region Planning Commission 3--Baton Rouge City PUMA
  "1502",	# Capital Region Planning Commission 4--East Baton Rouge Parish (North) PUMA
  "1600",	# Capital Region Planning Commission 5--Ascension Parish PUMA
  "1700",	# Capital Region Planning Commission 6--Livingston & St. Helena Parishes PUMA
  "1800",	# Capital Region Planning Commission 7--Tangipahoa & Washington Parishes PUMA
  "1900",	# South Central Planning & Development Commission 1--River Parishes PUMA
  "2000",	# South Central Planning & Development Commission 2--Lafourche & Assumption Parishes PUMA
  "2100",	# South Central Planning & Development Commission 3--Terrebonne Parish PUMA
  "2200",	# Regional Planning Commission 1--St. Tammany Parish (Northwest) PUMA
  "2201",	# Regional Planning Commission 2--St. Tammany Parish (Southeast) PUMA
  "2300",	# Regional Planning Commission 3--Jefferson Parish (North) PUMA
  "2301",	# Regional Planning Commission 4--Jefferson Parish (Central) PUMA
  "2302",	# Regional Planning Commission 5--Jefferson Parish (West Bank) PUMA
  "2400",	# Regional Planning Commission 6--New Orleans City (Northeast) PUMA
  "2401",	# Regional Planning Commission 7--New Orleans City (Central) PUMA
  "2402",	# Regional Planning Commission 8--New Orleans City (South) PUMA
  "2500"	# Regional Planning Commission 9--St. Bernard, Jefferson (South) & Plaquemines Parishes PUMA
)
  
orleans.pumacodes <- c("2400","2401","2402")

# This should be double checked.
NOmsa.pumacodes <- c("2400","2401","2402", # Orleans
                     "2500", # St Bernard, Plaquemines, south Jefferson
                     "2300","2301","2302", # Jefferson
                     "2200","2201", # St Tammany
                     "1900") # river parishes (Includes St. James, I think)

# Define asian categories
asian.ipums <- c("Chinese",
                 "Japanese",
                 "Other Asian or Pacific Islander")

```




```{r}
# Define industry sector based on ranges of variables. For industry and industry sector labels, you could use the structure of the original census document without too much trouble. You can either use a modified custom groupings like the [six categories used by CEPR](https://cepr.net/a-basic-demographic-profile-of-workers-in-frontline-industries/) or use the [ACS codes for NAICS-equivalent sectors](https://www.census.gov/topics/employment/industry-occupation/guidance/code-lists.html).
url <- "https://www2.census.gov/programs-surveys/demo/guidance/industry-occupation/2017-industry-code-list-with-crosswalk.xlsx"
destfile <- "X2017_industry_code_list_with_crosswalk.xlsx"
curl::curl_download(url, destfile)
ACS_industry_codes <- read_excel(destfile, 
    skip = 5)

names(ACS_industry_codes) <- c("sector",
                                                  "industry",
                                                  "DELETE",
                                                  "ACScode2017",
                                                  "NAICScode2017")

ACS_industry_codes <- ACS_industry_codes %>% select(-DELETE) 

# THIS DOESN'T WORK
#ACS_industry_codes.range <- 
#   ACS_industry_codes %>% 
#   separate(ACScode2017, c("begin","end"), "-") %>% 
#   mutate(end = ifelse(industry == "Management of companies and enterprises
# ", "7570", end)) %>% 
#   mutate(end = ifelse(sector == "Construction", "0770", end)) %>% 
#   mutate(industry = ifelse(begin=="4670" & end=="5790", "Retail Trade", industry),
#          industry = ifelse(begin=="4070" & end=="4590", "Wholesale Trade", industry),
#          industry = ifelse(begin=="1070" & end=="3990", "Manufacturing", industry)) %>% 
#   filter(!is.na(end)) %>% print(n=100)


```



```{r}
#Or just define them manually. Use this to join to the IPUMS data set. 

ACS_industry_codes.range <- tibble(ACScode.num = 1:9590, industry = NA) %>% 
  mutate(industry = ifelse(ACScode.num %in% 170:290, "Agriculture, Forestry, Fishing, and Hunting", industry),
         industry = ifelse(ACScode.num %in% 370:490, "Mining, Quarrying, and Oil and Gas Extraction", industry),
         industry = ifelse(ACScode.num %in% 770, "Construction", industry),
         industry = ifelse(ACScode.num %in% 1070:3990, "Manufacturing", industry),
         industry = ifelse(ACScode.num %in% 4070:4590, "Wholesale Trade", industry),
         industry = ifelse(ACScode.num %in% 4670:5790, "Retail Trade", industry),
         industry = ifelse(ACScode.num %in% 6070:6390, "Transportation and Warehousing", industry),
         industry = ifelse(ACScode.num %in% 570:690, "Utilities", industry),
         industry = ifelse(ACScode.num %in% 6470:6780, "Information", industry),
         industry = ifelse(ACScode.num %in% 6870:6992, "Finance and Insurance", industry),
         industry = ifelse(ACScode.num %in% 7071:7190, "Real Estate and Rental and Leasing", industry),
         industry = ifelse(ACScode.num %in% 7270:7490, "Professional, Scientific, and Technical Services", industry),
         industry = ifelse(ACScode.num %in% 7570, "Management of companies and enterprises", industry),
         industry = ifelse(ACScode.num %in% 7580:7790, "Administrative and support and waste management services", industry),
         industry = ifelse(ACScode.num %in% 7860:7890, "Educational Services", industry),
         industry = ifelse(ACScode.num %in% 7970:8470, "Health Care and Social Assistance", industry),
         industry = ifelse(ACScode.num %in% 8561:8590, "Arts, Entertainment, and Recreation", industry),
         industry = ifelse(ACScode.num %in% 8660:8690, "Accommodation and Food Services", industry),
         industry = ifelse(ACScode.num %in% 8770:9290, "Other Services, Except Public Administration", industry),
         industry = ifelse(ACScode.num %in% 9370:9590, "Public Administration", industry),
         industry = ifelse(ACScode.num %in% 9670:9870, "Military", industry)) %>% 
  mutate(ACScode = as.character(ACScode.num))
  #mutate(ACScode = str_pad(ACScode.num, side = "left", pad = "0", width = 4)) 

```


```{r}
## Identifying industries

# As we assemble it, I'm storing a version of this on my personal drive, then copying into a CSV in the inputs folder. Currently, this is just a test version using the BI/CEPR occupations. 

vulnerableEssentialWorkers <- read_csv("inputs/acsIndOcc/vulnerableEssentialWorkers_v3.csv", 
    col_types = cols(ACScode = col_character())) %>% 
  filter(Include %in% c("A","B"))

# vulnerableEssentialWorkers %>% count(Include)
# vulnerableEssentialWorkers %>% count(category2)
# vulnerableEssentialWorkers %>% count(category5)
# vulnerableEssentialWorkers %>% count(categoryCEPR)

vulnerableEssentialWorkers %>% 
  select(ACScode, Description, category5) %>%
  write.csv("outputs/graphicsEssentialWorkers/COVIDessentialACSIndustries.csv")
```

#### Number and percent of essential workers   

```{r}
ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = essential, value = n) %>%
  mutate(pct_essential = Essential / (No + Essential)) %>%
  kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

#### Number and percent of workers in 2 essential categories  

```{r}
ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category2 = ifelse(is.na(category2), "Other industries", category2)) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(category2) %>% 
  survey_count(category2, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = category2, value = n) %>%
  mutate(pct_essential = `Essential production, trade, and infrastructure` / (`Essential production, trade, and infrastructure` + `Frontline health, retail, and services` + `Other industries`),
         pct_frontline = `Frontline health, retail, and services` / (`Essential production, trade, and infrastructure` + `Frontline health, retail, and services`+ `Other industries`)) %>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

#### Percent of workers in 5 categories
```{r}
ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(category5) %>% 
  survey_count(category5, vartype = "se") %>% 
  select(-n_se) %>%
  ungroup() %>%
  mutate(pct_cat5 = n/sum(n)) %>%
  filter(category5 != "Other industries") %>%
  #   kable(format = "markdown", digits = 3) %>%
  # kable_styling(bootstrap_options = "striped", full_width = F)
  ggplot(aes(category5,pct_cat5)) +
  geom_bar(stat = "identity", fill = "lightblue") +
  coord_flip() +
  scale_y_continuous(labels = percent_format(accuracy = 1), limits = c(0,.15))+
  geom_text(aes(label = percent(pct_cat5, accuracy = .1)), 
            # hjust = 1,
            hjust = -.3, 
            position = position_dodge(width = .85),
            size=2.75, 
            family="Asap") +
  themeDC_vertical() +
  theme(legend.position = "none",
        axis.text.x = element_text(size = 8, vjust = -.1, family="Asap"),
        plot.title = element_text(size=12),
        plot.subtitle = element_text(size=10))+
  labs(title = "Five essential industry groups by share of the total workforce",
       subtitle = "New Orleans metro, 2014 - 2018",
       x = "",
       y="") 
ggsave(filename = "outputs/graphicsEssentialWorkers/essentialEmpShare_bars.png", type = "cairo",
       width = 8, height = 2.5, units = "in")

```  

#### Race and sex breakdown 

``` {r}
all.s.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed",
         CLASSWKR == "Works for wages") %>%
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White workers", "Workers of color")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(SEX, race.WOC, vartype = "se") %>% 
  select(-n_se)  %>%
  mutate(w.type = "All workers")%>%
  mutate(pct = n/sum(n))


ess.s.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed",
         CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White workers", "Workers of color"),
         essential = ifelse(Include %in% c("A","B"), "All essential workers", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, SEX, race.WOC, vartype = "se") %>% 
  select(-n_se)  %>%
  rename(w.type = essential) %>%
  filter(w.type != "No")%>%
  ungroup(essential, SEX, race.WOC) %>%
  mutate(pct = n/sum(n))

cat5.s.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed",
         CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White workers", "Workers of color"),
         essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, SEX, race.WOC, vartype = "se") %>% 
  select(-n_se)  %>%
  rename(w.type = category5) %>%
  filter(w.type != "Other industries")%>%
  group_by(w.type) %>%
  mutate(pct = n/sum(n))

# bind_rows(all.s.r,ess.s.r,cat5.s.r)%>%
#     kable(format = "markdown", digits = 3) %>%
#   kable_styling(bootstrap_options = "striped", full_width = F)

bind_rows(all.s.r,ess.s.r,cat5.s.r)%>%
  mutate(w.type.fac = factor(.$w.type, levels = c("All workers", "All essential workers","Essential retail", "Essential systems & services", "Health care & social assistance", "Production", "Transportation and trade")),
         desc = paste(race.WOC, SEX, sep = ",\n")) %>%  #desc.fac = factor(.$desc, levels = c("WOC, Female", "White, Female", "WOC, Male",  "White, Male"))
ggplot(aes(w.type.fac, pct, fill=desc, label = percent(pct, accuracy = 1))) +
  geom_bar(stat="identity", position="fill", color = "gray50") +
  coord_flip() +
  scale_fill_manual(values = c(DCcolor.p2blue80,
                               DCcolor.p2teal70,
                               DCcolor.p2violet90,
                               DCcolor.p2purple80),
                    guide = guide_legend(reverse = TRUE)) +
  geom_text(size = 3, position = position_stack(vjust = 0.5), family="Asap", color = "white") + 
  scale_y_continuous(labels = percent_format(accuracy = 1), expand = expand_scale(mult = c(0,.1)), limits = c(0,1.01)) + 
  themeDC_scatter() +
  theme(#legend.position = "right",
    
        legend.title = element_blank(),
        legend.text = element_text( size = 8),
        legend.key.size = unit(.3,"cm"),
        legend.margin = margin(1,1,1,1),
        plot.title = element_text(size = 10),
        plot.subtitle = element_text( size=8),
        axis.text.x = element_text(size = 8,  vjust = -.1, hjust = .1, family="Asap"),
        legend.position = "top",
        legend.justification = "left",
        legend.direction = "horizontal",
        legend.spacing.x = unit(.15, 'cm')) +
  labs(title = "Demographics of essential workers by essential industry group",
       subtitle = "New Orleans metro, 2014 - 2018",
       x="",
       y="")
ggsave(filename = "outputs/graphicsEssentialWorkers/essentialDemographics_stackedBars.png", type = "cairo",
       width = 8, height = 4, units = "in")

```   

#### Percent Black in essential industries, percent Black & percent Hispanic in categories of industries.   

```{r}
ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed",
         CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(black = ifelse(RACE =="Black/African American/Negro" & HISPAN == "Not Hispanic", "Black", "not Black"),
         essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, black, vartype = "se") %>% 
  select(-n_se)  %>%
  rename(w.type = essential) %>%
  filter(w.type == "Essential")%>%
  mutate(pct = n/sum(n))

ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed",
         CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(black = ifelse(RACE =="Black/African American/Negro" & HISPAN == "Not Hispanic", "Black", "not Black"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, black, vartype = "se") %>% 
  select(-n_se)  %>%
  rename(w.type = category5) %>%
  group_by(w.type) %>%
  mutate(pct = n/sum(n)) %>%
  filter(black == "Black")

ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed",
         CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(hispanic = ifelse(HISPAN == "Not Hispanic", "not Hispanic", "Hispanic"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, hispanic, vartype = "se") %>% 
  select(-n_se)  %>%
  rename(w.type = category5) %>%
  group_by(w.type) %>%
  mutate(pct = n/sum(n)) %>%
  filter(hispanic == "Hispanic")
```

#### Incomes  

```{r}
    
all.fam.inc <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", 
         CLASSWKR == "Works for wages") %>% 
  filter(FTOTINC != 9999999) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  summarise(median.inc = survey_median(FTOTINC.cpi, vartype = "se")) %>%
  mutate(w.type = "All workers",
         inc.type = "Family income")

all.ind.inc <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", 
         CLASSWKR == "Works for wages") %>% 
  filter(FTOTINC != 9999999) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  summarise(median.inc = survey_median(INCWAGE.cpi, vartype = "se")) %>%
  mutate(w.type = "All workers",
         inc.type = "Individual wages and salaries")

ess.fam.inc <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", 
         CLASSWKR == "Works for wages") %>% 
  filter(FTOTINC != 9999999) %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "All essential workers", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(essential) %>% 
  summarise(median.inc = survey_median(FTOTINC.cpi, vartype = "se")) %>%
  filter(essential != "No") %>%
  rename(w.type = essential) %>%
  mutate(inc.type = "Family income")

ess.ind.inc <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", 
         CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "All essential workers", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(essential) %>% 
  summarise(median.inc = survey_median(INCWAGE.cpi, vartype = "se")) %>%
  filter(essential != "No") %>%
  rename(w.type = essential)%>%
  mutate(inc.type = "Individual wages and salaries")

cat5.fam.inc <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", 
         CLASSWKR == "Works for wages") %>% 
  filter(FTOTINC != 9999999) %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(category5) %>% 
  summarise(median.inc = survey_median(FTOTINC.cpi, vartype = "se")) %>%
  rename(w.type = category5) %>%
  filter(w.type != "Other industries") %>%
  mutate(inc.type = "Family income")

cat5.ind.inc <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", 
         CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(category5) %>% 
  summarise(median.inc = survey_median(INCWAGE.cpi, vartype = "se")) %>%
  rename(w.type = category5)%>%
  filter(w.type != "Other industries") %>%
  mutate(inc.type = "Individual wages and salaries")

# bind_rows(all.fam.inc,all.ind.inc,ess.fam.inc,ess.ind.inc,cat5.fam.inc,cat5.ind.inc)%>%
#   arrange(w.type) %>%
#     kable(format = "markdown", digits = 3) %>%
#   kable_styling(bootstrap_options = "striped", full_width = F)

# With family income
# bind_rows(all.fam.inc,all.ind.inc,ess.fam.inc,ess.ind.inc,cat5.fam.inc,cat5.ind.inc)%>%
#   mutate(w.type.fac = factor(.$w.type, levels = c("All workers", "All essential workers","Essential retail", "Essential systems & services", "Health care & social assistance", "Production", "Transportation and trade"))) %>%
#     ggplot(aes(w.type.fac,median.inc, fill = inc.type)) +
#   geom_bar(stat = "identity", position = position_dodge(), 
#            width = .7,
#              color='gray50') +
#   scale_fill_manual(values = c(DCcolor.p2blue, DCcolor.p1skyblue),
#                     guide = guide_legend(reverse = TRUE)) + #"#F5D000",
#   
#   coord_flip() +
#   scale_y_continuous(labels = dollar_format(accuracy = 1),expand = expand_scale(mult = c(0,.1)))+
#   geom_text(aes(label = dollar(median.inc, accuracy = 1)), 
#             # hjust = 1,
#             hjust = -.2, 
#             position = position_dodge(width = .85),
#             size=2.75, 
#             family="Asap") +
#   themeDC_vertical() +
#   theme(legend.position = "top",
#         legend.justification = "left",
#         legend.direction = "vertical",
#         legend.title = element_blank(),
#         axis.text.x = element_text(size = 8, vjust = -.1, family="Asap"),
#         plot.title = element_text(size=12),
#         plot.subtitle = element_text(size=10))+
#   # guides(fill = guide_legend(nrow = 2)) +
#   labs(title = "Earnings and family income by essential industry group",
#        subtitle = "New Orleans metro, 2014 - 2018",
#        x = "",
#        y="") 
# 
# ggsave(filename = "outputs/graphicsEssentialWorkers/essentialEarnIncome_bars.png", type = "cairo",
#        width = 8, height = 4, units = "in")

# Without family income
bind_rows(all.ind.inc,ess.ind.inc,cat5.ind.inc)%>%
  mutate(w.type.fac = factor(.$w.type, levels = c("All workers", "All essential workers","Essential retail", "Essential systems & services", "Health care & social assistance", "Production", "Transportation and trade"))) %>%
    ggplot(aes(w.type.fac,median.inc, fill = inc.type)) +
  geom_bar(stat = "identity") +
  scale_fill_manual(values = c(DCcolor.p2blue, DCcolor.p1skyblue),
                    guide = guide_legend(reverse = TRUE)) + #"#F5D000",
  
  coord_flip() +
  scale_y_continuous(labels = dollar_format(accuracy = 1),expand = expand_scale(mult = c(0,.1)))+
  geom_text(aes(label = dollar(median.inc, accuracy = 1)), 
            # hjust = 1,
            hjust = -.2, 
            position = position_dodge(width = .85),
            size=2.75, 
            family="Asap") +
  themeDC_vertical() +
  theme(legend.position = "none",
        legend.justification = "left",
        legend.direction = "vertical",
        legend.title = element_blank(),
        axis.text.x = element_text(size = 8, vjust = -.1, family="Asap"),
        plot.title = element_text(size=12),
        plot.subtitle = element_text(size=10))+
  # guides(fill = guide_legend(nrow = 2)) +
  labs(title = "Median earnings by essential industry group",
       subtitle = "New Orleans metro, 2014 - 2018",
       x = "",
       y="") 

ggsave(filename = "outputs/graphicsEssentialWorkers/essentialEarnIncome_bars.png", type = "cairo",
       width = 8, height = 3.5, units = "in")



```   

#### Median age.   

```{r}
all.age <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed",CLASSWKR == "Works for wages") %>% # employed workers only,
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  summarise(median.age = survey_median(as.numeric(as.character(AGE)), vartype = "se")) %>%
  mutate(w.type = "All")

ess.age <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed",CLASSWKR == "Works for wages") %>% # employed workers only,
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(essential) %>%
  summarise(median.age = survey_median(as.numeric(as.character(AGE)), vartype = "se")) %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

bind_rows(all.age,ess.age)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

#### Percent over 50.   

```{r}

all.over50 <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed",
         CLASSWKR == "Works for wages") %>%
  mutate(age = ifelse(as.numeric(as.character(AGE)) >= 50, "50+", "under 50")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(age, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = age, value = n) %>%
  mutate(pct_over50 = `50+`/(`50+` + `under 50`)) %>%
  mutate(w.type = "All workers")

ess.over50 <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed",
         CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>%
  mutate(age = ifelse(as.numeric(as.character(AGE)) >= 50, "50+", "under 50"), 
         essential = ifelse(Include %in% c("A","B"), "All essential workers", "No")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, age, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = age, value = n) %>%
  mutate(pct_over50 = `50+`/(`50+` + `under 50`)) %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

cat5.over50 <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed",
         CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>%
  mutate(age = ifelse(as.numeric(as.character(AGE)) >= 50, "50+", "under 50"), 
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, age, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = age, value = n) %>%
  mutate(pct_over50 = `50+`/(`50+` + `under 50`)) %>%
  rename(w.type = category5)%>%
  filter(w.type != "Other industries")

bind_rows(all.over50,ess.over50, cat5.over50)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)

fifty <- bind_rows(all.over50,ess.over50, cat5.over50)%>%
  mutate(age = "Over 50") %>%
  rename(above = `50+`,
         below = `under 50`,
         pct_over = pct_over50)

```   


#### Percent over 60.   

```{r}

all.over60 <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed",
         CLASSWKR == "Works for wages") %>%
  mutate(age = ifelse(as.numeric(as.character(AGE)) >= 60, "60+", "under 60")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(age, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = age, value = n) %>%
  mutate(pct_over60 = `60+`/(`60+` + `under 60`)) %>%
  mutate(w.type = "All workers")

ess.over60 <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed",
         CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>%
  mutate(age = ifelse(as.numeric(as.character(AGE)) >= 60, "60+", "under 60"), 
         essential = ifelse(Include %in% c("A","B"), "All essential workers", "No")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, age, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = age, value = n) %>%
  mutate(pct_over60 = `60+`/(`60+` + `under 60`)) %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

cat5.over60 <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed",
         CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>%
  mutate(age = ifelse(as.numeric(as.character(AGE)) >= 60, "60+", "under 60"), 
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, age, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = age, value = n) %>%
  mutate(pct_over60 = `60+`/(`60+` + `under 60`)) %>%
  rename(w.type = category5)%>%
  filter(w.type != "Other industries")

bind_rows(all.over60,ess.over60, cat5.over60)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)

sixty <- bind_rows(all.over60,ess.over60, cat5.over60)%>%
  mutate(age = "Over age 60")%>%
  rename(above = `60+`,
         below = `under 60`,
         pct_over = pct_over60)

bind_rows(fifty, sixty) %>%
  select(-above, -below) %>%
  spread(key = age, value = pct_over) %>%
  mutate(`Age 50 to 60` = `Over 50` - `Over age 60`) %>%
  select(-`Over 50`) %>%
  gather(-w.type, key = age, value = pct) %>%
  mutate(w.type.fac = factor(.$w.type, levels = c("All workers", "All essential workers","Essential retail", "Essential systems & services", "Health care & social assistance", "Production", "Transportation and trade")),
         age.fac = factor(.$age, levels = c("Over age 60","Age 50 to 60"))) %>%
    ggplot(aes(x=w.type.fac,y=pct, fill= age.fac)) +
  geom_bar(stat = "identity", position = "stack") + 
  coord_flip() +
  scale_fill_manual(values = c(DCcolor.p2blue40,
                               DCcolor.p2limegreen60),
                    guide = guide_legend(reverse = TRUE)) +
  scale_y_continuous(labels = percent_format(accuracy = 1),expand = expand_scale(mult = c(0,.1))) +
  geom_text(aes(label = percent(pct, accuracy = .1)), 
            # hjust = 1,
            hjust = 1.1, 
            position = position_stack(),
            size=2.75, 
            family="Asap") +
  themeDC_vertical() +
  theme(legend.title = element_blank(),
        legend.text = element_text( size = 8),
        legend.key.size = unit(.3,"cm"),
        legend.margin = margin(1,1,1,1),
        axis.text.x = element_text(size = 8, vjust = -.1, family="Asap"),
        plot.title = element_text(size=12),
        plot.subtitle = element_text(size=10),
        legend.position = "top",
        legend.justification = "left",
        legend.direction = "horizontal",
        legend.spacing.x = unit(.15, 'cm'))+
  labs(title = "Older workers by essential industry group",
       subtitle = "New Orleans metro, 2014 - 2018",
       x = "",
       y="") +
ggsave(filename = "outputs/graphicsEssentialWorkers/older_workers.png", type = "cairo",
       width = 8, height = 2.8, units = "in")
  
```  

#### Household/family context & responsibilities --- may become figure?   

##### Child in household   

```{r}
all.child <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(children = "No children", 
         children = ifelse(as.numeric(YNGCH) < 10, 
                           "Children younger than 10", 
                           children),
         children = ifelse(as.numeric(YNGCH) >= 10 & as.numeric(YNGCH) <18, 
                           "Children between 10 and 18", 
                           children)) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(children, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = children, value = n) %>%
    mutate(pct_childUnder10 = `Children younger than 10`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_child10_18 = `Children between 10 and 18`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_noChild = `No children`/(`Children younger than 10` + `Children between 10 and 18` + `No children`))%>%
  mutate(w.type = "All")


ess.child <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         children = "No children", 
         children = ifelse(as.numeric(YNGCH) < 10, 
                           "Children younger than 10", 
                           children),
         children = ifelse(as.numeric(YNGCH) >= 10 & as.numeric(YNGCH) <18, 
                           "Children between 10 and 18", 
                           children)) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential,children, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = children, value = n) %>%
    mutate(pct_childUnder10 = `Children younger than 10`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_child10_18 = `Children between 10 and 18`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_noChild = `No children`/(`Children younger than 10` + `Children between 10 and 18` + `No children`))%>%
  filter(essential != "No") %>%
  rename(w.type = essential)

bind_rows(all.child, ess.child)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

```{r}
all.child.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(children = "No children", 
         children = ifelse(as.numeric(YNGCH) < 10, 
                           "Children younger than 10", 
                           children),
         children = ifelse(as.numeric(YNGCH) >= 10 & as.numeric(YNGCH) <18, 
                           "Children between 10 and 18", 
                           children)) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(race.WOC, children, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = children, value = n) %>%
    mutate(pct_childUnder10 = `Children younger than 10`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_child10_18 = `Children between 10 and 18`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_noChild = `No children`/(`Children younger than 10` + `Children between 10 and 18` + `No children`))%>%
  mutate(w.type = "All")


ess.child.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         children = "No children", 
         children = ifelse(as.numeric(YNGCH) < 10, 
                           "Children younger than 10", 
                           children),
         children = ifelse(as.numeric(YNGCH) >= 10 & as.numeric(YNGCH) <18, 
                           "Children between 10 and 18", 
                           children)) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential,race.WOC, children, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = children, value = n) %>%
    mutate(pct_childUnder10 = `Children younger than 10`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_child10_18 = `Children between 10 and 18`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_noChild = `No children`/(`Children younger than 10` + `Children between 10 and 18` + `No children`))%>%
  filter(essential != "No") %>%
  rename(w.type = essential)

bind_rows(all.child.r, ess.child.r)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```  

```{r}
all.child.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(children = "No children", 
         children = ifelse(as.numeric(YNGCH) < 10, 
                           "Children younger than 10", 
                           children),
         children = ifelse(as.numeric(YNGCH) >= 10 & as.numeric(YNGCH) <18, 
                           "Children between 10 and 18", 
                           children)) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(children, SEX, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = children, value = n) %>%
    mutate(pct_childUnder10 = `Children younger than 10`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_child10_18 = `Children between 10 and 18`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_noChild = `No children`/(`Children younger than 10` + `Children between 10 and 18` + `No children`))%>%
  mutate(w.type = "All")


ess.child.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         children = "No children", 
         children = ifelse(as.numeric(YNGCH) < 10, 
                           "Children younger than 10", 
                           children),
         children = ifelse(as.numeric(YNGCH) >= 10 & as.numeric(YNGCH) <18, 
                           "Children between 10 and 18", 
                           children)) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential,children, SEX, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = children, value = n) %>%
    mutate(pct_childUnder10 = `Children younger than 10`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_child10_18 = `Children between 10 and 18`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_noChild = `No children`/(`Children younger than 10` + `Children between 10 and 18` + `No children`))%>%
  filter(essential != "No") %>%
  rename(w.type = essential)

bind_rows(all.child.s, ess.child.s)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

```{r}
all.child.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(children = "No children", 
         children = ifelse(as.numeric(YNGCH) < 10, 
                           "Children younger than 10", 
                           children),
         children = ifelse(as.numeric(YNGCH) >= 10 & as.numeric(YNGCH) <18, 
                           "Children between 10 and 18", 
                           children)) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(race.WOC, SEX, children, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = children, value = n) %>%
    mutate(pct_childUnder10 = `Children younger than 10`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_child10_18 = `Children between 10 and 18`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_noChild = `No children`/(`Children younger than 10` + `Children between 10 and 18` + `No children`))%>%
  mutate(w.type = "All")


ess.child.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         children = "No children", 
         children = ifelse(as.numeric(YNGCH) < 10, 
                           "Children younger than 10", 
                           children),
         children = ifelse(as.numeric(YNGCH) >= 10 & as.numeric(YNGCH) <18, 
                           "Children between 10 and 18", 
                           children)) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, race.WOC, SEX, children, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = children, value = n) %>%
    mutate(pct_childUnder10 = `Children younger than 10`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_child10_18 = `Children between 10 and 18`/(`Children younger than 10` + `Children between 10 and 18` + `No children`),
           pct_noChild = `No children`/(`Children younger than 10` + `Children between 10 and 18` + `No children`))%>%
  filter(essential != "No") %>%
  rename(w.type = essential)

bind_rows(all.child.r.s, ess.child.r.s)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```  

##### Older person in household (does include worker -- have to think through how to fix this).  

```{r}
all.old <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
   group_by(YEAR, SERIAL) %>%
  mutate(max.age.in.hh = max(as.numeric(AGE)),
         old.in.hh = ifelse(max.age.in.hh >= 60, "Yes", "No")) %>% 
  ungroup() %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(old.in.hh, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = old.in.hh, value = n) %>%
    mutate(pct_old.in.hh = Yes/(No+Yes))%>%
  mutate(w.type = "All")


ess.old <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>%
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>%
     group_by(YEAR, SERIAL) %>%
  mutate(max.age.in.hh = max(as.numeric(AGE)),
         old.in.hh = ifelse(max.age.in.hh >= 60, "Yes", "No")) %>% 
  ungroup() %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(essential) %>%
  survey_count(old.in.hh, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = old.in.hh, value = n) %>%
    mutate(pct_old.in.hh = Yes/(No+Yes))%>%
  filter(essential != "No") %>%
  rename(w.type = essential)

bind_rows(all.old, ess.old)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

```{r}
all.old.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
   group_by(YEAR, SERIAL) %>%
  mutate(max.age.in.hh = max(as.numeric(AGE)),
         old.in.hh = ifelse(max.age.in.hh >= 60, "Yes", "No")) %>% 
  ungroup() %>%
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(race.WOC, old.in.hh, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = old.in.hh, value = n) %>%
    mutate(pct_old.in.hh = Yes/(No+Yes))%>%
  mutate(w.type = "All")


ess.old.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>%
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>%
     group_by(YEAR, SERIAL) %>%
  mutate(max.age.in.hh = max(as.numeric(AGE)),
         old.in.hh = ifelse(max.age.in.hh >= 60, "Yes", "No")) %>% 
  ungroup() %>%
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(essential) %>%
  survey_count(race.WOC, old.in.hh, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = old.in.hh, value = n) %>%
    mutate(pct_old.in.hh = Yes/(No+Yes))%>%
  filter(essential != "No") %>%
  rename(w.type = essential)

bind_rows(all.old.r, ess.old.r)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

```{r}
all.old.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
   group_by(YEAR, SERIAL) %>%
  mutate(max.age.in.hh = max(as.numeric(AGE)),
         old.in.hh = ifelse(max.age.in.hh >= 60, "Yes", "No")) %>% 
  ungroup() %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(SEX, old.in.hh, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = old.in.hh, value = n) %>%
    mutate(pct_old.in.hh = Yes/(No+Yes))%>%
  mutate(w.type = "All")


ess.old.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>%
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>%
     group_by(YEAR, SERIAL) %>%
  mutate(max.age.in.hh = max(as.numeric(AGE)),
         old.in.hh = ifelse(max.age.in.hh >= 60, "Yes", "No")) %>% 
  ungroup() %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(essential) %>%
  survey_count(SEX, old.in.hh, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = old.in.hh, value = n) %>%
    mutate(pct_old.in.hh = Yes/(No+Yes))%>%
  filter(essential != "No") %>%
  rename(w.type = essential)

bind_rows(all.old.s, ess.old.s)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

```{r}
all.old.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
   group_by(YEAR, SERIAL) %>%
  mutate(max.age.in.hh = max(as.numeric(AGE)),
         old.in.hh = ifelse(max.age.in.hh >= 60, "Yes", "No")) %>% 
  ungroup() %>%
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(race.WOC, SEX, old.in.hh, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = old.in.hh, value = n) %>%
    mutate(pct_old.in.hh = Yes/(No+Yes))%>%
  mutate(w.type = "All")


ess.old.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>%
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>%
     group_by(YEAR, SERIAL) %>%
  mutate(max.age.in.hh = max(as.numeric(AGE)),
         old.in.hh = ifelse(max.age.in.hh >= 60, "Yes", "No")) %>% 
  ungroup() %>%
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(essential) %>%
  survey_count(race.WOC, SEX, old.in.hh, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = old.in.hh, value = n) %>%
    mutate(pct_old.in.hh = Yes/(No+Yes))%>%
  filter(essential != "No") %>%
  rename(w.type = essential)

bind_rows(all.old.r.s, ess.old.r.s)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

##### Household size: 3.    



#### Childcare stats.   

``` {r}

ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  # filter(EMPSTAT == "Employed",
  #        CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  filter(IND.fac == "8470") %>%
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(race.WOC, vartype = "se") %>% 
  mutate(pct = n/sum(n)) %>%
  kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

``` {r}

ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  # filter(EMPSTAT == "Employed",
  #        CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  filter(IND.fac == "8470") %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(SEX,  vartype = "se") %>% 
  mutate(pct = n/sum(n)) %>%
  kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

``` {r}

ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  # filter(EMPSTAT == "Employed",
  #        CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  filter(IND.fac == "8470") %>%
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(SEX, race.WOC, vartype = "se") %>% 
  mutate(pct = n/sum(n)) %>%
  kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

##### Income level.   

```{r}
ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  # filter(EMPSTAT == "Employed",
  #        CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  filter(IND.fac == "8470") %>%
  mutate(income.level= ifelse(INCWAGE.cpi <= 30000, "low", "high")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(income.level, vartype = "se") %>% 
  mutate(pct = n/sum(n)) %>%
  kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```  

##### Health insurance.   

```{r}

ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  # filter(EMPSTAT == "Employed",
  #        CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  filter(IND.fac == "8470") %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(HCOVANY, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = HCOVANY, value = n) %>%
    mutate(pct_no.health.cov = `No health insurance coverage`/(`No health insurance coverage` + `With health insurance coverage`)) %>%
  kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```


#### Socioeconomic stats   

##### Rent burden

```{r}
all.rb <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>%
  filter(OWNERSHP == "Rented") %>% 
  filter(HHINCOME != 9999999) %>% 
  mutate(rent.burden = ifelse(RENTGRS/(HHINCOME/12) > .3, "Yes", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(rent.burden, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = rent.burden, value = n) %>%
  mutate(pct_rent.burdened = Yes/(No + Yes)) %>%
  mutate(w.type = "All")

ess.rb <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  filter(OWNERSHP == "Rented") %>% 
  filter(HHINCOME != 9999999) %>% 
  mutate(rent.burden = ifelse(RENTGRS/(HHINCOME/12) > .3, "Yes", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, rent.burden, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = rent.burden, value = n) %>%
  mutate(pct_rent.burdened = Yes/(No + Yes)) %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

cat5.rb <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  filter(OWNERSHP == "Rented") %>% 
  filter(HHINCOME != 9999999) %>% 
  mutate(rent.burden = ifelse(RENTGRS/(HHINCOME/12) > .3, "Yes", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, rent.burden, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = rent.burden, value = n) %>%
  mutate(pct_rent.burdened = Yes/(No + Yes)) %>%
  rename(w.type = category5)

bind_rows(all.rb, ess.rb, cat5.rb)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

```{r}
all.rb.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>%
  filter(OWNERSHP == "Rented") %>% 
  filter(HHINCOME != 9999999) %>% 
  mutate(rent.burden = ifelse(RENTGRS/(HHINCOME/12) > .3, "Yes", "No")) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(race.WOC, rent.burden, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = rent.burden, value = n) %>%
  mutate(pct_rent.burdened = Yes/(No + Yes)) %>%
  mutate(w.type = "All")

ess.rb.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  filter(OWNERSHP == "Rented") %>% 
  filter(HHINCOME != 9999999) %>% 
  mutate(rent.burden = ifelse(RENTGRS/(HHINCOME/12) > .3, "Yes", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, race.WOC, rent.burden, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = rent.burden, value = n) %>%
  mutate(pct_rent.burdened = Yes/(No + Yes)) %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

cat5.rb.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  filter(OWNERSHP == "Rented") %>% 
  filter(HHINCOME != 9999999) %>% 
  mutate(rent.burden = ifelse(RENTGRS/(HHINCOME/12) > .3, "Yes", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, race.WOC, rent.burden, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = rent.burden, value = n) %>%
  mutate(pct_rent.burdened = Yes/(No + Yes)) %>%
  rename(w.type = category5)


bind_rows(all.rb.r, ess.rb.r, cat5.rb.r)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

```{r}
all.rb.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>%
  filter(OWNERSHP == "Rented") %>% 
  filter(HHINCOME != 9999999) %>% 
  mutate(rent.burden = ifelse(RENTGRS/(HHINCOME/12) > .3, "Yes", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(SEX, rent.burden, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = rent.burden, value = n) %>%
  mutate(pct_rent.burdened = Yes/(No + Yes)) %>%
  mutate(w.type = "All")

ess.rb.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  filter(OWNERSHP == "Rented") %>% 
  filter(HHINCOME != 9999999) %>% 
  mutate(rent.burden = ifelse(RENTGRS/(HHINCOME/12) > .3, "Yes", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, SEX, rent.burden, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = rent.burden, value = n) %>%
  mutate(pct_rent.burdened = Yes/(No + Yes)) %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

cat5.rb.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  filter(OWNERSHP == "Rented") %>% 
  filter(HHINCOME != 9999999) %>% 
  mutate(rent.burden = ifelse(RENTGRS/(HHINCOME/12) > .3, "Yes", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, SEX, rent.burden, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = rent.burden, value = n) %>%
  mutate(pct_rent.burdened = Yes/(No + Yes)) %>%
  rename(w.type = category5)

bind_rows(all.rb.s, ess.rb.s, cat5.rb.s)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```  

```{r}
all.rb.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>%
  filter(OWNERSHP == "Rented") %>% 
  filter(HHINCOME != 9999999) %>% 
  mutate(rent.burden = ifelse(RENTGRS/(HHINCOME/12) > .3, "Yes", "No")) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(race.WOC, SEX, rent.burden, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = rent.burden, value = n) %>%
  mutate(pct_rent.burdened = Yes/(No + Yes)) %>%
  mutate(w.type = "All")

ess.rb.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  filter(OWNERSHP == "Rented") %>% 
  filter(HHINCOME != 9999999) %>% 
  mutate(rent.burden = ifelse(RENTGRS/(HHINCOME/12) > .3, "Yes", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, race.WOC, SEX, rent.burden, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = rent.burden, value = n) %>%
  mutate(pct_rent.burdened = Yes/(No + Yes)) %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

cat5.rb.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  filter(OWNERSHP == "Rented") %>% 
  filter(HHINCOME != 9999999) %>% 
  mutate(rent.burden = ifelse(RENTGRS/(HHINCOME/12) > .3, "Yes", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, race.WOC, SEX, rent.burden, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = rent.burden, value = n) %>%
  mutate(pct_rent.burdened = Yes/(No + Yes)) %>%
  rename(w.type = category5)

bind_rows(all.rb.r.s, ess.rb.r.s, cat5.rb.r.s)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```  

##### Received food stamps in past year.  

```{r}
all.snap <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(FOODSTMP, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = FOODSTMP, value = n) %>%
    mutate(pct_rec.foodstamps = Yes/(No + Yes)) %>%
  mutate(w.type = "All")

ess.snap <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(essential) %>% 
  survey_count(FOODSTMP, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = FOODSTMP, value = n) %>%
    mutate(pct_rec.foodstamps = Yes/(No + Yes)) %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

cat5.snap <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(category5) %>% 
  survey_count(FOODSTMP, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = FOODSTMP, value = n) %>%
    mutate(pct_rec.foodstamps = Yes/(No + Yes)) %>%
  rename(w.type = category5)

bind_rows(all.snap, ess.snap, cat5.snap)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)

```  

```{r}
all.snap.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(race.WOC, FOODSTMP, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = FOODSTMP, value = n) %>%
    mutate(pct_rec.foodstamps = Yes/(No + Yes)) %>%
  mutate(w.type = "All")

ess.snap.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(essential) %>% 
  survey_count(race.WOC, FOODSTMP, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = FOODSTMP, value = n) %>%
    mutate(pct_rec.foodstamps = Yes/(No + Yes)) %>%
  filter(essential != "No") %>%
  rename(w.type = essential)


cat5.snap.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(category5) %>% 
  survey_count(race.WOC, FOODSTMP, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = FOODSTMP, value = n) %>%
    mutate(pct_rec.foodstamps = Yes/(No + Yes)) %>%
  rename(w.type = category5)

bind_rows(all.snap.r, ess.snap.r, cat5.snap.r )%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)

```  

```{r}
all.snap.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(SEX, FOODSTMP, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = FOODSTMP, value = n) %>%
    mutate(pct_rec.foodstamps = Yes/(No + Yes)) %>%
  mutate(w.type = "All")

ess.snap.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(essential) %>% 
  survey_count(SEX, FOODSTMP, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = FOODSTMP, value = n) %>%
    mutate(pct_rec.foodstamps = Yes/(No + Yes)) %>%
  filter(essential != "No") %>%
  rename(w.type = essential)


cat5.snap.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(category5) %>% 
  survey_count(SEX, FOODSTMP, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = FOODSTMP, value = n) %>%
    mutate(pct_rec.foodstamps = Yes/(No + Yes)) %>%
  rename(w.type = category5)

bind_rows(all.snap.s, ess.snap.s, cat5.snap.s)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)

```  

```{r}
all.snap.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(race.WOC, SEX, FOODSTMP, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = FOODSTMP, value = n) %>%
    mutate(pct_rec.foodstamps = Yes/(No + Yes)) %>%
  mutate(w.type = "All")

ess.snap.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(essential) %>% 
  survey_count(race.WOC, SEX, FOODSTMP, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = FOODSTMP, value = n) %>%
    mutate(pct_rec.foodstamps = Yes/(No + Yes)) %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

cat5.snap.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(category5) %>% 
  survey_count(race.WOC, SEX, FOODSTMP, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = FOODSTMP, value = n) %>%
    mutate(pct_rec.foodstamps = Yes/(No + Yes)) %>%
  rename(w.type = category5)

bind_rows(all.snap.r.s, ess.snap.r.s, cat5.snap.r.s)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)

```   

##### < 200% poverty.   

```{r}
all.pov <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed") %>% # employed workers only
  filter(CLASSWKR == "Works for wages") %>%
  filter(POVERTY != 0) %>% 
  mutate(pov200 = ifelse(POVERTY < 200, "Below 200%", "200% or higher")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(pov200, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = pov200, value = n) %>%
  mutate(pct_below200 = `Below 200%`/(`Below 200%` + `200% or higher`))  %>%
  mutate(w.type = "All")


ess.pov <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed") %>% # employed workers only
  filter(CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>% 
  filter(POVERTY != 0) %>% 
  mutate(pov200 = ifelse(POVERTY < 200, "Below 200%", "200% or higher")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(essential) %>% 
  survey_count(pov200, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = pov200, value = n) %>%
  mutate(pct_below200 = `Below 200%`/(`Below 200%` + `200% or higher`))  %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

cat5.pov <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed") %>% # employed workers only
  filter(CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  filter(POVERTY != 0) %>% 
  mutate(pov200 = ifelse(POVERTY < 200, "Below 200%", "200% or higher")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  group_by(category5) %>% 
  survey_count(pov200, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = pov200, value = n) %>%
  mutate(pct_below200 = `Below 200%`/(`Below 200%` + `200% or higher`))  %>%
  rename(w.type = category5)


bind_rows(all.pov, ess.pov, cat5.pov)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```  

```{r}
all.pov.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed") %>% # employed workers only
  filter(CLASSWKR == "Works for wages") %>%
  filter(POVERTY != 0) %>% 
  mutate(pov200 = ifelse(POVERTY < 200, "Below 200%", "200% or higher")) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(race.WOC, pov200, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = pov200, value = n) %>%
  mutate(pct_below200 = `Below 200%`/(`Below 200%` + `200% or higher`))  %>%
  mutate(w.type = "All")


ess.pov.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed") %>% # employed workers only
  filter(CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  filter(POVERTY != 0) %>% 
  mutate(pov200 = ifelse(POVERTY < 200, "Below 200%", "200% or higher")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, race.WOC, pov200, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = pov200, value = n) %>%
  mutate(pct_below200 = `Below 200%`/(`Below 200%` + `200% or higher`))  %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

cat5.pov.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed") %>% # employed workers only
  filter(CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  filter(POVERTY != 0) %>% 
  mutate(pov200 = ifelse(POVERTY < 200, "Below 200%", "200% or higher")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, race.WOC, pov200, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = pov200, value = n) %>%
  mutate(pct_below200 = `Below 200%`/(`Below 200%` + `200% or higher`))  %>%
  rename(w.type = category5)

bind_rows(all.pov.r, ess.pov.r, cat5.pov.r)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```  

```{r}
all.pov.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed") %>% # employed workers only
  filter(CLASSWKR == "Works for wages") %>%
  filter(POVERTY != 0) %>% 
  mutate(pov200 = ifelse(POVERTY < 200, "Below 200%", "200% or higher")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(SEX, pov200, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = pov200, value = n) %>%
  mutate(pct_below200 = `Below 200%`/(`Below 200%` + `200% or higher`))  %>%
  mutate(w.type = "All")


ess.pov.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed") %>% # employed workers only
  filter(CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>% 
  filter(POVERTY != 0) %>% 
  mutate(pov200 = ifelse(POVERTY < 200, "Below 200%", "200% or higher")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, SEX, pov200, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = pov200, value = n) %>%
  mutate(pct_below200 = `Below 200%`/(`Below 200%` + `200% or higher`))  %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

cat5.pov.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed") %>% # employed workers only
  filter(CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  filter(POVERTY != 0) %>% 
  mutate(pov200 = ifelse(POVERTY < 200, "Below 200%", "200% or higher")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, SEX, pov200, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = pov200, value = n) %>%
  mutate(pct_below200 = `Below 200%`/(`Below 200%` + `200% or higher`))  %>%
  rename(w.type = category5)

bind_rows(all.pov.s, ess.pov.s, cat5.pov.s)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```  

```{r}
all.pov.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed") %>% # employed workers only
  filter(CLASSWKR == "Works for wages") %>%
  filter(POVERTY != 0) %>% 
  mutate(pov200 = ifelse(POVERTY < 200, "Below 200%", "200% or higher")) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(race.WOC, SEX, pov200, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = pov200, value = n) %>%
  mutate(pct_below200 = `Below 200%`/(`Below 200%` + `200% or higher`))  %>%
  mutate(w.type = "All")


ess.pov.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed") %>% # employed workers only
  filter(CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  filter(POVERTY != 0) %>% 
  mutate(pov200 = ifelse(POVERTY < 200, "Below 200%", "200% or higher")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, race.WOC, SEX, pov200, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = pov200, value = n) %>%
  mutate(pct_below200 = `Below 200%`/(`Below 200%` + `200% or higher`))  %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

cat5.pov.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed") %>% # employed workers only
  filter(CLASSWKR == "Works for wages") %>%
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  filter(POVERTY != 0) %>% 
  mutate(pov200 = ifelse(POVERTY < 200, "Below 200%", "200% or higher")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, race.WOC, SEX, pov200, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = pov200, value = n) %>%
  mutate(pct_below200 = `Below 200%`/(`Below 200%` + `200% or higher`))  %>%
  rename(w.type = category5)

bind_rows(all.pov.r.s, ess.pov.r.s, cat5.pov.r.s)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```  

##### Health insurance.   

```{r}
all.ins <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(HCOVANY, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = HCOVANY, value = n) %>%
    mutate(pct_no.health.cov = `No health insurance coverage`/(`No health insurance coverage` + `With health insurance coverage`)) %>%
  mutate(w.type = "All")


ess.ins <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, HCOVANY, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = HCOVANY, value = n) %>%
    mutate(pct_no.health.cov = `No health insurance coverage`/(`No health insurance coverage` + `With health insurance coverage`))  %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

cat5.ins <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, HCOVANY, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = HCOVANY, value = n) %>%
    mutate(pct_no.health.cov = `No health insurance coverage`/(`No health insurance coverage` + `With health insurance coverage`))  %>%
  rename(w.type = category5)

bind_rows(all.ins, ess.ins, cat5.ins)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

```{r}
all.ins.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(race.WOC, HCOVANY, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = HCOVANY, value = n) %>%
    mutate(pct_no.health.cov = `No health insurance coverage`/(`No health insurance coverage` + `With health insurance coverage`)) %>%
  mutate(w.type = "All")


ess.ins.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>%
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, race.WOC, HCOVANY, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = HCOVANY, value = n) %>%
    mutate(pct_no.health.cov = `No health insurance coverage`/(`No health insurance coverage` + `With health insurance coverage`))  %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

bind_rows(all.ins.r, ess.ins.r)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   

```{r}
all.ins.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(SEX, HCOVANY, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = HCOVANY, value = n) %>%
    mutate(pct_no.health.cov = `No health insurance coverage`/(`No health insurance coverage` + `With health insurance coverage`)) %>%
  mutate(w.type = "All")


ess.ins.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>% 
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, SEX, HCOVANY, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = HCOVANY, value = n) %>%
    mutate(pct_no.health.cov = `No health insurance coverage`/(`No health insurance coverage` + `With health insurance coverage`))  %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

bind_rows(all.ins.s, ess.ins.s)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```  

```{r}
all.ins.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(race.WOC, SEX, HCOVANY, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = HCOVANY, value = n) %>%
    mutate(pct_no.health.cov = `No health insurance coverage`/(`No health insurance coverage` + `With health insurance coverage`)) %>%
  mutate(w.type = "All")


ess.ins.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>%
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, race.WOC, SEX, HCOVANY, vartype = "se") %>% 
    select(-n_se) %>%
  spread(key = HCOVANY, value = n) %>%
    mutate(pct_no.health.cov = `No health insurance coverage`/(`No health insurance coverage` + `With health insurance coverage`))  %>%
  filter(essential != "No") %>%
  rename(w.type = essential)

bind_rows(all.ins.r.s, ess.ins.r.s)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```   


#### High- (>\$30k) and low- (<=$30K) earning workers.

```{r}
all.hilo <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(income.level= ifelse(INCWAGE.cpi <= 30000, "low", "high")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(income.level, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = income.level, value = n) %>%
  mutate(pct_high = high/(high + low),
         pct_low = low/(high + low)) %>%
  mutate(w.type = "All workers")

ess.hilo <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(income.level= ifelse(INCWAGE.cpi <= 30000, "low", "high"),
         essential = ifelse(Include %in% c("A","B"), "All essential workers", "No")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, income.level, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = income.level, value = n) %>%
  mutate(pct_high = high/(high + low),
         pct_low = low/(high + low)) %>%
    filter(essential != "No") %>%
  rename(w.type = essential)

cat5.hilo <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(income.level= ifelse(INCWAGE.cpi <= 30000, "low", "high"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, income.level, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = income.level, value = n) %>%
  mutate(pct_high = high/(high + low),
         pct_low = low/(high + low)) %>%
    filter(category5 != "Other industries") %>%
  rename(w.type = category5)

bind_rows(all.hilo, ess.hilo, cat5.hilo)%>%
  mutate(w.type.fac = factor(.$w.type, levels = c("All workers", "All essential workers","Essential retail", "Essential systems & services", "Health care & social assistance", "Production", "Transportation and trade"))) %>%
  #   kable(format = "markdown", digits = 3) %>%
  # kable_styling(bootstrap_options = "striped", full_width = F)
  ggplot(aes(x=w.type.fac,y=pct_low)) +
  geom_bar(stat = "identity", fill = "lightblue") + 
  coord_flip() +
  scale_y_continuous(labels = percent_format(accuracy = 1),expand = expand_scale(mult = c(0,.1))) +
  geom_text(aes(label = percent(pct_low, accuracy = .1)), 
            # hjust = 1,
            hjust = -.3, 
            position = position_dodge(width = .85),
            size=2.75, 
            family="Asap") +
  themeDC_vertical() +
  theme(legend.position = "none",
        axis.text.x = element_text(size = 8, vjust = -.1, family="Asap"),
        plot.title = element_text(size=12),
        plot.subtitle = element_text(size=10))+
  labs(title = "Percent of workers earning less than $30,000 annually by essential industry group",
       subtitle = "New Orleans metro, 2014 - 2018",
       x = "",
       y="") +
ggsave(filename = "outputs/graphicsEssentialWorkers/lowinc_bars.png", type = "cairo",
       width = 8, height = 2.5, units = "in")
```   

```{r}
all.hilo.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(income.level= ifelse(INCWAGE.cpi <= 30000, "low", "high")) %>%
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(race.WOC, income.level, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = income.level, value = n) %>%
  mutate(pct_high = high/(high + low),
         pct_low = low/(high + low)) %>%
  mutate(w.type = "All")

ess.hilo.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(income.level= ifelse(INCWAGE.cpi <= 30000, "low", "high"),
         essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>%
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, race.WOC, income.level, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = income.level, value = n) %>%
  mutate(pct_high = high/(high + low),
         pct_low = low/(high + low)) %>%
    filter(essential != "No") %>%
  rename(w.type = essential)

cat5.hilo.r <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(income.level= ifelse(INCWAGE.cpi <= 30000, "low", "high"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>%
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White", "WOC")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, race.WOC, income.level, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = income.level, value = n) %>%
  mutate(pct_high = high/(high + low),
         pct_low = low/(high + low)) %>%
    filter(category5 != "Other industries") %>%
  rename(w.type = category5)

bind_rows(all.hilo.r, ess.hilo.r, cat5.hilo.r)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```  

```{r}
all.hilo.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(income.level= ifelse(INCWAGE.cpi <= 30000, "low", "high")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(SEX, income.level, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = income.level, value = n) %>%
  mutate(pct_high = high/(high + low),
         pct_low = low/(high + low)) %>%
  mutate(w.type = "All")

ess.hilo.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(income.level= ifelse(INCWAGE.cpi <= 30000, "low", "high"),
         essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, SEX, income.level, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = income.level, value = n) %>%
  mutate(pct_high = high/(high + low),
         pct_low = low/(high + low)) %>%
    filter(essential != "No") %>%
  rename(w.type = essential)

cat5.hilo.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(income.level= ifelse(INCWAGE.cpi <= 30000, "low", "high"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, SEX, income.level, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = income.level, value = n) %>%
  mutate(pct_high = high/(high + low),
         pct_low = low/(high + low)) %>%
    filter(category5 != "Other industries") %>%
  rename(w.type = category5)

bind_rows(all.hilo.s, ess.hilo.s, cat5.hilo.s)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
```  

```{r}
all.hilo.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  mutate(income.level= ifelse(INCWAGE.cpi <= 30000, "low", "high")) %>%
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White workers", "Workers of color")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(race.WOC, SEX, income.level, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = income.level, value = n) %>%
  mutate(pct_high = high/(high + low),
         pct_low = low/(high + low)) %>%
  mutate(w.type = "All")

ess.hilo.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(income.level= ifelse(INCWAGE.cpi <= 30000, "low", "high"),
         essential = ifelse(Include %in% c("A","B"), "Essential", "No")) %>%
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White workers", "Workers of color")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(essential, race.WOC, SEX, income.level, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = income.level, value = n) %>%
  mutate(pct_high = high/(high + low),
         pct_low = low/(high + low)) %>%
    filter(essential != "No") %>%
  rename(w.type = essential)

cat5.hilo.r.s <- ipums.la.fac %>% 
  filter(PUMA %in% NOmsa.pumacodes, # filter to Orleans
         YEAR %in% as.character(2014:2018)) %>%  # filter by sample year
  filter(EMPSTAT == "Employed", CLASSWKR == "Works for wages") %>% 
  left_join(vulnerableEssentialWorkers, by=c("IND.fac"="ACScode")) %>% 
  mutate(income.level= ifelse(INCWAGE.cpi <= 30000, "low", "high"),
         category5 = ifelse(is.na(category5), "Other industries", category5)) %>%
  mutate(race.WOC = ifelse(RACE == "White" & HISPAN == "Not Hispanic", "White workers", "Workers of color")) %>%
  as_survey_design(ids = CLUSTER,
                   strata = STRATA, 
                   weights = PERWT) %>% 
  survey_count(category5, race.WOC, SEX, income.level, vartype = "se") %>% 
  select(-n_se) %>%
  spread(key = income.level, value = n) %>%
  mutate(pct_high = high/(high + low),
         pct_low = low/(high + low)) %>%
    filter(category5 != "Other industries") %>%
  rename(w.type = category5)

bind_rows(all.hilo.r.s, ess.hilo.r.s, cat5.hilo.r.s)%>%
    kable(format = "markdown", digits = 3) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)

ess.hilo.r.s %>%
  mutate(desc = paste(race.WOC, SEX, sep = ",\n")) %>%
    ggplot(aes(x=desc,y=pct_low, fill = desc)) +
  geom_bar(stat = "identity") + #, fill = "lightblue"
  coord_flip() +
  scale_fill_manual(values = c(DCcolor.p2blue80,
                               DCcolor.p2teal70,
                               DCcolor.p2violet90,
                               DCcolor.p2purple80)) +
  scale_y_continuous(labels = percent_format(accuracy = 1),expand = expand_scale(mult = c(0,.1))) +
  geom_text(aes(label = percent(pct_low, accuracy = .1)), 
            # hjust = 1,
            hjust = -.3, 
            position = position_dodge(width = .85),
            size=2.75, 
            family="Asap") +
  themeDC_vertical() +
  theme(legend.position = "none",
        axis.text.x = element_text(size = 8, vjust = -.1, family="Asap"),
        plot.title = element_text(size=12),
        plot.subtitle = element_text(size=10))+
  labs(title = "Percent of essential workers earning less than $30,000 annually by race and sex",
       subtitle = "New Orleans metro, 2014 - 2018",
       x = "",
       y="") +
ggsave(filename = "outputs/graphicsEssentialWorkers/lowinc_racesex_bars.png", type = "cairo",
       width = 8, height = 2.5, units = "in")
  
```  