Skip to content

Commit

Permalink
effect sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
remrama committed Oct 7, 2024
1 parent c6f0b26 commit ef8b121
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Click on the link below and navigate to the notebooks/ folder to run a collectio
:widths: auto

====== ===== ============= ======= ============= ========= ====== =======
T dof alternative p_val CI95 cohen-d BF10 power
T dof alternative p_val CI95 cohen_d BF10 power
====== ===== ============= ======= ============= ========= ====== =======
-3.401 58 two-sided 0.001 [-1.68 -0.43] 0.878 26.155 0.917
====== ===== ============= ======= ============= ========= ====== =======
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Quick start
:widths: auto

====== ===== ============= ======= ============= ========= ====== =======
T dof alternative p_val CI95 cohen-d BF10 power
T dof alternative p_val CI95 cohen_d BF10 power
====== ===== ============= ======= ============= ========= ====== =======
-3.401 58 two-sided 0.001 [-1.68 -0.43] 0.878 26.155 0.917
====== ===== ============= ======= ============= ========= ====== =======
Expand Down
6 changes: 3 additions & 3 deletions notebooks/03_EffectSizes.ipynb

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions src/pingouin/effsize.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,8 @@ def convert_effsize(ef, input_type, output_type, nx=None, ny=None):
* ``'cohen'``: Unbiased Cohen d
* ``'hedges'``: Hedges g
* ``'pointbiserialr'``: Point-biserial correlation
* ``'eta-square'``: Eta-square
* ``'odds-ratio'``: Odds ratio
* ``'eta_square'``: Eta-square
* ``'odds_ratio'``: Odds ratio
* ``'AUC'``: Area Under the Curve
* ``'none'``: pass-through (return ``ef``)
Expand Down Expand Up @@ -570,7 +570,7 @@ def convert_effsize(ef, input_type, output_type, nx=None, ny=None):
>>> import pingouin as pg
>>> d = .45
>>> eta = pg.convert_effsize(d, 'cohen', 'eta-square')
>>> eta = pg.convert_effsize(d, 'cohen', 'eta_square')
>>> print(eta)
0.048185603807257595
Expand Down Expand Up @@ -630,10 +630,10 @@ def convert_effsize(ef, input_type, output_type, nx=None, ny=None):
else:
a = 4
return d / np.sqrt(d**2 + a)
elif ot == "eta-square":
elif ot == "eta_square":
# Cohen 1988
return (d / 2) ** 2 / (1 + (d / 2) ** 2)
elif ot == "odds-ratio":
elif ot == "odds_ratio":
# Borenstein et al. 2009
return np.exp(d * np.pi / np.sqrt(3))
elif ot == "r":
Expand Down Expand Up @@ -670,8 +670,8 @@ def compute_effsize(x, y, paired=False, eftype="cohen"):
* ``'hedges'``: Hedges g
* ``'r'``: Pearson correlation coefficient
* ``'pointbiserialr'``: Point-biserial correlation
* ``'eta-square'``: Eta-square
* ``'odds-ratio'``: Odds ratio
* ``'eta_square'``: Eta-square
* ``'odds_ratio'``: Odds ratio
* ``'AUC'``: Area Under the Curve
* ``'CLES'``: Common Language Effect Size
Expand Down
12 changes: 6 additions & 6 deletions src/pingouin/pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def pairwise_tests(
* ``'cohen'``: Unbiased Cohen d
* ``'hedges'``: Hedges g
* ``'r'``: Pearson correlation coefficient
* ``'eta-square'``: Eta-square
* ``'odds-ratio'``: Odds ratio
* ``'eta_square'``: Eta-square
* ``'odds_ratio'``: Odds ratio
* ``'AUC'``: Area Under the Curve
* ``'CLES'``: Common Language Effect Size
correction : string or boolean
Expand Down Expand Up @@ -787,8 +787,8 @@ def pairwise_tukey(data=None, dv=None, between=None, effsize="hedges"):
* ``'cohen'``: Unbiased Cohen d
* ``'hedges'``: Hedges g
* ``'r'``: Pearson correlation coefficient
* ``'eta-square'``: Eta-square
* ``'odds-ratio'``: Odds ratio
* ``'eta_square'``: Eta-square
* ``'odds_ratio'``: Odds ratio
* ``'AUC'``: Area Under the Curve
* ``'CLES'``: Common Language Effect Size
Expand Down Expand Up @@ -951,8 +951,8 @@ def pairwise_gameshowell(data=None, dv=None, between=None, effsize="hedges"):
* ``'cohen'``: Unbiased Cohen d
* ``'hedges'``: Hedges g
* ``'r'``: Pearson correlation coefficient
* ``'eta-square'``: Eta-square
* ``'odds-ratio'``: Odds ratio
* ``'eta_square'``: Eta-square
* ``'odds_ratio'``: Odds ratio
* ``'AUC'``: Area Under the Curve
* ``'CLES'``: Common Language Effect Size
Expand Down
20 changes: 10 additions & 10 deletions src/pingouin/parametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def ttest(x, y, paired=False, alternative="two-sided", correction="auto", r=0.70
* ``'alternative'``: alternative of the test
* ``'p_val'``: p-value
* ``'CI95'``: confidence intervals of the difference in means
* ``'cohen-d'``: Cohen d effect size
* ``'cohen_d'``: Cohen d effect size
* ``'BF10'``: Bayes Factor of the alternative hypothesis
* ``'power'``: achieved power of the test ( = 1 - type II error)
Expand Down Expand Up @@ -143,21 +143,21 @@ def ttest(x, y, paired=False, alternative="two-sided", correction="auto", r=0.70
>>> from pingouin import ttest
>>> x = [5.5, 2.4, 6.8, 9.6, 4.2]
>>> ttest(x, 4).round(2)
T dof alternative p_val CI95 cohen-d BF10 power
T dof alternative p_val CI95 cohen_d BF10 power
T_test 1.4 4 two-sided 0.23 [2.32, 9.08] 0.62 0.766 0.19
2. One sided paired T-test.
>>> pre = [5.5, 2.4, 6.8, 9.6, 4.2]
>>> post = [6.4, 3.4, 6.4, 11., 4.8]
>>> ttest(pre, post, paired=True, alternative='less').round(2)
T dof alternative p_val CI95 cohen-d BF10 power
T dof alternative p_val CI95 cohen_d BF10 power
T_test -2.31 4 less 0.04 [-inf, -0.05] 0.25 3.122 0.12
Now testing the opposite alternative hypothesis
>>> ttest(pre, post, paired=True, alternative='greater').round(2)
T dof alternative p_val CI95 cohen-d BF10 power
T dof alternative p_val CI95 cohen_d BF10 power
T_test -2.31 4 greater 0.96 [-1.35, inf] 0.25 0.32 0.02
3. Paired T-test with missing values.
Expand All @@ -166,7 +166,7 @@ def ttest(x, y, paired=False, alternative="two-sided", correction="auto", r=0.70
>>> pre = [5.5, 2.4, np.nan, 9.6, 4.2]
>>> post = [6.4, 3.4, 6.4, 11., 4.8]
>>> ttest(pre, post, paired=True).round(3)
T dof alternative p_val CI95 cohen-d BF10 power
T dof alternative p_val CI95 cohen_d BF10 power
T_test -5.902 3 two-sided 0.01 [-1.5, -0.45] 0.306 7.169 0.073
Compare with SciPy
Expand All @@ -181,21 +181,21 @@ def ttest(x, y, paired=False, alternative="two-sided", correction="auto", r=0.70
>>> x = np.random.normal(loc=7, size=20)
>>> y = np.random.normal(loc=4, size=20)
>>> ttest(x, y)
T dof alternative p_val CI95 cohen-d BF10 power
T dof alternative p_val CI95 cohen_d BF10 power
T_test 9.106452 38 two-sided 4.306971e-11 [2.64, 4.15] 2.879713 1.366e+08 1.0
5. Independent two-sample T-test with unequal sample size. A Welch's T-test is used.
>>> np.random.seed(123)
>>> y = np.random.normal(loc=6.5, size=15)
>>> ttest(x, y)
T dof alternative p_val CI95 cohen-d BF10 power
T dof alternative p_val CI95 cohen_d BF10 power
T_test 1.996537 31.567592 two-sided 0.054561 [-0.02, 1.65] 0.673518 1.469 0.481867
6. However, the Welch's correction can be disabled:
>>> ttest(x, y, correction=False)
T dof alternative p_val CI95 cohen-d BF10 power
T dof alternative p_val CI95 cohen_d BF10 power
T_test 1.971859 33 two-sided 0.057056 [-0.03, 1.66] 0.673518 1.418 0.481867
Compare with SciPy
Expand Down Expand Up @@ -320,14 +320,14 @@ def ttest(x, y, paired=False, alternative="two-sided", correction="auto", r=0.70
"T": tval,
"p_val": pval,
"alternative": alternative,
"cohen-d": abs(d),
"cohen_d": abs(d),
ci_name: [ci],
"power": power,
"BF10": bf,
}

# Convert to dataframe
col_order = ["T", "dof", "alternative", "p_val", ci_name, "cohen-d", "BF10", "power"]
col_order = ["T", "dof", "alternative", "p_val", ci_name, "cohen_d", "BF10", "power"]
stats = pd.DataFrame(stats, columns=col_order, index=["T_test"])
return _postprocess_dataframe(stats)

Expand Down
4 changes: 2 additions & 2 deletions src/pingouin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ def _check_eftype(eftype):
"cohen",
"r",
"pointbiserialr",
"eta-square",
"odds-ratio",
"eta_square",
"odds_ratio",
"auc",
"cles",
]:
Expand Down
14 changes: 7 additions & 7 deletions tests/test_effsize.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ def test_convert_effsize(self):
assert round(cef(d, "cohen", "pointbiserialr"), 4) == 0.1961
cef(d, "cohen", "pointbiserialr", nx=10, ny=12) # When nx and ny are specified
assert np.allclose(cef(1.002549, "cohen", "pointbiserialr"), 0.4481248) # R
assert round(cef(d, "cohen", "eta-square"), 4) == 0.0385
assert round(cef(d, "cohen", "odds-ratio"), 4) == 2.0658
assert round(cef(d, "cohen", "eta_square"), 4) == 0.0385
assert round(cef(d, "cohen", "odds_ratio"), 4) == 2.0658
cef(d, "cohen", "hedges", nx=10, ny=10)
cef(d, "cohen", "pointbiserialr")
cef(d, "cohen", "hedges")
Expand All @@ -245,8 +245,8 @@ def test_convert_effsize(self):
assert cef(rpb, "pointbiserialr", "none") == rpb
assert round(cef(rpb, "pointbiserialr", "cohen"), 4) == 1.7107
assert np.allclose(cef(0.4481248, "pointbiserialr", "cohen"), 1.002549)
assert round(cef(rpb, "pointbiserialr", "eta-square"), 4) == 0.4225
assert round(cef(rpb, "pointbiserialr", "odds-ratio"), 4) == 22.2606
assert round(cef(rpb, "pointbiserialr", "eta_square"), 4) == 0.4225
assert round(cef(rpb, "pointbiserialr", "odds_ratio"), 4) == 22.2606
# Using actual values
np.random.seed(42)
x1, y1 = np.random.multivariate_normal(mean=[1, 2], cov=[[1, 0.5], [0.5, 1]], size=100).T
Expand All @@ -270,15 +270,15 @@ def test_convert_effsize(self):
with pytest.raises(ValueError):
cef(d, "coucou", "hibou")
with pytest.raises(ValueError):
cef(d, "AUC", "eta-square")
cef(d, "AUC", "eta_square")

def test_compute_effsize(self):
"""Test function compute_effsize"""
compute_effsize(x=x, y=y, eftype="cohen", paired=False)
compute_effsize(x=x, y=y, eftype="AUC", paired=True)
compute_effsize(x=x, y=y, eftype="r", paired=False)
compute_effsize(x=x, y=y, eftype="odds-ratio", paired=False)
compute_effsize(x=x, y=y, eftype="eta-square", paired=False)
compute_effsize(x=x, y=y, eftype="odds_ratio", paired=False)
compute_effsize(x=x, y=y, eftype="eta_square", paired=False)
compute_effsize(x=x, y=y, eftype="cles", paired=False)
compute_effsize(x=x, y=y, eftype="pointbiserialr", paired=False)
compute_effsize(x=x, y=y, eftype="none", paired=False)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_parametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_ttest(self):
tt = ttest(a, a, paired=True)
assert str(tt.loc["T_test", "T"]) == str(np.nan)
assert str(tt.loc["T_test", "p_val"]) == str(np.nan)
assert tt.loc["T_test", "cohen-d"] == 0.0
assert tt.loc["T_test", "cohen_d"] == 0.0
assert tt.loc["T_test", "BF10"] == str(np.nan)

# 4) Independent two-samples, equal variance (no correction)
Expand Down

0 comments on commit ef8b121

Please sign in to comment.