【问题标题】:wilcox test and fisher test to different types of columns对不同类型色谱柱的 wilcox 测试和 Fisher 测试
【发布时间】:2021-10-15 10:34:19
【问题描述】:

我有一个列的列表,其编号属于每个类别

  list.group <-list(list(id = 2, type = "num"), list(id = 3, type = "num"), 
        list(id = 4, type = "cat"), list(id = 5, type = "cat"))

我有一个函数,其中有 2 个测试(wilcox 测试和 Fisher 测试),用于类型为“num”的列,我希望执行 wilcox 测试,而对于类型“cat”,执行 Fisher 测试。

首先我把一个列表分成2个列表(一个列列表和一个类别列表):

w = unlist(list.group, recursive = TRUE)
w.length = length(w)
col.id   = w[seq(1,w.length,2)]
col.type = w[seq(2,w.length,2)]
col.id    = as.integer(col.id)
col.type = as.character(col.type)

功能:

combination <- list(c(3,24),c(3,14))
wilcox.fun <- function(df, id_group){
  df = df[df$GROUP%in%id_group,]
 x <- function(dat) { 
  do.call(rbind, lapply(combination, function(x) {
    if(col.type=="num"){
       test <- wilcox.test(dat[[x[1]]], dat[[x[2]]])}
    if(col.type=="cat"){
       test1 <- fisher.test(dat[[x[1]]], dat[[x[2]]])}
    data.frame(Test = sprintf('Group %s by Group %s Group',x[1],x[2]), 
               #W = round(test$statistic,4), 
               p = test$p.value,
               p1 = test1$p.value,
               #median=paste(x[1],median.group.1,x[2],median.group.2),
               nmat = table(dat[[x[1]]]),
               nmat1 = round((prop.table(table(dat[[x[1]]]), 1) * 100), 1),
               nmat2=table(dat[[x[2]]]) 
               )
  }))
 }
 return (purrr::map_df(split(df, df$GROUP),x,.id="GROUP" ))
}

数据框:

data <- structure(list(GROUP = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), 
    col1 = c(23L, 432L, 234L, 234L, 3123L, 657L, 8768L, 123L, 
    42323L), col2 = c(567L, 765L, 8678L, 46L, 35L, 24L, 76L, 
    789L, 45L), col3 = c(1L, 3L, 5L, 7L, 8L, 0L, 8L, 7L, 3L), 
    col4 = c("S", "S", "S", "S", "F", "F", "F", "F", "F")), class = "data.frame", row.names = c(NA, 
-9L))

【问题讨论】:

    标签: r dataframe pairwise.wilcox.test


    【解决方案1】:

    您可以检查函数中传递的列的类,而不是使用单独的向量(col.idcol.type)。如果lapply 中的两列都是数字,我们会进行 wilcox 测试或 Fisher 测试。

    另外我不认为在数据帧中有table(...)prop.table(table(...)) 会起作用,因为创建的数据帧是1 行数据帧,而table 的输出长度> 1。除此之外table(dat[[x[1]]]) table(dat[[x[2]]]) 也可以有不同长度的输出,这会在构建数据帧时产生问题。

    wilcox.fun <- function(df, id_group){
      df = df[df$GROUP%in%id_group,]
      x <- function(dat) { 
        do.call(rbind, lapply(combination, function(x) {
          col1 <- dat[[x[1]]]
          col2 <- dat[[x[2]]]
          if(is.numeric(col1) && is.numeric(col2)) test <- wilcox.test(col1, col2)
          else  test <- fisher.test(col1, col2)
          data.frame(Test = sprintf('Group %s by Group %s Group',x[1],x[2]), 
                     #W = round(test$statistic,4), 
                     p = test$p.value
          )
        }))
      }
      return (purrr::map_df(split(df, df$GROUP),x,.id="GROUP" ))
    }
    
    wilcox.fun(data, c(1, 2))
    

    如果你想显式传递col.type,它应该与combination具有相同的结构,然后你可以使用Map -

    col_type_list <- list(col.type[1:2], col.type[3:4])
    
    wilcox.fun <- function(df, id_group){
      df = df[df$GROUP%in%id_group,]
      x <- function(dat) { 
        do.call(rbind, Map(function(x, y) {
          col1 <- dat[[x[1]]]
          col2 <- dat[[x[2]]]
          if(all(y == 'num')) test <- wilcox.test(col1, col2)
          else  test <- fisher.test(col1, col2)
          data.frame(Test = sprintf('Group %s by Group %s Group',x[1],x[2]), 
                     #W = round(test$statistic,4), 
                     p = test$p.value
          )
        }, combination, col_type_list))
      }
      return (purrr::map_df(split(df, df$GROUP),x,.id="GROUP" ))
    }
    

    【讨论】:

    • 这是合适的,但有一个小问题,事实上“cat”类型的列也可以是数字的,但由于它们是 cat,因此对其应用了 Fisher 测试
    • 我添加了一个更新以使用 col.type 专门用于测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 2023-02-14
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    相关资源
    最近更新 更多