Estimate Cohen's d for effect size

前端 未结 3 882
北恋
北恋 2021-02-01 08:24

given two vectors:

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

How to calculate Cohen\'s d for effect size?

For example, I want to

3条回答
  •  一整个雨季
    2021-02-01 09:01

    Following this link and wikipedia, Cohen's d for a t-test seems to be:

    enter image description here

    Where sigma (denominator) is:

    enter image description here

    So, with your data:

    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
    

提交回复
热议问题