[R] creating 3-way tables for mantelhaen.test

P Ehlers ehlers at math.ucalgary.ca
Fri Feb 17 10:50:11 CET 2006


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:

> 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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list