cccnrc / bayer

Bayesian analysis in R made easy
0 stars 1 forks source link

bayer: Robust and Intuitive Bayesian Analysis in Clinical Studies

bayer is an R package crafted to enhance Bayesian statistical analysis in clinical research settings. Utilizing the robust capabilities of the brms::brm() function, bayer streamlines the application of Bayesian models for survival (Cox proportional hazards), logistic, linear, and multinomial analyses. This package is designed for statisticians and researchers who require powerful, yet user-friendly tools to manage complex datasets in medical studies.

Key Features

Advantages of Bayesian Analysis in Clinical Settings

Bayesian analysis is increasingly recognized in the field of clinical research for its comprehensive approach to statistical inference, particularly for its ability to incorporate prior knowledge. Here are some of the advantages:

Easy Installation

Install bayer directly from GitHub:

devtools::install_github("cccnrc/bayer")

Usage Example

Below is an example of how to conduct a Bayesian logistic regression analysis using bayer on the R built-in dataset mtcars which comes along R by default:

# Load the library
library(bayer)

### import the built-in dataset mtcars
mtcars <- datasets::mtcars

### specify the treatment variable and the covariates you want to analyze
model_logistic <- bayer_logistic(
                      data = mtcars,
                      outcome = 'am',
                      covariates = c( 'mpg', 'cyl', 'vs', 'carb' )
                    )

Catching posterior risk distribution and trace plots

bayer allows for easily visualization of posterior risk distribution to elucidate the specific effect of a variable of interest:

posterior <- extract_posterior( model_logistic, var = 'mpg' )

This will generate an object(posterior in the example above) for each variable factor identified in the model (b_mpg only in the example above) which contains:

Specify Bayesian Priors

You can easily include Bayesian priors in the analysis and see how the model changes:

### specify prior for a covariate of interest
priors <- brms::set_prior( 'normal( -0.15, 0.27 )', coef = 'mpg' )

### include priors in the model
model_logistic_prior <- bayer_logistic(
                          data = mtcars,
                          outcome = 'am',
                          covariates = c( 'mpg', 'cyl', 'vs', 'carb' ),
                          prior = priors
                        )

### plot results
posterior_prior <- extract_posterior( model_logistic_prior, var = 'mpg' )

prior risk plot

Calculate Bayes Factor, WAIC and LOO

bayer allows for calculation of Bayes Factor, Watanabe-Akaike Information Criterion (WAIC), and Leave-One-Out Cross-Validation (LOO) to elucidate actual effect of a specific variable:

bf <- get_bayes_factor( model_logistic, var = 'mpg' )
waic <- waic_analysis( model_logistic, var = 'mpg' )
loo <- loo_analysis( model_logistic, var = 'mpg'  )

bf contains the Bayesian Factor for the variable effect in the model. waic and loo are objects that contain mean WAIC and LOO difference (mean) with relative standard error (se) and p-value (p). bf, waic, and loo allows a precise analysis of how including the variable of interest in your model increase its quality, with relative statstical significance calculated.

Easy IPTW implementation

bayer is made to handle Inverse Probability of Treatment Weighting (IPTW) and weighted regression analysis.

Let's say we want to nullify covariates effect to focus on the actual effect of a treatment variable of interest vs of the model_logistic above.

library(riptw)

### calculate standardized weights for the variable of interest ("vs") based on the rest of covariates
iptw_weight <- riptw( data = mtcars,
                      outcome = 'vs',
                      covariates = c( 'mpg', 'cyl', 'carb' )
                     )

### use IPTW weights in the Bayesian analysis
model_logistic_iptw <- bayer_logistic(
                          data = iptw_weight$data,
                          outcome = 'am',
                          covariates = 'vs',
                          weights = 'sw'
                        )

### plot and analyze model quality and variable of interest effect as in the example above
posterior_iptw <- extract_posterior( model_logistic_iptw, var = 'vs' )
bf_iptw <- get_bayes_factor( model_logistic_iptw, var = 'vs' )
waic_iptw <- waic_analysis( model_logistic_iptw, var = 'vs' )
loo_iptw <- loo_analysis( model_logistic_iptw, var = 'vs'  )

bayer is built to be easily paired with riptw, an R package that allows easily IPTW analysis

Multiple Regression Types

bayer supports:

Parallelize bayer

In order to significantly speed up analysis, bayer needs you to have cmdstanr installed and configured. This is generally quite simple as:

remotes::install_github("stan-dev/cmdstanr")
library(cmdstanr)

check toolchain is properly set:

check_cmdstan_toolchain()

install_cmdstan:

install_cmdstan( cores = <your-CPU-number-here> )

check everything worked properly:

file <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan")
mod <- cmdstan_model(file)

if everything worked fine you should see:

Model executable is up to date!

Every time you run bayer it will automatically check for cmdstanr and, in case it is installed and properly configured, it will use it with specified cores, speeding up a lot the sampling part of the analysis!

Customize bayer

bayer allows for a lot of more detailed analysis. Take a look at:

?bayer_linear
?bayer_logistic
?bayer_multinomial
?bayer_coxph
?extract_posterior
?get_bayes_factor
?waic_analysis
?loo_analysis

Contributing

Contributions to bayer are more than welcomed!