在 R 中对 5 个以上的变量应用 Chi-Squared 测试并找到 p-values

Apply Chi-Squared Test in R on more than 5 variables and find the p-values

我是 Chi-Squared 测试的新手。我有一个包含大量分类变量的数据库。

具有少量变量的示例数据库是:

我想在 R 中应用 CHi-Squared 测试,并想找到所有这些分类变量的 p-values。基于此我将对我的变量进行排名并删除最不重要的变量。

你能告诉我如何在 R 中找到上述所有变量的 p-values 吗?

据我所知,Chi-Square 只能应用于 2 个分类变量,但我有很多分类变量。怎么做到的?

您可以使用lapply做重复的任务,这里是对数据框的多列与第一列的卡方检验。

CHIS <- lapply(data[,-1], function(x) chisq.test(data[,1], x)); CHIS

结果是一个列表,可以使用 do.callrbind.

将其组合成更好看的格式
do.call(rbind, CHIS)[,c(1,3)]
   statistic    parameter p.value  
X1 0.08680556   1         0.7682782
X2 0.9695384    1         0.3247953
X3 9.464545e-31 1         1        
X4 0.9695384    1         0.3247953
X5 0.78125      1         0.3767591

或者使用 broom 中的 tidy 函数。

library(broom)

do.call(rbind, lapply(CHIS, tidy))

# A tibble: 5 x 4
  statistic p.value parameter method                                                      
*     <dbl>   <dbl>     <int> <chr>                                                       
1  8.68e- 2   0.768         1 Pearson's Chi-squared test with Yates' continuity correction
2  9.70e- 1   0.325         1 Pearson's Chi-squared test with Yates' continuity correction
3  9.46e-31   1.00          1 Pearson's Chi-squared test with Yates' continuity correction
4  9.70e- 1   0.325         1 Pearson's Chi-squared test with Yates' continuity correction
5  7.81e- 1   0.377         1 Pearson's Chi-squared test with Yates' continuity correction

但不幸的是名字消失了。 data.table 中的 rbindlist 函数有一个可选的 idcol 参数来保留原始列表中的名称。

library(data.table)
rbindlist(lapply(CHIS, tidy), idcol=TRUE)

   .id    statistic   p.value parameter
1:  X1 8.680556e-02 0.7682782         1
2:  X2 9.695384e-01 0.3247953         1
3:  X3 9.464545e-31 1.0000000         1
4:  X4 9.695384e-01 0.3247953         1
5:  X5 7.812500e-01 0.3767591         1

可重现的例子

nvars=5; nrows=50
set.seed(123)
X <- data.frame(matrix(sample(c(0,1), size=nrows*nvars, replace=TRUE), nc=nvars))
data <- data.frame(AppCategory=c(rep("Benign", 20), rep("Malware", 30)), X)
str(data)

'data.frame':   50 obs. of  6 variables:
 $ AppCategory: Factor w/ 2 levels "Benign","Malware": 1 1 1 1 1 1 1 1 1 1 ...
 $ X1         : num  0 0 0 1 0 1 1 1 0 0 ...
 $ X2         : num  1 0 0 0 0 1 1 0 1 0 ...
 $ X3         : num  0 1 1 0 1 1 0 0 0 1 ...
 $ X4         : num  0 1 0 1 0 0 0 0 0 0 ...
 $ X5         : num  1 1 1 0 1 1 1 0 1 1 ...

首先查看此处的所有详细信息:performing a chi square test across multiple variables and extracting the relevant p value in R 然后看下面类似的解决方案代码:

> # Assuming your dataframe is something like: 
> x1 <- sample(1:7,5,replace = F)
> x2 <- sample(2:7,5,replace = T)
> x3 <- sample(1:6,5,replace = T)
> x4 <- sample(3:8,5,replace = T)
> y <- sample(1:100,5,replace = F)
> df <- data.frame(cbind(x1,x2,x3,x4,y))
> ?sample
> mapply(function(x, y) chisq.test(x, y)$p.value, df[, -5], MoreArgs=list(df[,5]))
       x1        x2        x3        x4 
0.2202206 0.2202206 0.2872975 0.2414365 
# Note this is just a schema - you will need to adapt & align statistical nuances...