## ----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     = "v11-metafor-parity-publication-bias",
  objects  = c(
    "fit_PET_brma", "fit_PEESE_brma", "fit_selmodel_brma",
    "fit_PET_brma_flex", "fit_PEESE_brma_flex", "fit_selmodel_brma_flex"
  ),
  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.lehmann2018", package = "metadat")
dat <- dat.lehmann2018

vignette_cache_load(cached_fits)

## ----precompute-models, include = FALSE, eval = [encoded data, 9950 characters]
# library("metafor")
# library("RoBMA")
# 
# data("dat.lehmann2018", package = "metadat")
# dat <- dat.lehmann2018
# 
# fit_PET_brma <- bPET(
#   yi = yi, vi = vi, measure = "SMD",
#   data = dat, seed = 1
# )
# 
# fit_PEESE_brma <- bPEESE(
#   yi = yi, vi = vi, measure = "SMD",
#   data = dat, seed = 1
# )
# 
# fit_selmodel_brma <- bselmodel(
#   yi = yi, vi = vi, measure = "SMD",
#   data = dat, seed = 1
# )
# 
# fit_PET_brma_flex <- bPET(
#   yi = yi, vi = vi, measure = "SMD",
#   prior_bias = prior_PET(
#     distribution = "cauchy",
#     parameters = list(location = 0, scale = 1),
#     truncation = list(lower = -Inf, upper = Inf)
#   ),
#   data = dat, seed = 1
# )
# 
# fit_PEESE_brma_flex <- bPEESE(
#   yi = yi, vi = vi, measure = "SMD",
#   prior_bias = prior_PEESE(
#     distribution = "cauchy",
#     parameters = list(location = 0, scale = 5),
#     truncation = list(lower = -Inf, upper = Inf)
#   ),
#   data = dat, seed = 1
# )
# 
# fit_selmodel_brma_flex <- bselmodel(
#   yi = yi, vi = vi, measure = "SMD",
#   prior_bias = prior_weightfunction(
#     "one-sided",
#     steps   = c(0.025),
#     weights = wf_independent(
#       prior("normal", parameters = list(mean = 0, sd = 0.5)),
#       scale = "log_omega"
#     )
#   ),
#   data = dat, seed = 1
# )
# 
# vignette_cache_save(cached_fits)

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

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

## ----fit-PET-brma, eval = [encoded data, 9972 characters]
# fit_PET_brma <- bPET(
#   yi = yi, vi = vi, measure = "SMD",
#   data = dat, seed = 1
# )

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

## ----PET-posterior, fig.width = 7, fig.height = 3.[encoded data, 9948 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(fit_PET_brma,  parameter = "PET", prior = TRUE, main = "PET coefficient", xlim = c(0, 4))
plot_pet_peese(fit_PET_brma, prior = TRUE, main = "PET regression")

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

## ----fit-PEESE-brma, eval = [encoded data, 9970 characters]
# fit_PEESE_brma <- bPEESE(
#   yi = yi, vi = vi, measure = "SMD",
#   data = dat, seed = 1
# )

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

## ----PEESE-posterior, fig.width = 7, fig.height = 3.[encoded data, 9946 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(fit_PEESE_brma, parameter = "PEESE", prior = TRUE, main = "PEESE coefficient", xlim = c(0, 8))
plot_pet_peese(fit_PEESE_brma, prior = TRUE, main = "PEESE regression")

## [encoded data, 9997 characters]
fit_rma_metafor      <- metafor::rma(yi, vi, data = dat, method = "ML")
fit_selmodel_metafor <- metafor::selmodel(fit_rma_metafor, type = "stepfun", steps = .025, decreasing = TRUE)
fit_selmodel_metafor

## ----fit-selmodel-brma, eval = [encoded data, 9967 characters]
# fit_selmodel_brma <- bselmodel(
#   yi = yi, vi = vi, measure = "SMD",
#   data = dat, seed = 1
# )

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

## ----selmodel-omega, fig.width = 7, fig.height = 3.[encoded data, 9947 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(fit_selmodel_brma, parameter = "omega", prior = TRUE, xlim = c(0, 1))

## ----selmodel-weightfunction, fig.width = 4.5, fig.height = 3.[encoded data, 9936 characters]
par(mar = c(4, 4, 2, 1), cex.axis = 0.8, cex.lab = 0.8, cex.main = 0.75)
plot_weightfunction(fit_selmodel_brma, prior = TRUE, main = "Weight-function")

## ----funnel-plots, fig.width = 7, fig.height = 3.[encoded data, 9949 characters]
par(mfrow = c(1, 3), mar = c(4, 4, 2, 1), cex.axis = 0.8, cex.lab = 0.8, cex.main = 0.75)
xlim <- c(-2, 2.5); ylim <- c(0, 0.8)
funnel(fit_PET_brma,      main = "PET",       xlim = xlim, ylim = ylim)
funnel(fit_PEESE_brma,    main = "PEESE",     xlim = xlim, ylim = ylim)
funnel(fit_selmodel_brma, main = "Sel. Mod.", xlim = xlim, ylim = ylim)

## ----fit-PET-brma-flex, eval = [encoded data, 9967 characters]
# fit_PET_brma_flex <- bPET(
#   yi = yi, vi = vi, measure = "SMD",
#   prior_bias = prior_PET(
#     distribution = "cauchy",
#     parameters = list(location = 0, scale = 1),
#     truncation = list(lower = -Inf, upper = Inf)
#   ),
#   data = dat, seed = 1
# )

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

## ----PET-flex-comparison, 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.85)
plot(fit_PET_brma,      parameter = "PET", prior = TRUE,
  main = "PET (default)",     xlim = c(-2, 4))
abline(v = 0, lty = 2, col = "grey50")
plot(fit_PET_brma_flex, parameter = "PET", prior = TRUE,
  main = "PET (untruncated)", xlim = c(-2, 4))
abline(v = 0, lty = 2, col = "grey50")

## ----fit-PEESE-brma-flex, eval = [encoded data, 9965 characters]
# fit_PEESE_brma_flex <- bPEESE(
#   yi = yi, vi = vi, measure = "SMD",
#   prior_bias = prior_PEESE(
#     distribution = "cauchy",
#     parameters = list(location = 0, scale = 5),
#     truncation = list(lower = -Inf, upper = Inf)
#   ),
#   data = dat, seed = 1
# )

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

## ----PEESE-flex-comparison, fig.width = 7, fig.height = 3.[encoded data, 9940 characters]
par(mfrow = c(1, 2), mar = c(4, 4, 2, 1), cex.axis = 0.8, cex.lab = 0.8, cex.main = 0.85)
plot(fit_PEESE_brma, parameter = "PEESE", prior = TRUE, main = "PEESE (default)", xlim = c(-5, 10))
abline(v = 0, lty = 2, col = "grey50")
plot(fit_PEESE_brma_flex, parameter = "PEESE", prior = TRUE, main = "PEESE (untruncated)", xlim = c(-5, 10))
abline(v = 0, lty = 2, col = "grey50")

## ----fit-selmodel-brma-flex, eval = [encoded data, 9962 characters]
# fit_selmodel_brma_flex <- bselmodel(
#   yi = yi, vi = vi, measure = "SMD",
#   prior_bias = prior_weightfunction(
#     "one-sided",
#     steps   = c(0.025),
#     weights = wf_independent(
#       prior("normal", parameters = list(mean = 0, sd = 0.5)),
#       scale = "log_omega"
#     )
#   ),
#   data = dat, seed = 1
# )

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

## ----selmodel-flex-comparison, fig.width = 7, fig.height = 3.[encoded data, 9937 characters]
par(mfrow = c(1, 2), mar = c(4, 4, 2, 1), cex.axis = 0.8, cex.lab = 0.8, cex.main = 0.85)
plot(fit_selmodel_brma, parameter = "omega", prior = TRUE, main = "omega (default)", xlim = c(0, 3))
abline(v = 1, lty = 2, col = "grey50")
plot(fit_selmodel_brma_flex, parameter = "omega", prior = TRUE, main = "omega (log_omega)", xlim = c(0, 3))
abline(v = 1, lty = 2, col = "grey50")

