From feab1cad6b83f73ddb9186a40c8f052c8c27267e Mon Sep 17 00:00:00 2001 From: "Matthijs S. Berends" Date: Sat, 28 Jul 2018 09:34:03 +0200 Subject: [PATCH] small fixes --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/globals.R | 1 + R/resistance.R | 36 +++++++++++++++++--------------- man/resistance_predict.Rd | 4 ++-- tests/testthat/test-atc.R | 22 ++++++++++--------- tests/testthat/test-resistance.R | 2 +- 7 files changed, 37 insertions(+), 31 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ba62be75..5e206ebb 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR Version: 0.2.0.9017 -Date: 2018-07-25 +Date: 2018-07-28 Title: Antimicrobial Resistance Analysis Authors@R: c( person( diff --git a/NAMESPACE b/NAMESPACE index 660ff3f1..dd949e0b 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -111,6 +111,7 @@ importFrom(dplyr,arrange) importFrom(dplyr,arrange_at) importFrom(dplyr,as_tibble) importFrom(dplyr,between) +importFrom(dplyr,case_when) importFrom(dplyr,desc) importFrom(dplyr,filter) importFrom(dplyr,filter_at) diff --git a/R/globals.R b/R/globals.R index e6de310e..f5f3a530 100755 --- a/R/globals.R +++ b/R/globals.R @@ -60,6 +60,7 @@ globalVariables(c('abname', 'septic_patients', 'species', 'umcg', + 'value', 'values', 'View', 'y', diff --git a/R/resistance.R b/R/resistance.R index d87492ab..35768cf1 100755 --- a/R/resistance.R +++ b/R/resistance.R @@ -429,7 +429,7 @@ rsi_df <- function(tbl, #' @return \code{data.frame} with columns: #' \itemize{ #' \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_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} @@ -440,7 +440,7 @@ rsi_df <- function(tbl, #' @rdname resistance_predict #' @export #' @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 #' @examples #' \dontrun{ @@ -493,7 +493,7 @@ rsi_df <- function(tbl, #' #' ggplot(data, #' aes(x = year)) + -#' geom_col(aes(y = resistance), +#' geom_col(aes(y = value), #' fill = "grey75") + #' geom_errorbar(aes(ymin = se_min, #' ymax = se_max), @@ -626,13 +626,13 @@ resistance_predict <- function(tbl, } # 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_max <- prediction$resistance + se + prediction$se_min <- prediction$value - se + prediction$se_max <- prediction$value + se if (model == 'loglin') { - prediction$resistance <- prediction$resistance %>% + prediction$value <- prediction$value %>% format(scientific = FALSE) %>% as.integer() prediction$se_min <- prediction$se_min %>% as.integer() @@ -653,12 +653,12 @@ resistance_predict <- function(tbl, if (!'I' %in% colnames(df)) { df$I <- 0 } - df$resistance <- df$R / rowSums(df[, c('R', 'S', 'I')]) + df$value <- df$R / rowSums(df[, c('R', 'S', 'I')]) } 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, - resistance = df$resistance, + value = df$value, se_min = NA, se_max = NA, observations = df$total, @@ -668,16 +668,18 @@ resistance_predict <- function(tbl, total <- rbind(measurements, prediction %>% filter(!year %in% df$year)) if (model %in% c('binomial', 'binom', 'logit')) { - total <- total %>% mutate(observed = ifelse(is.na(observations), NA, resistance), - estimated = prediction$resistance) + total <- total %>% mutate(observed = ifelse(is.na(observations), NA, value), + estimated = prediction$value) } } - try( - total$resistance[which(total$resistance > 1)] <- 1, - total$resistance[which(total$resistance < 0)] <- 0, - silent = TRUE - ) + if ("value" %in% colnames(total)) { + total <- total %>% + mutate(value = case_when(value > 1 ~ 1, + value < 0 ~ 0, + TRUE ~ value)) + } + total %>% arrange(year) } diff --git a/man/resistance_predict.Rd b/man/resistance_predict.Rd index 58f9241b..280b7250 100644 --- a/man/resistance_predict.Rd +++ b/man/resistance_predict.Rd @@ -40,7 +40,7 @@ rsi_predict(tbl, col_ab, col_date, year_min = NULL, year_max = NULL, \code{data.frame} with columns: \itemize{ \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_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} @@ -102,7 +102,7 @@ if (!require(ggplot2)) { ggplot(data, aes(x = year)) + - geom_col(aes(y = resistance), + geom_col(aes(y = value), fill = "grey75") + geom_errorbar(aes(ymin = se_min, ymax = se_max), diff --git a/tests/testthat/test-atc.R b/tests/testthat/test-atc.R index 2df63577..1c894ac3 100755 --- a/tests/testthat/test-atc.R +++ b/tests/testthat/test-atc.R @@ -1,20 +1,22 @@ context("atc.R") test_that("atc_property works", { - expect_equal(tolower(atc_property("J01CA04", property = "Name")), "amoxicillin") - expect_equal(atc_property("J01CA04", property = "unit"), "g") + if (!is.null(curl::nslookup("www.whocc.no", error = FALSE))) { + expect_equal(tolower(atc_property("J01CA04", property = "Name")), "amoxicillin") + expect_equal(atc_property("J01CA04", property = "unit"), "g") - expect_equal(atc_property("J01CA04", property = "DDD"), - atc_ddd("J01CA04")) + expect_equal(atc_property("J01CA04", property = "DDD"), + atc_ddd("J01CA04")) - expect_identical(atc_property("J01CA04", property = "Groups"), - atc_groups("J01CA04")) + expect_identical(atc_property("J01CA04", property = "Groups"), + atc_groups("J01CA04")) - expect_warning(atc_property("ABCDEFG", property = "DDD")) + expect_warning(atc_property("ABCDEFG", property = "DDD")) - expect_error(atc_property("J01CA04", property = c(1:5))) - expect_error(atc_property("J01CA04", property = "test")) - expect_error(atc_property("J01CA04", property = "test", administration = 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", administration = c(1:5))) + } }) test_that("abname works", { diff --git a/tests/testthat/test-resistance.R b/tests/testthat/test-resistance.R index 7ad14aa8..825dd742 100755 --- a/tests/testthat/test-resistance.R +++ b/tests/testthat/test-resistance.R @@ -86,7 +86,7 @@ test_that("prediction of rsi works", { col_date = "date", minimum = 10, info = TRUE) %>% - pull("resistance") + pull("value") # amox resistance will increase according to data set `septic_patients` expect_true(amox_R[3] < amox_R[20])