Skip to content

Fix malformed sample() calls in create_cachar_inspired_data() test helper#5

Closed
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-calc-risk-diff-tests
Closed

Fix malformed sample() calls in create_cachar_inspired_data() test helper#5
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-calc-risk-diff-tests

Conversation

Copy link

Copilot AI commented Feb 25, 2026

The create_cachar_inspired_data() test helper had a missing closing paren on the sample() call for age, causing all subsequent mutate() arguments (sex, residence, etc.) to be parsed as arguments to sample() instead. This broke every test that relies on Cachar-inspired data.

Changes

  • tests/testthat/test-calc-risk-diff.R
    • Added missing ) to close sample() in the age assignment, separating it correctly from subsequent mutate() arguments
    • Removed orphaned ) after dplyr::arrange(id) at the end of the pipe chain
# Before — sample() never closed; sex/residence/... become its arguments
age = sample(18:70, n, replace = TRUE,
             prob = c(rep(0.8, 8), rep(1.2, 30), rep(0.6, 15)),  # ← missing )
sex = factor(...),

# After — sample() properly closed before next mutate() argument
age = sample(18:70, n, replace = TRUE,
             prob = c(rep(0.8, 8), rep(1.2, 30), rep(0.6, 15))),
sex = factor(...),
Original prompt

Problem

The test suite in tests/testthat/test-calc-risk-diff.R is failing due to malformed dplyr::mutate() calls in the test data generation functions. Specifically, arguments intended for mutate() are being incorrectly nested as arguments to sample().

Root Cause

In functions like create_cachar_inspired_data(), the code attempts to pass multiple variables to mutate() but incorrectly nests them inside the sample() function call:

dplyr::mutate(
  .,
  age = sample(18:70, n, replace = TRUE, prob = ...,
  sex = factor(sample(c("male", "female"), n, replace = TRUE, prob = c(0.75, 0.25)), levels = c("male", "female")),
  residence = factor(sample(c("rural", "urban", "urban_slum"), n, replace = TRUE, prob = c(0.85, 0.12, 0.03)), levels = c("rural", "urban", "urban_slum")),
  ...
)

The error produced is:

Error in `dplyr::mutate(...)`: 
Caused by error in `sample()`:
! unused arguments (sex = factor(...), residence = factor(...))

Affected Tests

Multiple test functions fail due to this issue:

  • calc_risk_diff works with Cachar-inspired tobacco/areca data (line 226)
  • calc_risk_diff works with tobacco_areca_both combination variable (line 243)
  • calc_risk_diff compares single vs combined tobacco/areca exposures (line 260)
  • calc_risk_diff handles age-adjusted analysis with tobacco/areca data (line 280)
  • calc_risk_diff handles sex-stratified analysis with tobacco/areca data (line 295)
  • calc_risk_diff handles residence-stratified analysis (line 311)
  • calc_risk_diff works with head/neck specific outcomes (line 326)
  • calc_risk_diff produces epidemiologically plausible results with tobacco data (line 568)
  • calc_risk_diff shows expected sex differences in tobacco use patterns (line 590)
  • calc_risk_diff handles moderately large datasets efficiently (line 661)

Solution

Split each nested mutate() call so that each variable assignment is a separate argument to mutate(), with the sample() or other function calls properly closed before defining the next variable:

dplyr::mutate(
  age = sample(18:70, n, replace = TRUE, prob = c(rep(0.8, 8), rep(1.2, 30), rep(0.6, 15))),
  sex = factor(sample(c("male", "female"), n, replace = TRUE, prob = c(0.75, 0.25)), levels = c("male", "female")),
  residence = factor(sample(c("rural", "urban", "urban_slum"), n, replace = TRUE, prob = c(0.85, 0.12, 0.03)), levels = c("rural", "urban", "urban_slum")),
  ...
)

This fix will:

  1. Correct the syntax of all mutate() calls to properly separate variable assignments
  2. Allow the test data generation functions to execute without errors
  3. Enable the dependent test cases to run and validate the calc_risk_diff() functionality

This pull request was created from Copilot chat.


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

…_data()

Co-authored-by: jackmurphy2351 <120122776+jackmurphy2351@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix malformed mutate calls in test-calc-risk-diff Fix malformed sample() calls in create_cachar_inspired_data() test helper Feb 25, 2026
@jackmurphy2351 jackmurphy2351 marked this pull request as ready for review February 26, 2026 01:24
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