Overview for mand"

r Sys.Date() @Atsushi Kawaguchi

options(width = 300)

The mand package provides functions for multivariate analysis for neuroimaging data.

library(mand)

Introduction

Template

One subject image as the template is available in the mand package. The coad to load it is as follows.

data(template)

The image is compressed because of storage and computation time. The dimension is confirmed as follows.

dim(template)

The image is plotted by the coat function.

coat(template)

Other options with the plane argument (such as "axial," "coronal," "sagittal," and "all") are available. The default setting is "axial". If the argument is specified as plane="all", three directional slices at a certain coordinate are plotted.

coat(x=template, plane="all")

Image Data Matrix

The imgdatamat function reads image files saved in the nifti format and creates data matrix with subjects in row and voxel in column (this example does not work).

fnames1 = c("data1.nii", "data2.nii")
imgmat = imgdatamat(fnames1, simscale=1/4)

The first argument is file names with the length equaling the number of subjects (the number of rows in the resulting data matrix). The second argument simscale is the image resize scale. In this example, the all sizes (number of voxel) for three direction was reduced into its quarter size. The output is the list form where the "S" is data matrix, the "brainpos" is a binary image indicating brain region, and the "imagedim" is image dimension. The ROI (Region Of Interest) volume is computed in the "roi" if the ROI argument is TRUE.

imgmat = imgdatamat(fnames1, schange=TRUE, simscale=1/4, ROI=TRUE)

Overlay

The resulting map from the statistical analysis such as the t statistics map from the SPM is represented with overlapping on the template. For example, the mand package has the average difference assuming Alzheimer's disease and healthy subjects with the array format.

'

The overlay is implemented by the coat function.

data(diffimg)
coat(template, diffimg)

Atlas

Anatomical brain segmentation region is useful for the plot and the interpretation. For example, the Automated Anatomical Labeling (AAL) atlas is used. The data.frame has two columns ("ROIid" and "ROIname") format.

data(atlasdatasets)
atlasname = "aal3"
atlasdataset = atlasdatasets[[atlasname]]
head(atlasdataset)

It is also neccesary to prepare the array data.

data(atlas)
tmpatlas = atlas[[atlasname]]
dim(tmpatlas)

It has the ROIid as the element.

tmpatlas[,15:20,12]

The anatomical region can be plotted by the coat function with regionplot=TRUE.

coat(template, tmpatlas, regionplot=TRUE, 
atlasdataset=atlasdataset, ROIids = c(1:2, 41:44), regionlegend=TRUE)

The resulting map can be converted into the summary of the anatomical regions.

atlastable(tmpatlas, diffimg, atlasdataset, ROIids = c(1:2, 41:44))

The outputs are the number of voxel in the sizenum column, the percentage of the voxel in the sizepct column, and the minimum, mean, and maximum valued of the region of the overlaying map. The order of the table row is in the larger absolute value of the minimum or maximum values.

The brain image corresponding to the region of interest can be extracted as follows. First, we create a mask image in which the hippocampal region is represented by 1 and others by 0. Then the product of the template and the mask image is taken for each voxel.

hipmask = (tmpatlas == 41) + (tmpatlas == 42)
template2 = template * hipmask

The images generated by these processes are plotted from left to right in one slice.

par(mfrow=c(1,3), mar=rep(1,4))
coat(template, pseq=12, paron=FALSE)
coat(hipmask, pseq=12, paron=FALSE)
coat(template2, pseq=12, paron=FALSE)

The template image (left) and the mask image (middle) are multiplied voxel by voxel to obtain the only hippocampus region image (right).

The sum of the voxel values in the region is calculated as follows.

sum(template[which(hipmask==1, arr.ind = TRUE)])/1000

Such a value is calculated for each region and a dataset with ROI values is created.

Principal Component Analysis

Generate Simulation Data

The mand package has a function to generate simulation brain data from the base image, the difference image and the standard deviation image. These basic images are loaded as follows.

data(baseimg)
data(diffimg)
data(mask)
data(sdevimg)

The number of voxels in the original 3D image is as follows.

dim(baseimg)

To understand the result easily, the difference region was restricted to Parahippocampus and Hippocampus.

diffimg2 = diffimg * (tmpatlas %in% 41:44)

An image data matrix with subjects in the rows and voxels in the columns was generated by using the simbrain function.

img1 = simbrain(baseimg = baseimg, diffimg = diffimg2, 
sdevimg=sdevimg, mask=mask, n0=20, c1=0.01, sd1=0.05)

The base image, the difference image and the standard deviation image were specified in the first three arguments. The out-of-brain region was specified by the mask argument, which was the binary image. The remaining arguments are the number of subjects per group, the coefficient multiplied by the difference image and the standard deviation for the noise.

The data matrix dimension was r nrow(img1$S)(subject) x r ncol(img1$S)(voxel).

dim(img1$S)

The rec function creates the 3D image from the vectorized data (the first subject).

coat(rec(img1$S[1,], img1$imagedim, mask=img1$brainpos))

The standard deviation image is created from the resulting data matrix.

sdimg = apply(img1$S, 2, sd)
coat(template, rec(sdimg, img1$imagedim, mask=img1$brainpos))

Principal Component Analysis

If the input is a matrix, a principal component analysis is implemented by the msma function of the msma package. Principal component analysis with the number of components of 2 for the image data matrix is performed as follows.

(fit111 = msma(img1$S, comp=2))

The scatter plots for the score vectors specified by the argument v. The argument axes is specified by the two length vectors on which components are displayed.

plot(fit111, v="score", axes = 1:2, plottype="scatter")

The weight (loading) vectors can be obtained and reconstructed as follows.

midx = 1 ## the index for the modality
vidx = 1 ## the index for the component
Q = fit111$wbX[[midx]][,vidx]
outstat1 = rec(Q, img1$imagedim, mask=img1$brainpos)

The reconstructed loadings as image is overlayed on the template.

coat(template, outstat1)

The output is unclear; however, this will be improved later.

Two-steps dimension reduction

Basis Expansion

This is an example of the two-step dimension reduction.

Generate radial basis function.

B1 = rbfunc(imagedim=img1$imagedim, seppix=2, hispec=FALSE, mask=img1$brainpos)

Multiplying the basis function to image data matrix.

SB1 = basisprod(img1$S, B1)

The original dimension was r dim(img1$S)[2].

dim(img1$S)

The dimension was reduced to r dim(SB1)[2].

dim(SB1)

Principal Component Analysis

The Principal Component Analysis (PCA) is applied to the dimension-reduced image.

(fit211 = msma(SB1, comp=2))

The loading is reconstructed to the original space by using the rec function.

Q = fit211$wbX[[1]][,1]
outstat1 = rec(Q, img1$imagedim, B=B1, mask=img1$brainpos)

The plotted loading is smoother than the one without the dimension reduction by the basis function.

coat(template, outstat1)

Sparse PCA

If lambdaX (>0) is specified, a sparse principal component analysis is implemented.

(fit112 = msma(SB1, comp=2, lambdaX=0.06))

The plotted loading is narrower than that from the PCA.

Q = fit112$wbX[[midx]][,vidx]
outstat1 = rec(Q, img1$imagedim, B=B1, mask=img1$brainpos)
outstat2 = outstat1
coat(template, outstat2)

The region is reported as follows to be compared with the next method.

atlastable(tmpatlas, outstat2, atlasdataset)

Supervised Sparse PCA

The simbrain generates the synthetic brain image data and the binary outcome. The outcome Z is obtained.

Z = img1$Z

If the outcome Z is specified in the msma function, a supervised sparse principal component analysis is implemented.

(fit113 = msma(SB1, Z=Z, comp=2, lambdaX=0.06, muX=0.5))

The plotted loading is located differently from the sparse PCA.

Q = fit113$wbX[[1]][,1]
outstat1 = rec(Q, img1$imagedim, B=B1, mask=img1$brainpos)
outstat2 = -outstat1
coat(template, outstat2)

The region near the hippocampus, which differs from the sparse PCA (without supervision).

atlastable(tmpatlas, outstat2, atlasdataset)

The loading for the second component

Q = fit113$wbX[[1]][,2]
outstat1 = rec(Q, img1$imagedim, B=B1, mask=img1$brainpos)
outstat2 = -outstat1
coat(template, outstat2)
atlastable(tmpatlas, outstat2, atlasdataset)

This is similar to the result from the sparse PCA (without supervision).

The following method can be used to plot the weights of several components simultaneously. It is first reconstructed in three dimensions with the multirec function and then plotted with the multicompplot function. It is set to display four columns per component.

ws = multirec(fit113, imagedim=img1$imagedim, B=B1, mask=img1$brainpos)
multicompplot(ws, template, col4comp=4)


Try the mand package in your browser

Any scripts or data that you put into this service are public.

mand documentation built on Sept. 13, 2023, 1:06 a.m.