Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# alpha=.05, tail='two-sided', padjust='fdr_bh',
# effsize='hedges')
# print(stats)
# 3 - Interaction within * between
# stats = pairwise_ttests(dv='DV', within='Time', between='Group',
# effects='interaction', data=df, alpha=.05,
# tail='two-sided', padjust='bonf', effsize='eta-square')
# print(stats)
# 4 - all = return all three above
stats = pairwise_ttests(dv='DV', within='Time', between='Group',
effects='all', data=df, alpha=.05,
tail='two-sided', padjust='fdr_by',
effsize='cohen', return_desc=False)
print_table(stats)
for c in covar:
formula += ' + ' + c
model = ols(formula, data=data).fit()
aov = stats.anova_lm(model, typ=2).reset_index()
aov.rename(columns={'index': 'Source', 'sum_sq': 'SS',
'df': 'DF', 'PR(>F)': 'p-unc'}, inplace=True)
aov.at[0, 'Source'] = between
aov['DF'] = aov['DF'].astype(int)
aov[['SS', 'F']] = aov[['SS', 'F']].round(3)
# Export to .csv
if export_filename is not None:
_export_table(aov, export_filename)
return aov
Code inspired by Matlab code from Cyril Pernet and Guillaume
Rousselet [1]_.
Requires scikit-learn.
References
----------
.. [1] Pernet CR, Wilcox R, Rousselet GA. Robust Correlation Analyses:
False Positive and Power Validation Using a New Open Source Matlab
Toolbox. Frontiers in Psychology. 2012;3:606.
doi:10.3389/fpsyg.2012.00606.
"""
# Check that sklearn is installed
from pingouin.utils import _is_sklearn_installed
_is_sklearn_installed(raise_error=True)
from scipy.stats import chi2
from sklearn.covariance import MinCovDet
X = np.column_stack((x, y))
nrows, ncols = X.shape
gval = np.sqrt(chi2.ppf(0.975, 2))
# Compute center and distance to center
center = MinCovDet(random_state=42).fit(X).location_
B = X - center
B2 = B**2
bot = B2.sum(axis=1)
# Loop over rows
dis = np.zeros(shape=(nrows, nrows))
for i in np.arange(nrows):
if bot[i] != 0:
directory. To change that, specify the filename with full path.
Returns
-------
aov : DataFrame
ANCOVA summary ::
'Source' : Names of the factor considered
'SS' : Sums of squares
'DF' : Degrees of freedom
'F' : F-values
'p-unc' : Uncorrected p-values
"""
# Check that stasmodels is installed
from pingouin.utils import _is_statsmodels_installed
_is_statsmodels_installed(raise_error=True)
from statsmodels.api import stats
from statsmodels.formula.api import ols
# Check that covariates are numeric ('float', 'int')
assert all([data[covar[i]].dtype.kind in 'fi' for i in range(len(covar))])
# Fit ANCOVA model
formula = dv + ' ~ C(' + between + ')'
for c in covar:
formula += ' + ' + c
model = ols(formula, data=data).fit()
aov = stats.anova_lm(model, typ=2).reset_index()
aov.rename(columns={'index': 'Source', 'sum_sq': 'SS',
'df': 'DF', 'PR(>F)': 'p-unc'}, inplace=True)
TOST 0.5 5 0.954854
3. One sample TOST
>>> pg.tost(a, y=0, bound=4)
bound dof pval
TOST 4 5 0.825967
"""
x = np.asarray(x)
y = np.asarray(y)
assert isinstance(bound, (int, float)), 'bound must be int or float.'
# T-tests
df_a = ttest(x + bound, y, paired=paired, correction=correction,
tail='greater')
df_b = ttest(x - bound, y, paired=paired, correction=correction,
tail='less')
pval = max(df_a.at['T-test', 'p-val'], df_b.at['T-test', 'p-val'])
# Create output dataframe
stats = {'bound': bound, 'dof': df_a.at['T-test', 'dof'], 'pval': pval}
return pd.DataFrame.from_records(stats, index=['TOST'])
>>> pg.tost(a, b, bound=0.5, paired=True)
bound dof pval
TOST 0.5 5 0.954854
3. One sample TOST
>>> pg.tost(a, y=0, bound=4)
bound dof pval
TOST 4 5 0.825967
"""
x = np.asarray(x)
y = np.asarray(y)
assert isinstance(bound, (int, float)), 'bound must be int or float.'
# T-tests
df_a = ttest(x + bound, y, paired=paired, correction=correction,
tail='greater')
df_b = ttest(x - bound, y, paired=paired, correction=correction,
tail='less')
pval = max(df_a.at['T-test', 'p-val'], df_b.at['T-test', 'p-val'])
# Create output dataframe
stats = {'bound': bound, 'dof': df_a.at['T-test', 'dof'], 'pval': pval}
return pd.DataFrame.from_records(stats, index=['TOST'])
stats[cols_bool] = stats[cols_bool].astype(bool)
# Fill str columns
stats.loc[:, 'A'] = A
stats.loc[:, 'B'] = B
stats.loc[:, 'Contrast'] = col
stats.loc[:, 'Tail'] = tail
stats.loc[:, 'Paired'] = paired
for i in range(stats.shape[0]):
col1, col2 = stats.at[i, 'A'], stats.at[i, 'B']
x = grp_col.get_group(col1).to_numpy(dtype=np.float64)
y = grp_col.get_group(col2).to_numpy(dtype=np.float64)
if parametric:
stat_name = 'T'
df_ttest = ttest(x, y, paired=paired, tail=tail)
stats.at[i, 'BF10'] = df_ttest.at['T-test', 'BF10']
stats.at[i, 'dof'] = df_ttest.at['T-test', 'dof']
else:
if paired:
stat_name = 'W-val'
df_ttest = wilcoxon(x, y, tail=tail)
else:
stat_name = 'U-val'
df_ttest = mwu(x, y, tail=tail)
# Compute Hedges / Cohen
ef = np.round(compute_effsize(x=x, y=y, eftype=effsize,
paired=paired), 3)
if return_desc:
stats.at[i, 'mean(A)'] = np.round(np.nanmean(x), 3)
'p-GG-corr': [p_a_corr, p_b_corr, p_ab_corr],
'np2': [eta_a, eta_b, eta_ab],
'eps': [eps_a, eps_b, eps_ab],
})
col_order = ['Source', 'SS', 'ddof1', 'ddof2', 'MS', 'F', 'p-unc',
'p-GG-corr', 'np2', 'eps']
# Round
aov[['SS', 'MS', 'F', 'eps', 'np2']] = aov[['SS', 'MS', 'F', 'eps',
'np2']].round(3)
aov = aov.reindex(columns=col_order)
# Export to .csv
if export_filename is not None:
_export_table(aov, export_filename)
return aov
'SS': np.round([ss_fac1, ss_fac2, ss_inter,
ss_resid], 3),
'DF': [df_fac1, df_fac2, df_inter, df_resid],
'MS': np.round([ms_fac1, ms_fac2, ms_inter,
ms_resid], 3),
'F': [fval_fac1, fval_fac2, fval_inter, np.nan],
'p-unc': [pval_fac1, pval_fac2, pval_inter, np.nan],
'np2': [np2_fac1, np2_fac2, np2_inter, np.nan]
})
col_order = ['Source', 'SS', 'DF', 'MS', 'F', 'p-unc', 'np2']
aov = aov.reindex(columns=col_order)
aov.dropna(how='all', axis=1, inplace=True)
# Export to .csv
if export_filename is not None:
_export_table(aov, export_filename)
return aov
f_b = ms_b / ms_w
# P-values
p_c = f(df_c, df_w).sf(f_c)
p_b = f(df_b, df_w).sf(f_b)
# Create dataframe
aov = pd.DataFrame({'Source': [between, covar, 'Residual'],
'SS': [ss_b, ss_c, ss_w],
'DF': [df_b, df_c, df_w],
'F': [f_b, f_c, np.nan],
'p-unc': [p_b, p_c, np.nan],
})
# Export to .csv
if export_filename is not None:
_export_table(aov, export_filename)
# Add bw as an attribute (for rm_corr function)
aov.bw_ = bw
return aov