-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: main
Are you sure you want to change the base?
Changes from 37 commits
88a24bd
5ec8ec0
b82e3b5
3c271ee
ee3d7ca
39ea75b
0cb88d6
97efcf3
b3523c7
c09df9b
42c7af6
0f36e94
f97218a
757676e
13be640
87625a1
3bf79b3
927911a
655985b
8302735
47dd63a
2ef8e8e
6b365c0
7cac80b
cc000f7
d3a8b8c
c22ed3c
1eb1441
58e4598
bc7fe55
98c881d
474d54c
a2fc79a
05aabc1
c73eb38
a78d82a
23c8ec2
5107025
c0bd521
3c8b8b1
cebb5be
a5f4ff6
7dc8a6c
7cb6af9
41435d6
737a1c6
7ba202b
4232c42
78af1c6
4b496de
8478415
9c9626f
28cdd72
154fb5e
a73c8db
0fa2294
6b362f6
cb9c6fd
2dcff93
f2cce72
543788f
3682fac
2d251c4
7ad67e7
d8385bb
c017571
3ae6bed
201853d
c83e0cc
266bae1
c3d18bf
2587245
c3dfcbd
6fba6a8
2e395ad
491cda6
8742288
23a8baa
5c98bd5
1a73533
ae4fc68
d99d4da
cf0070b
33dc191
c984c18
9122ae8
177d334
f953b3b
8dc73c8
15a32d8
15f2bc2
4727731
4e2b8e9
1cddf51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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-sectional | ||
#' serology data and longitudinal | ||
#' 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 | ||
#' | ||
|
@@ -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 | ||
|
@@ -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 ` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
chrisorwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' 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"`. | ||
#' | ||
|
@@ -39,7 +50,8 @@ | |
#' | ||
#' 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 | ||
#' # Reduce dataset for the purposes of this example | ||
chrisorwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' slice(1:100, .by = antigen_iso) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove comment to make the line less than 80 characters. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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/") | ||
#' | ||
|
@@ -49,13 +61,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 70 in R/est.incidence.by.R GitHub Actions / lint-changed-files
|
||
pop_data, | ||
curve_params, | ||
noise_params, | ||
|
@@ -75,16 +87,19 @@ | |
warning( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please update from |
||
"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" | ||
d-morrison marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, "") | ||
d-morrison marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (strata_is_empty) { | ||
to_return <- | ||
|
@@ -110,7 +125,7 @@ | |
) | ||
|
||
# Split data per stratum | ||
stratumDataList <- stratify_data( | ||
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), | ||
|
@@ -123,15 +138,17 @@ | |
strata_table <- stratumDataList %>% attr("strata") | ||
|
||
if (verbose) { | ||
message("Data has been stratified.") | ||
message("Here are the strata that will be analyzed:") | ||
cli::cli_inform("Data has been stratified.") | ||
cli::cli_inform("Here are the strata that will be analyzed:") | ||
print(strata_table) | ||
} | ||
|
||
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." | ||
cli::cli_warn( | ||
"The `parallel` package is not installed, | ||
so `num_cores > 1` has no effect.", | ||
"To install `parallel`, run `install.packages('parallel')` | ||
in the console." | ||
d-morrison marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
} | ||
|
||
|
@@ -142,11 +159,11 @@ | |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update from |
||
`num_cores` = ", num_cores, ".") | ||
d-morrison marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
|
||
libPaths <- .libPaths() | ||
cl <- | ||
num_cores %>% | ||
parallel::makeCluster() %>% | ||
|
@@ -155,14 +172,19 @@ | |
parallel::stopCluster(cl) | ||
}) | ||
|
||
# Export library paths to the cluster | ||
parallel::clusterExport(cl, c("libPaths"), envir = environment()) | ||
|
||
# Evaluate library loading on the cluster | ||
parallel::clusterEvalQ(cl, { | ||
.libPaths(libPaths) | ||
require(serocalculator) # note - this gets out of sync when using load_all() in development | ||
# note - this gets out of sync when using load_all() in development | ||
require(serocalculator) | ||
require(dplyr) | ||
}) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why? |
||
{ | ||
# Perform parallel computation and record execution time | ||
time <- system.time({ | ||
fits <- parallel::parLapplyLB( | ||
cl = cl, | ||
X = stratumDataList, | ||
|
@@ -183,49 +205,41 @@ | |
) | ||
} | ||
) | ||
} %>% system.time() -> time | ||
}) | ||
|
||
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() | ||
fits <- list() # Initialize an empty list for fits | ||
|
||
{ # time progress | ||
# Time progress | ||
for (cur_stratum in names(stratumDataList)) { | ||
cur_stratum_vars <- strata_table %>% | ||
dplyr::filter(.data$Stratum == cur_stratum) | ||
|
||
for (cur_stratum in names(stratumDataList)) | ||
{ | ||
cur_stratum_vars <- | ||
strata_table %>% | ||
dplyr::filter(.data$Stratum == cur_stratum) | ||
|
||
if (verbose) { | ||
message("starting new stratum: ", cur_stratum) | ||
print(cur_stratum_vars) | ||
} | ||
if (verbose) { | ||
message("starting new stratum: ", cur_stratum) | ||
print(cur_stratum_vars) | ||
} | ||
|
||
fits[[cur_stratum]] <- | ||
do.call( | ||
what = est.incidence, | ||
args = c( | ||
stratumDataList[[cur_stratum]], | ||
list( | ||
lambda_start = lambda_start, | ||
antigen_isos = antigen_isos, | ||
build_graph = build_graph, | ||
print_graph = print_graph, | ||
verbose = verbose, | ||
... | ||
) | ||
) | ||
fits[[cur_stratum]] <- do.call( | ||
what = est.incidence, | ||
args = c( | ||
stratumDataList[[cur_stratum]], | ||
list( | ||
lambda_start = lambda_start, | ||
antigen_isos = antigen_isos, | ||
build_graph = build_graph, | ||
print_graph = print_graph, | ||
verbose = verbose, | ||
... | ||
) | ||
} | ||
} %>% system.time() -> time | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't want to lose the I understand that this was changed to avoid the right-hand assignment operator |
||
) | ||
) | ||
} | ||
|
||
|
||
if (verbose) { | ||
message("Elapsed time for loop over strata: ") | ||
|
@@ -233,7 +247,7 @@ | |
} | ||
} | ||
|
||
incidenceData <- structure( | ||
fits, | ||
antigen_isos = antigen_isos, | ||
Strata = strata_table, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# est.incidence.by() produces expected results for typhoid data | ||
|
||
Code | ||
typhoid_results | ||
Output | ||
`seroincidence.by` object estimated given the following setup: | ||
a) Antigen isotypes : HlyE_IgG, HlyE_IgA | ||
b) Strata : catchment | ||
|
||
This object is a list of `seroincidence` objects, with added meta-data attributes:`antigen_isos` - Character vector of antigen isotypes used in analysis. | ||
`Strata` - Input parameter strata of function `est.incidence.by()` | ||
|
||
Call the `summary()` function to obtain output results. | ||
|
||
--- | ||
|
||
structure(list("Stratum 1" = structure(list(minimum = 111.463434776159, | ||
estimate = -2.41983536033088, gradient = -3.74675296382939e-06, | ||
hessian = structure(15.251407603436, dim = c(1L, 1L)), code = 1L, | ||
iterations = 4L), class = c("seroincidence", "list"), lambda_start = 0.1, antigen_isos = c("HlyE_IgG", | ||
"HlyE_IgA")), "Stratum 2" = structure(list(minimum = 146.290052920951, | ||
estimate = -1.640278556419, gradient = -8.66368377467952e-08, | ||
hessian = structure(21.109488557163, dim = c(1L, 1L)), code = 1L, | ||
iterations = 5L), class = c("seroincidence", "list"), lambda_start = 0.1, antigen_isos = c("HlyE_IgG", | ||
"HlyE_IgA"))), antigen_isos = c("HlyE_IgG", "HlyE_IgA"), Strata = structure(list( | ||
Stratum = c("Stratum 1", "Stratum 2"), catchment = c("aku", | ||
"kgh"), n = c(25L, 25L)), row.names = c(NA, -2L), class = c("tbl_df", | ||
"tbl", "data.frame"), strata_vars = "catchment"), graphs_included = FALSE, class = c("seroincidence.by", | ||
"list")) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use dev version
There was a problem hiding this comment.
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