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 2 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] 86
## [1] 90
## [1] 122
## [1] 118
Run iCARH model.
rstan::rstan_options(auto_write = TRUE)
options(mc.cores = 2)
# For testing, does not converge
fit.sim = iCARH.model(XX, Y, Z, pathways, control = list(max_treedepth=8),
iter = 4, chains = 1, pars=c("YY","Xmis","Ymis"), include=F)
## DIAGNOSTIC(S) FROM PARSER:
## Info: integer division implicitly rounds to integer. Found int division: (N * T) / 4
## Positive values rounded down, negative values rounded up or down in platform-dependent way.
## Info: integer division implicitly rounds to integer. Found int division: (N * T) / 4
## Positive values rounded down, negative values rounded up or down in platform-dependent way.
## Info (non-fatal):
## Left-hand side of sampling statement (~) may contain a non-linear transform of a parameter or local variable.
## If it does, you need to include a target += statement with the log absolute determinant of the Jacobian of the transform.
## Left-hand-side of sampling statement:
## YY[1, , k] ~ multi_normal(...)
## Info: integer division implicitly rounds to integer. Found int division: N / 2
## Positive values rounded down, negative values rounded up or down in platform-dependent way.
## Info: integer division implicitly rounds to integer. Found int division: N / 2
## Positive values rounded down, negative values rounded up or down in platform-dependent way.
## Info: integer division implicitly rounds to integer. Found int division: N / 2
## Positive values rounded down, negative values rounded up or down in platform-dependent way.
## Info: integer division implicitly rounds to integer. Found int division: N / 2
## Positive values rounded down, negative values rounded up or down in platform-dependent way.
## Info: integer division implicitly rounds to integer. Found int division: N / 2
## Positive values rounded down, negative values rounded up or down in platform-dependent way.
## Info: integer division implicitly rounds to integer. Found int division: N / 2
## Positive values rounded down, negative values rounded up or down in platform-dependent way.
##
##
## SAMPLING FOR MODEL 'iCARH' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 0.001207 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 12.07 seconds.
## Chain 1: Adjust your expectations accordingly!
## Chain 1:
## Chain 1:
## Chain 1: WARNING: No variance estimation is
## Chain 1: performed for num_warmup < 20
## Chain 1:
## Chain 1: Iteration: 1 / 4 [ 25%] (Warmup)
## Chain 1: Iteration: 2 / 4 [ 50%] (Warmup)
## Chain 1: Iteration: 3 / 4 [ 75%] (Sampling)
## Chain 1: Iteration: 4 / 4 [100%] (Sampling)
## Chain 1:
## Chain 1: Elapsed Time: 0.009488 seconds (Warm-up)
## Chain 1: 0.003709 seconds (Sampling)
## Chain 1: 0.013197 seconds (Total)
## Chain 1:
## Warning: There were 2 divergent transitions after warmup. Increasing adapt_delta above 0.8 may help. See
## http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
## Warning: Examine the pairs() plot to diagnose sampling problems
# Not run
# fit.sim = iCARH.model(XX, Y, Z, pathways, control = list(adapt_delta = 0.99, max_treedepth=10),
# iter = 2000, 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, path.names=names(data.sim$pathways))
gplot
}
Normality assumptions
if(!is.null(fit.sim)){
par(mfrow=c(1,2))
iCARH.checkNormality(fit.sim)
}
## , , 1
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -12.237984 8.490859 -0.6478478 10.300806 -12.268149 -2.411147
## [2,] 11.713670 -7.496130 -1.6214825 -4.820452 12.577735 12.947961
## [3,] 1.021523 10.052922 12.9306237 3.441832 -2.413336 -2.860247
## [4,] 4.199890 -8.481284 -16.8067734 -9.524799 -7.074830 12.377829
## [,7] [,8] [,9] [,10]
## [1,] 7.903677 10.854274 2.034738 7.935948
## [2,] -6.576714 -5.104006 10.272471 -5.256710
## [3,] 12.981828 3.697520 -6.632645 8.725233
## [4,] -1.955856 8.460666 13.039247 10.003400
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -6.873712 9.087354 -5.279670 5.611758 -7.605675 -17.9557540
## [2,] 10.307124 -1.839748 -5.345065 -13.552732 14.907345 6.8843141
## [3,] -12.500734 -8.062655 18.800889 16.161683 -8.288709 0.9741412
## [4,] 12.516779 4.972371 -13.584046 -7.541273 -13.478724 15.4781404
## [,7] [,8] [,9] [,10]
## [1,] 5.515110 -2.581255 -2.723684 4.529729
## [2,] -17.573886 -1.917850 12.031709 -1.938758
## [3,] 2.654757 5.481844 -26.099673 -3.380712
## [4,] 4.171427 -13.123796 12.399156 -10.222782
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -10.0234763 5.835776 2.2444127 8.9119321 -7.9525272 1.3196726
## [2,] 5.0750628 -6.335915 -0.3052585 -0.9292296 8.8855373 11.3564194
## [3,] 4.9346763 12.526087 6.5970297 -2.1116980 0.2435676 -0.2322115
## [4,] -0.1601174 -9.474656 -11.6774141 -6.5227308 -0.6262396 8.2426437
## [,7] [,8] [,9] [,10]
## [1,] 7.412760 11.5989404 -0.2288015 8.781401
## [2,] -3.409522 -4.9713114 9.1923440 -5.075501
## [3,] 14.272215 0.6751651 -1.9636239 11.486032
## [4,] -2.582690 15.3056702 11.7450317 13.594039
##
## , , 4
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -6.549886 1.023368 0.8905038 1.818032 -5.787394 -4.433348
## [2,] 3.516789 -3.670282 1.9397796 2.035148 -1.225630 1.810065
## [3,] 4.156115 7.197439 2.4500649 -2.809247 -2.166462 -4.055916
## [4,] -4.505178 -2.662305 -9.5983651 -3.982157 -2.791907 2.467104
## [,7] [,8] [,9] [,10]
## [1,] -6.407432 -1.095246 -4.9876689 0.9011707
## [2,] -5.392784 -8.326320 0.6749083 -6.6238525
## [3,] 4.716112 -1.217768 -6.2638709 2.9234018
## [4,] -6.295200 5.659637 -0.0944266 3.1935738
##
## , , 5
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -2.997516 1.793244 0.196020 2.2672893 -2.2491094 -4.4641925
## [2,] 5.599549 -3.500118 0.186072 -0.3440584 1.9904964 0.4696804
## [3,] 3.344495 4.047842 3.270699 -0.6396523 0.6207069 -1.8569378
## [4,] -1.233999 -2.692158 -7.573681 -1.3508235 -1.9760671 2.6808330
## [,7] [,8] [,9] [,10]
## [1,] -1.521020 1.000499 -1.281322 2.543664
## [2,] -3.886805 -5.371908 2.414000 -4.904216
## [3,] 4.209928 0.995852 -5.670159 2.890474
## [4,] -3.217798 5.348853 1.657433 1.651563
##
## , , 6
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -3.568637 -0.1254793 -2.341684 -1.655294 -5.1574836 -3.712821
## [2,] 2.607228 -3.3249565 -1.118996 -1.174107 2.6431254 2.626470
## [3,] 1.705382 5.0554025 1.195662 -1.718003 -0.8424389 -1.200157
## [4,] -1.589131 -4.3501403 -7.348960 -3.860275 -2.1894324 3.886463
## [,7] [,8] [,9] [,10]
## [1,] -2.306304 0.3825681 -1.203439 -0.3237491
## [2,] -4.318277 -4.8528438 3.974261 -4.7247280
## [3,] 5.589502 -0.7745619 -4.370963 0.9460591
## [4,] -3.615768 3.5662766 4.983421 2.0471862
##
## , , 7
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -0.9620815 -0.6023768 1.708583 -2.3487132 -0.7298570 -6.16081028
## [2,] 2.2901422 -0.3086197 2.185448 2.3743082 -2.6577412 -1.11834955
## [3,] 1.0902212 1.7265736 -1.109974 -1.0479062 1.1037389 -1.64914921
## [4,] -2.8511401 1.6091551 -2.951299 -0.1125336 -0.8458407 -0.05586637
## [,7] [,8] [,9] [,10]
## [1,] -6.7053207 -0.5351024 -2.9644775 -3.653044
## [2,] -0.1941249 -2.5224984 -1.6395547 -1.055224
## [3,] 1.8655252 1.1937515 -1.3751489 -2.458560
## [4,] -2.1667545 1.2072420 0.3992678 1.891848
##
## , , 8
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -2.6708758 2.789970 1.632444 3.5376593 -1.111445 -1.3595662
## [2,] 4.7258106 -3.378753 -1.297064 -0.7869188 5.714094 3.9469865
## [3,] -0.4342072 2.390922 5.545502 1.2421750 -1.134018 -0.4014049
## [4,] 2.7355627 -1.725730 -6.172784 -1.6893412 -1.669455 5.2545036
## [,7] [,8] [,9] [,10]
## [1,] 3.5417629 4.051873 2.169223 4.068313
## [2,] -2.2887548 -1.663166 3.851915 -3.329199
## [3,] 5.1225174 1.028497 -5.393590 3.693894
## [4,] -0.5688281 3.655952 5.981731 2.207941
##
## , , 9
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -4.4502245 -0.7444472 -0.39065459 2.2704178 -4.7887747 6.401279
## [2,] 0.0841801 -3.4034902 -0.74410114 0.9103914 1.8357038 4.916481
## [3,] 4.0719217 5.6236485 0.05618371 -4.4816508 -0.1719921 -1.040700
## [4,] -2.0067211 -7.0582139 -2.09668539 -2.6423759 1.4762204 1.106144
## [,7] [,8] [,9] [,10]
## [1,] 6.0076403 7.2366936 3.4587495 3.6434919
## [2,] 0.5936031 0.2112418 0.8520687 -0.7960091
## [3,] 6.2897816 -0.4125737 6.7387694 5.0272165
## [4,] -2.2419471 6.3022267 3.7253802 7.9851393
##
## , , 10
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -11.39130424 3.670489 0.1065878 8.0163776 -7.250982 -8.078708
## [2,] 9.78377426 -8.639213 -1.9675448 -2.8418492 12.750425 10.996128
## [3,] 3.40948937 10.458425 10.3611414 -0.3259331 -2.582017 -3.530624
## [4,] -0.01783724 -11.052751 -17.6386446 -8.1746071 -4.654508 11.973391
## [,7] [,8] [,9] [,10]
## [1,] 4.767008 7.619174 -3.059506 3.735907
## [2,] -10.914580 -11.505705 12.525012 -10.530116
## [3,] 16.764318 3.082147 -13.913848 7.686628
## [4,] -7.057328 12.432027 12.680774 8.340894
##
## , , 11
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -0.35294076 -0.2847374 -0.4491810 0.7855569 -0.29525086 2.7141022
## [2,] -0.26621645 -0.1023307 -0.5817760 0.1584271 0.75462915 2.2614402
## [3,] 0.07028852 0.8472602 -0.5880407 -0.5624458 -0.03952543 0.5686656
## [4,] -0.48360387 -0.9299503 0.1467804 0.1634589 -0.21865586 0.6753575
## [,7] [,8] [,9] [,10]
## [1,] 2.5625810 3.220282 3.1175020 1.331210
## [2,] 1.0334407 1.633745 -0.1084303 2.839217
## [3,] 1.2354029 2.078150 2.1529223 1.987014
## [4,] 0.8834778 1.153952 1.6401252 2.033472
##
## , , 12
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -0.8099437 0.6451935 1.433033 4.5366519 -3.3476675 10.571634
## [2,] -0.5149203 -1.3063038 -2.330340 -0.4141577 2.7791754 7.219334
## [3,] 3.0974363 3.8349407 1.088451 -2.3971881 -0.2819714 1.396214
## [4,] 1.0767526 -4.0177781 1.406713 -0.9889432 2.5876002 1.511218
## [,7] [,8] [,9] [,10]
## [1,] 10.675871 10.9129378 6.044573 6.059953
## [2,] 4.096950 3.2019352 2.095664 2.856619
## [3,] 5.379538 0.3357998 8.607618 6.974755
## [4,] 1.227973 7.7717241 5.655493 8.546604
##
## , , 13
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -7.1564864 3.273060 2.3610359 6.611459 -4.5124868 -2.920844
## [2,] 4.7233992 -4.879892 0.4745448 1.031019 6.1503394 7.534672
## [3,] 4.9323393 8.794666 5.9113407 -1.302857 0.4950936 -1.320413
## [4,] -0.5759343 -6.690421 -10.8946510 -5.362831 -1.6151617 7.407678
## [,7] [,8] [,9] [,10]
## [1,] 3.150055 6.5072180 -0.4965134 4.738834
## [2,] -4.132604 -8.0443080 7.8679480 -7.157734
## [3,] 12.806551 0.3821275 -4.8649618 6.735008
## [4,] -4.047490 11.5822956 7.1603428 8.828332
##
## , , 14
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -0.6558976 -4.503802 3.599540 -2.057960 1.931843 3.391438
## [2,] -1.9358900 -3.572152 6.346651 9.488705 -7.963440 -1.407111
## [3,] 10.7757817 10.202365 -7.569173 -7.404362 2.026411 -2.816414
## [4,] -8.6943933 -1.647032 -4.045630 -1.000695 5.597910 -8.712013
## [,7] [,8] [,9] [,10]
## [1,] -12.185651 -2.788492 -2.539622 -5.807520
## [2,] 4.948610 -6.229564 -8.609056 -6.218252
## [3,] 3.671658 -7.048009 7.936822 -1.277135
## [4,] -5.674312 9.674427 -4.329952 7.947191
WAIC
if(!is.null(fit.sim)){
waic = iCARH.waic(fit.sim)
waic
}
## [1] 21949.33
Posterior predictive checks MAD : mean absolute deviation between covariance matrices
if(!is.null(fit.sim)){
mad = iCARH.mad(fit.sim, XX)
quantile(mad)
}
## 0% 25% 50% 75% 100%
## 0.1241618 2.6312763 6.9843658 22.8047328 633.0183807