library(metapsyData)
library(metapsyTools)
library(readr)

d <- getData("gad-psyctr", version = "23.0.2")
dat <- d$data

# Convert variable format
dat$percent_women = dat$percent_women %>%
  parse_number() %>% round()
dat$mean_age = dat$mean_age %>%
  parse_number() %>% round(1)

# Run combined MA for GAD symptoms
filterPoolingData(
  dat, outcome_domain  == "GAD") %>%
  runMetaAnalysis(which.run = "combined",
                  rho.within.study = 0.5,
                  which.combine = "arms") %>%
  { .$model.combined$data$.TE = .$model.combined$data$.TE*-1;
    .$model.combined$data$N =
    .$model.combined$data$totaln_arm1 +
    .$model.combined$data$totaln_arm2;
  .$model.combined$data} -> gad

# Run combined MA for anxiety symptoms
filterPoolingData(
  dat, outcome_domain  == "gen anx") %>%
  runMetaAnalysis(which.run = "combined",
                  rho.within.study = 0.5,
                  which.combine = "arms") %>%
  { .$model.combined$data$.TE = .$model.combined$data$.TE*-1;
    .$model.combined$data$N =
    .$model.combined$data$totaln_arm1 +
    .$model.combined$data$totaln_arm2;
  .$model.combined$data} -> anx


# Run combined MA for worrying
filterPoolingData(
  dat, outcome_domain == "worry") %>%
  runMetaAnalysis(which.run = "combined",
                  rho.within.study = 0.5,
                  which.combine = "arms") %>%
  { .$model.combined$data$.TE = .$model.combined$data$.TE*-1;
    .$model.combined$data$N =
   .$model.combined$data$totaln_arm1 +
   .$model.combined$data$totaln_arm2;
   .$model.combined$data} -> worry

# Combine and save
list(gad = gad,
     anx = anx,
     worry = worry,
     metadata = d$returnMetadata()) -> GadDB

save(GadDB, file="www/data/GadDB.rda")