Contingency tables

Inference for Categorical Data in R

Andrew Bray

Assistant Professor of Statistics at Reed College

Politics and military spending

gss2016 %>%
  select(party, natarms) %>%
  glimpse()
Observations: 150
Variables: 2
$ party   <fct> Ind, Ind, Dem, Ind, Ind, Ind, Ind, Dem, Dem, Ind,...
$ natarms <fct> TOO LITTLE, TOO MUCH, TOO MUCH,...
Inference for Categorical Data in R

Politics and military spending

ggplot(gss2016, aes(x = party, fill = natarms)) +
  geom_bar(position = "fill")

ch3v1-natarms-barplot-filled.png

Inference for Categorical Data in R

Politics and military spending

ggplot(gss2016, aes(x = party, fill = natarms)) +
  geom_bar()
library(broom)

ch3v1-natarms-barplot-stacked.png

Inference for Categorical Data in R

Tables and tidy data

tab <- gss2016 %>%
  select(natarms, party) %>%
  table()
tab
             party
natarms          Dem  Ind  Rep  Oth
  TOO LITTLE     17    20   24    0
  ABOUT RIGHT    14    28    8    1
  TOO MUCH       12    24    2    0

ch3v1-natarms-barplot.png

Inference for Categorical Data in R

Tables and tidy data

tab <- gss2016 %>%
  select(natarms, party) %>%
  table()
tab
             party
natarms          Dem  Ind  Rep  Oth
  TOO LITTLE     17    20   24    0
  ABOUT RIGHT    14    28    8    1
  TOO MUCH       12    24    2    0
tab %>%
  tidy()
# A tibble: 12 x 3
   natarms     party     n
   <chr>       <chr> <int>
 1 TOO LITTLE  Dem      17
 2 ABOUT RIGHT Dem      14
 3 TOO MUCH    Dem      12
 4 TOO LITTLE  Ind      20
 5 ABOUT RIGHT Ind      28
 6 TOO MUCH    Ind      24
 7 TOO LITTLE  Rep      24
 8 ABOUT RIGHT Rep       8
 9 TOO MUCH    Rep       2
10 TOO LITTLE  Oth       0
11 ABOUT RIGHT Oth       1
12 TOO MUCH    Oth       0
Inference for Categorical Data in R

Tables and tidy data

tab <- gss2016 %>%
  select(natarms, party) %>%
  table()
tab
             party
natarms          Dem  Ind  Rep  Oth
  TOO LITTLE     17    20   24    0
  ABOUT RIGHT    14    28    8    1
  TOO MUCH       12    24    2    0
tab %>%
  tidy() %>%
  uncount(n)
# A tibble: 150 x 2
   natarms    party
   <chr>      <chr>
 1 TOO LITTLE Dem  
 2 TOO LITTLE Dem  
 3 TOO LITTLE Dem  
 4 TOO LITTLE Dem  
 5 TOO LITTLE Dem  
 6 TOO LITTLE Dem  
 7 TOO LITTLE Dem  
 8 TOO LITTLE Dem  
 9 TOO LITTLE Dem  
10 TOO LITTLE Dem  
# … with 140 more rows
Inference for Categorical Data in R

Let's practice!

Inference for Categorical Data in R

Preparing Video For Download...