20.28 经典案例

20.28.1 1973 年加州大学伯克利分校的学生招生

录取人数按院系和性别分类统计,研究目标是各个院系在录取学生的时候是否有性别歧视?统计数据见表 20.1

as.data.frame(UCBAdmissions) %>%
  reshape(.,
    v.names = "Freq", idvar = c("Admit", "Gender"),
    timevar = "Dept", direction = "wide", sep = ""
  ) %>%
  knitr::kable(.,
    caption = "伯克利大学各个院系的录取人数",
    row.names = FALSE, col.names = gsub("(Freq)", "Dept", names(.)),
    align = "c"
  )
表 20.1: 伯克利大学各个院系的录取人数
Admit Gender DeptA DeptB DeptC DeptD DeptE DeptF
Admitted Male 512 353 120 138 53 22
Rejected Male 313 207 205 279 138 351
Admitted Female 89 17 202 131 94 24
Rejected Female 19 8 391 244 299 317
# plot(UCBAdmissions, col = "lightblue", border = "white")
library(ggmosaic)
ggplot(data = as.data.frame(UCBAdmissions)) +
  geom_mosaic(aes(weight = Freq, x = product(Gender, Admit), fill = Dept)) +
  coord_flip() +
  theme_minimal() +
  labs(x = "Admit", y = "Gender")
UCBAdmissions 马赛克图

图 20.4: UCBAdmissions 马赛克图

20.28.2 1976~1977年美国佛罗里达州的凶杀案件中被告肤色和死刑判决的关系

被告 被害人 判死 不判死
白人 白人 19 132
黑人 0 9
黑人 白人 11 32
黑人 6 97

20.28.3 统计专业学生的头发和眼睛的颜色

HairEyeColor 是一个 table 类型的数据对象,和数组的关系 array

class(HairEyeColor)
## [1] "table"
str(HairEyeColor)
##  'table' num [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"
apply(HairEyeColor, c(1, 2), sum)
##        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
# plot(HairEyeColor, col = "lightblue", border = "white")
library(ggmosaic)
ggplot(data = as.data.frame(HairEyeColor)) +
  geom_mosaic(aes(weight = Freq, x = product(Hair, Eye), fill = Sex)) +
  theme_minimal() +
  labs(x = "Hair", y = "Eye")
头发、眼睛颜色和性别的比例

图 20.5: 头发、眼睛颜色和性别的比例