easystats / effectsize

:dragon: Compute and work with indices of effect size and standardized parameters
https://easystats.github.io/effectsize/
Other
338 stars 24 forks source link

One-sample cohens_d() has attribute paired == TRUE #645

Closed einGlasRotwein closed 5 months ago

einGlasRotwein commented 5 months ago

Dear effectsize team,

I noticed that in the one sample case, cohens_d() has the attribute paired == TRUE, see:

d_sepal_length <- cohens_d(iris$Sepal.Length, mu = 5)
attr(d_sepal_length, "paired")
# [1] TRUE

I was wondering whether that is intentional and if so, why it makes sense to treat one sample tests as paired. I understand that a paired test is essentially a one sample test, but did not think of a one sample test as paired (i.e., the reverse). Maybe I'm missing something, but in effectsize, there is no equivalent of ttest_obj$method, which explicitly names the test as e.g., "One Sample t-test", "Welch Two Sample t-test", or "Paired t-test?

To give a little bit of context, the reason why I stumbled across this is that I have a function that formats t.test() and cohens_d() results in my Quarto document. To prevent errors, it is also supposed to check whether t.test() and cohens_d() are both (not) paired, and I was wondering how to best handle the one sample case.

Thank you in advance for any clarification! Juli

mattansb commented 5 months ago

I didn't know anyone was looking at the attributes 😅

The basic idea here was the since $d_z$ for paired samples is just a one-sample $d$ on the pairwise differences, so in the underlying code both cases get pushed into the same internal functionality.

I have fixed the attribute to only reflect paired inputs (even though the outputs are the same).

library(effectsize)
is.paired <- \(x) isTRUE(attr(x, "paired"))

(d1 <- cohens_d(Pair(extra[group == 1], extra[group == 2]) ~ 1, 
                data = sleep))
#> For paired samples, 'repeated_measures_d()' provides more options.
#> Cohen's d |         95% CI
#> --------------------------
#> -1.28     | [-2.12, -0.41]
is.paired(d1)
#> [1] TRUE

(d2 <- cohens_d(extra[group == 1] - extra[group == 2] ~ 1, 
                data = sleep))
#> Cohen's d |         95% CI
#> --------------------------
#> -1.28     | [-2.12, -0.41]
is.paired(d2)
#> [1] FALSE

Created on 2024-06-13 with reprex v2.1.0