Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Resting-State fMRI Analysis in R

Run this notebook

From Preprocessed Data to Functional Connectivity

Authors: Giulia Baracchini & Monika Doerig

Date: 13 Jan 2026

License:

MIT License

Note: If this notebook uses neuroimaging tools from Neurocontainers, those tools retain their original licenses. Please see Neurodesk citation guidelines for details.

Citation and Resources:

Tools included in this workflow

R:

  • R Core Team. (2025). R: A language and environment for statistical computing (Version 4.4.3) [Software]. R Foundation for Statistical Computing. https://www.R-project.org/

Workflows this work is based on

Original work from Giulia Baracchini:

Dataset

HCP

Schefer parcellation

Introduction

This notebook is adapted from Giulia Baracchini’s comprehensive fMRI preprocessing tutorial (available on GitHub), which covers the essential steps of resting-state fMRI analysis from raw data preprocessing through functional connectivity analyses.

The original tutorial provides a complete pipeline covering:

  1. Standard fMRI preprocessing - preparing raw neuroimaging data for analysis

  2. Resting-state fMRI denoising - removing artifacts and noise from the signal

  3. Time-series extraction and parcellation - converting voxel-level data to meaningful brain regions

  4. Functional connectivity analyses - examining statistical relationships between brain regions

What This Notebook Covers

While the original tutorial works with multiple subjects and uses the Schaefer 200 region-7 network parcellation, this notebook focuses on a single-subject analysis using the Schaefer 200 region-17 network parcellation applied to subject 101309.

Key Concepts

Parcellation: The process of grouping individual voxels into meaningful brain regions or “parcels.” Instead of analyzing thousands of individual voxels, we average signals within anatomically or functionally defined regions, making our analyses more interpretable and computationally manageable.

Functional Connectivity (FC): A statistical measure (typically Pearson’s correlation) that quantifies how synchronously different brain regions activate during rest. The result is a region × region matrix where each entry represents the strength of correlation between two brain areas.

Analysis Pipeline

This notebook focuses on the analysis phase using already preprocessed and parcellated data. Starting with clean time series data from 200 brain regions, we implement:

  1. Data normalization - standardizing time series signals across regions

  2. Functional connectivity calculation - computing correlation matrices between brain regions

  3. Fisher z-transformation - normalizing correlation values for statistical analysis

  4. Network visualization - creating heatmaps and brain plots to visualize connectivity patterns

  5. Nodal strength analysis - quantifying each region’s overall connectivity

  6. Relationships to other measures of brain organisation - relating connectivity patterns to brain organization gradients and gene expression patterns

The Schaefer parcellation we’re using divides the brain into 200 regions across 17 functional networks, providing a good balance between spatial resolution and interpretability for resting-state connectivity analyses.

Running R in Jupyter with Python Kernel

⚠️ Run R in Jupyter Notebook: This notebook uses R magic commands (%%R) to run R code within a Python kernel environment. This approach offers several advantages:

  • No kernel switching required - all code runs in the Python kernel

  • Seamless Python ↔ R integration - easy data exchange between languages

  • Fully automated setup - no manual kernel installation needed

Note: Alternatively, Jupyter supports native R kernels, but the magic command approach keeps everything within the Python kernel for simpler workflow management.

Setup Steps

1. Install R runtime and packages via mamba:

2. Enable %%R magic commands:

We only need to run it once for the first time. After these installations, the Jupyter Notebook now supports both Python 3 and R programming languages.

3. Install R packages from CRAN and r-universe:

* installing *source* package ‘utf8’ ...
** this is package ‘utf8’ version ‘1.2.6’
** package ‘utf8’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C compiler: ‘x86_64-conda-linux-gnu-cc (conda-forge gcc 15.2.0-19) 15.2.0’
installing to /home/jovyan/workspace/.r-library/00LOCK-utf8/00new/utf8/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (utf8)
* installing *source* package ‘pkgconfig’ ...
** this is package ‘pkgconfig’ version ‘2.0.3’
** package ‘pkgconfig’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (pkgconfig)
* installing *source* package ‘stringi’ ...
** this is package ‘stringi’ version ‘1.8.7’
** package ‘stringi’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C++ compiler: ‘x86_64-conda-linux-gnu-c++ (conda-forge gcc 15.2.0-19) 15.2.0’
In file included from stri_ICU_settings.cpp:37:
uconfig_local.h:42:9: warning: 'UCONFIG_USE_WINDOWS_LCID_MAPPING_API' redefined
   42 | #define UCONFIG_USE_WINDOWS_LCID_MAPPING_API 0
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/conda/include/unicode/platform.h:24,
                 from /opt/conda/include/unicode/ptypes.h:46,
                 from stri_external.h:52,
                 from stri_stringi.h:36,
                 from stri_ICU_settings.cpp:33:
/opt/conda/include/unicode/uconfig.h:391:12: note: this is the location of the previous definition
  391 | #   define UCONFIG_USE_WINDOWS_LCID_MAPPING_API 1
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from stri_stringi.cpp:40:
uconfig_local.h:42:9: warning: 'UCONFIG_USE_WINDOWS_LCID_MAPPING_API' redefined
   42 | #define UCONFIG_USE_WINDOWS_LCID_MAPPING_API 0
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/conda/include/unicode/platform.h:24,
                 from /opt/conda/include/unicode/ptypes.h:46,
                 from stri_external.h:52,
                 from stri_stringi.h:36,
                 from stri_stringi.cpp:33:
/opt/conda/include/unicode/uconfig.h:391:12: note: this is the location of the previous definition
  391 | #   define UCONFIG_USE_WINDOWS_LCID_MAPPING_API 1
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
installing via 'install.libs.R' to /home/jovyan/workspace/.r-library/00LOCK-stringi/00new/stringi
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (stringi)
* installing *source* package ‘generics’ ...
** this is package ‘generics’ version ‘0.1.4’
** package ‘generics’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (generics)
* installing *source* package ‘tidyselect’ ...
** this is package ‘tidyselect’ version ‘1.2.1’
** package ‘tidyselect’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (tidyselect)
* installing *source* package ‘evaluate’ ...
** this is package ‘evaluate’ version ‘1.0.5’
** package ‘evaluate’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (evaluate)
* installing *source* package ‘xfun’ ...
** this is package ‘xfun’ version ‘0.59’
** package ‘xfun’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C compiler: ‘x86_64-conda-linux-gnu-cc (conda-forge gcc 15.2.0-19) 15.2.0’
installing to /home/jovyan/workspace/.r-library/00LOCK-xfun/00new/xfun/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (xfun)
* installing *source* package ‘yaml’ ...
** this is package ‘yaml’ version ‘2.3.12’
** package ‘yaml’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C compiler: ‘x86_64-conda-linux-gnu-cc (conda-forge gcc 15.2.0-19) 15.2.0’
installing to /home/jovyan/workspace/.r-library/00LOCK-yaml/00new/yaml/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (yaml)
* installing *source* package ‘remotes’ ...
** this is package ‘remotes’ version ‘2.5.0’
** package ‘remotes’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (remotes)
* installing *source* package ‘magrittr’ ...
** this is package ‘magrittr’ version ‘2.0.5’
** package ‘magrittr’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C compiler: ‘x86_64-conda-linux-gnu-cc (conda-forge gcc 15.2.0-19) 15.2.0’
installing to /home/jovyan/workspace/.r-library/00LOCK-magrittr/00new/magrittr/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (magrittr)
* installing *source* package ‘ggplot2’ ...
** this is package ‘ggplot2’ version ‘4.0.3’
** package ‘ggplot2’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** data
*** moving datasets to lazyload DB
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (ggplot2)
* installing *source* package ‘pillar’ ...
** this is package ‘pillar’ version ‘1.11.1’
** package ‘pillar’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (pillar)
* installing *source* package ‘purrr’ ...
** this is package ‘purrr’ version ‘1.2.2’
** package ‘purrr’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C compiler: ‘x86_64-conda-linux-gnu-cc (conda-forge gcc 15.2.0-19) 15.2.0’
installing to /home/jovyan/workspace/.r-library/00LOCK-purrr/00new/purrr/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (purrr)
* installing *source* package ‘stringr’ ...
** this is package ‘stringr’ version ‘1.6.0’
** package ‘stringr’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** data
*** moving datasets to lazyload DB
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (stringr)
* installing *source* package ‘highr’ ...
** this is package ‘highr’ version ‘0.12’
** package ‘highr’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (highr)
* installing *source* package ‘tibble’ ...
** this is package ‘tibble’ version ‘3.3.1’
** package ‘tibble’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C compiler: ‘x86_64-conda-linux-gnu-cc (conda-forge gcc 15.2.0-19) 15.2.0’
installing to /home/jovyan/workspace/.r-library/00LOCK-tibble/00new/tibble/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (tibble)
* installing *source* package ‘knitr’ ...
** this is package ‘knitr’ version ‘1.51’
** package ‘knitr’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (knitr)
* installing *source* package ‘dplyr’ ...
** this is package ‘dplyr’ version ‘1.2.1’
** package ‘dplyr’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C++ compiler: ‘x86_64-conda-linux-gnu-c++ (conda-forge gcc 15.2.0-19) 15.2.0’
installing to /home/jovyan/workspace/.r-library/00LOCK-dplyr/00new/dplyr/libs
** R
** data
*** moving datasets to lazyload DB
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (dplyr)
* installing *source* package ‘tidyr’ ...
** this is package ‘tidyr’ version ‘1.3.2’
** package ‘tidyr’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C++ compiler: ‘x86_64-conda-linux-gnu-c++ (conda-forge gcc 15.2.0-19) 15.2.0’
installing to /home/jovyan/workspace/.r-library/00LOCK-tidyr/00new/tidyr/libs
** R
** data
*** moving datasets to lazyload DB
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (tidyr)
* installing *source* package ‘ggdendro’ ...
** this is package ‘ggdendro’ version ‘0.2.0’
** package ‘ggdendro’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (ggdendro)
* installing *source* package ‘plyr’ ...
** this is package ‘plyr’ version ‘1.8.9’
** package ‘plyr’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C compiler: ‘x86_64-conda-linux-gnu-cc (conda-forge gcc 15.2.0-19) 15.2.0’
using C++ compiler: ‘x86_64-conda-linux-gnu-c++ (conda-forge gcc 15.2.0-19) 15.2.0’
installing to /home/jovyan/workspace/.r-library/00LOCK-plyr/00new/plyr/libs
** R
** data
*** moving datasets to lazyload DB
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (plyr)
* installing *source* package ‘superheat’ ...
** this is package ‘superheat’ version ‘0.1.0’
** package ‘superheat’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (superheat)
* installing *source* package ‘otel’ ...
** this is package ‘otel’ version ‘0.2.0’
** package ‘otel’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (otel)
* installing *source* package ‘ps’ ...
** this is package ‘ps’ version ‘1.9.3’
** package ‘ps’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C compiler: ‘x86_64-conda-linux-gnu-cc (conda-forge gcc 15.2.0-19) 15.2.0’
installing via 'install.libs.R' to /home/jovyan/workspace/.r-library/00LOCK-ps/00new/ps
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (ps)
* installing *source* package ‘processx’ ...
** this is package ‘processx’ version ‘3.9.0’
** package ‘processx’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C compiler: ‘x86_64-conda-linux-gnu-cc (conda-forge gcc 15.2.0-19) 15.2.0’
installing via 'install.libs.R' to /home/jovyan/workspace/.r-library/00LOCK-processx/00new/processx
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (processx)
* installing *source* package ‘callr’ ...
** this is package ‘callr’ version ‘3.8.0’
** package ‘callr’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (callr)
* installing *source* package ‘geometries’ ...
** this is package ‘geometries’ version ‘0.2.5’
** package ‘geometries’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C++ compiler: ‘x86_64-conda-linux-gnu-c++ (conda-forge gcc 15.2.0-19) 15.2.0’
installing to /home/jovyan/workspace/.r-library/00LOCK-geometries/00new/geometries/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (geometries)
* installing *source* package ‘sfheaders’ ...
** this is package ‘sfheaders’ version ‘0.4.5’
** package ‘sfheaders’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
using C++ compiler: ‘x86_64-conda-linux-gnu-c++ (conda-forge gcc 15.2.0-19) 15.2.0’
installing to /home/jovyan/workspace/.r-library/00LOCK-sfheaders/00new/sfheaders/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (sfheaders)
* installing *source* package ‘ggseg.formats’ ...
** this is package ‘ggseg.formats’ version ‘0.0.4’
** package ‘ggseg.formats’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (ggseg.formats)
* installing *source* package ‘ggsegSchaefer’ ...
** this is package ‘ggsegSchaefer’ version ‘2.0.3’
** using staged installation
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (ggsegSchaefer)
* installing *source* package ‘ggseg’ ...
** this is package ‘ggseg’ version ‘2.1.1’
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (ggseg)
Fetching long content....
Installing packages into ‘/home/jovyan/workspace/.r-library’ (as ‘lib’ is unspecified) Warning: unable to access index for repository https://ggseg.r-universe.dev/src/contrib: cannot open URL 'https://ggseg.r-universe.dev/src/contrib/PACKAGES' also installing the dependencies ‘utf8’, ‘pkgconfig’, ‘stringi’, ‘generics’, ‘pillar’, ‘tibble’, ‘tidyselect’, ‘purrr’, ‘stringr’, ‘evaluate’, ‘highr’, ‘xfun’, ‘yaml’ trying URL 'https://cloud.r-project.org/src/contrib/utf8_1.2.6.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/pkgconfig_2.0.3.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/stringi_1.8.7.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/generics_0.1.4.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/pillar_1.11.1.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/tibble_3.3.1.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/tidyselect_1.2.1.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/purrr_1.2.2.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/stringr_1.6.0.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/evaluate_1.0.5.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/highr_0.12.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/xfun_0.59.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/yaml_2.3.12.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/remotes_2.5.0.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/magrittr_2.0.5.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/ggplot2_4.0.3.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/dplyr_1.2.1.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/tidyr_1.3.2.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/knitr_1.51.tar.gz' The downloaded source packages are in ‘/tmp/RtmpffYIdp/downloaded_packages’ Downloading package from url: https://cloud.r-project.org/src/contrib/Archive/superheat/superheat_0.1.0.tar.gz Installing 2 packages: ggdendro, plyr Installing packages into ‘/home/jovyan/workspace/.r-library’ (as ‘lib’ is unspecified) trying URL 'https://cloud.r-project.org/src/contrib/ggdendro_0.2.0.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/plyr_1.8.9.tar.gz' The downloaded source packages are in ‘/tmp/RtmpffYIdp/downloaded_packages’ Installing package into ‘/home/jovyan/workspace/.r-library’ (as ‘lib’ is unspecified) Installing packages into ‘/home/jovyan/workspace/.r-library’ (as ‘lib’ is unspecified) Warning: unable to access index for repository https://ggseg.r-universe.dev/src/contrib: cannot open URL 'https://ggseg.r-universe.dev/src/contrib/PACKAGES' also installing the dependencies ‘otel’, ‘processx’ trying URL 'https://cloud.r-project.org/src/contrib/otel_0.2.0.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/processx_3.9.0.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/ps_1.9.3.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/callr_3.8.0.tar.gz' The downloaded source packages are in ‘/tmp/RtmpffYIdp/downloaded_packages’ Installing package into ‘/home/jovyan/workspace/.r-library’ (as ‘lib’ is unspecified) Warning: unable to access index for repository https://ggseg.r-universe.dev/src/contrib: cannot open URL 'https://ggseg.r-universe.dev/src/contrib/PACKAGES' ggsegSchaefer not found via r-universe, installing from GitHub... Using github PAT from envvar GITHUB_PAT. Use `gitcreds::gitcreds_set()` and unset GITHUB_PAT in .Renviron (or elsewhere) if you want to use the more secure git credential store instead. Downloading GitHub repo ggseg/ggsegSchaefer@HEAD Installing 3 packages: geometries, sfheaders, ggseg.formats Installing packages into ‘/home/jovyan/workspace/.r-library’ (as ‘lib’ is unspecified) Warning: unable to access index for repository https://ggseg.r-universe.dev/src/contrib: cannot open URL 'https://ggseg.r-universe.dev/src/contrib/PACKAGES' trying URL 'https://cloud.r-project.org/src/contrib/geometries_0.2.5.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/sfheaders_0.4.5.tar.gz' trying URL 'https://cloud.r-project.org/src/contrib/ggseg.formats_0.0.4.tar.gz' The downloaded source packages are in ‘/tmp/RtmpffYIdp/downloaded_packages’ Running `R CMD build`... Installing package into ‘/home/jovyan/workspace/.r-library’ (as ‘lib’ is unspecified) Using github PAT from envvar GITHUB_PAT. Use `gitcreds::gitcreds_set()` and unset GITHUB_PAT in .Renviron (or elsewhere) if you want to use the more secure git credential store instead. Downloading GitHub repo ggseg/ggseg@v2.1.1 Running `R CMD build`... Installing package into ‘/home/jovyan/workspace/.r-library’ (as ‘lib’ is unspecified) In addition: Warning message: package ‘ggsegSchaefer’ is not available for this version of R A version of this package for your version of R might be available elsewhere, see the ideas at https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages

4. All subsequent R code uses the %%R cell magic:

Each cell containing R code must start with %%R to be executed as R code.

Linking to GEOS 3.14.1, GDAL 3.12.4, PROJ 9.8.0; sf_use_s2() is TRUE udunits database from /opt/conda/lib/R/library/units/share/udunits/udunits2.xml Attaching package: ‘dplyr’ The following objects are masked from ‘package:stats’: filter, lag The following objects are masked from ‘package:base’: intersect, setdiff, setequal, union

Data download

trying URL 'https://raw.githubusercontent.com/giuliabaracc/teaching_fMRI/main/data/margulies2016_fcgradient01_20017Schaefer.csv' Content type 'text/plain; charset=utf-8' length 8044 bytes ================================================== downloaded 8044 bytes trying URL 'https://raw.githubusercontent.com/giuliabaracc/teaching_fMRI/main/data/gene_pc1_20017Schaefer.csv' Content type 'text/plain; charset=utf-8' length 8032 bytes ================================================== downloaded 8032 bytes trying URL 'https://raw.githubusercontent.com/giuliabaracc/teaching_fMRI/main/data/sub-101309_Schaefer20017.txt' Content type 'text/plain; charset=utf-8' length 3094605 bytes (3.0 MB) ================================================== downloaded 3.0 MB

Load data and visualize data from one subject

<IPython.core.display.Image object>

Normalize time series data

<IPython.core.display.Image object>

Calculate their Functional Connectivity (FC) matrix

Let’s do some analyses on these data now! First thing, let’s calculate functional connectivity (FC). As a refresher, FC is a statistical construct derived (typically) as the Pearson’s correlation between pairs of brain regions. The output is therefore a region x region matrix where each entry indicates how strong the correlation is between two regions. In our case, our FC matrix will be 200x200.

<IPython.core.display.Image object>

Normalise FC values

For group analyses, we need to normalise these FC values: Fisher-z transformation

<IPython.core.display.Image object>

Visualise FC matrix

You can play with the value limits, but in generally you want to make sure you see boxes in your matrix reflecting the brain’s functional network organisation.

In addition: Warning message: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0. ℹ Please use `linewidth` instead. ℹ The deprecated feature was likely used in the superheat package. Please report the issue to the authors. This warning is displayed once per session. Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
<IPython.core.display.Image object>

Calculate Nodal strength

Let’s take this a step further. Let’s calculate how much each region is connected to the rest of the brain, a measure that is called node strength. Node strength, or region strength, is “the sum of weights of links connected to the node”. This will allow us to obtain a 1x200 vector that we can relate to a bunch of other measures of brain organisation.

Merging atlas and data by region and label.
<IPython.core.display.Image object>
<IPython.core.display.Image object>

Relate their nodal strength measures to measures of brain organisation and gene organisation

Merging atlas and data by region and label.
<IPython.core.display.Image object>
Merging atlas and data by region and label.
<IPython.core.display.Image object>


Table: Spearman correlations for Nodal Strength

|Comparison                           | Spearman_rho|
|:------------------------------------|------------:|
|Nodal Strength vs Brain Organisation |       -0.359|
|Nodal Strength vs Gene Organisation  |        0.459|

Complete session information for reproducibility

=== R Session Information ===

R version 4.5.3 (2026-03-11)
Platform: x86_64-conda-linux-gnu
Running under: Ubuntu 24.04.4 LTS

Matrix products: default
BLAS/LAPACK: /opt/conda/lib/libopenblasp-r0.3.33.so;  LAPACK version 3.12.0

locale:
 [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
 [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
 [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
[10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   

time zone: Etc/UTC
tzcode source: system (glibc)

attached base packages:
[1] tools     stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] knitr_1.51          tidyr_1.3.2         dplyr_1.2.1        
 [4] ggplot2_4.0.3       superheat_0.1.0     s2_1.1.11          
 [7] units_1.0-1         sf_1.1-0            ggsegSchaefer_2.0.3
[10] ggseg_2.1.1        

loaded via a namespace (and not attached):
 [1] gtable_0.3.6        compiler_4.5.3      tidyselect_1.2.1   
 [4] Rcpp_1.1.2          scales_1.4.0        R6_2.6.1           
 [7] sfheaders_0.4.5     labeling_0.4.3      generics_0.1.4     
[10] classInt_0.4-11     ggseg.formats_0.0.4 tibble_3.3.1       
[13] DBI_1.3.0           pillar_1.11.1       RColorBrewer_1.1-3 
[16] rlang_1.3.0         xfun_0.59           S7_0.2.2           
[19] otel_0.2.0          viridisLite_0.4.3   cli_3.6.6          
[22] withr_3.0.3         magrittr_2.0.5      class_7.3-23       
[25] wk_0.9.5            grid_4.5.3          remotes_2.5.0      
[28] lifecycle_1.0.5     vctrs_0.7.3         KernSmooth_2.23-26 
[31] evaluate_1.0.5      proxy_0.4-29        glue_1.8.1         
[34] farver_2.1.2        e1071_1.7-17        purrr_1.2.2        
[37] pkgconfig_2.0.3    
Neurodesktop version: 2026-06-04