简体   繁体   English

R中比例差异的置信区间

[英]Confidence interval for proportion differences in R

I am looking for a quick method (without creating too many new dataframes) for the following: 我正在寻找一种快速的方法(不创建太多新的数据框)用于以下操作:

Imagine I have two variables: data$occupation (rows, from top to bottom, "1" to "4"), and data$disease (columns, from left to right, "yes" and "no") with the following data: 想象一下,我有两个变量: data$occupation (行,从上到下,从“ 1”到“ 4”),以及data$disease (列,从左到右,“是”和“否”),其中包含以下数据:

mat1<-matrix(c(54,23,28,45,16,10,17,13), 4,2)

I would like to end up with a table with proportions of "yes" in the different categories of "occupation", percentage difference in proportions between occupations and the confidence interval of this difference: 我想得出一个表格,其中包含“职业”不同类别中“是”的比例,职业之间的比例差异百分比和该差异的置信区间:

with prop.test(table(data$occupation, data$disease), correct=FALSE) , I get the different proportions, but now I would like to find a commando which is giving the difference between proportions (with I reference I can enter) with related CI. 使用prop.test(table(data$occupation, data$disease), correct=FALSE) ,我得到了不同的比例,但是现在我想找到一个突击队,它给出了比例之间的差异(参考我可以输入)和相关CI。

Something like twoby2() (which gives OR and RR), would be nice. twoby2() (给出OR和RR)之类的东西会很好。

I'm a newbie in statistics, however, with regards to this posting , I'd try it like this 我是统计方面的新手,但是关于此发布 ,我会尝试这样

tab <- table(data$occupation, data$disease)
combinations <- t(combn(nrow(tab), 2))
cbind(combinations, t(apply(combinations, 1, function(rows) {
  re <- prop.test(x=tab[rows, 1], n=rep(nrow(data), 2), correct=F)
  re$estimate <- unname(re$estimate)
  return(c(
    propY1 = re$estimate[1], 
    propY2 = re$estimate[2],
    diff = re$estimate[1]-re$estimate[2], 
    l = re$conf.int[1],
    u = re$conf.int[2]
  ))
})))
#             propY1    propY2        diff           l           u
# [1,] 1 2 0.2621359 0.1116505  0.15048544  0.07661756  0.22435331
# [2,] 1 3 0.2621359 0.1359223  0.12621359  0.05007540  0.20235179
# [3,] 1 4 0.2621359 0.2184466  0.04368932 -0.03871570  0.12609434
# [4,] 2 3 0.1116505 0.1359223 -0.02427184 -0.08783067  0.03928698
# [5,] 2 4 0.1116505 0.2184466 -0.10679612 -0.17774178 -0.03585045
# [6,] 3 4 0.1359223 0.2184466 -0.08252427 -0.15583081 -0.00921773

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM