# r - 数组操作：计算3向表中图层的优势比

Mice <-
structure(list(litter = c(7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L,
11L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 7L, 7L, 8L,
8L, 9L, 9L, 10L, 10L, 11L, 11L), treatment = structure(c(1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("A",
"B"), class = "factor"), deaths = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("0", "1",
"2 "), class = "factor"), Freq = c(58L, 75L, 49L, 58L, 33L, 45L,
15L, 39L, 4L, 5L, 11L, 19L, 14L, 17L, 18L, 22L, 13L, 22L, 12L,
15L, 5L, 7L, 10L, 8L, 15L, 10L, 15L, 18L, 17L, 8L)), .Names = c("litter",
"treatment", "deaths", "Freq"), row.names = c(NA, 30L), class = "data.frame")

mice.tab <- xtabs(Freq ~ litter   treatment   deaths, data=Mice)
ftable(mice.tab)

deaths  0  1 2
litter treatment
7      A                58 11  5
B                75 19  7
8      A                49 14 10
B                58 17  8
9      A                33 18 15
B                45 22 10
10     A                15 13 15
B                39 22 18
11     A                 4 12 17
B                 5 15  8
>

odds1 <- log(mice.tab[,,1]/mice.tab[,,2])  # contrast 0:1
odds2 <- log(mice.tab[,,2]/mice.tab[,,3])  # contrast 1:2

odds1
treatment
litter          A          B
7   1.6625477  1.3730491
8   1.2527630  1.2272297
9   0.6061358  0.7156200
10  0.1431008  0.5725192
11 -1.0986123 -1.0986123
>

log(T) %*% matrix(c(1, -1, 0,
0,  1, -1))

0条回复