# ==============================================================================
# Hypoxic training strategies and VO2max - Analysis
# ==============================================================================
# Packages
library(dplyr)
library(tidyr)
library(stringr)
library(readr)
library(meta)
library(metafor)
library(janitor)
library(gtsummary)
library(gt)
library(metaviz)

theme_gtsummary_journal(journal = "jama")
theme_gtsummary_compact()

set.seed(2026)

fig_path <- ""
database <- ""

# ==============================================================================
# Section: Import data - cleaning - calculation
# ==============================================================================

db_raw <- read.csv2(database, sep = ";", dec = ",")

# Assumed pre-post correlation for change score variance
r_prepost <- 0.78

db <- db_raw |>
  clean_names() |>
   # Remove study without a control group (id = 41)
  filter(id != 41) |>
  
  # Harmonize FiO2 values:
  # - Values > 1 are assumed to be percentages and converted to fractions
  # - FiO2 equal to 0.20 is recoded to 0.21 (ambient air)
  mutate(
    fio2 = ifelse(fio2 == 0.2, 0.21, fio2),
    fio2 = case_when(
      fio2 <= 1    ~ fio2 * 100,
      TRUE        ~ fio2),
    
    study = str_squish(as.character(authors)),
    studlab = paste0(study, " et al [", id, "]"),
    studgroup = paste0(study, year, id), 

    # Harmonize group coding
    group = str_squish(as.character(group)),

    # Harmonize subgroup descriptors used in your original .qmd
    subname = str_squish(as.character(subname)),
    athlele = str_squish(as.character(athlele)),
    athlele = str_replace_all(athlele, regex("^non\\s*[- ]?athletes?$", ignore_case = TRUE), "Non-Athletes"),
    athlele = str_replace_all(athlele, regex("^non\\s*athletes?$", ignore_case = TRUE), "Non-Athletes"),
    athlele = str_replace_all(athlele, regex("^athletes?$", ignore_case = TRUE), "Athletes"),
    exp_ath = paste0(subname, "_", athlele),
  )

# ----------------------------------------------------------
# Derivation of standard deviations when only SE is reported
# ----------------------------------------------------------
db <- db |>
  mutate(
    pre_sd = case_when(
      !is.na(vo2max_pre_sd)  ~ vo2max_pre_sd,
      is.na(vo2max_pre_sd) & !is.na(vo2max_pre_se) & !is.na(n) ~ vo2max_pre_se * sqrt(n),
      TRUE ~ NA_real_
    ),
    post_sd = case_when(
      !is.na(vo2max_post_sd) ~ vo2max_post_sd,
      is.na(vo2max_post_sd) & !is.na(vo2max_post_se) & !is.na(n) ~ vo2max_post_se * sqrt(n),
      TRUE ~ NA_real_
    )
  )

# ----------------------------------------------------------
# Descriptive table
# ----------------------------------------------------------
db |>
  select(-id, -link, -authors, -title, -studgroup, -journal_s_name, -study, -studlab, -year) |>
  tbl_summary(by = athlele,
              statistic = all_continuous() ~ "{median} [{p25}–{p75}] ({min}–{max})") |>
  add_overall()

db |>
  select(-id, -link, -authors, -title, -studgroup, -journal_s_name, -study, -studlab, -year) |>
  tbl_summary(by = subname,
              statistic = all_continuous() ~ "{median} [{p25}–{p75}] ({min}–{max})") |>
  add_overall()

# ----------------------------------------------------------
# Participants counts (sum of n)
# ----------------------------------------------------------
n_participants_overall <- db |>
  summarise(N_participants = sum(n, na.rm = TRUE))

n_participants_by_ath <- db |>
  group_by(athlele) |>
  summarise(N_participants = sum(n, na.rm = TRUE), .groups = "drop")

n_participants_by_modality <- db |>
  group_by(subname) |>
  summarise(N_participants = sum(n, na.rm = TRUE), .groups = "drop") |>
  arrange(desc(N_participants))

n_participants_by_ath_modality_group <- db |>
  group_by(athlele, subname, group) |>
  summarise(N_participants = sum(n, na.rm = TRUE), .groups = "drop") |>
  arrange(athlele, subname, group)

gt::gt(n_participants_by_ath_modality_group) |>
  gt::fmt_number(columns = "N_participants", decimals = 0) |>
  gt::tab_header(title = "Participants (somme des n par bras)")

# ==============================================================================
# Section: Construction of study-level effects
# ==============================================================================

# Identify duplicated arms within each study, study group,
# and experimental group, and assign a unique arm identifier
dup_check <- db |>
  summarise(
    n_rows = dplyr::n(),
    .by = c(id, studgroup, group)
  ) |>
  filter(n_rows > 1)

# Assign a unique arm number within each
# (id, studgroup, group) combination
db <- db |>
  group_by(id, studgroup, group) |>
  mutate(
    studgroup = paste0(studgroup, "_", dplyr::row_number())
  ) |>
  ungroup()

# ----------------------------------------------------------
# Compute within-arm mean change and its sampling variance using escalc()
# measure="MC" returns yi = (m2 - m1) and vi based on SDs and ri
# ----------------------------------------------------------
db_escalc <- metafor::escalc(
  measure = "MC",
  m1i = vo2max_pre,
  m2i = vo2max_post,
  sd1i = pre_sd,
  sd2i = post_sd,
  ri = r_prepost,
  ni = n,
  data = db
) |>
  as_tibble() |>
  mutate(
    # For clarity
    mean_change = yi,
    var_change  = vi
  )

# ----------------------------------------------------------
# Build study-level effects: (Change_H - Change_N)
# Use vi = vi_H + vi_N assuming independent groups
# ----------------------------------------------------------
es <- db_escalc |>
  select(
    id, studlab, studgroup, exp_ath,
    dur_day, session_freq, tot_dur, ratio_day,
    group, n, mean_change, var_change, subname, 
    vo2max_pre, athlele, age, x_male, prop_vo2max_pmax, 
    prop_h_rmax, dur_day, fio2 , session_freq, tot_dur, 
    ratio_day, n_cycles, cont_interval, n
  ) |>
  pivot_wider(
    names_from  = group,
    values_from = c(n, mean_change, var_change, exp_ath,
                    dur_day, fio2, session_freq, tot_dur, ratio_day,
                    subname, vo2max_pre, athlele, age, x_male,
                    prop_vo2max_pmax, prop_h_rmax,
                    n_cycles, cont_interval),
    names_sep   = "."
  ) |>
  filter(!is.na(mean_change.H), !is.na(mean_change.N)) |>
  mutate(
    yi = mean_change.H - mean_change.N,
    vi = var_change.H + var_change.N,
    n = n.H + n.N,

    # Bring moderators to study level (prefer H, otherwise N)
    exp_ath            = coalesce(exp_ath.H, exp_ath.N),
    dur_day            = coalesce(dur_day.H, dur_day.N),
    session_freq       = coalesce(session_freq.H, session_freq.N),
    tot_dur            = coalesce(tot_dur.H, tot_dur.N),
    ratio_day          = coalesce(ratio_day.H, ratio_day.N),
    fio2               = coalesce(fio2.H, fio2.N),
    subname            = coalesce(subname.H, subname.N),
    vo2max_pre         = coalesce(vo2max_pre.H, vo2max_pre.N),
    athlele            = coalesce(athlele.H, athlele.N),
    age                = coalesce(age.H, age.N),
    x_male             = coalesce(x_male.H, x_male.N),
    prop_vo2max_pmax   = coalesce(prop_vo2max_pmax.H, prop_vo2max_pmax.N),
    prop_h_rmax        = coalesce(prop_h_rmax.H, prop_h_rmax.N),
    n_cycles           = coalesce(n_cycles.H, n_cycles.N),
    cont_interval      = coalesce(cont_interval.H, cont_interval.N)
  ) |>
  select(id, studlab, studgroup, yi, vi,
    exp_ath, subname, vo2max_pre, athlele, age, x_male,
    prop_vo2max_pmax, prop_h_rmax,
    dur_day, fio2, session_freq, tot_dur, ratio_day,
    n_cycles, cont_interval, n.H, n.N, n
  ) |>
  arrange(id, studgroup)

# ----------------------------------------------------------
# Fit meta-analysis with meta::metagen (REML)
# and use meta plotting functions for forest plots
# ----------------------------------------------------------
m_vo2 <- metagen(
  TE         = -yi,
  seTE       = sqrt(vi),
  studlab    = studlab,
  data       = es,
  sm         = "MD",
  cluster    = id,
  method.tau = "REML",
  prediction = FALSE,
  control    = list(maxiter = 1000, stepadj = 0.5)
)

summary(m_vo2)

metafor::forest(m_vo2,
       common=FALSE,
       allstudies=FALSE,
       smlab= "Mean Change (95% CI)",
       label.e="",
       leftcols="studlab",
       rightlab=c("MC","95% CI"," Weights"), 
       layout="meta", 
       calcwidth.hetstat=TRUE,
       test.subgroup=FALSE,
       subgroup.name="", 
       sort.subgroup=FALSE, 
       fontsize = 8) 

# ----------------------------------------------------------
# Fit meta-analysis with meta::metagen (REML) by subgroup
# and use meta plotting functions for forest plots
# ----------------------------------------------------------
m_vo2_subgroup <- metagen(
  TE         = -yi,
  seTE       = sqrt(vi),
  studlab    = studlab,
  data       = es,
  sm         = "MD",
  cluster    = id,
  method.tau = "REML",
  prediction = FALSE,
  control    = list(maxiter = 1000, stepadj = 0.5),
  subgroup   = exp_ath
)

summary(m_vo2_subgroup)

metafor::forest(m_vo2_subgroup,
       common=FALSE,
       allstudies=FALSE,
       smlab= "Mean Change (95% CI)",
       label.e="",
       leftcols="studlab",
       rightlab=c("MC","95% CI"," Weights"), 
       layout="meta", 
       calcwidth.hetstat=TRUE,
       test.subgroup=FALSE,
       subgroup.name="", 
       fontsize = 8) 

# ==============================================================================
# Section: Construction of study-level effects > By subgroup
# ==============================================================================

exp_ath_levels <- es |>
  distinct(exp_ath) |>
  pull(exp_ath) |>
  sort()

for(g in exp_ath_levels){
es_filter <- es |>
  filter(exp_ath == g)

m_vo2_filter <- metagen(
  TE         = -yi,
  seTE       = sqrt(vi),
  studlab    = studlab,
  data       = es_filter,
  sm         = "MD",
  cluster    = id,
  method.tau = "REML",
  prediction = FALSE,
  control    = list(maxiter = 1000, stepadj = 0.5)
)

summary(m_vo2_filter)

rightlabs_use <- if (m_vo2_filter$k > 1) {
    c("MC", "95% CI", "Weights")
  } else {
    c("MC", "95% CI")
  }

metafor::forest(m_vo2_filter,
       common=FALSE,
       allstudies=FALSE,
       smlab= paste0(g, '\n', "Mean Change (95% CI)"),
       label.e="",
       leftcols="studlab",
       rightlab=rightlabs_use, 
       layout="meta", 
       calcwidth.hetstat=TRUE,
       test.subgroup=FALSE,
       subgroup.name="", 
       sort.subgroup=FALSE, 
       fontsize = 8) 

eps_file <- file.path(fig_path, 
                        paste0("forest_", g, ".eps"))
}

# ==============================================================================
# Section: Metaregression > Univariate
# ==============================================================================
mods <- c("subname","vo2max_pre", "athlele", "age", "prop_male", "prop_vo2max_pmax", 
          "prop_h_rmax", "dur_day","fio2", "session_freq", "tot_dur", "cont_interval", "n" )

pred <- c()
n <- c()
estimate <- c()
Cinf <- c()
Csup <- c()
p_val <- c()

  for(mod in mods){
    tryCatch({
  m_reg <- rma.mv(yi = -yi, 
    V = vi, 
    data = es, 
    method = 'REML', 
    mods = as.formula(paste('~', mod)),
    random = list(~1| studlab, ~1| id))
  
    pred <- c(pred, row.names(m_reg$beta)[-1])  
    n <- c(n, rep(m_reg$k, length(m_reg$b[-1])))
    estimate <- c(estimate, m_reg$b[-1])
    Cinf <- c(Cinf, m_reg$ci.lb[-1])
    Csup <- c(Csup, m_reg$ci.ub[-1])
    p_val <- c(p_val, m_reg$pval[-1])
    
    }, error = function(e){
    pred <- c(pred, mod) 
    n <- c(n, m_reg$k)
    estimate <- c(estimate, NA)
    Cinf <- c(Cinf, NA)
    Csup <- c(Csup, NA)
    p_val <- c(p_val, NA)
})
  }

meta_table <- gt(data.frame(
  predictor = pred,
  n = n, 
  estimate = round(estimate,3),
  Cinf = round(Cinf, 3), 
  Csup = round(Csup, 3), 
  p_val = round(p_val,3)) )

meta_table |>
  data_color(columns = contains('p_val'),
             method = 'bin',
             palette = 'lightblue', 
             bins = c(0, 0.05), 
             na_color = 'white')

# ==============================================================================
# Section: Metaregression > Multivariate
# ==============================================================================

mods <- c("age", "prop_male", "prop_vo2max_pmax", "prop_h_rmax", "dur_day","fio2",
          "session_freq", "tot_dur", "cont_interval", "blinded","n" )


pred <- c()
n <- c()
estimate <- c()
Cinf <- c()
Csup <- c()
p_val <- c()

  for(mod in mods){
    tryCatch({
  m_reg <- rma.mv(yi = -yi, 
    V = vi, #vector of length with the corresponding sampling variances 
    data = es, 
    method = 'REML', 
    mods = reformulate(c(mod, 'subname', 'athlele', 'vo2max_pre')),
    random = list(~1| studlab, ~1| id))
  
    pred <- c(pred, row.names(m_reg$beta)[2])  
    n <- c(n, rep(m_reg$k, length(m_reg$b[2])))
    estimate <- c(estimate, m_reg$b[2])
    Cinf <- c(Cinf, m_reg$ci.lb[2])
    Csup <- c(Csup, m_reg$ci.ub[2])
    p_val <- c(p_val, m_reg$pval[2])
    
    
    }, error = function(e){
    pred <- c(pred, mod) 
    n <- c(n, m_reg$k)
    estimate <- c(estimate, NA)
    Cinf <- c(Cinf, NA)
    Csup <- c(Csup, NA)
    p_val <- c(p_val, NA)
})
  }

meta_table <- gt(data.frame(
  predictor = pred,
  n = n, 
  estimate = round(estimate,3),
  Cinf = round(Cinf, 3), 
  Csup = round(Csup, 3), 
  p_val = round(p_val,3)))

meta_table |>
  data_color(columns = contains('p_val'),
             method = 'bin',
             palette = 'lightblue', 
             bins = c(0, 0.05), 
             na_color = 'white')

# ==============================================================================
# Metaregression > By subgroup > LLTH
# ==============================================================================
#Univariate
mods <- c("subname","vo2max_pre", "athlele", "age", "prop_male", "prop_vo2max_pmax", 
          "prop_h_rmax", "dur_day","fio2", "session_freq", "tot_dur", "cont_interval", "n" )

pred <- c()
n <- c()
estimate <- c()
Cinf <- c()
Csup <- c()
p_val <- c()

  for(mod in mods){
    tryCatch({
  m_reg <- rma.mv(yi = -yi, 
    V = vi, 
    data = subset(es, subname == 'LLTH'), 
    method = 'REML', 
    mods = as.formula(paste('~', mod)),
    random = list(~1| studlab, ~1| id))
  
    pred <- c(pred, row.names(m_reg$beta)[-1])  
    n <- c(n, rep(m_reg$k, length(m_reg$b[-1])))
    estimate <- c(estimate, m_reg$b[-1])
    Cinf <- c(Cinf, m_reg$ci.lb[-1])
    Csup <- c(Csup, m_reg$ci.ub[-1])
    p_val <- c(p_val, m_reg$pval[-1])
    
    }, error = function(e){
    pred <- c(pred, mod) 
    n <- c(n, m_reg$k)
    estimate <- c(estimate, NA)
    Cinf <- c(Cinf, NA)
    Csup <- c(Csup, NA)
    p_val <- c(p_val, NA)
})
  }

meta_table <- gt(data.frame(
  predictor = pred,
  n = n, 
  estimate = round(estimate,3),
  Cinf = round(Cinf, 3), 
  Csup = round(Csup, 3), 
  p_val = round(p_val,3)) )

meta_table |>
  data_color(columns = contains('p_val'),
             method = 'bin',
             palette = 'lightblue', 
             bins = c(0, 0.05), 
             na_color = 'white')

#Multivariate
mods <- c("age", "prop_male", "prop_vo2max_pmax", "prop_h_rmax", "dur_day","fio2", 
          "session_freq", "tot_dur","cont_interval", "blinded","n" )

pred <- c()
n <- c()
estimate <- c()
Cinf <- c()
Csup <- c()
p_val <- c()

  for(mod in mods){
    tryCatch({
  m_reg <- rma.mv(yi = -yi, 
    V = vi, #vector of length with the corresponding sampling variances 
    data = subset(es, subname == 'LLTH'), 
    method = 'REML', 
    mods = reformulate(c(mod, 'athlele', 'vo2max_pre')),
    random = list(~1| studlab, ~1| id))
  
    pred <- c(pred, row.names(m_reg$beta)[2])  
    n <- c(n, rep(m_reg$k, length(m_reg$b[2])))
    estimate <- c(estimate, m_reg$b[2])
    Cinf <- c(Cinf, m_reg$ci.lb[2])
    Csup <- c(Csup, m_reg$ci.ub[2])
    p_val <- c(p_val, m_reg$pval[2])
    
    }, error = function(e){
    pred <- c(pred, mod) 
    n <- c(n, m_reg$k)
    estimate <- c(estimate, NA)
    Cinf <- c(Cinf, NA)
    Csup <- c(Csup, NA)
    p_val <- c(p_val, NA)
})
  }

meta_table <- gt(data.frame(
  predictor = pred,
  n = n, 
  estimate = round(estimate,3),
  Cinf = round(Cinf, 3), 
  Csup = round(Csup, 3), 
  p_val = round(p_val,3)))

meta_table |>
  data_color(columns = contains('p_val'),
             method = 'bin',
             palette = 'lightblue', 
             bins = c(0, 0.05), 
             na_color = 'white')

# ==============================================================================
# Section: Metaregression > By subgroup > LHTL
# ==============================================================================
#Univariate
mods <- c("subname","vo2max_pre", "athlele", "age", "prop_male", "prop_vo2max_pmax", 
          "prop_h_rmax", "dur_day","fio2", "session_freq", "tot_dur","cont_interval", "n" )

pred <- c()
n <- c()
estimate <- c()
Cinf <- c()
Csup <- c()
p_val <- c()

  for(mod in mods){
    tryCatch({
  m_reg <- rma.mv(yi = -yi, 
    V = vi, 
    data = subset(es, subname == 'LHTL'), 
    method = 'REML', 
    mods = as.formula(paste('~', mod)),
    random = list(~1| studlab, ~1| id))
  
    pred <- c(pred, row.names(m_reg$beta)[-1])  
    n <- c(n, rep(m_reg$k, length(m_reg$b[-1])))
    estimate <- c(estimate, m_reg$b[-1])
    Cinf <- c(Cinf, m_reg$ci.lb[-1])
    Csup <- c(Csup, m_reg$ci.ub[-1])
    p_val <- c(p_val, m_reg$pval[-1])
    
    }, error = function(e){
    pred <- c(pred, mod) 
    n <- c(n, m_reg$k)
    estimate <- c(estimate, NA)
    Cinf <- c(Cinf, NA)
    Csup <- c(Csup, NA)
    p_val <- c(p_val, NA)
})
  }

meta_table <- gt(data.frame(
  predictor = pred,
  n = n, 
  estimate = round(estimate,3),
  Cinf = round(Cinf, 3), 
  Csup = round(Csup, 3), 
  p_val = round(p_val,3)) )

meta_table |>
  data_color(columns = contains('p_val'),
             method = 'bin',
             palette = 'lightblue', 
             bins = c(0, 0.05), 
             na_color = 'white')

#Multivariate
mods <- c("age", "prop_male", "prop_vo2max_pmax", "prop_h_rmax", "dur_day","fio2", 
          "session_freq", "tot_dur", "cont_interval", "blinded","n" )

pred <- c()
n <- c()
estimate <- c()
Cinf <- c()
Csup <- c()
p_val <- c()

  for(mod in mods){
    tryCatch({
  m_reg <- rma.mv(yi = -yi, 
    V = vi, #vector of length with the corresponding sampling variances 
    data = subset(es, subname == 'LHTL'), 
    method = 'REML', 
    mods = reformulate(c(mod, 'athlele', 'vo2max_pre')),
    random = list(~1| studlab, ~1| id))
  
    pred <- c(pred, row.names(m_reg$beta)[2])  
    n <- c(n, rep(m_reg$k, length(m_reg$b[2])))
    estimate <- c(estimate, m_reg$b[2])
    Cinf <- c(Cinf, m_reg$ci.lb[2])
    Csup <- c(Csup, m_reg$ci.ub[2])
    p_val <- c(p_val, m_reg$pval[2])
    
    }, error = function(e){
    pred <- c(pred, mod) 
    n <- c(n, m_reg$k)
    estimate <- c(estimate, NA)
    Cinf <- c(Cinf, NA)
    Csup <- c(Csup, NA)
    p_val <- c(p_val, NA)
})
  }

meta_table <- gt(data.frame(
  predictor = pred,
  n = n, 
  estimate = round(estimate,3),
  Cinf = round(Cinf, 3), 
  Csup = round(Csup, 3), 
  p_val = round(p_val,3)))

meta_table |>
  data_color(columns = contains('p_val'),
             method = 'bin',
             palette = 'lightblue', 
             bins = c(0, 0.05), 
             na_color = 'white')

# ==============================================================================
# Section:  Metaregression > By subgroup > Passive HC
# ==============================================================================

#Univariate
mods <- c("subname","vo2max_pre", "athlele", "age", "prop_male", "prop_vo2max_pmax", 
          "prop_h_rmax", "dur_day","fio2", "session_freq", "tot_dur","cont_interval", "n" )

pred <- c()
n <- c()
estimate <- c()
Cinf <- c()
Csup <- c()
p_val <- c()

  for(mod in mods){
    tryCatch({
  m_reg <- rma.mv(yi = -yi, 
    V = vi, 
    data = subset(es, subname == 'Passive HC'), 
    method = 'REML', 
    mods = as.formula(paste('~', mod)),
    random = list(~1| studlab, ~1| id))
  
    pred <- c(pred, row.names(m_reg$beta)[-1])  
    n <- c(n, rep(m_reg$k, length(m_reg$b[-1])))
    estimate <- c(estimate, m_reg$b[-1])
    Cinf <- c(Cinf, m_reg$ci.lb[-1])
    Csup <- c(Csup, m_reg$ci.ub[-1])
    p_val <- c(p_val, m_reg$pval[-1])
    
    }, error = function(e){
    pred <- c(pred, mod) 
    n <- c(n, m_reg$k)
    estimate <- c(estimate, NA)
    Cinf <- c(Cinf, NA)
    Csup <- c(Csup, NA)
    p_val <- c(p_val, NA)
})
  }

meta_table <- gt(data.frame(
  predictor = pred,
  n = n, 
  estimate = round(estimate,3),
  Cinf = round(Cinf, 3), 
  Csup = round(Csup, 3), 
  p_val = round(p_val,3)) )

meta_table |>
  data_color(columns = contains('p_val'),
             method = 'bin',
             palette = 'lightblue', 
             bins = c(0, 0.05), 
             na_color = 'white')

#Multivariate
mods <- c("age", "prop_male", "prop_vo2max_pmax", "prop_h_rmax", "dur_day","fio2", 
          "session_freq", "tot_dur","cont_interval", "blinded","n" )


pred <- c()
n <- c()
estimate <- c()
Cinf <- c()
Csup <- c()
p_val <- c()

  for(mod in mods){
    tryCatch({
  m_reg <- rma.mv(yi = -yi, 
    V = vi, #vector of length with the corresponding sampling variances 
    data = subset(es, subname == 'Passive HC'), 
    method = 'REML', 
    mods = reformulate(c(mod, 'athlele', 'vo2max_pre')),
    random = list(~1| studlab, ~1| id))
  
    pred <- c(pred, row.names(m_reg$beta)[2])  
    n <- c(n, rep(m_reg$k, length(m_reg$b[2])))
    estimate <- c(estimate, m_reg$b[2])
    Cinf <- c(Cinf, m_reg$ci.lb[2])
    Csup <- c(Csup, m_reg$ci.ub[2])
    p_val <- c(p_val, m_reg$pval[2])
    
    }, error = function(e){
    pred <- c(pred, mod) 
    n <- c(n, m_reg$k)
    estimate <- c(estimate, NA)
    Cinf <- c(Cinf, NA)
    Csup <- c(Csup, NA)
    p_val <- c(p_val, NA)
})
  }

meta_table <- gt(data.frame(
  predictor = pred,
  n = n, 
  estimate = round(estimate,3),
  Cinf = round(Cinf, 3), 
  Csup = round(Csup, 3), 
  p_val = round(p_val,3)))

meta_table |>
  data_color(columns = contains('p_val'),
             method = 'bin',
             palette = 'lightblue', 
             bins = c(0, 0.05), 
             na_color = 'white')

# ==============================================================================
# Section: Funnel Plot
# ==============================================================================
resHT<-rma(data=es,
          yi= -yi, 
          vi= vi,
          ri= r_prepost,
          ni=n,
          method = "REML",
          control = list(maxiter = 1000, stepadj = 0.5))

restest <- regtest(resHT)

print(viz_funnel(resHT, 
           contours = TRUE, 
           trim_and_fill = TRUE, 
           egger = FALSE, 
           xlab = paste('', "\n", 
                        "Egger_test_pval=", 
                          restest$pval[1]),
           method="DL"))

# ==============================================================================
# Section: Infosession R
# ==============================================================================
sessionInfo()
