【问题标题】:Estimate Cohen's d for effect size估计 Cohen's d 的效应大小
【发布时间】:2013-03-15 15:52:19
【问题描述】:

给定两个向量:

x <- rnorm(10, 10, 1)
y <- rnorm(10, 5, 5)

如何计算 Cohen's d 的效应大小?

例如,我想使用 pwr package 来估计具有不等方差的 t 检验的功效,它需要 Cohen 的 d。

【问题讨论】:

    标签: r statistics


    【解决方案1】:

    this linkwikipedia 之后,Cohen 的 t 检验 d 似乎是:

    sigma(分母)在哪里:

    因此,使用您的数据:

    set.seed(45)                        ## be reproducible 
    x <- rnorm(10, 10, 1)                
    y <- rnorm(10, 5, 5)
    
    cohens_d <- function(x, y) {
        lx <- length(x)- 1
        ly <- length(y)- 1
        md  <- abs(mean(x) - mean(y))        ## mean difference (numerator)
        csd <- lx * var(x) + ly * var(y)
        csd <- csd/(lx + ly)
        csd <- sqrt(csd)                     ## common sd computation
    
        cd  <- md/csd                        ## cohen's d
    }
    > res <- cohens_d(x, y)
    > res
    # [1] 0.5199662
    

    【讨论】:

      【解决方案2】:

      有几个包提供了计算 Cohen's d 的功能。例如,您可以使用 lsr 包中的 cohensD 函数:

      library(lsr)
      set.seed(45)
      x <- rnorm(10, 10, 1)
      y <- rnorm(10, 5, 5)
      cohensD(x,y)
      # [1] 0.5199662
      

      【讨论】:

      • 您可以将种子设置为 45 并再次计算并粘贴结果吗? (为了重现性)
      • 其他包含 Cohen 函数的包有:effsizepwr(参见cran.r-project.org/web/packages
      【解决方案3】:

      另一种选择是使用effsize 包。

      library(effsize) 
      set.seed(45) x <- rnorm(10, 10, 1) 
      y <- rnorm(10, 5, 5) 
      cohen.d(x,y)
      # Cohen's d
      # d estimate: 0.5199662 (medium)
      # 95 percent confidence interval:
      #        inf        sup 
      # -0.4353393  1.4752717
      

      【讨论】:

        【解决方案4】:

        另一个较新的选项是使用effectsize,它非常灵活并且还返回置信区间: https://easystats.github.io/effectsize/reference/cohens_d.html

        library(effectsize)
        
        x <- rnorm(10, 10, 1)
        y <- rnorm(10, 5, 5)
        
        # for independent measures design
        cohens_d(x, y)
        #> Cohen's d |        95% CI
        #> -------------------------
        #> 0.77      | [-0.15, 1.67]
        #> 
        #> - Estimated using pooled SD.
        
        # in case design is paired
        cohens_d(x, y, paired = TRUE)
        #> Cohen's d |        95% CI
        #> -------------------------
        #> 0.49      | [-0.19, 1.20]
        

        reprex package (v2.0.0) 于 2021 年 6 月 29 日创建

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-02
          • 2020-09-27
          • 2010-12-17
          • 1970-01-01
          • 1970-01-01
          • 2020-12-24
          相关资源
          最近更新 更多