Two-sample t test

Two-sample t test#

A two-sample t test assesses whether the means of two independent groups differ. By default spm1d does not assume that the two groups have equal variances.

The example below uses plantar arch angles from Caravaggi et al. (2010) with ten observations for each of normal-speed and fast walking. These data are actually paired because the same participants walked at both speeds. They are treated here as two independent groups for illustrative purposes only.

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.PlantarArchAngle()      # 1D dataset:  two conditions, ten participants
y, A    = dataset.y, dataset.x       # (20,101) array of observations, (20,) array of condition labels
y0, y1  = y[A == 0], y[A == 1]       # normal-speed and fast walking
ti      = spm1d.stats.ttest2( y1, y0 ).inference( 0.05, dirn=0 )   # test statistic for  y1 - y0


The figure below depicts the means and standard deviations of the two groups.

plt.figure( figsize=(6.4, 4.5), tight_layout=True )
ax = plt.gca()
h0 = spm1d.plot.plot_mean_sd( y0, ax=ax, linecolor='k', facecolor='0.7' )[0]
h1 = spm1d.plot.plot_mean_sd( y1, ax=ax, linecolor='#0e6e84', facecolor='#2eb0ce' )[0]
ax.legend( [h0, h1], ['Normal speed', 'Fast'], fontsize=20 )
ttest2
ttest2


The figure below depicts the test statistic and the critical thresholds. ttest2( y1, y0 ) tests y1 - y0. There are two critical thresholds because the test is two-tailed. Use dirn=1 or dirn=-1 for a one-tailed test.

plt.figure( figsize=(8, 4.5), tight_layout=True )
ax = plt.gca()
ti.plot( ax=ax )
ax.yaxis.label.set_size( 20 )
h0, h1 = ax.lines[0], ax.lines[2]                 # the test statistic and the (upper) 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
ttest2
ttest2