Second-level fMRI model: two-sample test, unpaired and paired#

Full step-by-step example of fitting a GLM to perform a second level analysis in experimental data and visualizing the results

More specifically:

  1. A sample of n=16 visual activity fMRIs are downloaded.

2. An unpaired, two-sample t-test is applied to the brain maps in order to see the effect of the contrast difference across subjects.

3. A paired, two-sample t-test is applied to the brain maps in order to see the effect of the contrast difference across subjects, considering subject intercepts

The contrast is between responses to retinotopically distinct vertical versus horizontal checkerboards. At the individual level, these stimuli are sometimes used to map the borders of primary visual areas. At the group level, such a mapping is not possible. Yet, we may observe some significant effects in these areas.

import pandas as pd
from nilearn import plotting
from nilearn.datasets import fetch_localizer_contrasts
import matplotlib.pyplot as plt

Fetch dataset#

We download a list of left vs right button press contrasts from a localizer dataset.

n_subjects = 16
sample_vertical = fetch_localizer_contrasts(
    ["vertical checkerboard"], n_subjects,
    get_tmaps=True, legacy_format=False
)
sample_horizontal = fetch_localizer_contrasts(
    ["horizontal checkerboard"], n_subjects,
    get_tmaps=True, legacy_format=False
)

# Implicitly, there is a one-to-one correspondence between the two samples:
# the first image of both samples comes from subject S1, the second from subject S2 etc.
Downloading data from https://osf.io/download/5d27c2c41c5b4a001d9f4e7e/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d27ce321c5b4a001aa080fc/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d27d3c3114a42001804500a/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d27d175a26b340018084d23/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d27e5fa1c5b4a001aa09681/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d27e900a26b340017083df6/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d27f18945253a00193cb2dd/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d27f8a845253a001b3c3280/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d2808401c5b4a001d9f83b2/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d280aab45253a001b3c3d51/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d2811fba26b340017085492/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d280d0a114a42001704b6a7/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d282b2345253a001c3e7d09/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d2829f7a26b34001708642d/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d28318445253a00193ce6d7/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d28399545253a001c3e9288/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d2848581c5b4a001aa10aac/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d2849cc114a42001804994e/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d28545ca26b340018089ba7/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d2852861c5b4a001c9ed5bf/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d285cd945253a001a3c8509/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d285c8b45253a00193d0295/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d286e49114a42001904ab90/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d286b6b45253a001c3ec067/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d288af11c5b4a001d9ff0cb/ ...
 ...done. (1 seconds, 0 min)
Downloading data from https://osf.io/download/5d2881cd1c5b4a001d9fe799/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d289be945253a001c3ef5e2/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d288c4c114a42001804bfb6/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d28a1c91c5b4a001da00bd9/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d28ad481c5b4a001aa17d00/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d28bb90a26b3400190925d2/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d28c24f114a42001904ea5b/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d27ccde1c5b4a001d9f5602/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d27c7f01c5b4a001c9e778e/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d27d9c6114a420019045370/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d27d99fa26b340018085146/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d27de38a26b340016099771/ ...
 ...done. (1 seconds, 0 min)
Downloading data from https://osf.io/download/5d27ec4d45253a001c3e3f47/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d27fb651c5b4a001d9f7938/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d27fb1e1c5b4a001aa0ab78/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d280057a26b340019089965/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d27ff2645253a001c3e4fb9/ ...
 ...done. (1 seconds, 0 min)
Downloading data from https://osf.io/download/5d2814d145253a001c3e6404/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d280e3945253a001a3c4f15/ ...
 ...done. (1 seconds, 0 min)
Downloading data from https://osf.io/download/5d28244745253a001b3c4afa/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d2827ff114a420018047f9a/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d28309645253a001a3c6a8d/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d282ef61c5b4a001b9f3747/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d284a3445253a001c3ea2d1/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d284636114a42001704e0f6/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d28564b1c5b4a001d9fc9d6/ ...
 ...done. (1 seconds, 0 min)
Downloading data from https://osf.io/download/5d28516e45253a001a3c7e03/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d285b6c1c5b4a001c9edada/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d285cffa26b34001908db9c/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d28765645253a001b3c8106/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d287486114a42001804af82/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d287eeb45253a001c3ed1ba/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d287f8f114a4200170503d0/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d2896fb45253a001a3cabe0/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d288e101c5b4a001aa14e0f/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d28af541c5b4a001da01caa/ ...
 ...done. (2 seconds, 0 min)
Downloading data from https://osf.io/download/5d28aa701c5b4a001aa1794c/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d28b9af45253a001a3ccb85/ ...
 ...done. (3 seconds, 0 min)
Downloading data from https://osf.io/download/5d28b579114a42001804e2cd/ ...
 ...done. (2 seconds, 0 min)

Estimate second level models#

We define the input maps and the design matrix for the second level model and fit it.

Next, we model the effect of conditions (sample 1 vs sample 2).

import numpy as np
condition_effect = np.hstack(([1] * n_subjects, [- 1] * n_subjects))

The design matrix for the unpaired test doesn’t need any more columns For the paired test, we include an intercept for each subject.

subject_effect = np.vstack((np.eye(n_subjects), np.eye(n_subjects)))
subjects = [f'S{i:02d}' for i in range(1, n_subjects + 1)]

We then assemble those into design matrices

unpaired_design_matrix = pd.DataFrame(
    condition_effect[:, np.newaxis],
    columns=['vertical vs horizontal'])

paired_design_matrix = pd.DataFrame(
    np.hstack((condition_effect[:, np.newaxis], subject_effect)),
    columns=['vertical vs horizontal'] + subjects)

and plot the designs.

from nilearn.plotting import plot_design_matrix
_, (ax_unpaired, ax_paired) = plt.subplots(1,2, gridspec_kw={'width_ratios': [1, 17]})
plot_design_matrix(unpaired_design_matrix, rescale=False, ax=ax_unpaired)
plot_design_matrix(paired_design_matrix, rescale=False, ax=ax_paired)
ax_unpaired.set_title('unpaired design', fontsize=12)
ax_paired.set_title('paired design', fontsize=12)
plt.tight_layout()
plotting.show()
unpaired design, paired design

We specify the analysis models and fit them.

Estimating the contrast is simple. To do so, we provide the column name of the design matrix. The argument ‘output_type’ is set to return all available outputs so that we can compare differences in the effect size, variance, and z-score.

stat_maps_unpaired = second_level_model_unpaired.compute_contrast(
                                                    'vertical vs horizontal',
                                                    output_type='all')

stat_maps_paired = second_level_model_paired.compute_contrast(
                                                'vertical vs horizontal',
                                                output_type='all')

Plot the results#

The two effect_size images are essentially identical

(stat_maps_unpaired['effect_size'].get_fdata()
    - stat_maps_paired['effect_size'].get_fdata()).max()
9.992007221626409e-16

But the variance in the unpaired image is larger.

plotting.plot_glass_brain(
    stat_maps_unpaired['effect_variance'], colorbar=True, vmin=0, vmax=6,
    title='vertical vs horizontal effect variance, unpaired')

plotting.plot_glass_brain(
    stat_maps_paired['effect_variance'], colorbar=True, vmin=0, vmax=6,
    title='vertical vs horizontal effect variance, paired')

plotting.show()
  • plot second level two sample test
  • plot second level two sample test

Together, this makes the z_scores from the paired test larger. We threshold the second level contrast and plot it.

threshold = 3.1  # corresponds to  p < .001, uncorrected
display = plotting.plot_glass_brain(
    stat_maps_unpaired['z_score'], threshold=threshold, colorbar=True, plot_abs=False,
    title='vertical vs horizontal (unc p<0.001)', vmin=0, vmax=6)

display = plotting.plot_glass_brain(
    stat_maps_paired['z_score'], threshold=threshold, colorbar=True, plot_abs=False,
    title='vertical vs horizontal (unc p<0.001)', vmin=0, vmax=6)

plotting.show()
  • plot second level two sample test
  • plot second level two sample test

Unsurprisingly, we see activity in the primary visual cortex, both positive and negative.

Total running time of the script: ( 2 minutes 53.006 seconds)

Estimated memory usage: 9 MB

Gallery generated by Sphinx-Gallery