I want to compute the cohens d value for more than one factor at the same time.
So for example, in the iris dataset we could compute the cohens d for sepal length between setosa and versicolor very easily with:
virginica <- subset(iris, Species =="virginica")
versicolor <- subset(iris, Species =="versicolor")
cohen.d(virginica$Sepal.Length, versicolor$Sepal.Length)
Of course we could replicate this process again for the remaining factor.
In summary, what I want is to obtain the measure of all the factors against one, not all the factors against each other. So it would be like generating several cohensd but just in one step.
In this case, versicolor vs setosa and versicolor vs virginica.
CodePudding user response:
I don't think this is the answer you want, but it is an answer that will work.
Instead of trying to make the function fit your data, make your data fit the function.
First, find all possible combinations of groups–I know you used the Iris data, but it's likely that you're just using that as an example.
library(tidyverse)
library(psych)
library(RcppAlgos)
# find unique pairs
iS = RcppAlgos::comboGeneral(unique(iris$Species),
2, F) # all unique combinations of 2 options
Then use these possible groups and get the effect size between each of them.
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
The effect sizes between each group:
# [[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
#