knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

This vignette reproduces the results of some popular segregation packages.

library(rsegregation)
library(testthat)
library(dplyr, warn.conflicts = FALSE)

The Divergence Index: A Decomposable Measure of Segregation and Inequality

In this paper, Elizabeth Roberto calculates divergence and entropy scores for the city of Detroit. Her calculations compare the black and white population specifically.

Load Data

load(system.file('extdata','detroit_race.rda', package='rsegregation', mustWork = T))


# get black/white specific stats
detroit_mod <- detroit_race %>%
    filter(population>0) %>%
    mutate(pop_bw=population*(black+white), black_bw=black/(black+white),
      white_bw=white/(black+white))

Divergence Index

# Roberto's results
robCity <- 0.14
robMetro <- 0.48

city <- dplyr::filter(detroit_mod, grepl('Detroit',place_name))

# compare city black/white results
city_bw <- summarize(city, div=divergence(black_bw, white_bw,
  population=pop_bw, summed=T, logBase=2))
expect_equivalent(city_bw$div, robCity, tolerance=0.01)

# compare metro results
metro_bw <- summarize(detroit_mod, div=divergence(black_bw, white_bw,
  population=pop_bw, summed=T, logBase=2))
expect_equal(metro_bw$div, robMetro, tolerance=0.01)

Decomposed Divergence

rob_result <- data.frame(within=c(0.05,0.32), between=c(0.5,0.14))
dec <- detroit_mod %>%
    transmute(place_name = ifelse(grepl('Detroit',place_name), place_name, 'Suburbs'),
      pop_bw, white_bw, black_bw) %>%
    decompose_divergence(groupCol='place_name', popCol = 'pop_bw', output='percentage',
      logBase=2) %>%
    select(-place_name) %>% as.data.frame()


  expect_equal(dec, rob_result, tolerance = 0.03, ignore_attr=T)

Entropy/Information Theory Indexes

Entropy has several scores, comparing them here.

# compare city black/white results
  create_comp <- function(df) summarize(df, ent=entropy(black_bw, white_bw,
    population=pop_bw, summed=T, logBase=2),
    inf=entropy(black_bw, white_bw, entropy_type = 'information_theory',
      population=pop_bw, summed=T, logBase=2))
  city_bw <- create_comp(city)
  expect_equivalent(city_bw$ent, 0.29, tolerance=0.01)
  expect_equivalent(city_bw$inf, 0.32, tolerance=0.01)

  # compare metro results
  metro_bw <- create_comp(detroit_mod)
  expect_equal(metro_bw$ent, 0.33, tolerance=0.01)
  expect_equivalent(metro_bw$inf, 0.59, tolerance=0.01)


arthurgailes/rsegregation documentation built on May 23, 2021, 6:33 a.m.