I would like to perform a Tukey Post Hoc test on a list of dataframes. As outcome I would like to have letters indicating which groups are significantly different from each other. The HSD.test() of the agricolae package does this, but I can't figure out how to apply this on several variables at once. This will save me a lot of time as my dataset contains a lot of variables.
This is part of my data:
category <- c(rep("young", 3), rep("Middle", 4), rep("old", 5))
fat <- c(1857.87, 1953.90, 1440.70, 1553.81, 1785.91, 1893.82, 1483.75, 1784.99, 2011.01, 2023.04, 2011.05, 1788.81)
BMI <- c(21.1, 23.2, 24.5, 25.6, 21.8, 18.0, 19.2, 20.1, 22.1, 25.0, 26.1, 25.1)
age <- c(25, 23, 27, 55, 58, 62, 45, 75, 80, 75, 83, 89)
df2 <- data.frame(fat, BMI, age, category)
I know how to do the anova and HSD.test() on one variable.
lm.fat <- (lm(fat ~ as.factor(category), data = df2))
anova(lm.fat)
require(agricolae)
HSD.test(lm.fat, "as.factor(category)", group = TRUE, console = TRUE)
In addition, I know how to apply the anova to all my variables in my dataset by using the sapply() function:
an <- lapply(df, function(x) aov(x~category, data = df))
sapply(an, anova, simplify=FALSE)
But I don't know how to peform the posthoc HSD.test() on these outcomes. I tried this:
lapply(an, function(m) HSD.test((m), "as.factor(category)", group = TRUE, console = TRUE))
Name: as.factor(category)
category
Name: as.factor(category)
category
Name: as.factor(category)
category
$fat
NULL
$BMI
NULL
$age
NULL
I get NULL as outcome, so something went wrong, but I can't figure out what. I tried also another function of the Tukey post hoc test, namely: TukeyHSD().
lapply(an, function(m) TukeyHSD(aov(m)))
$fat
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = m)
$category
diff lwr upr p adj
old-Middle 244.45750 -110.2508 599.1658 0.1874162
young-Middle 71.50083 -332.3523 475.3540 0.8757638
young-old -172.95667 -559.1142 213.2008 0.4554780
$BMI
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = m)
$category
diff lwr upr p adj
old-Middle 2.5300000 -2.494240 7.554240 0.3781804
young-Middle 1.7833333 -3.937015 7.503682 0.6711525
young-old -0.7466667 -6.216366 4.723033 0.9237118
$age
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = m)
$category
diff lwr upr p adj
old-Middle 25.4 14.49330 36.30670 0.0002928
young-Middle -30.0 -42.41783 -17.58217 0.0002219
young-old -55.4 -67.27372 -43.52628 0.0000010
This works for my dataset, but this function doesn't give the letters, which I really would like to have. Does someone know how I can do the same with HSD.test() so that I will obtain the letters? Thanks!