本文介绍了在R中多次运行"prop.test"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据显示了很长的地区列表,每个地区的人口以及每个地区患有某种疾病的人数.我正在尝试显示每个比例的置信区间(但我没有测试比例在统计上是否不同).

I have some data showing a long list of regions, the population of each region and the number of people in each region with a certain disease. I'm trying to show the confidence intervals for each proportion (but I'm not testing whether the proportions are statistically different).

一种方法是手动计算标准误差和置信区间,但我想使用诸如prop.test之类的内置工具,因为它具有一些有用的选项.但是,当我将prop.test与向量一起使用时,它将对所有比例进行卡方检验.

One approach is to manually calculate the standard errors and confidence intervals but I'd like to use a built-in tool like prop.test, because it has some useful options. However, when I use prop.test with vectors, it runs a chi-square test across all the proportions.

我已经用while循环解决了这个问题(请参阅下面的虚拟数据),但是我认为必须有一种更好,更简单的方法来解决此问题.将在这里申请工作,以及如何申请?谢谢!

I've solved this with a while loop (see dummy data below), but I sense there must be a better and simpler way to approach this problem. Would apply work here, and how? Thanks!

dat <- data.frame(1:5, c(10, 50, 20, 30, 35))
names(dat) <- c("X", "N")
dat$Prop <- dat$X / dat$N

ConfLower = 0
x = 1
while (x < 6) {
    a <- prop.test(dat$X[x], dat$N[x])$conf.int[1]
    ConfLower <- c(ConfLower, a)
    x <- x + 1
}

ConfUpper = 0
x = 1
while (x < 6) {
    a <- prop.test(dat$X[x], dat$N[x])$conf.int[2]
    ConfUpper <- c(ConfUpper, a)
    x <- x + 1
}

dat$ConfLower <- ConfLower[2:6]
dat$ConfUpper <- ConfUpper[2:6] 

推荐答案

这是尝试使用Map的方法,基本上是从先前的答案在这里被窃取的:
https://stackoverflow.com/a/15059327/496803

Here's an attempt using Map, essentially stolen from a previous answer here:
https://stackoverflow.com/a/15059327/496803

res <- Map(prop.test,dat$X,dat$N)
dat[c("lower","upper")] <- t(sapply(res,"[[","conf.int"))

#  X  N      Prop       lower     upper
#1 1 10 0.1000000 0.005242302 0.4588460
#2 2 50 0.0400000 0.006958623 0.1485882
#3 3 20 0.1500000 0.039566272 0.3886251
#4 4 30 0.1333333 0.043597084 0.3164238
#5 5 35 0.1428571 0.053814457 0.3104216

这篇关于在R中多次运行"prop.test"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 16:05