Skip to content

Conversation

Copy link

Copilot AI commented Nov 23, 2025

Identified and eliminated duplicated code patterns across 6 files, reducing ~160 lines of redundant logic.

Changes

Created R/utils.R

Extracted cross-file utilities:

  • create_counterfactual_datasets() - Treatment counterfactual generation (used in gcomp_ml, gcomp_stan, mim)
  • extract_treatment_coef_name() - Coefficient name extraction with factor level handling (used in calc_stc, mim)

Enhanced R/calculate_ate.R

Consolidated ALD data extraction and effect function dispatch:

  • extract_ald_value() - Eliminates repeated dplyr::filterpull chains across 6 trial calculation functions
  • execute_effect_function() - Centralizes effect function validation and execution

Before:

calculate_trial_variance_binary <- function(ald, tid, effect) {
  y <- dplyr::filter(ald, .data$variable == "y", .data$trt == tid, 
                     .data$statistic == "sum") |> dplyr::pull(.data$value)
  N <- dplyr::filter(ald, .data$trt == tid, 
                     .data$statistic == "N") |> dplyr::pull(.data$value)
  
  effect_functions <- list(
    "log_odds" = function() 1/y + 1/(N-y),
    ...
  )
  
  if (!effect %in% names(effect_functions)) {
    stop(paste0("Unsupported effect function. Choose from ", ...))
  }
  effect_functions[[effect]]()
}

After:

calculate_trial_variance_binary <- function(ald, tid, effect) {
  y <- extract_ald_value(ald, tid, variable = "y", statistic = "sum")
  N <- extract_ald_value(ald, tid, variable = NULL, statistic = "N")
  
  effect_functions <- list(
    "log_odds" = function() 1/y + 1/(N-y),
    ...
  )
  
  execute_effect_function(effect, effect_functions)
}

Files Modified

  • R/calculate_ate.R - 6 functions refactored (variance/mean calculations for binary, continuous, count data)
  • R/gcomp_ml.R, R/gcomp_stan.R, R/mim.R - Counterfactual dataset creation
  • R/calc_stc.R - Coefficient name extraction

All helper functions marked @keywords internal for package-internal use.

Original prompt

Find and refactor duplicated code


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 4 commits November 23, 2025 18:44
…_ate.R

Co-authored-by: n8thangreen <3061918+n8thangreen@users.noreply.github.com>
Co-authored-by: n8thangreen <3061918+n8thangreen@users.noreply.github.com>
…unction

Co-authored-by: n8thangreen <3061918+n8thangreen@users.noreply.github.com>
Co-authored-by: n8thangreen <3061918+n8thangreen@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicated code for better maintainability Refactor: Extract helper functions to eliminate code duplication Nov 23, 2025
Copilot AI requested a review from n8thangreen November 23, 2025 18:55
@n8thangreen n8thangreen marked this pull request as ready for review November 23, 2025 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants