1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
#' Cohen's *d* and Other Standardized Differences
#'
#' Compute effect size indices for standardized differences: Cohen's *d*,
#' Hedges' *g* and Glass’s *delta* (\eqn{\Delta}). (This function returns the
#' **population** estimate.) Pair with any reported [`stats::t.test()`].
#' \cr\cr
#' Both Cohen's *d* and Hedges' *g* are the estimated the standardized
#' difference between the means of two populations. Hedges' *g* provides a bias
#' correction (using the exact method) to Cohen's *d* for small sample sizes.
#' For sample sizes > 20, the results for both statistics are roughly
#' equivalent. Glass’s *delta* is appropriate when the standard deviations are
#' significantly different between the populations, as it uses only the *second*
#' group's standard deviation.
#'
#' @param x,y A numeric vector, or a character name of one in `data`.
#' Any missing values (`NA`s) are dropped from the resulting vector.
#' `x` can also be a formula (see [`stats::t.test()`]), in which case `y` is
#' ignored.
#' @param alternative a character string specifying the alternative hypothesis;
#' Controls the type of CI returned: `"two.sided"` (default, two-sided CI),
#' `"greater"` or `"less"` (one-sided CI). Partial matching is allowed (e.g.,
#' `"g"`, `"l"`, `"two"`...). See *One-Sided CIs* in [effectsize_CIs].
#' @param data An optional data frame containing the variables.
#' @param pooled_sd If `TRUE` (default), a [sd_pooled()] is used (assuming equal
#' variance). Else the mean SD from both groups is used instead.
#' @param paired If `TRUE`, the values of `x` and `y` are considered as paired.
#' This produces an effect size that is equivalent to the one-sample effect
#' size on `x - y`.
#' @param ... Arguments passed to or from other methods. When `x` is a formula,
#' these can be `subset` and `na.action`.
#' @inheritParams chisq_to_phi
#' @inheritParams eta_squared
#' @inheritParams stats::t.test
#'
#' @note The indices here give the population estimated standardized difference.
#' Some statistical packages give the sample estimate instead (without
#' applying Bessel's correction).
#'
#' @details
#' Set `pooled_sd = FALSE` for effect sizes that are to accompany a Welch's
#' *t*-test (Delacre et al, 2021).
#'
#' @inheritSection effectsize_CIs Confidence (Compatibility) Intervals (CIs)
#' @inheritSection effectsize_CIs CIs and Significance Tests
#'
#' @return A data frame with the effect size ( `Cohens_d`, `Hedges_g`,
#' `Glass_delta`) and their CIs (`CI_low` and `CI_high`).
#'
#' @family standardized differences
#' @seealso [sd_pooled()], [t_to_d()], [r_to_d()]
#'
#' @examples
#' \donttest{
#' data(mtcars)
#' mtcars$am <- factor(mtcars$am)
#'
#' # Two Independent Samples ----------
#'
#' (d <- cohens_d(mpg ~ am, data = mtcars))
#' # Same as:
#' # cohens_d("mpg", "am", data = mtcars)
#' # cohens_d(mtcars$mpg[mtcars$am=="0"], mtcars$mpg[mtcars$am=="1"])
#'
#' # More options:
#' cohens_d(mpg ~ am, data = mtcars, pooled_sd = FALSE)
#' cohens_d(mpg ~ am, data = mtcars, mu = -5)
#' cohens_d(mpg ~ am, data = mtcars, alternative = "less")
#' hedges_g(mpg ~ am, data = mtcars)
#' glass_delta(mpg ~ am, data = mtcars)
#'
#'
#' # One Sample ----------
#'
#' cohens_d(wt ~ 1, data = mtcars)
#'
#' # same as:
#' # cohens_d("wt", data = mtcars)
#' # cohens_d(mtcars$wt)
#'
#' # More options:
#' cohens_d(wt ~ 1, data = mtcars, mu = 3)
#' hedges_g(wt ~ 1, data = mtcars, mu = 3)
#'
#'
#' # Paired Samples ----------
#'
#' data(sleep)
#'
#' cohens_d(Pair(extra[group == 1], extra[group == 2]) ~ 1, data = sleep)
#'
#' # same as:
#' # cohens_d(sleep$extra[sleep$group == 1], sleep$extra[sleep$group == 2], paired = TRUE)
#'
#' # More options:
#' cohens_d(Pair(extra[group == 1], extra[group == 2]) ~ 1, data = sleep, mu = -1)
#' hedges_g(Pair(extra[group == 1], extra[group == 2]) ~ 1, data = sleep)
#'
#'
#' # Interpretation -----------------------
#' interpret_cohens_d(-1.48, rules = "cohen1988")
#' interpret_hedges_g(-1.48, rules = "sawilowsky2009")
#' interpret_glass_delta(-1.48, rules = "gignac2016")
#' # Or:
#' interpret(d, rules = "sawilowsky2009")
#'
#' # Common Language Effect Sizes
#' d_to_u3(1.48)
#' # Or:
#' print(d, append_CLES = TRUE)
#' }
#'
#' @references
#' - Algina, J., Keselman, H. J., & Penfield, R. D. (2006). Confidence intervals
#' for an effect size when variances are not equal. Journal of Modern Applied
#' Statistical Methods, 5(1), 2.
#'
#' - Cohen, J. (1988). Statistical power analysis for the behavioral
#' sciences (2nd Ed.). New York: Routledge.
#'
#' - Delacre, M., Lakens, D., Ley, C., Liu, L., & Leys, C. (2021, May 7). Why
#' Hedges’ g*s based on the non-pooled standard deviation should be reported
#' with Welch's t-test. \doi{10.31234/osf.io/tu6mp}
#'
#' - Hedges, L. V. & Olkin, I. (1985). Statistical methods for
#' meta-analysis. Orlando, FL: Academic Press.
#'
#' - Hunter, J. E., & Schmidt, F. L. (2004). Methods of meta-analysis:
#' Correcting error and bias in research findings. Sage.
#'
#' @importFrom stats var model.frame
#' @export
cohens_d <- function(x, y = NULL, data = NULL,
pooled_sd = TRUE, mu = 0, paired = FALSE,
ci = 0.95, alternative = "two.sided",
verbose = TRUE, ...) {
var.equal <- eval.parent(match.call()[["var.equal"]])
if (!is.null(var.equal)) pooled_sd <- var.equal
.effect_size_difference(
x,
y = y, data = data,
type = "d",
pooled_sd = pooled_sd, mu = mu, paired = paired,
ci = ci, alternative = alternative,
verbose = verbose,
...
)
}
#' @rdname cohens_d
#' @export
hedges_g <- function(x, y = NULL, data = NULL,
pooled_sd = TRUE, mu = 0, paired = FALSE,
ci = 0.95, alternative = "two.sided",
verbose = TRUE, ...) {
var.equal <- eval.parent(match.call()[["var.equal"]])
if (!is.null(var.equal)) pooled_sd <- var.equal
.effect_size_difference(
x,
y = y, data = data,
type = "g",
pooled_sd = pooled_sd, mu = mu, paired = paired,
ci = ci, alternative = alternative,
verbose = verbose,
...
)
}
#' @rdname cohens_d
#' @export
glass_delta <- function(x, y = NULL, data = NULL,
mu = 0,
ci = 0.95, alternative = "two.sided",
verbose = TRUE, ...) {
.effect_size_difference(
x,
y = y, data = data,
type = "delta",
mu = mu,
ci = ci, alternative = alternative,
verbose = verbose,
pooled_sd = NULL, paired = FALSE,
...
)
}
#' @importFrom stats sd
#' @keywords internal
.effect_size_difference <- function(x, y = NULL, data = NULL,
type = "d",
mu = 0, pooled_sd = TRUE, paired = FALSE,
ci = 0.95, alternative = "two.sided",
verbose = TRUE, ...) {
if (type != "delta") {
if (.is_htest_of_type(x, "t-test")) {
return(effectsize(x, type = type, verbose = verbose, ...))
} else if (.is_BF_of_type(x, c("BFoneSample", "BFindepSample"), "t-squared")) {
return(effectsize(x, ci = ci, verbose = verbose, ...))
}
}
alternative <- .match.alt(alternative)
out <- .get_data_2_samples(x, y, data, paired = paired, verbose = verbose, ...)
x <- out[["x"]]
y <- out[["y"]]
if (is.null(y)) {
if (type == "delta") {
insight::format_error("For Glass' Delta, please provide data from two samples.")
}
y <- 0
paired <- TRUE
}
# Compute index
if (paired) {
d <- mean(x - y)
n <- length(x)
s <- stats::sd(x - y)
hn <- 1 / n
se <- s / sqrt(n)
df <- n - 1
pooled_sd <- NULL
} else {
d <- mean(x) - mean(y)
s1 <- stats::sd(x)
s2 <- stats::sd(y)
n1 <- length(x)
n2 <- length(y)
n <- n1 + n2
if (type %in% c("d", "g")) {
if (pooled_sd) {
s <- suppressWarnings(sd_pooled(x, y))
hn <- (1 / n1 + 1 / n2)
se <- s * sqrt(1 / n1 + 1 / n2)
df <- n - 2
} else {
s <- sqrt((s1^2 + s2^2) / 2)
hn <- (2 * (n2 * s1^2 + n1 * s2^2)) / (n1 * n2 * (s1^2 + s2^2))
se1 <- sqrt(s1^2 / n1)
se2 <- sqrt(s2^2 / n2)
se <- sqrt(se1^2 + se2^2)
df <- se^4 / (se1^4 / (n1 - 1) + se2^4 / (n2 - 1))
}
} else if (type == "delta") {
pooled_sd <- NULL
s <- s2
hn <- 1 / n2 + s1^2 / (n1 * s2^2)
se <- (s2 * sqrt(1 / n2 + s1^2 / (n1 * s2^2)))
df <- n2 - 1
}
}
out <- data.frame(d = (d - mu) / s)
types <- c("d" = "Cohens_d", "g" = "Hedges_g", "delta" = "Glass_delta")
colnames(out) <- types[type]
if (.test_ci(ci)) {
# Add cis
out$CI <- ci
ci.level <- .adjust_ci(ci, alternative)
t <- (d - mu) / se
ts <- .get_ncp_t(t, df, ci.level)
out$CI_low <- ts[1] * sqrt(hn)
out$CI_high <- ts[2] * sqrt(hn)
ci_method <- list(method = "ncp", distribution = "t")
out <- .limit_ci(out, alternative, -Inf, Inf)
} else {
ci_method <- alternative <- NULL
}
if (type == "g") {
J <- exp(lgamma(df / 2) - log(sqrt(df / 2)) - lgamma((df - 1) / 2)) # exact method
out[, colnames(out) %in% c("Hedges_g", "CI_low", "CI_high")] <-
out[, colnames(out) %in% c("Hedges_g", "CI_low", "CI_high")] * J
}
class(out) <- c("effectsize_difference", "effectsize_table", "see_effectsize_table", class(out))
.someattributes(out) <- .nlist(
paired, pooled_sd, mu, ci, ci_method, alternative,
approximate = FALSE
)
return(out)
}
|