R中卡方的置信区间

idv4meu8  于 2023-03-10  发布在  其他
关注(0)|答案(3)|浏览(365)

如何计算R中卡方的置信区间。是否有类似chisq.test()的函数,

h43kikqp

h43kikqp1#

卡方检验没有置信区间(你只是检查第一个分类变量和第二个分类变量是否独立),但是你可以为比例差异计算置信区间,就像这样。
假设您有一些数据,其中第一组中有30%的人报告成功,而第二组中有70%的人报告成功:

row1 <- c(70,30)
row2 <- c(30,70)
my.table <- rbind(row1,row2)

现在列联表中有了数据:

> my.table
     [,1] [,2]
row1   70   30
row2   30   70

你可以在上面运行chisq.test,很明显这两个比例是显著不同的,所以分类变量必须是独立的:

> chisq.test(my.table)

    Pearson's Chi-squared test with Yates' continuity correction

data:  my.table
X-squared = 30.42, df = 1, p-value = 3.479e-08

如果你做prop.test,你会发现你有95%的置信度,比例之间的差异在26.29%和53.70%之间,这是有意义的,因为两个观察到的比例之间的实际差异是70%-30%=40%:

> prop.test(x=c(70,30),n=c(100,100))

    2-sample test for equality of proportions with continuity correction

data:  c(70, 30) out of c(100, 100)
X-squared = 30.42, df = 1, p-value = 3.479e-08
alternative hypothesis: two.sided
95 percent confidence interval:
 0.2629798 0.5370202
sample estimates:
prop 1 prop 2 
   0.7    0.3
7xllpg7q

7xllpg7q2#

对@ mysterous“漂亮回答的补充:如果您有2x2列联矩阵,则可以使用fisher.test而不是prop.test来检验比例的 * 比率 * 的差异,而不是比率的 * 差异 *。在Fisher精确检验中,零假设对应于比值比(OR)= 1。
使用@ mysterRious '示例数据

ft <- fisher.test(my.table)
ft
#
#   Fisher's Exact Test for Count Data
#
#data:  my.table
#p-value = 2.31e-08
#alternative hypothesis: true odds ratio is not equal to 1
#95 percent confidence interval:
#  2.851947 10.440153
#sample estimates:
#odds ratio
#  5.392849

OR的置信区间以fit$conf.int表示

ft$conf.int
#[1]  2.851947 10.440153
#attr(,"conf.level")
#[1] 0.95

为了确认,我们手动计算OR

OR <- Reduce("/", my.table[, 1] / my.table[, 2])
OR
#[1] 5.444444
xnifntxz

xnifntxz3#

据我所知,当2X2表中有一个或多个小于5的值时,就会使用fisher.test,但是你在上面的例子中没有做这个限制,为什么它可以使用fisher.test呢?
还有,OR的置信区间是不是只有使用fisher.test才可用?有没有其他方法来计算置信区间?因为感觉我在我的情况下只能使用chisq.test。
非常感谢

相关问题