我有一个如下所示的数据集。
Group <- c("A", "B", "A", "A", "A", "B", "A", "A", "B", "B","A", "A", "B", "B")
Score <- c(26, 22, 15, 5, 19, 3, 4, 5, 23, 3, 5, 2, 20, 4)
Order <- c("First", "First", "First", "Second", "First", "Second", "Second", "Second", "First", "Second", "Second", "Second", "First", "Second")
Data <- data.frame(Group, Score, Order)
Data
Group Score Order
1 A 26 First
2 B 22 First
3 A 15 First
4 A 5 Second
5 A 19 First
6 B 3 Second
7 A 4 Second
8 A 5 Second
9 B 23 First
10 B 3 Second
11 A 5 Second
12 A 2 Second
13 B 20 First
14 B 4 Second
我需要绘制每组之间的分数差异,以及每次比较的效果大小(Cohen 的 d 或 Hedge 的 g 都可以)。
下面的代码为我提供了情节中我想要的一切,但影响大小。
OrderComparison <- list(c("First", "Second"))
ggplot(Data, aes(Order, Score, fill=Order))+
stat_summary(geom = "bar", fun = mean, position = "dodge", color="black")+
stat_summary(geom = "errorbar", fun.data = mean_se, position = "dodge", width=.2)+
stat_compare_means(method = "t.test", comparisons = OrderComparison, label = "p.signif", position = "identity")+
facet_wrap(~Group, scales="fixed", strip.position = "bottom")+
theme_classic()+
theme(legend.position = "none")+
scale_fill_grey(start = .6, end = 1)
问题
我需要什么代码(和/或包)来自动放置效果 每次比较的尺寸?
如有任何建议或指导,我们将不胜感激。
最佳答案
library(effsize)
library(dplyr)
cohen=numeric()
for (i in LETTERS[1:2]) {
Group=filter(Data, Group==i)
treatment = filter(Group, Order=="First")%>%select(Score)%>%unlist()
control = filter(Group, Order=="Second")%>%select(Score)%>%unlist()
d = (c(treatment,control))
f = c(rep("Treatment", length(treatment)), rep("Control", length(control)))
c=cohen.d(treatment,control)
cohen[i]=c$estimate
}
effsize=data.frame(sz=c(rep(paste("Cohen's d:", cohen[1]),8), rep(paste("Cohen's d:", cohen[2]),6)), Group=c(rep("A", 8), rep("B", 6)))
ggplot(Data, aes(Order, Score, fill=Order))+
stat_summary(geom = "bar", fun = mean, position = "dodge", color="black")+
stat_summary(geom = "errorbar", fun.data = mean_se, position = "dodge", width=.2)+
stat_compare_means(method = "t.test", comparisons = OrderComparison, label = "p.signif", position = "identity")+
facet_wrap(~Group, scales="fixed", strip.position = "bottom")+
theme_classic()+
theme(legend.position = "none")+
scale_fill_grey(start = .6, end = 1)+
geom_text(aes(x=1.5,y=25, label=sz), data=effsize)
它手动计算两组的 Cohen's d,并将其放入图中。
关于r - 如何将效果大小添加到 t 检验的 ggplot 条形图? (例如 Cohen 的 d 或 Hedges 的 g),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66464930/