## ----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     = "v21-robust-bayesian-meta-analysis",
  objects  = c("fit_RoBMA", "fit_RoBMA_custom"),
  packages = c("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.lehmann2018", package = "metadat")
dat <- dat.lehmann2018

vignette_cache_load(cached_fits)

## ----precompute-models, include = FALSE, eval = [encoded data, 9950 characters]
# library("RoBMA")
# 
# data("dat.lehmann2018", package = "metadat")
# dat <- dat.lehmann2018
# 
# fit_RoBMA <- RoBMA(
#   yi = yi, vi = vi, measure = "SMD",
#   data = dat, seed = 1
# )
# 
# fit_RoBMA_custom <- RoBMA(
#   yi = yi, vi = vi, measure = "SMD",
#   prior_bias = list(
#     prior_PET(
#       distribution = "Cauchy",
#       parameters   = list(location = 0, scale = 1),
#       truncation   = list(lower = 0, upper = Inf)
#     ),
#     prior_PEESE(
#       distribution = "Cauchy",
#       parameters   = list(location = 0, scale = 5),
#       truncation   = list(lower = 0, upper = Inf)
#     ),
#     prior_weightfunction(
#       "one-sided",
#       steps   = c(0.025),
#       weights = wf_cumulative(c(1, 1))
#     )
#   ),
#   data = dat, seed = 1
# )
# 
# vignette_cache_save(cached_fits)

## ----data, include = [encoded data, 9977 characters]
library("RoBMA")

data("dat.lehmann2018", package = "metadat")
dat <- dat.lehmann2018
head(dat[, c("Short_Title", "Year", "yi", "vi")])

## ----fit-RoBMA, eval = [encoded data, 9975 characters]
# fit_RoBMA <- RoBMA(
#   yi = yi, vi = vi, measure = "SMD",
#   data = dat, seed = 1
# )

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

## [encoded data, 9997 characters]
summary_models(fit_RoBMA)

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

## [encoded data, 9997 characters]
interpret(fit_RoBMA, scope = "all")

## [encoded data, 9997 characters]
fit_priors <- RoBMA(
  yi = yi, vi = vi, measure = "SMD",
  model_type = "PP",
  data = dat, only_priors = TRUE
)
print_prior(fit_priors, parameter = "bias")

## ----fit-RoBMA-custom, eval = [encoded data, 9968 characters]
# fit_RoBMA_custom <- RoBMA(
#   yi = yi, vi = vi, measure = "SMD",
#   prior_bias = list(
#     prior_PET(
#       distribution = "Cauchy",
#       parameters   = list(location = 0, scale = 1),
#       truncation   = list(lower = 0, upper = Inf)
#     ),
#     prior_PEESE(
#       distribution = "Cauchy",
#       parameters   = list(location = 0, scale = 5),
#       truncation   = list(lower = 0, upper = Inf)
#     ),
#     prior_weightfunction(
#       "one-sided",
#       steps   = c(0.025),
#       weights = wf_cumulative(c(1, 1))
#     )
#   ),
#   data = dat, seed = 1
# )

## [encoded data, 9997 characters]
interpret(fit_RoBMA_custom, scope = c("components", "estimates"))

