You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

R语言中Games-Howell事后检验添加字母标记问题求助

Solution for Generating Group Letter Labels with Games-Howell Post Hoc Test

Hey there, I get your frustration—getting those handy letter labels for group comparisons when using Games-Howell can be tricky since userfriendlyscience doesn't output them directly, and multcompView::cld() doesn't play nice with its output. Let's walk through two solid solutions to fix this:


Method 1: Use rstatix (Simplest & Most Straightforward)

The rstatix package is built for tidy, easy-to-use statistical tests, and it natively supports generating letter labels for Games-Howell results. Here's how to do it:

# Install and load required packages (if not already installed)
if (!require("agricolae")) install.packages("agricolae")
if (!require("rstatix")) install.packages("rstatix")

library(agricolae)
library(rstatix)

# Load the sweetpotato dataset
data(sweetpotato)

# Run the Games-Howell post hoc test
gh_test <- sweetpotato %>% 
  games_howell_test(yield ~ virus)

# Generate group letter labels
cld_results <- gh_test %>% 
  cld_list(p.adj = "p.adj", threshold = 0.05)

# View the final labels (group names paired with their letters)
print(cld_results)

How this works:

  • games_howell_test() returns a tidy dataframe of post hoc results, including adjusted p-values.
  • cld_list() takes these results and generates compact letter displays (CLD) just like you get with Tukey tests in agricolae—groups sharing the same letter are not significantly different at your chosen threshold (default 0.05).

Method 2: Manually Extract Results from userfriendlyscience

If you prefer sticking with userfriendlyscience, you can extract the post hoc p-values and feed them into multcompView to generate labels. Here's the code:

# Install and load packages
if (!require("userfriendlyscience")) install.packages("userfriendlyscience")
if (!require("multcompView")) install.packages("multcompView")
if (!require("agricolae")) install.packages("agricolae")

library(userfriendlyscience)
library(multcompView)
library(agricolae)

data(sweetpotato)

# Run the oneway analysis with Games-Howell post hoc
oneway <- oneway(sweetpotato$virus, y=sweetpotato$yield, posthoc = 'games-howell')

# Extract adjusted p-values from the post hoc results
posthoc_pvals <- oneway$posthoc$games.howell$comparisons$p.adj
# Name the p-values with "Group1-Group2" format (required by multcompLetters)
names(posthoc_pvals) <- paste(oneway$posthoc$games.howell$comparisons$group1,
                              oneway$posthoc$games.howell$comparisons$group2,
                              sep = "-")

# Generate the letter labels
letter_labels <- multcompLetters(posthoc_pvals, threshold = 0.05)$Letters

# Sort the labels by group name for readability
letter_labels <- letter_labels[order(names(letter_labels))]
print(letter_labels)

Why this fixes your issue:

The cld() function from multcompView is designed for objects from the multcomp package, not userfriendlyscience's oneway output. By manually extracting the adjusted p-values and formatting them correctly, we can use multcompLetters() (the underlying function that generates CLDs) to get the labels you need.


Either method will give you the group letter labels you're looking for. I personally recommend the rstatix approach since it's more streamlined and fits into a tidy workflow.

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

火山引擎 最新活动