Nothing
#' Sensitivity Analysis to Unobserved Confounding (sensemakr)
#'
#' Performs the Cinelli & Hazlett style sensitivity analysis using
#' \pkg{sensemakr} for two linear models:
#' \itemize{
#' \item \code{I ~ trans_FC + t_norm + PopDensity + War}
#' \item \code{C ~ trans_FC + t_norm + PopDensity + War}
#' }
#' treating \code{trans_FC} as the exposure of interest and using
#' \code{PopDensity} and \code{War} as benchmark covariates.
#'
#' @param DT A \code{data.frame} or \code{data.table} containing at least
#' the columns \code{I}, \code{C}, \code{trans_FC}, \code{t_norm},
#' \code{PopDensity}, and \code{War}.
#' @param dir_csv Character scalar or \code{NULL}; directory where the
#' sensitivity summaries (\code{"sensemakr_I_FC.csv"},
#' \code{"sensemakr_C_FC.csv"}) are written. If \code{NULL} (default),
#' nothing is written to disk.
#'
#' @details
#' This function requires the \pkg{sensemakr} package (listed under
#' \code{Suggests}); an informative error is raised at call time if it is
#' not installed.
#' For each outcome (\code{I} and \code{C}), an OLS model is estimated and
#' passed to \code{sensemakr::sensemakr()} with:
#' \itemize{
#' \item \code{treatment = "trans_FC"}
#' \item \code{benchmark_covariates = c("PopDensity", "War")}
#' }
#' The resulting \code{sensemakr} objects are summarized via
#' \code{summary()}, converted to data frames, and written to CSV files:
#' \itemize{
#' \item \code{"sensemakr_I_FC.csv"} for outcome \code{I}.
#' \item \code{"sensemakr_C_FC.csv"} for outcome \code{C}.
#' }
#' These files are written only when \code{dir_csv} is supplied.
#'
#' @return A list with components:
#' \itemize{
#' \item \code{I}: the \code{sensemakr} object for the model with
#' outcome \code{I}.
#' \item \code{C}: the \code{sensemakr} object for the model with
#' outcome \code{C}.
#' }
#'
#' @examples
#' \donttest{
#' # This example runs only when 'sensemakr' is installed.
#' if (requireNamespace("sensemakr", quietly = TRUE)) {
#' DT <- data.frame(
#' I = rpois(30, lambda = 5),
#' C = rpois(30, lambda = 3),
#' trans_FC = sample(0:1, 30, replace = TRUE),
#' t_norm = rnorm(30),
#' PopDensity = rnorm(30),
#' War = sample(0:1, 30, replace = TRUE)
#' )
#'
#' res_sense <- run_sensemakr(DT)
#' if (!is.null(res_sense$I)) {
#' print(summary(res_sense$I))
#' }
#' }
#' }
#'
#' @export
run_sensemakr <- function(DT, dir_csv = NULL) {
if (!requireNamespace("sensemakr", quietly = TRUE)) {
stop("Package 'sensemakr' is required for run_sensemakr(). Please install it.",
call. = FALSE)
}
lm_I <- lm(I ~ trans_FC + t_norm + PopDensity + War, data = DT)
sm_I <- sensemakr::sensemakr(lm_I, treatment = "trans_FC",
benchmark_covariates = c("PopDensity", "War"))
lm_C <- lm(C ~ trans_FC + t_norm + PopDensity + War, data = DT)
sm_C <- sensemakr::sensemakr(lm_C, treatment = "trans_FC",
benchmark_covariates = c("PopDensity", "War"))
if (!is.null(dir_csv)) {
if (!dir.exists(dir_csv)) dir.create(dir_csv, recursive = TRUE)
readr::write_csv(as.data.frame(summary(sm_I)),
file.path(dir_csv, "sensemakr_I_FC.csv"))
readr::write_csv(as.data.frame(summary(sm_C)),
file.path(dir_csv, "sensemakr_C_FC.csv"))
}
list(I = sm_I, C = sm_C)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.