"""
n-way RM-ANOVA with corrections
===============================

**spm1d** v0.4 assumed equal variances and sphericity for most ANOVA
procedures. Version 0.5 implements these corrections for most ANOVA
procedures, where neither equal variance nor sphericity is assumed by
default. ℹ️

.. dropdown:: ℹ️ Unequal variance and nonsphericity corrections

   - Assumptions of equal variance and sphericity can be imposed using
     ``cov_model='iid'`` as a keyword argument to ``spm1d.stats``
     procedures.

   - Corrections for heteroscedasticity and non-sphericity can be set
     using ``cov_model='unstructured'`` (default).

   - These corrections change the degrees of freedom and therefore the
     critical threshold.

   - These corrections apply only to between-subjects designs and
     one-way repeated measures designs; multi-factor repeated-measures
     designs are not corrected (see :doc:`/theory/multifactor_rm`).


The figure below depicts a one-way ANOVA scenario where the corrected
threshold is substantially higher due to large inter-group variance
differences.

"""




import numpy as np
from matplotlib import pyplot as plt
import spm1d

y, A    = spm1d.rng.anova1( nlevels=3, nsubj=8, Q=101, fwhm=20, sd=[1, 1, 4], mu=[0, 0, 1.5], seed=4 )
FFiid   = spm1d.stats.anova1( y, A, cov_model='iid' ).inference( 0.05 )           # 0.4 assumptions
FF      = spm1d.stats.anova1( y, A, cov_model='unstructured' ).inference( 0.05 )  # 0.5 default
for Fi, label in [(FFiid, 'iid'), (FF, 'unstructured')]:
    print( f'{label:13s} df = ({Fi.df[0]:.1f}, {Fi.df[1]:.1f}),  zc = {Fi.zc:.2f},  h0reject = {Fi.h0reject}' )

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.02, 0.52, 0.96, 0.46] )                                  # the data
ax1 = fig.add_axes( [0.02, 0.07, 0.96, 0.43] )                                  # the statistic
spm1d.plot.plot_mean_sd( y[A == 2], ax=ax0, linecolor='#6fd3e8', facecolor='#6fd3e8', alpha=0.2 )   # the high-variance group, drawn first
spm1d.plot.plot_mean_sd( y[A == 0], ax=ax0, linecolor='#08343f', facecolor='#08343f', alpha=0.6 )
spm1d.plot.plot_mean_sd( y[A == 1], ax=ax0, linecolor='#0e6e84', facecolor='#0e6e84', alpha=0.6 )
ax0.set_ylim( top=ax0.get_ylim()[1] + 0.45 * np.ptp( ax0.get_ylim() ) )                     # room for the text above the clouds
ax0.text( 0.02, 0.97, 'Unequal variances', transform=ax0.transAxes, ha='left', va='top', fontsize=26 )
ax0.set_xticks( [] );  ax0.set_yticks( [] )
ax1.plot( FF.z, 'k', lw=2.5 )
ax1.axhline( FFiid.zc, color='0.5', ls='--', lw=2 )
ax1.axhline( FF.zc, color='r', ls='--', lw=2 )
ax1.text( 1, FFiid.zc + 0.3, 'uncorrected', ha='left', va='bottom', fontsize=20, color='0.4' )
ax1.text( 1, FF.zc + 0.3, 'corrected', ha='left', va='bottom', fontsize=20, color='r' )
ax1.set_ylim( 0, 1.3 * FF.zc )
ax1.set_yticks( [] )
ax1.tick_params( labelsize=14 )
for ax in (ax0, ax1):
    for side in ('left', 'right', 'top'):
        ax.spines[side].set_visible( False )
ax0.spines['bottom'].set_visible( False )

