我希望同时计算多个因子的cohens值。
因此,例如,在虹膜数据集中,我们可以很容易地计算出刚毛和云芝之间萼片长度的cohens d:
virginica <- subset(iris, Species =="virginica")
versicolor <- subset(iris, Species =="versicolor")
cohen.d(virginica$Sepal.Length, versicolor$Sepal.Length)当然,对于剩下的因素,我们可以再次复制这个过程。
总之,我想要的是衡量每一个因素,而不是所有相互对立的因素。因此,这就像产生几个cohensd,但只是一个步骤。
在这种情况下,云天色对刚毛和云天色对处女。
发布于 2022-03-07 01:00:21
我不认为这是你想要的答案,但这是一个会奏效的答案。
与其尝试使函数适合您的数据,不如让您的数据与该函数相匹配。
首先,查找所有可能的组组合--我知道您使用了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
# https://stackoverflow.com/questions/71374660
复制相似问题