Contents

library(graper)
library(ggplot2)

Note that the implementation of logistic regression is still experimental. In particular, it has not yet been optimized in terms of computational speed and can be slow for large data sets. Furthermore, calculations of the evidence lower bound are currently not implemented to monitor convergence of the variational inference and the algorithm will stop only when the maximum number of iterations has been reached. This needs to be set to a sufficiently large number.

1 Make example data with four groups

Create an example data set with 4 groups, 100 train + 100 test samples and 320 features.

set.seed(123)
data <- makeExampleData(n=200, p=320, g=4,
                        pis=c(0.05, 0.1, 0.05, 0.1),
                        gammas=c(0.1, 0.1, 10, 10),
                        response="bernoulli")
# training data set
Xtrain <- data$X[1:100, ]
ytrain <- data$y[1:100]

# annotations of features to groups
annot <- data$annot

# test data set
Xtest <- data$X[101:200, ]
ytest <- data$y[101:200]

2 Fit the model

graper is the main function of this package, which allows to fit the proposed Bayesian models with different settings on the prior (by setting spikeslab to FALSE or TRUE) and the variational approximation (by setting factoriseQ to FALSE or TRUE). By default, the model is fit with a sparsity promoting spike-and-slab prior and a fully-factorised mean-field assumption. The ELBO is currently not calculated in the logisitc regession framework.

fit <- graper(Xtrain, ytrain, annot, verbose=FALSE,
            family="binomial", calcELB=FALSE)
## Fitting a model with 4 groups, 100 samples and 320 features.
## Fitting with random init 1
## Maximum numbers of iterations reached - no convergence or ELB not calculated
fit
## Sparse graper object for a logistic regression model with 320 predictors in 4 groups.
##  Group-wise shrinkage:
##  1   2   3   4 
##  170.93  4.17    297.4   329.72 
## Group-wise sparsity (1 = dense, 0 = sparse):
## 1    2   3   4 
## 0.35 0.1 0.29    0.25

3 Posterior distribtions

The variational Bayes (VB) approach directly yields posterior distributions for each parameter. Note, however, that using VB these are often too concentrated and cannot be directly used for construction of confidence intervals etc. However, they can provide good point estimates.

plotPosterior(fit, "gamma", gamma0=data$gammas)

plotPosterior(fit, "pi", pi0=data$pis)

4 Model coefficients and intercept

The estimated coefficients, their posterior inclusion probabilities and the intercept are contained in the result list.

# get coefficients (without the intercept)
beta <- coef(fit, include_intercept=FALSE)
# beta <- fit$EW_beta

# plot estimated versus true beta
qplot(beta, data$beta)

# get intercept
intercept <- fit$intercept
# get estimated posterior inclusion probabilities per feature
pips <- getPIPs(fit)

5 Make predictions

The function predict can be used to make prediction on new data. Here, we illustrate its use by predicting the response on the test data defined above.

preds <- predict(fit, Xtest)

#SessionInfo

sessionInfo()
## R version 4.2.2 (2022-10-31)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.5 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.16-bioc/R/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_GB              LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggplot2_3.4.0    graper_1.14.2    BiocStyle_2.26.0
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.9          highr_0.10          bslib_0.4.2        
##  [4] compiler_4.2.2      pillar_1.8.1        BiocManager_1.30.19
##  [7] jquerylib_0.1.4     tools_4.2.2         digest_0.6.31      
## [10] lattice_0.20-45     jsonlite_1.8.4      evaluate_0.20      
## [13] lifecycle_1.0.3     tibble_3.1.8        gtable_0.3.1       
## [16] pkgconfig_2.0.3     rlang_1.0.6         Matrix_1.5-3       
## [19] DBI_1.1.3           cli_3.6.0           magick_2.7.3       
## [22] yaml_2.3.6          xfun_0.36           fastmap_1.1.0      
## [25] withr_2.5.0         stringr_1.5.0       dplyr_1.0.10       
## [28] knitr_1.41          generics_0.1.3      vctrs_0.5.1        
## [31] sass_0.4.4          cowplot_1.1.1       tidyselect_1.2.0   
## [34] grid_4.2.2          glue_1.6.2          R6_2.5.1           
## [37] fansi_1.0.3         rmarkdown_2.19      bookdown_0.32      
## [40] farver_2.1.1        magrittr_2.0.3      matrixStats_0.63.0 
## [43] scales_1.2.1        htmltools_0.5.4     assertthat_0.2.1   
## [46] colorspace_2.0-3    labeling_0.4.2      utf8_1.2.2         
## [49] stringi_1.7.12      munsell_0.5.0       cachem_1.0.6