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

## ----install------------------------------------------------------------------
# # From CRAN (recommended)
# install.packages("ukbflow")
# 
# # Latest development version from GitHub
# pak::pkg_install("evanbio/ukbflow")

## ----load-data----------------------------------------------------------------
# library(ukbflow)
# 
# df <- ops_toy()   # synthetic UKB-like cohort, no RAP connection needed
# 
# # On RAP, replace with:
# # auth_login()
# # auth_select_project("project-XXXXXXXXXXXX")
# # df <- extract_pheno(c(31, 21022, 53, 20116)) |>
# #   decode_values() |>
# #   decode_names()

## ----derive-------------------------------------------------------------------
# df <- df |>
#   derive_missing() |>                                               # recode "Prefer not to answer" → NA
#   derive_selfreport(name = "t2dm", regex = "diabetes",           # T2DM self-report
#                     field = "noncancer") |>
#   derive_icd10(name = "t2dm", icd10 = "E11", source = "hes") |> # T2DM from HES
#   derive_case(name = "t2dm") |>                                  # → t2dm_status, t2dm_date
#   derive_followup(name         = "t2dm",
#                   event_col    = "t2dm_date",
#                   baseline_col = "p53_i0",                          # assessment centre date
#                   censor_date  = as.Date("2022-06-01"))

## ----assoc--------------------------------------------------------------------
# res <- assoc_coxph(
#   data         = df,
#   outcome_coll  = "t2dm_status",
#   time_col     = "t2dm_followup_years",
#   exposure_col = "p21001_i0",   # BMI (continuous)
#   covariates   = c("p21022",    # age_at_recruitment
#                    "p31")       # sex
# )

## ----plot---------------------------------------------------------------------
# # Forest plot — see vignette("plot") for full usage
# res_df <- as.data.frame(res)
# plot_forest(
#   data      = res_df,
#   est       = res_df$HR,
#   lower     = res_df$CI_lower,
#   upper     = res_df$CI_upper,
#   ci_column = 7L   # res_df has 6 cols before HR; CI graphic goes here
# )
# 
# # Table 1
# plot_tableone(
#   data   = as.data.frame(df),
#   vars   = c("p21022",     # age_at_recruitment
#              "p31",        # sex
#              "p21001_i0"), # bmi
#   strata = "t2dm_status"
# )

