Improve formatting of tabular p values

This commit is contained in:
RunasSudo 2022-10-15 23:30:41 +11:00
parent a2442214ed
commit 8d40e9db8b
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 30 additions and 16 deletions

View File

@ -189,10 +189,10 @@ class RegressionResult:
if html: if html:
right_col.append(('<i>F</i>:', format(f_result.statistic, '.2f'))) right_col.append(('<i>F</i>:', format(f_result.statistic, '.2f')))
right_col.append(('<i>p</i> (<i>F</i>):', fmt_p(f_result.pvalue, html=True, nospace=True))) right_col.append(('<i>p</i> (<i>F</i>):', fmt_p(f_result.pvalue, html=True, tabular=True)))
else: else:
right_col.append(('F:', format(f_result.statistic, '.2f'))) right_col.append(('F:', format(f_result.statistic, '.2f')))
right_col.append(('p (F):', fmt_p(f_result.pvalue, html=False, nospace=True))) right_col.append(('p (F):', fmt_p(f_result.pvalue, html=False, tabular=True)))
else: else:
# Otherwise report likelihood ratio test as overall test # Otherwise report likelihood ratio test as overall test
lrtest_result = self.lrtest_null() lrtest_result = self.lrtest_null()
@ -200,9 +200,9 @@ class RegressionResult:
right_col.append(('LL-Model:', format(self.llf, '.2f'))) right_col.append(('LL-Model:', format(self.llf, '.2f')))
right_col.append(('LL-Null:', format(self.llnull, '.2f'))) right_col.append(('LL-Null:', format(self.llnull, '.2f')))
if html: if html:
right_col.append(('<i>p</i> (LR):', fmt_p(lrtest_result.pvalue, html=True, nospace=True))) right_col.append(('<i>p</i> (LR):', fmt_p(lrtest_result.pvalue, html=True, tabular=True)))
else: else:
right_col.append(('p (LR):', fmt_p(lrtest_result.pvalue, html=False, nospace=True))) right_col.append(('p (LR):', fmt_p(lrtest_result.pvalue, html=False, tabular=True)))
return left_col, right_col return left_col, right_col
@ -232,7 +232,7 @@ class RegressionResult:
if self.exp: if self.exp:
beta = np.exp(beta) beta = np.exp(beta)
out += '<tr><th>{}</th><td>{:.2f}</td><td style="padding-right:0">({:.2f}</td><td>–</td><td style="padding-left:0">{:.2f})</td><td style="text-align:left">{}</td></tr>'.format(term_name, beta.point, beta.ci_lower, beta.ci_upper, fmt_p(term.pvalue, html=True, nospace=True)) out += '<tr><th>{}</th><td>{:.2f}</td><td style="padding-right:0">({:.2f}</td><td>–</td><td style="padding-left:0">{:.2f})</td><td style="text-align:left">{}</td></tr>'.format(term_name, beta.point, beta.ci_lower, beta.ci_upper, fmt_p(term.pvalue, html=True, tabular=True))
elif isinstance(term, CategoricalTerm): elif isinstance(term, CategoricalTerm):
# Categorical term # Categorical term
out += '<tr><th>{}</th><td></td><td style="padding-right:0"></td><td></td><td style="padding-left:0"></td><td></td></tr>'.format(term_name) out += '<tr><th>{}</th><td></td><td style="padding-right:0"></td><td></td><td style="padding-left:0"></td><td></td></tr>'.format(term_name)
@ -247,7 +247,7 @@ class RegressionResult:
if self.exp: if self.exp:
beta = np.exp(beta) beta = np.exp(beta)
out += '<tr><td style="text-align:right;font-style:italic">{}</td><td>{:.2f}</td><td style="padding-right:0">({:.2f}</td><td>–</td><td style="padding-left:0">{:.2f})</td><td style="text-align:left">{}</td></tr>'.format(sub_term_name, beta.point, beta.ci_lower, beta.ci_upper, fmt_p(sub_term.pvalue, html=True, nospace=True)) out += '<tr><td style="text-align:right;font-style:italic">{}</td><td>{:.2f}</td><td style="padding-right:0">({:.2f}</td><td>–</td><td style="padding-left:0">{:.2f})</td><td style="text-align:left">{}</td></tr>'.format(sub_term_name, beta.point, beta.ci_lower, beta.ci_upper, fmt_p(sub_term.pvalue, html=True, tabular=True))
else: else:
raise Exception('Attempt to render unknown term type') raise Exception('Attempt to render unknown term type')
@ -287,7 +287,7 @@ class RegressionResult:
beta = np.exp(beta) beta = np.exp(beta)
# Add some extra padding # Add some extra padding
table_data.append([term_name + ' ', format(beta.point, '.2f'), '({:.2f}'.format(beta.ci_lower), '-', '{:.2f})'.format(beta.ci_upper), ' ' + fmt_p(term.pvalue, html=False, nospace=True)]) table_data.append([term_name + ' ', format(beta.point, '.2f'), '({:.2f}'.format(beta.ci_lower), '-', '{:.2f})'.format(beta.ci_upper), ' ' + fmt_p(term.pvalue, html=False, tabular=True)])
elif isinstance(term, CategoricalTerm): elif isinstance(term, CategoricalTerm):
# Categorical term # Categorical term
table_data.append([term_name + ' ', '', '', '', '', '']) table_data.append([term_name + ' ', '', '', '', '', ''])
@ -302,7 +302,7 @@ class RegressionResult:
if self.exp: if self.exp:
beta = np.exp(beta) beta = np.exp(beta)
table_data.append([sub_term_name + ' ', format(beta.point, '.2f'), '({:.2f}'.format(beta.ci_lower), '-', '{:.2f})'.format(beta.ci_upper), ' ' + fmt_p(sub_term.pvalue, html=False, nospace=True)]) table_data.append([sub_term_name + ' ', format(beta.point, '.2f'), '({:.2f}'.format(beta.ci_lower), '-', '{:.2f})'.format(beta.ci_upper), ' ' + fmt_p(sub_term.pvalue, html=False, tabular=True)])
else: else:
raise Exception('Attempt to render unknown term type') raise Exception('Attempt to render unknown term type')

View File

@ -96,30 +96,44 @@ def do_fmt_p(p):
# OK to round to pvalue_min_dps # OK to round to pvalue_min_dps
return None, '{0:.{dps}f}'.format(p, dps=config.pvalue_min_dps) return None, '{0:.{dps}f}'.format(p, dps=config.pvalue_min_dps)
def fmt_p(p, *, html, nospace=False): def fmt_p(p, *, html, tabular=False):
"""Format p value""" """
Format p value
tabular: If true, output in tabular format of p values where decimal points align
"""
sign, fmt = do_fmt_p(p) sign, fmt = do_fmt_p(p)
# Strip leading zero if required
if not config.pvalue_leading_zero: if not config.pvalue_leading_zero:
fmt = fmt.lstrip('0') fmt = fmt.lstrip('0')
# Add significance asterisk if required
if p < config.alpha: if p < config.alpha:
fmt += '*' fmt += '*'
if sign is not None: if sign is not None:
if nospace: if html:
# Escape angle quotes
sign = sign.replace('<', '&lt;')
sign = sign.replace('>', '&gt;')
if tabular:
pfmt = sign + fmt # e.g. "<0.001" pfmt = sign + fmt # e.g. "<0.001"
else: else:
pfmt = sign + ' ' + fmt # e.g. "< 0.001" pfmt = sign + ' ' + fmt # e.g. "< 0.001"
else: else:
if nospace: if tabular:
pfmt = fmt # e.g. "0.05" # Tabular format with no sign: add a preceding space to get decimal points to line up
if html:
# Insert space with width of '=' which should be width of '<' and '>' too
pfmt = '<span style="visibility:hidden">=</span>' + fmt # e.g. "0.05"
else:
pfmt = ' ' + fmt # e.g. "0.05"
else: else:
pfmt = '= ' + fmt # e.g. "= 0.05" pfmt = '= ' + fmt # e.g. "= 0.05"
if html:
pfmt = pfmt.replace('<', '&lt;')
return pfmt return pfmt
# ------------------------------ # ------------------------------