---
title: "Categorical Data, Simpson's Paradox"
author: "Evan L. Ray"
date: "September 11, 2017"
output: ioslides_presentation
---

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

library(dplyr)
library(tidyr)
```

## Murder Cases in Indiana, 1977-1999 =(

```{r setup-data, echo = FALSE}
set.seed(1234)

murder_cases <- rbind(
  expand.grid(
    case_id = seq_len(2139),
    offender_race = "black",
    victim_race = "black",
    sentence = "jail",
    stringsAsFactors = FALSE
  ),
  expand.grid(
    case_id = seq_len(12),
    offender_race = "black",
    victim_race = "black",
    sentence = "death",
    stringsAsFactors = FALSE
  ),
  expand.grid(
    case_id = seq_len(100),
    offender_race = "white",
    victim_race = "black",
    sentence = "jail",
    stringsAsFactors = FALSE
  ),
  expand.grid(
    case_id = seq_len(0),
    offender_race = "white",
    victim_race = "black",
    sentence = "death",
    stringsAsFactors = FALSE
  ),
  expand.grid(
    case_id = seq_len(359),
    offender_race = "black",
    victim_race = "white",
    sentence = "jail",
    stringsAsFactors = FALSE
  ),
  expand.grid(
    case_id = seq_len(16),
    offender_race = "black",
    victim_race = "white",
    sentence = "death",
    stringsAsFactors = FALSE
  ),
  expand.grid(
    case_id = seq_len(2223),
    offender_race = "white",
    victim_race = "white",
    sentence = "jail",
    stringsAsFactors = FALSE
  ),
  expand.grid(
    case_id = seq_len(49),
    offender_race = "white",
    victim_race = "white",
    sentence = "death",
    stringsAsFactors = FALSE
  )
)

murder_cases <- murder_cases %>%
  mutate(case_id = sample(seq_len(nrow(murder_cases))),
    offender_race = factor(offender_race),
    victim_race = factor(victim_race),
    sentence = factor(sentence)) %>%
  arrange(case_id)
```

```{r show-data, echo = TRUE}
head(murder_cases)
str(murder_cases)
```

## Death Sentence Counts

Here are the same data, summarized in a contingency table:

```{r show-contingency-table, echo = TRUE}
table(murder_cases$offender_race, murder_cases$sentence)
```

Let's calculate:

 * The **joint distribution** of the offender's race and the sentence.
 * The **marginal distribution** of the sentence.
 * The **conditional distribution** of the sentence given that the offender was black. (And again, for white offenders)
 * Is the sentence **independent** of the offender's race?


## Looking a little deeper...

```{r show-contingency-table-white-victim, echo = TRUE}
murder_cases_white_victim <- filter(murder_cases, victim_race == "white")
table(murder_cases_white_victim$offender_race,
  murder_cases_white_victim$sentence)
```

```{r show-contingency-table-black-victim, echo = TRUE}
murder_cases_black_victim <- filter(murder_cases, victim_race == "black")
table(murder_cases_black_victim$offender_race,
  murder_cases_black_victim$sentence)
```

## References

The original data were published in:

Blume et al., Explaining Death Row's Population and Racial Composition.  Journal of Empirical Legal Studies, Vol. 1, Issue 1, p. 165-207, 2004.

The issue of Simpson's paradox in relation to these data was discussed further in:

Norton et al., Simpson's Paradox and How to Avoid It.  Significance, p. 40-43, August, 2015.
