1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-26 17:26:12 +01:00

small fixes

This commit is contained in:
dr. M.S. (Matthijs) Berends 2018-07-28 09:34:03 +02:00
parent 498e88b5cf
commit feab1cad6b
7 changed files with 37 additions and 31 deletions

View File

@ -1,6 +1,6 @@
Package: AMR Package: AMR
Version: 0.2.0.9017 Version: 0.2.0.9017
Date: 2018-07-25 Date: 2018-07-28
Title: Antimicrobial Resistance Analysis Title: Antimicrobial Resistance Analysis
Authors@R: c( Authors@R: c(
person( person(

View File

@ -111,6 +111,7 @@ importFrom(dplyr,arrange)
importFrom(dplyr,arrange_at) importFrom(dplyr,arrange_at)
importFrom(dplyr,as_tibble) importFrom(dplyr,as_tibble)
importFrom(dplyr,between) importFrom(dplyr,between)
importFrom(dplyr,case_when)
importFrom(dplyr,desc) importFrom(dplyr,desc)
importFrom(dplyr,filter) importFrom(dplyr,filter)
importFrom(dplyr,filter_at) importFrom(dplyr,filter_at)

View File

@ -60,6 +60,7 @@ globalVariables(c('abname',
'septic_patients', 'septic_patients',
'species', 'species',
'umcg', 'umcg',
'value',
'values', 'values',
'View', 'View',
'y', 'y',

View File

@ -429,7 +429,7 @@ rsi_df <- function(tbl,
#' @return \code{data.frame} with columns: #' @return \code{data.frame} with columns:
#' \itemize{ #' \itemize{
#' \item{\code{year}} #' \item{\code{year}}
#' \item{\code{resistance}, the same as \code{estimated} when \code{preserve_measurements = FALSE}, and a combination of \code{observed} and \code{estimated} otherwise} #' \item{\code{value}, the same as \code{estimated} when \code{preserve_measurements = FALSE}, and a combination of \code{observed} and \code{estimated} otherwise}
#' \item{\code{se_min}, the lower bound of the standard error with a minimum of \code{0}} #' \item{\code{se_min}, the lower bound of the standard error with a minimum of \code{0}}
#' \item{\code{se_max} the upper bound of the standard error with a maximum of \code{1}} #' \item{\code{se_max} the upper bound of the standard error with a maximum of \code{1}}
#' \item{\code{observations}, the total number of observations, i.e. S + I + R} #' \item{\code{observations}, the total number of observations, i.e. S + I + R}
@ -440,7 +440,7 @@ rsi_df <- function(tbl,
#' @rdname resistance_predict #' @rdname resistance_predict
#' @export #' @export
#' @importFrom stats predict glm lm #' @importFrom stats predict glm lm
#' @importFrom dplyr %>% pull mutate group_by_at summarise filter n_distinct arrange #' @importFrom dplyr %>% pull mutate group_by_at summarise filter n_distinct arrange case_when
# @importFrom tidyr spread # @importFrom tidyr spread
#' @examples #' @examples
#' \dontrun{ #' \dontrun{
@ -493,7 +493,7 @@ rsi_df <- function(tbl,
#' #'
#' ggplot(data, #' ggplot(data,
#' aes(x = year)) + #' aes(x = year)) +
#' geom_col(aes(y = resistance), #' geom_col(aes(y = value),
#' fill = "grey75") + #' fill = "grey75") +
#' geom_errorbar(aes(ymin = se_min, #' geom_errorbar(aes(ymin = se_min,
#' ymax = se_max), #' ymax = se_max),
@ -626,13 +626,13 @@ resistance_predict <- function(tbl,
} }
# prepare the output dataframe # prepare the output dataframe
prediction <- data.frame(year = years_predict, resistance = prediction, stringsAsFactors = FALSE) prediction <- data.frame(year = years_predict, value = prediction, stringsAsFactors = FALSE)
prediction$se_min <- prediction$resistance - se prediction$se_min <- prediction$value - se
prediction$se_max <- prediction$resistance + se prediction$se_max <- prediction$value + se
if (model == 'loglin') { if (model == 'loglin') {
prediction$resistance <- prediction$resistance %>% prediction$value <- prediction$value %>%
format(scientific = FALSE) %>% format(scientific = FALSE) %>%
as.integer() as.integer()
prediction$se_min <- prediction$se_min %>% as.integer() prediction$se_min <- prediction$se_min %>% as.integer()
@ -653,12 +653,12 @@ resistance_predict <- function(tbl,
if (!'I' %in% colnames(df)) { if (!'I' %in% colnames(df)) {
df$I <- 0 df$I <- 0
} }
df$resistance <- df$R / rowSums(df[, c('R', 'S', 'I')]) df$value <- df$R / rowSums(df[, c('R', 'S', 'I')])
} else { } else {
df$resistance <- df$R / rowSums(df[, c('R', 'S')]) df$value <- df$R / rowSums(df[, c('R', 'S')])
} }
measurements <- data.frame(year = df$year, measurements <- data.frame(year = df$year,
resistance = df$resistance, value = df$value,
se_min = NA, se_min = NA,
se_max = NA, se_max = NA,
observations = df$total, observations = df$total,
@ -668,16 +668,18 @@ resistance_predict <- function(tbl,
total <- rbind(measurements, total <- rbind(measurements,
prediction %>% filter(!year %in% df$year)) prediction %>% filter(!year %in% df$year))
if (model %in% c('binomial', 'binom', 'logit')) { if (model %in% c('binomial', 'binom', 'logit')) {
total <- total %>% mutate(observed = ifelse(is.na(observations), NA, resistance), total <- total %>% mutate(observed = ifelse(is.na(observations), NA, value),
estimated = prediction$resistance) estimated = prediction$value)
} }
} }
try( if ("value" %in% colnames(total)) {
total$resistance[which(total$resistance > 1)] <- 1, total <- total %>%
total$resistance[which(total$resistance < 0)] <- 0, mutate(value = case_when(value > 1 ~ 1,
silent = TRUE value < 0 ~ 0,
) TRUE ~ value))
}
total %>% arrange(year) total %>% arrange(year)
} }

View File

@ -40,7 +40,7 @@ rsi_predict(tbl, col_ab, col_date, year_min = NULL, year_max = NULL,
\code{data.frame} with columns: \code{data.frame} with columns:
\itemize{ \itemize{
\item{\code{year}} \item{\code{year}}
\item{\code{resistance}, the same as \code{estimated} when \code{preserve_measurements = FALSE}, and a combination of \code{observed} and \code{estimated} otherwise} \item{\code{value}, the same as \code{estimated} when \code{preserve_measurements = FALSE}, and a combination of \code{observed} and \code{estimated} otherwise}
\item{\code{se_min}, the lower bound of the standard error with a minimum of \code{0}} \item{\code{se_min}, the lower bound of the standard error with a minimum of \code{0}}
\item{\code{se_max} the upper bound of the standard error with a maximum of \code{1}} \item{\code{se_max} the upper bound of the standard error with a maximum of \code{1}}
\item{\code{observations}, the total number of observations, i.e. S + I + R} \item{\code{observations}, the total number of observations, i.e. S + I + R}
@ -102,7 +102,7 @@ if (!require(ggplot2)) {
ggplot(data, ggplot(data,
aes(x = year)) + aes(x = year)) +
geom_col(aes(y = resistance), geom_col(aes(y = value),
fill = "grey75") + fill = "grey75") +
geom_errorbar(aes(ymin = se_min, geom_errorbar(aes(ymin = se_min,
ymax = se_max), ymax = se_max),

View File

@ -1,6 +1,7 @@
context("atc.R") context("atc.R")
test_that("atc_property works", { test_that("atc_property works", {
if (!is.null(curl::nslookup("www.whocc.no", error = FALSE))) {
expect_equal(tolower(atc_property("J01CA04", property = "Name")), "amoxicillin") expect_equal(tolower(atc_property("J01CA04", property = "Name")), "amoxicillin")
expect_equal(atc_property("J01CA04", property = "unit"), "g") expect_equal(atc_property("J01CA04", property = "unit"), "g")
@ -15,6 +16,7 @@ test_that("atc_property works", {
expect_error(atc_property("J01CA04", property = c(1:5))) expect_error(atc_property("J01CA04", property = c(1:5)))
expect_error(atc_property("J01CA04", property = "test")) expect_error(atc_property("J01CA04", property = "test"))
expect_error(atc_property("J01CA04", property = "test", administration = c(1:5))) expect_error(atc_property("J01CA04", property = "test", administration = c(1:5)))
}
}) })
test_that("abname works", { test_that("abname works", {

View File

@ -86,7 +86,7 @@ test_that("prediction of rsi works", {
col_date = "date", col_date = "date",
minimum = 10, minimum = 10,
info = TRUE) %>% info = TRUE) %>%
pull("resistance") pull("value")
# amox resistance will increase according to data set `septic_patients` # amox resistance will increase according to data set `septic_patients`
expect_true(amox_R[3] < amox_R[20]) expect_true(amox_R[3] < amox_R[20])