Example for simulating and running the iCARH model

For real data Build pathway matrices using iCARH.getPathwaysMat. Elements in kegg id list may contain multiple kegg ids per metabolite. If KEGG id unknown : “Unk[number]”.

keggid = list("Unk1", "C03299","Unk2","Unk3",
 c("C08363", "C00712"),  # allowing multiple ids per metabolite
 )
 pathways = iCARH.GetPathwaysMat(keggid, "rno")

To simulate data use iCARH.simulate

# Manually choose pathways
path.names = c("path:map00564","path:map00590","path:map00061","path:map00591",
               "path:map00592","path:map00600","path:map01040","path:map00563")
data.sim = iCARH.simulate(Tp, N, J, P, K, path.names=path.names, Zgroupeff=c(0,4),
                          beta.val=c(1,-1,0.5, -0.5))
## Warning in iCARH.simulate(Tp, N, J, P, K, path.names = path.names, Zgroupeff = c(0, : Number of pathways reduced to  1 due to random selection of metabolites
##                                  in the intially specified pathways.
XX = data.sim$XX
Y = data.sim$Y
Z = data.sim$Z
pathways = data.sim$pathways

Check inaccuracies between covariance and design matrices

pathways.bin = lapply(pathways, function(x) { y=1/(x+1); diag(y)=0; y})
adjmat = rowSums(abind::abind(pathways.bin, along = 3), dims=2)
cor.thresh = 0.7
# check number of metabolites in same pathway but not correlated
for(i in 1:Tp) print(sum(abs(cor(XX[i,,])[which(adjmat>0)])<cor.thresh))
## [1] 108
## [1] 120
## [1] 146
## [1] 124

Run iCARH model. Takes about 12 mins.

rstan::rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
fit.sim = iCARH.model(XX, Y, Z, pathways, control = list(adapt_delta = 0.99, max_treedepth=10),
                     iter = 500, chains = 1, pars=c("YY","Xmis","Ymis"), include=F)
## Warning: rstan >= 2.18.2 or R < 3.6.0 needed for this function. Program
## will exit promptly
# Not run
# fit.sim = iCARH.model(XX, Y, Z, pathways, control = list(adapt_delta = 0.99, max_treedepth=10),
#                      iter = 1100, chains = 2, pars=c("YY","Xmis","Ymis"), include=F)

Processing results. Bacteria effects.

if(!is.null(fit.sim)){
  gplot = iCARH.plotBeta(fit.sim)
  gplot
}

Treatments effects

if(!is.null(fit.sim)){
  gplot = iCARH.plotTreatmentEffect(fit.sim)
  gplot
}

Pathway analysis

if(!is.null(fit.sim)){
  gplot = iCARH.plotPathwayPerturbation(fit.sim, names(data.sim$pathways))
  gplot
}

Normality assumptions

if(!is.null(fit.sim)){
  par(mfrow=c(1,2))
  iCARH.checkNormality(fit.sim)
}

WAIC

if(!is.null(fit.sim)){
  waic = iCARH.waic(fit.sim)
  waic
}

Posterior predictive checks MAD : mean absolute deviation between covariance matrices

if(!is.null(fit.sim)){
  mad = iCARH.mad(fit.sim, XX)
  quantile(mad)
}