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 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
88a24bd
add error message for missing strata
chrisorwa Aug 1, 2024
5ec8ec0
run document() for est.incidence.by
chrisorwa Aug 1, 2024
b82e3b5
add any() to is.element()
chrisorwa Aug 4, 2024
3c271ee
add test
chrisorwa Aug 7, 2024
ee3d7ca
Merge branch 'main' into add-user-error-message-for-missing-strata
chrisorwa Aug 12, 2024
39ea75b
Merge branch 'main' into add-user-error-message-for-missing-strata
chrisorwa Aug 14, 2024
0cb88d6
modify error messaging
chrisorwa Aug 14, 2024
97efcf3
Merge branch 'add-user-error-message-for-missing-strata' of https://g…
chrisorwa Aug 14, 2024
b3523c7
make changes to tests and est.incidence.by
chrisorwa Aug 21, 2024
c09df9b
add purrr to DESCRIPTION
chrisorwa Aug 22, 2024
42c7af6
modify error message
chrisorwa Aug 26, 2024
0f36e94
correct errors
chrisorwa Aug 26, 2024
f97218a
remove testthat from DESCRIPTION
chrisorwa Aug 26, 2024
757676e
Merge branch 'main' into add-user-error-message-for-missing-strata
chrisorwa Aug 27, 2024
87625a1
Merge branch 'main' into add-user-error-message-for-missing-strata
chrisorwa Sep 10, 2024
3bf79b3
lint file
chrisorwa Sep 10, 2024
927911a
lint tests
chrisorwa Sep 11, 2024
655985b
Merge branch 'main' into add-user-error-message-for-missing-strata
chrisorwa Sep 12, 2024
8302735
increment version
chrisorwa Sep 17, 2024
2ef8e8e
Merge branch 'main' into add-user-error-message-for-missing-strata
d-morrison Sep 23, 2024
6b365c0
increment version
d-morrison Sep 24, 2024
7cac80b
Merge branch 'main' into add-user-error-message-for-missing-strata
chrisorwa Sep 29, 2024
cc000f7
Merge branch 'main' into add-user-error-message-for-missing-strata
chrisorwa Sep 30, 2024
d3a8b8c
correct conflicts
chrisorwa Oct 13, 2024
c22ed3c
requested changes
chrisorwa Oct 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion R/est.incidence.by.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#' curve_params = curve,
#' noise_params = noise %>% filter(Country == "Pakistan"),
#' antigen_isos = c("HlyE_IgG", "HlyE_IgA"),
#' #num_cores = 8 # Allow for parallel processing to decrease run time
#' # num_cores = 8 # Allow for parallel processing to decrease run time
#' iterlim = 5 # limit iterations for the purpose of this example
#' )
#'
Expand Down Expand Up @@ -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.


# search strata variable from pop_data
present_vars <-
grep(
x = pop_data %>% names(),
value = TRUE,
pattern = combined_pattern,
ignore.case = TRUE
)

not_present_vars <- setdiff(
x = strata,
y = present_vars
)

cli::cli_abort(
class = "missing_var",
message = c(
"x" = "Can't stratify with the provided strata.",
"i" = "The {.arg strata} option{?s} {.arg {not_present_vars}} {?is/are} missing in {.arg pop_data}.",
if (length(present_vars) > 0) {
"i" <- "Did you mean {.var {present_vars}}?"
}
)
)
}

.checkStrata(data = pop_data, strata = strata)

.errorCheck(
Expand Down
2 changes: 1 addition & 1 deletion man/est.incidence.by.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions tests/testthat/test-est.incidence.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,36 @@ test_that("`est.incidence()` produces expected results", {

expect_equal(est_true, est_false)
})

test_that("est.incidence() aborts when strata is missing in `pop_data`", {
# get pop data
xs_data <- load_pop_data(
file_path = "https://osf.io/download//n6cp3/",
age = "Age",
value = "result",
id = "index_id",
standardize = TRUE
) %>%
filter(Country == "Pakistan") %>%
slice_head(n = 100)

# get noise data
noise <- load_noise_params("https://osf.io/download//hqy4v/") %>%
filter(Country == "Pakistan")

# get curve data
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.

strata = c("catch"),
pop_data = xs_data %>% filter(Country == "Pakistan"),
curve_params = curve,
noise_params = noise %>% filter(Country == "Pakistan"),
antigen_isos = c("HlyE_IgG", "HlyE_IgA"),
# num_cores = 8 # Allow for parallel processing to decrease run time
iterlim = 5 # limit iterations for the purpose of this example
),
class = "missing_var"
)
})
Loading