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 20 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
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# serocalculator (development version)
# serocalculator 1.3.0
* Add test for `est.incidence`
Copy link
Member

Choose a reason for hiding this comment

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

use dev version


Copy link
Member

Choose a reason for hiding this comment

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

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.

and what behavior is being tested?

* Add option to test `strata` in `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.

is this a user-facing option? not sure what this means.

# serocalculator 1.2.0
* Added `test-summary.pop_data` test
Expand Down
71 changes: 41 additions & 30 deletions R/est.incidence.by.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
#' Estimate Seroincidence
#'
#' @description
#' Function to estimate seroincidences based on cross-section serology data and longitudinal
#' Function to estimate seroincidences based on cross-section
Copy link
Member

Choose a reason for hiding this comment

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

cross-sectional

#' serology data and longitudinal
d-morrison marked this conversation as resolved.
Show resolved Hide resolved
#' 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
#' @param strata a [character] vector of stratum-defining variables. Values must be variable names in `pop_data`.
#' @param curve_strata_varnames A subset of `strata`. Values must be variable names in `curve_params`. Default = "".
#' @param noise_strata_varnames A subset of `strata`. Values must be variable names in `noise_params`. Default = "".
#' @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.
#' @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
#' @param strata a [character] vector of stratum-defining variables.
#' Values must be variable names in `pop_data`.
#' @param curve_strata_varnames A subset of `strata`.
#' Values must be variable names in `curve_params`. Default = "".
#' @param noise_strata_varnames A subset of `strata`.
#' Values must be variable names in `noise_params`. Default = "".
#' @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.
d-morrison marked this conversation as resolved.
Show resolved Hide resolved
d-morrison marked this conversation as resolved.
Show resolved Hide resolved

#' @details
#'
Expand All @@ -17,7 +25,8 @@
#' 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.
#' you may use `NA`, `NULL`, or `""` as the `strata`
#' argument to avoid that warning.
d-morrison marked this conversation as resolved.
Show resolved Hide resolved
#'
#'
#' @inheritParams est.incidence
Expand All @@ -26,7 +35,9 @@
#'
#' @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.
#' An object of class `"seroincidence.by"`; i.e., a list of `
Copy link
Member

Choose a reason for hiding this comment

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

please move linebreak to before the backtick

#' "seroincidence"` objects from [est.incidence()], one for each stratum,
#' with some meta-data attributes.
d-morrison marked this conversation as resolved.
Show resolved Hide resolved
#' * if `strata` is missing, `NULL`, `NA`, or `""`:
#' An object of class `"seroincidence"`.
#'
Expand All @@ -39,7 +50,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
#' slice(1:100, .by = antigen_iso)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Remove comment to make the line less than 80 characters.

Copy link
Member

Choose a reason for hiding this comment

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

instead of removing the comment, why not move it to a new line?

#'
#' noise <- load_noise_params("https://osf.io/download//hqy4v/")
#'
Expand All @@ -49,13 +60,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 69 in R/est.incidence.by.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=69,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 15, this has 21.

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

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=69,col=1,[object_name_linter] Variable and function name style should match snake_case or symbols.
pop_data,
curve_params,
noise_params,
Expand All @@ -75,16 +86,19 @@
warning(
Copy link
Member

Choose a reason for hiding this comment

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

please update from warning() to cli_warn()

"The `strata` argument to `est.incidence.by()` is missing.",
"\n\n If you do not want to stratify your data, ",
"consider using the `est.incidence()` function to simplify your code and avoid this warning.",
"\n\n Since the `strata` argument is empty, `est.incidence.by()` will return a `seroincidence` object, instead of a `seroincidence.by` object.\n"
"consider using the `est.incidence()` function to
simplify your code and avoid this warning.",
"\n\n Since the `strata` argument is empty, `est.incidence.by()`
will return a `seroincidence` object, instead of a
`seroincidence.by` object.\n"

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

View check run for this annotation

Codecov / codecov/patch

R/est.incidence.by.R#L89-L93

Added lines #L89 - L93 were not covered by tests
d-morrison marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +90 to +94
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 tests everywhere indicated by Codecov comments

)
}

strata_is_empty <-
missing(strata) ||
is.null(strata) ||
setequal(strata, NA) ||
setequal(strata, "")
is.null(strata) ||
setequal(strata, NA) ||
setequal(strata, "")

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

View check run for this annotation

Codecov / codecov/patch

R/est.incidence.by.R#L99-L101

Added lines #L99 - L101 were not covered by tests
d-morrison marked this conversation as resolved.
Show resolved Hide resolved

if (strata_is_empty) {
to_return <-
Expand All @@ -110,7 +124,7 @@
)

# Split data per stratum
stratumDataList <- stratify_data(

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

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=127,col=3,[object_name_linter] Variable and function name style should match snake_case or symbols.
antigen_isos = antigen_isos,
data = pop_data %>% filter(.data$antigen_iso %in% antigen_isos),
curve_params = curve_params %>% filter(.data$antigen_iso %in% antigen_isos),
Expand All @@ -130,8 +144,10 @@

if (num_cores > 1L && !requireNamespace("parallel", quietly = TRUE)) {
warning(
"The `parallel` package is not installed, so `num_cores > 1` has no effect.",
"To install `parallel`, run `install.packages('parallel')` in the console."
"The `parallel` package is not installed,
so `num_cores > 1` has no effect.",
"To install `parallel`, run `install.packages('parallel')`
in the console."

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

View check run for this annotation

Codecov / codecov/patch

R/est.incidence.by.R#L147-L150

Added lines #L147 - L150 were not covered by tests
d-morrison marked this conversation as resolved.
Show resolved Hide resolved
)
}

Expand All @@ -142,11 +158,12 @@
num_cores <- num_cores %>% check_parallel_cores()

if (verbose) {
message("Setting up parallel processing with `num_cores` = ", num_cores, ".")
message("Setting up parallel processing with
Copy link
Member

Choose a reason for hiding this comment

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

Please update from message() to cli_inform()

`num_cores` = ", num_cores, ".")

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

View check run for this annotation

Codecov / codecov/patch

R/est.incidence.by.R#L161-L162

Added lines #L161 - L162 were not covered by tests
d-morrison marked this conversation as resolved.
Show resolved Hide resolved
}


libPaths <- .libPaths()

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

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=166,col=5,[object_name_linter] Variable and function name style should match snake_case or symbols.
cl <-
num_cores %>%
parallel::makeCluster() %>%
Expand All @@ -158,11 +175,11 @@
parallel::clusterExport(cl, c("libPaths"), envir = environment())
parallel::clusterEvalQ(cl, {
.libPaths(libPaths)
require(serocalculator) # note - this gets out of sync when using load_all() in development
require(serocalculator) # note - this gets out of sync" %>%
Copy link
Member

Choose a reason for hiding this comment

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

why add %>% at the end of this line? what does it do?

"when using load_all() in development"

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

View check run for this annotation

Codecov / codecov/patch

R/est.incidence.by.R#L178-L179

Added lines #L178 - L179 were not covered by tests
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Reduce sentences to 80 characters (linting requirement).

Copy link
Member

Choose a reason for hiding this comment

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

it looks like the second half of that comment got turned into code; why?

require(dplyr)
})

Copy link
Member

Choose a reason for hiding this comment

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

why?

{

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

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=182,col=5,[brace_linter] Opening curly braces should never go on their own line and should always be followed by a new line.
fits <- parallel::parLapplyLB(
cl = cl,
X = stratumDataList,
Expand All @@ -183,23 +200,16 @@
)
}
)
} %>% system.time() -> time

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

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=203,col=5,[brace_linter] Closing curly-braces should always be on their own line, unless they are followed by an else.

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

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=203,col=25,[assignment_linter] Use <-, not ->, for assignment.

if (verbose) {
message("Elapsed time for parallelized code: ")
print(time)
}
} else {
# fits <- lapply(
# X = stratumDataList,
# FUN = function(x) est.incidence(dataList = x, verbose = verbose, ...))

fits <- list()

{ # time progress

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

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=211,col=5,[brace_linter] Opening curly braces should never go on their own line and should always be followed by a new line.

for (cur_stratum in names(stratumDataList))
{
for (cur_stratum in names(stratumDataList)) {

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

View check run for this annotation

Codecov / codecov/patch

R/est.incidence.by.R#L212

Added line #L212 was not covered by tests
cur_stratum_vars <-
strata_table %>%
dplyr::filter(.data$Stratum == cur_stratum)
Expand All @@ -225,7 +235,8 @@
)
)
}
} %>% system.time() -> time
Copy link
Member

Choose a reason for hiding this comment

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

we don't want to lose the system.time() call; we need it below for verbose messaging.

I understand that this was changed to avoid the right-hand assignment operator ->, but there needs to be a left-hand assignment at the beginning of this whole expression instead.

}


if (verbose) {
message("Elapsed time for loop over strata: ")
Expand All @@ -233,7 +244,7 @@
}
}

incidenceData <- structure(

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

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/est.incidence.by.R,line=247,col=3,[object_name_linter] Variable and function name style should match snake_case or symbols.
fits,
antigen_isos = antigen_isos,
Strata = strata_table,
Expand Down
31 changes: 21 additions & 10 deletions man/est.incidence.by.Rd

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

6 changes: 4 additions & 2 deletions man/stratify_data.Rd

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

Loading
Loading