Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add error message for missing strata #227

Open
wants to merge 94 commits into
base: main
Choose a base branch
from

Conversation

chrisorwa
Copy link
Collaborator

No description provided.

@chrisorwa chrisorwa linked an issue Aug 1, 2024 that may be closed by this pull request
Copy link

codecov bot commented Aug 1, 2024

Codecov Report

Attention: Patch coverage is 93.63636% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
R/est.incidence.by.R 89.55% 7 Missing ⚠️
Files with missing lines Coverage Δ
R/check_strata.R 100.00% <100.00%> (ø)
R/utils.R 30.76% <ø> (+4.10%) ⬆️
R/est.incidence.by.R 94.52% <89.55%> (+94.52%) ⬆️

... and 3 files with indirect coverage changes

Copy link
Member

@d-morrison d-morrison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR; please address comments and add tests for the code lines in this PR.

cli::cli_abort(
class = "missing_var",
message = c(
"Can't find the column {.var {strata}} in {.envvar pop_data}.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revise this inline markup; pop_data is a function argument, not an environment variable (see https://cli.r-lib.org/reference/inline-markup.html)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is well-noted.

Comment on lines 118 to 120
if (length(strata_var) > 0) {
"i" <- "Did you mean {.var {strata_var}}."
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test that covers this case

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been implemented.

@@ -101,6 +101,27 @@ est.incidence.by <- function(
return(to_return)
}

if (!any(is.element(strata,pop_data %>% names()))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please always format according to the style guide (add a space after the comma in strata,pop_data, etc)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change has been made.

Copy link
Member

@d-morrison d-morrison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting closer; please see inline comments.

@@ -101,6 +101,27 @@ est.incidence.by <- function(
return(to_return)
}

if (!any(is.element(strata, pop_data %>% names()))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should any() be all()?

Copy link
Collaborator Author

@chrisorwa chrisorwa Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be all(). The change has been made.

grep(
x = pop_data %>% names(),
value = TRUE,
pattern = strata,
Copy link
Member

@d-morrison d-morrison Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens here if strata has multiple entries in it? for example, strata = c("Country", "catch")? Please check this scenario and adjust this code if needed (probably, try to match each missing stratum variable separately).

Copy link
Collaborator Author

@chrisorwa chrisorwa Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in order and adds complexity to Issue #233. For example, if strata = c("age", "Country"), I had opted to use regex paste0("^", strata) to capture only columns that begin with age to avoid and avoid words such as village.

Addendum:
Never mind, I found a solution to it.

message = c(
"Can't find the column {.arg {strata}} in {.arg pop_data}.",
if (length(strata_var) > 0) {
"i" <- "Did you mean {.var {strata_var}}."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please end questions with a question mark instead of a period.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been implemented.

cli::cli_abort(
class = "missing_var",
message = c(
"Can't find the column {.arg {strata}} in {.arg pop_data}.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please inform the user that this error relates to their input for the strata argument, so they know which input to fix.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been implemented.

Copy link
Member

@d-morrison d-morrison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hard work on this PR; please see inline comments.

@@ -101,6 +101,36 @@ est.incidence.by <- function(
return(to_return)
}

if (!all(is.element(strata, pop_data %>% names()))) {
# accommodate multiple strata
combined_pattern <- paste(strata, collapse = "|")
Copy link
Member

@d-morrison d-morrison Aug 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach seems sub-optimal, for two reasons:

  1. we won't be able to tell which columns partially match each specific missing element of strata.
  2. we didn't remove the elements of strata that already exactly match columns in pop_data

More generally, complicated regular expressions are a recipe for bugs; if you think you need to use | in a regex, please carefully consider alternative approaches. As always, we want to keep the code as simple as possible; for example, in this case, instead of pasting strata together, we could instead individually grep each element of strata that isn't exactly in pop_data, using a for loop, or lapply(), or purrr::map(), etc.

Then, we can report the possible partial matches for each missing element of strata; the markup capabilities of cli_abort() enable us to provide this information in an orderly, structured format. Please give all this a try, but if you get stuck, just let me know and I'll provide more detailed instructions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made changes to this approach to capture full match, partial match and no match.

curve <- load_curve_params("https://osf.io/download/rtw5k/")

expect_error(
object = est.incidence.by(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we testing est.incidence.by() in the test file for est.incidence()? Please be careful to keep these tests properly organized.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test in which strata contains multiple elements that don't exactly match the columns of pop_data

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have separated est.incidence and est.incidence.by into different scripts. In addition, I have added a test for multiple strata in est.incidence.by.

@d-morrison d-morrison added bug an unexpected problem or unintended behavior enhancement API application programming interface (exported functions, etc) unit testing labels Aug 14, 2024
@d-morrison d-morrison added this to the v2.0 release milestone Aug 14, 2024
Copy link
Member

@d-morrison d-morrison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this PR is still in-progress?

  • Please address the lint errors and restore the files that seem to have been accidentally deleted.

  • Before re-requesting review, please look through Files changed and add at least one comment per changed file: briefly explain what's been changed and why.

Copy link
Member

@d-morrison d-morrison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this work; please see comments below and suggested edits in #245

NEWS.md Outdated
@@ -1,4 +1,7 @@
# serocalculator (development version)
# serocalculator 1.3.0
Copy link
Member

@d-morrison d-morrison Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment in previous review; this change still needs to be reverted

R/est.incidence.by.R Outdated Show resolved Hide resolved
R/est.incidence.by.R Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API application programming interface (exported functions, etc) bug an unexpected problem or unintended behavior feature a feature request or enhancement unit testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add user error message for missing strata
2 participants