Skip to content

Commit

Permalink
Merge branch 'main' into create-as_noise_params
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisorwa authored Oct 1, 2024
2 parents ce79a66 + 402b829 commit 06f01c3
Show file tree
Hide file tree
Showing 17 changed files with 843 additions and 330 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ allpopsamples_hlye.csv$
^CRAN-SUBMISSION$
^README\.qmd$
^codecov\.yml$
^\.lintr$
2 changes: 2 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
linters: linters_with_defaults() # see vignette("lintr")
encoding: "UTF-8"
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: serocalculator
Title: Estimating Infection Rates from Serological Data
Version: 1.2.0.9009
Version: 1.2.0.9010
Authors@R: c(
person("Peter", "Teunis", , "[email protected]", role = c("aut", "cph"),
comment = "Author of the method and original code."),
Expand Down Expand Up @@ -49,6 +49,7 @@ Suggests:
readr,
rmarkdown,
spelling,
ssdtools (>= 1.0.6.9016),
testthat (>= 3.0.0),
tidyverse
LinkingTo:
Expand All @@ -60,3 +61,5 @@ LazyData: true
NeedsCompilation: no
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Remotes:
bcgov/ssdtools
13 changes: 0 additions & 13 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,10 @@ S3method(autoplot,pop_data)
S3method(autoplot,seroincidence)
S3method(autoplot,seroincidence.by)
S3method(autoplot,summary.seroincidence.by)
S3method(get_age,pop_data)
S3method(get_age_var,pop_data)
S3method(get_biomarker_levels,pop_data)
S3method(get_biomarker_names,pop_data)
S3method(get_biomarker_names_var,pop_data)
S3method(get_id,pop_data)
S3method(get_id_var,pop_data)
S3method(get_value,pop_data)
S3method(get_value_var,pop_data)
S3method(print,seroincidence)
S3method(print,seroincidence.by)
S3method(print,summary.pop_data)
S3method(print,summary.seroincidence.by)
S3method(set_age,pop_data)
S3method(set_biomarker_var,pop_data)
S3method(set_id,pop_data)
S3method(set_value,pop_data)
S3method(strata,seroincidence.by)
S3method(summary,pop_data)
S3method(summary,seroincidence)
Expand Down
11 changes: 8 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@

* Add `as_noise_params`

# serocalculator 1.2.9.9007
* Updated enteric fever example article with upgraded code and visualizations

## New features

* Updated enteric fever example article with upgraded code and visualizations (#290)

* Added template for reporting Issues
(from `usethis::use_tidy_issue_template()`) (#270)

* Added template for pull requests
(from <https://github.com/bcgov/ssdtools>) (#265)

## Developer-facing changes

* initialized [`lintr`](https://lintr.r-lib.org/) with `lintr::use_lint()` (#278)

* created unit test for `df_to_array()` (#276)

* fixed `dplyr::select()` deprecation warning in `df_to_array()` (#276)

* Generalized `get_()` and `set_()` methods to be general-purpose
(no S3 class-specific methods needed yet) (#274).

* Updated GitHub Action files and reformatted `DESCRIPTION` (#268)

* Added `.gitattributes` file (<https://git-scm.com/docs/gitattributes>)
Expand Down
26 changes: 16 additions & 10 deletions R/as_curve_params.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#' Load antibody decay curve parameter
#'
#' @param data a [data.frame()] or [tibble::tbl_df]
#' @param antigen_isos [character()] vector of antigen isotypes to be used in analyses
#' @returns a `curve_data` object (a [tibble::tbl_df] with extra attribute `antigen_isos`)
#' @param antigen_isos a [character()] vector of antigen isotypes
#' to be used in analyses
#' @returns a `curve_data` object
#' (a [tibble::tbl_df] with extra attribute `antigen_isos`)
#' @export
#' @examples
#' library(magrittr)
Expand All @@ -14,8 +16,7 @@
#' print(curve_data)
as_curve_params <- function(data, antigen_isos = NULL) {

if(!is.data.frame(data))
{
if (!is.data.frame(data)) {
cli::cli_abort(
class = "not data.frame",
message = c(
Expand All @@ -31,16 +32,18 @@ as_curve_params <- function(data, antigen_isos = NULL) {
data %>%
tibble::as_tibble()

# check if object has expected columns:

# define curve columns
curve_cols <- c("antigen_iso", "y0", "y1", "t1", "alpha", "r")

# check if object is curve (with columns)
if (!all(is.element(curve_cols, curve_data %>% names()))) {
# get columns from provided data
data_cols <- data %>% names()
# get columns from provided data
data_cols <- data %>% names()

# get any missing column(s)
missing_cols <- setdiff(x = curve_cols, y = data_cols)

# get any missing column(s)
missing_cols <- setdiff(x = curve_cols, y = data_cols)
if (length(missing_cols) > 0) {

cli::cli_abort(
class = "not curve_params",
Expand All @@ -67,5 +70,8 @@ as_curve_params <- function(data, antigen_isos = NULL) {
# assign antigen attribute
attr(curve_data, "antigen_isos") <- antigen_isos

curve_data <- curve_data %>%
set_biomarker_var(biomarker = "antigen_iso", standardize = FALSE)

return(curve_data)
}
191 changes: 191 additions & 0 deletions R/class_attributes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
get_age_var <- function(object, ...) {
age_var <- attributes(object)$age_var
return(age_var)
}

get_age <- function(object, ...) {
age_var <- object %>% get_age_var()
age_data <- object %>% pull(age_var)
return(age_data)
}

get_value_var <- function(object, ...) {
value_var <- attributes(object)$value_var
return(value_var)
}

get_value <- function(object, ...) {
value_var_name <- object %>% get_value_var()
value_data <- object %>% pull(value_var_name)
return(value_data)
}

get_id_var <- function(object, ...) {
id_var <- attributes(object)$id_var
return(id_var)
}

get_id <- function(object, ...) {
id_var_name <- object %>% get_id_var()
id_data <- object %>% pull(id_var_name)
return(id_data)
}

get_biomarker_levels <- function(object, ...) {
attr(object, "antigen_isos")
}

get_biomarker_names_var <- function(object, ...) {
# get value attribute
biomarker_var <- attributes(object)[["biomarker_var"]]

return(biomarker_var)
}

get_biomarker_names <- function(object, ...) {
# get biomarker name data
biomarker_names_var <- get_biomarker_names_var(object)
biomarker_data <- object %>% pull(biomarker_names_var)

return(biomarker_data)
}


set_age <- function(object,
age = "Age",
standardize = TRUE,
...) {
# check if age column exists
if (age %in% colnames(object)) {
attr(object, "age_var") <- age
} else {
cli::cli_warn(class = "missing variable",
'The specified `age` column "{age}" does not exist.')

# search age variable from object
age_var <-
grep(
x = colnames(object),
value = TRUE,
pattern = age,
ignore.case = TRUE
)

if (length(age_var) == 1) {
attr(object, "age_var") <- age_var

# create warning when using searched age instead of provided age
cli::cli_inform('Proceeding to use "{.var {age_var}}"')
} else if (length(age_var) == 0) {
cli::cli_abort("No similar column name was detected.")
} else if (length(age_var) > 1) {
cli::cli_warn("Multiple potential matches found: {.var {age_var}}")
cli::cli_warn("Using first match: {.var {age_var[1]}}")
attr(object, "age_var") <- age_var[1]
} else {
cli::cli_abort("{.code length(age_var)} = {.val {length(age_var)}}")
}
}

if (standardize) {
object <- object %>%
rename(c("age" = attr(object, "age_var")))

# set age attribute
attr(object, "age_var") <- "age"
}

return(object)
}


set_value <- function(object,
value = "result",
standardize = TRUE,
...) {
# check if value column exists
if (value %in% colnames(object)) {
attr(object, "value_var") <- value
} else {
cli::cli_warn('The specified `value` column "{.var {value}}"
does not exist.')

# search value variable from pop_data
value_var <-
grep(
x = colnames(object),
value = TRUE,
pattern = value,
ignore.case = TRUE
)

if (length(value_var) == 1) {
attr(object, "value_var") <- value_var

# create warning when using searched age instead of provided age
cli::cli_inform('Proceeding to use "{.var {value_var}}"')
} else if (length(value_var) == 0) {
cli::cli_abort("No similar column name was detected.")
} else {
# i.e. if (length(value_var) > 1)
cli::cli_warn("Multiple potential matches found: {.var {value_var}}")
cli::cli_inform("Using first match: {.var {value_var[1]}}")
attr(object, "value_var") <- value_var[1]
}
}

if (standardize) {
object <- object %>%
rename(c("value" = attr(object, "value_var")))

# set id attribute
attr(object, "value_var") <- "value"
}

return(object)
}

set_id <- function(object,
id = "index_id",
standardize = TRUE,
...) {
# check if id column exists
if (id %in% colnames(object)) {
attr(object, "id_var") <- id
} else {
cli::cli_warn("The specified {.var id} column {.val {id}} does not exist.")

# search id variable from object
id_var <-
grep(
x = colnames(object),
value = TRUE,
pattern = id,
ignore.case = TRUE
)

if (length(id_var) == 1) {
attr(object, "id_var") <- id_var

# create warning when using searched id instead of provided id
cli::cli_inform('Proceeding to use "{id_var}"')
} else if (length(id_var) == 0) {
cli::cli_abort("No similar column name was detected.")
} else {
# if (length(id_var) > 1)
cli::cli_warn("Multiple potential matches found: {.var {id_var}}")
cli::cli_inform("Using first match: {.var {id_var[1]}}")
attr(object, "id_var") <- id_var[1]
}
}

if (standardize) {
object <- object %>%
rename(c("id" = attr(object, "id_var")))

# set id attribute
attr(object, "id_var") <- "id"
}

return(object)
}
Loading

0 comments on commit 06f01c3

Please sign in to comment.