首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >dplyr中分组数据的Chi -Square测试

dplyr中分组数据的Chi -Square测试
EN

Stack Overflow用户
提问于 2018-03-16 12:04:25
回答 1查看 6.7K关注 0票数 1

我在总结如下data.frame时遇到了困难:

代码语言:javascript
复制
db <- data.frame(ID = c(rep(1, 3), rep(2,4), rep(3, 2), 4),
             Gender = factor(c(rep("woman", 7), rep("man", 2), "woman")),
             Grade = c(rep(3, 3), rep(1, 4), rep(2, 2), 1),
             Drug = c(1, 2, 2, 1, 2, 6, 9, 8, 5, 1),
             Group = c(rep(1, 3), rep(2,4), rep(1, 2), 2))
db

#    ID Gender Grade Drug Group
# 1   1  woman     3    1     1
# 2   1  woman     3    2     1
# 3   1  woman     3    2     1
# 4   2  woman     1    1     2
# 5   2  woman     1    2     2
# 6   2  woman     1    6     2
# 7   2  woman     1    9     2
# 8   3    man     2    8     1
# 9   3    man     2    5     1
# 10  4  woman     1    1     2

理想情况下,我每个观察都有一行,但是由于Drugs随时间的不同,所以我最终会有很多重复的行。这使我很难进行分析。

我的最终目标是构建一个汇总表,正如在另一篇文章:Using dplyr to create summary proportion table with several categorical/factor variables中已经讨论过的那样。就像这样:

变量组1=第2组:差组1/2

性别 ................................| .........................p =1

男的..。_

女性。\x{e76f}.1.

但是,由于这个帖子只得到了部分回答,并且不直接适用于我的问题(主要是由于重复的行),如果能够单独执行汇总统计,我已经很高兴了。在这篇文章中:How to get the frequency from grouped data with dplyr?,我问了如何从观测中获得独特的/不同的频率。现在,我需要找出性别在两组之间的分布是否有统计学上的显著差异。

根据ID,我知道有四种观察,其中三种是女性,一种是男性。因此,所期望的结果可以这样计算:

代码语言:javascript
复制
gen <- factor(c("woman", "woman", "man", "woman"))
gr <- c(1, 2 ,1 ,2)
chisq.test(gen, gr)

#   Pearson's Chi-squared test with Yates' continuity correction
# 
# data:  gen and gr
# X-squared = 0, df = 1, p-value = 1
#
# Warning message:
# In chisq.test(gen, gr) : Chi-squared approximation may be incorrect

如何使用data.frame dplyr**?**从中计算p值?

我失败的方法是:

代码语言:javascript
复制
db %>% 
  group_by(ID) %>% 
  distinct(ID, Gender, Group) %>% 
  summarise_all(funs(chisq.test(db$Gender, 
                               db$Group)$p.value))
# A tibble: 4 x 3
#      ID Gender Group
#  <dbl>  <dbl> <dbl>
# 1    1.  0.429 0.429
# 2    2.  0.429 0.429
# 3    3.  0.429 0.429
# 4    4.  0.429 0.429
# Warning messages:
# 1: In chisq.test(db$Gender, db$Group) :
#   Chi-squared approximation may be incorrect
# 2: In chisq.test(db$Gender, db$Group) :
#   Chi-squared approximation may be incorrect
# 3: In chisq.test(db$Gender, db$Group) :
#  Chi-squared approximation may be incorrect
# 4: In chisq.test(db$Gender, db$Group) :
#  Chi-squared approximation may be incorrect
# 5: In chisq.test(db$Gender, db$Group) :
#   Chi-squared approximation may be incorrect
# 6: In chisq.test(db$Gender, db$Group) :
#  Chi-squared approximation may be incorrect
# 7: In chisq.test(db$Gender, db$Group) :
#  Chi-squared approximation may be incorrect
# 8: In chisq.test(db$Gender, db$Group) :
#  Chi-squared approximation may be incorrect
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-16 12:10:00

我们可以ungroup,然后用summarise获取pvalue

代码语言:javascript
复制
db %>% 
  group_by(ID) %>% 
  distinct(ID, Gender, Group) %>%
  ungroup %>%
  summarise(pval = chisq.test(Gender, Group)$p.value)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49320347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档