Linear regression#
In spm1d simple linear regression can be run with spm1d.stats.regress,
where the dependent variable is univariate 1D (i.e., a scalar varying over a
one-dimensional domain) and the independent variable is univariate 0D
(i.e., a simple scalar).
The example below uses a simulated regression dataset from Pataky et al. (2015). The dataset contains ten observations and one independent variable.
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import spm1d
import jikudata as jd
dataset = jd.SimulatedPataky2015c() # 1D dataset: ten observations
y, x = dataset.y, dataset.x # (10,101) array of observations, (10,) array of independent variable values
ti = spm1d.stats.regress( y, x ).inference( 0.05 ) # null hypothesis: slope = 0
In the figure below, 1D observations are colored according to the value of the corresponding 0D independent variable.
plt.figure( figsize=(7.2, 4.5), tight_layout=True )
ax = plt.gca()
norm = plt.Normalize( x.min(), x.max() )
cmap = LinearSegmentedColormap.from_list( 'spm1d', ['#08343f', '#0e6e84', '#2eb0ce', '#6fd3e8'] ) # dark blue-green to cyan, as in the spm1d logo
for yy, xx in zip( y, x ):
ax.plot( yy, color=cmap( norm( xx ) ), lw=2 )
ax.tick_params( labelsize=18 )
cb = plt.colorbar( plt.cm.ScalarMappable( norm=norm, cmap=cmap ), ax=ax )
cb.set_ticks( [x.min(), x.max()] )
cb.set_label( 'Independent variable', fontsize=20 )


The figure below depicts SPM results (left) and the r-value field
(right). The r-value can be accessed from the regression results
as results.r.
fig,axs = plt.subplots( 1, 2, figsize=(9, 3), tight_layout=True )
ti.plot( ax=axs[0] )
h0, h1 = axs[0].lines[0], axs[0].lines[2] # the test statistic and upper critical threshold
axs[0].legend( [h0, h1], ['Test statistic', 'Critical threshold'] )
axs[1].plot( ti.r, label='r field' )
axs[1].axhline(0, color='k', linestyle=':')
axs[1].legend()
axs[1].set_ylim(-0.95, 0.95)
[ax.set_ylabel(s, size=12) for ax,s in zip(axs, ['t-value', 'r-value'])]
plt.show()

