Note
Click here to download the full example code
Statistical inference#
Here we will briefly cover multiple concepts of inferential statistics in an introductory manner, and demonstrate how to use some MNE statistical functions.
# Authors: Eric Larson <larson.eric.d@gmail.com>
# License: BSD3Clause
from functools import partial
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D # noqa, analysis:ignore
import mne
from mne.stats import (ttest_1samp_no_p, bonferroni_correction, fdr_correction,
permutation_t_test, permutation_cluster_1samp_test)
print(__doc__)
Hypothesis testing#
Null hypothesis#
From Wikipedia:
In inferential statistics, a general statement or default position that there is no relationship between two measured phenomena, or no association among groups.
We typically want to reject a null hypothesis with some probability (e.g., p < 0.05). This probability is also called the significance level \(\alpha\). To think about what this means, let’s follow the illustrative example from 1 and construct a toy dataset consisting of a 40 x 40 square with a “signal” present in the center with white noise added and a Gaussian smoothing kernel applied.
width = 40
n_subjects = 10
signal_mean = 100
signal_sd = 100
noise_sd = 0.01
gaussian_sd = 5
sigma = 1e3 # sigma for the "hat" method
n_permutations = 'all' # run an exact test
n_src = width * width
# For each "subject", make a smoothed noisy signal with a centered peak
rng = np.random.RandomState(2)
X = noise_sd * rng.randn(n_subjects, width, width)
# Add a signal at the center
X[:, width // 2, width // 2] = signal_mean + rng.randn(n_subjects) * signal_sd
# Spatially smooth with a 2D Gaussian kernel
size = width // 2  1
gaussian = np.exp((np.arange(size, size + 1) ** 2 / float(gaussian_sd ** 2)))
for si in range(X.shape[0]):
for ri in range(X.shape[1]):
X[si, ri, :] = np.convolve(X[si, ri, :], gaussian, 'same')
for ci in range(X.shape[2]):
X[si, :, ci] = np.convolve(X[si, :, ci], gaussian, 'same')
The data averaged over all subjects looks like this:
In this case, a null hypothesis we could test for each voxel is:
There is no difference between the mean value and zero (\(H_0 \colon \mu = 0\)).
The alternative hypothesis, then, is that the voxel has a nonzero mean (\(H_1 \colon \mu \neq 0\)). This is a twotailed test because the mean could be less than or greater than zero, whereas a onetailed test would test only one of these possibilities, i.e. \(H_1 \colon \mu \geq 0\) or \(H_1 \colon \mu \leq 0\).
Note
Here we will refer to each spatial location as a “voxel”. In general, though, it could be any sort of data value, including cortical vertex at a specific time, pixel in a timefrequency decomposition, etc.
Parametric tests#
Let’s start with a paired ttest, which is a standard test for differences in paired samples. Mathematically, it is equivalent to a 1sample ttest on the difference between the samples in each condition. The paired ttest is parametric because it assumes that the underlying sample distribution is Gaussian, and is only valid in this case. This happens to be satisfied by our toy dataset, but is not always satisfied for neuroimaging data.
In the context of our toy dataset, which has many voxels (\(40 \cdot 40 = 1600\)), applying the paired ttest is called a massunivariate approach as it treats each voxel independently.
titles = ['t']
out = stats.ttest_1samp(X, 0, axis=0)
ts = [out[0]]
ps = [out[1]]
mccs = [False] # these are not multiplecomparisons corrected
def plot_t_p(t, p, title, mcc, axes=None):
if axes is None:
fig = plt.figure(figsize=(6, 3))
axes = [fig.add_subplot(121, projection='3d'), fig.add_subplot(122)]
show = True
else:
show = False
p_lims = [0.1, 0.001]
t_lims = stats.distributions.t.ppf(p_lims, n_subjects  1)
p_lims = [np.log10(p) for p in p_lims]
# t plot
x, y = np.mgrid[0:width, 0:width]
surf = axes[0].plot_surface(x, y, np.reshape(t, (width, width)),
rstride=1, cstride=1, linewidth=0,
vmin=t_lims[0], vmax=t_lims[1], cmap='viridis')
axes[0].set(xticks=[], yticks=[], zticks=[],
xlim=[0, width  1], ylim=[0, width  1])
axes[0].view_init(30, 15)
cbar = plt.colorbar(ax=axes[0], shrink=0.75, orientation='horizontal',
fraction=0.1, pad=0.025, mappable=surf)
cbar.set_ticks(t_lims)
cbar.set_ticklabels(['%0.1f' % t_lim for t_lim in t_lims])
cbar.set_label('tvalue')
cbar.ax.get_xaxis().set_label_coords(0.5, 0.3)
if not show:
axes[0].set(title=title)
if mcc:
axes[0].title.set_weight('bold')
# p plot
use_p = np.log10(np.reshape(np.maximum(p, 1e5), (width, width)))
img = axes[1].imshow(use_p, cmap='inferno', vmin=p_lims[0], vmax=p_lims[1],
interpolation='nearest')
axes[1].set(xticks=[], yticks=[])
cbar = plt.colorbar(ax=axes[1], shrink=0.75, orientation='horizontal',
fraction=0.1, pad=0.025, mappable=img)
cbar.set_ticks(p_lims)
cbar.set_ticklabels(['%0.1f' % p_lim for p_lim in p_lims])
cbar.set_label(r'$\log_{10}(p)$')
cbar.ax.get_xaxis().set_label_coords(0.5, 0.3)
if show:
text = fig.suptitle(title)
if mcc:
text.set_weight('bold')
plt.subplots_adjust(0, 0.05, 1, 0.9, wspace=0, hspace=0)
mne.viz.utils.plt_show()
plot_t_p(ts[1], ps[1], titles[1], mccs[1])
“Hat” variance adjustment#
The “hat” technique regularizes the variance values used in the ttest calculation 1 to compensate for implausibly small variances.
Nonparametric tests#
Instead of assuming an underlying Gaussian distribution, we could instead use a nonparametric resampling method. In the case of a paired ttest between two conditions A and B, which is mathematically equivalent to a onesample ttest between the difference in the conditions AB, under the null hypothesis we have the principle of exchangeability. This means that, if the null is true, we can exchange conditions and not change the distribution of the test statistic.
When using a paired ttest, exchangeability thus means that we can flip the signs of the difference between A and B. Therefore, we can construct the null distribution values for each voxel by taking random subsets of samples (subjects), flipping the sign of their difference, and recording the absolute value of the resulting statistic (we record the absolute value because we conduct a twotailed test). The absolute value of the statistic evaluated on the veridical data can then be compared to this distribution, and the pvalue is simply the proportion of null distribution values that are smaller.
Warning
In the case of a true onesample ttest, i.e. analyzing a single condition rather than the difference between two conditions, it is not clear where/how exchangeability applies; see this FieldTrip discussion.
In the case where n_permutations
is large enough (or “all”) so
that the complete set of unique resampling exchanges can be done
(which is \(2^{N_{samp}}1\) for a onetailed and
\(2^{N_{samp}1}1\) for a twotailed test, not counting the
veridical distribution), instead of randomly exchanging conditions
the null is formed from using all possible exchanges. This is known
as a permutation test (or exact test).
# Here we have to do a bit of gymnastics to get our function to do
# a permutation test without correcting for multiple comparisons:
X.shape = (n_subjects, n_src) # flatten the array for simplicity
titles.append('Permutation')
ts.append(np.zeros(width * width))
ps.append(np.zeros(width * width))
mccs.append(False)
for ii in range(n_src):
ts[1][ii], ps[1][ii] = permutation_t_test(X[:, [ii]], verbose=False)[:2]
plot_t_p(ts[1], ps[1], titles[1], mccs[1])
Multiple comparisons#
So far, we have done no correction for multiple comparisons. This is potentially problematic for these data because there are \(40 \cdot 40 = 1600\) tests being performed. If we use a threshold p < 0.05 for each individual test, we would expect many voxels to be declared significant even if there were no true effect. In other words, we would make many type I errors (adapted from here):
Null hypothesis 


True 
False 

Reject 
Yes 


No 


To see why, consider a standard \(\alpha = 0.05\). For a single test, our probability of making a type I error is 0.05. The probability of making at least one type I error in \(N_{\mathrm{test}}\) independent tests is then given by \(1  (1  \alpha)^{N_{\mathrm{test}}}\):
To combat this problem, several methods exist. Typically these provide control over either one of the following two measures:
 Familywise error rate (FWER)
The probability of making one or more type I errors:
\[\mathrm{P}(N_{\mathrm{type\ I}} >= 1 \mid H_0)\]
 False discovery rate (FDR)
The expected proportion of rejected null hypotheses that are actually true:
\[\mathrm{E}(\frac{N_{\mathrm{type\ I}}}{N_{\mathrm{reject}}} \mid N_{\mathrm{reject}} > 0) \cdot \mathrm{P}(N_{\mathrm{reject}} > 0 \mid H_0)\]
We cover some techniques that control FWER and FDR below.
Bonferroni correction#
Perhaps the simplest way to deal with multiple comparisons, Bonferroni correction conservatively multiplies the pvalues by the number of comparisons to control the FWER.
False discovery rate (FDR) correction#
Typically FDR is performed with the BenjaminiHochberg procedure, which is less restrictive than Bonferroni correction for large numbers of comparisons (fewer type II errors), but provides less strict control of type I errors.
Nonparametric resampling test with a maximum statistic#
Nonparametric resampling tests can also be used to correct for multiple comparisons. In its simplest form, we again do permutations using exchangeability under the null hypothesis, but this time we take the maximum statistic across all voxels in each permutation to form the null distribution. The pvalue for each voxel from the veridical data is then given by the proportion of null distribution values that were smaller.
This method has two important features:
It controls FWER.
It is nonparametric. Even though our initial test statistic (here a 1sample ttest) is parametric, the null distribution for the null hypothesis rejection (the mean value across subjects is indistinguishable from zero) is obtained by permutations. This means that it makes no assumptions of Gaussianity (which do hold for this example, but do not in general for some types of processed neuroimaging data).
Clustering#
Each of the aforementioned multiple comparisons corrections have the disadvantage of not fully incorporating the correlation structure of the data, namely that points close to one another (e.g., in space or time) tend to be correlated. However, by defining the adjacency/adjacency/neighbor structure in our data, we can use clustering to compensate.
To use this, we need to rethink our null hypothesis. Instead of thinking about a null hypothesis about means per voxel (with one independent test per voxel), we consider a null hypothesis about sizes of clusters in our data, which could be stated like:
The distribution of spatial cluster sizes observed in two experimental conditions are drawn from the same probability distribution.
Here we only have a single condition and we contrast to zero, which can be thought of as:
The distribution of spatial cluster sizes is independent of the sign of the data.
In this case, we again do permutations with a maximum statistic, but, under each permutation, we:
Compute the test statistic for each voxel individually.
Threshold the test statistic values.
Cluster voxels that exceed this threshold (with the same sign) based on adjacency.
Retain the size of the largest cluster (measured, e.g., by a simple voxel count, or by the sum of voxel tvalues within the cluster) to build the null distribution.
After doing these permutations, the cluster sizes in our veridical data are compared to this null distribution. The pvalue associated with each cluster is again given by the proportion of smaller null distribution values. This can then be subjected to a standard pvalue threshold (e.g., p < 0.05) to reject the null hypothesis (i.e., find an effect of interest).
This reframing to consider cluster sizes rather than individual means maintains the advantages of the standard nonparametric permutation test – namely controlling FWER and making no assumptions of parametric data distribution. Critically, though, it also accounts for the correlation structure in the data – which in this toy case is spatial but in general can be multidimensional (e.g., spatiotemporal) – because the null distribution will be derived from data in a way that preserves these correlations.
However, there is a drawback. If a cluster significantly deviates from the null, no further inference on the cluster (e.g., peak location) can be made, as the entire cluster as a whole is used to reject the null. Moreover, because the test statistic concerns the full data, the null hypothesis (and our rejection of it) refers to the structure of the full data. For more information, see also the comprehensive FieldTrip tutorial.
Defining the adjacency matrix#
First we need to define our adjacency (sometimes called “neighbors”) matrix.
This is a square array (or sparse matrix) of shape (n_src, n_src)
that
contains zeros and ones to define which spatial points are neighbors, i.e.,
which voxels are adjacent to each other. In our case this
is quite simple, as our data are aligned on a rectangular grid.
Let’s pretend that our data were smaller – a 3 x 3 grid. Thinking about each voxel as being connected to the other voxels it touches, we would need a 9 x 9 adjacency matrix. The first row of this matrix contains the voxels in the flattened data that the first voxel touches. Since it touches the second element in the first row and the first element in the second row (and is also a neighbor to itself), this would be:
[1, 1, 0, 1, 0, 0, 0, 0, 0]
sklearn.feature_extraction
provides a convenient function for this:
from sklearn.feature_extraction.image import grid_to_graph # noqa: E402
mini_adjacency = grid_to_graph(3, 3).toarray()
assert mini_adjacency.shape == (9, 9)
print(mini_adjacency[0])
[1 1 0 1 0 0 0 0 0]
In general the adjacency between voxels can be more complex, such as those between sensors in 3D space, or timevarying activation at brain vertices on a cortical surface. MNE provides several convenience functions for computing adjacency matrices (see the Statistics API).
Standard clustering#
Here, since our data are on a grid, we can use adjacency=None
to
trigger optimized gridbased code, and run the clustering algorithm.
titles.append('Clustering')
# Reshape data to what is equivalent to (n_samples, n_space, n_time)
X.shape = (n_subjects, width, width)
# Compute threshold from t distribution (this is also the default)
threshold = stats.distributions.t.ppf(1  alpha, n_subjects  1)
t_clust, clusters, p_values, H0 = permutation_cluster_1samp_test(
X, n_jobs=None, threshold=threshold, adjacency=None,
n_permutations=n_permutations, out_type='mask')
# Put the cluster data in a viewable format
p_clust = np.ones((width, width))
for cl, p in zip(clusters, p_values):
p_clust[cl] = p
ts.append(t_clust)
ps.append(p_clust)
mccs.append(True)
plot_t_p(ts[1], ps[1], titles[1], mccs[1])
stat_fun(H1): min=3.195527 max=5.120434
Running initial clustering
Found 2 clusters
Permuting 510 times...
0%  : 0/510 [00:00<?, ?it/s]
13%#2  : 64/510 [00:00<00:00, 1821.74it/s]
24%##4  : 124/510 [00:00<00:00, 1793.27it/s]
36%###6  : 184/510 [00:00<00:00, 1784.77it/s]
47%####7  : 241/510 [00:00<00:00, 1756.56it/s]
61%######  : 310/510 [00:00<00:00, 1818.08it/s]
73%#######2  : 372/510 [00:00<00:00, 1820.05it/s]
86%########5  : 437/510 [00:00<00:00, 1835.93it/s]
99%#########8 : 503/510 [00:00<00:00, 1852.63it/s]
100%########## : 510/510 [00:00<00:00, 1843.75it/s]
Computing cluster pvalues
Done.
“Hat” variance adjustment#
This method can also be used in this context to correct for small variances 1:
titles.append(r'$\mathbf{C_{hat}}$')
stat_fun_hat = partial(ttest_1samp_no_p, sigma=sigma)
t_hat, clusters, p_values, H0 = permutation_cluster_1samp_test(
X, n_jobs=None, threshold=threshold, adjacency=None, out_type='mask',
n_permutations=n_permutations, stat_fun=stat_fun_hat, buffer_size=None)
p_hat = np.ones((width, width))
for cl, p in zip(clusters, p_values):
p_hat[cl] = p
ts.append(t_hat)
ps.append(p_hat)
mccs.append(True)
plot_t_p(ts[1], ps[1], titles[1], mccs[1])
stat_fun(H1): min=0.043603 max=3.127369
Running initial clustering
Found 1 clusters
Permuting 510 times...
0%  : 0/510 [00:00<?, ?it/s]
28%##8  : 144/510 [00:00<00:00, 4216.53it/s]
61%######  : 309/510 [00:00<00:00, 4554.61it/s]
87%########7  : 444/510 [00:00<00:00, 4359.51it/s]
100%########## : 510/510 [00:00<00:00, 4403.06it/s]
Computing cluster pvalues
Done.
Thresholdfree cluster enhancement (TFCE)#
TFCE eliminates the free parameter initial threshold
value that
determines which points are included in clustering by approximating
a continuous integration across possible threshold values with a standard
Riemann sum
2.
This requires giving a starting threshold start
and a step
size step
, which in MNE is supplied as a dict.
The smaller the step
and closer to 0 the start
value,
the better the approximation, but the longer it takes.
A significant advantage of TFCE is that, rather than modifying the statistical null hypothesis under test (from one about individual voxels to one about the distribution of clusters in the data), it modifies the data under test while still controlling for multiple comparisons. The statistical test is then done at the level of individual voxels rather than clusters. This allows for evaluation of each point independently for significance rather than only as cluster groups.
titles.append(r'$\mathbf{C_{TFCE}}$')
threshold_tfce = dict(start=0, step=0.2)
t_tfce, _, p_tfce, H0 = permutation_cluster_1samp_test(
X, n_jobs=None, threshold=threshold_tfce, adjacency=None,
n_permutations=n_permutations, out_type='mask')
ts.append(t_tfce)
ps.append(p_tfce)
mccs.append(True)
plot_t_p(ts[1], ps[1], titles[1], mccs[1])
stat_fun(H1): min=3.195527 max=5.120434
Running initial clustering
Using 26 thresholds from 0.00 to 5.00 for TFCE computation (h_power=2.00, e_power=0.50)
Found 1600 clusters
Permuting 510 times...
0%  : 0/510 [00:00<?, ?it/s]
0%  : 2/510 [00:00<00:08, 58.33it/s]
1%  : 5/510 [00:00<00:06, 73.82it/s]
2%1  : 8/510 [00:00<00:06, 79.03it/s]
2%2  : 12/510 [00:00<00:05, 89.58it/s]
3%2  : 15/510 [00:00<00:05, 89.42it/s]
4%3  : 19/510 [00:00<00:05, 94.89it/s]
5%4  : 23/510 [00:00<00:04, 98.69it/s]
5%5  : 26/510 [00:00<00:04, 97.20it/s]
6%5  : 29/510 [00:00<00:05, 96.04it/s]
6%6  : 32/510 [00:00<00:05, 95.15it/s]
7%6  : 35/510 [00:00<00:05, 94.41it/s]
8%7  : 40/510 [00:00<00:04, 100.22it/s]
8%8  : 43/510 [00:00<00:04, 99.06it/s]
9%9  : 47/510 [00:00<00:04, 100.92it/s]
10%9  : 50/510 [00:00<00:04, 99.78it/s]
11%#  : 54/510 [00:00<00:04, 101.43it/s]
11%#1  : 57/510 [00:00<00:04, 100.33it/s]
12%#1  : 61/510 [00:00<00:04, 101.78it/s]
13%#2  : 64/510 [00:00<00:04, 100.75it/s]
13%#3  : 67/510 [00:00<00:04, 99.83it/s]
14%#3  : 71/510 [00:00<00:04, 101.23it/s]
15%#4  : 74/510 [00:00<00:04, 100.28it/s]
15%#5  : 77/510 [00:00<00:04, 99.46it/s]
16%#5  : 81/510 [00:00<00:04, 100.79it/s]
17%#6  : 85/510 [00:00<00:04, 101.97it/s]
17%#7  : 89/510 [00:00<00:04, 103.08it/s]
18%#8  : 92/510 [00:00<00:04, 102.14it/s]
19%#8  : 96/510 [00:00<00:04, 103.20it/s]
19%#9  : 99/510 [00:00<00:04, 102.28it/s]
20%##  : 103/510 [00:01<00:03, 103.28it/s]
21%##  : 107/510 [00:01<00:03, 104.23it/s]
22%##1  : 111/510 [00:01<00:03, 105.10it/s]
23%##2  : 115/510 [00:01<00:03, 105.89it/s]
23%##3  : 118/510 [00:01<00:03, 104.85it/s]
24%##3  : 121/510 [00:01<00:03, 103.88it/s]
25%##4  : 125/510 [00:01<00:03, 104.74it/s]
25%##5  : 129/510 [00:01<00:03, 105.54it/s]
26%##6  : 133/510 [00:01<00:03, 106.28it/s]
27%##6  : 136/510 [00:01<00:03, 105.28it/s]
27%##7  : 140/510 [00:01<00:03, 105.99it/s]
28%##8  : 143/510 [00:01<00:03, 104.94it/s]
29%##8  : 147/510 [00:01<00:03, 105.68it/s]
29%##9  : 149/510 [00:01<00:03, 103.05it/s]
30%##9  : 152/510 [00:01<00:03, 102.23it/s]
31%###  : 156/510 [00:01<00:03, 103.11it/s]
31%###1  : 160/510 [00:01<00:03, 103.95it/s]
32%###2  : 164/510 [00:01<00:03, 104.73it/s]
33%###2  : 167/510 [00:01<00:03, 103.84it/s]
33%###3  : 170/510 [00:01<00:03, 103.01it/s]
34%###3  : 173/510 [00:01<00:03, 102.23it/s]
34%###4  : 175/510 [00:01<00:03, 99.90it/s]
35%###4  : 177/510 [00:01<00:03, 97.71it/s]
35%###5  : 179/510 [00:01<00:03, 95.64it/s]
35%###5  : 181/510 [00:01<00:03, 93.70it/s]
36%###5  : 183/510 [00:01<00:03, 91.87it/s]
36%###6  : 185/510 [00:01<00:03, 90.13it/s]
37%###6  : 188/510 [00:01<00:03, 90.05it/s]
37%###7  : 190/510 [00:01<00:03, 88.43it/s]
38%###8  : 194/510 [00:01<00:03, 89.92it/s]
39%###8  : 197/510 [00:02<00:03, 89.85it/s]
39%###9  : 201/510 [00:02<00:03, 91.34it/s]
40%####  : 205/510 [00:02<00:03, 92.74it/s]
41%####  : 207/510 [00:02<00:03, 90.99it/s]
41%####1  : 210/510 [00:02<00:03, 90.86it/s]
42%####1  : 212/510 [00:02<00:03, 89.21it/s]
42%####2  : 215/510 [00:02<00:03, 89.18it/s]
43%####2  : 218/510 [00:02<00:03, 89.14it/s]
44%####3  : 222/510 [00:02<00:03, 90.64it/s]
44%####4  : 225/510 [00:02<00:03, 90.53it/s]
45%####4  : 228/510 [00:02<00:03, 90.44it/s]
45%####5  : 230/510 [00:02<00:03, 88.83it/s]
46%####5  : 233/510 [00:02<00:03, 88.83it/s]
46%####6  : 236/510 [00:02<00:03, 88.79it/s]
47%####6  : 239/510 [00:02<00:03, 88.78it/s]
48%####7  : 243/510 [00:02<00:02, 90.27it/s]
48%####8  : 245/510 [00:02<00:02, 88.67it/s]
48%####8  : 247/510 [00:02<00:03, 87.17it/s]
49%####9  : 250/510 [00:02<00:02, 87.24it/s]
50%####9  : 254/510 [00:02<00:02, 88.80it/s]
50%#####  : 256/510 [00:02<00:02, 87.23it/s]
51%#####  : 260/510 [00:02<00:02, 88.79it/s]
51%#####1  : 262/510 [00:02<00:02, 87.29it/s]
52%#####1  : 265/510 [00:02<00:02, 87.35it/s]
53%#####2  : 268/510 [00:02<00:02, 87.41it/s]
53%#####2  : 270/510 [00:02<00:02, 85.98it/s]
54%#####3  : 273/510 [00:02<00:02, 86.13it/s]
54%#####4  : 276/510 [00:02<00:02, 86.26it/s]
55%#####4  : 278/510 [00:02<00:02, 84.83it/s]
55%#####5  : 282/510 [00:03<00:02, 86.51it/s]
56%#####5  : 285/510 [00:03<00:02, 86.61it/s]
57%#####6  : 289/510 [00:03<00:02, 88.19it/s]
57%#####7  : 292/510 [00:03<00:02, 88.23it/s]
58%#####8  : 296/510 [00:03<00:02, 89.62it/s]
58%#####8  : 298/510 [00:03<00:02, 88.05it/s]
59%#####9  : 302/510 [00:03<00:02, 89.56it/s]
60%#####9  : 305/510 [00:03<00:02, 89.50it/s]
60%######  : 308/510 [00:03<00:02, 89.46it/s]
61%######  : 311/510 [00:03<00:02, 89.41it/s]
62%######1  : 314/510 [00:03<00:02, 89.38it/s]
62%######2  : 318/510 [00:03<00:02, 90.83it/s]
63%######3  : 322/510 [00:03<00:02, 92.21it/s]
64%######3  : 325/510 [00:03<00:02, 92.03it/s]
65%######4  : 329/510 [00:03<00:01, 93.23it/s]
65%######5  : 332/510 [00:03<00:01, 92.98it/s]
66%######5  : 336/510 [00:03<00:01, 94.25it/s]
66%######6  : 339/510 [00:03<00:01, 93.90it/s]
67%######7  : 343/510 [00:03<00:01, 95.12it/s]
68%######7  : 346/510 [00:03<00:01, 94.79it/s]
68%######8  : 349/510 [00:03<00:01, 94.49it/s]
69%######9  : 353/510 [00:03<00:01, 95.68it/s]
70%######9  : 356/510 [00:03<00:01, 95.22it/s]
70%#######  : 358/510 [00:03<00:01, 93.42it/s]
71%#######  : 362/510 [00:03<00:01, 94.66it/s]
71%#######1  : 364/510 [00:03<00:01, 92.89it/s]
72%#######1  : 367/510 [00:03<00:01, 92.68it/s]
73%#######2  : 371/510 [00:03<00:01, 93.87it/s]
73%#######3  : 373/510 [00:03<00:01, 92.12it/s]
74%#######3  : 375/510 [00:04<00:01, 90.46it/s]
74%#######4  : 378/510 [00:04<00:01, 90.33it/s]
75%#######4  : 380/510 [00:04<00:01, 88.75it/s]
75%#######4  : 382/510 [00:04<00:01, 87.27it/s]
75%#######5  : 384/510 [00:04<00:01, 85.86it/s]
76%#######5  : 386/510 [00:04<00:01, 84.49it/s]
76%#######6  : 389/510 [00:04<00:01, 84.71it/s]
77%#######6  : 392/510 [00:04<00:01, 84.91it/s]
77%#######7  : 394/510 [00:04<00:01, 83.62it/s]
78%#######7  : 397/510 [00:04<00:01, 83.87it/s]
78%#######8  : 400/510 [00:04<00:01, 84.11it/s]
79%#######8  : 402/510 [00:04<00:01, 82.87it/s]
79%#######9  : 405/510 [00:04<00:01, 82.92it/s]
80%#######9  : 407/510 [00:04<00:01, 81.72it/s]
80%########  : 410/510 [00:04<00:01, 82.07it/s]
81%########  : 412/510 [00:04<00:01, 80.92it/s]
81%########1  : 415/510 [00:04<00:01, 81.30it/s]
82%########1  : 417/510 [00:04<00:01, 80.18it/s]
82%########2  : 420/510 [00:04<00:01, 80.60it/s]
83%########2  : 423/510 [00:04<00:01, 80.98it/s]
84%########3  : 426/510 [00:04<00:01, 81.37it/s]
84%########4  : 429/510 [00:04<00:00, 81.72it/s]
85%########4  : 431/510 [00:04<00:00, 80.54it/s]
85%########5  : 434/510 [00:04<00:00, 80.94it/s]
86%########5  : 437/510 [00:04<00:00, 81.32it/s]
86%########6  : 439/510 [00:04<00:00, 80.22it/s]
87%########6  : 442/510 [00:04<00:00, 80.64it/s]
87%########7  : 444/510 [00:04<00:00, 79.57it/s]
88%########7  : 447/510 [00:04<00:00, 80.01it/s]
88%########8  : 450/510 [00:04<00:00, 80.43it/s]
89%########8  : 452/510 [00:05<00:00, 79.37it/s]
89%########9  : 455/510 [00:05<00:00, 79.84it/s]
90%#########  : 459/510 [00:05<00:00, 81.74it/s]
91%#########  : 462/510 [00:05<00:00, 82.07it/s]
91%#########  : 464/510 [00:05<00:00, 80.92it/s]
92%#########1 : 468/510 [00:05<00:00, 82.77it/s]
92%#########2 : 471/510 [00:05<00:00, 83.06it/s]
93%#########2 : 474/510 [00:05<00:00, 83.35it/s]
94%#########3 : 478/510 [00:05<00:00, 85.09it/s]
94%#########4 : 481/510 [00:05<00:00, 85.26it/s]
95%#########4 : 483/510 [00:05<00:00, 83.96it/s]
95%#########5 : 485/510 [00:05<00:00, 82.71it/s]
95%#########5 : 487/510 [00:05<00:00, 81.54it/s]
96%#########6 : 490/510 [00:05<00:00, 81.90it/s]
97%#########6 : 494/510 [00:05<00:00, 83.71it/s]
97%#########7 : 497/510 [00:05<00:00, 83.95it/s]
98%#########8 : 500/510 [00:05<00:00, 84.17it/s]
99%#########8 : 504/510 [00:05<00:00, 85.87it/s]
99%#########9 : 507/510 [00:05<00:00, 86.01it/s]
100%########## : 510/510 [00:05<00:00, 87.73it/s]
100%########## : 510/510 [00:05<00:00, 90.26it/s]
Computing cluster pvalues
Done.
We can also combine TFCE and the “hat” correction:
titles.append(r'$\mathbf{C_{hat,TFCE}}$')
t_tfce_hat, _, p_tfce_hat, H0 = permutation_cluster_1samp_test(
X, n_jobs=None, threshold=threshold_tfce, adjacency=None, out_type='mask',
n_permutations=n_permutations, stat_fun=stat_fun_hat, buffer_size=None)
ts.append(t_tfce_hat)
ps.append(p_tfce_hat)
mccs.append(True)
plot_t_p(ts[1], ps[1], titles[1], mccs[1])
stat_fun(H1): min=0.043603 max=3.127369
Running initial clustering
Using 16 thresholds from 0.00 to 3.00 for TFCE computation (h_power=2.00, e_power=0.50)
Found 1600 clusters
Permuting 510 times...
0%  : 0/510 [00:00<?, ?it/s]
1%  : 3/510 [00:00<00:05, 87.74it/s]
2%1  : 8/510 [00:00<00:04, 118.23it/s]
3%2  : 14/510 [00:00<00:03, 138.88it/s]
4%3  : 18/510 [00:00<00:03, 133.18it/s]
5%4  : 23/510 [00:00<00:03, 136.41it/s]
6%5  : 29/510 [00:00<00:03, 144.08it/s]
7%6  : 34/510 [00:00<00:03, 144.70it/s]
7%7  : 38/510 [00:00<00:03, 140.67it/s]
8%8  : 43/510 [00:00<00:03, 141.56it/s]
9%9  : 48/510 [00:00<00:03, 142.28it/s]
10%#  : 52/510 [00:00<00:03, 139.29it/s]
11%#1  : 57/510 [00:00<00:03, 139.86it/s]
12%#2  : 63/510 [00:00<00:03, 143.69it/s]
14%#3  : 69/510 [00:00<00:03, 146.93it/s]
15%#4  : 75/510 [00:00<00:02, 149.75it/s]
16%#6  : 82/510 [00:00<00:02, 154.79it/s]
17%#7  : 88/510 [00:00<00:02, 156.74it/s]
19%#8  : 96/510 [00:00<00:02, 163.28it/s]
20%##  : 103/510 [00:00<00:02, 166.57it/s]
21%##1  : 109/510 [00:00<00:02, 167.20it/s]
23%##2  : 115/510 [00:00<00:02, 167.96it/s]
24%##3  : 121/510 [00:00<00:02, 168.59it/s]
25%##4  : 127/510 [00:00<00:02, 169.20it/s]
26%##5  : 132/510 [00:00<00:02, 167.70it/s]
27%##6  : 137/510 [00:00<00:02, 166.33it/s]
28%##7  : 142/510 [00:00<00:02, 165.07it/s]
29%##8  : 147/510 [00:00<00:02, 163.92it/s]
30%##9  : 152/510 [00:00<00:02, 162.86it/s]
31%###  : 157/510 [00:00<00:02, 161.88it/s]
32%###1  : 162/510 [00:01<00:02, 160.96it/s]
33%###2  : 166/510 [00:01<00:02, 158.23it/s]
34%###3  : 171/510 [00:01<00:02, 157.55it/s]
35%###4  : 176/510 [00:01<00:02, 156.87it/s]
35%###5  : 179/510 [00:01<00:02, 152.51it/s]
36%###5  : 183/510 [00:01<00:02, 150.43it/s]
37%###6  : 187/510 [00:01<00:02, 148.53it/s]
37%###7  : 190/510 [00:01<00:02, 145.00it/s]
38%###8  : 194/510 [00:01<00:02, 143.43it/s]
39%###8  : 197/510 [00:01<00:02, 140.28it/s]
39%###9  : 200/510 [00:01<00:02, 137.31it/s]
40%###9  : 203/510 [00:01<00:02, 134.53it/s]
41%####  : 208/510 [00:01<00:02, 135.23it/s]
42%####1  : 212/510 [00:01<00:02, 134.26it/s]
42%####2  : 216/510 [00:01<00:02, 133.35it/s]
43%####2  : 219/510 [00:01<00:02, 130.85it/s]
44%####3  : 223/510 [00:01<00:02, 130.14it/s]
45%####4  : 227/510 [00:01<00:02, 129.46it/s]
46%####5  : 233/510 [00:01<00:02, 132.07it/s]
47%####6  : 238/510 [00:01<00:02, 132.89it/s]
48%####7  : 243/510 [00:01<00:01, 133.68it/s]
49%####8  : 248/510 [00:01<00:01, 134.42it/s]
50%####9  : 253/510 [00:01<00:01, 135.12it/s]
50%#####  : 256/510 [00:01<00:01, 132.63it/s]
51%#####  : 260/510 [00:01<00:01, 131.82it/s]
52%#####1  : 265/510 [00:01<00:01, 132.63it/s]
53%#####2  : 269/510 [00:01<00:01, 131.85it/s]
54%#####3  : 274/510 [00:01<00:01, 132.69it/s]
55%#####4  : 278/510 [00:01<00:01, 131.94it/s]
55%#####5  : 283/510 [00:02<00:01, 132.75it/s]
56%#####6  : 287/510 [00:02<00:01, 131.96it/s]
57%#####7  : 292/510 [00:02<00:01, 132.70it/s]
58%#####8  : 296/510 [00:02<00:01, 131.75it/s]
59%#####9  : 301/510 [00:02<00:01, 132.56it/s]
60%#####9  : 305/510 [00:02<00:01, 131.79it/s]
61%######  : 310/510 [00:02<00:01, 132.59it/s]
62%######1  : 314/510 [00:02<00:01, 131.84it/s]
62%######2  : 318/510 [00:02<00:01, 131.03it/s]
63%######3  : 323/510 [00:02<00:01, 131.90it/s]
64%######4  : 328/510 [00:02<00:01, 132.71it/s]
65%######5  : 332/510 [00:02<00:01, 131.97it/s]
66%######6  : 337/510 [00:02<00:01, 132.78it/s]
67%######7  : 342/510 [00:02<00:01, 133.45it/s]
68%######8  : 347/510 [00:02<00:01, 134.12it/s]
69%######8  : 351/510 [00:02<00:01, 133.26it/s]
70%######9  : 356/510 [00:02<00:01, 133.98it/s]
71%#######  : 361/510 [00:02<00:01, 134.68it/s]
72%#######1  : 366/510 [00:02<00:01, 135.32it/s]
73%#######2  : 370/510 [00:02<00:01, 134.43it/s]
74%#######3  : 375/510 [00:02<00:00, 135.08it/s]
75%#######4  : 380/510 [00:02<00:00, 135.71it/s]
75%#######5  : 385/510 [00:02<00:00, 136.04it/s]
76%#######6  : 390/510 [00:02<00:00, 136.62it/s]
78%#######7  : 396/510 [00:02<00:00, 138.66it/s]
79%#######8  : 402/510 [00:02<00:00, 140.59it/s]
80%########  : 408/510 [00:02<00:00, 142.45it/s]
81%########1  : 415/510 [00:02<00:00, 145.69it/s]
82%########1  : 418/510 [00:02<00:00, 142.81it/s]
83%########3  : 424/510 [00:02<00:00, 144.56it/s]
84%########3  : 428/510 [00:03<00:00, 143.23it/s]
85%########4  : 432/510 [00:03<00:00, 141.95it/s]
86%########5  : 437/510 [00:03<00:00, 142.24it/s]
86%########6  : 441/510 [00:03<00:00, 141.02it/s]
87%########7  : 446/510 [00:03<00:00, 141.34it/s]
88%########8  : 450/510 [00:03<00:00, 140.07it/s]
89%########9  : 454/510 [00:03<00:00, 138.96it/s]
89%########9  : 456/510 [00:03<00:00, 134.89it/s]
90%#########  : 461/510 [00:03<00:00, 135.52it/s]
92%#########1 : 467/510 [00:03<00:00, 137.58it/s]
93%#########2 : 472/510 [00:03<00:00, 138.07it/s]
94%#########3 : 477/510 [00:03<00:00, 138.43it/s]
95%#########4 : 483/510 [00:03<00:00, 140.35it/s]
96%#########5 : 488/510 [00:03<00:00, 140.71it/s]
97%#########6 : 493/510 [00:03<00:00, 141.04it/s]
98%#########7 : 498/510 [00:03<00:00, 141.34it/s]
99%#########8 : 503/510 [00:03<00:00, 141.57it/s]
100%#########9 : 508/510 [00:03<00:00, 141.87it/s]
100%########## : 510/510 [00:03<00:00, 141.23it/s]
Computing cluster pvalues
Done.
Visualize and compare methods#
Let’s take a look at these statistics. The top row shows each test statistic, and the bottom shows pvalues for various statistical tests, with the ones with proper control over FWER or FDR with bold titles.
fig = plt.figure(facecolor='w', figsize=(14, 3))
assert len(ts) == len(titles) == len(ps)
for ii in range(len(ts)):
ax = [fig.add_subplot(2, 10, ii + 1, projection='3d'),
fig.add_subplot(2, 10, 11 + ii)]
plot_t_p(ts[ii], ps[ii], titles[ii], mccs[ii], ax)
fig.tight_layout(pad=0, w_pad=0.05, h_pad=0.1)
plt.show()
The first three columns show the parametric and nonparametric statistics that are not corrected for multiple comparisons:
Mass univariate ttests result in jagged edges.
“Hat” variance correction of the ttests produces less peaky edges, correcting for sharpness in the statistic driven by lowvariance voxels.
Nonparametric resampling tests are very similar to ttests. This is to be expected: the data are drawn from a Gaussian distribution, and thus satisfy parametric assumptions.
The next three columns show multiple comparison corrections of the mass univariate tests (parametric and nonparametric). These too conservatively correct for multiple comparisons because neighboring voxels in our data are correlated:
Bonferroni correction eliminates any significant activity.
FDR correction is less conservative than Bonferroni.
A permutation test with a maximum statistic also eliminates any significant activity.
The final four columns show the nonparametric clusterbased permutation tests with a maximum statistic:
Standard clustering identifies the correct region. However, the whole area must be declared significant, so no peak analysis can be done. Also, the peak is broad.
Clustering with “hat” variance adjustment tightens the estimate of significant activity.
Clustering with TFCE allows analyzing each significant point independently, but still has a broadened estimate.
Clustering with TFCE and “hat” variance adjustment tightens the area declared significant (again FWER corrected).
Statistical functions in MNE#
The complete listing of statistical functions provided by MNE are in the Statistics API list, but we will give a brief overview here.
MNE provides several convenience parametric testing functions that can be used in conjunction with the nonparametric clustering methods. However, the set of functions we provide is not meant to be exhaustive.
If the univariate statistical contrast of interest is not listed here
(e.g., interaction term in an unbalanced ANOVA), consider checking out the
statsmodels
package. It offers many functions for computing
statistical contrasts, e.g., statsmodels.stats.anova.anova_lm()
.
To use these functions in clustering:
Determine which test statistic (e.g., tvalue, Fvalue) you would use in a univariate context to compute your contrast of interest. In other words, if there were only a single output such as reaction times, what test statistic might you compute on the data?
Wrap the call to that function within a function that takes an input of the same shape that is expected by your clustering function, and returns an array of the same shape without the “samples” dimension (e.g.,
mne.stats.permutation_cluster_1samp_test()
takes an array of shape(n_samples, p, q)
and returns an array of shape(p, q)
).Pass this wrapped function to the
stat_fun
argument to the clustering function.Set an appropriate
threshold
value (float or dict) based on the values your statistical contrast function returns.
Parametric methods provided by MNE#
mne.stats.ttest_1samp_no_p()
Paired ttest, optionally with hat adjustment. This is used by default for contrast enhancement in paired cluster tests.
mne.stats.f_oneway()
Oneway ANOVA for independent samples. This can be used to compute various Fcontrasts. It is used by default for contrast enhancement in nonpaired cluster tests.
mne.stats.f_mway_rm()
Mway ANOVA for repeated measures and balanced designs. This returns Fstatistics and pvalues. The associated helper function
mne.stats.f_threshold_mway_rm()
can be used to determine the Fthreshold at a given significance level.
mne.stats.linear_regression()
Compute ordinary least square regressions on multiple targets, e.g., sensors, time points across trials (samples). For each regressor it returns the beta value, tstatistic, and uncorrected pvalue. While it can be used as a test, it is particularly useful to compute weighted averages or deal with continuous predictors.
Nonparametric methods#
mne.stats.permutation_cluster_test()
Unpaired contrasts with clustering.
mne.stats.spatio_temporal_cluster_test()
Unpaired contrasts with spatiotemporal clustering.
mne.stats.permutation_t_test()
Paired contrast with no clustering.
mne.stats.permutation_cluster_1samp_test()
Paired contrasts with clustering.
mne.stats.spatio_temporal_cluster_1samp_test()
Paired contrasts with spatiotemporal clustering.
Warning
In most MNE functions, data has shape
(..., n_space, n_time)
, where the spatial dimension can
be e.g. sensors or source vertices. But for our spatiotemporal
clustering functions, the spatial dimensions need to be last
for computational efficiency reasons. For example, for
mne.stats.spatio_temporal_cluster_1samp_test()
, X
needs to be of shape (n_samples, n_time, n_space)
. You can
use numpy.transpose()
to transpose axes if necessary.
References#
 1(1,2,3)
Gerard R. Ridgway, Vladimir Litvak, Guillaume Flandin, Karl J. Friston, and Will D. Penny. The problem of low variance voxels in statistical parametric mapping; a new hat avoids a ‘haircut’. NeuroImage, 59(3):2131–2141, 2012. doi:10.1016/j.neuroimage.2011.10.027.
 2
Stephen M. Smith and Thomas E. Nichols. Thresholdfree cluster enhancement: addressing problems of smoothing, threshold dependence and localisation in cluster inference. NeuroImage, 44(1):83–98, 2009. doi:10.1016/j.neuroimage.2008.03.061.
Total running time of the script: ( 0 minutes 31.614 seconds)
Estimated memory usage: 17 MB