## ----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     = "v20-bayesian-model-averaging",
  objects  = c("fit_RE", "fit_FE", "fit_BMA_he", "fit_BMA", "fit_BMA_mod"),
  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("RoBMA")

data("dat.baskerville2012", package = "metadat")
dat <- dat.baskerville2012

vignette_cache_load(cached_fits)

## ----precompute-models, include = FALSE, eval = [encoded data, 9950 characters]
# library("RoBMA")
# 
# data("dat.baskerville2012", package = "metadat")
# dat <- dat.baskerville2012
# dat$tailor <- as.factor(dat$tailor)
# 
# fit_RE <- brma(
#   yi = smd, sei = se, measure = "SMD",
#   data = dat, seed = 1
# )
# fit_RE <- add_marglik(fit_RE)
# 
# fit_FE <- brma(
#   yi = smd, sei = se, measure = "SMD",
#   prior_heterogeneity = prior("spike", list(location = 0)),
#   data = dat, seed = 1
# )
# fit_FE <- add_marglik(fit_FE)
# 
# fit_BMA_he <- BMA(
#   yi = smd, sei = se, measure = "SMD",
#   prior_effect_null = NULL,
#   data = dat, seed = 1
# )
# 
# fit_BMA <- BMA(
#   yi = smd, sei = se, measure = "SMD",
#   data = dat, seed = 1
# )
# 
# fit_BMA_mod <- BMA(
#   yi = smd, sei = se, measure = "SMD",
#   mods = ~ tailor,
#   data = dat, seed = 1
# )
# 
# vignette_cache_save(cached_fits)

## ----set-up, include = [encoded data, 9975 characters]
library("RoBMA")

data("dat.baskerville2012", package = "metadat")
dat <- dat.baskerville2012
dat$tailor <- as.factor(dat$tailor)
head(dat[, c("year", "smd", "se", "tailor")])

## [encoded data, 9997 characters]
res <- metafor::rma.uni(
  yi = smd, sei = se,
  data = dat, method = "REML"
)
res

## ----fit-RE, eval = [encoded data, 9978 characters]
# fit_RE <- brma(
#   yi = smd, sei = se, measure = "SMD",
#   data = dat, seed = 1
# )
# fit_RE <- add_marglik(fit_RE)

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

## ----fit-FE, eval = [encoded data, 9978 characters]
# fit_FE <- brma(
#   yi = smd, sei = se, measure = "SMD",
#   prior_heterogeneity = prior("spike", list(location = 0)),
#   data = dat, seed = 1
# )
# fit_FE <- add_marglik(fit_FE)

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

## [encoded data, 9997 characters]
bf(fit_RE, fit_FE)

## ----fit-BMA-he, eval = [encoded data, 9974 characters]
# fit_BMA_he <- BMA(
#   yi = smd, sei = se, measure = "SMD",
#   prior_effect_null = NULL,
#   data = dat, seed = 1
# )

## [encoded data, 9997 characters]
summary(fit_BMA_he)

## ----fit-BMA, eval = [encoded data, 9977 characters]
# fit_BMA <- BMA(
#   yi = smd, sei = se, measure = "SMD",
#   data = dat, seed = 1
# )

## [encoded data, 9997 characters]
summary(fit_BMA)

## ----plot-BMA-mu, fig.width = 6, fig.height = 4, out.width = "75%", fig.align = "center"[encoded data, 9910 characters]
par(mar = c(4, 4, 1, 4))
plot(fit_BMA, parameter = "mu", prior = TRUE, xlim = c(-1, 1.5), lwd = 2)

## ----plot-BMA-tau, fig.width = 6, fig.height = 4, out.width = "75%", fig.align = "center"[encoded data, 9909 characters]
par(mar = c(4, 4, 1, 4))
plot(fit_BMA, parameter = "tau", prior = TRUE, xlim = c(0, 1), lwd = 2)

## [encoded data, 9997 characters]
summary(fit_BMA, conditional = TRUE)

## [encoded data, 9997 characters]
res_mod <- metafor::rma.uni(
  yi = smd, sei = se, mods = ~ tailor,
  data = dat, method = "REML"
)
res_mod

## ----fit-BMA-mod, eval = [encoded data, 9973 characters]
# fit_BMA_mod <- BMA(
#   yi = smd, sei = se, measure = "SMD",
#   mods = ~ tailor,
#   data = dat, seed = 1
# )

## [encoded data, 9997 characters]
summary(fit_BMA_mod)

## ----regplot-BMA-mod, fig.width = 5, fig.height = 3.5, fig.align = "center"[encoded data, 9923 characters]
par(mar = c(4, 4, 1, 1))
regplot(fit_BMA_mod, mod = "tailor")

## ----regplot-BMA-emm, fig.width = 5, fig.height = 3.5, fig.align = "center"[encoded data, 9923 characters]
par(mar = c(4, 4, 1, 1))
emm <- marginal_means(fit_BMA_mod)
emm

plot(emm, parameter = "tailor", prior = TRUE)

## [encoded data, 9997 characters]
fit_priors <- BMA(
  yi = smd, sei = se, measure = "SMD",
  rescale_priors = 0.5,
  data = dat, only_priors = TRUE
)
print_prior(fit_priors, parameter = "mu")

## [encoded data, 9997 characters]
fit_priors <- BMA(
  yi = smd, sei = se, measure = "SMD",
  prior_effect_null = prior("normal", list(mean = 0, sd = 0.05)),
  data = dat, only_priors = TRUE
)
print_prior(fit_priors, parameter = "mu")

## [encoded data, 9997 characters]
fit_priors <- BMA(
  yi = smd, sei = se, measure = "SMD",
  mods = ~ tailor,
  prior_mods_null = list(tailor = NULL),
  data = dat, only_priors = TRUE
)
print_prior(fit_priors, parameter_mods = "tailor")

## [encoded data, 9997 characters]
fit_priors <- BMA(
  yi = smd, sei = se, measure = "SMD",
  prior_heterogeneity = prior(
    distribution = "normal",
    parameters   = list(mean = 0, sd = 0.25),
    truncation   = list(lower = 0)
  ),
  data = dat, only_priors = TRUE
)
print_prior(fit_priors, parameter = "tau")

