|
5 | 5 | ## because I can never remember how to do these otherwise.
|
6 | 6 |
|
7 | 7 |
|
8 |
| -calc.cohens.d <- function(x, y, paired, standardizer="av") { |
9 |
| - ## Returns Cohen's d for paired or unpaired comparisons |
10 |
| - ## standardizer can be one of "z", "av", or "rm" (N/A if paired=F) |
11 |
| - |
12 |
| - if ( paired ) { # within-subjects |
13 |
| - m <- mean(x-y) |
14 |
| - if (standardizer == "z") { |
15 |
| - s <- sd(x-y) |
16 |
| - } else if (standardizer == "av") { |
17 |
| - s <- (sd(x) + sd(y)) / 2 |
18 |
| - } else if (standardizer == "rm") { |
19 |
| - s <- sd(x-y) * sqrt(2 * (1 - cor(x,y))) |
20 |
| - } else { |
21 |
| - stop("Invalid standardizer") |
22 |
| - } |
| 8 | +calc.cohens.d <- function(x, y=NULL, paired=F, standardizer="av") { |
| 9 | + ## Cohen's d for independent, paired, or one-sample comparisons. |
| 10 | + ## standardizer can be one of "z", "av", or "rm" (only matters for |
| 11 | + ## paired samples). |
| 12 | + |
| 13 | + if ( is.null(y) ) { # one-sample |
| 14 | + m <- mean(x) |
| 15 | + s <- sd(x) |
| 16 | + |
| 17 | + } else if ( paired ) { # within-subjects |
| 18 | + m <- mean(x-y) |
| 19 | + if ( standardizer == "z" ) { |
| 20 | + s <- sd(x-y) |
| 21 | + } else if ( standardizer == "av" ) { |
| 22 | + s <- (sd(x) + sd(y)) / 2 |
| 23 | + } else if ( standardizer == "rm" ) { |
| 24 | + r <- cor(x,y) |
| 25 | + s <- sqrt(var(x) + var(y) - 2 * r * sd(x) * sd(y)) / sqrt(2 * (1 - r)) |
| 26 | + } else { |
| 27 | + stop("Invalid standardizer") |
| 28 | + } |
23 | 29 |
|
24 | 30 | } else { # between-subjects
|
25 |
| - m <- mean(x) - mean(y) |
26 |
| - nx <- length(x) |
27 |
| - ny <- length(y) |
28 |
| - s <- sqrt( ((nx-1)*var(x) + (ny-1)*var(y)) / (nx + ny - 2) ) |
| 31 | + m <- mean(x) - mean(y) |
| 32 | + nx <- length(x) |
| 33 | + ny <- length(y) |
| 34 | + s <- sqrt( ((nx-1)*var(x) + (ny-1)*var(y)) / (nx + ny - 2) ) |
29 | 35 | }
|
30 | 36 |
|
31 |
| - return(m/s) |
| 37 | + return(m/s) |
32 | 38 | }
|
33 | 39 |
|
34 | 40 |
|
35 |
| -calc.hedges.g <- function(x, y, paired, ...) { |
36 |
| - ## Calculate Hedges' g for paired or unpaired comparisons |
37 |
| - d <- calc.cohens.d(x, y, paired, ...) |
38 |
| - if ( paired ) { # within-subjects |
39 |
| - dof <- length(x) - 1 |
40 |
| - J <- 1 - ( 3 / (4 * dof - 1) ) |
41 |
| - } else { # between-subjects |
42 |
| - J <- 1 - ( 3 / (4 * (length(x) + length(y)) - 9) ) |
43 |
| - } |
44 |
| - return(d * J) |
| 41 | +calc.hedges.g <- function(x, y=NULL, paired=F, ...) { |
| 42 | + ## Hedges' g for independent, paired, or one-sample comparisons. |
| 43 | + ## Arguments as per calc.cohens.d function. |
| 44 | + d <- calc.cohens.d(x, y, paired, ...) |
| 45 | + if ( is.null(y) || paired ) { # one-sample or within-subjects |
| 46 | + J <- 1 - (3 / (4 * (length(x) - 1) - 1)) |
| 47 | + } else { # between-subjects |
| 48 | + J <- 1 - (3 / (4 * (length(x) + length(y)) - 9)) |
| 49 | + } |
| 50 | + return(d * J) |
45 | 51 | }
|
0 commit comments