【问题标题】:Chi -Square test with grouped data in dplyr使用 dplyr 中的分组数据进行卡方检验
【发布时间】:2018-03-16 12:04:25
【问题描述】:

我很难总结一个看起来像这样的data.frame

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 |
|男...... |............1 | ....0 | ..................................|
|女性。 |………………………………1 |……………………………………………………………………2 ....|

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


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

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

如何使用 dplyr 从我的 data.frame 计算 p-vale?


我失败的方法是:

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

【问题讨论】:

    标签: r dplyr chi-squared


    【解决方案1】:

    我们可以ungroup 然后得到pvaluesummarise

    db %>% 
      group_by(ID) %>% 
      distinct(ID, Gender, Group) %>%
      ungroup %>%
      summarise(pval = chisq.test(Gender, Group)$p.value)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多