Skip to contents

Representational Similarity Analysis

Generate a volumetric dataset with 100 observations and an external “model” RSA dissimilarity matrix

To generate a dataset we use the gen_sample_dataset function. We are creating a 4-dimensional neuroimaging dataset, with 6-by-6-by-6 spatial dimensions and 80 observations in the 4th dimension. These 80 observations are divided into 4 blocks, each consisting of 20 trials.

To run RSA with an external model matrix, we need to create and rsa_design object. First we generate a sample dataset with 80 trials as follows.

dataset <- gen_sample_dataset(D=c(20,20,8), nobs = 80, blocks=4)

Now we create a external dissimilarity matrix with the same number of trials as our fMRI dataset. Then we create an rsa_design object and an rsa_model object. Finally, we run a searchlight analysis with a 4mm radius using a pearson correlation as a metric to compare the similarity between the external model matrix (Dmat) and

Dmat <- dist(matrix(rnorm(80*100), 80, 100))
rsades <- rsa_design(~ Dmat, list(Dmat=Dmat), block_var=factor(dataset$design$block_var))

## use 'matrix' instead of 'dist'
Dmat2 <- as.matrix(Dmat)

rsades2 <- rsa_design(~ Dmat2, list(Dmat=Dmat2), block_var=factor(dataset$design$block_var))

dset <- mvpa_dataset(dataset$dataset$train_data, mask=dataset$dataset$mask)

### if we had a file called "betas.nii.gz" and associated mask, "mask.nii.gz" we would do as follows:
### traindat <- neuroim2::read_vector("betas.nii.gz")
### mask <- neuroim2::read_vector("mask.nii.gz")   
## dset <- mvpa_dataset(traindat, mask=mask)

rsamod <- rsa_model(dset, rsades)
result <- run_searchlight(rsamod, radius=4, method="standard",regtype="pearson")
## INFO [2024-04-23 13:40:24] running standard RSA searchlight with 4 radius 
## INFO [2024-04-23 13:40:24] creating standard searchlight
## INFO [2024-04-23 13:40:24] running standard searchlight iterator
rsamod2 <- rsa_model(dset, rsades2)
result2 <- run_searchlight(rsamod2, radius=4, method="standard",regtype="pearson")
## INFO [2024-04-23 13:40:28] running standard RSA searchlight with 4 radius 
## INFO [2024-04-23 13:40:28] creating standard searchlight
## INFO [2024-04-23 13:40:28] running standard searchlight iterator
range(result$Dmat)
## [1] -0.0438099  0.0252217
range(result2$Dmat)
## [1] -0.0438099  0.0252217

The return object result contains a list of NeuroVol objects, one for each model matrix. Here, since there is only one external model matrix, Dmat there is only one element in the returned list.

result
## $Dmat
## NeuroVol
##   Type           : DenseNeuroVol 
##   Dimension      : 20 20 8 
##   Spacing        : 1  X  1  X  1 
##   Origin         : 0  X  0  X  0 
##   Axes           : Left-to-Right Posterior-to-Anterior Inferior-to-Superior
### we can save the result to disk, as follows:

## write_vol(result$Dmat, "RSA_Dmat.nii.gz") 

Let’s rerun using a Spearman correlation instead of pearson correlation to compare brain vs model matrices.

result <- run_searchlight(rsamod, radius=4, method="standard",regtype="spearman")
## INFO [2024-04-23 13:40:31] running standard RSA searchlight with 4 radius 
## INFO [2024-04-23 13:40:31] creating standard searchlight
## INFO [2024-04-23 13:40:31] running standard searchlight iterator
range(result$Dmat)
## [1] -0.04623480  0.03528682