## ----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     = "v33-robma-multilevel-metaregression",
  objects  = "fit_reg"
)

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

unit_information_sd_361 <- estimate_unit_information_sd(
  sei = Havrankova2025$se,
  ni  = Havrankova2025$N
)
prior_scale_361 <- unit_information_sd_361 * 0.5

vignette_cache_load(cached_fits)

## ----precompute-models, include = FALSE, eval = [encoded data, 9950 characters]
# library("RoBMA")
# 
# data("Havrankova2025", package = "RoBMA")
# 
# unit_information_sd_361 <- estimate_unit_information_sd(
#   sei = Havrankova2025$se,
#   ni  = Havrankova2025$N
# )
# prior_scale_361 <- unit_information_sd_361 * 0.5
# 
# prior_effect_361        <- prior("normal",   list(mean = 0, sd = prior_scale_361))
# prior_heterogeneity_361 <- prior("invgamma", list(shape = 1, scale = 0.15 * prior_scale_361))
# prior_mods_361          <- list(
#   facing_customer = prior_factor("mnormal", list(mean = 0, sd = 0.25 * prior_scale_361), contrast = "meandif")
# )
# prior_bias_361          <- list(
#   prior_weightfunction("two-sided", c(0.05),             wf_cumulative(c(1, 1)),       prior_weights = 1/12),
#   prior_weightfunction("two-sided", c(0.05, 0.10),       wf_cumulative(c(1, 1, 1)),    prior_weights = 1/12),
#   prior_weightfunction("one-sided", c(0.05),             wf_cumulative(c(1, 1)),       prior_weights = 1/12),
#   prior_weightfunction("one-sided", c(0.025, 0.05),      wf_cumulative(c(1, 1, 1)),    prior_weights = 1/12),
#   prior_weightfunction("one-sided", c(0.05, 0.5),        wf_cumulative(c(1, 1, 1)),    prior_weights = 1/12),
#   prior_weightfunction("one-sided", c(0.025, 0.05, 0.5), wf_cumulative(c(1, 1, 1, 1)), prior_weights = 1/12),
#   prior_PET("Cauchy", list(0, 1), list(0, Inf), 1/4),
#   prior_PEESE("Cauchy", list(0, 5 / prior_scale_361), list(0, Inf), 1/4)
# )
# 
# fit_reg <- RoBMA(
#   yi = y, sei = se, ni = N, measure = "GEN",
#   mods = ~ facing_customer,
#   cluster = study_id,
#   prior_unit_information_sd = unit_information_sd_361,
#   prior_effect              = prior_effect_361,
#   prior_heterogeneity       = prior_heterogeneity_361,
#   prior_mods                = prior_mods_361,
#   prior_bias                = prior_bias_361,
#   sample = 20000, burnin = 10000, adapt = 10000,
#   thin = 5, parallel = TRUE, seed = 1,
#   data = Havrankova2025
# )
# 
# vignette_cache_save(cached_fits)

## [encoded data, 9997 characters]
prior_effect_361        <- prior("normal",   list(mean = 0, sd = 25.8))
prior_heterogeneity_361 <- prior("invgamma", list(shape = 1, scale = 0.15 * 25.8))
prior_mods_361          <- list(
  facing_customer = prior_factor("mnormal", list(mean = 0, sd = 0.25 * 25.8), contrast = "meandif")
)
prior_bias_361          <- list(
  prior_weightfunction("two-sided", c(0.05),             wf_cumulative(c(1, 1)),       prior_weights = 1/12),
  prior_weightfunction("two-sided", c(0.05, 0.10),       wf_cumulative(c(1, 1, 1)),    prior_weights = 1/12),
  prior_weightfunction("one-sided", c(0.05),             wf_cumulative(c(1, 1)),       prior_weights = 1/12),
  prior_weightfunction("one-sided", c(0.025, 0.05),      wf_cumulative(c(1, 1, 1)),    prior_weights = 1/12),
  prior_weightfunction("one-sided", c(0.05, 0.5),        wf_cumulative(c(1, 1, 1)),    prior_weights = 1/12),
  prior_weightfunction("one-sided", c(0.025, 0.05, 0.5), wf_cumulative(c(1, 1, 1, 1)), prior_weights = 1/12),
  prior_PET("Cauchy", list(0, 1), list(0, Inf), 1/4),
  prior_PEESE("Cauchy", list(0, 5 / 25.8), list(0, Inf), 1/4)
)

## ----fit-reg, eval = [encoded data, 9977 characters]
# fit_reg <- RoBMA(
#   yi = y, sei = se, measure = "GEN",
#   mods = ~ facing_customer,
#   cluster = study_id,
#   prior_effect              = prior_effect_361,
#   prior_heterogeneity       = prior_heterogeneity_361,
#   prior_mods                = prior_mods_361,
#   prior_bias                = prior_bias_361,
#   sample = 20000, burnin = 10000, adapt = 10000,
#   thin = 5, parallel = TRUE, seed = 1,
#   data = Havrankova2025
# )

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

## [encoded data, 9997 characters]
pooled_effect(fit_reg)

## [encoded data, 9997 characters]
mm_reg <- marginal_means(fit_reg)
summary(mm_reg)

## ----zplot, fig.width = 6, fig.height = [encoded data, 9958 characters]
par(mar = c(4, 4, 0, 0))
zplot(fit_reg, by.hist = 0.25, plot_extrapolation = FALSE, from = -4, to = 8)

