Random datasets for any design

Random datasets for any design#

spm1d 0.5 introduces a random number generator module: spm1d.rng to generate random 0D and 1D datasets for most procedures in spm1d.stats. Means, variances, smoothness and non-sphericity can all be specified.

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


The example below generates a random two-way ANOVA dataset with a true effect of factor B in the middle of the domain. The figure depicts the mean and standard deviation of each group.

mu = np.zeros( (2, 3, 101) )
mu[:, 2, 35:65] = 1.5 * np.hanning( 30 )  # true effect
y, A, B = spm1d.rng.anova2( nlevels=(2, 3), nsubj=8, Q=101, fwhm=20, mu=mu, sd=0.6, seed=0 )
print( y.shape, A.shape, B.shape )

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) )
ax  = fig.add_axes( [0.02, 0.08, 0.96, 0.90] )
for b, color in zip( (0, 1, 2), ('C0', 'C2', 'C3') ):                       # factor B:  color
    for a, ls in zip( (0, 1), ('-', '--') ):                                    # factor A:  line style
        spm1d.plot.plot_mean_sd( y[(A == a) & (B == b)], ax=ax, linecolor=color, linestyle=ls, facecolor=color, alpha=0.2, label=f'A{a},B{b}' )
ax.legend( loc='upper left', bbox_to_anchor=(-0.03, 0.84), ncol=3, fontsize=26, frameon=False )
ax.text( 0.01, 0.95, 'spm1d.rng.anova2( nlevels=(2,3) )', transform=ax.transAxes, ha='left', va='top',
         fontsize=27, family='monospace', bbox=dict( boxstyle='round,pad=0.5', facecolor='#e6f4f8', edgecolor='#0e6e84' ) )
ax.set_xlim( 0, 100 )
ax.set_ylim( top=ax.get_ylim()[1] + 0.6 * np.ptp( ax.get_ylim() ) )
ax.set_yticks( [] )
ax.tick_params( labelsize=14 )
for side in ('left', 'right', 'top'):
    ax.spines[side].set_visible( False )
plt.show()
rng
rng
(48, 101) (48,) (48,)


The generated data can be analyzed directly using the corresponding spm1d.stats procedure. The figure below depicts the main effect of factor B.

FFi = spm1d.stats.anova2( y, A, B ).inference( 0.05 )
print( FFi )
plt.figure( figsize=(8, 4.5), tight_layout=True )
FFi['Main B'].plot( ax=plt.gca() )
plt.title( 'Main effect of B' )
Main effect of B
Main effect of B
SPMiList
    STAT     : F
    fn_name  : anova2
    nfactors : 2
    neffects : 3
Effects
    A     z=(101,) array       df=(1, 40.384)      h0reject=False
    B     z=(101,) array       df=(1.996, 40.384)  h0reject=True
    A:B   z=(101,) array       df=(1.996, 40.384)  h0reject=False


These random datasets are useful primarily for experimental simulation and planning, as well as for probability validations, but they may also be used to conveniently explore spm1d’s various procedures without requiring you to format and re-format your data for the different procedures.