## ----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     = "v31-robma-metaregression",
  objects  = c("fit_BMA", "fit_RoBMA"),
  packages = c("metafor", "emmeans")
)

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("Andrews2021", package = "RoBMA")

Andrews2021_z <- metafor::escalc(
  ri = ri, ni = ni,
  measure = "ZCOR", data = Andrews2021
)

vignette_cache_load(cached_fits)

## ----precompute-models, include = FALSE, eval = [encoded data, 9950 characters]
# library("RoBMA")
# 
# data("Andrews2021", package = "RoBMA")
# 
# Andrews2021_z <- metafor::escalc(
#   ri = ri, ni = ni,
#   measure = "ZCOR", data = Andrews2021
# )
# 
# prior_effect_361 <- prior("normal", list(mean = 0, sd = 0.5))
# prior_heterogeneity_361 <- prior("invgamma", list(shape = 1, scale = 0.075))
# 
# fit_BMA <- BMA(
#   yi = yi, vi = vi, measure = "ZCOR",
#   mods = ~ measure + age,
#   prior_effect        = prior_effect_361,
#   prior_heterogeneity = prior_heterogeneity_361,
#   data = Andrews2021_z, parallel = TRUE, seed = 1
# )
# 
# fit_RoBMA <- RoBMA(
#   yi = yi, vi = vi, measure = "ZCOR",
#   mods = ~ measure + age,
#   prior_effect        = prior_effect_361,
#   prior_heterogeneity = prior_heterogeneity_361,
#   data = Andrews2021_z, parallel = TRUE, seed = 1
# )
# 
# vignette_cache_save(cached_fits)

## [encoded data, 9997 characters]
library(RoBMA)
data("Andrews2021", package = "RoBMA")
head(Andrews2021)[,c("slab", "ri", "ni", "measure", "age")]

## [encoded data, 9997 characters]
Andrews2021_z <- metafor::escalc(
  ri = ri, ni = ni,
  measure = "ZCOR", data = Andrews2021
)

## [encoded data, 9997 characters]
fit_rma <- metafor::rma(yi, vi, mods = ~ measure + age, data = Andrews2021_z)
fit_rma

## [encoded data, 9997 characters]
emm_rma <- emmeans::emmeans(metafor::emmprep(fit_rma), specs = "measure")
emm_rma
metafor::transf.ztor(as.data.frame(emm_rma)[, c("emmean", "asymp.LCL", "asymp.UCL")])

## [encoded data, 9997 characters]
prior_effect_361 <- prior("normal", list(mean = 0, sd = 0.5))
prior_heterogeneity_361 <- prior("invgamma", list(shape = 1, scale = 0.075))

## ----fit-bma, eval = [encoded data, 9977 characters]
# fit_BMA <- BMA(
#   yi = yi, vi = vi, measure = "ZCOR",
#   mods = ~ measure + age,
#   prior_effect        = prior_effect_361,
#   prior_heterogeneity = prior_heterogeneity_361,
#   data = Andrews2021_z, parallel = TRUE, seed = 1
# )

## [encoded data, 9997 characters]
summary(fit_BMA, include_mcmc_diagnostics = FALSE)
pooled_effect(fit_BMA, output_measure = "COR")

## [encoded data, 9997 characters]
BMA_marginal <- marginal_means(fit_BMA, output_measure = "COR")
summary(BMA_marginal)

## ----fig_BMA, fig.width = 6, fig.height = 4, out.width = "75%", fig.align = "center"[encoded data, 9914 characters]
par(mar = c(4, 4, 1, 4))
plot(BMA_marginal, parameter = "measure", lwd = 2)

## ----fit-robma, eval = [encoded data, 9975 characters]
# fit_RoBMA <- RoBMA(
#   yi = yi, vi = vi, measure = "ZCOR",
#   mods = ~ measure + age,
#   prior_effect        = prior_effect_361,
#   prior_heterogeneity = prior_heterogeneity_361,
#   data = Andrews2021_z, parallel = TRUE, seed = 1
# )

## [encoded data, 9997 characters]
summary(fit_RoBMA, include_mcmc_diagnostics = FALSE)
pooled_effect(fit_RoBMA, output_measure = "COR")

## [encoded data, 9997 characters]
RoBMA_marginal <- marginal_means(fit_RoBMA, output_measure = "COR")
summary(RoBMA_marginal)

## ----fig_RoBMA, fig.width = 6, fig.height = 4, out.width = "75%", fig.align = "center"[encoded data, 9912 characters]
par(mar = c(4, 4, 1, 4))
plot(RoBMA_marginal, parameter = "measure", lwd = 2)

