'''
One- and two-sample t tests.
'''
# Copyright (C) 2026 Todd Pataky
import numpy as np
from . _dec import appendargs, checkargs, docdims
from . _glm.cov import resolve_cov_model
[docs]
@appendargs
@checkargs
@docdims('univariate')
def regress(y, x, roi=None, _fwhm_method='taylor2007'):
'''
Linear regression.
:Parameters:
- *y* --- (J,) array for 0D data or (J,Q) array for 1D data
- *x* --- Independent variable as a (J,) array or list
- *roi* --- DEPRECATED (use ``inference``)
:Returns:
- **SPM0D** or **SPM1D** object
:Example:
The 0D example uses the "cars" dataset from R (speed and stopping
distance of 50 cars).
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.R_Cars()
>>> y, x = dataset.y, dataset.x # (50,) arrays of response and independent variable
>>> spmi = spm1d.stats.regress( y, x ).inference( 0.05 )
>>> print( spmi )
SPM{t} (0D) inference
fn_name : regress
STAT : T
z : 9.46399
r : 0.80689
df : (1, 48)
Inference results:
method : param
isparametric : True
alpha : 0.05
dirn : 0
zc : 2.01063
h0reject : True
p : <0.00001
The 1D example uses ground reaction force vs. walking speed from Pataky
et al. (2008) (60 trials).
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.SpeedGRF()
>>> y, x = dataset.y, dataset.x # (60,101) array and (60,) array
>>> spmi = spm1d.stats.regress( y, x ).inference( 0.05 )
>>> print( spmi.h0reject )
True
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- Correlation coefficient: ``spm.r``.
'''
from . _glm.design.designs import REGRESS
from . _glm.ui import _glm_via_design
spm = _glm_via_design(y, REGRESS(x) , roi=roi, _fwhm_method=_fwhm_method)
return spm
[docs]
@appendargs
@checkargs
@docdims('univariate')
def ttest(y, mu=0, roi=None, _fwhm_method='taylor2007'):
'''
One-sample t test.
:Parameters:
- *y* --- (J,) array for 0D data or (J,Q) array for 1D data
- *mu* --- Datum as a scalar for 0D data or a scalar or (Q,) array for 1D
data (default 0)
- *roi* --- DEPRECATED (use ``inference``)
:Returns:
- **SPM0D** or **SPM1D** object
:Example:
The 0D example uses the "morley" dataset from R (100 measurements of
the speed of light). The test is one-tailed with a null value of 792.
>>> import spm1d
>>> import jikudata as jd
>>> y = jd.R_Morley().y # (100,) array
>>> spmi = spm1d.stats.ttest( y, 792 ).inference( 0.05, dirn=1 )
>>> print( spmi )
SPM{t} (0D) inference
fn_name : ttest
STAT : T
z : 7.64455
df : (1, 99)
Inference results:
method : param
isparametric : True
alpha : 0.05
dirn : 1
zc : 1.66039
h0reject : True
p : <0.00001
The 1D example uses ten simulated responses from Pataky et al. (2015).
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> y = jd.SimulatedPataky2015b().y # (10,101) array
>>> spmi = spm1d.stats.ttest( y, 0 ).inference( 0.05, dirn=1 )
>>> print( spmi.h0reject, round( spmi.p_zmax, 3 ) )
True 0.019
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- Inference is two-tailed by default.
'''
from . _glm.design.designs import TTEST
from . _glm.ui import _glm_via_design
return _glm_via_design(y-mu, TTEST( y.shape[0] ) , roi=roi, _fwhm_method=_fwhm_method)
[docs]
@appendargs
@checkargs
@docdims('univariate')
def ttest2(y0, y1, cov_model='unstructured', roi=None, equal_var=None, _fwhm_method='taylor2007'):
'''
Two-sample t test.
:Parameters:
- *y0* --- Group A data as a (J0,) array for 0D data or a (J0,Q) array for
1D data
- *y1* --- Group B data as a (J1,) array for 0D data or a (J1,Q) array for
1D data.
- *cov_model* --- ``iid``, ``unstructured`` (default) or a list of
covariance component matrices
- *roi* --- DEPRECATED (use ``inference``)
- *equal_var* --- DEPRECATED (use ``cov_model``)
:Returns:
- **SPM0D** or **SPM1D** object
:Example:
The 0D example uses the "sleep" dataset from R (two groups of 10).
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.R_Sleep2Test()
>>> y, A = dataset.y, dataset.x # (20,) arrays of responses and group labels
>>> y0, y1 = y[ A == 0 ], y[ A == 1 ]
>>> spmi = spm1d.stats.ttest2( y0, y1 ).inference( 0.05 )
>>> print( spmi )
SPM{t} (0D) inference
fn_name : ttest2
STAT : T
z : -1.86081
df0 : (1, 18)
df : (1, 17.776)
Inference results:
method : param
isparametric : True
alpha : 0.05
dirn : 0
zc : 2.10282
h0reject : False
p : 0.07939
The 1D example uses two simulated groups of six responses.
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.SimulatedTwoLocalMax()
>>> y, A = dataset.y, dataset.x # (12,101) array and (12,) array
>>> y0, y1 = y[ A == 0 ], y[ A == 1 ]
>>> spmi = spm1d.stats.ttest2( y0, y1 ).inference( 0.05 )
>>> print( spmi.h0reject )
True
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
:Note:
- ``ttest2(y0, y1)`` tests ``y0 - y1`` (``dirn=1`` tests ``y0`` > ``y1``).
- Degrees of freedom are corrected using Welch's rule for 0D data and
``trRV**2/trRVRV`` for 1D data.
``spm1d.settings.enable_spm12_compatibility()`` applies the 1D rule to 0D
data (for reproducing SPM12 results).
'''
from . _glm.design.designs import TTEST2
from . _glm.ui import _glm_via_design
n0,n1 = y0.shape[0], y1.shape[0]
y = np.hstack( (y0,y1) ) if (y0.ndim==1) else np.vstack( (y0, y1) )
cm = resolve_cov_model(cov_model, equal_var)
return _glm_via_design(y, TTEST2(n0, n1) , cm, roi, _fwhm_method=_fwhm_method)
[docs]
@appendargs
@checkargs
@docdims('univariate')
def ttest_paired(y0, y1, roi=None, _fwhm_method='taylor2007'):
'''
Paired t test.
:Parameters:
- *y0* --- Condition A data as a (J,) array for 0D data or a (J,Q) array
for 1D data
- *y1* --- Condition B data as a (J,) array for 0D data or a (J,Q) array
for 1D data. Rows must match condition A.
- *roi* --- DEPRECATED (use ``inference``)
:Returns:
- **SPM0D** or **SPM1D** object
:Example:
The 0D example uses the "sleep" dataset from R (10 participants and two
conditions).
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.R_SleepPairedTest()
>>> y, A = dataset.y, dataset.x # (20,) arrays of responses and condition labels
>>> y0, y1 = y[ A == 0 ], y[ A == 1 ] # same participants in the same order
>>> spmi = spm1d.stats.ttest_paired( y0, y1 ).inference( 0.05 )
>>> print( spmi )
SPM{t} (0D) inference
fn_name : ttest_paired
STAT : T
z : -4.06213
df : (1, 9)
Inference results:
method : param
isparametric : True
alpha : 0.05
dirn : 0
zc : 2.26216
h0reject : True
p : 0.00283
The 1D example uses plantar arch angle from Caravaggi et al. (2010)
(ten participants and two conditions).
>>> import spm1d
>>> import jikudata as jd
>>> from matplotlib import pyplot as plt
>>> dataset = jd.PlantarArchAngle()
>>> y, A = dataset.y, dataset.x # (20,101) array and (20,) array
>>> y0, y1 = y[ A == 0 ], y[ A == 1 ]
>>> spmi = spm1d.stats.ttest_paired( y0, y1 ).inference( 0.05 )
>>> print( spmi.h0reject, round( spmi.p_zmax, 3 ) )
True 0.002
>>> spmi.plot()
>>> plt.show()
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
'''
from . _glm.design.designs import TTEST_PAIRED
from . _glm.ui import _glm_via_design
return _glm_via_design(y0-y1, TTEST_PAIRED( y0.shape[0] ), roi=roi, _fwhm_method=_fwhm_method)