r - 如何将效果大小添加到 t 检验的 ggplot 条形图? (例如 Cohen 的 d 或 Hedges 的 g)

标签 r ggplot2 statistics data-visualization t-test

我有一个如下所示的数据集。

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)

image of bar plot

问题

我需要什么代码(和/或包)来自动放置效果 每次比较的尺寸?

如有任何建议或指导,我们将不胜感激。

最佳答案

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,并将其放入图中。

enter image description here

关于r - 如何将效果大小添加到 t 检验的 ggplot 条形图? (例如 Cohen 的 d 或 Hedges 的 g),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66464930/

相关文章:

xml - 将函数应用于 R 中的 xmlNodeList(不是整个 xml 文件)

r - R中的错误栏?

python - 来自 scipy.stats.rv_continuous 的自定义 PDF 不需要的上限

machine-learning - 逻辑回归机器学习?

r - 从 R 打开 pdf 文件

r - 无法访问用于生成动态 Shiny 内容的函数内部的 react 对象

r - ggplot2:根据填充值为某些点设置alpha = 0

python - 如何计算几个进球转化率的统计显着性?

html - Rpres HTML5 演示文稿 "Save As PDF"(Google Chrome) 显示不正确

r - 如何避免 x 轴和 y 轴上的额外间距