new relative episode determination in `get_episode()`, fix for plotting disk/MIC values

This commit is contained in:
dr. M.S. (Matthijs) Berends 2023-02-24 17:06:30 +01:00
parent 92029c9e95
commit 2c5a9bb622
6 changed files with 220 additions and 74 deletions

View File

@ -1,5 +1,5 @@
Package: AMR
Version: 1.8.2.9144
Version: 1.8.2.9145
Date: 2023-02-24
Title: Antimicrobial Resistance Data Analysis
Description: Functions to simplify and standardise antimicrobial resistance (AMR)

View File

@ -1,4 +1,4 @@
# AMR 1.8.2.9144
# AMR 1.8.2.9145
*(this beta version will eventually become v2.0! We're happy to reach a new major milestone soon!)*
@ -105,6 +105,11 @@ We now added extensive support for antiviral agents! For the first time, the `AM
## Changes
* `get_episode()` (and its wrapper `is_new_episode()`):
* Gained an argument `new_after_days` to determine episodes based on epidemic periods
* Fix for working with `NA` values
* Fix for unsorted dates of length 2
* Now returns class `integer` instead of `numeric` since they are always whole numbers
* Argument `combine_IR` has been removed from this package (affecting functions `count_df()`, `proportion_df()`, and `sir_df()` and some plotting functions), since it was replaced with `combine_SI` three years ago
* Using `units` in `ab_ddd(..., units = "...")` had been deprecated for some time and is now not supported anymore. Use `ab_ddd_units()` instead.
* Support for `data.frame`-enhancing R packages, more specifically: `data.table::data.table`, `janitor::tabyl`, `tibble::tibble`, and `tsibble::tsibble`. AMR package functions that have a data set as output (such as `sir_df()` and `bug_drug_combinations()`), will now return the same data type as the input.
@ -134,7 +139,6 @@ We now added extensive support for antiviral agents! For the first time, the `AM
* Fix for `mo_shortname()` in case of higher taxonomic ranks (order, class, phylum)
* Cleaning columns with `as.sir()`, `as.mic()`, or `as.disk()` will now show the column name in the warning for invalid results
* Fix for using `g.test()` with zeroes in a 2x2 table
* `get_episode()` now returns class `integer` instead of `numeric` since they are always whole numbers
* `mo_synonyns()` now contains the scientific reference as names
## Other

View File

@ -27,16 +27,53 @@
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
# ==================================================================== #
#' Determine (Clinical) Episodes
#' Determine (Clinical or Epidemic) Episodes
#'
#' These functions determine which items in a vector can be considered (the start of) a new episode, based on the argument `episode_days`. This can be used to determine clinical episodes for any epidemiological analysis. The [get_episode()] function returns the index number of the episode per group, while the [is_new_episode()] function returns `TRUE` for every new [get_episode()] index, and is thus equal to `!duplicated(get_episode(...))`.
#' These functions determine which items in a vector can be considered (the start of) a new episode, based on the argument `episode_days`. This can be used to determine clinical episodes for any epidemiological analysis. The [get_episode()] function returns the index number of the episode per group, while the [is_new_episode()] function returns `TRUE` for every new [get_episode()] index. Both absolute and relative episode determination are supported.
#' @param x vector of dates (class `Date` or `POSIXt`), will be sorted internally to determine episodes
#' @param episode_days required episode length in days, can also be less than a day or `Inf`, see *Details*
#' @param episode_days episode length in days, can also be less than a day or `Inf`, see *Details*
#' @param case_free_days length in days after which new episode will start, can also be less than a day or `Inf`, see *Details*
#' @param ... ignored, only in place to allow future extensions
#' @details The functions [get_episode()] and [is_new_episode()] differ in this way when setting `episode_days` to 365:
#'
#'
#' | person_id | date | `get_episode()` | `is_new_episode()` |
#' @details Episodes can be determined in two ways: absolute and relative.
#'
#' 1. Absolute
#'
#' This method uses `episode_days` to define an episode length in days, after which a new episode will start. A common use case in AMR data analysis is microbial epidemiology: episodes of *S. aureus* bacteraemia in ICU patients for example. The episode length could then be 30 days, so that new *S. aureus* isolates after an ICU episode of 30 days will be considered a different (or new) episode.
#'
#' Thus, this method counts **since the start of the previous episode**.
#'
#' 2. Relative
#'
#' This method uses `case_free_days` to quantify the duration of (inter-epidemic) intervals, after which a new episode will start. A common use case is infectious disease epidemiology: episodes of norovirus outbreaks in a hospital for example. The case-free period could then be 14 days, so that new norovirus cases after that time will be considered a different (or new) episode.
#'
#' Thus, this methods counts **since the last case in the previous episode**.
#'
#' In a table:
#'
#' | Date | Using `episode_days = 7` | Using `case_free_days = 7` |
#' |:----------:|:------------------------:|:--------------------------:|
#' | 2023-01-01 | 1 | 1 |
#' | 2023-01-02 | 1 | 1 |
#' | 2023-01-05 | 1 | 1 |
#' | 2023-01-08 | 2\code{*} | 1 |
#' | 2023-02-21 | 3 | 2\code{**} |
#' | 2023-02-22 | 3 | 2 |
#' | 2023-02-23 | 3 | 2 |
#' | 2023-02-24 | 3 | 2 |
#' | 2023-03-01 | 4 | 2 |
#'
#' \code{*} This marks the start of a new episode, because 8 January 2023 is more than 7 days since the start of the previous episode (1 January 2023). \cr
#' \code{**} This marks the start of a new episode, because 21 January 2023 is more than 7 days since the last case in the previous episode (8 January 2023).
#'
#' ### Difference between `get_episode()` and `is_new_episode()`
#'
#' The [get_episode()] function returns the index number of the episode, so all cases/patients/isolates in the first episode will have the number 1, all cases/patients/isolates in the second episode will have the number 2, etc.
#'
#' The [is_new_episode()] function returns `TRUE` for every new [get_episode()] index, and is thus equal to `!duplicated(get_episode(...))`.
#'
#' To specify, when setting `episode_days = 365` (using method 1 as explained above), this is how the two functions differ:
#'
#' | patient | date | `get_episode()` | `is_new_episode()` |
#' |:---------:|:----------:|:---------------:|:------------------:|
#' | A | 2019-01-01 | 1 | TRUE |
#' | A | 2019-03-01 | 1 | FALSE |
@ -45,8 +82,8 @@
#' | B | 2008-01-01 | 1 | FALSE |
#' | C | 2020-01-01 | 1 | TRUE |
#'
#' Dates are first sorted from old to new. The oldest date will mark the start of the first episode. After this date, the next date will be marked that is at least `episode_days` days later than the start of the first episode. From that second marked date on, the next date will be marked that is at least `episode_days` days later than the start of the second episode which will be the start of the third episode, and so on. Before the vector is being returned, the original order will be restored.
#'
#' ### Other
#'
#' The [first_isolate()] function is a wrapper around the [is_new_episode()] function, but is more efficient for data sets containing microorganism codes or names and allows for different isolate selection methods.
#'
#' The `dplyr` package is not required for these functions to work, but these episode functions do support [variable grouping][dplyr::group_by()] and work conveniently inside `dplyr` verbs such as [`filter()`][dplyr::filter()], [`mutate()`][dplyr::mutate()] and [`summarise()`][dplyr::summarise()].
@ -57,6 +94,24 @@
#' @rdname get_episode
#' @export
#' @examples
#' # difference between absolute and relative determination of episodes:
#' x <- data.frame(dates = as.Date(c(
#' "2021-01-01",
#' "2021-01-02",
#' "2021-01-05",
#' "2021-01-08",
#' "2021-02-21",
#' "2021-02-22",
#' "2021-02-23",
#' "2021-02-24",
#' "2021-03-01",
#' "2021-03-01"
#' )))
#' x$absolute <- get_episode(x$dates, episode_days = 7)
#' x$relative <- get_episode(x$dates, case_free_days = 7)
#' x
#'
#'
#' # `example_isolates` is a data set available in the AMR package.
#' # See ?example_isolates
#' df <- example_isolates[sample(seq_len(2000), size = 100), ]
@ -140,55 +195,69 @@
#' select(group_vars(.), flag_episode)
#' }
#' }
get_episode <- function(x, episode_days, ...) {
get_episode <- function(x, episode_days = NULL, case_free_days = NULL, ...) {
meet_criteria(x, allow_class = c("Date", "POSIXt"), allow_NA = TRUE)
meet_criteria(episode_days, allow_class = c("numeric", "integer"), has_length = 1, is_positive = TRUE, is_finite = FALSE)
as.integer(exec_episode(x, episode_days, ...))
meet_criteria(episode_days, allow_class = c("numeric", "integer"), has_length = 1, is_positive = TRUE, is_finite = FALSE, allow_NULL = TRUE)
meet_criteria(case_free_days, allow_class = c("numeric", "integer"), has_length = 1, is_positive = TRUE, is_finite = FALSE, allow_NULL = TRUE)
as.integer(exec_episode(x, episode_days, case_free_days, ...))
}
#' @rdname get_episode
#' @export
is_new_episode <- function(x, episode_days, ...) {
is_new_episode <- function(x, episode_days = NULL, case_free_days = NULL, ...) {
meet_criteria(x, allow_class = c("Date", "POSIXt"), allow_NA = TRUE)
meet_criteria(episode_days, allow_class = c("numeric", "integer"), has_length = 1, is_positive = TRUE, is_finite = FALSE)
!duplicated(exec_episode(x, episode_days, ...))
meet_criteria(episode_days, allow_class = c("numeric", "integer"), has_length = 1, is_positive = TRUE, is_finite = FALSE, allow_NULL = TRUE)
meet_criteria(case_free_days, allow_class = c("numeric", "integer"), has_length = 1, is_positive = TRUE, is_finite = FALSE, allow_NULL = TRUE)
!duplicated(exec_episode(x, episode_days, case_free_days, ...))
}
exec_episode <- function(x, episode_days, ...) {
x <- as.double(as.POSIXct(x)) # as.POSIXct() required for Date classes
exec_episode <- function(x, episode_days, case_free_days, ...) {
stop_if((is.null(episode_days) && is.null(case_free_days)) || (!is.null(episode_days) && !is.null(case_free_days)),
"either `episode_days` or `case_free_days` must be set.", call = -2)
# since x is now in seconds, get seconds from episode_days as well
episode_seconds <- episode_days * 60 * 60 * 24
x <- as.double(as.POSIXct(x)) # as.POSIXct() required for Date classes
if (length(x) == 1) { # this will also match 1 NA, which is fine
return(1)
} else if (length(x) == 2 && !all(is.na(x))) {
if (max(x) - min(x) >= episode_seconds) {
return(c(1, 2))
} else {
return(c(1, 1))
}
}
# since x is now in seconds, get seconds from episode_days as well
episode_seconds <- episode_days * 60 * 60 * 24
case_free_seconds <- case_free_days * 60 * 60 * 24
# we asked on StackOverflow:
# https://stackoverflow.com/questions/42122245/filter-one-row-every-year
run_episodes <- function(x, episode_seconds) {
indices <- integer()
start <- x[1]
ind <- 1
indices[1] <- 1
for (i in 2:length(x)) {
if (isTRUE((x[i] - start) >= episode_seconds)) {
ind <- ind + 1
start <- x[i]
if (length(x) == 1) { # this will also match 1 NA, which is fine
return(1)
} else if (length(x) == 2 && all(!is.na(x))) {
if ((length(episode_seconds) > 0 && (max(x) - min(x)) >= episode_seconds) ||
(length(case_free_seconds) > 0 && (max(x) - min(x)) >= case_free_seconds)) {
if (x[1] <= x[2]) {
return(c(1, 2))
} else {
return(c(2, 1))
}
} else {
return(c(1, 1))
}
indices[i] <- ind
}
indices
}
ord <- order(x)
out <- run_episodes(x[ord], episode_seconds)[order(ord)]
out[is.na(x) & ord != 1] <- NA # every NA expect for the first must remain NA
out
run_episodes <- function(x, episode_seconds, case_free) {
NAs <- which(is.na(x))
x[NAs] <- 0
indices <- integer(length = length(x))
start <- x[1]
ind <- 1
indices[ind] <- 1
for (i in 2:length(x)) {
if ((length(episode_seconds) > 0 && (x[i] - start) >= episode_seconds) ||
(length(case_free_seconds) > 0 && (x[i] - x[i - 1]) >= case_free_seconds)) {
ind <- ind + 1
start <- x[i]
}
indices[i] <- ind
}
indices[NAs] <- NA
indices
}
ord <- order(x)
out <- run_episodes(x[ord], episode_seconds, case_free_seconds)[order(ord)]
out[is.na(x) & ord != 1] <- NA # every NA expect for the first must remain NA
out
}

View File

@ -127,6 +127,7 @@ plot.mic <- function(x,
colours_SIR = colours_SIR,
fn = as.mic,
language = language,
type = "MIC",
...
)
barplot(x,
@ -267,6 +268,7 @@ autoplot.mic <- function(object,
colours_SIR = colours_SIR,
fn = as.mic,
language = language,
type = "MIC",
...
)
df <- as.data.frame(x, stringsAsFactors = TRUE)
@ -368,6 +370,7 @@ plot.disk <- function(x,
colours_SIR = colours_SIR,
fn = as.disk,
language = language,
type = "disk",
...
)
@ -508,6 +511,7 @@ autoplot.disk <- function(object,
colours_SIR = colours_SIR,
fn = as.disk,
language = language,
type = "disk",
...
)
df <- as.data.frame(x, stringsAsFactors = TRUE)
@ -778,33 +782,44 @@ plot_name_of_I <- function(guideline) {
}
}
plot_colours_subtitle_guideline <- function(x, mo, ab, guideline, colours_SIR, fn, language, ...) {
plot_colours_subtitle_guideline <- function(x, mo, ab, guideline, colours_SIR, fn, language, type, ...) {
guideline <- get_guideline(guideline, AMR::clinical_breakpoints)
if (!is.null(mo) && !is.null(ab)) {
# interpret and give colour based on MIC values
mo <- as.mo(mo)
ab <- as.ab(ab)
sir <- suppressWarnings(suppressMessages(as.sir(fn(names(x)), mo = mo, ab = ab, guideline = guideline, ...)))
cols <- character(length = length(sir))
cols[is.na(sir)] <- "#BEBEBE"
cols[sir == "S"] <- colours_SIR[1]
cols[sir == "I"] <- colours_SIR[2]
cols[sir == "R"] <- colours_SIR[3]
moname <- mo_name(mo, language = language)
ab <- as.ab(ab)
abname <- ab_name(ab, language = language)
if (all(cols == "#BEBEBE")) {
message_(
"No ", guideline, " interpretations found for ",
ab_name(ab, language = NULL, tolower = TRUE), " in ", moname
)
guideline_txt <- ""
sir <- suppressWarnings(suppressMessages(as.sir(fn(names(x)), mo = mo, ab = ab, guideline = guideline, include_screening = FALSE, include_PKPD = TRUE, ...)))
guideline_txt <- guideline
if (all(is.na(sir))) {
sir_screening <- suppressWarnings(suppressMessages(as.sir(fn(names(x)), mo = mo, ab = ab, guideline = guideline, include_screening = TRUE, include_PKPD = TRUE, ...)))
if (!all(is.na(sir_screening))) {
message_(
"Only ", guideline, " ", type, " interpretations found for ",
ab_name(ab, language = NULL, tolower = TRUE), " in ", italicise(moname), " for screening"
)
sir <- sir_screening
guideline_txt <- paste0("(Screen, ", guideline_txt, ")")
} else {
message_(
"No ", guideline, " ", type, " interpretations found for ",
ab_name(ab, language = NULL, tolower = TRUE), " in ", italicise(moname)
)
guideline_txt <- ""
}
} else {
guideline_txt <- guideline
if (isTRUE(list(...)$uti)) {
guideline_txt <- paste("UTIs,", guideline_txt)
}
guideline_txt <- paste0("(", guideline_txt, ")")
}
cols <- character(length = length(sir))
cols[is.na(sir)] <- "#BEBEBE"
cols[sir == "S"] <- colours_SIR[1]
cols[sir == "I"] <- colours_SIR[2]
cols[sir == "R"] <- colours_SIR[3]
sub <- bquote(.(abname) ~ "-" ~ italic(.(moname)) ~ .(guideline_txt))
} else {
cols <- "#BEBEBE"

View File

@ -95,8 +95,8 @@ new_mo_codes <- breakpoints %>%
new_mo_codes %>%
mutate(code = toupper(ORGANISM_CODE)) %>%
rename(mo_new = mo) %>%
left_join(microorganisms.codes) %>%
filter(mo != mo_new)
left_join(microorganisms.codes %>% rename(mo_old = mo)) %>%
filter(mo_old != mo_new)
microorganisms.codes <- microorganisms.codes %>%
filter(!code %in% toupper(new_mo_codes$ORGANISM_CODE)) %>%

View File

@ -3,16 +3,18 @@
\name{get_episode}
\alias{get_episode}
\alias{is_new_episode}
\title{Determine (Clinical) Episodes}
\title{Determine (Clinical or Epidemic) Episodes}
\usage{
get_episode(x, episode_days, ...)
get_episode(x, episode_days = NULL, case_free_days = NULL, ...)
is_new_episode(x, episode_days, ...)
is_new_episode(x, episode_days = NULL, case_free_days = NULL, ...)
}
\arguments{
\item{x}{vector of dates (class \code{Date} or \code{POSIXt}), will be sorted internally to determine episodes}
\item{episode_days}{required episode length in days, can also be less than a day or \code{Inf}, see \emph{Details}}
\item{episode_days}{episode length in days, can also be less than a day or \code{Inf}, see \emph{Details}}
\item{case_free_days}{length in days after which new episode will start, can also be less than a day or \code{Inf}, see \emph{Details}}
\item{...}{ignored, only in place to allow future extensions}
}
@ -23,11 +25,47 @@ is_new_episode(x, episode_days, ...)
}
}
\description{
These functions determine which items in a vector can be considered (the start of) a new episode, based on the argument \code{episode_days}. This can be used to determine clinical episodes for any epidemiological analysis. The \code{\link[=get_episode]{get_episode()}} function returns the index number of the episode per group, while the \code{\link[=is_new_episode]{is_new_episode()}} function returns \code{TRUE} for every new \code{\link[=get_episode]{get_episode()}} index, and is thus equal to \code{!duplicated(get_episode(...))}.
These functions determine which items in a vector can be considered (the start of) a new episode, based on the argument \code{episode_days}. This can be used to determine clinical episodes for any epidemiological analysis. The \code{\link[=get_episode]{get_episode()}} function returns the index number of the episode per group, while the \code{\link[=is_new_episode]{is_new_episode()}} function returns \code{TRUE} for every new \code{\link[=get_episode]{get_episode()}} index. Both absolute and relative episode determination are supported.
}
\details{
The functions \code{\link[=get_episode]{get_episode()}} and \code{\link[=is_new_episode]{is_new_episode()}} differ in this way when setting \code{episode_days} to 365:\tabular{cccc}{
person_id \tab date \tab \code{get_episode()} \tab \code{is_new_episode()} \cr
Episodes can be determined in two ways: absolute and relative.
\enumerate{
\item Absolute
This method uses \code{episode_days} to define an episode length in days, after which a new episode will start. A common use case in AMR data analysis is microbial epidemiology: episodes of \emph{S. aureus} bacteraemia in ICU patients for example. The episode length could then be 30 days, so that new \emph{S. aureus} isolates after an ICU episode of 30 days will be considered a different (or new) episode.
Thus, this method counts \strong{since the start of the previous episode}.
\item Relative
This method uses \code{case_free_days} to quantify the duration of (inter-epidemic) intervals, after which a new episode will start. A common use case is infectious disease epidemiology: episodes of norovirus outbreaks in a hospital for example. The case-free period could then be 14 days, so that new norovirus cases after that time will be considered a different (or new) episode.
Thus, this methods counts \strong{since the last case in the previous episode}.
}
In a table:\tabular{ccc}{
Date \tab Using \code{episode_days = 7} \tab Using \code{case_free_days = 7} \cr
2023-01-01 \tab 1 \tab 1 \cr
2023-01-02 \tab 1 \tab 1 \cr
2023-01-05 \tab 1 \tab 1 \cr
2023-01-08 \tab 2\code{*} \tab 1 \cr
2023-02-21 \tab 3 \tab 2\code{**} \cr
2023-02-22 \tab 3 \tab 2 \cr
2023-02-23 \tab 3 \tab 2 \cr
2023-02-24 \tab 3 \tab 2 \cr
2023-03-01 \tab 4 \tab 2 \cr
}
\code{*} This marks the start of a new episode, because 8 January 2023 is more than 7 days since the start of the previous episode (1 January 2023). \cr
\code{**} This marks the start of a new episode, because 21 January 2023 is more than 7 days since the last case in the previous episode (8 January 2023).
\subsection{Difference between \code{get_episode()} and \code{is_new_episode()}}{
The \code{\link[=get_episode]{get_episode()}} function returns the index number of the episode, so all cases/patients/isolates in the first episode will have the number 1, all cases/patients/isolates in the second episode will have the number 2, etc.
The \code{\link[=is_new_episode]{is_new_episode()}} function returns \code{TRUE} for every new \code{\link[=get_episode]{get_episode()}} index, and is thus equal to \code{!duplicated(get_episode(...))}.
To specify, when setting \code{episode_days = 365} (using method 1 as explained above), this is how the two functions differ:\tabular{cccc}{
patient \tab date \tab \code{get_episode()} \tab \code{is_new_episode()} \cr
A \tab 2019-01-01 \tab 1 \tab TRUE \cr
A \tab 2019-03-01 \tab 1 \tab FALSE \cr
A \tab 2021-01-01 \tab 2 \tab TRUE \cr
@ -36,14 +74,34 @@ The functions \code{\link[=get_episode]{get_episode()}} and \code{\link[=is_new_
C \tab 2020-01-01 \tab 1 \tab TRUE \cr
}
}
Dates are first sorted from old to new. The oldest date will mark the start of the first episode. After this date, the next date will be marked that is at least \code{episode_days} days later than the start of the first episode. From that second marked date on, the next date will be marked that is at least \code{episode_days} days later than the start of the second episode which will be the start of the third episode, and so on. Before the vector is being returned, the original order will be restored.
\subsection{Other}{
The \code{\link[=first_isolate]{first_isolate()}} function is a wrapper around the \code{\link[=is_new_episode]{is_new_episode()}} function, but is more efficient for data sets containing microorganism codes or names and allows for different isolate selection methods.
The \code{dplyr} package is not required for these functions to work, but these episode functions do support \link[dplyr:group_by]{variable grouping} and work conveniently inside \code{dplyr} verbs such as \code{\link[dplyr:filter]{filter()}}, \code{\link[dplyr:mutate]{mutate()}} and \code{\link[dplyr:summarise]{summarise()}}.
}
}
\examples{
# difference between absolute and relative determination of episodes:
x <- data.frame(dates = as.Date(c(
"2021-01-01",
"2021-01-02",
"2021-01-05",
"2021-01-08",
"2021-02-21",
"2021-02-22",
"2021-02-23",
"2021-02-24",
"2021-03-01",
"2021-03-01"
)))
x$absolute <- get_episode(x$dates, episode_days = 7)
x$relative <- get_episode(x$dates, case_free_days = 7)
x
# `example_isolates` is a data set available in the AMR package.
# See ?example_isolates
df <- example_isolates[sample(seq_len(2000), size = 100), ]