Skip to content

etverse/negatr

Repository files navigation

negatr: Negative Control Analyses for Epidemiological Research

Lifecycle: experimental R-CMD-check Codecov test coverage CRAN status Buy Me A Coffee

Overview

negatr implements negative control methods for detecting and correcting unmeasured confounding in epidemiological studies. The package provides a comprehensive framework for:

  • Bias Detection: Test for unmeasured confounding using negative control exposures (NCE) and negative control outcomes (NCO)
  • Bias Correction: Apply difference-in-differences and calibration methods
  • Assumption Validation: Check exclusion restrictions, U-comparability, positivity, and completeness
  • DAG Integration: Specify and validate causal structures with dagitty
  • Flexible Modeling: Support for GLM, GAM, survival models, and custom fitting functions

Installation

# Install from GitHub
# install.packages("remotes")
remotes::install_github("etverse/negatr")

Quick Start

library(negatr)

# Load example data
data(nc_example_data)

# Specify negative control analysis
spec <- nc_specify(
  data = nc_example_data,
  exposure = "exposure",
  outcome = "outcome",
  nce = "nce_future",      # Future exposure as NCE
  nco = "nco_prior",       # Prior outcome as NCO
  confounders = c("age", "sex", "ses")
)

# Fit models
fit <- nc_fit(spec)

# Detect bias
bias_test <- nc_detect_bias(fit)
print(bias_test)

# Correct for bias
corrected <- nc_correct_bias(fit, method = "did")
print(corrected)

# Visualize
plot(fit, type = "effects")
summary(fit)

Core Workflow

  1. Specify Analysis: Define exposure, outcome, negative controls, and confounders with nc_specify()
  2. Fit Models: Estimate associations with nc_fit() using flexible model specifications
  3. Detect Bias: Test negative control assumptions with nc_detect_bias()
  4. Correct Bias: Apply correction methods with nc_correct_bias()
  5. Validate: Check assumptions with nc_validate_assumptions() and nc_dag()

Key Features

Negative Control Types

Negative Control Exposures (NCE):

  • Should NOT causally affect the outcome
  • Should share unmeasured confounders with primary exposure
  • Examples: future exposure levels, paternal exposures, spatially distant exposures

Negative Control Outcomes (NCO):

  • Should NOT be causally affected by the exposure
  • Should share unmeasured confounders with primary outcome
  • Examples: pre-exposure outcomes, analogous outcomes from different mechanisms

Bias Correction Methods

  • Difference-in-Differences (DiD): Uses NCO to estimate and subtract bias
  • Calibration: Adjusts p-values and confidence intervals using NC test statistics

Flexible Modeling

# GLM with binary outcome
spec <- nc_specify(data, exposure, outcome, nce, nco, confounders)
fit <- nc_fit(spec, family = binomial())

# GAM with smooth terms
library(mgcv)
fit_gam <- nc_fit(
  spec,
  model_function = mgcv::gam,
  formula_primary = outcome ~ s(exposure) + s(age) + sex
)

# Survival analysis
library(survival)
fit_cox <- nc_fit(
  spec,
  model_function = survival::coxph
)

DAG-Based Validation

# Create causal DAG
dag <- nc_dag(
  exposure = "smoking",
  outcome = "lung_cancer",
  nce = "future_smoking",
  nco = "prior_injury",
  confounders = c("age", "ses"),
  unmeasured = "genetic_susceptibility"
)

# Visualize
plot(dag)

# Check assumptions
nc_dag_check(dag)

Methodological Foundation

Negative control methods detect and correct unmeasured confounding by leveraging variables with known causal relationships:

  • Exclusion Restriction: NCE doesn’t cause outcome; exposure doesn’t cause NCO
  • U-Comparability: Negative controls share unmeasured confounders with primary variables
  • Completeness: NCE and NCO are associated (for double NC designs)

If negative control associations are significant, this indicates either:

  1. Assumption violation (invalid negative control)
  2. Unmeasured confounding affecting the primary analysis

With valid negative controls, significant associations provide evidence of bias that can be quantified and corrected.

References

Lipsitch M, Tchetgen Tchetgen E, Cohen T (2010). “Negative controls: a tool for detecting confounding and bias in observational studies.” Epidemiology, 21(3), 383-388.

Shi X, Miao W, Tchetgen Tchetgen E (2020). “A selective review of negative control methods in epidemiology.” Current Epidemiology Reports, 7, 190-202.

Tchetgen Tchetgen EJ, et al. (2020). “An introduction to proximal causal learning.” arXiv preprint arXiv:2009.10982.

Getting Help

  • Documentation: ?nc_specify, ?nc_fit, ?nc_detect_bias, ?nc_correct_bias
  • Vignettes: vignette("negatr-methodology"), vignette("negatr-workflow")
  • Issues: GitHub Issues

License

MIT License - see LICENSE file for details

About

R package for negative control analysis.

Topics

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

  •  

Packages

No packages published