# Connectivity Analysis in AFNI (Part 1)

I’ve written before about how AFNI offers users the ability to perform the same or very similar analyses using a variety of tools.  Performing connectivity analysis is no different.  First of all, most people who are talking about connectivity are really referring to “seed connectivity” (sometimes called functional connectivity or fcMRI), whereby one region of the brain is correlated with all other regions of the brain.  This is in contrast to “network analysis” (also called effective connectivity), which is usually done via Structural Equation Modeling (SEM), Granger Causality, or Dynamic Causal Modeling (DCM).

The plan is that today’s post is about “seed connectivity”.  The next post will cover InstaCorr and 3dGroupInCorr, the amazing AFNI programs for seeing real-time correlations.  And we’ll leave the network analysis for later posts.

The following examples are simple cases performed on resting-state data.  If you wished to remove events from event-related data, you can do that via 3dSynthesize.  Finally, before performing these analyses on your data, you will likely want to move your functional data to a standard space using @auto_tlrc or auto_warp.py.

Correlating two ROIs

The first thing you can do in AFNI is correlate two ROIs.  The easiest way to do this is first extract your ROIs as 1D (Text) files using 3dmaskave (alternatives include 3dROIstats, 3dmaskdump).  You can then use 1dCorrelate, 1ddot, or even 3ddot to compute a correlation between those two ROIs.  In the sample below, I created two ROIs and then exported them via 3dmaskave to the text files ROI1.1D and ROI2.1D.  The examples below show these are the input files.

1dCorrelate

```1dCorrelate ROI1.1D ROI2.1D

# Pearson correlation [n=150 #col=2]
# Name Name Value BiasCorr 2.50% 97.50% N: 2.50% N:97.50%
# ---------- ---------- -------- -------- -------- -------- -------- --------
ROI1.1D[0] ROI2.1D[0] +0.96900 +0.96993 +0.39512 +0.99150 +0.95741 +0.97746```

1ddot

```1ddot -dem ROI1.1D ROI2.1D
++ 1ddot input vectors:
00..00: ROI1.1D
01..01: ROI2.1D
++ Correlation Matrix:
00 01
--------- ---------
00: 1.00000 0.96899
01: 0.96899 1.00000
++ Eigensolution of Correlation Matrix:
0.03101 1.96899
--------- ---------
00: 0.70711 0.70711
01: -0.70711 0.70711
++ Correlation Matrix Inverse:
00 01
--------- ---------
00: 16.38033 -15.87245
01: -15.87245 16.38033
++ Correlation sqrt(diagonal)
4.04726 4.04726
++ Correlation Matrix Inverse Normalized:
00 01
--------- ---------
00: 1.00000 -0.96899
01: -0.96899 1.00000```

3ddot

```3ddot -demean ROI1.1D ROI2.1D
0.968995```

R – Using the R environment to calculate the correlation.  Here we put the two ROIs into one file for ease of importing, read the data into R and then run cor.test() on those two columns of the data.frame.

```1dcat ROI1.1D ROI2.1D > ROI_all.1D
R
cor.test(thedata\$V2, thedata\$V3)
Pearson's product-moment correlation
data: thedata\$V2 and thedata\$V3
t = 47.7105, df = 148, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.9574125 0.9774636
sample estimates:
cor
0.9689949```

Correlating seed to whole-brain

If you wish to correlate one seed region to the rest of the brain, you also have a few options.  Mainly you can use 3dfim+, 3dDeconvolve, or 3dTcorr1D.  The major advantages of using 3dDeconvolve are that you can also model the effects of non-interest (e.g. baseline, motion, respiration/heartbeat).  In these examples, minor differences may be found due to the overall design matrix including different baseline measures by default or the inclusion of extra regressors (e.g. motion).

3dfim+

`3dfim+ -input ep2dbold150Restings011a001.nii.gz -ideal_file ROI2.1D -out Correlation -bucket fim_data`

3dTcorr1D

`3dTcorr1D -pearson -prefix corr1_data ep2dbold150Restings011a001.nii.gz ROI2.1D`

3dDeconvolve

In this (admittedly very simple) example we run 3dDeconvolve, and then take the R-squared bucket and take the square root to get a correlation, getting the sign from the beta-weight.  You would likely wish to include motion regressors, etc.   I would recommend using afni_proc.py and modifying the 3dDeconvolve output to include a stim_file for your ROI.

```3dDeconvolve -input ep2dbold150Restings011a001.nii.gz -polort 1 -num_stimts 1 -stim_file 1 ROI2.1D -rout -bucket decon_data
3dcalc -a 'decon_data+orig.[Stim#1_R^2]' -b 'decon_data+orig.[Stim#1#0_Coef]' -prefix decon_corr -expr 'ispositive(b)*sqrt(a)-isnegative(b)*sqrt(a)'```

You can find more direct comparisons between 3dDeconvolve and 3dfim+ on Gang Chen’s page.  Next time we’ll go into how you can see real-time correlations in AFNI without doing all of this leg work.