Home > Software engineering >  Use `dplyr` to run all pairwise comparisons and add Cohen's *d*
Use `dplyr` to run all pairwise comparisons and add Cohen's *d*

Time:09-15

I am using rstatix to perform multiple t-tests on a dataset which works very well, but I also need Cohen's d. rstatix also includes a function to calculate Cohen's d, but it requires the original dataset, not the table generated by the t_test function.

library(tidyverse)
library(rstatix)

iris %>%
    t_test(Petal.Width ~ Species, paired = FALSE, var.equal = FALSE)

Which gives me:

# A tibble: 3 × 10
  .y.         group1     group2        n1    n2 statistic    df        p    p.adj p.adj.signif
* <chr>       <chr>      <chr>      <int> <int>     <dbl> <dbl>    <dbl>    <dbl> <chr>       
1 Petal.Width setosa     versicolor    50    50     -34.1  74.8 2.72e-47 5.44e-47 ****        
2 Petal.Width setosa     virginica     50    50     -42.8  63.1 2.44e-48 7.32e-48 ****        
3 Petal.Width versicolor virginica     50    50     -14.6  89.0 2.11e-25 2.11e-25 ****     

This output would be perfect if it had an additional column with Cohen's d for each t-test. How do I get Cohen's d?

CodePudding user response:

We may do a join

library(dplyr)
library(rstatix)
iris %>% 
  cohens_d(Petal.Width ~ Species, paired = TRUE) %>% 
  inner_join(iris %>%
    t_test(Petal.Width ~ Species, paired = FALSE, var.equal = FALSE)
)
  • Related