|
| 1 | +#' Check Effective Sample Size |
| 2 | +#' |
| 3 | +#' Computes the effective sample size (ESS) for one or more weighting schemes, |
| 4 | +#' optionally stratified by treatment groups. ESS reflects how many observations |
| 5 | +#' you would have if all were equally weighted. |
| 6 | +#' |
| 7 | +#' @details |
| 8 | +#' The effective sample size (ESS) is calculated using the classical formula: |
| 9 | +#' \eqn{ESS = (\sum w)^2 / \sum(w^2)}. |
| 10 | +#' |
| 11 | +#' When weights vary substantially, the ESS can be much smaller than the actual |
| 12 | +#' number of observations, indicating that a few observations carry |
| 13 | +#' disproportionately large weights. |
| 14 | +#' |
| 15 | +#' When `.group` is provided, ESS is calculated separately for each group level: |
| 16 | +#' - For binary/categorical exposures: ESS is computed within each treatment level |
| 17 | +#' - For continuous exposures: The variable is divided into quantiles (using |
| 18 | +#' `dplyr::ntile()`) and ESS is computed within each quantile |
| 19 | +#' |
| 20 | +#' The function returns results in a tidy format suitable for plotting or |
| 21 | +#' further analysis. |
| 22 | +#' |
| 23 | +#' @inheritParams check_params |
| 24 | +#' @param .group Optional grouping variable. When provided, ESS is calculated |
| 25 | +#' separately for each group level. For continuous variables, groups are |
| 26 | +#' created using quantiles. |
| 27 | +#' @param n_tiles For continuous `.group` variables, the number of quantile |
| 28 | +#' groups to create. Default is 4 (quartiles). |
| 29 | +#' @param tile_labels Optional character vector of labels for the quantile groups |
| 30 | +#' when `.group` is continuous. If NULL, uses "Q1", "Q2", etc. |
| 31 | +#' |
| 32 | +#' @return A tibble with columns: |
| 33 | +#' \item{method}{Character. The weighting method ("observed" or weight variable name).} |
| 34 | +#' \item{group}{Character. The group level (if `.group` is provided).} |
| 35 | +#' \item{n}{Integer. The number of observations in the group.} |
| 36 | +#' \item{ess}{Numeric. The effective sample size.} |
| 37 | +#' \item{ess_pct}{Numeric. ESS as a percentage of the actual sample size.} |
| 38 | +#' |
| 39 | +#' @family balance functions |
| 40 | +#' @seealso [ess()] for the underlying ESS calculation, [plot_ess()] for visualization |
| 41 | +#' |
| 42 | +#' @examples |
| 43 | +#' # Overall ESS for different weighting schemes |
| 44 | +#' check_ess(nhefs_weights, .wts = c(w_ate, w_att, w_atm)) |
| 45 | +#' |
| 46 | +#' # ESS by treatment group (binary exposure) |
| 47 | +#' check_ess(nhefs_weights, .wts = c(w_ate, w_att), .group = qsmk) |
| 48 | +#' |
| 49 | +#' # ESS by treatment group (categorical exposure) |
| 50 | +#' check_ess(nhefs_weights, .wts = w_cat_ate, .group = alcoholfreq_cat) |
| 51 | +#' |
| 52 | +#' # ESS by quartiles of a continuous variable |
| 53 | +#' check_ess(nhefs_weights, .wts = w_ate, .group = age, n_tiles = 4) |
| 54 | +#' |
| 55 | +#' # Custom labels for continuous groups |
| 56 | +#' check_ess(nhefs_weights, .wts = w_ate, .group = age, |
| 57 | +#' n_tiles = 3, tile_labels = c("Young", "Middle", "Older")) |
| 58 | +#' |
| 59 | +#' # Without unweighted comparison |
| 60 | +#' check_ess(nhefs_weights, .wts = w_ate, .group = qsmk, |
| 61 | +#' include_observed = FALSE) |
| 62 | +#' |
| 63 | +#' @export |
| 64 | +check_ess <- function( |
| 65 | + .data, |
| 66 | + .wts = NULL, |
| 67 | + .group = NULL, |
| 68 | + include_observed = TRUE, |
| 69 | + n_tiles = 4, |
| 70 | + tile_labels = NULL |
| 71 | +) { |
| 72 | + # Validate inputs |
| 73 | + validate_data_frame(.data) |
| 74 | + |
| 75 | + # Handle group variable |
| 76 | + group_quo <- rlang::enquo(.group) |
| 77 | + has_group <- !rlang::quo_is_null(group_quo) |
| 78 | + |
| 79 | + if (has_group) { |
| 80 | + group_name <- get_column_name(group_quo, ".group") |
| 81 | + validate_column_exists(.data, group_name, ".group") |
| 82 | + group_var <- .data[[group_name]] |
| 83 | + |
| 84 | + # Check if continuous (numeric and more than 10 unique values) |
| 85 | + is_continuous <- is.numeric(group_var) && |
| 86 | + length(unique(stats::na.omit(group_var))) > 10 |
| 87 | + |
| 88 | + if (is_continuous) { |
| 89 | + # Create quantile groups |
| 90 | + if (!is.null(tile_labels) && length(tile_labels) != n_tiles) { |
| 91 | + abort( |
| 92 | + "Length of {.arg tile_labels} must equal {.arg n_tiles}", |
| 93 | + error_class = "halfmoon_length_error" |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + # Create tile groups |
| 98 | + .data$.ess_group <- dplyr::ntile(group_var, n_tiles) |
| 99 | + |
| 100 | + # Apply labels |
| 101 | + if (is.null(tile_labels)) { |
| 102 | + tile_labels <- paste0("Q", seq_len(n_tiles)) |
| 103 | + } |
| 104 | + .data$.ess_group <- factor( |
| 105 | + .data$.ess_group, |
| 106 | + levels = seq_len(n_tiles), |
| 107 | + labels = tile_labels |
| 108 | + ) |
| 109 | + group_col <- ".ess_group" |
| 110 | + } else { |
| 111 | + group_col <- group_name |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + # Handle weights |
| 116 | + wts_quo <- rlang::enquo(.wts) |
| 117 | + |
| 118 | + if (rlang::quo_is_null(wts_quo)) { |
| 119 | + # No weights provided, just use observed |
| 120 | + wts_names <- character() |
| 121 | + } else { |
| 122 | + wts_cols <- tidyselect::eval_select(wts_quo, .data) |
| 123 | + wts_names <- names(wts_cols) |
| 124 | + |
| 125 | + # Convert psw weight columns to numeric |
| 126 | + for (wts_name in wts_names) { |
| 127 | + .data[[wts_name]] <- extract_weight_data(.data[[wts_name]]) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + # Add observed if requested |
| 132 | + if (include_observed || length(wts_names) == 0) { |
| 133 | + .data$.observed <- 1 |
| 134 | + wts_names <- c(".observed", wts_names) |
| 135 | + } |
| 136 | + |
| 137 | + # Reshape to long format |
| 138 | + plot_data <- tidyr::pivot_longer( |
| 139 | + .data, |
| 140 | + cols = dplyr::all_of(wts_names), |
| 141 | + names_to = "method", |
| 142 | + values_to = "weight" |
| 143 | + ) |
| 144 | + |
| 145 | + # Clean up method names |
| 146 | + plot_data$method <- ifelse( |
| 147 | + plot_data$method == ".observed", |
| 148 | + "observed", |
| 149 | + plot_data$method |
| 150 | + ) |
| 151 | + |
| 152 | + # Calculate ESS |
| 153 | + if (has_group) { |
| 154 | + # Group-wise ESS |
| 155 | + ess_data <- plot_data |> |
| 156 | + dplyr::group_by(method, .data[[group_col]]) |> |
| 157 | + dplyr::summarise( |
| 158 | + n = dplyr::n(), |
| 159 | + ess = ess(weight, na.rm = TRUE), |
| 160 | + ess_pct = ess / n * 100, |
| 161 | + .groups = "drop" |
| 162 | + ) |> |
| 163 | + dplyr::rename(group = !!group_col) |
| 164 | + } else { |
| 165 | + # Overall ESS |
| 166 | + ess_data <- plot_data |> |
| 167 | + dplyr::group_by(method) |> |
| 168 | + dplyr::summarise( |
| 169 | + n = dplyr::n(), |
| 170 | + ess = ess(weight, na.rm = TRUE), |
| 171 | + ess_pct = ess / n * 100, |
| 172 | + .groups = "drop" |
| 173 | + ) |
| 174 | + } |
| 175 | + |
| 176 | + # Clean up temporary columns |
| 177 | + if (has_group && is_continuous && ".ess_group" %in% names(ess_data)) { |
| 178 | + ess_data <- dplyr::select(ess_data, -.ess_group) |
| 179 | + } |
| 180 | + |
| 181 | + ess_data |
| 182 | +} |
0 commit comments