簡體   English   中英

R 中多列的卡方檢驗

[英]Chi square tests for multiple columns in R

在這里,我做了如下data

data<-data.frame(alzheimer=c(1,1,0,1,0,0,1,0,0,0),
                 asthma=c(1,1,0,0,1,1,1,1,0,0),
                 points=c(0,1,3,5,3,2,1,2,1,5),
                 sex=c(1,1,0,0,0,0,1,1,1,0))

我想知道sex是否會影響alzheimerasthmapoints 所以我正在考慮為獨立性做卡方檢驗。 alzheimerasthma是二元變量,所以我想我可以分別將sex ==1 和sex ==0 中的所有數字相加,並制作列聯表來進行卡方檢驗。 對於變量points ,我不知道是否可以進行卡方檢驗,因為points是一個序數變量,范圍從 0 到 5,只有整數。

總結一下,我想做3個測試。

  1. sexalzheimer是獨立的嗎?
  2. sexasthma是獨立的嗎?
  3. sexpoints是獨立的嗎?

另外,在我的實際data中,有很多列,所以我需要知道如何一次完成許多測試並將其寫入 csv 文件。 csv 文件應包含測試統計數據和 p 值。

我們可以編寫一個 function stat_test ,它在二進制列上應用chisq.test ,在其他列上應用wilcox.test (假設它們都是序數列)。 我們可以把這個 function output 做成三件事。

  1. 測試名稱
  2. 統計數據(stats)的值
  3. p值

然后我們可以使用dplyr::across()將此測試應用於所有列(期望在我們的函數中用作y輸入的alzheimer列)。 之后我們只需將標簽添加為第一行。

data <- data.frame(alzheimer=c(1,1,0,1,0,0,1,0,0,0),
                   asthma=c(1,1,0,0,1,1,1,1,0,0),
                   points=c(0,1,3,5,3,2,1,2,1,5),
                   sex=c(1,1,0,0,0,0,1,1,1,0))

library(dplyr)

stat_test <- function(x, y) {
  if (length(unique(na.omit(x))) > 2) {
    res <- chisq.test(x = x,
               y = y)
    label <- "chi_square"
  } else {
    res <- wilcox.test(x, y = y)
    label <- "wilcox"
  }
  
  c(
    test = label,
    stats = res$statistic,
    p_val = res$p.value
  )
}

data %>% 
  as_tibble %>% 
  summarise(across(-alzheimer,
                   ~ stat_test(.x, alzheimer))) %>% 
  mutate(label = c("test", "stats", "pvalue"), .before = 1L)
#> Warning in wilcox.test.default(x, y = y): cannot compute exact p-value with ties
#> Warning in chisq.test(x = x, y = y): Chi-squared approximation may be incorrect
#> Warning in wilcox.test.default(x, y = y): cannot compute exact p-value with ties
#> # A tibble: 3 x 4
#>   label  asthma            points            sex              
#>   <chr>  <chr>             <chr>             <chr>            
#> 1 test   wilcox            chi_square        wilcox           
#> 2 stats  60                5.13888888888889  55               
#> 3 pvalue 0.407562453620744 0.273341191458911 0.693376361757653

代表 package (v2.0.1) 於 2022 年 9 月 27 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM