library(metapsyData)
library(metapsyTools)

d <- getData("peer-support", version = "23.0.3")
dat <- d$data

# Run combined MA for clinical outcome data
filterPoolingData(
  dat, outcome_category  == "Clinical recovery") %>%
  runMetaAnalysis(which.run = "combined",
                  rho.within.study = 0.5,
                  which.combine = "arms") %>%
  {.$model.combined$data$N =
    .$model.combined$data$totaln_arm1 +
    .$model.combined$data$totaln_arm2;
  .$model.combined$data} -> clinical

# Run combined MA for functional outcome data
filterPoolingData(
  dat, outcome_category  == "Functional recovery") %>%
  runMetaAnalysis(which.run = "combined",
                  rho.within.study = 0.5,
                  which.combine = "arms") %>%
  {.$model.combined$data$N =
    .$model.combined$data$totaln_arm1 +
    .$model.combined$data$totaln_arm2;
  .$model.combined$data} -> functional


# Run combined MA for personal outcome data
filterPoolingData(
  dat, outcome_category == "Personal recovery") %>%
  runMetaAnalysis(which.run = "combined",
                  rho.within.study = 0.5,
                  which.combine = "arms") %>%
  {.$model.combined$data$N =
   .$model.combined$data$totaln_arm1 +
   .$model.combined$data$totaln_arm2;
   .$model.combined$data} -> personal

# Combine and save
list(clinical = clinical,
     functional = functional,
     personal = personal,
     metadata = d$returnMetadata()) -> PeerSupportDB

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