3

I have a dataset like the following.

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

I need to plot the differences in scores between each group, with effect sizes for each comparison (either Cohen's d or Hedge's g would work).

The code below gets me everything I want on the plot, but the effect size.

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

Question

What code (and/or package) do I need to automatically put an effect size on each comparison?

Any advice or guidance is appreciated.

Nick Byrd
  • 163
  • 1
  • 14

1 Answers1

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)

It manually calculates the Cohen's d for the two groups and puts it in the plot.

enter image description here

Vons
  • 3,277
  • 2
  • 16
  • 19
  • 1
    Thanks! This does work on the sample data I provided. Alas, I am unable to get it to work on the actual data, which has many more rows (depending on the study) and different variable names (e.g., Groups are not labeled "A" and "B"). Will have to figure out how to make this generalizable to datasets of different sizes and names. – Nick Byrd Mar 04 '21 at 16:10
  • @NickByrd You should be able to modify the code to get it to work. Good luck! – Vons Mar 05 '21 at 19:54
  • @ Stacker it would be good if you could update your code, if at all possible – user330 Mar 05 '21 at 22:11