我试图在R中运行Mantel-Haenszel分析,以确定在考虑组内的“诊断”比率时,比例测试的比较是否仍然很重要。此测试可在统计数据包中找到。
library(stats)
mantelhaen.test(x)
完成了一些阅读后,我发现这个测试可以在 n x n x k 的列联表上执行优势比测试,而不是简单地 n x n 。但是,我无法以正确的方式安排我的数据,因为我对R很新。我创建了一些示例数据......
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)
看起来像这样......
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 -
我最初能够使用一个简单的N-1卡方来运行比较测试+到 - 仅针对A和B,但现在我希望能够考虑{{1}也是。我将在这里显示我想要查看的图表,这基本上是比较每列中比率的显着性。我能够做到这一点,但我现在想要能够解释ex.diag。
我尝试使用ex.diag
函数以可行的方式排列数据。
ftable()
看起来像这样......
ex.ftable <- ftable(ex.data)
但是,当我运行 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
时,我收到错误mantelhaen.test(ex.ftable)
。如何以可以实际运行此测试的方式排列数据?
答案 0 :(得分:1)
在mantelhaen.test
中,三维列联表x
的最后一个维度需要是分层变量(ex.diag
)。该矩阵可以如下生成:
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
可以使用aperm
生成3D矩阵。
# 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
现在可以进行Cochran-Mantel-Haenszel卡方检验。
# Cochran-Mantel-Haenszel chi-squared test of the null that
# two nominal variables are conditionally independent in each stratum
#
mantelhaen.test(mtx3D, exact=FALSE)
测试结果是
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
鉴于案例数量较少,最好计算一个精确的条件测试(选项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