Objective: To create two segregation index (dissimilarity and interaction) by race in Texas 2019

library(tidycensus)

Getting data from acs

race <-  get_acs(geography = "tract",
                         year=2019,
                         geometry = F,
                         output="wide",
                         table = "DP05",
                         cache_table = T,
                         state = "TX")
## Getting data from the 2015-2019 5-year ACS
## Loading ACS5/PROFILE variables for 2019 from table DP05 and caching the dataset for faster future access.
## Using the ACS Data Profile
## Using the ACS Data Profile
## Using the ACS Data Profile
## Using the ACS Data Profile
## Using the ACS Data Profile
## Using the ACS Data Profile
## Using the ACS Data Profile
## Using the ACS Data Profile
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
tract<-race%>%
  mutate(nhwhite=DP05_0037E,
         nhblack=DP05_0038E,
         hisp=DP05_0070E, 
         total=DP05_0033E,
         year=2019,
         cofips=substr(GEOID, 1,5))%>%
  select(GEOID,nhwhite, nhblack , hisp, total, year, cofips )%>%
  arrange(cofips, GEOID)

#look at the first few cases
head(tract)
## # A tibble: 6 x 7
##   GEOID       nhwhite nhblack  hisp total  year cofips
##   <chr>         <dbl>   <dbl> <dbl> <dbl> <dbl> <chr> 
## 1 48001950100    4341     284  4844  4844  2019 48001 
## 2 48001950401    2872    1890  4838  4838  2019 48001 
## 3 48001950402    4400    3063  7511  7511  2019 48001 
## 4 48001950500    2982    1012  4465  4465  2019 48001 
## 5 48001950600    3549    1545  5148  5148  2019 48001 
## 6 48001950700    1314    1059  2783  2783  2019 48001

The number of non-Hispanic black and non-Hispanic white at tract and county level have been selected to understand the segregation.

county<-tract%>%
  group_by(cofips)%>%
  summarise(co_total=sum(total),
            co_wht=sum(nhwhite),
            co_blk=sum(nhblack),
            cohisp=sum(hisp))

#we merge the county data back to the tract data by the county FIPS code
merged<-left_join(x=tract,
                  y=county,
                  by="cofips")


head(merged)

Merged the dataset with tract and county total population by race and ethnicity.

Dissimilarity index

d<-merged%>%
  mutate(d.wb=abs(nhwhite/co_wht - nhblack/co_blk))%>%
  group_by(cofips)%>%
  summarise(dissim= .5*sum(d.wb, na.rm=T))
i<-merged%>%
  mutate(int.bw=(nhblack/co_blk * nhwhite/total))%>%
  group_by(cofips)%>%
  summarise(int= sum(int.bw, na.rm=T))
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.2     v stringr 1.4.0
## v tidyr   1.1.3     v forcats 0.5.1
## v readr   1.4.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
tx_seg<-list(d, i)%>% reduce (left_join, by="cofips")
library(tigris)
## To enable 
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
options(tigris_class = "sf")
tx_counties<- counties(state="TX", cb=T, year=2010)
tx_counties$cofips<-substr(tx_counties$GEO_ID, 10,15)

twoseg<- geo_join(tx_counties, tx_seg, by_sp="cofips", by_df="cofips")
## Warning: We recommend using the dplyr::*_join() family of functions instead.
## Warning: `group_by_()` was deprecated in dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help

Map for dissimilarity index of nhblack and nhwhite in Texas 2019

library(ggplot2)

twoseg%>%
  ggplot()+geom_sf(aes(fill=dissim))+
  scale_fill_viridis_c()+
  scale_color_viridis_c()+
  ggtitle("Black and white dissimilarity index", subtitle = "2019 ACS")

Description: Dissimilarity index indicates the distribution of a group into popoulaiton. In this case, on the map, 0 index indicates both non-Hispanic black and non-Hispanic white equally distributed on the proportion of total population of the counties. Higher index score indicates counties with either of them share highet proportion of the population.

Map for iteraction index of nhblack and nhwhite in Texas 2019

library(ggplot2)

twoseg%>%
  ggplot()+geom_sf(aes(fill=int))+
  scale_fill_viridis_c()+
  scale_color_viridis_c()+
  ggtitle("Black and white iteraction index", subtitle = "2019 ACS")

Description: Interaction index provides information about how two groups are evenly distributed and have equal number at the tract level. The probability of black people meets white people higher in western part of the Texas. Less chance in the eastern part.

Higher dissimilarity index score at the western part of Texas indicates lower interaction of non-Hispanic Black and non-Hispanic white in the map.