final_prop_svyglm: Propensity-weighted survey GLM

View source: R/final_prop_svyglm.R

final_prop_svyglmR Documentation

Propensity-weighted survey GLM

Description

Calculates IPTW weights and fits survey-weighted GLM. Supports binary, multinomial, or continuous exposures.

Usage

final_prop_svyglm(
  data,
  dep_var,
  covariates,
  exposure,
  id_var,
  strata_var,
  weight_var,
  exposure_type = "binary",
  outcome_covariates = NULL,
  level = 0.95,
  ...
)

Arguments

data

Data frame

dep_var

Character; binary outcome

covariates

Character vector; adjustment variables

exposure

Character; treatment/exposure variable

id_var

Character; PSU

strata_var

Character; strata

weight_var

Character; base weight

exposure_type

Character; "binary", "multinomial", "continuous"

outcome_covariates

Character vector of additional covariates to include in the final outcome model after applying propensity weights (default = NULL)

level

Numeric; confidence interval level

...

Additional args to svyglm

Value

A list with:

  • ps_model: Propensity score svyglm model.

  • final_model: Weighted outcome svyglm model.

  • OR_table: Odds ratios with CI and p-values.

  • AUC: Weighted AUC.

  • data: Data with IPTW and predictions.

Examples

set.seed(123)
n <- 1500
dat <- data.frame(
  psu = sample(1:10, n, replace = TRUE),
  strata = sample(1:5, n, replace = TRUE),
  weight = runif(n, 0.5, 2),
  age = rnorm(n, 50, 10),
  sex = factor(sample(c("Male", "Female"), n, replace = TRUE)),
  exposure_bin = rbinom(n, 1, 0.5)
)
dat$outcome <- rbinom(n, 1, plogis(-2 + 0.03*dat$age + 0.5*dat$exposure_bin))
## ---- Example 1: Binary exposure ----
fit_bin_exp<-final_prop_svyglm(dat, dep_var="outcome",
                  covariates=c("age","sex"),
                  exposure="exposure_bin",
                  id_var="psu", strata_var="strata",
                  weight_var="weight", outcome_covariates = NULL)
fit_bin_exp$OR_table
## ---- Example 2: Continuous exposure ----
fit_cont_exp <- final_prop_svyglm(
  dat,
  dep_var     = "outcome",
  covariates  = c("sex"),
  exposure    = "age",
  id_var      = "psu",
  strata_var  = "strata",
  weight_var  = "weight",
 exposure_type = "continuous",
 outcome_covariates = NULL)
fit_cont_exp$OR_table
#### ---- Example 1: Multinomial exposure ----
dat$exposure_3cat <- cut(dat$age,
breaks = quantile(dat$age, probs = c(0, 1/3, 2/3, 1)),  # tertiles
 labels = c("Low", "Medium", "High"),
include.lowest = TRUE)
# Numeric coding for exposure effect
exp_eff <- ifelse(dat$exposure_3cat == "Low", 0,
                ifelse(dat$exposure_3cat == "Medium", 0.6, 1.2))
dat$outcome <- rbinom(n,1,plogis(-3 +0.02 * dat$age +0.4  * (dat$sex == "Male") +exp_eff))
fit_multi_cat <- final_prop_svyglm(dat, dep_var     = "outcome",
covariates  = c("age", "sex"), exposure    = "exposure_3cat",
id_var      = "psu", strata_var  = "strata", weight_var  = "weight",
exposure_type = "multinomial",
outcome_covariates = NULL)
fit_multi_cat$OR_table

svyCausalGLM documentation built on March 3, 2026, 5:08 p.m.