简体   繁体   中英

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:

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.

Something like twoby2() (which gives OR and RR), would be nice.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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