r - 计算跨另一个分类变量的两个变量的置信区间的平均差
问题描述
拜托,我想计算另一个分类变量中两个变量的置信区间的平均差。
我有兴趣计算 p1、p2 和 pdiff 的置信区间
非常感谢
library(tidyverse)
iris %>%
mutate(out1 = Sepal.Length < 6,
out2 = Sepal.Length < 5) %>%
group_by(Species) %>%
summarise(p1 = mean(out1),
p2 = mean(out2),
pdiff = p1 - p2)
# A tibble: 3 x 4
Species p1 p2 pdiff
<fct> <dbl> <dbl> <dbl>
1 setosa 1 0.4 0.6
2 versicolor 0.52 0.02 0.5
3 virginica 0.14 0.02 0.12
解决方案
获得置信区间的一种方法是通过prop.test
. 您可以为您的每一个指标(p1
、p2
、diff
)运行此测试,然后提取您想要使用的信息map
。
library(tidyverse)
iris %>%
mutate(out1 = Sepal.Length < 6,
out2 = Sepal.Length < 5) %>%
group_by(Species) %>%
summarise(p1 = mean(out1),
p2 = mean(out2),
pdiff = p1 - p2,
p1_test = list(prop.test(sum(out1), length(out1))), # create tests for p1, p2 and diff and save the outputs as list
p2_test = list(prop.test(sum(out2), length(out2))),
pdiff_test = list(prop.test(c(sum(out1),sum(out2)), c(length(out1),length(out2)))),
p1_low = map_dbl(p1_test, ~.$conf.int[1]), # extract low and high confidence intervals based on the corresponding test
p1_high = map_dbl(p1_test, ~.$conf.int[2]),
p2_low = map_dbl(p2_test, ~.$conf.int[1]),
p2_high = map_dbl(p2_test, ~.$conf.int[2]),
pdiff_low = map_dbl(pdiff_test, ~.$conf.int[1]),
pdiff_high = map_dbl(pdiff_test, ~.$conf.int[2])) %>%
select(-matches("test")) # remove test columns
# # A tibble: 3 x 10
# Species p1 p2 pdiff p1_low p1_high p2_low p2_high pdiff_low pdiff_high
# <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 setosa 1 0.4 0.6 0.911 1 0.267 0.548 0.444 0.756
# 2 versicolor 0.52 0.02 0.5 0.376 0.661 0.00104 0.120 0.336 0.664
# 3 virginica 0.14 0.02 0.12 0.0628 0.274 0.00104 0.120 -0.00371 0.244
推荐阅读
- javascript - 如何在Javascript中获得承诺后的价值?
- angular - 组件被销毁后,可观察订阅保持活动状态
- glsl - 循环运行 glDispatchCompute
- amazon-web-services - Terraform 无法承担启用 MFA 的角色
- python-3.x - cifar10:在 keras 中实现模型,但获得的准确度与文章不同
- tableau-api - 从 Tableau Desktop 将仪表板保存到 Tableau Reader 时隐藏选项卡?
- javascript - 从列表中删除记录,Apollo GraphQL Optimistic UI Mutation
- javascript - Javascript根据标签从选择选项中获取值
- python-3.x - 在 Python 中将频谱图存储为图像
- r - ggplot2中整数的轴排序