## ----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     = "v12-metafor-parity-location-scale",
  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.bangertdrowns2004", package = "metadat")
dat <- dat.bangertdrowns2004
dat$ni100 <- dat$ni / 100
dat$meta  <- as.factor(dat$meta)
dat$imag  <- as.factor(dat$imag)

vignette_cache_load(cached_fits)

## ----precompute-models, include = FALSE, eval = [encoded data, 9950 characters]
# library("metafor")
# library("RoBMA")
# 
# data("dat.bangertdrowns2004", package = "metadat")
# dat <- dat.bangertdrowns2004
# dat$ni100 <- dat$ni / 100
# dat$meta  <- as.factor(dat$meta)
# dat$imag  <- as.factor(dat$imag)
# 
# fit1_brma <- brma(
#   yi = yi, vi = vi, measure = "SMD",
#   data = dat, seed = 1
# )
# 
# fit2_brma <- brma(
#   yi = yi, vi = vi, measure = "SMD",
#   mods  = ~ ni100,
#   scale = ~ ni100,
#   data = dat, seed = 1
# )
# 
# fit3_brma <- brma(
#   yi = yi, vi = vi, measure = "SMD",
#   mods  = ~ ni100 + meta,
#   scale = ~ ni100 + imag,
#   data = dat, seed = 1
# )
# 
# vignette_cache_save(cached_fits)

## [encoded data, 9997 characters]
data("dat.bangertdrowns2004", package = "metadat")
dat <- dat.bangertdrowns2004
dat$ni100 <- dat$ni / 100
dat$meta  <- as.factor(dat$meta)
dat$imag  <- as.factor(dat$imag)

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

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

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

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

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

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

## ----regplot-pi, fig.width = 7, fig.height = 3.[encoded data, 9951 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, mod = "ni100", pi = TRUE,
                 main = "metafor", xlim = c(0, 6), ylim = c(-1, 2))
regplot(fit2_brma, mod = "ni100", pi = TRUE,
        main = "RoBMA",   xlim = c(0, 6), ylim = c(-1, 2))

## [encoded data, 9997 characters]
fit3_metafor <- suppressWarnings(metafor::rma(
  yi, vi,
  mods  = ~ ni100 + meta,
  scale = ~ ni100 + imag,
  data = dat
))
fit3_metafor

## ----fit3-brma, eval = [encoded data, 9975 characters]
# fit3_brma <- brma(
#   yi = yi, vi = vi, measure = "SMD",
#   mods  = ~ ni100 + meta,
#   scale = ~ ni100 + imag,
#   data = dat, seed = 1
# )

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

## ----scale-posterior, fig.width = 5, fig.height = 3.[encoded data, 9946 characters]
par(mar = c(4, 4, 1, 1))
plot(fit2_brma, parameter_scale = "ni100", prior = TRUE, xlim = c(-2, 2))

