在 R 中多次运行“prop.test" [英] Running 'prop.test' multiple times in R

查看:17
本文介绍了在 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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆