mirror of
https://github.com/msberends/AMR.git
synced 2024-12-26 07:26:13 +01:00
documentation fix
This commit is contained in:
parent
cc403169c6
commit
2ea0c93e44
@ -22,20 +22,21 @@
|
||||
#' Predict antimicrobial resistance
|
||||
#'
|
||||
#' Create a prediction model to predict antimicrobial resistance for the next years on statistical solid ground. Standard errors (SE) will be returned as columns \code{se_min} and \code{se_max}. See Examples for a real live example.
|
||||
#' @inheritParams first_isolate
|
||||
#' @inheritParams graphics::plot
|
||||
#' @param col_ab column name of \code{tbl} with antimicrobial interpretations (\code{R}, \code{I} and \code{S})
|
||||
#' @param col_ab column name of \code{x} with antimicrobial interpretations (\code{R}, \code{I} and \code{S})
|
||||
#' @param col_date column name of the date, will be used to calculate years if this column doesn't consist of years already, defaults to the first column of with a date class
|
||||
#' @param year_min lowest year to use in the prediction model, dafaults to the lowest year in \code{col_date}
|
||||
#' @param year_max highest year to use in the prediction model, defaults to 10 years after today
|
||||
#' @param year_every unit of sequence between lowest year found in the data and \code{year_max}
|
||||
#' @param minimum minimal amount of available isolates per year to include. Years containing less observations will be estimated by the model.
|
||||
#' @param model the statistical model of choice. Defaults to a generalised linear regression model with binomial distribution, assuming that a period of zero resistance was followed by a period of increasing resistance leading slowly to more and more resistance. See Details for valid options.
|
||||
#' @param I_as_R a logical to indicate whether values \code{I} should be treated as \code{R}
|
||||
#' @param I_as_S a logical to indicate whether values \code{I} should be treated as \code{S}
|
||||
#' @param preserve_measurements a logical to indicate whether predictions of years that are actually available in the data should be overwritten by the original data. The standard errors of those years will be \code{NA}.
|
||||
#' @param info a logical to indicate whether textual analysis should be printed with the name and \code{\link{summary}} of the statistical model.
|
||||
#' @param main title of the plot
|
||||
#' @param ribbon a logical to indicate whether a ribbon should be shown (default) or error bars
|
||||
#' @param ... parameters passed on to functions
|
||||
#' @inheritParams first_isolate
|
||||
#' @inheritParams graphics::plot
|
||||
#' @details Valid options for the statistical model are:
|
||||
#' \itemize{
|
||||
#' \item{\code{"binomial"} or \code{"binom"} or \code{"logit"}: a generalised linear regression model with binomial distribution}
|
||||
@ -104,7 +105,7 @@
|
||||
#' x = "Year") +
|
||||
#' theme_minimal(base_size = 13)
|
||||
#' }
|
||||
resistance_predict <- function(tbl,
|
||||
resistance_predict <- function(x,
|
||||
col_ab,
|
||||
col_date = NULL,
|
||||
year_min = NULL,
|
||||
@ -112,33 +113,46 @@ resistance_predict <- function(tbl,
|
||||
year_every = 1,
|
||||
minimum = 30,
|
||||
model = 'binomial',
|
||||
I_as_R = TRUE,
|
||||
I_as_S = TRUE,
|
||||
preserve_measurements = TRUE,
|
||||
info = TRUE) {
|
||||
info = TRUE,
|
||||
...) {
|
||||
|
||||
if (nrow(tbl) == 0) {
|
||||
if (nrow(x) == 0) {
|
||||
stop('This table does not contain any observations.')
|
||||
}
|
||||
|
||||
if (!col_ab %in% colnames(tbl)) {
|
||||
if (!col_ab %in% colnames(x)) {
|
||||
stop('Column ', col_ab, ' not found.')
|
||||
}
|
||||
|
||||
dots <- unlist(list(...))
|
||||
if (length(dots) != 0) {
|
||||
# backwards compatibility with old parameters
|
||||
dots.names <- dots %>% names()
|
||||
if ('tbl' %in% dots.names) {
|
||||
x <- dots[which(dots.names == 'tbl')]
|
||||
}
|
||||
if ('I_as_R' %in% dots.names) {
|
||||
warning("`I_as_R is deprecated - use I_as_S instead.", call. = FALSE)
|
||||
}
|
||||
}
|
||||
|
||||
# -- date
|
||||
if (is.null(col_date)) {
|
||||
col_date <- search_type_in_df(tbl = tbl, type = "date")
|
||||
col_date <- search_type_in_df(tbl = x, type = "date")
|
||||
}
|
||||
if (is.null(col_date)) {
|
||||
stop("`col_date` must be set.", call. = FALSE)
|
||||
}
|
||||
|
||||
if (!col_date %in% colnames(tbl)) {
|
||||
if (!col_date %in% colnames(x)) {
|
||||
stop('Column ', col_date, ' not found.')
|
||||
}
|
||||
|
||||
if (n_groups(tbl) > 1) {
|
||||
if (n_groups(x) > 1) {
|
||||
# no grouped tibbles please, mutate will throw errors
|
||||
tbl <- base::as.data.frame(tbl, stringsAsFactors = FALSE)
|
||||
x <- base::as.data.frame(x, stringsAsFactors = FALSE)
|
||||
}
|
||||
|
||||
year <- function(x) {
|
||||
@ -149,14 +163,15 @@ resistance_predict <- function(tbl,
|
||||
}
|
||||
}
|
||||
|
||||
df <- tbl %>%
|
||||
df <- x %>%
|
||||
mutate_at(col_ab, as.rsi) %>%
|
||||
mutate_at(col_ab, droplevels) %>%
|
||||
mutate_at(col_ab, funs(
|
||||
if (I_as_R == TRUE) {
|
||||
gsub("I", "R", .)
|
||||
} else {
|
||||
if (I_as_S == TRUE) {
|
||||
gsub("I", "S", .)
|
||||
} else {
|
||||
# then I as R
|
||||
gsub("I", "R", .)
|
||||
}
|
||||
)) %>%
|
||||
filter_at(col_ab, all_vars(!is.na(.))) %>%
|
||||
@ -289,7 +304,7 @@ resistance_predict <- function(tbl,
|
||||
structure(
|
||||
.Data = df_prediction,
|
||||
class = c("resistance_predict", "data.frame"),
|
||||
I_as_R = I_as_R,
|
||||
I_as_S = I_as_S,
|
||||
model_title = model,
|
||||
model = model_lm,
|
||||
ab = col_ab
|
||||
@ -306,10 +321,10 @@ rsi_predict <- resistance_predict
|
||||
#' @importFrom graphics plot axis arrows points
|
||||
#' @rdname resistance_predict
|
||||
plot.resistance_predict <- function(x, main = paste("Resistance prediction of", attributes(x)$ab), ...) {
|
||||
if (attributes(x)$I_as_R == TRUE) {
|
||||
ylab <- "%IR"
|
||||
} else {
|
||||
if (attributes(x)$I_as_S == TRUE) {
|
||||
ylab <- "%R"
|
||||
} else {
|
||||
ylab <- "%IR"
|
||||
}
|
||||
plot(x = x$year,
|
||||
y = x$value,
|
||||
@ -352,10 +367,10 @@ ggplot_rsi_predict <- function(x,
|
||||
stop("`x` must be a resistance prediction model created with resistance_predict().")
|
||||
}
|
||||
|
||||
if (attributes(x)$I_as_R == TRUE) {
|
||||
ylab <- "%IR"
|
||||
} else {
|
||||
if (attributes(x)$I_as_S == TRUE) {
|
||||
ylab <- "%R"
|
||||
} else {
|
||||
ylab <- "%IR"
|
||||
}
|
||||
|
||||
p <- ggplot2::ggplot(x, ggplot2::aes(x = year, y = value)) +
|
||||
|
@ -14,3 +14,6 @@ coverage:
|
||||
project: no
|
||||
patch: no
|
||||
changes: no
|
||||
|
||||
ignore:
|
||||
- "R/atc_online.R"
|
||||
|
@ -241,15 +241,15 @@
|
||||
|
||||
</div>
|
||||
|
||||
<pre class="usage"><span class='fu'>resistance_predict</span>(<span class='no'>tbl</span>, <span class='no'>col_ab</span>, <span class='kw'>col_date</span> <span class='kw'>=</span> <span class='kw'>NULL</span>, <span class='kw'>year_min</span> <span class='kw'>=</span> <span class='kw'>NULL</span>,
|
||||
<pre class="usage"><span class='fu'>resistance_predict</span>(<span class='no'>x</span>, <span class='no'>col_ab</span>, <span class='kw'>col_date</span> <span class='kw'>=</span> <span class='kw'>NULL</span>, <span class='kw'>year_min</span> <span class='kw'>=</span> <span class='kw'>NULL</span>,
|
||||
<span class='kw'>year_max</span> <span class='kw'>=</span> <span class='kw'>NULL</span>, <span class='kw'>year_every</span> <span class='kw'>=</span> <span class='fl'>1</span>, <span class='kw'>minimum</span> <span class='kw'>=</span> <span class='fl'>30</span>,
|
||||
<span class='kw'>model</span> <span class='kw'>=</span> <span class='st'>"binomial"</span>, <span class='kw'>I_as_R</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>, <span class='kw'>preserve_measurements</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>,
|
||||
<span class='kw'>info</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>)
|
||||
<span class='kw'>model</span> <span class='kw'>=</span> <span class='st'>"binomial"</span>, <span class='kw'>I_as_S</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>, <span class='kw'>preserve_measurements</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>,
|
||||
<span class='kw'>info</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>, <span class='no'>...</span>)
|
||||
|
||||
<span class='fu'>rsi_predict</span>(<span class='no'>tbl</span>, <span class='no'>col_ab</span>, <span class='kw'>col_date</span> <span class='kw'>=</span> <span class='kw'>NULL</span>, <span class='kw'>year_min</span> <span class='kw'>=</span> <span class='kw'>NULL</span>,
|
||||
<span class='fu'>rsi_predict</span>(<span class='no'>x</span>, <span class='no'>col_ab</span>, <span class='kw'>col_date</span> <span class='kw'>=</span> <span class='kw'>NULL</span>, <span class='kw'>year_min</span> <span class='kw'>=</span> <span class='kw'>NULL</span>,
|
||||
<span class='kw'>year_max</span> <span class='kw'>=</span> <span class='kw'>NULL</span>, <span class='kw'>year_every</span> <span class='kw'>=</span> <span class='fl'>1</span>, <span class='kw'>minimum</span> <span class='kw'>=</span> <span class='fl'>30</span>,
|
||||
<span class='kw'>model</span> <span class='kw'>=</span> <span class='st'>"binomial"</span>, <span class='kw'>I_as_R</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>, <span class='kw'>preserve_measurements</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>,
|
||||
<span class='kw'>info</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>)
|
||||
<span class='kw'>model</span> <span class='kw'>=</span> <span class='st'>"binomial"</span>, <span class='kw'>I_as_S</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>, <span class='kw'>preserve_measurements</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>,
|
||||
<span class='kw'>info</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>, <span class='no'>...</span>)
|
||||
|
||||
<span class='co'># S3 method for resistance_predict</span>
|
||||
<span class='fu'><a href='https://www.rdocumentation.org/packages/graphics/topics/plot'>plot</a></span>(<span class='no'>x</span>,
|
||||
@ -261,9 +261,13 @@
|
||||
<h2 class="hasAnchor" id="arguments"><a class="anchor" href="#arguments"></a>Arguments</h2>
|
||||
<table class="ref-arguments">
|
||||
<colgroup><col class="name" /><col class="desc" /></colgroup>
|
||||
<tr>
|
||||
<th>x</th>
|
||||
<td><p>a <code>data.frame</code> containing isolates.</p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>col_ab</th>
|
||||
<td><p>column name of <code>tbl</code> with antimicrobial interpretations (<code>R</code>, <code>I</code> and <code>S</code>)</p></td>
|
||||
<td><p>column name of <code>x</code> with antimicrobial interpretations (<code>R</code>, <code>I</code> and <code>S</code>)</p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>col_date</th>
|
||||
@ -290,8 +294,8 @@
|
||||
<td><p>the statistical model of choice. Defaults to a generalised linear regression model with binomial distribution, assuming that a period of zero resistance was followed by a period of increasing resistance leading slowly to more and more resistance. See Details for valid options.</p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>I_as_R</th>
|
||||
<td><p>a logical to indicate whether values <code>I</code> should be treated as <code>R</code></p></td>
|
||||
<th>I_as_S</th>
|
||||
<td><p>a logical to indicate whether values <code>I</code> should be treated as <code>S</code></p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>preserve_measurements</th>
|
||||
@ -302,17 +306,13 @@
|
||||
<td><p>a logical to indicate whether textual analysis should be printed with the name and <code><a href='https://www.rdocumentation.org/packages/base/topics/summary'>summary</a></code> of the statistical model.</p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>x</th>
|
||||
<td><p>a <code>data.frame</code> containing isolates.</p></td>
|
||||
<th>...</th>
|
||||
<td><p>parameters passed on to functions</p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>main</th>
|
||||
<td><p>title of the plot</p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>...</th>
|
||||
<td><p>parameters passed on to the <code>first_isolate</code> function</p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>ribbon</th>
|
||||
<td><p>a logical to indicate whether a ribbon should be shown (default) or error bars</p></td>
|
||||
|
@ -7,15 +7,15 @@
|
||||
\alias{ggplot_rsi_predict}
|
||||
\title{Predict antimicrobial resistance}
|
||||
\usage{
|
||||
resistance_predict(tbl, col_ab, col_date = NULL, year_min = NULL,
|
||||
resistance_predict(x, col_ab, col_date = NULL, year_min = NULL,
|
||||
year_max = NULL, year_every = 1, minimum = 30,
|
||||
model = "binomial", I_as_R = TRUE, preserve_measurements = TRUE,
|
||||
info = TRUE)
|
||||
model = "binomial", I_as_S = TRUE, preserve_measurements = TRUE,
|
||||
info = TRUE, ...)
|
||||
|
||||
rsi_predict(tbl, col_ab, col_date = NULL, year_min = NULL,
|
||||
rsi_predict(x, col_ab, col_date = NULL, year_min = NULL,
|
||||
year_max = NULL, year_every = 1, minimum = 30,
|
||||
model = "binomial", I_as_R = TRUE, preserve_measurements = TRUE,
|
||||
info = TRUE)
|
||||
model = "binomial", I_as_S = TRUE, preserve_measurements = TRUE,
|
||||
info = TRUE, ...)
|
||||
|
||||
\method{plot}{resistance_predict}(x,
|
||||
main = paste("Resistance prediction of", attributes(x)$ab), ...)
|
||||
@ -24,7 +24,9 @@ ggplot_rsi_predict(x, main = paste("Resistance prediction of",
|
||||
attributes(x)$ab), ribbon = TRUE, ...)
|
||||
}
|
||||
\arguments{
|
||||
\item{col_ab}{column name of \code{tbl} with antimicrobial interpretations (\code{R}, \code{I} and \code{S})}
|
||||
\item{x}{a \code{data.frame} containing isolates.}
|
||||
|
||||
\item{col_ab}{column name of \code{x} with antimicrobial interpretations (\code{R}, \code{I} and \code{S})}
|
||||
|
||||
\item{col_date}{column name of the date, will be used to calculate years if this column doesn't consist of years already, defaults to the first column of with a date class}
|
||||
|
||||
@ -38,18 +40,16 @@ ggplot_rsi_predict(x, main = paste("Resistance prediction of",
|
||||
|
||||
\item{model}{the statistical model of choice. Defaults to a generalised linear regression model with binomial distribution, assuming that a period of zero resistance was followed by a period of increasing resistance leading slowly to more and more resistance. See Details for valid options.}
|
||||
|
||||
\item{I_as_R}{a logical to indicate whether values \code{I} should be treated as \code{R}}
|
||||
\item{I_as_S}{a logical to indicate whether values \code{I} should be treated as \code{S}}
|
||||
|
||||
\item{preserve_measurements}{a logical to indicate whether predictions of years that are actually available in the data should be overwritten by the original data. The standard errors of those years will be \code{NA}.}
|
||||
|
||||
\item{info}{a logical to indicate whether textual analysis should be printed with the name and \code{\link{summary}} of the statistical model.}
|
||||
|
||||
\item{x}{a \code{data.frame} containing isolates.}
|
||||
\item{...}{parameters passed on to functions}
|
||||
|
||||
\item{main}{title of the plot}
|
||||
|
||||
\item{...}{parameters passed on to the \code{first_isolate} function}
|
||||
|
||||
\item{ribbon}{a logical to indicate whether a ribbon should be shown (default) or error bars}
|
||||
}
|
||||
\value{
|
||||
|
Loading…
Reference in New Issue
Block a user