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

R语言games_howell_test报错:df需为15或1行而非12行

解决R中Games-Howell成对比较检验的报错问题

问题重现

运行Games-Howell成对比较检验时触发数据尺寸不匹配报错,核心提示为:! df must be size 15 or 1, not 12.

测试数据

可直接用以下代码构造测试数据集:

df <- tibble::tribble(
  ~Genotype, ~Tillering,
  "Geno1", 22, "Geno1", 23, "Geno1", 23, "Geno1", 23,
  "Geno2", 22, "Geno2", 22, "Geno2", 22, "Geno2", 22,
  "Geno3", 23, "Geno3", 22, "Geno3", 23, "Geno3", 23,
  "Geno4", 23, "Geno4", 23, "Geno4", 23, "Geno4", 23,
  "Geno5", 21, "Geno5", 21, "Geno5", 20, "Geno5", 21,
  "Geno6", 22, "Geno6", 22, "Geno6", 22, "Geno6", 22
) %>%
  dplyr::mutate(Genotype = factor(Genotype))

原报错代码

library(rstatix)
library(dplyr)

df2 <- games_howell_test(df, Tillering~Genotype, detailed = T) %>% print(n=15)

报错原因

该错误是rstatix包中games_howell_test函数在detailed = TRUE参数下的内部处理bug:当处理平衡设计的数据集时,内部计算的统计量维度与成对比较总数(6组共15对)不匹配,导致尺寸校验失败。

解决方案

方案1:更新rstatix到最新版本

该bug已在新版rstatix中修复,运行以下命令更新包:

install.packages("rstatix")

更新后重新执行原代码即可正常输出详细结果。

方案2:绕开detailed = TRUE参数手动提取信息

若暂时无法更新包,可先运行基础检验,再手动拼接详细统计信息:

# 运行基础Games-Howell检验
df_result <- games_howell_test(df, Tillering~Genotype)

# 计算各组基础统计量
group_stats <- df %>%
  group_by(Genotype) %>%
  summarise(n = n(), mean = mean(Tillering), sd = sd(Tillering), .groups = "drop")

# 合并结果得到详细输出
df_detailed <- df_result %>%
  left_join(group_stats, by = c("group1" = "Genotype")) %>%
  left_join(group_stats, by = c("group2" = "Genotype"), suffix = c("_1", "_2"))

print(df_detailed, n=15)

验证结果

更新包后运行原代码,将输出包含均值、标准差、样本量等信息的15组成对比较结果,示例片段如下:

# A tibble: 15 × 16
   group1 group2 estimate conf.low conf.high p.adj p.adj.signif  mean1  mean2    sd1    sd2     n1     n2    df statistic    p
   <fct>  <fct>     <dbl>    <dbl>     <dbl> <dbl> <chr>        <dbl>  <dbl>  <dbl>  <dbl>  <int>  <int> <dbl>     <dbl> <dbl>
 1 Geno1  Geno2      0.75  -0.189      1.69  0.174 ns            22.75 22     0.5    0          4      4  5.98      2.34 0.064
 2 Geno1  Geno3      0     -0.939      0.939 1     ns            22.75 22.75  0.5    0.5        4      4  6       0     1    
 3 Geno1  Geno4     -0.25  -1.19       0.689 0.767 ns            22.75 23     0.5    0          4      4  5.98     -0.781 0.467
...

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 22:09:24