MixedTypeData

Zhiwen Tan

Introduction

BCClong is an R package for performing Bayesian Consensus Clustering (BCC) model for clustering continuous, discrete and categorical longitudinal data, which are commonly seen in many clinical studies. This document gives a tour of BCClong package.

see help(package = "BCClong") for more information and references provided by citation("BCClong")

To download BCClong, use the following commands:

require("devtools")
devtools::install_github("ZhiwenT/BCClong", build_vignettes = TRUE)
library("BCClong")

To list all functions available in this package:

ls("package:BCClong")

Components

Currently, there are 5 function in this package which are BCC.multi, BayesT, model.selection.criteria, traceplot, trajplot.

BCC.multi function performs clustering on mixed-type (continuous, discrete and categorical) longitudinal markers using Bayesian consensus clustering method with MCMC sampling and provide a summary statistics for the computed model. This function will take in a data set and multiple parameters and output a BCC model with summary statistics.

BayesT function assess the model goodness of fit by calculate the discrepancy measure T(, ) with following steps (a) Generate T.obs based on the MCMC samples (b) Generate T.rep based on the posterior distribution of the parameters (c) Compare T.obs and T.rep, and calculate the P values.

model.selection.criteria function calculates DIC and WAIC for the fitted model traceplot function visualize the MCMC chain for model parameters trajplot function plot the longitudinal trajectory of features by local and global clustering

Pre-process (Setting up)

In this example, the PBCseq data in the mixAK package was used as it is a public data set. The variables used here include lbili, platelet, and spiders. Of these three variables, lbili and platelet are continuous variables, while spiders are categorical variables.

library(BCClong)
library(mixAK)
data(PBC910)

Fit BCC Model Using BCC.multi Function

Here, We used a binomial distribution for spiders marker, a gaussian distribution for the lbili marker and poisson distribution for platelet, respectively. The number of clusters was set to 2. All hyper parameters were set to default.

We ran the model with 12,000 iterations, discard the first 2,000 sample, and kept every 10th sample. This resulted in 1,000 samples for each model parameter. The MCMC sampling process took about 30 minutes on an AMD Ryzen\(^{TM}\) 5 5600X desktop computer.

Since this program takes a long time to run, here we will use the pre-compile result in this example. The pre-compiled data file can be found here (./inst/extdata/PBCseq.rds)

set.seed(89)
fit.BCC2 <- BCC.multi(
    mydat = list(PBC910$lbili,PBC910$platelet,PBC910$spiders),
    dist = c("gaussian","poisson","binomial"),
    id = list(PBC910$id),
    time = list(PBC910$month),
    formula =list(y ~ time + (1|id),y ~ time + (1|id), y ~ time + (1|id)),
    num.cluster = 2,
    burn.in = 100,            
    thin = 10,                  
    per = 10,                     
    max.iter = 200) 

To run the pre-compiled result, please download the PBCseq.rds object from github under inst/extdata/ folder. Then run the following code.

# pre-compiled result
fit.BCC2 <- readRDS("../inst/extdata/PBCseq.rds")

Printing Summary Statistics for key model parameters

To print the summary statistics for all parameters

fit.BCC2$summary.stat

To print the proportion for each cluster (mean, sd, 2.5% and 97.5% percentile) geweke statistics (geweke.stat) between -2 and 2 suggests the parameters converge

fit.BCC2$summary.stat$PPI

The code below prints out all major parameters

print(fit.BCC2$summary.stat$PPI)
#>                     [,1]        [,2]
#> mean          0.92156956  0.07843044
#> sd            0.05106657  0.05106657
#> 2.5%          0.85047520  0.01171743
#> 97.5%         0.98828257  0.14952480
#> geweke.stat -10.34604373 10.34604373
print(fit.BCC2$summary.stat$ALPHA)
#>                    [,1]       [,2]       [,3]
#> mean        0.504219437 0.51748095 0.80783581
#> sd          0.003763516 0.01699388 0.02903671
#> 2.5%        0.500782965 0.50037179 0.77781701
#> 97.5%       0.511449666 0.55117093 0.85592659
#> geweke.stat 1.895661030 4.42684423 0.80798946
print(fit.BCC2$cluster.global)
#>   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#>  [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#>  [75] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#> [112] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#> [149] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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1 1 2 2 1 1 2 1 2 2
#> [112] 1 1 2 1 1 2 2 1 2 1 1 2 1 1 1 2 1 1 1 2 2 2 1 1 1 1 2 1 1 1 2 1 1 2 2 2 1
#> [149] 1 1 2 2 2 2 1 1 1 2 2 2 1 1 2 2 1 2 1 2 2 2 1 1 2 2 1 1 2 2 1 2 2 2 2 1 1
#> [186] 1 2 2 2 1 1 1 1 1 2 1 1 1 2 2 1 2 1 1 2 1 2 1 2 2 2 2 2 1 2 1 2 1 2 2 1 2
#> [223] 2 1 1 2 2 1 1 2 1 2 1 2 2 1 1 2 2 2 1 1 1 2 2 2 2 2 1 1 1 2 1 2 1 1 2 1 1
#> [260] 2
print(fit.BCC2$cluster.local[[3]])
#>   [1] 2 2 2 2 1 1 1 1 1 2 1 1 1 1 1 2 1 1 2 1 1 1 1 1 1 1 2 1 1 1 2 1 1 2 1 1 1
#>  [38] 1 1 1 1 2 1 2 2 1 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 1 1 2 1 1 1 1 2 1 1 1 1
#>  [75] 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 1 1 2 2 1 2 1 2 1 1 1 2 1 2 1 1 1 1 1 1 1 1
#> [112] 1 1 1 2 2 1 1 1 2 1 1 1 2 1 1 1 2 1 1 2 1 1 1 1 1 1 2 2 1 1 2 1 1 1 1 2 1
#> [149] 1 1 1 2 1 1 2 1 1 1 2 1 2 1 2 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#> [186] 1 1 2 2 1 2 1 2 2 1 1 1 2 1 1 1 1 1 1 2 1 1 2 2 2 2 1 1 1 1 2 1 1 1 1 1 1
#> [223] 1 2 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1
#> [260] 2

Visualize Clusters

We can use the traceplot function to plot the MCMC process and the trajplot function to plot the trajectory for each feature.

gp1 <- trajplot(fit=fit.BCC2,feature.ind=1,which.cluster = "local.cluster",
            title= bquote(paste("Local Clustering (",hat(alpha)[1] ==.(round(fit.BCC2$alpha[1],2)),")")),
            xlab="months",ylab="lbili",color=c("#00BA38", "#619CFF"))
gp2 <- trajplot(fit=fit.BCC2,feature.ind=2,which.cluster = "local.cluster",
            title= bquote(paste("Local Clustering (",hat(alpha)[2] ==.(round(fit.BCC2$alpha[2],2)),")")),
            xlab="months",ylab="platelet",color=c("#00BA38", "#619CFF"))
gp3 <- trajplot(fit=fit.BCC2,feature.ind=3,which.cluster = "local.cluster",
            title= bquote(paste("Local Clustering (",hat(alpha)[3] ==.(round(fit.BCC2$alpha[3],2)),")")),
            xlab="months",ylab="spiders",color=c("#00BA38", "#619CFF"))
gp4 <- trajplot(fit=fit.BCC2,feature.ind=1,which.cluster = "global.cluster",
            title="Global Clustering",
            xlab="months",ylab="lbili",color=c("#00BA38", "#619CFF"))
gp5 <- trajplot(fit=fit.BCC2,feature.ind=2,which.cluster = "global.cluster",
            title="Global Clustering",
            xlab="months",ylab="platelet",color=c("#00BA38", "#619CFF"))
gp6 <- trajplot(fit=fit.BCC2,feature.ind=3,which.cluster = "global.cluster",
            title="Global Clustering",
            xlab="months",ylab="spiders",color=c("#00BA38", "#619CFF"))

library(cowplot)
#dev.new(width=180, height=120)
plot_grid(gp1, gp2,gp3,gp4,gp5,gp6, 
          labels=c("(A)", "(B)", "(C)", "(D)", "(E)", "(F)"), ncol = 3,   align = "v" )

Package References

Tan, Z., Shen, C., Lu, Z. (2022) BCClong: an R package for performing Bayesian Consensus Clustering model for clustering continuous, discrete and categorical longitudinal data.

sessionInfo()
#> R version 4.3.2 (2023-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 11 x64 (build 22631)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=C                    LC_CTYPE=English_Canada.utf8   
#> [3] LC_MONETARY=English_Canada.utf8 LC_NUMERIC=C                   
#> [5] LC_TIME=English_Canada.utf8    
#> 
#> time zone: America/Toronto
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] mixAK_5.7        lme4_1.1-35.1    Matrix_1.6-5     colorspace_2.1-0
#>  [5] cowplot_1.1.3    ggplot2_3.5.0    joineRML_0.4.6   survival_3.5-7  
#>  [9] nlme_3.1-163     BCClong_1.0.2   
#> 
#> loaded via a namespace (and not attached):
#>  [1] gtable_0.3.4         xfun_0.41            bslib_0.6.1         
#>  [4] lattice_0.21-9       vctrs_0.6.4          tools_4.3.2         
#>  [7] generics_0.1.3       stats4_4.3.2         parallel_4.3.2      
#> [10] tibble_3.2.1         fansi_1.0.6          highr_0.10          
#> [13] cluster_2.1.4        pkgconfig_2.0.3      lifecycle_1.0.4     
#> [16] farver_2.1.1         compiler_4.3.2       MatrixModels_0.5-3  
#> [19] mcmc_0.9-8           munsell_0.5.0        mnormt_2.1.1        
#> [22] combinat_0.0-8       fastGHQuad_1.0.1     codetools_0.2-19    
#> [25] SparseM_1.81         quantreg_5.97        htmltools_0.5.7     
#> [28] sass_0.4.8           evd_2.3-6.1          yaml_2.3.8          
#> [31] gmp_0.7-4            pillar_1.9.0         nloptr_2.0.3        
#> [34] jquerylib_0.1.4      MASS_7.3-60          randtoolbox_2.0.4   
#> [37] truncdist_1.0-2      cachem_1.0.8         iterators_1.0.14    
#> [40] foreach_1.5.2        boot_1.3-28.1        abind_1.4-5         
#> [43] mclust_6.0.1         tidyselect_1.2.0     digest_0.6.34       
#> [46] mvtnorm_1.2-4        LaplacesDemon_16.1.6 dplyr_1.1.4         
#> [49] labeling_0.4.3       splines_4.3.2        fastmap_1.1.1       
#> [52] grid_4.3.2           cli_3.6.1            magrittr_2.0.3      
#> [55] cobs_1.3-7           label.switching_1.8  utf8_1.2.4          
#> [58] withr_3.0.0          Rmpfr_0.9-5          scales_1.3.0        
#> [61] rmarkdown_2.25       rngWELL_0.10-9       nnet_7.3-19         
#> [64] coda_0.19-4.1        evaluate_0.23        lpSolve_5.6.20      
#> [67] knitr_1.45           doParallel_1.0.17    mgcv_1.9-0          
#> [70] rlang_1.1.1          MCMCpack_1.7-0       Rcpp_1.0.12         
#> [73] glue_1.6.2           rstudioapi_0.15.0    minqa_1.2.6         
#> [76] jsonlite_1.8.8       R6_2.5.1