## ----vignette-nowrap-options, include = FALSE, eval = [encoded data, 9944 characters]
options(width = 10000)

## ----setup, include = [encoded data, 9976 characters]
source("vignette-cache.R", local = knitr::knit_global())

cached_fits <- vignette_cache(
  name     = "v02-bayesian-meta-analysis",
  objects  = c("fit1_brma", "fit2_brma", "fit3_brma"),
  packages = c("metafor", "metadat")
)

knitr::opts_chunk$set(
  collapse    = TRUE,
  comment     = "#>",
  eval        = vignette_cache_eval(cached_fits),
  message     = FALSE,
  warning     = FALSE,
  fig.width   = 7,
  fig.height  = 3.5,
  dev         = "png",
  fig.retina  = 3
)
if (.Platform$OS.type == "windows") {
  knitr::opts_chunk$set(dev.args = list(type = "cairo"))
}

## ----load-models, include = [encoded data, 9970 characters]
library("metafor")
library("RoBMA")

data("dat.bcg", package = "metadat")

dat <- metafor::escalc(
  ai = tpos, bi = tneg, ci = cpos, di = cneg,
  measure = "RR", data = dat.bcg
)

vignette_cache_load(cached_fits)

## ----precompute-models, include = FALSE, eval = [encoded data, 9950 characters]
# library("metafor")
# library("RoBMA")
# 
# data("dat.bcg", package = "metadat")
# 
# dat <- metafor::escalc(
#   ai = tpos, bi = tneg, ci = cpos, di = cneg,
#   measure = "RR", data = dat.bcg
# )
# 
# fit1_brma <- brma(
#   yi = yi, vi = vi, measure = "RR",
#   data = dat, seed = 1
# )
# fit1_brma <- add_loo(fit1_brma)
# fit1_brma <- add_marglik(fit1_brma)
# 
# fit2_brma <- brma(
#   yi = yi, vi = vi, measure = "RR",
#   mods = ~ ablat,
#   data = dat, seed = 1
# )
# fit2_brma <- add_loo(fit2_brma)
# fit2_brma <- add_marglik(fit2_brma)
# 
# fit3_brma <- brma(
#   yi = yi, vi = vi, measure = "RR",
#   mods = ~ ablat + alloc,
#   data = dat, seed = 1
# )
# fit3_brma <- add_loo(fit3_brma)
# fit3_brma <- add_marglik(fit3_brma)
# 
# vignette_cache_save(cached_fits)

## [encoded data, 9997 characters]
data("dat.bcg", package = "metadat")

dat <- metafor::escalc(
  ai = tpos, bi = tneg, ci = cpos, di = cneg,
  measure = "RR", data = dat.bcg
)

## [encoded data, 9997 characters]
fit1_metafor <- metafor::rma(yi, vi, data = dat, method = "REML")
fit1_metafor

## ----fit1-brma, eval = [encoded data, 9975 characters]
# fit1_brma <- brma(
#   yi = yi, vi = vi, measure = "RR",
#   data = dat, seed = 1
# )

## [encoded data, 9997 characters]
summary(fit1_brma, include_mcmc_diagnostics = FALSE)

## [encoded data, 9997 characters]
pooled_effect(fit1_brma, transform = "EXP")

## ----fit1-brma-plot, fig.width = 5, fig.height = 3.[encoded data, 9947 characters]
par(mar = c(4, 4, 1, 1))
plot(fit1_brma, parameter = "mu", prior = TRUE, xlim = c(-3, 3))

## [encoded data, 9997 characters]
confint(fit1_metafor)

## [encoded data, 9997 characters]
summary_heterogeneity(fit1_brma)

## ----funnel-plot, fig.width = 7, fig.height = 3.[encoded data, 9950 characters]
par(mfrow = c(1, 2), mar = c(4, 4, 2, 1), cex.axis = 0.8, cex.lab = 0.8, cex.main = 0.75)
metafor::funnel(fit1_metafor, main = "metafor", xlim = c(-2, 1), ylim = c(0, 0.8))
funnel(fit1_brma, main = "RoBMA", xlim = c(-2, 1), ylim = c(0, 0.8), sampling_heterogeneity = FALSE)

## [encoded data, 9997 characters]
fit2_metafor <- metafor::rma(yi, vi, mods = ~ ablat, data = dat)
fit2_metafor

## ----fit2-brma, eval = [encoded data, 9975 characters]
# fit2_brma <- brma(
#   yi = yi, vi = vi, measure = "RR",
#   mods = ~ ablat,
#   data = dat, seed = 1
# )

## [encoded data, 9997 characters]
summary(fit2_brma, include_mcmc_diagnostics = FALSE)

## ----regplot-continuous, fig.width = 7, fig.height = 3.[encoded data, 9943 characters]
par(mfrow = c(1, 2), mar = c(4, 4, 2, 1), cex.axis = 0.8, cex.lab = 0.8, cex.main = 0.75)
metafor::regplot(fit2_metafor, main = "metafor", xlim = c(10, 60), ylim = c(-2, 0.5))
regplot(fit2_brma, main = "RoBMA", xlim = c(10, 60), ylim = c(-2, 0.5))

## [encoded data, 9997 characters]
new_data <- data.frame(ablat = c(15, 35, 55))
predict(fit2_brma, newdata = new_data, type = "terms")

## ----add-marglik, eval = [encoded data, 9973 characters]
# fit1_brma <- add_marglik(fit1_brma)
# fit2_brma <- add_marglik(fit2_brma)

## [encoded data, 9997 characters]
bf(fit2_brma, fit1_brma)

## [encoded data, 9997 characters]
fit3_metafor <- metafor::rma(yi, vi, mods = ~ ablat + alloc, data = dat)

## ----fit3-brma, eval = [encoded data, 9975 characters]
# fit3_brma <- brma(
#   yi = yi, vi = vi, measure = "RR",
#   mods = ~ ablat + alloc,
#   data = dat, seed = 1
# )

## [encoded data, 9997 characters]
summary(fit3_brma, include_mcmc_diagnostics = FALSE)

## ----regplot-categorical, fig.width = 7, fig.height = 3.[encoded data, 9942 characters]
par(mfrow = c(1, 2), mar = c(4, 4, 2, 1), cex.axis = 0.8, cex.lab = 0.8, cex.main = 0.75)
metafor::regplot(fit3_metafor, mod = "allocrandom", main = "metafor: random dummy", ylim = c(-2, 0.5))
regplot(fit3_brma, mod = "alloc", main = "RoBMA: alloc factor", ylim = c(-2, 0.5))

## [encoded data, 9997 characters]
emm <- marginal_means(fit3_brma)
summary(emm)

## ----marginal-means-plot, fig.width = 4.5, fig.height = 3.[encoded data, 9940 characters]
par(mar = c(4, 4, 1, 1), cex.axis = 0.8, cex.lab = 0.8, cex.main = 0.75)
plot(emm, parameter = "alloc", xlim = c(-2, 0.5), lwd = 2)

## ----add-loo, eval = [encoded data, 9977 characters]
# fit3_brma <- add_loo(fit3_brma)

## [encoded data, 9997 characters]
head(rstudent(fit3_metafor)$z)
head(rstudent(fit3_brma)$z)

## ----qqplot, fig.width = 7, fig.height = 3.[encoded data, 9955 characters]
par(mfrow = c(1, 2), mar = c(4, 4, 2, 1), cex.axis = 0.8, cex.lab = 0.8, cex.main = 0.75)
qqnorm(fit3_metafor, main = "metafor: studentized", xlim = c(-1.5, 1.5), ylim = c(-3, 3))
qqnorm(fit3_brma, main = "RoBMA: LOO-PIT", xlim = c(-1.5, 1.5), ylim = c(-3, 3))

## [encoded data, 9997 characters]
inf_brma <- influence(fit3_brma)
head(inf_brma$inf)

