Skip to content

creating 3-way tables for mantelhaen.test

3 messages · Taka Matzmoto, Jacques VESLOT, Peter Ehlers

#
Hi R users
I have serveral binary variables (e.g., X1, X2, X3, X4, X5, X,6, and X7) and 
one continuous variable (e.g., Y1).

I combined these variables using data.frame()

mydata <- data.frame(X1,X2,X3,X4,X5,X6,X7,Y1)

after that, I sorted this data.frame

rank.by.Y1<-order(mydata[,8])
sorted.mydata<-mydata[rank.by.Y1,]

after that, I replaced Y1's values with values ranging from 1 to 10 ( 1 
represents the lowest group on Y1 and 10 presents the hight group on Y1). 
Now Y1 becomes a grouping variable.

What I like to do is to apply mantelhaen.test for each binary variable pair 
(e.g, X1 and X2, X1 and X3, X1 and X4, .... , X6 and X7)

In order to apply mantelhaen.test, a 3-dimensional contingency table is 
required.

Could you provide some advice on how to create a 3-dimensional contingency 
table (first dimension represents the first variable of  variable pair, 
second dimension the second variable of  variable  pair, and third dimension 
represents 1 to 10 ) and apply mantelhaen.test ?

I looked at arrary, xtabs, table commands but I couldn't figure out yet.

Thank you
#
library(gtools)
index <- cbind(combinations(7,2),8)
lapply(as.data.frame(t(index)), function(x) 
mantelhaen.test(table(mydata[,x])))


Taka Matzmoto a ??crit :
#
Taka,

Maybe you did something strange when you "replaced Y1's values ....".
Is the result a 10-level factor?
The following works for me.

x1 <- sample(c("y", "n"), 100, replace = TRUE)
x2 <- sample(c("a", "b"), 100, replace = TRUE)
y  <- sample(1:10, 100, replace = TRUE)
y  <- factor(y)

dat <- data.frame(x1, x2, y)
dat.xt <- xtabs(~ x1 + x2 + y, data = dat)
mantelhaen.test(dat.xt)

Peter Ehlers
Taka Matzmoto wrote: