代码之家  ›  专栏  ›  技术社区  ›  RoyBatty

如何获得两个以上因素之间的统计量(cohensd)

r
  •  0
  • RoyBatty  · 技术社区  · 2 年前

    我想同时计算多个因子的内聚值。

    因此,例如,在虹膜数据集中,我们可以很容易地计算刚毛和云芝之间萼片长度的内聚力d,如下所示:

    virginica <- subset(iris, Species =="virginica")
    versicolor <- subset(iris, Species =="versicolor")
    
    cohen.d(virginica$Sepal.Length, versicolor$Sepal.Length)
    

    当然,对于剩下的因素,我们可以再次复制这个过程。

    总之,我想要的是获得所有因素对一的度量,而不是所有因素对彼此的度量。因此,这就像在一个步骤中生成几个内聚体。

    在这种情况下,versicolor vs setosa和versicolor vs virginica。

    0 回复  |  直到 2 年前
        1
  •  2
  •   Kat    2 年前

    我不认为这是你想要的答案,但这是一个可行的答案。

    与其试图让函数适合你的数据,不如让你的数据适合函数。

    首先,找到所有可能的组组合我知道你使用了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 
    #