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

如何将ggbetweenstats两分组t-test替换为Games-Howell检验

统一两组数据的检验方法:将双类别组的t-test替换为Games-Howell检验

问题说明

我在对比多组数据时遇到以下问题:

  • 部分数据组包含2个以上类别,使用ggbetweenstats绘图时,程序会自动采用Games-Howell成对比较(基于单因素ANOVA)
  • 仅含2个类别的数据组,程序默认使用t-test进行成对比较,导致不同组间的检验逻辑和p值计算方式不统一

我希望所有组都统一使用Games-Howell检验,但找不到直接修改参数的方法,仅知道可以调用PMCMRplus::gamesHowellTest()实现该检验,需要技术方案将其整合到现有代码中。

数据示例

structure(list(lake_method = structure(c(3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), levels = c("Clear Lake - Angling", 
"Clear Lake - E-fishing", "Clear Lake - Spearfishing", "Cultus Lake - Angling"
), class = "factor"), group = c("TOTAL cpuefh", "TOTAL cpuekgh", 
"MM cpuefh", "MM-N cpuefh", "MM-NN cpuefh", "MF cpuefh", "J cpuefh", 
"TOTAL cpuefh", "TOTAL cpuekgh", "MM cpuefh", "MM-N cpuefh", 
"MM-NN cpuefh", "MF cpuefh", "J cpuefh", "TOTAL cpuefh", "TOTAL cpuekgh", 
"MM cpuefh", "MM-N cpuefh", "MM-NN cpuefh", "MF cpuefh"), value = c(3.20045832408987, 
0.627289831521615, NA, NA, NA, 3.20045832408987, NA, 13.6134036988146, 
1.23881973659213, NA, NA, NA, NA, 13.6134036988146, 65.5138665685633, 
15.4612725101809, NA, NA, NA, 65.5138665685633)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

当前代码与差异

多类别组代码(自动使用Games-Howell)

p1 <- ggbetweenstats(
  data = dplyr::filter(data_violin_f, group == "MM cpuefh"),
  x = lake_method,
  y = value,
  xlab = "",
  ylab = "fish/h",
  title = "MM fish/h of smallmouth bass captured per method (both lakes)",
  pairwise.comparisons = TRUE, ## 显示成对比较
  results.subtitle = FALSE,
  pairwise.display = "s",
  p.adjust.method = "none" ## 不调整多重检验p值
)

p1

双类别组代码(自动使用t-test)

p2 <- ggbetweenstats(
  data = dplyr::filter(data_violin_f, group == "MM-N cpuefh"),
  x = lake_method,
  y = value,
  xlab = "",
  ylab = "fish/h",
  title = "MM-N fish/h of smallmouth bass captured per method (both lakes)",
  pairwise.comparisons = TRUE, ## 显示成对比较
  results.subtitle = TRUE,
  pairwise.display = "s",
  p.adjust.method = "none", ## 不调整多重检验p值
)

p2

解决方案

通过自定义成对检验函数,强制ggbetweenstats对所有组使用Games-Howell检验,步骤如下:

  1. 确保已安装并加载所需包:
install.packages(c("ggstatsplot", "PMCMRplus", "dplyr"))
library(ggstatsplot)
library(PMCMRplus)
library(dplyr)
  1. 定义自定义成对检验函数,调用PMCMRplus::gamesHowellTest():
custom_games_howell <- function(data, x, y, ...) {
  # 构造公式并执行Games-Howell检验
  PMCMRplus::gamesHowellTest(
    formula = as.formula(paste(y, "~", x)),
    data = data,
    ...
  )
}
  1. 修改双类别组的绘图代码,指定pairwise.test参数为自定义函数:
p2 <- ggbetweenstats(
  data = dplyr::filter(data_violin_f, group == "MM-N cpuefh"),
  x = lake_method,
  y = value,
  xlab = "",
  ylab = "fish/h",
  title = "MM-N fish/h of smallmouth bass captured per method (both lakes)",
  pairwise.comparisons = TRUE,
  results.subtitle = TRUE,
  pairwise.display = "s",
  p.adjust.method = "none",
  # 强制使用自定义的Games-Howell检验
  pairwise.test = custom_games_howell
)

p2

这样修改后,无论数据组包含2个还是更多类别,都会统一使用Games-Howell检验进行成对比较,保证结果的一致性。

内容的提问来源于stack exchange,提问作者Peter Pauker

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 05:36:00