Skip to content

Fix 17 test failures: probability vectors, object init, vestigial dataset, boundary SE detection#1

Closed
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-test-failures
Closed

Fix 17 test failures: probability vectors, object init, vestigial dataset, boundary SE detection#1
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-test-failures

Conversation

Copy link
Copy Markdown

Copilot AI commented Feb 25, 2026

17 test failures caused by mismatched probability vector lengths, a forward-reference in data.frame(), a test referencing a removed dataset, and incomplete large-SE boundary detection logic.

Changes

tests/testthat/test-calc-risk-diff.R

  • Age probability vector (create_cachar_inspired_data): 18:70 is 53 values; original prob argument supplied 73 weights. Replaced with explicit length-matched array normalized to sum to 1:

    ages <- 18:70
    age_probs <- numeric(length(ages))
    age_probs[ages >= 18 & ages <= 39] <- 0.8
    age_probs[ages >= 40 & ages <= 59] <- 1.2
    age_probs[ages >= 60 & ages <= 70] <- 0.6
    age_probs <- age_probs / sum(age_probs)
    sample(ages, n, replace = TRUE, prob = age_probs)
  • Object initialization (create_convergence_challenge_data): exposure was referenced inside data.frame() before it existed. Refactored to build the frame first, then assign outcome_prob and outcome as follow-on steps.

  • Removed birthweight test: birthweight dataset was replaced by cachar_sample in a prior iteration; test referencing it was dead code causing a load failure.

R/boundary_detection.R

  • Large SE detection: Previous logic only flagged large SEs via relative ratio (SE / |coef| > 2), which misses cases where the coefficient itself is near zero. Detection now triggers on either absolute SE > 2.0 or relative ratio > 2.0 (guarded against all-small-coefficient false positives).
Original prompt

Fix Test Failures: Correct Probability Vectors, Object Initialization, and Remove Vestigial Data

This PR fixes 17 test failures in the riskdiff test suite:

Critical Issues Fixed:

1. Test Data Generation Errors (9 failures)

  • Root Cause: Mismatched probability vector lengths in sample() calls
  • Example Problem: sample(18:70, prob = c(rep(0.8, 22), rep(1.2, 30), rep(0.6, 21))) attempts to sample from 53 values (18:70) but provides 73 probability weights
  • Fix Applied: Create explicit probability distribution array of correct length (53) and normalize

In create_cachar_inspired_data(), replace:

age = sample(18:70, n, replace = TRUE,
             prob = c(rep(0.8, 22), rep(1.2, 30), rep(0.6, 21)))

With:

ages <- 18:70
age_probs <- numeric(length(ages))
age_probs[ages >= 18 & ages <= 39] <- 0.8   # Young
age_probs[ages >= 40 & ages <= 59] <- 1.2   # Middle
age_probs[ages >= 60 & ages <= 70] <- 0.6   # Older
age_probs <- age_probs / sum(age_probs)
age = sample(ages, n, replace = TRUE, prob = age_probs)

2. Object Initialization Error (1 failure)

  • Root Cause: create_convergence_challenge_data() uses exposure variable in dplyr::mutate() before it's fully defined
  • Error Message: object 'exposure' not found
  • Fix Applied: Create complete data frame first, then pipe to mutate

3. Remove Vestigial "birthweight" Dataset (1 failure + cleanup)

  • Context: The "birthweight" dataset was completely replaced with "cachar_sample" in a previous package iteration
  • Issue: Test references non-existent birthweight dataset
  • Fix Applied: Remove entire test section referencing birthweight data

4. Boundary Detection Large SE Test (1 failure)

  • Root Cause: Large standard error detection logic only checks relative ratio, missing edge cases
  • Fix Applied: Enhanced detection to trigger on EITHER:
    • Large absolute SE (> 2.0), OR
    • Large relative SE (ratio > 2.0)
    • Properly handles small coefficient edge cases

Files Modified:

  1. tests/testthat/test-calc-risk-diff.R (complete rewrite of test data generation)

    • Fixed create_cachar_inspired_data()
    • Fixed create_convergence_challenge_data()
    • Removed birthweight test section
  2. R/boundary_detection.R (lines ~120-140)

    • Improved large standard error detection logic

Expected Results:

  • Before: 545 passing, 17 failing
  • After: 562 passing, 0 failing

Testing:

All changes maintain backward compatibility with existing passing tests. Only failing tests are fixed.

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…, remove birthweight test, enhance SE detection

Co-authored-by: jackmurphy2351 <120122776+jackmurphy2351@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix test failures in riskdiff test suite Fix 17 test failures: probability vectors, object init, vestigial dataset, boundary SE detection Feb 25, 2026
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