|
| 1 | +#' Check assumptions for a negative control analysis |
| 2 | +#' |
| 3 | +#' `nc_assume()` evaluates whether the dataset and specification are consistent |
| 4 | +#' with core assumptions required by negative control methods. Checks are |
| 5 | +#' performed heuristically (e.g. via regression tests) and should be treated |
| 6 | +#' as diagnostic aids, not proofs. |
| 7 | +#' |
| 8 | +#' Checks performed: |
| 9 | +#' |
| 10 | +#' - **Exclusion restriction** (`"exclusion"`): Tests whether the negative |
| 11 | +#' control exposure (NCE) is associated with the primary outcome after |
| 12 | +#' adjusting for the primary exposure and covariates. A significant |
| 13 | +#' association is evidence against the exclusion restriction. |
| 14 | +#' - **U-comparability** (`"u_comparability"`): Tests whether the NCE is |
| 15 | +#' associated with the primary exposure. A significant association is |
| 16 | +#' consistent with U-comparability: the NCE and the primary exposure share |
| 17 | +#' the same unmeasured confounders. |
| 18 | +#' |
| 19 | +#' @param ncdata `[nc_data]`\cr |
| 20 | +#' Dataset prepared with [nc_data()]. |
| 21 | +#' @param checks `[character()]`\cr |
| 22 | +#' Which assumptions to check. Defaults to all available checks. |
| 23 | +#' @param model `[nc_model]`\cr |
| 24 | +#' Model specification for regression-based checks. Defaults to OLS. |
| 25 | +#' @param ... `[any]`\cr |
| 26 | +#' Reserved for future use. |
| 27 | +#' |
| 28 | +#' @return An object of class `nc_assume_result`: a named list where each |
| 29 | +#' element corresponds to one check and contains `$passed` (`TRUE`/`FALSE`/ |
| 30 | +#' `NA`), `$message`, and optionally `$model_fit`. |
| 31 | +#' |
| 32 | +#' @export |
| 33 | +#' |
| 34 | +#' @examples |
| 35 | +#' df <- data.frame( |
| 36 | +#' A = rbinom(200, 1, 0.5), |
| 37 | +#' Y = rnorm(200), |
| 38 | +#' Z = rbinom(200, 1, 0.5), |
| 39 | +#' W = rnorm(200), |
| 40 | +#' age = rnorm(200) |
| 41 | +#' ) |
| 42 | +#' spec <- nc_spec( |
| 43 | +#' exposure = "A", outcome = "Y", |
| 44 | +#' nce = "Z", nco = "W", |
| 45 | +#' covariates = "age" |
| 46 | +#' ) |
| 47 | +#' nd <- nc_data(df, spec) |
| 48 | +#' nc_assume(nd) |
| 49 | +nc_assume <- function( |
| 50 | + ncdata, |
| 51 | + checks = c("exclusion", "u_comparability"), |
| 52 | + model = nc_model(stats::lm), |
| 53 | + ...) { |
| 54 | + if (!inherits(ncdata, "nc_data")) { |
| 55 | + rlang::abort("`ncdata` must be an `nc_data` object created by `nc_data()`.") |
| 56 | + } |
| 57 | + if (!inherits(model, "nc_model")) { |
| 58 | + rlang::abort("`model` must be an `nc_model` object created by `nc_model()`.") |
| 59 | + } |
| 60 | + |
| 61 | + supported <- c("exclusion", "u_comparability") |
| 62 | + for (ch in checks) check_choice(ch, supported) |
| 63 | + |
| 64 | + spec <- attr(ncdata, "spec") |
| 65 | + results <- list() |
| 66 | + |
| 67 | + if ("exclusion" %in% checks) { |
| 68 | + results[["exclusion"]] <- check_exclusion(ncdata, spec, model) |
| 69 | + } |
| 70 | + if ("u_comparability" %in% checks) { |
| 71 | + results[["u_comparability"]] <- check_u_comparability(ncdata, spec, model) |
| 72 | + } |
| 73 | + |
| 74 | + structure(results, class = "nc_assume_result") |
| 75 | +} |
| 76 | + |
| 77 | +#' @export |
| 78 | +print.nc_assume_result <- function(x, ...) { |
| 79 | + cat("Negative control assumption checks\n") |
| 80 | + for (nm in names(x)) { |
| 81 | + item <- x[[nm]] |
| 82 | + status <- if (isTRUE(item$passed)) "[PASS]" else if (isFALSE(item$passed)) "[FAIL]" else "[WARN]" |
| 83 | + cat(" ", status, nm, ":", item$message, "\n") |
| 84 | + } |
| 85 | + invisible(x) |
| 86 | +} |
| 87 | + |
| 88 | +check_exclusion <- function(ncdata, spec, model) { |
| 89 | + if (is.null(spec$nce)) { |
| 90 | + return(list( |
| 91 | + passed = NA, |
| 92 | + message = "No NCE specified; exclusion restriction cannot be checked." |
| 93 | + )) |
| 94 | + } |
| 95 | + |
| 96 | + predictors <- c( |
| 97 | + spec$nce, |
| 98 | + build_predictors(spec, include = c("exposure", "covariates")) |
| 99 | + ) |
| 100 | + fit <- fit_model(spec, ncdata, spec$outcome, predictors, model) |
| 101 | + cs <- summary(fit)$coefficients |
| 102 | + |
| 103 | + if (!spec$nce %in% rownames(cs)) { |
| 104 | + return(list( |
| 105 | + passed = NA, |
| 106 | + message = "NCE coefficient not found in model; check model specification.", |
| 107 | + model_fit = fit |
| 108 | + )) |
| 109 | + } |
| 110 | + |
| 111 | + p_val <- cs[spec$nce, 4L] |
| 112 | + passed <- p_val >= 0.05 |
| 113 | + msg <- if (passed) { |
| 114 | + sprintf( |
| 115 | + "NCE not significantly associated with outcome (p = %.3f).", |
| 116 | + p_val |
| 117 | + ) |
| 118 | + } else { |
| 119 | + sprintf( |
| 120 | + "NCE is significantly associated with outcome (p = %.3f); exclusion restriction may be violated.", |
| 121 | + p_val |
| 122 | + ) |
| 123 | + } |
| 124 | + |
| 125 | + list(passed = passed, message = msg, model_fit = fit) |
| 126 | +} |
| 127 | + |
| 128 | +check_u_comparability <- function(ncdata, spec, model) { |
| 129 | + if (is.null(spec$nce)) { |
| 130 | + return(list( |
| 131 | + passed = NA, |
| 132 | + message = "No NCE specified; U-comparability cannot be checked." |
| 133 | + )) |
| 134 | + } |
| 135 | + |
| 136 | + predictors <- c( |
| 137 | + spec$nce, |
| 138 | + build_predictors(spec, include = c("covariates")) |
| 139 | + ) |
| 140 | + fit <- fit_model(spec, ncdata, spec$exposure, predictors, model) |
| 141 | + cs <- summary(fit)$coefficients |
| 142 | + |
| 143 | + if (!spec$nce %in% rownames(cs)) { |
| 144 | + return(list( |
| 145 | + passed = NA, |
| 146 | + message = "NCE coefficient not found in model; check model specification.", |
| 147 | + model_fit = fit |
| 148 | + )) |
| 149 | + } |
| 150 | + |
| 151 | + p_val <- cs[spec$nce, 4L] |
| 152 | + passed <- p_val < 0.05 |
| 153 | + msg <- if (passed) { |
| 154 | + sprintf( |
| 155 | + "NCE is associated with primary exposure (p = %.3f), consistent with U-comparability.", |
| 156 | + p_val |
| 157 | + ) |
| 158 | + } else { |
| 159 | + sprintf( |
| 160 | + "NCE is not associated with primary exposure (p = %.3f); U-comparability may not hold.", |
| 161 | + p_val |
| 162 | + ) |
| 163 | + } |
| 164 | + |
| 165 | + list(passed = passed, message = msg, model_fit = fit) |
| 166 | +} |
0 commit comments