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

data("data_birthwt", package = "gtregression")
data("data_lungcancer", 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")),
    ftv_cat = factor(case_when(
      ftv == 0 ~ "None",
      ftv == 1 ~ "One",
      ftv >= 2 ~ "Two or more"
    ), levels = c("None", "One", "Two or more"))
  )

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

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"
attr(birthwt_data$ptl_cat, "label") <- "Previous preterm labour"
attr(birthwt_data$ftv_cat, "label") <- "First trimester visits"

birthwt_desc <- descriptive_table(
  birthwt_data,
  exposures = birthwt_exposures,
  by = "low",
  show_overall = "last"
)
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", "ptl_cat", "ftv_cat"),
  adjust_for = c("age", "lwt", "race"),
  approach = "logit"
)

lung_data <- data_lungcancer |>
  mutate(
    trt = factor(trt, levels = c(1, 2),
                 labels = c("Standard treatment", "Test treatment")),
    prior = factor(prior, levels = c(0, 10), labels = c("No", "Yes"))
  )

lung_surv <- surv_reg(
  data = lung_data,
  time = time,
  event = status,
  exposures = "trt",
  adjust_for = c("age", "karno"),
  distribution = weibull
)

## ----km-plot, message=FALSE, warning=FALSE------------------------------------
km_plot(
  data = lung_data,
  time = time,
  event = status,
  by = trt,
  break_time_by = 200,
  ylim = c(50, 100),
  title = "Kaplan-Meier Survival by Treatment"
)

## ----km-panel-plot, fig.width=7, fig.height=5, message=FALSE, warning=FALSE----
km_treatment <- km_plot(
  data = lung_data,
  time = time,
  event = status,
  by = trt,
  risk_table = FALSE,
  title = "A. Treatment",
  title_size = 10,
  title_face = plain,
  legend_position = bottom,
  base_size = 10
)

km_prior <- km_plot(
  data = lung_data,
  time = time,
  event = status,
  by = prior,
  risk_table = FALSE,
  title = "B. Prior therapy",
  title_size = 10,
  title_face = plain,
  legend_position = bottom,
  base_size = 10
)

patchwork::wrap_plots(km_treatment, km_prior, ncol = 2) +
  patchwork::plot_layout(guides = "collect") &
  ggplot2::theme(legend.position = "bottom")

## ----km-risk-table, message=FALSE, warning=FALSE------------------------------
km_risk_table(
  data = lung_data,
  time = time,
  event = status,
  by = trt,
  times = c(0, 90, 180, 365)
)

## ----rmst-table, message=FALSE, warning=FALSE---------------------------------
rmst_table(
  data = lung_data,
  time = time,
  event = status,
  by = trt,
  tau = 365
)

## ----survival-summary, message=FALSE, warning=FALSE---------------------------
survival_summary(
  data = lung_data,
  time = time,
  event = status,
  by = trt
)

## ----survival-quantiles, message=FALSE, warning=FALSE-------------------------
survival_quantiles(
  data = lung_data,
  time = time,
  event = status,
  by = trt
)

## ----survival-prob, message=FALSE, warning=FALSE------------------------------
survival_prob(
  data = lung_data,
  time = time,
  event = status,
  by = trt,
  times = c(90, 180, 365)
)

## ----logrank-test, message=FALSE, warning=FALSE-------------------------------
logrank_test(
  data = lung_data,
  time = time,
  event = status,
  by = trt
)

## ----surv-model-compare, message=FALSE, warning=FALSE-------------------------
surv_model_compare(
  data = lung_data,
  time = time,
  event = status,
  exposures = c("trt", "celltype"),
  adjust_for = c("age", "karno")
)

## ----plot-surv-fit, fig.width=7, fig.height=5, message=FALSE, warning=FALSE----
plot_surv_fit(
  data = lung_data,
  time = time,
  event = status,
  by = trt,
  distributions = c(weibull, lognormal),
  break_time_by = 200
)

## ----plot-surv-fit-adjusted, fig.width=7, fig.height=5, message=FALSE, warning=FALSE----
plot_surv_fit(
  data = lung_data,
  time = time,
  event = status,
  by = trt,
  adjust_for = c(age, karno),
  distributions = loglogistic,
  xlim = c(0, 800)
)

## ----surv-predict, fig.width=7, fig.height=5, message=FALSE, warning=FALSE----
surv_predict(
  model = lung_surv$models$trt,
  newdata = data.frame(
    trt = factor("Test treatment", levels = levels(lung_data$trt)),
    age = 60,
    karno = 70
  ),
  times = c(90, 180, 365)
)

## ----one-plot, fig.width=7, fig.height=5, message=FALSE, warning=FALSE--------
plot_reg(
  birthwt_uni,
  title = "Crude Associations With Low Birth Weight"
)

## ----plot-style, fig.width=7, fig.height=5, message=FALSE, warning=FALSE------
plot_reg(
  birthwt_uni,
  show_ref = FALSE,
  point_size = 3.5,
  point_stroke = 0.7,
  ci_linewidth = 0.75,
  base_size = 13,
  title = "Crude Associations With Low Birth Weight"
)

## ----adjusted-plot, fig.width=10, fig.height=5, message=FALSE, warning=FALSE----
plot_reg(
  birthwt_multi,
  show_ref = FALSE,
  log_x = TRUE,
  title = "Adjusted Associations With Low Birth Weight"
)

## ----compact-binary-plot, fig.width=10, fig.height=7, message=FALSE, warning=FALSE----
plot_reg(
  birthwt_uni,
  show_ref = FALSE,
  title = "Crude Associations With Reference Rows Hidden"
)

## ----log-axis-plot,fig.width=10, fig.height=7, message=FALSE, warning=FALSE----
plot_reg(
  birthwt_uni,
  log_x = TRUE,
  title = "Crude Associations on a Log Scale"
)

## ----custom-axis-plot, fig.width=10, fig.height=7, message=FALSE, warning=FALSE----
plot_reg(
  birthwt_uni,
  show_ref = FALSE,
  log_x = TRUE,
  xlim = c(0.25, 12),
  breaks = c(0.5, 1, 2, 4, 8),
  title = "Crude Associations With Custom Axis"
)

## ----combined-plot, fig.width=12, fig.height=7, message=FALSE, warning=FALSE----
plot_reg_combine(
  tbl_uni = birthwt_uni,
  tbl_multi = birthwt_multi,
  show_ref = FALSE,
  log_x = TRUE,
  xlim_uni = c(0.25, 12),
  breaks_uni = c(0.5, 1, 2, 4, 8),
  xlim_multi = c(0.25, 16),
  breaks_multi = c(0.5, 1, 2, 4, 8),
  title_uni = "Crude Effects",
  title_multi = "Adjusted Effects"
)

## ----forest-table, fig.width=13, fig.height=10, message=FALSE, warning=FALSE----
forest_data <- forest_df(
  uni = birthwt_uni,
  multi = birthwt_multi,
  desc = birthwt_desc
)
forest_reg(forest_data)

## ----forest-axis-control, fig.width=13, fig.height=10,message=FALSE, warning=FALSE----
forest_reg(
  forest_data,
  xlim = list(c(0.25, 8), c(0.8, 25)),
  ticks_at = list(
    c(0.5, 1, 2, 4, 8),
    c(1, 2, 4, 8, 16)
  ),
  quiet = TRUE
)

## ----forest-ci-width, fig.width=13, fig.height=10, message=FALSE, warning=FALSE----
forest_reg(
  forest_data,
  ci_col_width = c(18, 22),
  xlim = list(c(0.25, 8), c(0.8, 25)),
  ticks_at = list(
    c(0.5, 1, 2, 4, 8),
    c(1, 2, 4, 8, 16)
  )
)

## ----forest-one-call, fig.width=13, fig.height=10,message=FALSE, warning=FALSE----
forest_reg(
  uni = birthwt_uni,
  multi = birthwt_multi,
  desc = birthwt_desc,
  side = "left"
)

