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

## ----load-data----------------------------------------------------------------
library(RegCalib)

data("main_data_sim", package = "RegCalib")
data("valid_data_sim", package = "RegCalib")

head(main_data_sim, 3)
head(valid_data_sim, 3)

## ----create-subset------------------------------------------------------------
set.seed(2026)

rows_by_outcome <- split(
  seq_len(nrow(main_data_sim)),
  main_data_sim$case
)

example_rows <- unlist(
  lapply(
    rows_by_outcome,
    function(rows) sample(rows, min(length(rows), 2500L))
  ),
  use.names = FALSE
)

main_example <- main_data_sim[example_rows, , drop = FALSE]

table(main_example$case)

## ----rc-df--------------------------------------------------------------------
rcdf <- RegCalibDF(
  supplyEstimates = FALSE,
  ms = main_example,
  vs = valid_data_sim,
  sur = c("fqtfatinc", "fqcalinc", "fqalcinc"),
  exp = c("drtfatinc", "drcalinc", "dralcinc"),
  covCalib = "agec",
  covOutcomePlus = NULL,
  outcome = "case",
  method = "glm",
  family = binomial,
  link = "logit",
  external = TRUE,
  pointEstimates = NA,
  vcovEstimates = NA
)

rcdf$correctedCoefTable

## ----rc-df-or-----------------------------------------------------------------
corrected_RC_DF <- exp(
  cbind(
    OR = rcdf$correctedCoefTable[, 1],
    "2.5 %" = rcdf$correctedCoefTable[, 5],
    "97.5 %" = rcdf$correctedCoefTable[, 6]
  )
)

corrected_RC_DF

## ----rc-sub-------------------------------------------------------------------
rcsub <- RegCalibSub(
  ms = main_example,
  vs = valid_data_sim,
  sur = c("fqtfatinc", "fqcalinc", "fqalcinc"),
  exp = c("drtfatinc", "drcalinc", "dralcinc"),
  covCalib = "agec",
  covOutcome = "agec",
  outcome = "case",
  method = "glm",
  family = binomial,
  link = "logit",
  external = TRUE
)

rcsub$correctedCoefTable

## ----rc-sub-or----------------------------------------------------------------
corrected_RC_SUB <- exp(
  cbind(
    OR = rcsub$correctedCoefTable[, 1],
    "2.5 %" = rcsub$correctedCoefTable[, 5],
    "97.5 %" = rcsub$correctedCoefTable[, 6]
  )
)

corrected_RC_SUB

## ----covariance---------------------------------------------------------------
rcdf$correctedVCOV
rcsub$correctedVCOV

