## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  out.width = "300px",
  fig.width =  1.5625, # 150px / 96dpi
  fig.height =  1.5625, # makes 300 px plots
  dpi = 96,
  fig.align = "center"
)


## ----eval = TRUE, echo = FALSE------------------------------------------------
show_plot <- function(svg){
  old_mar <- par()["mar"]
  on.exit(par(old_mar))
  par(mar=rep(0,4))
  charToRaw(svg) |> 
  rsvg::rsvg_png(width = 300) |> 
  magick::image_read() |>
  plot()
}


## ----echo = TRUE, eval=FALSE--------------------------------------------------
# library(metainsight)
# run_metainsight()


## ----write_plot, echo = TRUE, eval = FALSE------------------------------------
# summary_study(configured_data) |> write_plot("summary_study_plot.pdf")


## ----setup_load---------------------------------------------------------------
library(metainsight)

loaded_data <- setup_load(outcome = "continuous")

loaded_data$data[1:6, 1:7] |> knitr::kable()


## ----setup_configure----------------------------------------------------------
configured_data <- setup_configure(loaded_data = loaded_data,
                                   reference_treatment = "Placebo",
                                   effects = "random",
                                   outcome_measure = "MD",
                                   ranking_option = "good",
                                   seed = 123)


## ----setup_exclude------------------------------------------------------------
subsetted_data <- setup_exclude(configured_data = configured_data, 
                                exclusions = c("Study01", "Study02"))


## ----summary_char-------------------------------------------------------------
characteristics <- summary_char(configured_data)

characteristics$network |> knitr::kable()


## ----summary_study------------------------------------------------------------
summary_study(configured_data) |> show_plot()


## ----summary_network----------------------------------------------------------
summary_network(configured_data, style = "netplot") |> show_plot()


## ----freq_forest--------------------------------------------------------------
freq_forest(configured_data) |> show_plot()


## ----freq_compare-------------------------------------------------------------
freq_compare(configured_data) |> knitr::kable()


## ----freq_inconsistency-------------------------------------------------------
freq_inconsistency(configured_data) |> knitr::kable()


## ----freq_summary-------------------------------------------------------------
freq_summary(configured_data) |> show_plot()


## ----bayes_model--------------------------------------------------------------
fitted_bayes_model <- bayes_model(configured_data,
                                  n_adapt = 100,
                                  n_iter = 100)


## ----bayes_ranking------------------------------------------------------------
ranking_data <- bayes_ranking(fitted_bayes_model, configured_data)
ranking_plot(ranking_data, "rankogram") |> show_plot()


## ----bayes_mcmc, eval = FALSE-------------------------------------------------
# mcmc_plots <- bayes_mcmc(fitted_bayes_model)
# 
# mcmc_plots$gelman_plots
# mcmc_plots$trace_plots
# mcmc_plots$density_plots


## ----bayes_deviance, eval = FALSE---------------------------------------------
# deviance_plots <- bayes_deviance(fitted_bayes_model)
# 
# deviance_plots$scat_plot
# deviance_plots$stem_plot
# deviance_plots$lev_plot


## ----bayes_nodesplit----------------------------------------------------------
nodesplit_path <- system.file("extdata", 
                              "continuous_nodesplit.csv", 
                              package = "metainsight")

nodesplit_loaded_data <- setup_load(data_path = nodesplit_path,
                                    outcome = "continuous")

nodesplit_configured_data <- setup_configure(loaded_data = nodesplit_loaded_data,
                                             reference_treatment = "Placebo",
                                             effects = "random",
                                             outcome_measure = "MD",
                                             ranking_option = "good",
                                             seed = 123)

nodesplit_model <- bayes_nodesplit(nodesplit_configured_data, 
                                   n_adapt = 100,
                                   n_iter = 100)

bayes_nodesplit_plot(nodesplit_model) |> show_plot()


## ----covariate_model----------------------------------------------------------
fitted_covariate_model <- covariate_model(configured_data,
                                          covariate_value = 50,
                                          regressor_type = "shared",
                                          n_adapt = 100,
                                          n_iter = 100)


## ----covariate_regression-----------------------------------------------------
regression_data <- covariate_regression(fitted_covariate_model, 
                                        configured_data)

metaregression_plot(fitted_covariate_model,
                    configured_data,
                    regression_data,
                    comparators = c("Glucocorticoids",
                                    "Ketamine",
                                    "Gabapentinoids")) |> show_plot()


## ----baseline_summary---------------------------------------------------------
baseline_summary(configured_data) |> show_plot()


## ----baseline_model-----------------------------------------------------------
fitted_baseline_model <- baseline_model(configured_data,
                                        regressor_type = "shared",
                                        n_iter = 120,
                                        max_iter = 120,
                                        check_iter = 12)


## ----export_cinema------------------------------------------------------------
cinema_project <- export_cinema(configured_data)
writeLines(cinema_project, tempfile(fileext = ".json"))


## ----eval = FALSE-------------------------------------------------------------
# common <- readRDS("<path to file>")

