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

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

birthwt_data <- data_birthwt |>
  mutate(
    race = factor(race, levels = c(1, 2, 3),
                  labels = c("White", "Black", "Other")),
    smoke = factor(smoke, levels = c(0, 1), labels = c("No", "Yes")),
    ht = factor(ht, levels = c(0, 1), labels = c("No", "Yes")),
    ui = factor(ui, levels = c(0, 1), labels = c("No", "Yes")),
    low = factor(low, levels = c(0, 1), labels = c("Normal BW", "Low BW")),
    ptl_cat = factor(ifelse(ptl > 0, "Yes", "No"), levels = c("No", "Yes"))
  )

attr(birthwt_data$age, "label") <- "Maternal age"
attr(birthwt_data$lwt, "label") <- "Maternal weight"
attr(birthwt_data$smoke, "label") <- "Smoking during pregnancy"
attr(birthwt_data$ht, "label") <- "Hypertension"
attr(birthwt_data$ui, "label") <- "Uterine irritability"
attr(birthwt_data$ptl_cat, "label") <- "Previous preterm labour"

## ----strata-desc, message=FALSE, warning=FALSE--------------------------------
strata_desc <- descriptive_table(
  data = birthwt_data,
  exposures = c("age", "lwt", "smoke", "ht", "ui", "ptl_cat"),
  by = race,
  percent = column,
  show_overall = last,
  theme = clinical
)

strata_desc$table

## ----strata-uni, message=FALSE, warning=FALSE---------------------------------
strata_uni <- stratified_uni_reg(
  data = birthwt_data,
  outcome = low,
  exposures = c("age", "lwt", "smoke", "ht", "ui", "ptl_cat"),
  stratifier = race,
  approach = logit,
  theme = clinical
)

strata_uni$table

## ----strata-multi-full, message=FALSE, warning=FALSE--------------------------
strata_full <- stratified_multi_reg(
  data = birthwt_data,
  outcome = low,
  exposures = c("age", "lwt", "smoke", "ht", "ui", "ptl_cat"),
  stratifier = race,
  approach = logit,
  theme = clinical
)

strata_full$table

## ----strata-multi, message=FALSE, warning=FALSE-------------------------------
strata_multi <- stratified_multi_reg(
  data = birthwt_data,
  outcome = low,
  exposures = c("smoke", "ht", "ui", "ptl_cat"),
  stratifier = race,
  adjust_for = c("age", "lwt"),
  approach = logit,
  theme = striped
)

strata_multi$table

## ----strata-forest, message=FALSE, warning=FALSE, fig.width=8, fig.height=7----
strata_forest_data <- forest_df(strata_multi)

forest_reg(
  strata_forest_data,
  ci_col_width = 18
)

