You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让rcompanion::cldList按指定顺序分配显著性字母?

解决rcompanion::cldList按指定组顺序分配显著性字母的问题

在处理海绵骨针嵌套模型的TukeyHSD结果时,需要让rcompanion::cldList生成的*紧凑字母显示(CLD)*匹配图表的组顺序,且要求Summer:Beggers:Mid组从字母a开始分配显著性字母。

尝试过以下方法均未达到预期效果:

  • 直接排序输入数据框
  • 手动调整比较组的顺序
  • 手动翻转比较组对,反而导致CLD结果异常

解决方案:自定义修改cldList函数

通过复制并修改rcompanion::cldList的源码,得到cldList2函数,新增group.order参数来指定组的排序优先级,确保目标组从a开始分配字母。

完整代码示例

# 加载所需包
library(rcompanion)

# 自定义cldList2函数,支持指定组顺序
cldList2 <- function(comparison,
                     p.value,
                     threshold = 0.05,
                     print.comp = FALSE,
                     remove.space = TRUE,
                     group.order = NULL) {
  
  if (remove.space) {
    comparison <- gsub(" ", "", comparison)
  }
  
  Comp <- data.frame(Comparison = comparison,
                     p.value    = p.value,
                     stringsAsFactors = FALSE)
  
  Comp$Left  <- gsub("-.*", "", Comp$Comparison)
  Comp$Right <- gsub(".*-", "", Comp$Comparison)
  
  Groups <- unique(c(as.character(Comp$Left),
                     as.character(Comp$Right)))
  
  # 核心修改:按指定顺序排序组
  if (!is.null(group.order)) {
    if (!all(Groups %in% group.order)) {
      stop("Not all groups in comparison are present in group.order")
    }
    # 按指定顺序重新排列Groups
    Groups <- group.order[group.order %in% Groups]
  } else {
    Groups <- sort(Groups)
  }
  
  k <- length(Groups)
  
  Z <- matrix(data = TRUE, nrow = k, ncol = k)
  rownames(Z) <- Groups
  colnames(Z) <- Groups
  
  for (i in 1:nrow(Comp)) {
    if (Comp$p.value[i] <= threshold) {
      Z[Comp$Left[i], Comp$Right[i]] <- FALSE
      Z[Comp$Right[i], Comp$Left[i]] <- FALSE
    }
  }
  
  diag(Z) <- FALSE
  
  if (print.comp) {
    print(Z)
  }
  
  CLD <- character(k)
  names(CLD) <- Groups
  
  CurrentLetter <- 65
  
  while (sum(CLD == "") > 0) {
    Candidates <- names(CLD[CLD == ""])
    
    Found <- TRUE
    while (Found) {
      Found <- FALSE
      for (i in Candidates) {
        if (all(Z[i, Candidates])) {
          CLD[i] <- paste0(CLD[i], intToUtf8(CurrentLetter))
          Candidates <- Candidates[Candidates != i]
          Found <- TRUE
        }
      }
    }
    
    CurrentLetter <- CurrentLetter + 1
    if (CurrentLetter > 122) {
      stop("Too many groups for lettering scheme.")
    }
  }
  
  CLD <- gsub("^$", " ", CLD)
  
  CLD <- data.frame(Group = names(CLD),
                    Letter = CLD,
                    stringsAsFactors = FALSE)
  
  CLD$Letter <- tolower(CLD$Letter)
  
  rownames(CLD) <- NULL
  
  return(CLD)
}

# 示例:基于TukeyHSD结果生成CLD
# 模拟从TukeyHSD中提取的比较组和p值
tukey_comparisons <- c("Summer:Beggers:Mid-Summer:Beggers:Low",
                       "Summer:Beggers:Mid-Winter:Beggers:Mid",
                       "Summer:Beggers:Low-Winter:Beggers:Mid")
tukey_pvalues <- c(0.06, 0.03, 0.02)

# 指定组顺序,确保目标组排在首位
target_group_order <- c("Summer:Beggers:Mid", "Summer:Beggers:Low", "Winter:Beggers:Mid")

# 生成CLD结果
cld_output <- cldList2(comparison = tukey_comparisons,
                       p.value = tukey_pvalues,
                       threshold = 0.05,
                       group.order = target_group_order)

print(cld_output)

内容的提问来源于stack exchange,提问作者Graham Sharpe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 05:06:19