## ----export-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"))
  )

birthwt_exposures <- c("age", "lwt", "race", "smoke", "ht", "ui")

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

birthwt_desc <- descriptive_table(
  birthwt_data,
  exposures = birthwt_exposures,
  by = low
)
birthwt_uni <- uni_reg(
  birthwt_data,
  outcome = low,
  exposures = birthwt_exposures,
  approach = logit
)
birthwt_multi <- multi_reg(
  birthwt_data,
  outcome = low,
  exposures = c("smoke", "ht", "ui"),
  adjust_for = c("age", "lwt", "race"),
  approach = logit
)

## ----modify-table, message=FALSE, warning=FALSE-------------------------------
birthwt_custom <- modify_table(
  birthwt_multi,
  variable_labels = c(
    smoke = "Smoked during pregnancy",
    ht = "History of hypertension",
    ui = "Uterine irritability"
  ),
  level_labels = list(
    smoke = c(Yes = "Smoker"),
    ht = c(Yes = "Hypertensive")
  ),
  header_labels = c(estimate = "Adjusted OR", p.value = "P"),
  caption = "Adjusted regression for low birth weight",
  caveat = "Adjusted for maternal age, maternal weight, and maternal race."
)

birthwt_custom

## ----merge-tables, message=FALSE, warning=FALSE-------------------------------
birthwt_merged <- merge_tables(
  birthwt_desc,
  birthwt_uni,
  birthwt_multi,
  spanners = c("Descriptive", "Crude", "Adjusted")
)

birthwt_merged$table

## ----merged-polish, message=FALSE, warning=FALSE------------------------------
birthwt_merged_paper <- modify_table(
  birthwt_merged,
  variable_labels = c(
    age = "Maternal age",
    lwt = "Maternal weight",
    race = "Maternal race",
    smoke = "Smoking during pregnancy",
    ht = "Hypertension",
    ui = "Uterine irritability"
  ),
  caption = "Clinical profile and regression estimates for low birth weight",
  caveat = "Adjusted estimates are adjusted for maternal age, maternal weight, and maternal race."
)

birthwt_merged_paper$table

## ----save-examples, eval=FALSE------------------------------------------------
# table_path <- save_table(
#   birthwt_merged_paper,
#   filename = "birthwt-table",
#   format = html
# )
# 
# birthwt_plot <- plot_reg(
#   birthwt_multi,
#   title = "Adjusted Regression for Low Birth Weight"
# )
# 
# plot_path <- save_plot(
#   birthwt_plot,
#   filename = "birthwt-forest",
#   format = png
# )

## ----word-example, eval=FALSE-------------------------------------------------
# birthwt_multi_ft <- multi_reg(
#   birthwt_data,
#   outcome = low,
#   exposures = c("smoke", "ht", "ui"),
#   adjust_for = c("age", "lwt", "race"),
#   approach = logit,
#   format = flextable
# )
# 
# docx_path <- save_docx(
#   tables = list(birthwt_multi_ft),
#   filename = "birthwt-report",
#   titles = "Adjusted Regression",
#   table_width = 6.5
# )

