作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个由“年份”和“认知障碍”组成的数据框(1=是,0=否则)
我想比较每年的比例。因此,2000 年将是:
df %>%
filter(year == 2000) %>%
{prop.test(rev(table(.$cogimp)),p = 0.5, conf.level=0.95)}
prop.test(x = 3, n = 30, p = 0.5, conf.level=0.95)
df <- structure(list(year = c(2000, 2000, 2015, 2015, 2000, 2015, 2000,
2000, 2000, 2000, 2015, 2006, 2015, 2015, 2010, 2006, 2006, 2010,
2000, 2006, 2015, 2006, 2015, 2015, 2000, 2015, 2000, 2015, 2015,
2010, 2015, 2015, 2015, 2000, 2006, 2006, 2006, 2015, 2015, 2006,
2015, 2010, 2000, 2000, 2010, 2006, 2010, 2010, 2015, 2000, 2015,
2006, 2000, 2006, 2015, 2006, 2000, 2010, 2010, 2010, 2015, 2006,
2015, 2000, 2015, 2010, 2010, 2010, 2010, 2000, 2000, 2000, 2006,
2015, 2015, 2000, 2000, 2000, 2015, 2006, 2006, 2010, 2006, 2000,
2010, 2000, 2015, 2015, 2015, 2015, 2010, 2000, 2000, 2010, 2006,
2010, 2010, 2000, 2000, 2000), cogimp = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -100L), class = c("tbl_df",
"tbl", "data.frame"))
df %>%
count(year, cogimp)
df %>%
filter(year == 2006) %>%
{prop.test(rev(table(.$cogimp)),p = 0.5, conf.level=0.95)}
prop.test(x = 3, n = 30, p = 0.5, conf.level=0.95)
prop.test(x = 2, n = 19, p = 0.5, conf.level=0.95)
最佳答案
使用 tidy
从扫帚包。改编自 https://stackoverflow.com/a/30015869/13157536
library(dplyr)
library(broom)
df <- structure(list(year = c(2000, 2000, 2015, 2015, 2000, 2015, 2000,
2000, 2000, 2000, 2015, 2006, 2015, 2015, 2010, 2006, 2006, 2010,
2000, 2006, 2015, 2006, 2015, 2015, 2000, 2015, 2000, 2015, 2015,
2010, 2015, 2015, 2015, 2000, 2006, 2006, 2006, 2015, 2015, 2006,
2015, 2010, 2000, 2000, 2010, 2006, 2010, 2010, 2015, 2000, 2015,
2006, 2000, 2006, 2015, 2006, 2000, 2010, 2010, 2010, 2015, 2006,
2015, 2000, 2015, 2010, 2010, 2010, 2010, 2000, 2000, 2000, 2006,
2015, 2015, 2000, 2000, 2000, 2015, 2006, 2006, 2010, 2006, 2000,
2010, 2000, 2015, 2015, 2015, 2015, 2010, 2000, 2000, 2010, 2006,
2010, 2010, 2000, 2000, 2000), cogimp = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
1, 1, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -100L), class = c("tbl_df",
"tbl", "data.frame"))
df_test <- df %>%
group_by(year) %>%
summarize(cogimp = sum(cogimp), n = n()) %>%
group_by(year, cogimp, n) %>%
do(fitYear = prop.test(.$cogimp, .$n, p = 0.5, conf.level = 0.95))
tidy(df_test, fitYear) %>%
select(year, cogimp, n, p.value)
#> # A tibble: 4 x 4
#> # Groups: year, cogimp, n [4]
#> year cogimp n p.value
#> <dbl> <dbl> <int> <dbl>
#> 1 2000 3 30 0.0000268
#> 2 2006 2 19 0.00132
#> 3 2010 8 20 0.502
#> 4 2015 3 31 0.0000163
关于r - Purrr(或扫帚)用于计算分组数据集的比例测试(多比例测试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61063396/
我是一名优秀的程序员,十分优秀!