Learn R Programming

bivarhr (version 0.1.5)

run_sensemakr: Sensitivity Analysis to Unobserved Confounding (sensemakr)

Description

Performs the Cinelli & Hazlett style sensitivity analysis using sensemakr for two linear models:

  • I ~ trans_FC + t_norm + PopDensity + War

  • C ~ trans_FC + t_norm + PopDensity + War

treating trans_FC as the exposure of interest and using PopDensity and War as benchmark covariates.

Usage

run_sensemakr(DT)

Value

A list with components:

  • I: the sensemakr object for the model with outcome I.

  • C: the sensemakr object for the model with outcome C.

Arguments

DT

A data.frame or data.table containing at least the columns I, C, trans_FC, t_norm, PopDensity, and War.

Details

For each outcome (I and C), an OLS model is estimated and passed to sensemakr::sensemakr() with:

  • treatment = "trans_FC"

  • benchmark_covariates = c("PopDensity", "War")

The resulting sensemakr objects are summarized via summary(), converted to data frames, and written to CSV files:

  • "sensemakr_I_FC.csv" for outcome I.

  • "sensemakr_C_FC.csv" for outcome C.

The function assumes that a global character scalar dir_csv is defined and points to the directory where CSV outputs should be saved.

Examples

Run this code
# \donttest{
library(data.table)

# 1. Create dummy data with ALL columns required by the lm() formulas
DT <- data.table(
  I = rpois(30, lambda = 5),
  C = rpois(30, lambda = 3),
  trans_FC = sample(0:1, 30, replace = TRUE),   # Treatment
  t_norm = rnorm(30),                           # Trend/Time
  PopDensity = rnorm(30),                       # Benchmark Covariate
  War = sample(0:1, 30, replace = TRUE)         # Benchmark Covariate
)

# 2. Define global path using tempdir() (Fixes CRAN policy)
# run_sensemakr writes output to 'dir_csv', so it must be defined.
tmp_dir <- tempdir()
dir_csv <- file.path(tmp_dir, "csv")
if (!dir.exists(dir_csv)) dir.create(dir_csv, recursive = TRUE)

# 3. Run the function
# This requires the 'sensemakr' package to be installed.
res_sense <- run_sensemakr(DT)

# Inspect results
if (!is.null(res_sense$I)) {
  print(summary(res_sense$I))
}
# }

Run the code above in your browser using DataLab