# get the pipe operator
library(magrittr)
HairEyeColor
## , , Sex = Male
##
## Eye
## Hair Brown Blue Hazel Green
## Black 32 11 10 3
## Brown 53 50 25 15
## Red 10 10 7 7
## Blond 3 30 5 8
##
## , , Sex = Female
##
## Eye
## Hair Brown Blue Hazel Green
## Black 36 9 5 2
## Brown 66 34 29 14
## Red 16 7 7 7
## Blond 4 64 5 8
str(HairEyeColor)
## table [1:4, 1:4, 1:2] 32 53 10 3 11 50 10 30 10 25 ...
## - attr(*, "dimnames")=List of 3
## ..$ Hair: chr [1:4] "Black" "Brown" "Red" "Blond"
## ..$ Eye : chr [1:4] "Brown" "Blue" "Hazel" "Green"
## ..$ Sex : chr [1:2] "Male" "Female"
Sum across sex:
HairEyeColor %>%
margin.table(c(1,2))
## Eye
## Hair Brown Blue Hazel Green
## Black 68 20 15 5
## Brown 119 84 54 29
## Red 26 17 14 14
## Blond 7 94 10 16
Joint distribution table (combined across gender):
HairEyeColor %>%
margin.table(c(1,2)) %>%
prop.table() %>%
round(2)
## Eye
## Hair Brown Blue Hazel Green
## Black 0.11 0.03 0.03 0.01
## Brown 0.20 0.14 0.09 0.05
## Red 0.04 0.03 0.02 0.02
## Blond 0.01 0.16 0.02 0.03
Marginal for hair:
HairEyeColor %>%
margin.table(1) %>%
prop.table() %>%
round(2)
## Hair
## Black Brown Red Blond
## 0.18 0.48 0.12 0.21
Marginal for eye:
HairEyeColor %>%
margin.table(2) %>%
prop.table() %>%
round(2)
## Eye
## Brown Blue Hazel Green
## 0.37 0.36 0.16 0.11
Conditional distribution given eye:
HairEyeColor %>%
margin.table(c(1,2)) %>%
prop.table(2) %>%
round(2)
## Eye
## Hair Brown Blue Hazel Green
## Black 0.31 0.09 0.16 0.08
## Brown 0.54 0.39 0.58 0.45
## Red 0.12 0.08 0.15 0.22
## Blond 0.03 0.44 0.11 0.25
Conditional distribution given hair:
HairEyeColor %>%
margin.table(c(1,2)) %>%
prop.table(1) %>%
round(2)
## Eye
## Hair Brown Blue Hazel Green
## Black 0.63 0.19 0.14 0.05
## Brown 0.42 0.29 0.19 0.10
## Red 0.37 0.24 0.20 0.20
## Blond 0.06 0.74 0.08 0.13