2

I am attempting to run a Mantel-Haenszel analysis in R to determine whether or not a comparison of proportions test is still significant when accounting for a 'diagnosis' ratio within groups. This test is available in the stats package.

library(stats)
mantelhaen.test(x)

Having done some reading, I've found that this test can perform an odds ratio test on a contingency table that is n x n x k, as opposed to simply n x n. However, I am having trouble arranging my data in the proper way, as I am fairly new to R. I have created some example data...

ex.label <- c("A","A","A","A","A","A","A","B","B","B")
ex.status  <- c("+","+","-","+","-","-","-","+","+","-")
ex.diag <- c("X","X","Z","Y","Y","Y","X","Y","Z","Z")
ex.data <- data.frame(ex.label,ex.diag,ex.status)

Which looks like this...

  ex.label ex.diag ex.status
1         A       X         +
2         A       X         +
3         A       Z         -
4         A       Y         +
5         A       Y         -
6         A       Y         -
7         A       X         -
8         B       Y         +
9         B       Z         +
10        B       Z         -

I was originally able to use a simple N-1 chi-square to run a comparison of proportions test of + to - for only the A and B, but now I want to be able to account for the ex.diag as well. I'll show a graph here for what I wanted to be looking at, which is basically to compare the significance of the ratio in each column. I was able to do this, but I now want to be able to account for ex.diag. Graph

I tried to use the ftable() function to arrange my data in a way that would work.

ex.ftable <- ftable(ex.data)

Which looks like this...

                 ex.status - +
ex.label ex.diag              
A        X                 1 2
         Y                 2 1
         Z                 1 0
B        X                 0 0
         Y                 0 1
         Z                 1 1

However, when I run mantelhaen.test(ex.ftable), I get the error 'x' must be a 3-dimensional array. How can I arrange my data in such a way that I can actually run this test?

David
  • 259
  • 1
  • 2
  • 8

1 Answers1

2

In mantelhaen.test the last dimension of the 3-dimensional contingency table x needs to be the stratification variable (ex.diag). This matrix can be generated as follows:

ex.label <- c("A","A","A","A","A","A","A","B","B","B")
ex.status  <- c("+","+","-","+","-","-","-","+","+","-")
ex.diag <- c("X","X","Z","Y","Y","Y","X","Y","Z","Z")

# Now ex.diag is in the first column
ex.data <- data.frame(ex.diag, ex.label, ex.status)

# The flat table
( ex.ftable <- ftable(ex.data) )

#                  ex.status - +
# ex.diag ex.label              
# X       A                  1 2
#         B                  0 0
# Y       A                  2 1
#         B                  0 1
# Z       A                  1 0
#         B                  1 1

The 3D matrix can be generated using aperm.

# Trasform the ftable into a 2 x 2 x 3 array
# First dimension: ex.label
# Second dimension: ex.status
# Third dimension: ex.diag
( mtx3D <- aperm(array(t(as.matrix(ex.ftable)),c(2,2,3)),c(2,1,3)) )

#     , , 1
# 
#      [,1] [,2]
# [1,]    1    2
# [2,]    0    0
# 
# , , 2
# 
#      [,1] [,2]
# [1,]    2    1
# [2,]    0    1
# 
# , , 3
# 
#      [,1] [,2]
# [1,]    1    0
# [2,]    1    1

Now the Cochran-Mantel-Haenszel chi-squared test can be performed.

# Cochran-Mantel-Haenszel chi-squared test of the null that 
# two nominal variables are conditionally independent in each stratum
#
mantelhaen.test(mtx3D, exact=FALSE)

The results of the test is

        Mantel-Haenszel chi-squared test with continuity correction

data:  mtx3D
Mantel-Haenszel X-squared = 0.23529, df = 1, p-value = 0.6276
alternative hypothesis: true common odds ratio is not equal to 1
95 percent confidence interval:
 NaN NaN
sample estimates:
common odds ratio 
              Inf    

Given the low number of cases, it is preferable to compute an exact conditional test (option exact=TRUE).

mantelhaen.test(mtx3D, exact=T)

#         Exact conditional test of independence in 2 x 2 x k tables
# 
# data:  mtx3D
# S = 4, p-value = 0.5
# alternative hypothesis: true common odds ratio is not equal to 1
# 95 percent confidence interval:
#  0.1340796       Inf
# sample estimates:
# common odds ratio 
#               Inf
Marco Sandri
  • 23,289
  • 7
  • 54
  • 58