MatLab Training Course Notes
Session 4

Plotting With Statistical Parametric Mapping (SPM)

Instructors for this session:
Bill Miller
Dynamic Neuroimaging Laboratory
415.502.3726
http://dnl.ucsf.edu

Dave McGonigle
BioMagnetic Imaging Laboratory
davemc@radiology.ucsf.edu

Topics for Session 4

In this special session we focused on plotting variables generated by the fMRI analysis package, Statistical Parametric Mapping (SPM), which is written with MatLab. This session assumes familiarity with SPM, especially with performing contrast analysis.

  1. Structure variables in MatLab
  2. SPM Basics
  3. Using SPM to plot variables of interest
  4. Accessing and plotting SPM variables in the base workspace

Structure variables in MatLab

Since SPM makes use of structure variables, we need to examine them briefly here. Structures are variables that can contain many "fields" of information. They are a very powerful and compact way to organize information in MatLab.

For instance, it is common in event-related analyses to have different numbers of events in each event class. So, say you have 5 different event classes - 3 with 11 events, 2 with 15 events. You would like to store these as a single matrix (for clarity and parsimony - and also because it gets incredibly tedious typing in design matrices when you are doing multi-subject analyses). However, what size will the matrix be? If it's 11 x 5 - you lose the extra events in the other event class. If its 15 x 5 - you have 'empty entries' in some of the columns, which will give SPM problems. The structure approach array gets around this, by allowing you to use different fields for different types of related data, and even different sized data in different indexes of the same field.

The simplest way to think about structures is in the context of making a database, say, for an address book: In an address book database, each "record" (i.e., for one person) includes associated fields for name, address, phone number, etc. For instance,

Record IndexNameAddressMail-StopPhoneEmail
1.Bill MillerDNL06282-3726bill.miller@radiology
2.Dave McGonigleBiomag Imaging Lab0628davemc@radiology
::::::

Note that records 1 and 2 contain the same fields, although the values in a given field may or may not be the same. Also note that the name, address, and email fields are strings of alphanumeric text, while the phone and mail-stop fields are numeric.

We can create this address database in MatLab by typing the following at the command line:

   addressbook(1).Name='Bill Miller';
   addressbook(1).Address='DNL';
   addressbook(1).Mail_Stop=0628;
   addressbook(1).Phone=23726;
   addressbook(1).Email='bill.miller@radiology';
   addressbook(2).Name='Dave McGonigle';
   addressbook(2).Address='Biomag Imaging Lab';
   addressbook(2).Mail_Stop=0628;
   addressbook(2).Email='davemc@radiology';

Typing whos addressbook produces the following output:

NameSizeBytesClass
addressbook1x21270struct array

The output of whos tells us that we've created a structure variable "addressbook" which has two columns (corresponding to the two records).

In the above example, fields were created "on-the-fly" by using the period (".") after the main variable name. To see the field names of the structure variable, just type the variable name, or alternatively, use the fieldnames function, e.g.

    fieldnames(addressbook)
To see the actual content of all fields for one index of the structure (i.e., one record of the database), type the variable name with the appropriate index, e.g.
    addressbook(1)
To access the value of only one field, type the variable name, index, and field name, e.g.
    addressbook(2).Name

Matlab also provides functions for creating structures, and getting and setting field values. Take a look at the help information for the struct, setfield, and getfield functions.


2. SPM Basics

The SPM software is a suite of MatLab functions and subroutines (with some externally compiled C routines) for functional PET and MRI brain image analysis that implements "statistical parametric mapping". SPM was developed by members and collaborators of the Functional Imaging Laboratory (FIL), Institute of Neurology, London and is distributed as open source code (http://www.fil.ion.ucl.ac.uk/spm). A discussion of SPM's capabilities and features is beyond our scope. Here, we assume some familiarity with the SPM software and functional imaging analysis, and we draw on ideas developed in previous MatLab sessions to plot quantities of interest that exceed SPM's limited display capabilities. (See the SPM website for software downloading and installation. At this writing (March 2001) the latest version is SPM99).

In the class, we used an example from an fMRI study processed with SPM through the GLM step (before contrast analysis). The points discussed here involve variables used in contrast analysis. These on-line notes should be valid for any typical SPM data set, if you want to follow along with this tutorial using your own data. We assume that you know how to perform contrast analysis in SPM.

SPMstart-up: Start SPM in fMRI mode by typing "spm fmri" at the MatLab prompt The SPM "environment" consists of the three windows that appear, in addition to the MatLab command window. Even though SPM primarily uses a Graphical User Interface (GUI) to interact with the user, it is also possible to use the MatLab command line to drive SPM. This basic point will be be expanded upon below.


3. Using SPM to Plot Variables of Interest

Here we will look at plotting quantities of interest after a contrast analysis using SPM's graphical user interface. To follow along, load your processed dataset into SPM and perform a contrast analysis by pressing the 'results' button, and selecting the appropriate data file ("spm.mat") in the directory where the your data is located.

In our example, we are interested in those voxels that show a statistically significant positive covariation with our convolved boxcar regressor (our example data was from a "block" design fMRI study). Thus, we have a model, and the goodness of fit of the model to the data will determine the significance of the statistic produced at each voxel. This will be displayed as an 'SPM' in the large graphics window. It is useful to remember at all times that the SPM is an image of statistics - NOT of effect sizes.

There are a number of ways to interrogate the maps that SPM produces. Once the program has evaluated the contrast that you have defined (in the present case, the boxcar that corresponds to the timecourse of our experimental paradigm), it displays three Maximum Intensity Projections (MIPs). Each MIP is an orthogonal 2D plane through the 3D SPM, and contain voxels of different intensities that correspond to their statistical significance. Select one by right-clicking in the general space where the MIPs are, and select 'global maximum' from the pull-down menu. The cursor will go automatically to the voxel of highest significance. The graphics window will change to tell you the co-ordinates and statistical information relating to the voxel you have selected. However, this will not give you any graphical information on the voxel you have selected.

To look at plots, select the 'plot' button from the bottom left GUI. There are a number of different variables that may be plotted.

- Contrast of parameter estimates
This is best understood, simply, as a regression coefficient (since SPM is effectively evaluating a multilinear regression model at each voxel) for the contrast of interest. This is a 'BETA' in the parlance of SPM from the matrix formulation of the GLM i.e.
Y(data) = G*BETA + e
where G is the model ("design matrix"), BETA contains the parameters, and e is error (hopefully IID). This plots the SIZE of the relationship, and its SIGN i.e. is it a +ve or a -ve relationship. The error bar is the standard error at this voxel. However, in multilinear regression, expecially with complex regressors, it is often possible to have the significance of the fit driven by a low sum of squared error (SSE) at one point of the curve, with a large SSE at others - so often you will want to instead actually PLOT the data itself.

- Fitted and adjusted
This option will display the model and data (the timecourse) at the selected voxel over the ENTIRE timecourse of the experiment. The model (conditioned by the contrast used) is plotted as RED, and the data is plotted as BLUE. However, this option is not terribly useful when you have >100 scans, as it becomes progressively harder to evaluate the relationship between model and data when the timeseries is long.

- Epoch/event-related plotting
This option will allow more concise plots of the timeseries, as it allows the evoked responses to each event/epoch of your design to be extracted from the general timeseries and displayed in summary fashion.
- fitted response: This will plot the basis function X parameter estimate.
- fitted response + standard error: Similar to the above, with the addition of an idea of the error.
- fitted response & adjusted data: this effectively summarises your data in a fashion similar to ERP analyses. It is important to note that the actual analysis of data is not performed in this way in SPM. Points along your timecourse are binned according to regressor/contrast type, and plotted simply around the fitted waveform (a single exemplar of the shape of your modelled response). Thus, for event-related analysis you will see a plot of the raw data to that event/contrast type plotted along with the model fit, conditioned by the contrast. The data is similarly adjusted for the 'null space' of the contrast - more on this later.
- fitted response and PSTH: Similar to the above, except instead of plotting the data a mean response at each binned timepoint is calculated, with appropriate error bars.

4. Accessing and plotting SPM variables in the base workspace

SPM generates many variables during analysis, but only a limited set quantities can plotted using the SPM graphical interface. It is thus important to know how to access the variables that SPM generates (and may use to construct its own plots), and so you can use them yourself. For instance, you may want to export the model and the data to a file for use in another analysis package such as SPSS. You can also plot these variables manually using MatLab's extensive plotting capabilities.

1. Type whos to display the variables currently loaded. These contain most of the quantities that you need to plot any aspect of your analysis.

2. Click on a voxel on SPM's graphics window - for example, the local maximum activation.

3. Use the plot command from the SPM GUI (bottom left window) as before. Click on 'fitted and adjusted responses', and plot them against 'scan or time'. In the graphics window, as before, you will see a nice plot of the data (blue) vs the response (red).

If you type 'whos' again into the MatLab window, you'll find that there are a number of additional variables which are specific to the voxel that you've just selected. These are

   Y      - fitted   data for the selected voxel
   y      - adjusted data for the selected voxel
   beta   - parameter estimates
   SE     - standard error of parameter estimates

These variables are returned to the workspace after each call to SPM's "plot" command.

Here's an example of creating your own graph of the same information manually using the variables SPM returns into the base workspace.

To plot just the parameter estimate and the SEM around it in a new figure:

   figure
   bar(beta(1),'b');
   errorbar(beta(1),SE,'r');

As you can see, allowing for scaling and windowing difference, this is exactly what SPM produces. Now let's create a fitted plot (the model), and the adjusted data (adjusted for the null space, which only contains a mean term in our example).

Clear the figure using clf, and type:

   hold on
   plot(Y,'r-')
   plot(y,'b:')

This is also similar to SPM's graph of the same information.

Most of the SPM information available in the base workspace is contained in structure variables, which were introduced above. The structure xX contains most information relating to the design matrix and the subsequent pre-processing that is applied to it. In a future separate SPM tutorial, we may delve further into the esoteric means by which SPM stores its data, and how you can persuade the variables to give up their secrets.

Dave suggests several sites which may be helpful examples and information regarding SPM (in addition to the SPM site):