Array manipulation: calculating odds ratios for a layer in a 3-sided table

This is a question of manipulating and calculating array and data in the context of the models for the log coefficients in the contingency tables. The closest question I have found is How can I calculate odds ratio in many tables , but mine is more general.

I have a data frame representing a 3-way frequency table of size 5 (garbage) x 2 (cure) x 3 (death). "Frequency" is the frequency in each cell and mortality is the response variable.

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")

      

From this I want to calculate the log coefficients for the adjacent categories of the last variable (death) and have this value in the dataframe with litter factors (5), processing (2) and contrast (2) as described below.

The data can be seen in the form xtabs ():

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
> 

      

From this I want to calculate the (adjacent) log coefficients of 0 versus 1 and 1 versus 2 + deaths, which is easy into an array,

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
> 

      

But, for analysis, I want to have them in the dataframe, with garbage, treatment and contrast factors, and the "logodds" column containing the entries in the odds1 and odds2 tables, stripped out accordingly.

Generally for a table I x J x K where the last factor is the answer, my desired output is an IJ (K-1) row data frame with adjacent log codes in the "logodds" column, and ideally I would like to execute general function.

Note that if T is a 10 x 3 frequency matrix shown by ftable (), the computation is essentially

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

      

followed by change and labeling.

Can anyone help with this?

+3


source to share





All Articles