This vignette documents the implementation of NBR 0.1.2 for linear models applied to a sample level of matrices edges.
Here it is an example applied into a 3D volume. Where, its first two dimension must be equal, i.e., the number of nodes of every individual network matrix (nnodes
). And its third dimension equals the sample number.
library(NBR)
cmx <- NBR:::frontal3D # Load 3D array
brain_labs <- NBR:::frontal_roi # Load network nodes labels
phen <- NBR:::frontal_phen # Load sample phenotypic info
dim(cmx) # Show 3D array dimensions
#> [1] 28 28 48
We can plot the sample average network matrix, with lattice::levelplot
.
library(lattice)
avg_mx <- apply(cmx, 1:2, mean)
# Set max-absolute value in order to set a color range centered in zero.
flim <- max(abs(avg_mx)[is.finite(avg_mx)])
levelplot(avg_mx, main = "Average", ylab = "ROI", xlab = "ROI",
at = seq(-flim, flim, length.out = 100))
As we can observe, this is a symmetric matrix with the pairwise connections of the 28 regions of interest (ROI) brain_labs
. The next step is to check the sample inference data to be tested edgewise, in this case phen
. Before applied the NBR-LM it is necessary to be sure that the number of networks (i.e., the third dimension of the 3D array) matches the number of observations of the data.frame one wants to test.
head(phen)
#> Group Sex Age
#> 1 Control F 8.52
#> 2 Control M 16.16
#> 3 Patient M 17.75
#> 4 Control M 12.27
#> 5 Control F 12.07
#> 6 Patient F 8.71
nrow(phen)
#> [1] 48
identical(nrow(phen), dim(cmx)[3])
#> [1] TRUE
The data.frame contains the individual information for diagnostic group, sex, and chronological age. So, there are all the ingredients to perform a NBR-LM. We are going to test first a group inference (with just ten permutations in one core).
set.seed(18900217) # Because R. Fisher is my hero
before <- Sys.time()
nbr_group <- nbr_lm_aov(net = cmx, nnodes = 28, idata = phen,
mod = "~ Group", thrP = 0.01, nperm = 10)
#> Computing observed stats.
#> Computing permutated stats.
#> Permutation progress: ....
after <- Sys.time()
show(after-before)
#> Time difference of 6.709661 secs
Although ten permutations is faraway for a proper null distribution, we can see that it takes several seconds to performed it. So we suggest to parallel to multiple CPU cores with cores
argument.
set.seed(18900217)
library(parallel)
before <- Sys.time()
nbr_group <- nbr_lm_aov(net = cmx, nnodes = 28, idata = phen,
mod = "~ Group", thrP = 0.01, nperm = 100, cores = detectCores())
after <- Sys.time()
length(nbr_group)
NBR functions return a nested list of at least two list. The first list encompasses all the individual significant edges and its corresponding component and statistical strength above threshold (p < 0.01, in this example). In this case all the significant edges belong to a single component.
# Plot significant edges
edge_mat <- array(0, dim(avg_mx))
edge_mat[nbr_group$components$Group[,2:3]] <- 1
levelplot(edge_mat, col.regions = rev(heat.colors(100)),
main = "Component", ylab = "ROI", xlab = "ROI")
As we can observe, significant edges are display in the upper triangle of the matrix, and the second list fwe
contains, for each term of the equation, the probability of the observed values within the generated null distribution.