"""
Three-way ANOVA
===============

A three-way ANOVA assesses the effects of three factors (A, B and C). It
yields seven effects: three main effects (A, B and C), three two-way
interactions (AB, AC and BC) and one three-way interaction (ABC).

The example below uses the simulated dataset ``SPM1D_ANOVA3_2x2x2``. It has two
levels of each factor.
"""

import numpy as np
from matplotlib import pyplot as plt
import spm1d
import jikudata as jd
plt.rc( 'font', size=18 )             # minimum font size for all figure text

dataset    = jd.SPM1D_ANOVA3_2x2x2()   # 1D dataset:  2 x 2 x 2 design
y          = dataset.y                 # (40,101) array of observations
A, B, C    = dataset.x.T               # factor A, B and C labels
FFi        = spm1d.stats.anova3( y, A, B, C ).inference( 0.05 )

# %%
#  |
#  |
#
#  The figure below depicts the F statistic (solid line) and critical
#  threshold (dashed line) for each of the seven effects. In this dataset only the main effect of B is
#  significant.
plt.figure( figsize=(8, 6.4) )
FFi.plot( plot_threshold_label=False, plot_p_values=False )
for ax, label in zip( plt.gcf().axes, FFi.effect_labels ):
    ax.set_title( label.split()[-1], fontsize=26 )   # short effect names:  A, B, AB, ...
    ax.set_ylabel( '' )
    ax.set_xticks( [] );  ax.set_yticks( [] )      # tick labels omitted:  the panels are small
plt.tight_layout()

# %%
#  |
#  |
#
#  The effects' names are listed in ``FFi.effect_labels``. A single effect can
#  be accessed by name as shown below.
print( FFi.effect_labels )
print( FFi['Main B'].h0reject )
