Search code examples
rpairwise

Compact letter display from pairwise test


I would like to create a compact letter display from a post-hoc test I did on a linear mixed effect model (lmer)

Here is an example of what I would like when I do a pairwise t.test

df <- read.table("https://pastebin.com/raw/Dzfh7b2f", header=T,sep="")

library(rcompanion)
library(multcompView)
PT <- pairwise.t.test(df$fit,df$treatment, method=bonferroni)
PT = PT$p.value
PT1 = fullPTable(PT)
multcompLetters(PT1,
                compare="<",
                threshold=0.05,
                Letters=letters,
                reversed = FALSE)

This works our great, because from the pairwise.t.test, it is easy to simply extract the p values, and create the table I would like.

Now lets say I run a linear model, do a pairwise comparison, and would like to also create a table, as I did above, that creates a compact letter display for me from the extracted pvalues



library(multcomp)
mult<- summary(glht(model, linfct = mcp(treatment = "Tukey")), test = adjusted("holm"))
mult

I can see the p values, but have spent the last 2-3 hours trying to figure out how to just extract those values (as I did above with the pairwise.t.test), and subsequently, create a compact letter display table.

Any help is much appreciated. All the best


Solution

  • Find more details here.

    mod <- lm(Sepal.Width ~ Species, data = iris)
    
    mod_means_contr <- emmeans::emmeans(object = mod,
                                        pairwise ~ "Species",
                                        adjust = "tukey")
    
    mod_means <- multcomp::cld(object = mod_means_contr$emmeans,
                               Letters = letters)
    
    ### Bonus plot
    library(ggplot2)
    
    ggplot(data = mod_means,
           aes(x = Species, y = emmean)) +
      geom_point() +
      geom_errorbar(aes(ymin = lower.CL, 
                        ymax = upper.CL), 
                    width = 0.2) +
      geom_text(aes(label = gsub(" ", "", .group)),
                position = position_nudge(x = 0.2)) +
      labs(caption = "Means followed by a common letter are\nnot significantly different according to the Tukey-test")
    

    Created on 2021-06-03 by the reprex package (v2.0.0)