# 2.4.1. Example of second level design matrix¶

The shows how a second-level design matrix is specified: assuming that the data refer to a group of individuals, with one image per subject, the design matrix typically holds the characteristics of each individual.

This is used in a second-level analysis to assess the impact of these characteristics on brain signals.

Requires matplotlib.

```try:
import matplotlib.pyplot as plt
except ImportError:
raise RuntimeError("This script needs the matplotlib library")
```

## 2.4.1.1. Create a simple experimental paradigm¶

We want to get the group result of a contrast for 20 subjects

```n_subjects = 20
subjects_label = ['sub-%02d' % i for i in range(1, n_subjects + 1)]
```

Specify extra information about the subjects to create confounders Without confounders the design matrix would correspond to a one sample test

```import pandas as pd
extra_info_subjects = pd.DataFrame({'subject_label': subjects_label,
'age': range(15, 15 + n_subjects),
'sex': [0, 1] * int(n_subjects / 2)})
```

## 2.4.1.2. Create a second level design matrix¶

```from nistats.design_matrix import make_second_level_design_matrix
design_matrix = make_second_level_design_matrix(subjects_label, extra_info_subjects)
```

Out:

```/home/kshitij/workspace/nistats-org/nistats-repo/kchawla-pi/nistats/nistats/design_matrix.py:460: UserWarning: Attention: Design matrix is singular. Aberrant estimates are expected.
warn('Attention: Design matrix is singular. Aberrant estimates '
```

plot the results

```from nistats.reporting import plot_design_matrix
ax = plot_design_matrix(design_matrix)
ax.set_title('Second level design matrix', fontsize=12)
ax.set_ylabel('maps')
plt.tight_layout()
plt.show()
``` Out:

```/home/kshitij/.programs/anaconda3/envs/nistats-py36-latest/lib/python3.6/site-packages/matplotlib/figure.py:445: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
% get_backend())
```

Total running time of the script: ( 0 minutes 0.251 seconds)

Gallery generated by Sphinx-Gallery