Cohen's d 在多个因子水平的两个分数之间

Cohen's d between two scores for multiple factor levels

我有 9 个不同的因素水平(等级),有两个不同的分数(分数 1 和分数 2)。

我使用以下代码毫无问题地获得了这两个分数之间的效果大小:

cohens_d(df$score1, df$score2)

我想做的是计算每个因子水平的这两个分数之间的效果大小。以下代码不起作用:

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

到目前为止,我已经尝试了 lsr、effsize 和 effectsize 软件包,但其中 none 有效。

编辑 2:这是我得到的错误:

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

我的代码的前两行本应 select 数据并按因子水平分组,但似乎不起作用。

df %>%
group_by(class) %>%

提前致谢。

这里是 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,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 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 
    0, 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))

这是一个方法。规则很简单,如果你想在 dplyr 管道中计算某些东西,使用 mutatesummarise,不要直接管道到你想使用的函数。

library(dplyr)
library(apa)

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

如果此函数适用于整个数据框:

cohens_d(df$score1, df$score2)

然后考虑 by 到 运行 它跨因子水平到 return 一个 cohens_d 结果的命名列表,其中列表中的项目数等于因子数级别:

cohens_d_list <- by(df, df$grade, function(sub) 
                       cohens_d(sub$score1, sub$score2)
                 )

cohens_d_list$class1
cohens_d_list$class2
cohens_d_list$class3
...