[英]How can I cross all variables against each other and gather Chi Square test values in R?
I would like to set all the 40 categorical variables in my datafile against each other (= 160 crosstabs) and gather the p-values of all the Chi-Square tests, preferably in one list, in order to see which variables are most closely related.我想将数据文件中的所有 40 个分类变量相互设置(= 160 个交叉表)并收集所有卡方检验的 p 值,最好在一个列表中,以便查看哪些变量最密切相关.
Is there an R code to execute this request in a simple way?是否有 R 代码以简单的方式执行此请求?
You can use comb
function to find all combinations and run any number of variables against each other.您可以使用
comb
函数来查找所有组合并相互运行任意数量的变量。
As a simple solution, if you have a data.table
named dt
, and the independent variable is result
, then use the following code.作为一个简单的解决方案,如果您有一个名为
dt
的data.table
,并且自变量是result
,则使用以下代码。
library(data.table)
library(magrittr)
library(dplyr)
chi_dt <- dt %>%
map(~chisq.test(.x, dt$result)) %>%
tibble(names = names(.), data = .) %>%
mutate(stats = map(data, broom::tidy)) %>%
unnest(stats) %>% select(-data) %>%
arrange(p.value, desc(statistic))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.