【问题标题】:Apply Chi-Squared Test in R on more than 5 variables and find the p-values对超过 5 个变量应用 R 中的卡方检验并找到 p 值
【发布时间】:2020-06-26 00:16:58
【问题描述】:

我是卡方检验的新手。我有一个包含很多分类变量的数据库。

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

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

你能告诉我如何在 R 中找到上述所有变量的 p 值。

据我所知,卡方只能应用于 2 个分类变量,但我有很多分类变量。怎么会这样?

【问题讨论】:

  • 我不确定卡方检验是否适合您的数据。你能澄清你试图检验的假设吗?另外,在提出新问题之前,也许您应该回顾一下:stackoverflow.com/help/no-one-answers
  • 我有大约 90 个分类变量,想删除在数据集中不重要的最不重要的变量。

标签: r chi-squared


【解决方案1】:

您可以使用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 ...

【讨论】:

  • 非常感谢您的帮助。有什么方法可以根据 p 值对这些进行排序,因为我有 90 个变量。 do.call(rbind, CHIS)[,c(1,3)]
  • %&gt;% dplyr::arrange(p.value)
【解决方案2】:

首先在此处查看所有详细信息: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...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多