| Title: | Survey-Weighted Modeling Utilities |
|---|---|
| Description: | Utility functions for survey-weighted regression, diagnostics, and visualization. |
| Authors: | Shakeel Ahmed [aut, cre] |
| Maintainer: | Shakeel Ahmed <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-06-02 09:36:48 UTC |
| Source: | https://github.com/cran/svyCausalGLM |
Fits a survey-weighted logistic regression model using stabilized prognostic score weights derived from a model predicting the outcome conditional on baseline covariates and excluding the exposure effect. The function supports design-based inference under complex survey sampling while adjusting for confounding through prognostic weighting
final_prog_svyglm( data, dep_var, exposure, covariates, id_var, strata_var, weight_var, outcome_covariates = NULL, level = 0.95, ... )final_prog_svyglm( data, dep_var, exposure, covariates, id_var, strata_var, weight_var, outcome_covariates = NULL, level = 0.95, ... )
data |
Data frame |
dep_var |
Character; binary outcome |
exposure |
Character; exposure variable |
covariates |
Character vector; adjustment variables |
id_var |
Character; PSU |
strata_var |
Character; strata |
weight_var |
Character; survey weight |
outcome_covariates |
Character vector; optional covariates for final model |
level |
Numeric; CI level |
... |
Additional args to svyglm |
A list with:
prog_model: Prognostic svyglm.
final_model: Weighted outcome svyglm.
OR_table: Odds ratios with CI.
AUC: Weighted AUC.
data: Data with prognostic weights.
set.seed(123) n <- 1000 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 = rbinom(n, 1, 0.5) ) dat$outcome <- rbinom(n, 1, plogis(-2 + 0.03*dat$age + 0.5*dat$exposure)) fit<-final_prog_svyglm(data = dat, dep_var = "outcome", exposure="exposure", covariates = c("age", "sex"), id_var = "psu", strata_var = "strata", weight_var = "weight", level = 0.95 ) names(fit) fit$OR_tableset.seed(123) n <- 1000 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 = rbinom(n, 1, 0.5) ) dat$outcome <- rbinom(n, 1, plogis(-2 + 0.03*dat$age + 0.5*dat$exposure)) fit<-final_prog_svyglm(data = dat, dep_var = "outcome", exposure="exposure", covariates = c("age", "sex"), id_var = "psu", strata_var = "strata", weight_var = "weight", level = 0.95 ) names(fit) fit$OR_table
Calculates IPTW weights and fits survey-weighted GLM. Supports binary, multinomial, or continuous exposures.
final_prop_svyglm( data, dep_var, covariates, exposure, id_var, strata_var, weight_var, exposure_type = "binary", outcome_covariates = NULL, level = 0.95, ... )final_prop_svyglm( data, dep_var, covariates, exposure, id_var, strata_var, weight_var, exposure_type = "binary", outcome_covariates = NULL, level = 0.95, ... )
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 |
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.
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_tableset.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
Fits a survey-weighted logistic regression model (quasibinomial) using raw survey variables. Returns ORs, confidence intervals, p-values, and model discrimination statistics.
final_svyglm( data, dep_var, covariates, id_var, strata_var, weight_var, family = "binomial", level = 0.95, interaction_terms = NULL )final_svyglm( data, dep_var, covariates, id_var, strata_var, weight_var, family = "binomial", level = 0.95, interaction_terms = NULL )
data |
A data frame containing the survey data. |
dep_var |
Character. Name of the binary outcome variable (0/1). |
covariates |
Character vector of covariate names to adjust for. |
id_var |
Character. Name of the primary sampling unit variable. |
strata_var |
Character. Name of the stratification variable. |
weight_var |
Character. Name of the survey weight variable. |
family |
Character. Currently supports only |
level |
Numeric. Confidence level for intervals (default = 0.95). |
interaction_terms |
Optional character vector of interaction terms. |
A list containing:
model: Survey-weighted logistic regression model.
results_table: Odds ratios with confidence intervals and p-values.
AUC: Survey-weighted AUC (Somers' C).
data: Input data with predicted probabilities.
design: Survey design object.
set.seed(123) n <- 100 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 = rbinom(n, 1, 0.5) ) dat$outcome <- rbinom(n, 1, plogis(-2 + 0.03*dat$age + 0.5*dat$exposure)) fit_simple<-final_svyglm(dat, dep_var="outcome", covariates=c("age","sex"), id_var="psu", strata_var="strata", weight_var="weight") fit_simple$OR_tableset.seed(123) n <- 100 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 = rbinom(n, 1, 0.5) ) dat$outcome <- rbinom(n, 1, plogis(-2 + 0.03*dat$age + 0.5*dat$exposure)) fit_simple<-final_svyglm(dat, dep_var="outcome", covariates=c("age","sex"), id_var="psu", strata_var="strata", weight_var="weight") fit_simple$OR_table
Print method for svyCausal objects
## S3 method for class 'svyCausal' print(x, ...)## S3 method for class 'svyCausal' print(x, ...)
x |
An object of class |
... |
Additional arguments passed to |
The object x, invisibly.
Produces a weighted ROC curve and reports weighted AUC for survey-based models.
viz_auc_svyglm( fit_object, title = "Weighted ROC Curve", line_color = "#0072B2" )viz_auc_svyglm( fit_object, title = "Weighted ROC Curve", line_color = "#0072B2" )
fit_object |
object obtain from logistic regression |
title |
Character. Plot title. |
line_color |
Character. ROC curve color. |
AUC is computed using, consistent with complex survey weighting.
A ggplot object.
set.seed(123) n <- 100 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 = rbinom(n, 1, 0.5) ) dat$outcome <- rbinom(n, 1, plogis(-2 + 0.03*dat$age + 0.5*dat$exposure)) fit_example<-final_svyglm(dat, dep_var="outcome", covariates=c("age","sex"), id_var="psu", strata_var="strata", weight_var="weight") viz_auc_svyglm(fit_object=fit_example)set.seed(123) n <- 100 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 = rbinom(n, 1, 0.5) ) dat$outcome <- rbinom(n, 1, plogis(-2 + 0.03*dat$age + 0.5*dat$exposure)) fit_example<-final_svyglm(dat, dep_var="outcome", covariates=c("age","sex"), id_var="psu", strata_var="strata", weight_var="weight") viz_auc_svyglm(fit_object=fit_example)