diff --git a/docs/sig_tests.rst b/docs/sig_tests.rst index c5993da..8330f12 100644 --- a/docs/sig_tests.rst +++ b/docs/sig_tests.rst @@ -6,6 +6,8 @@ Functions .. autofunction:: yli.anova_oneway +.. autofunction:: yli.auto_univariable + .. autofunction:: yli.chi2 .. autofunction:: yli.mannwhitney @@ -17,6 +19,9 @@ Functions Result classes -------------- +.. autoclass:: yli.sig_tests.AutoBinaryResult + :members: + .. autoclass:: yli.sig_tests.BrunnerMunzelResult :members: diff --git a/yli/__init__.py b/yli/__init__.py index 1afea8d..89415d7 100644 --- a/yli/__init__.py +++ b/yli/__init__.py @@ -19,7 +19,7 @@ from .config import config from .distributions import beta_oddsratio, beta_ratio, hdi, transformed_dist from .io import pickle_read_compressed, pickle_read_encrypted, pickle_write_compressed, pickle_write_encrypted from .regress import PenalisedLogit, logit_then_regress, regress, vif -from .sig_tests import anova_oneway, chi2, mannwhitney, pearsonr, ttest_ind +from .sig_tests import anova_oneway, auto_univariable, chi2, mannwhitney, pearsonr, ttest_ind def reload_me(): import importlib diff --git a/yli/sig_tests.py b/yli/sig_tests.py index 1644b9c..b461cfc 100644 --- a/yli/sig_tests.py +++ b/yli/sig_tests.py @@ -98,6 +98,18 @@ class TTestResult: """ return '{}\n\nt({:.0f}) = {:.2f}; p {}\nΔμ ({:g}% CI) = {}, {}'.format(self._comparison_table(False), self.dof, self.statistic, fmt_p(self.pvalue, PValueStyle.RELATION), (1-config.alpha)*100, self.delta.summary(), self.delta_direction) + + def summary_short(self, html): + """ + Return a stringified summary of the *t* test (*t* statistic only) + + :rtype: str + """ + + if html: + return 't({:.0f}) = {:.2f}'.format(self.dof, self.statistic) + else: + return 't({:.0f}) = {:.2f}'.format(self.dof, self.statistic) def ttest_ind(df, dep, ind, *, nan_policy='warn'): """ @@ -336,6 +348,18 @@ class MannWhitneyResult: return line1 + '\n' + self.brunnermunzel.summary() else: return line1 + + def summary_short(self, html): + """ + Return a stringified summary of the Mann–Whitney test (*U* statistic only) + + :rtype: str + """ + + if html: + return 'U = {:.1f}'.format(self.statistic) + else: + return 'U = {:.1f}'.format(self.statistic) class BrunnerMunzelResult: """ @@ -422,7 +446,7 @@ def mannwhitney(df, dep, ind, *, nan_policy='warn', brunnermunzel=True, use_cont # Check for/clean NaNs df = check_nan(df[[ind, dep]], nan_policy) - # Convert pandas nullable types for independent variables as this breaks statsmodels + # Convert pandas nullable types for independent variables as this breaks mannwhitneyu df = convert_pandas_nullable(df) # Ensure 2 groups for ind @@ -506,6 +530,18 @@ class PearsonChiSquaredResult: else: return '{}\n\nχ²({}) = {:.2f}; p {}'.format( self.ct, self.dof, self.statistic, fmt_p(self.pvalue, PValueStyle.RELATION)) + + def summary_short(self, html): + """ + Return a stringified summary of the *χ*:sup:`2` test (*χ*:sup:`2` statistic only) + + :rtype: str + """ + + if html: + return 'χ2({}) = {:.2f}'.format(self.dof, self.statistic) + else: + return 'χ²({}) = {:.2f}'.format(self.dof, self.statistic) def chi2(df, dep, ind, *, nan_policy='warn'): """ @@ -662,3 +698,144 @@ def pearsonr(df, dep, ind, *, nan_policy='warn'): ci = result.confidence_interval() return PearsonRResult(statistic=Estimate(result.statistic, ci.low, ci.high), pvalue=result.pvalue) + +# ---------------------------- +# Automatic selection of tests + +class AutoBinaryResult: + """ + Result of automatically computed univariable tests of association for a dichotomous dependent variable + + See :func:`yli.auto_univariable`. + + Results data stored within instances of this class is not intended to be directly accessed. + """ + + def __init__(self, *, dep, group1, group2, result_data, result_labels): + #: Name of the dependent variable (*str*) + self.dep = dep + + #: Name of the first group (*str*) + self.group1 = group1 + #: Name of the second group (*str*) + self.group2 = group2 + + # List of tuples (first group summary, second group summary, test result) + self._result_data = result_data + # List of row labels for the independente variables + self._result_labels = result_labels + + def __repr__(self): + if config.repr_is_summary: + return self.summary() + return super().__repr__() + + def _repr_html_(self): + result = '
{} | {} | {} | p | |
---|---|---|---|---|
{} | {} | {} | {} | {} |