Reputation: 18219
Intro
After performing an ANOVA, we often perform a series of pairwise comparisons. When the number of groups is relatively large, the number of pairwise comparison becomes very big (it is its triangular number). On graphs, it is common to indicate groups that are not significantly different by marking them with the same letter and it would therefore be handy to have a function that would produce these letters.
Goal
I am trying to build a function in R
that would take in input:
TukeyHSD(aov(y~x))
.. and would output:
Example
set.seed(10)
d = data.frame(
y=c(rnorm(100,10),rnorm(100,10),rnorm(100,8.5),rnorm(100,8)),
x=rep(c("High","MidHigh", "MidLow","Low"), each=100)
)
z=TukeyHSD(aov(y~x,data=d))
z
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = y ~ x, data = d)
$x
diff lwr upr p adj
Low-High -1.66461230 -2.02288676 -1.3063378 0.0000000
MidHigh-High 0.04158636 -0.31668810 0.3998608 0.9906564
MidLow-High -1.33469921 -1.69297367 -0.9764247 0.0000000
MidHigh-Low 1.70619866 1.34792420 2.0644731 0.0000000
MidLow-Low 0.32991309 -0.02836138 0.6881876 0.0834543
MidLow-MidHigh -1.37628557 -1.73456004 -1.0180111 0.0000000
And here is what the function fun
would do
fun(Tukey=z, alpha=0.05)
High MidHigh MidLow Low
"A" "A" "B" "B"
fun(Tukey=z, alpha=0.1)
High MidHigh MidLow Low
"A" "A" "B" "C"
Upvotes: 0
Views: 171
Reputation: 37754
Try the multcompView package.
library(multcompView)
multcompLetters(z$x[,4])
## Low MidHigh MidLow High
## "a" "b" "a" "b"
Upvotes: 3