## ----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     = "v10-metafor-parity-multilevel",
  objects  = c("fit1_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.konstantopoulos2011", package = "metadat")
dat <- dat.konstantopoulos2011

vignette_cache_load(cached_fits)

## ----precompute-models, include = FALSE, eval = [encoded data, 9950 characters]
# library("metafor")
# library("RoBMA")
# 
# data("dat.konstantopoulos2011", package = "metadat")
# dat <- dat.konstantopoulos2011
# 
# fit1_brma <- brma(
#   yi = yi, vi = vi, measure = "SMD",
#   cluster = district,
#   data = dat, seed = 1
# )
# 
# vignette_cache_save(cached_fits)

## [encoded data, 9997 characters]
data("dat.konstantopoulos2011", package = "metadat")
dat <- dat.konstantopoulos2011
head(dat)

## [encoded data, 9997 characters]
fit1_metafor <- metafor::rma.mv(yi, vi, random = ~ school | district, data = dat)
fit1_metafor

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

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

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

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

## ----tau-rho-posterior, fig.width = 7, fig.height = 3.[encoded data, 9944 characters]
par(mfrow = c(1, 2), mar = c(4, 4, 2, 1), cex.axis = 0.8, cex.lab = 0.8, cex.main = 0.75)
plot(fit1_brma, parameter = "tau", prior = TRUE, main = "tau", xlim = c(0, 1))
plot(fit1_brma, parameter = "rho", prior = TRUE, main = "rho", xlim = c(0, 1))

