Module stikpetP.effect_sizes.eff_size_jbm_r
Expand source code
import pandas as pd
from scipy.special import comb
def es_jbm_r(data, success=None):
'''
Berry-Johnston-Mielke R
-----------------------
A chance-corrected version of eta-squared, as an effect size measure for a Cochran Q test.
Parameters
----------
data : dataframe
dataframe with a column for each category
success : object, optional
the value that represents a 'success'. If None (default) the first value found will be used as success.
Returns
-------
r : float
the effect size measure value
Notes
-----
The formula used (Berry et al., 2007, pp. 1237,1239; Alexis, 2014):
$$\\Re = 1 - \\frac{\\delta}{\\mu_{\\delta}}$$
With:
$$\\delta = \\left[k\\times\\binom{n}{2}\\right]^{-1}\\times\\sum_{c=1}^k\\sum_{i=1}^{n-1}\\sum_{j=i+1}^n\\left|x_{i,c}-x_{j,c}\\right|$$
$$\\mu_{\\delta}=\\frac{2}{n\\times\\left(n-1\\right)}\\times\\left(\\left(\\sum_{i=1}^n p_i\\right)\\times\\left(n-\\sum_{i=1}^n p_i\\right)-\\sum_{i=1}^n p_i\\times\\left(1-p_i\\right)\\right)$$
$$\\sum_{i=1}^n p_i = \\frac{\\sum_{j=1}^k x_{i,j}}{k}$$
Note that the function uses an equivelant for the calculation of \\(\\delta\\), using:
$$\\sum_{c=1}^k\\sum_{i=1}^{n-1}\\sum_{j=i+1}^n\\left|x_{i,c}-x_{j,c}\\right|=\\sum_{j=1}^k C_j\\times\\left(n-C_j\\right)$$
*Symbols used*
* \\(k\\), the number of categories (factors)
* \\(x_{i,j}\\), the i-th score in category j, 1 = success, 0 = no success.
* \\(n\\), the number of cases
* \\(C_j\\), the number of successes in category j
The original article has in the equation for \\(\\mu_{\\delta}\\) the first factor written as \\(\\frac{2}{k\\times\\left(k - 1\\right)}\\). In personal communication with one of the authors Alexis (2014) indicated this was wrong and \\(n\\) should be used.
References
----------
Alexis. (2014, September 7). Answer to “Effect size of Cochran’s Q.” Cross Validated. https://stats.stackexchange.com/a/114649
Berry, K. J., Johnston, J. E., & Mielke, P. W. (2007). An alternative measure of effect size for Cochran’s Q test for related proportions. *Perceptual and Motor Skills, 104*(3_suppl), 1236–1242. doi:10.2466/pms.104.4.1236-1242
Author
------
Made by P. Stikker
Companion website: https://PeterStatistics.com
YouTube channel: https://www.youtube.com/stikpet
Donations: https://www.patreon.com/bePatron?u=19398076
'''
#Remove rows with missing values and reset index
data = data.dropna()
data = data.reset_index(drop=True)
n = len(data)
k = len(data.columns)
if success is None:
suc = data.iloc[0,0]
else:
suc = success
isSuc = data == suc
cj = isSuc.sum()
ri = isSuc.sum(axis=1)
ns = cj.sum()
pi = ri/k
delta = 1/(k*comb(n, 2)) * (cj*(n - cj)).sum()
mu = 2/(n*(n - 1)) * (pi.sum()*(n - pi.sum()) - (pi*(1 - pi)).sum())
r = 1 - delta/mu
return r
Functions
def es_jbm_r(data, success=None)
-
Berry-Johnston-Mielke R
A chance-corrected version of eta-squared, as an effect size measure for a Cochran Q test.
Parameters
data
:dataframe
- dataframe with a column for each category
success
:object
, optional- the value that represents a 'success'. If None (default) the first value found will be used as success.
Returns
r
:float
- the effect size measure value
Notes
The formula used (Berry et al., 2007, pp. 1237,1239; Alexis, 2014): \Re = 1 - \frac{\delta}{\mu_{\delta}}
With: \delta = \left[k\times\binom{n}{2}\right]^{-1}\times\sum_{c=1}^k\sum_{i=1}^{n-1}\sum_{j=i+1}^n\left|x_{i,c}-x_{j,c}\right| \mu_{\delta}=\frac{2}{n\times\left(n-1\right)}\times\left(\left(\sum_{i=1}^n p_i\right)\times\left(n-\sum_{i=1}^n p_i\right)-\sum_{i=1}^n p_i\times\left(1-p_i\right)\right) \sum_{i=1}^n p_i = \frac{\sum_{j=1}^k x_{i,j}}{k}
Note that the function uses an equivelant for the calculation of \delta, using: \sum_{c=1}^k\sum_{i=1}^{n-1}\sum_{j=i+1}^n\left|x_{i,c}-x_{j,c}\right|=\sum_{j=1}^k C_j\times\left(n-C_j\right)
Symbols used
- k, the number of categories (factors)
- x_{i,j}, the i-th score in category j, 1 = success, 0 = no success.
- n, the number of cases
- C_j, the number of successes in category j
The original article has in the equation for \mu_{\delta} the first factor written as \frac{2}{k\times\left(k - 1\right)}. In personal communication with one of the authors Alexis (2014) indicated this was wrong and n should be used.
References
Alexis. (2014, September 7). Answer to “Effect size of Cochran’s Q.” Cross Validated. https://stats.stackexchange.com/a/114649
Berry, K. J., Johnston, J. E., & Mielke, P. W. (2007). An alternative measure of effect size for Cochran’s Q test for related proportions. Perceptual and Motor Skills, 104(3_suppl), 1236–1242. doi:10.2466/pms.104.4.1236-1242
Author
Made by P. Stikker
Companion website: https://PeterStatistics.com
YouTube channel: https://www.youtube.com/stikpet
Donations: https://www.patreon.com/bePatron?u=19398076Expand source code
def es_jbm_r(data, success=None): ''' Berry-Johnston-Mielke R ----------------------- A chance-corrected version of eta-squared, as an effect size measure for a Cochran Q test. Parameters ---------- data : dataframe dataframe with a column for each category success : object, optional the value that represents a 'success'. If None (default) the first value found will be used as success. Returns ------- r : float the effect size measure value Notes ----- The formula used (Berry et al., 2007, pp. 1237,1239; Alexis, 2014): $$\\Re = 1 - \\frac{\\delta}{\\mu_{\\delta}}$$ With: $$\\delta = \\left[k\\times\\binom{n}{2}\\right]^{-1}\\times\\sum_{c=1}^k\\sum_{i=1}^{n-1}\\sum_{j=i+1}^n\\left|x_{i,c}-x_{j,c}\\right|$$ $$\\mu_{\\delta}=\\frac{2}{n\\times\\left(n-1\\right)}\\times\\left(\\left(\\sum_{i=1}^n p_i\\right)\\times\\left(n-\\sum_{i=1}^n p_i\\right)-\\sum_{i=1}^n p_i\\times\\left(1-p_i\\right)\\right)$$ $$\\sum_{i=1}^n p_i = \\frac{\\sum_{j=1}^k x_{i,j}}{k}$$ Note that the function uses an equivelant for the calculation of \\(\\delta\\), using: $$\\sum_{c=1}^k\\sum_{i=1}^{n-1}\\sum_{j=i+1}^n\\left|x_{i,c}-x_{j,c}\\right|=\\sum_{j=1}^k C_j\\times\\left(n-C_j\\right)$$ *Symbols used* * \\(k\\), the number of categories (factors) * \\(x_{i,j}\\), the i-th score in category j, 1 = success, 0 = no success. * \\(n\\), the number of cases * \\(C_j\\), the number of successes in category j The original article has in the equation for \\(\\mu_{\\delta}\\) the first factor written as \\(\\frac{2}{k\\times\\left(k - 1\\right)}\\). In personal communication with one of the authors Alexis (2014) indicated this was wrong and \\(n\\) should be used. References ---------- Alexis. (2014, September 7). Answer to “Effect size of Cochran’s Q.” Cross Validated. https://stats.stackexchange.com/a/114649 Berry, K. J., Johnston, J. E., & Mielke, P. W. (2007). An alternative measure of effect size for Cochran’s Q test for related proportions. *Perceptual and Motor Skills, 104*(3_suppl), 1236–1242. doi:10.2466/pms.104.4.1236-1242 Author ------ Made by P. Stikker Companion website: https://PeterStatistics.com YouTube channel: https://www.youtube.com/stikpet Donations: https://www.patreon.com/bePatron?u=19398076 ''' #Remove rows with missing values and reset index data = data.dropna() data = data.reset_index(drop=True) n = len(data) k = len(data.columns) if success is None: suc = data.iloc[0,0] else: suc = success isSuc = data == suc cj = isSuc.sum() ri = isSuc.sum(axis=1) ns = cj.sum() pi = ri/k delta = 1/(k*comb(n, 2)) * (cj*(n - cj)).sum() mu = 2/(n*(n - 1)) * (pi.sum()*(n - pi.sum()) - (pi*(1 - pi)).sum()) r = 1 - delta/mu return r