I eventually want to plot each variable with CLDs. But in the mean time I am having trouble just getting the CLDs. I have data that looks like this;
df <- data.frame(Treat = rep(LETTERS[1:4], 100, replace = TRUE),
A = rnorm(400),
B = rnorm(400),
C = rnorm(400),
D = rnorm(400),
E = rnorm(400),
F = rnorm(400),
G = rnorm(400),
H = rnorm(400))
Thanks to a very helpful answer from a previous question here, I then looped ANOVA's and Tukey's through each variable (and made data frames) like this;
## perform anova on dependent variables
aov_res <- apply(df[,2:ncol(df)], 2, function(x) aov(x ~ Treat, data = df))
# Apply Tukey's HSD test to the results of each ANOVA test
tukey_res <- sapply(aov_res, function(x) TukeyHSD(x, "Treat", ordered = TRUE))
# Convert the results of each ANOVA test into a tidy data frame using the broom package
aov_res_df <- do.call(rbind, lapply(aov_res, broom::tidy))
# Combine the results of the Tukey HSD tests into a single data frame
tukey_res_df <- as.data.frame(do.call(rbind, Map(cbind, Name = names(tukey_res), tukey_res)))
Now I just need to get the CLDs for each variable. I usually use multcompView::multcompLetters4()
but I know there are alternatives. I also couldn't figure out lapply
or mapply
for working with two separate lists. Eventually I would love to geom_boxplot()
the results, and I will be totally lost, but baby steps. Any help is greatly appreciated!
To get the CLDs you can pass the 'aov_res' to first, the
emmeans()
function from emmeans package to obtain the marginal means with SEs and confidence limits. Then this output would be used as a desired object forcld()
function from mulicomp and multicompview packages which add letters to compare the Treats with compact letter display.If you wish to do adjustment with Tukey method, simply removeadjust=
option in thecld()
function.