Skip to content

3. R package

Tamas Spisak edited this page Nov 12, 2018 · 71 revisions

Installation

Option 1.

The latest developer version of pTFCE can always be installed from GitHub:

Step 1. To be able to install pTFCE R-package from GitHub, first make sure you have the devtools package installed.

Type the followings into the R console to install and load devtools:

install.packages("devtools")

library(devtools)

Step 2. Install and load the pTFCE package from GitHub by typing into the R-consol:

install_github("spisakt/pTFCE@v0.1.0")

Make sure to change the version number after the @ sign to the latest one (or the preferred one) from the releases. Then load the installed package:

library(pTFCE)

Step 3. Test your installation by e.g. looking at the citations:

citation("pTFCE")

Now, you are ready for cluster-based belief boosting!

Option 2.

pTFCE will be soon also available on CRAN...


Usage

Installing pTFCE automatically installed its dependencies, the mmand and the oro.nifti R-packages. The latter one is needed to read NIfTI files. We must load it, and also the pTFCE package:

library(oro.nifti)

library(pTFCE)

Step 1. Load the NIfTI data of your Z-score map.

  • in FSL this will be something like <your_gfeat_folder>/cope.feat/stats/zstat.nii.gz
  • in SPM, by default you have only your T-score map, called spmT_.nii (or .hdr/.img pair) at hand. In R, after loading this image with "readNIfTI" (see below) you can convert it to a Z-score map by the command "Z=qnorm( pt(Tmap, df=degrees_of_freedom, log.p = T), log.p = T )", where your degrees-of-freedom can be found in your SPM.mat.
  • or you can download and use this simulated sample file to continue with this tutorial: https://github.com/spisakt/pTFCE/blob/master/data/example.nii.gz

Z=readNIfTI("path/to/your/z-score-map.nii.gz")

Step 2. Load your brain mask.

MASK=readNIfTI("path/to/your/mask.nii.gz")

Step 3. Run pTFCE!

pTFCE=ptfce(Z, MASK)

Now, the enhanced image is in the returned pTFCE object. E.g. the enhanced Z-score map is available as pTFCE$Z.

Step 4. View results

Let's compare the pTFCE enhanced image to the original one:

orthographic(Z, zlim=c(0, max(pTFCE$Z)), crosshair=F) #original

orthographic(pTFCE$Z, zlim=c(0, max(pTFCE$Z)), crosshair=F) #pTFCE

In case you used the simulated sample files, you should see something like:

Alternatively, you can save the enhanced image as NIfTI with:

writeNIfTI(pTFCE$Z, "path/to/your/pTFCE-z-score-map.nii.gz")

And use your favourite NIfTI image viewer.

Step 5. Threshold enhanced image.

In general, it is approximately valid to threshold your enhanced Z-score image with the same Z-score threshold you would have applied for the original (unenhanced) Z-score image. E.g. you can threshold the enhanced Z-score image for an uncorrected p<0.001 with the corresponding Z-score value of Z>3.1. Or you can perform a correction for multiple comparisons on the original (unenhanced) Z-score map via controlling for the family-wise error rate, then get the Z-threshold and use it on the pTFCE-enhanced Z-score image.

Never use the enhanced Z-score map as an input for GRF-based multiple correction techniques. Instead, use the original Z-score map to compute the Z-score threshold and then use this threshold on the enhanced image.

The pTFCE R-package also provides some functionality to perform image thresholding with GRF theory-based multiple comparison.

To apply the common p<0.05 corrected statistical threshold, simply use the 'fwer0.05.Z' field of the ptfce output:

orthographic(pTFCE$Z, zlim=c(pTFCE$fwer0.05.Z, max(pTFCE$Z)), crosshair=F)

This displays the pTFCE enhanced Z-score image thresholded at p<0.05, corrected for multiple comparisons via GRF Theory-based maximum height thresholding.

To apply corrected p-value threshold other than 0.05, you might also consider using the fwe.p2z() and fwe.z2p() functions of the pTFCE R-package.

fwer0.01.Z=fwe.p2z(pTFCE$number_of_resels, 0.01)

orthographic(pTFCE$Z, zlim=c(fwer0.01.Z, max(pTFCE$Z)), crosshair=F)