## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  warning=FALSE,
  comment = "#>"
)
options(rmarkdown.html_vignette.check_title = FALSE)

## ----warning=FALSE,message=FALSE----------------------------------------------
library(RaCE.NMA)
library(reshape2) # data reformatting
library(ggplot2)  # creating nice figures

## -----------------------------------------------------------------------------
data("toy_data")
head(toy_data)

## ----fig.asp=0.45, fig.cap="Figure 1: Forest plot of estimated relative treatment effects in a toy example dataset, based on a standard NMA model."----
forestplot_muhat(data = toy_data, level = 0.95, 
                  order_by_average = FALSE)

## ----eval=FALSE---------------------------------------------------------------
# mcmc_raceNMA(posterior = toy_data)

## ----eval=TRUE----------------------------------------------------------------
mu_hat_toy <- apply(toy_data, 2, mean)
cov_toy <- cov(toy_data)
s_toy <- apply(toy_data, 2, sd)

## ----eval=FALSE---------------------------------------------------------------
# mcmc_raceNMA(mu_hat = mu_hat_toy, cov = cov_toy) # example with mu_hat and cov
# mcmc_raceNMA(mu_hat = mu_hat_toy, s   = s_toy  ) # example with mu_hat and s

## ----eval=FALSE---------------------------------------------------------------
# mcmc_raceNMA(
#   posterior = toy_data, # results of a standard NMA model
#   mu0 = mean(mu_hat_toy), sigma0 = sqrt(10*var(mu_hat_toy)), # model hyperparameters
# )

## -----------------------------------------------------------------------------
mcmc_results_toy <- mcmc_raceNMA(
  posterior = toy_data, # results of a standard NMA model
  mu0 = mean(mu_hat_toy), sigma0 = sqrt(10*var(mu_hat_toy)), # model hyperparameters
  iter = 20000, nu_iter = 3, # MCMC settings
  chains = 2, burn_prop = 0.5, thin = 2, seed = 1, cores=2
)

## ----fig.cap="Figure 2: Trace plots of relative treatment effects in a toy example dataset, based on the RaCE-NMA model."----
traceplot_mu(mcmc_results_toy)

## ----fig.cap="Figure 3: Trace plot of the number of rank-clusters, K, in a toy example dataset, based on the RaCE-NMA model."----
traceplot_K(mcmc_results_toy)

## ----fig.cap="Table 1: R-hat statistics for the relative treatment effects in a toy example dataset, based on the RaCE-NMA model."----
calculate_Rhat(mcmc_results_toy, level = 0.9)

## ----fig.asp=0.6, fig.cap="Figure 4: Posterior rank probabilities for each intervention in a toy example dataset, based on the RaCE-NMA model. Ranks are displayed on the ordinal scale (1 = best). Interventions are grouped into rank clusters representing non-exclusive superiority sets; multiple interventions within the same cluster may share identical posterior ranks."----
clusterplot_ranks(mcmc=mcmc_results_toy, label_ranks = 1:4)

## ----fig.asp=0.4, fig.cap="Figure 5: Cumulative ranking probability curves for each intervention in a toy example dataset, based on the RaCE-NMA model."----
cumulativeprobplot_ranks(mcmc=mcmc_results_toy)

## ----fig.cap="Table 2: SUCRA and MNBT statistics for each intervention in a toy example dataset, based on the RaCE-NMA model."----
calculate_SUCRA_MNBT(mcmc=mcmc_results_toy, level = 0.5)

## -----------------------------------------------------------------------------
data("wang_posterior")
round(head(wang_posterior), 3)

## -----------------------------------------------------------------------------
# define assumed mean and variance for baseline treatment (R-CHOP)
mu_hat_baseline <- 0
var_baseline <- min(apply(wang_posterior,2,var))/10

# calculate summary statistics, mu_hat and cov
mu_hat_wang <- c( mu_hat_baseline, apply(wang_posterior,2,mean) )
cov_wang <- cbind( c(var_baseline, rep(0, 10) ),
              rbind(0, cov(wang_posterior)) )

# store treatment names
treatments <- c("R-CHOP", names(wang_posterior))

## ----fig.asp=0.5, fig.cap="Figure 6: Forest plot of estimated relative treatment effects in a network meta-analysis by Wang et. al (2022), based on a standard NMA model."----
forestplot_muhat(data = cbind(0,wang_posterior), 
                 names = treatments)

## ----cache=TRUE---------------------------------------------------------------
mcmc_results_casestudy <- mcmc_raceNMA(
  mu_hat = mu_hat_wang, cov = cov_wang,
  iter = 20000, nu_iter = 4, chains = 2, seed = 1, cores = 2
)

## ----fig.cap="Figure 7: Trace plots of relative treatment effects in a network meta-analysis by Wang et. al (2022), based on the RaCE-NMA model."----
traceplot_mu(mcmc_results_casestudy, names=treatments)

## ----fig.cap="Figure 8: Trace plot of the number of rank-clusters, K, in a network meta-analysis by Wang et. al (2022), based on the RaCE-NMA model."----
traceplot_K(mcmc_results_casestudy)

## ----fig.cap="Table 3: R-hat statistics for the relative treatment effects in a network meta-analysis by Wang et. al (2022), based on the RaCE-NMA model."----
calculate_Rhat(mcmc_results_casestudy, names=treatments, level = 0.8)

## ----fig.asp=0.6, fig.cap="Figure 9: Posterior rank probabilities for each intervention in a network meta-analysis by Wang et. al (2022), based on the RaCE-NMA model. Ranks are displayed on the ordinal scale (1 = best). Interventions are grouped into rank clusters representing non-exclusive superiority sets; multiple interventions within the same cluster may share identical posterior ranks."----
clusterplot_ranks(mcmc=mcmc_results_casestudy, names = treatments, label_ranks = 1)

## ----fig.asp=0.4, fig.cap="Figure 10: Cumulative ranking probability curves for each intervention in a network meta-analysis by Wang et. al (2022), based on the RaCE-NMA model."----
cumulativeprobplot_ranks(mcmc=mcmc_results_casestudy, names=treatments)

## ----fig.cap="Table 4: SUCRA and MNBT statistics for each intervention in a network meta-analysis by Wang et. al (2022), based on the RaCE-NMA model."----
calculate_SUCRA_MNBT(mcmc=mcmc_results_casestudy,names=treatments,level=0.50)

