1
0
mirror of https://github.com/msberends/AMR.git synced 2025-07-09 19:01:51 +02:00

(v1.4.0.9046) get_episode

This commit is contained in:
2020-12-27 00:07:00 +01:00
parent 291f802be3
commit acbd0cf7ca
99 changed files with 1143 additions and 683 deletions

View File

@ -1,30 +1,36 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/is_new_episode.R
\name{is_new_episode}
% Please edit documentation in R/episode.R
\name{get_episode}
\alias{get_episode}
\alias{is_new_episode}
\title{Determine (new) episodes for patients}
\usage{
is_new_episode(x, episode_days = 365, ...)
get_episode(x, episode_days, ...)
is_new_episode(x, episode_days, ...)
}
\arguments{
\item{x}{vector of dates (class \code{Date} or \code{POSIXt})}
\item{episode_days}{length of the required episode in days, defaults to 365. Every element in the input will return \code{TRUE} after this number of days has passed since the last included date, independent of calendar years. Please see \emph{Details}.}
\item{episode_days}{length of the required episode in days, please see \emph{Details}}
\item{...}{arguments passed on to \code{\link[=as.Date]{as.Date()}}}
}
\value{
a \link{logical} vector
\itemize{
\item \code{\link[=get_episode]{get_episode()}}: a \link{double} vector
\item \code{\link[=is_new_episode]{is_new_episode()}}: a \link{logical} vector
}
}
\description{
This function determines 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.
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.
}
\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 more efficient for data sets containing microorganism codes or names.
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.
The \code{dplyr} package is not required for this function to work, but this function works 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 functions 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()}}.
}
\section{Stable lifecycle}{
@ -43,8 +49,12 @@ On our website \url{https://msberends.github.io/AMR/} you can find \href{https:/
# `example_isolates` is a dataset available in the AMR package.
# See ?example_isolates.
is_new_episode(example_isolates$date)
get_episode(example_isolates$date, episode_days = 60)
is_new_episode(example_isolates$date, episode_days = 60)
# filter on results from the third 60-day episode using base R
example_isolates[which(get_episode(example_isolates$date, 60) == 3), ]
\donttest{
if (require("dplyr")) {
# is_new_episode() can also be used in dplyr verbs to determine patient
@ -54,7 +64,15 @@ if (require("dplyr")) {
size = 2000,
replace = TRUE)) \%>\%
group_by(condition) \%>\%
mutate(new_episode = is_new_episode(date))
mutate(new_episode = is_new_episode(date, 365))
example_isolates \%>\%
group_by(hospital_id, patient_id) \%>\%
transmute(date,
patient_id,
new_index = get_episode(date, 60),
new_logical = is_new_episode(date, 60))
example_isolates \%>\%
group_by(hospital_id) \%>\%
@ -71,7 +89,7 @@ if (require("dplyr")) {
y <- example_isolates \%>\%
group_by(patient_id, mo) \%>\%
filter(is_new_episode(date))
filter(is_new_episode(date, 365))
identical(x$patient_id, y$patient_id)
@ -79,7 +97,10 @@ if (require("dplyr")) {
# since you can now group on anything that seems relevant:
example_isolates \%>\%
group_by(patient_id, mo, hospital_id, ward_icu) \%>\%
mutate(flag_episode = is_new_episode(date))
mutate(flag_episode = is_new_episode(date, 365))
}
}
}
\seealso{
\code{\link[=first_isolate]{first_isolate()}}
}