我不认为这是你想要的答案,但这是一个可行的答案。
与其试图让函数适合你的数据,不如让你的数据适合函数。
首先,找到所有可能的组组合我知道你使用了Iris数据,但很可能你只是在用它作为一个例子。
library(tidyverse)
library(psych)
library(RcppAlgos)
# find unique pairs
iS = RcppAlgos::comboGeneral(unique(iris$Species),
2, F) # all unique combinations of 2 options
然后使用这些可能的组,得到每个组之间的效果大小。
res = map(1:nrow(iS),
.f = function(x){
filter(iris, Species %in% c(iS[x, 1], iS[x, 2])) %>%
cohen.d(., group = "Species")
})
names(res) <- paste0(iS[,1], "-",iS[,2])
res
每组之间的效果大小:
# [[1]]
# Call: cohen.d(x = ., group = "Species")
# Cohen d statistic of difference between two means
# lower effect upper
# Sepal.Length 1.55 2.13 2.69
# Sepal.Width -2.45 -1.91 -1.36
# Petal.Length 6.35 7.98 9.57
# Petal.Width 5.47 6.89 8.27
#
# Multivariate (Mahalanobis) distance between groups
# [1] 10
# r equivalent of difference between two means
# Sepal.Length Sepal.Width Petal.Length Petal.Width
# 0.73 -0.69 0.97 0.96
#
# [[2]]
# Call: cohen.d(x = ., group = "Species")
# Cohen d statistic of difference between two means
# lower effect upper
# Sepal.Length 2.38 3.11 3.83
# Sepal.Width -1.77 -1.30 -0.83
# Petal.Length 8.01 10.10 12.08
# Petal.Width 6.89 8.64 10.36
#
# Multivariate (Mahalanobis) distance between groups
# [1] 14
# r equivalent of difference between two means
# Sepal.Length Sepal.Width Petal.Length Petal.Width
# 0.84 -0.55 0.98 0.97
#
# [[3]]
# Call: cohen.d(x = ., group = "Species")
# Cohen d statistic of difference between two means
# lower effect upper
# Sepal.Length 0.68 1.14 1.58
# Sepal.Width 0.23 0.65 1.06
# Petal.Length 1.90 2.55 3.18
# Petal.Width 2.25 2.95 3.65
#
# Multivariate (Mahalanobis) distance between groups
# [1] 3.8
# r equivalent of difference between two means
# Sepal.Length Sepal.Width Petal.Length Petal.Width
# 0.49 0.31 0.79 0.83
#