Search code examples
reffect

Cohen's d between two scores for multiple factor levels


I have 9 different factor levels (grade) with two different scores (score 1, and score 2).

I get the effect size between those two scores without a problem by using the following code:

cohens_d(df$score1, df$score2)

What I want to do is to calculate the effect size between these two scores for each factor level. The following code does not work:

df %>%
group_by(class) %>%
cohens_d(score1, score2)

So far I have tried lsr, effsize, and effectsize packages, but none of them worked.

Edit 2: Here is the error I got:

Error in .deal_with_cohens_d_arguments(x, y, data) : object 'score1' not found

Looks like the first two lines of my code, which were supposed to select the data and group by factor levels, does not work.

df %>%
group_by(class) %>%

Thanks in advance.

Here is the result of dput(head(df, 20)):

structure(list(`Filter 1` = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), ID = 301:320, 
    Grade = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("class1", 
    "class2", "class3", "class4", "class5", 
    "class6", "class7", "class8", "class9"), class = "factor"), 
    Class = structure(c(9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
    9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("8/D", 
    "8/G", "8/A", "8/B", "7/E", "7/G", "6/H", "6/D", "5/G", "5/H", 
    "5CK", "5AA", "adults", "7/D"), class = "factor"), birthdate = c(2005, 
    2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 
    2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005), Q1 = c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    0L, 1L, 1L, 1L), Q2 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 1, 0, 0, 0, 0, 0, 0), Q3 = c(1, 1, 0, 1, 0, 0, 0, 1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 0, 0), Q48 = c(0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 
    0, 1, 0, 0, 0, 0, 1), Q49 = c(0, 1, 0, 1, 0, 0, 0, 1, 1, 
    1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0), Q50 = c(0, 0, 0, 0, 0, 
    0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0), totalscore = c(30, 
    16, 19, 15, 15, 24, 17, 32, 36, 20, 19, 42, 12, 18, 40, 25, 
    6, 9, 21, 15), transformed = c(-0.489961597251375, -1.6416086159321, 
    -1.29700091350809, -1.78769329956576, -1.78769329956576, 
    -1.02498741152925, -1.52396770612663, -0.347537009605224, 
    0.0378608125108563, -1.19639199386294, -1.29700091350809, 
    0.72280668820074, -2.01768420296626, -1.40146915348254, 0.471079599763142, 
    -0.94308469425977, -2.93312341654017, -2.57351548813345, 
    -1.13020640446587, -1.78769329956576), score2= c(8, 
    5, 4, 6, 2, 6, 4, 9, 9, 8, 6, 10, 2, 7, 11, 8, 2, 1, 5, 5
    ), score1= c(8, 5, 4, 3, 4, 9, 5, 10, 11, 5, 5, 13, 
    2, 4, 12, 5, 3, 2, 6, 4)), row.names = c(NA, -20L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(Grade = structure(1L, .Label = c("class1", 
"class2", "class3", "class4", "class5", 
"class6", "class7", "class8", "class9"), class = "factor"), 
    .rows = list(1:20)), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE))

Solution

  • Here is a way. The rule is simple, if you want to compute something in a dplyr pipe, use mutate or summarise, do not pipe directly to the function you want to use.

    library(dplyr)
    library(apa)
    
    df %>%
      group_by(Class) %>%
      mutate(effect = cohens_d(score1, score2))