"""
Post hoc tests
==============

In **spm1d** 0.4 *post hoc* comparisons were unsupported. In version 0.5
a new ``posthoc`` method has been implemented to facilitate *post hoc*
testing for relevant univariate and multivariate procedures.

For example, after one-way ANOVA inference with
``spmi = spm1d.stats.anova1(y, A).inference(0.05)``, running ``spmi.posthoc()``
will conduct all relevant pairwise comparisons.

Note that ``posthoc()`` is only permitted when the omnibus null
hypothesis is rejected. If the omnibus test fails to reach significance,
an ``SPM1DPostHocError`` is raised.

.. code-block:: text
   :class: spm1d-error

   SPM1DPostHocError: The main test failed to reach significance. Post hoc
   tests are therefore disabled.

*Post hoc* tests inherit the main model's error term, degrees of freedom
and smoothness.

There are two *post hoc* correction options:

- ``spmi.posthoc( correction='bonf' )`` --- Bonferroni correction (default)
- ``spmi.posthoc( correction='scheffe' )`` --- Scheffé correction

For multi-factor designs a factor label is required to limit post hoc
analyses to the specified factor, for example: ``FFi.posthoc( 'A' )``.

Refer to :doc:`/theory/posthoc` for more details.

"""

# sphinx_gallery_thumbnail_path = '_static/thumbs/posthoc.png'
import numpy as np
from matplotlib import pyplot as plt
import spm1d
import jikudata as jd

dataset     = jd.SpeedGRFcategoricalRM()   # 1D dataset:  three walking speeds, repeated measures
y, (A, S)   = dataset.y, dataset.x.T
Fi          = spm1d.stats.anova1rm( y, A, S ).inference( 0.05 )
phi         = Fi.posthoc( correction='bonf' )            # all pairwise comparisons
phi.print_table()

# %%
#  |
#  |
#
#  One way to visualize post hoc results is to present main test results in a main panel
#  with post hoc results in a separate panel below as in the figure below.
#
plt.rc( 'font', family='sans-serif' )
plt.rc( 'font', **{'sans-serif': ['Helvetica Neue', 'Helvetica', 'Arial', 'Liberation Sans', 'DejaVu Sans']} )
fig = plt.figure( figsize=(8, 6) )
ax0 = fig.add_axes( [0.10, 0.52, 0.88, 0.46] )
ax1 = fig.add_axes( [0.10, 0.08, 0.88, 0.42] )
for a, color, label in zip( (0, 1, 2), ('#2eb0ce', 'k', '#0e6e84'), ('slow', 'normal', 'fast') ):
    spm1d.plot.plot_mean_sd( y[A == a], ax=ax0, linecolor=color, facecolor=color, label=label )
Fi.plot_cluster_patches( ax=ax0 )
ax0.legend( loc='upper left', fontsize=16, frameon=False, bbox_to_anchor=(0.4,1.0) )
phi.plot_cluster_bars( ax=ax1, pvalues=True, fontsize=14, height=0.55, label_colors={'A0': '#2eb0ce', 'A1': 'k', 'A2': '#0e6e84'} )
ax1.text( 0.02, 0.97, 'Post hoc', transform=ax1.transAxes, ha='left', va='top', fontsize=26 )
ax1.tick_params( axis='y', labelsize=18 )
ax1.tick_params( axis='x', labelsize=14 )
ax1.set_ylim( top=ax1.get_ylim()[1] + 0.8 )
ax0.set_xticks( [] );  ax0.set_yticks( [] )
ax1.set_xlim( ax0.get_xlim() )
for ax in (ax0, ax1):
    for side in ('left', 'right', 'top'):
        ax.spines[side].set_visible( False )
ax0.spines['bottom'].set_visible( False )
