[英]How to perform Tukey HSD.test() on list of dataframes?
I would like to perform a Tukey Post Hoc test on a list of dataframes.我想对数据帧列表执行 Tukey Post Hoc 测试。 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.
agricolae 包的 HSD.test() 执行此操作,但我无法弄清楚如何一次将其应用于多个变量。 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.我知道如何对一个变量进行方差分析和 HSD.test()。
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:此外,我知道如何使用 sapply() 函数将方差分析应用于数据集中的所有变量:
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.但我不知道如何对这些结果执行 posthoc HSD.test() 。 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.我得到 NULL 作为结果,所以出了点问题,但我不知道是什么。 I tried also another function of the Tukey post hoc test, namely: TukeyHSD().
我还尝试了 Tukey post hoc 测试的另一个功能,即: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?
有人知道我如何用 HSD.test() 做同样的事情,以便我获得这些字母吗? Thanks!
谢谢!
You can to try the following code:您可以尝试以下代码:
List <- names(df2)[1:3] # select just the variables
model1 <- lapply(List, function(x) {
lm(substitute(i~category, list(i = as.name(x))), data = df2)})
lapply(model1, summary)
letters = lapply(model1, function(m) HSD.test((m), "category", group = TRUE, console = TRUE))
If were it interaction it would be:如果是交互,它将是:
tx <- with(df2, interaction(category1, category2)) # determining the factors
model2 <- lapply(List, function(x) {
glm(substitute(i~tx, list(i = as.name(x))), data = df2)}) # using the factors already in "tx"
lapply(model2, summary)
letters = lapply(model2, function(m) HSD.test((m), "tx", alpha = 0.05, group = TRUE, console = TRUE))
Best regards此致
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.