## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----package------------------------------------------------------------------
library(mars)

## ----univariate-fit-----------------------------------------------------------
fit_uni <- mars(
  data = bcg,
  formula = yi ~ 1,
  studyID = "trial",
  variance = "vi",
  varcov_type = "univariate",
  structure = "univariate",
  estimation_method = "MLE"
)

summary(fit_uni)

## ----univariate-moderator-----------------------------------------------------
fit_uni_mod <- mars(
  data = bcg,
  formula = yi ~ 1 + ablat,
  studyID = "trial",
  variance = "vi",
  varcov_type = "univariate",
  structure = "univariate",
  estimation_method = "MLE"
)

summary(fit_uni_mod)

## ----univariate-wald-inference------------------------------------------------
summary(fit_uni_mod, inference = "model", test = "z", level = 0.90)
summary(fit_uni_mod, inference = "model", test = "t", level = 0.90)

## ----univariate-hksj-inference------------------------------------------------
summary(fit_uni_mod, inference = "model", test = "hksj",
        hksj_adjust = "standard")
summary(fit_uni_mod, inference = "model", test = "hksj",
        hksj_adjust = "modified")

# Use the same settings to extract matching coefficient intervals.
confint(fit_uni_mod, inference = "model", test = "hksj",
        hksj_adjust = "modified")

## ----univariate-satterthwaite-inference---------------------------------------
summary(fit_uni_mod, inference = "model", test = "Satterthwaite")

## ----multivariate-fit---------------------------------------------------------
fit_mv <- mars(
  data = becker09,
  studyID = "ID",
  effectID = "numID",
  sample_size = "N",
  effectsize_type = "cor",
  varcov_type = "weighted",
  variable_names = c(
    "Cognitive_Performance",
    "Somatic_Performance",
    "Selfconfidence_Performance",
    "Somatic_Cognitive",
    "Selfconfidence_Cognitive",
    "Selfconfidence_Somatic"
  )
)

summary(fit_mv)

## ----multivariate-robust-inference--------------------------------------------
summary(fit_mv, inference = "robust", cluster = fit_mv$study_id,
        robust_type = "CR2", test = "Satterthwaite")

## ----multilevel-fit-----------------------------------------------------------
fit_ml <- mars(
  data = school,
  formula = effect ~ 1 + (1 | district/study),
  studyID = "district",
  variance = "var",
  varcov_type = "multilevel",
  structure = "multilevel",
  estimation_method = "MLE"
)

summary(fit_ml)

## ----multilevel-moderator-----------------------------------------------------
fit_ml_mod <- mars(
  data = school,
  formula = effect ~ 1 + year + (1 | district/study),
  studyID = "district",
  variance = "var",
  varcov_type = "multilevel",
  structure = "multilevel",
  estimation_method = "MLE"
)

summary(fit_ml_mod)

## ----multilevel-inference-----------------------------------------------------
summary(fit_ml, inference = "model", test = "Satterthwaite")
summary(fit_ml_mod, inference = "robust", cluster = "district",
        robust_type = "CR2", test = "Satterthwaite", level = 0.90)

