英文:
In R is there a way to loop multcompview functions (i.e. CLD) through lists of ANOVAs and Tukeys?
问题
以下是翻译好的内容:
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;
我最终想要使用CLDs绘制每个变量。但与此同时,我只是在努力获得CLDs。我的数据看起来像这样:
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;
感谢之前的一个非常有帮助的答案这里,然后我通过循环对每个变量执行ANOVA和Tukey的方法(并创建了数据框),如下所示:
## 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!
现在,我只需要获取每个变量的CLDs。我通常使用multcompView::multcompLetters4()
,但我知道还有其他选择。我也无法弄清楚如何使用lapply
或mapply
来处理两个不同的列表。最终,我希望能使用geom_boxplot()
绘制结果,虽然我可能会完全迷失方向,但慢慢来。非常感谢任何帮助!
英文:
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!
答案1
得分: 1
以下是使用multcompView::multcompLetters4()
的另一种方法:
## 对依赖变量执行方差分析
aov_res <- apply(df[,2:ncol(df)], 2, function(x) aov(x ~ Treat, data = df))
# 使用lapply而不是sapply对每个ANOVA测试的结果应用Tukey的HSD测试
tukey_res <- lapply(aov_res, function(x) TukeyHSD(x, "Treat", ordered = TRUE))
clds <- lapply(names(aov_res), function(x) multcompLetters4(aov_res[[x]], tukey_res[[x]]))
# 整理数据
names(clds) <- names(aov_res)
clds <- as.data.frame(clds)
英文:
Here is another way using multcompView::multcompLetters4()
:
## 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 using lapply instead of sapply
tukey_res <- lapply(aov_res, function(x) TukeyHSD(x, "Treat", ordered = TRUE))
clds <- lapply(names(aov_res), function(x) multcompLetters4(aov_res[[x]], tukey_res[[x]]))
# tidy up
names(clds) <- names(aov_res)
clds <- as.data.frame(clds)
答案2
得分: 1
获取CLDs,您可以将'aov_res'传递给首先使用emmeans包的**emmeans()
函数,以获取带有SE和置信限的边际均值。然后,此输出将用作mulicomp和multicompview包中cld()
函数的所需对象,它会添加字母以比较Treats并进行紧凑的字母显示。如果您希望使用Tukey方法进行调整,只需在cld()
函数中删除adjust=
**选项。
library(multcomp)
library(multcompView)
library(emmeans)
library(tidyverse)
df$Treat <- factor(df$Treat)
## 对因变量执行方差分析
aov_res <- apply(df[,2:ncol(df)], 2, function(x) aov(x ~ Treat, data = df))
# 估计边际均值(最小二乘均值)
CLD <- lapply(aov_res, FUN = function(i) emmeans::emmeans(i, specs = "Treat"))
# 将紧凑字母添加到估计均值中,请注意,“tukey”仅适用于一组成对比较
CLD_letters <- lapply(CLD
,cld # 设置紧凑字母显示的所有成对比较
,adjust = 'sidak' # 使用sidak方法调整p值(其他方法包括fdr、BH等)
,Letters = letters # 使用小写字母分组
,alpha = 0.05 # 您的显著性水平
,reversed = T) # 如果希望较大的均值,则排序均值
# 将比较的列表转换为包括所有关于处理均值和每个因变量的信息的数据框
CLD_letters_df <- bind_rows(CLD_letters, .id = "variable")
CLD_letters_df
英文:
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 for cld()
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 remove adjust=
option in the cld()
function.
library(multcomp)
library(multcompView)
library(emmeans)
library(tidyverse)
df$Treat<-factor(df$Treat)
## perform anova on dependent variables
aov_res <- apply(df[,2:ncol(df)], 2, function(x) aov(x ~ Treat, data = df))
#Estimate marginal means (Least-squares means)
CLD <- lapply(aov_res ,FUN = function(i) emmeans::emmeans(i, specs = "Treat"))
# add your compact letters to the estimated means, note that "tukey" is only appropriate for one set of pairwise comparisons
CLD_letters <- lapply(CLD
,cld #Set up a compact letter display of all pair-wise comparisons
,adjust= 'sidak' # adjusting p-values using sidak method (other methods are fdr, BH,...)
,Letters = letters # grouping with small letters
,alpha = 0.05 # your significance level
,reversed = T) # sort means if larger means are desired
# convert list of comparisons to dataframe including all informations on treatment means and for each dependent variable
CLD_letters_df <- bind_rows(CLD_letters, .id = "variable")
CLD_letters_df
答案3
得分: 0
你可以使用multcomp
包中的函数吗?
英文:
Could you use the functions in multcomp
:
library(multcomp)
library(broom)
library(dplyr)
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))
df$Treat <- as.ordered(df$Treat)
## 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 <- lapply(aov_res, function(a)summary(glht(a, linfct = mcp(Treat = "Tukey"))))
# 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 <- bind_rows(lapply(tukey_res, function(t)do.call(data.frame, t$test[-c(1,2)]) %>%
as_tibble(rownames="comparison")), .id = "DV")
clds <- lapply(tukey_res, cld)
<sup>Created on 2023-02-23 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论