tidyverse:适用于所有列组合的“卡方" [英] tidyverse: Chi Square for all combinations of columns

查看:60
本文介绍了tidyverse:适用于所有列组合的“卡方"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码对所有可能的列组合进行卡方分析.

I used the following code to do the Chi Square Analysis for all possible combinations of columns.

Dat <- esoph[ , 1:3]

library(plyr)

combos <- combn(ncol(Dat),2)

adply(combos, 2, function(x) {
  test <- chisq.test(Dat[, x[1]], Dat[, x[2]])

  out <- data.frame("Row" = colnames(Dat)[x[1]]
                    , "Column" = colnames(Dat[x[2]])
                    , "Chi.Square" = round(test$statistic,3)
                    ,  "df"= test$parameter
                    ,  "p.value" = round(test$p.value, 3)
  )
  return(out)

})  

  X1   Row Column Chi.Square df p.value
1  1 agegp  alcgp      1.419 15       1
2  2 agegp  tobgp      2.400 15       1
3  3 alcgp  tobgp      0.619  9       1

我想知道如何使用 tidyverse 执行相同的操作.任何提示.

I wonder how the same can be performed with tidyverse. Any hints.

推荐答案

Dat <- esoph[, 1:3]

library(tidyverse)
library(broom)

data.frame(t(combn(names(Dat),2)), stringsAsFactors = F) %>%
  mutate(d = map2(X1, X2, ~tidy(chisq.test(Dat[,.x], Dat[,.y])))) %>%
  unnest()

#      X1    X2 statistic   p.value parameter                     method
# 1 agegp alcgp 1.4189096 0.9999971        15 Pearson's Chi-squared test
# 2 agegp tobgp 2.4000000 0.9999022        15 Pearson's Chi-squared test
# 3 alcgp tobgp 0.6194617 0.9999240         9 Pearson's Chi-squared test

这篇关于tidyverse:适用于所有列组合的“卡方"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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