# Statistics on the Brain with AFNI

In previous posts I’ve covered how to use AFNI to run GLMs on your data to find task-related activations.  But there are a host of other statistics that you can run on the brain outside of the GLM!  And this is where AFNI really shines in terms of having a diverse set of tools, yet can be confusing because there are so many tools.  So here’s some basics:

Whole-Brain Statistics

To calculate the statistics (e.g. mean, standard deviation, sum) for a whole brain you have a few options.

3dMean – will calculate a dataset consisting of Mean, Std. Deviation, or Square Root of your input datasets.  This can be very useful if you want to create an “Average Brain” or for calculations to make a z-score of your input datasets.

`3dMean -prefix AverageBrain brain1.nii.gz brain2.nii.gz brain3.nii.gz`

3dcalc – is the general purpose calculator for whole-brain images.  It can do every type of mathematical calculation imaginable on images (e.g. multiplication) as well as perform conjunction style conditioning (e.g. AND, OR).

`3dcalc -a 'Dataset1+tlrc.' -b 'Dataset2+tlrc.' -prefix both -expr 'AND(a,b)'`

Statistics over Time

AFNI tools that work over Time or multiple sub-bricks of a dataset have a capital T in the name.

3dTstat – this tool calculates statistics over time.  If you wish to find the average for each voxel over a time series of the first 201 time points in a data file.

`3dTstat -prefix AvgOverTime -mean InputDset[0..200]`

Statistics within an image

If instead of calculating statistics at each voxel, you can calculate a statistic (e.g. Mean) over the entire image – so that you get one number for an image.  There’s a tool for that.

3dBrickStat – this tool calculates statistics over a single image.  You can also pass it multiple images using sub-brick selectors.

`3dBrickStat -mean -mask ../TT_Mask_small+tlrc.HEAD \$aSub`

The code above calculates the mean of all voxels in the mask and returns one number.

Using calculated numbers inside or your scripts

ccalc – Is a useful general calculation tool.  It behaves like 3dcalc / 1deval.

```var=`3dBrickStat -var -mask ../TT_Mask_small+tlrc.HEAD \$aSub`
std=`ccalc -expr "sqrt(\$var)"````

Using the example above, I calculate variance from a dataset (masking outside of the brain) and then get  the standard deviation using ccalc.