One-way ANOVA

One-way ANOVA#

A one-way analysis of variance (ANOVA) assesses whether the means of three or more independent groups differ. By default spm1d does not assume that the groups have equal variances (see Unequal variances).

The example below uses the Canadian weather dataset from Ramsay & Silverman (2005). The dataset contains daily temperatures from 35 weather stations in four climate regions.

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.Weather()               # 1D dataset:  35 weather stations, four regions, 365 days
y, A    = dataset.y, dataset.x       # (35,365) array of temperatures, (35,) array of region labels
Fi      = spm1d.stats.anova1( y, A ).inference( 0.05 )
print( f'df = ({Fi.df[0]:.2f}, {Fi.df[1]:.2f})' )
df = (2.84, 29.47)


The figure below depicts the mean temperature in each of the four regions.

plt.figure( figsize=(6.4, 4.5), tight_layout=True )
ax = plt.gca()
for a, color in zip( np.unique( A ), ('#08343f', '#0e6e84', '#2eb0ce', '#6fd3e8') ):
    ax.plot( y[A == a].mean( axis=0 ), lw=3, color=color )
ax.set_xlabel( 'Day', fontsize=20 )
ax.set_ylabel( 'Temperature (°C)', fontsize=20 )
anova1
anova1


The figure below depicts the F statistic and the critical threshold. The F statistic indicates only that the region means are not all equal. Assessing which regions differ from each other requires post hoc analysis (see Post hoc tests).

plt.figure( figsize=(8, 4.5), tight_layout=True )
ax = plt.gca()
Fi.plot( ax=ax )
ax.yaxis.label.set_size( 20 )
h0, h1 = ax.lines[0], ax.lines[2]                 # the test statistic and the critical threshold
ax.legend( [h0, h1], ['Test statistic', 'Critical threshold'], fontsize=20, ncol=2, loc='lower center', bbox_to_anchor=(0.5, 1) )   # above the axes
anova1
anova1