## ----setup, message=FALSE, warning=FALSE--------------------------------------
library(gtregression)
library(dplyr)

data("data_diabetes_mediation", package = "gtregression")

dissect(data_diabetes_mediation)

## ----mediation-logit, message=FALSE, warning=FALSE----------------------------
diabetes_med <- mediation_analysis(
  data = data_diabetes_mediation,
  exposure = obesity,
  mediator = glucose,
  outcome = diabetes,
  covariates = c(age, blood_pressure, pregnancies, diabetes_pedigree),
  outcome_approach = logit,
  sims = 100,
  seed = 123
)

diabetes_med

## ----inspect-object-----------------------------------------------------------
diabetes_med$table_body
diabetes_med$values
diabetes_med$models$mediator
diabetes_med$models$outcome
head(diabetes_med$boot)

## ----mediation-plot, fig.width=7, fig.height=5--------------------------------
plot_mediation(diabetes_med)

## ----mediation-plot-no-estimates, fig.width=7, fig.height=5-------------------
plot_mediation(diabetes_med, show_estimates = FALSE)

## ----mediation-quoted, message=FALSE, warning=FALSE---------------------------
exposure_var <- "obesity"
mediator_var <- "glucose"
outcome_var <- "diabetes"
covariate_vars <- c(
  "age", "blood_pressure", "pregnancies", "diabetes_pedigree"
)

med_quoted <- mediation_analysis(
  data = data_diabetes_mediation,
  exposure = exposure_var,
  mediator = mediator_var,
  outcome = outcome_var,
  covariates = covariate_vars,
  outcome_approach = "logit",
  sims = 100,
  seed = 456
)

med_quoted

## ----mediation-linear, message=FALSE, warning=FALSE---------------------------
med_linear <- mediation_analysis(
  data = data_diabetes_mediation,
  exposure = obesity,
  mediator = glucose,
  outcome = bmi,
  covariates = c(age, blood_pressure, pregnancies, diabetes_pedigree),
  outcome_approach = linear,
  sims = 100,
  seed = 789
)

med_linear

## ----mediation-linear-plot, fig.width=7, fig.height=5-------------------------
plot_mediation(med_linear)

