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 14 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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Imports:
tidyr,
utils,
tidyselect,
cli
cli,
purrr
d-morrison marked this conversation as resolved.
Show resolved Hide resolved
Suggests:
parallel,
knitr,
Expand Down
57 changes: 56 additions & 1 deletion R/est.incidence.by.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#' Estimate Seroincidence
#'
#' @description
#' Function to estimate seroincidences based on cross-section serology data and longitudinal

Check warning on line 4 in R/est.incidence.by.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=4,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 92 characters.
#' response model.
#'
#' @param pop_data a [data.frame] with cross-sectional serology data per antibody and age, and additional columns corresponding to each element of the `strata` input

Check warning on line 7 in R/est.incidence.by.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=7,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 165 characters.
#' @param strata a [character] vector of stratum-defining variables. Values must be variable names in `pop_data`.

Check warning on line 8 in R/est.incidence.by.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=8,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 113 characters.
#' @param curve_strata_varnames A subset of `strata`. Values must be variable names in `curve_params`. Default = "".

Check warning on line 9 in R/est.incidence.by.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=9,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 116 characters.
#' @param noise_strata_varnames A subset of `strata`. Values must be variable names in `noise_params`. Default = "".

Check warning on line 10 in R/est.incidence.by.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=10,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 116 characters.
#' @param num_cores Number of processor cores to use for calculations when computing by strata. If set to more than 1 and package \pkg{parallel} is available, then the computations are executed in parallel. Default = 1L.

Check warning on line 11 in R/est.incidence.by.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=11,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 220 characters.

#' @details
#'
Expand All @@ -17,7 +17,7 @@
#' and then the data will be passed to [est.incidence()].
#' If for some reason you want to use [est.incidence.by()]
#' with no strata instead of calling [est.incidence()],
#' you may use `NA`, `NULL`, or `""` as the `strata` argument to avoid that warning.

Check warning on line 20 in R/est.incidence.by.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=20,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 84 characters.
#'
#'
#' @inheritParams est.incidence
Expand All @@ -26,7 +26,7 @@
#'
#' @return
#' * if `strata` has meaningful inputs:
#' An object of class `"seroincidence.by"`; i.e., a list of `"seroincidence"` objects from [est.incidence()], one for each stratum, with some meta-data attributes.

Check warning on line 29 in R/est.incidence.by.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=29,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 163 characters.
#' * if `strata` is missing, `NULL`, `NA`, or `""`:
#' An object of class `"seroincidence"`.
#'
Expand All @@ -39,7 +39,7 @@
#'
#' curve <- load_curve_params("https://osf.io/download/rtw5k/") %>%
#' filter(antigen_iso %in% c("HlyE_IgA", "HlyE_IgG")) %>%
#' slice(1:100, .by = antigen_iso) # Reduce dataset for the purposes of this example

Check warning on line 42 in R/est.incidence.by.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=42,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 86 characters.
#'
#' noise <- load_noise_params("https://osf.io/download//hqy4v/")
#'
Expand All @@ -49,13 +49,13 @@
#' 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
#' )
#'
#' summary(est2)
#'
est.incidence.by <- function(

Check warning on line 58 in R/est.incidence.by.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=58,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 15, this has 23.
pop_data,
curve_params,
noise_params,
Expand Down Expand Up @@ -101,6 +101,61 @@
return(to_return)
}

if (!all(is.element(strata, pop_data %>% names()))) {

# find strata with full match
present_vars <- purrr::map(.x = strata, .f = function(pattern) {
grep(
x = pop_data %>% names(),
value = TRUE,
pattern = paste0("^", pattern, "$"),
ignore.case = TRUE
)
}) %>%
purrr::keep(~ length(.x) > 0) %>%
unlist()

# find strata with partial matches
present_partial_vars <- purrr::keep(strata,
function(pattern) {
length(
grep(
pattern,
pop_data %>% names(),
ignore.case = TRUE)) > 0
}) %>%
setdiff(y = present_vars)

# get partial matches
partial_match <- purrr::map(.x = present_partial_vars, .f = function(pattern) {
grep(
x = pop_data %>% names(),
value = TRUE,
pattern = pattern,
ignore.case = TRUE
)
}) %>%
purrr::keep(~ length(.x) > 0) %>%
unlist()

# strata with no match
no_match_vars <- strata %>%
setdiff(pop_data %>% names()) %>%
setdiff(present_partial_vars)

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

.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.

124 changes: 0 additions & 124 deletions tests/testthat/_snaps/as_curve_params.md
Copy link
Member

Choose a reason for hiding this comment

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

why deleted? if accidental, please revert to main branch version.

This file was deleted.

32 changes: 0 additions & 32 deletions tests/testthat/test-as_curve_params.R
Copy link
Collaborator Author

@chrisorwa chrisorwa Sep 17, 2024

Choose a reason for hiding this comment

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

This file has been moved to PR #228 . This file was originally part of the as_noise_params PR and got to this PR by mistake. I have moved it back to the noise PR.

Copy link
Member

Choose a reason for hiding this comment

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

see above

This file was deleted.

54 changes: 54 additions & 0 deletions tests/testthat/test-est.incidence.by.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

library(dplyr)

# 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/")

test_that("est.incidence.by() aborts when strata is missing in `pop_data`", {

expect_error(
object = 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"
)
})

test_that("est.incidence.by() aborts when multiple elements that don't exactly match the columns of `pop_data` are provided", {

expect_error(
object = est.incidence.by(
strata = c("catch","Count"),
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