mirror of
https://github.com/msberends/AMR.git
synced 2025-07-13 06:01:53 +02:00
use dplyr where available, new antibiogram()
for WISCA, fixed Salmonella Typhi/Paratyphi
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/episode.R
|
||||
% Please edit documentation in R/get_episode.R
|
||||
\name{get_episode}
|
||||
\alias{get_episode}
|
||||
\alias{is_new_episode}
|
||||
@ -23,19 +23,19 @@ 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 values \code{TRUE}/\code{FALSE} to indicate whether an item in a vector is the start of a new 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 values \code{TRUE}/\code{FALSE} for where \code{\link[=get_episode]{get_episode()}} returns 1, and is thus equal to \code{get_episode(...) == 1}.
|
||||
}
|
||||
\details{
|
||||
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.
|
||||
|
||||
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 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()}}.
|
||||
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{
|
||||
# `example_isolates` is a data set available in the AMR package.
|
||||
# See ?example_isolates
|
||||
df <- example_isolates[sample(seq_len(2000), size = 200), ]
|
||||
df <- example_isolates[sample(seq_len(2000), size = 100), ]
|
||||
|
||||
get_episode(df$date, episode_days = 60) # indices
|
||||
is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
|
||||
@ -44,13 +44,9 @@ is_new_episode(df$date, episode_days = 60) # TRUE/FALSE
|
||||
df[which(get_episode(df$date, 60) == 3), ]
|
||||
|
||||
# the functions also work for less than a day, e.g. to include one per hour:
|
||||
get_episode(
|
||||
c(
|
||||
Sys.time(),
|
||||
Sys.time() + 60 * 60
|
||||
),
|
||||
episode_days = 1 / 24
|
||||
)
|
||||
get_episode(c(Sys.time(),
|
||||
Sys.time() + 60 * 60),
|
||||
episode_days = 1 / 24)
|
||||
|
||||
\donttest{
|
||||
if (require("dplyr")) {
|
||||
@ -66,6 +62,7 @@ if (require("dplyr")) {
|
||||
mutate(new_episode = is_new_episode(date, 365)) \%>\%
|
||||
select(patient, date, condition, new_episode)
|
||||
}
|
||||
|
||||
if (require("dplyr")) {
|
||||
df \%>\%
|
||||
group_by(ward, patient) \%>\%
|
||||
@ -75,6 +72,7 @@ if (require("dplyr")) {
|
||||
new_logical = is_new_episode(date, 60)
|
||||
)
|
||||
}
|
||||
|
||||
if (require("dplyr")) {
|
||||
df \%>\%
|
||||
group_by(ward) \%>\%
|
||||
@ -85,25 +83,10 @@ if (require("dplyr")) {
|
||||
n_episodes_30 = sum(is_new_episode(date, episode_days = 30))
|
||||
)
|
||||
}
|
||||
if (require("dplyr")) {
|
||||
# grouping on patients and microorganisms leads to the same
|
||||
# results as first_isolate() when using 'episode-based':
|
||||
x <- df \%>\%
|
||||
filter_first_isolate(
|
||||
include_unknown = TRUE,
|
||||
method = "episode-based"
|
||||
)
|
||||
|
||||
y <- df \%>\%
|
||||
group_by(patient, mo) \%>\%
|
||||
filter(is_new_episode(date, 365)) \%>\%
|
||||
ungroup()
|
||||
|
||||
identical(x, y)
|
||||
}
|
||||
if (require("dplyr")) {
|
||||
# but is_new_episode() has a lot more flexibility than first_isolate(),
|
||||
# since you can now group on anything that seems relevant:
|
||||
# is_new_episode() has a lot more flexibility than first_isolate(),
|
||||
# since you can group on anything that seems relevant:
|
||||
df \%>\%
|
||||
group_by(patient, mo, ward) \%>\%
|
||||
mutate(flag_episode = is_new_episode(date, 365)) \%>\%
|
||||
|
Reference in New Issue
Block a user