diff --git a/DESCRIPTION b/DESCRIPTION index 0b343ec4..892b37d0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 0.7.1.9026 -Date: 2019-08-06 +Version: 0.7.1.9027 +Date: 2019-08-07 Title: Antimicrobial Resistance Analysis Authors@R: c( person( @@ -53,6 +53,7 @@ Imports: hms, knitr (>= 1.0.0), microbenchmark, + pillar, rlang (>= 0.3.1), scales, tidyr (>= 0.7.0) diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..e29e4408 --- /dev/null +++ b/LICENSE @@ -0,0 +1,280 @@ +GNU GENERAL PUBLIC LICENSE +Version 2, June 1991 + +Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +Everyone is permitted to copy and distribute verbatim copies +of this license document, but changing it is not allowed. + +Preamble + +The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + +The precise terms and conditions for copying, distribution and +modification follow. + +GNU GENERAL PUBLIC LICENSE +TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + +0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: + that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in + the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + +1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + +7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or + otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + +8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + +9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + +10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + +NO WARRANTY + +11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + +12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED + TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY + YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER + PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + +END OF TERMS AND CONDITIONS diff --git a/NAMESPACE b/NAMESPACE index 0e07b823..98bd9feb 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -18,6 +18,10 @@ S3method(freq,rsi) S3method(kurtosis,data.frame) S3method(kurtosis,default) S3method(kurtosis,matrix) +S3method(pillar_shaft,ab) +S3method(pillar_shaft,mic) +S3method(pillar_shaft,mo) +S3method(pillar_shaft,rsi) S3method(plot,mic) S3method(plot,resistance_predict) S3method(plot,rsi) @@ -37,6 +41,10 @@ S3method(skewness,matrix) S3method(summary,mic) S3method(summary,mo) S3method(summary,rsi) +S3method(type_sum,ab) +S3method(type_sum,mic) +S3method(type_sum,mo) +S3method(type_sum,rsi) export("%like%") export(ab_atc) export(ab_atc_group1) @@ -262,6 +270,8 @@ importFrom(graphics,plot) importFrom(graphics,points) importFrom(graphics,text) importFrom(microbenchmark,microbenchmark) +importFrom(pillar,pillar_shaft) +importFrom(pillar,type_sum) importFrom(rlang,as_label) importFrom(rlang,enquos) importFrom(scales,percent) diff --git a/NEWS.md b/NEWS.md index 6ce82d4d..0eba434b 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# AMR 0.7.1.9026 +# AMR 0.7.1.9027 ### Breaking * Function `freq()` has moved to a new package, [`clean`](https://github.com/msberends/clean) ([CRAN link](https://cran.r-project.org/package=clean)). Creating frequency tables is actually not the scope of this package (never was) and this function has matured a lot over the last two years. Therefore, a new package was created for data cleaning and checking and it perfectly fits the `freq()` function. The [`clean`](https://github.com/msberends/clean) package is available on CRAN and will be installed automatically when updating the `AMR` package, that now imports it. In a later stage, the `skewness()` and `kurtosis()` functions will be moved to the `clean` package too. @@ -35,7 +35,18 @@ Since this is a major change, usage of the old `also_single_tested` will throw an informative error that it has been replaced by `only_all_tested`. ### Changed -* Fixed a bug in `eucast_rules()` that caused an error when the input was a specific kind of `tibble` +* Added more informative errors and warnings to `eucast_rules()` +* Added tibble printing support for classes `rsi`, `mic`, `ab` and `mo`. When using tibbles containing antibiotic columns, values `S` will print in green, values `I` will print in yellow and values `R` will print in red: + ```r + (run this on your own console, as this page does not support colour printing) + tibble(mo = sample(AMR::microorganisms$fullname, 10), + drug1 = as.rsi(sample(c("S", "I", "R"), 10, replace = TRUE, + prob = c(0.6, 0.1, 0.3))), + drug2 = as.rsi(sample(c("S", "I", "R"), 10, replace = TRUE, + prob = c(0.6, 0.1, 0.3))), + drug3 = as.rsi(sample(c("S", "I", "R"), 10, replace = TRUE, + prob = c(0.6, 0.1, 0.3)))) + ``` * Removed class `atc` - using `as.atc()` is now deprecated in favour of `ab_atc()` and this will return a character, not the `atc` class anymore * Removed deprecated functions `abname()`, `ab_official()`, `atc_name()`, `atc_official()`, `atc_property()`, `atc_tradenames()`, `atc_trivial_nl()` * Fix and speed improvement for `mo_shortname()` @@ -50,6 +61,7 @@ * The `antibiotics` data set is now sorted by name * Using verbose mode with `eucast_rules(..., verbose = TRUE)` returns more informative and readable output * Speed improvement for `guess_ab_col()` which is now 30 times faster for antibiotic abbreviations +* Using factors as input for `eucast_rules()` now adds missing factors levels when the function changes antibiotic results # AMR 0.7.1 diff --git a/R/ab.R b/R/ab.R index 8a99c0be..b2481507 100755 --- a/R/ab.R +++ b/R/ab.R @@ -262,7 +262,7 @@ is.ab <- function(x) { #' @noRd print.ab <- function(x, ...) { cat("Class 'ab'\n") - print.default(as.character(x), quote = FALSE) + print(as.character(x), quote = FALSE) } #' @exportMethod as.data.frame.ab @@ -286,3 +286,17 @@ as.data.frame.ab <- function (x, ...) { pull.ab <- function(.data, ...) { pull(as.data.frame(.data), ...) } + +#' @importFrom pillar type_sum +#' @export +type_sum.ab <- function(x) { + "ab" +} + +#' @importFrom pillar pillar_shaft +#' @export +pillar_shaft.ab <- function(x, ...) { + out <- format(x) + out[is.na(x)] <- NA + pillar::new_pillar_shaft_simple(out, align = "left", min_width = 4) +} diff --git a/R/eucast_rules.R b/R/eucast_rules.R index 1f2a4ba3..ae683861 100755 --- a/R/eucast_rules.R +++ b/R/eucast_rules.R @@ -228,18 +228,18 @@ eucast_rules <- function(x, warned <- FALSE - txt_error <- function() { cat("", bgRed(white(" ERROR ")), "\n") } - txt_warning <- function() { if (warned == FALSE) { cat("", bgYellow(black(" WARNING ")), "\n") }; warned <<- TRUE } + txt_error <- function() { cat("", bgRed(white(" ERROR ")), "\n\n") } + txt_warning <- function() { if (warned == FALSE) { cat("", bgYellow(black(" WARNING "))) }; warned <<- TRUE } txt_ok <- function(no_of_changes) { if (warned == FALSE) { if (no_of_changes > 0) { if (no_of_changes == 1) { - cat(blue(" (1 new change)\n")) + cat(blue(" (1 value changed)\n")) } else { - cat(blue(paste0(" (", formatnr(no_of_changes), " new changes)\n"))) + cat(blue(paste0(" (", formatnr(no_of_changes), " values changed)\n"))) } } else { - cat(green(" (no new changes)\n")) + cat(green(" (no values changed)\n")) } warned <<- FALSE } @@ -402,20 +402,37 @@ eucast_rules <- function(x, x_original[rows, cols] <<- to, warning = function(w) { if (w$message %like% 'invalid factor level') { - warning('Value "', to, '" could not be applied to column(s) `', paste(cols, collapse = '`, `'), '` because this value is not an existing factor level. You can use as.rsi() to fix this.', call. = FALSE) + x_original <<- x_original %>% mutate_at(vars(cols), ~factor(x = as.character(.), levels = c(to, levels(.)))) + x <<- x %>% mutate_at(vars(cols), ~factor(x = as.character(.), levels = c(to, levels(.)))) + x_original[rows, cols] <<- to + warning('Value "', to, '" added to the factor levels of column(s) `', paste(cols, collapse = '`, `'), '` because this value was not an existing factor level.\nA better way is to use as.rsi() on beforehand on antibiotic columns to guarantee the right structure.', call. = FALSE) + txt_warning() + warned <<- FALSE } else { warning(w$message, call. = FALSE) + txt_warning() + cat("\n") # txt_warning() does not append a "\n" on itself } - txt_warning() }, error = function(e) { txt_error() - stop(e, call. = FALSE) + stop(paste0("Error in row(s) ", paste(rows[1:min(length(rows), 10)], collapse = ","), + '... while writing value "', to, + '" to column(s) `', paste(cols, collapse = "`, `"), + "` (data class:", paste(class(x_original), collapse = "/"), "):\n", e$message), call. = FALSE) } ) - - x[rows, cols] <<- x_original[rows, cols] - + + tryCatch( + x[rows, cols] <<- x_original[rows, cols], + error = function(e) { + stop(paste0("Error in row(s) ", paste(rows[1:min(length(rows), 10)], collapse = ","), + '... while writing value "', to, + '" to column(s) `', paste(cols, collapse = "`, `"), + "` (data class:", paste(class(x), collapse = "/"), "):\n", e$message), call. = FALSE) + } + ) + # before_df might not be a data.frame, but a tibble or data.table instead old <- as.data.frame(before_df, stringsAsFactors = FALSE)[rows,] no_of_changes_this_run <- 0 @@ -719,7 +736,7 @@ eucast_rules <- function(x, mutate(plural = ifelse(n > 1, "s", ""), txt = paste0(formatnr(n), " test result", plural, " added as ", new)) %>% pull(txt) %>% - paste(" -", ., collapse = "\n") %>% + paste(" *", ., collapse = "\n") %>% cat() } @@ -748,16 +765,16 @@ eucast_rules <- function(x, mutate(plural = ifelse(n > 1, "s", ""), txt = paste0(formatnr(n), " test result", plural, " changed from ", old, " to ", new)) %>% pull(txt) %>% - paste(" -", ., collapse = "\n") %>% + paste(" *", ., collapse = "\n") %>% cat() cat("\n") } cat(paste0(silver(strrep("-", options()$width - 1)), "\n")) if (verbose == FALSE & nrow(verbose_info) > 0) { - cat(paste("\nUse", bold("verbose = TRUE"), "(on your original data) to get a data.frame with all specified edits instead.\n")) + cat(paste("\nUse", bold("eucast_rules(..., verbose = TRUE)"), "(on your original data) to get a data.frame with all specified edits instead.\n\n")) } else if (verbose == TRUE) { - cat(paste(red("\nUsed 'Verbose mode' (verbose = TRUE)."), "This returns a data.frame with all specified edits.\nUse", bold("verbose = FALSE"), "to apply the rules on your data.\n")) + cat(paste(red("\nUsed 'Verbose mode' (verbose = TRUE)"), ", which returns a data.frame with all specified edits.\nUse", bold("verbose = FALSE"), "to apply the rules on your data.\n\n")) } } diff --git a/R/extended.R b/R/extended.R index bb94623e..8e115338 100644 --- a/R/extended.R +++ b/R/extended.R @@ -45,4 +45,3 @@ scale_type.ab <- function(x) { # "Error: Discrete value supplied to continuous scale" "discrete" } - diff --git a/R/mic.R b/R/mic.R index f8222e9b..291f7224 100755 --- a/R/mic.R +++ b/R/mic.R @@ -173,9 +173,8 @@ as.mic <- function(x, na.rm = FALSE) { list_missing, call. = FALSE) } - x <- factor(x, levels = lvls, ordered = TRUE) - class(x) <- c('mic', 'ordered', 'factor') - x + structure(.Data = factor(x, levels = lvls, ordered = TRUE), + class = c('mic', 'ordered', 'factor')) } } @@ -279,3 +278,17 @@ barplot.mic <- function(height, ...) axis(2, seq(0, max(table(droplevels.factor(height))))) } + +#' @importFrom pillar type_sum +#' @export +type_sum.mic <- function(x) { + "mic" +} + +#' @importFrom pillar pillar_shaft +#' @export +pillar_shaft.mic <- function(x, ...) { + out <- trimws(format(x)) + out[is.na(x)] <- NA + pillar::new_pillar_shaft_simple(out, align = "right", min_width = 4) +} diff --git a/R/mo.R b/R/mo.R index a8587773..3ba245dc 100755 --- a/R/mo.R +++ b/R/mo.R @@ -268,13 +268,17 @@ as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, allow_uncertain = TRUE, } - structure(.Data = y, class = "mo") + to_class_mo(y) +} + +to_class_mo <- function(x) { + structure(.Data = x, class = "mo") } #' @rdname as.mo #' @export is.mo <- function(x) { - identical(class(x), "mo") + identical(class(x), class(to_class_mo(x))) } #' @importFrom dplyr %>% pull left_join n_distinct progress_estimated filter distinct @@ -391,8 +395,7 @@ exec_as.mo <- function(x, # all empty if (all(identical(trimws(x_input), "") | is.na(x_input) | length(x) == 0)) { if (property == "mo") { - return(structure(rep(NA_character_, length(x_input)), - class = "mo")) + return(to_class_mo(rep(NA_character_, length(x_input)))) } else { return(rep(NA_character_, length(x_input))) } @@ -1455,7 +1458,7 @@ exec_as.mo <- function(x, ) if (property == "mo") { - class(x) <- "mo" + x <- to_class_mo(x) } if (length(mo_renamed()) > 0) { @@ -1507,6 +1510,20 @@ print.mo <- function(x, ...) { print.default(x, quote = FALSE) } +#' @importFrom pillar type_sum +#' @export +type_sum.mo <- function(x) { + "mo" +} + +#' @importFrom pillar pillar_shaft +#' @export +pillar_shaft.mo <- function(x, ...) { + out <- format(x) + out[is.na(x)] <- NA + pillar::new_pillar_shaft_simple(out, align = "left", min_width = 11) +} + #' @exportMethod summary.mo #' @importFrom dplyr n_distinct #' @importFrom clean freq top_freq diff --git a/R/mo_property.R b/R/mo_property.R index 56834003..dc4807e3 100755 --- a/R/mo_property.R +++ b/R/mo_property.R @@ -425,11 +425,10 @@ mo_validate <- function(x, property, ...) { } if (property == "mo") { - return(structure(x, class = "mo")) + return(to_class_mo(x)) } else if (property == "col_id") { return(as.integer(x)) } else { return(x) } - } diff --git a/R/resistance_predict.R b/R/resistance_predict.R index bf100ce9..d13a256e 100755 --- a/R/resistance_predict.R +++ b/R/resistance_predict.R @@ -28,7 +28,7 @@ #' @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 (i.e. using \code{\link{glm}(..., family = \link{binomial})}), 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 model the statistical model of choice. This could be a generalised linear regression model with binomial distribution (i.e. using \code{\link{glm}(..., family = \link{binomial})}), 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 all valid options. #' @param I_as_S a logical to indicate whether values \code{I} should be treated as \code{S} (will otherwise be treated as \code{R}) #' @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. @@ -112,7 +112,7 @@ resistance_predict <- function(x, year_max = NULL, year_every = 1, minimum = 30, - model = 'binomial', + model = NULL, I_as_S = TRUE, preserve_measurements = TRUE, info = TRUE, @@ -121,6 +121,10 @@ resistance_predict <- function(x, if (nrow(x) == 0) { stop('This table does not contain any observations.') } + + if (is.null(model)) { + stop('Choose a regression model with the `model` parameter, e.g. resistance_predict(..., model = "binomial").') + } if (!col_ab %in% colnames(x)) { stop('Column ', col_ab, ' not found.') @@ -252,7 +256,7 @@ resistance_predict <- function(x, se <- predictmodel$se.fit } else { - stop('No valid model selected.') + stop('No valid model selected. See ?resistance_predict.') } # prepare the output dataframe diff --git a/R/rsi.R b/R/rsi.R index 39178ba6..028287e7 100755 --- a/R/rsi.R +++ b/R/rsi.R @@ -472,3 +472,21 @@ barplot.rsi <- function(height, axis(side = 1, labels = levels(height), at = c(1, 2, 3) + 0.5, lwd = 0) } } + +#' @importFrom pillar type_sum +#' @export +type_sum.rsi <- function(x) { + "rsi" +} + +#' @importFrom pillar pillar_shaft +#' @importFrom crayon bgGreen bgYellow bgRed white black +#' @export +pillar_shaft.rsi <- function(x, ...) { + out <- trimws(format(x)) + out[is.na(x)] <- pillar::style_subtle("NA") + out[x == "S"] <- bgGreen(white(" S ")) + out[x == "I"] <- bgYellow(black(" I ")) + out[x == "R"] <- bgRed(white(" R ")) + pillar::new_pillar_shaft_simple(out, align = "left", min_width = 4) +} diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index 6b0af1f4..4ded8f9c 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -78,7 +78,7 @@ AMR (for R) - 0.7.1.9023 + 0.7.1.9027 @@ -224,17 +224,17 @@

License

-
                    GNU GENERAL PUBLIC LICENSE
-                       Version 2, June 1991
+
GNU GENERAL PUBLIC LICENSE
+Version 2, June 1991
 
- Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/>
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- Everyone is permitted to copy and distribute verbatim copies
- of this license document, but changing it is not allowed.
+Copyright (C) 1989, 1991 Free Software Foundation, Inc., <http://fsf.org/>
+  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+Everyone is permitted to copy and distribute verbatim copies
+of this license document, but changing it is not allowed.
 
-                            Preamble
+Preamble
 
-  The licenses for most software are designed to take away your
+The licenses for most software are designed to take away your
 freedom to share and change it.  By contrast, the GNU General Public
 License is intended to guarantee your freedom to share and change free
 software--to make sure the software is free for all its users.  This
@@ -279,21 +279,21 @@ program will individually obtain patent licenses, in effect making the
 program proprietary.  To prevent this, we have made it clear that any
 patent must be licensed for everyone's free use or not licensed at all.
 
-  The precise terms and conditions for copying, distribution and
+The precise terms and conditions for copying, distribution and
 modification follow.
 
-                    GNU GENERAL PUBLIC LICENSE
-   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+GNU GENERAL PUBLIC LICENSE
+TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
 
-  0. This License applies to any program or other work which contains
+0. This License applies to any program or other work which contains
 a notice placed by the copyright holder saying it may be distributed
 under the terms of this General Public License.  The "Program", below,
 refers to any such program or work, and a "work based on the Program"
 means either the Program or any derivative work under copyright law:
-that is to say, a work containing the Program or a portion of it,
+  that is to say, a work containing the Program or a portion of it,
 either verbatim or with modifications and/or translated into another
 language.  (Hereinafter, translation is included without limitation in
-the term "modification".)  Each licensee is addressed as "you".
+            the term "modification".)  Each licensee is addressed as "you".
 
 Activities other than copying, distribution and modification are not
 covered by this License; they are outside its scope.  The act of
@@ -302,7 +302,7 @@ is covered only if its contents constitute a work based on the
 Program (independent of having been made by running the Program).
 Whether that is true depends on what the Program does.
 
-  1. You may copy and distribute verbatim copies of the Program's
+1. You may copy and distribute verbatim copies of the Program's
 source code as you receive it, in any medium, provided that you
 conspicuously and appropriately publish on each copy an appropriate
 copyright notice and disclaimer of warranty; keep intact all the
@@ -420,10 +420,10 @@ restrictions on the recipients' exercise of the rights granted herein.
 You are not responsible for enforcing compliance by third parties to
 this License.
 
-  7. If, as a consequence of a court judgment or allegation of patent
+7. If, as a consequence of a court judgment or allegation of patent
 infringement or for any other reason (not limited to patent issues),
 conditions are imposed on you (whether by court order, agreement or
-otherwise) that contradict the conditions of this License, they do not
+                               otherwise) that contradict the conditions of this License, they do not
 excuse you from the conditions of this License.  If you cannot
 distribute so as to satisfy simultaneously your obligations under this
 License and any other pertinent obligations, then as a consequence you
@@ -452,7 +452,7 @@ impose that choice.
 This section is intended to make thoroughly clear what is believed to
 be a consequence of the rest of this License.
 
-  8. If the distribution and/or use of the Program is restricted in
+8. If the distribution and/or use of the Program is restricted in
 certain countries either by patents or by copyrighted interfaces, the
 original copyright holder who places the Program under this License
 may add an explicit geographical distribution limitation excluding
@@ -460,7 +460,7 @@ those countries, so that distribution is permitted only in or among
 countries not thus excluded.  In such case, this License incorporates
 the limitation as if written in the body of this License.
 
-  9. The Free Software Foundation may publish revised and/or new versions
+9. The Free Software Foundation may publish revised and/or new versions
 of the General Public License from time to time.  Such new versions will
 be similar in spirit to the present version, but may differ in detail to
 address new problems or concerns.
@@ -473,7 +473,7 @@ Software Foundation.  If the Program does not specify a version number of
 this License, you may choose any version ever published by the Free Software
 Foundation.
 
-  10. If you wish to incorporate parts of the Program into other free
+10. If you wish to incorporate parts of the Program into other free
 programs whose distribution conditions are different, write to the author
 to ask for permission.  For software which is copyrighted by the Free
 Software Foundation, write to the Free Software Foundation; we sometimes
@@ -481,9 +481,9 @@ make exceptions for this.  Our decision will be guided by the two goals
 of preserving the free status of all derivatives of our free software and
 of promoting the sharing and reuse of software generally.
 
-                            NO WARRANTY
+NO WARRANTY
 
-  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
 FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
 PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
@@ -493,76 +493,17 @@ TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
 PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
 REPAIR OR CORRECTION.
 
-  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
 REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
 INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
 OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
-TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
-YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
-PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+                                                TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+                                                YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+                                                PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGES.
 
-                     END OF TERMS AND CONDITIONS
-
-            How to Apply These Terms to Your New Programs
-
-  If you develop a new program, and you want it to be of the greatest
-possible use to the public, the best way to achieve this is to make it
-free software which everyone can redistribute and change under these terms.
-
-  To do so, attach the following notices to the program.  It is safest
-to attach them to the start of each source file to most effectively
-convey the exclusion of warranty; and each file should have at least
-the "copyright" line and a pointer to where the full notice is found.
-
-    {description}
-    Copyright (C) {year}  {fullname}
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License along
-    with this program; if not, write to the Free Software Foundation, Inc.,
-    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-
-Also add information on how to contact you by electronic and paper mail.
-
-If the program is interactive, make it output a short notice like this
-when it starts in an interactive mode:
-
-    Gnomovision version 69, Copyright (C) year name of author
-    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
-    This is free software, and you are welcome to redistribute it
-    under certain conditions; type `show c' for details.
-
-The hypothetical commands `show w' and `show c' should show the appropriate
-parts of the General Public License.  Of course, the commands you use may
-be called something other than `show w' and `show c'; they could even be
-mouse-clicks or menu items--whatever suits your program.
-
-You should also get your employer (if you work as a programmer) or your
-school, if any, to sign a "copyright disclaimer" for the program, if
-necessary.  Here is a sample; alter the names:
-
-  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
-  `Gnomovision' (which makes passes at compilers) written by James Hacker.
-
-  {signature of Ty Coon}, 1 April 1989
-  Ty Coon, President of Vice
-
-This General Public License does not permit incorporating your program into
-proprietary programs.  If your program is a subroutine library, you may
-consider it more useful to permit linking proprietary applications with the
-library.  If this is what you want to do, use the GNU Lesser General
-Public License instead of this License.
+END OF TERMS AND CONDITIONS
 
diff --git a/docs/articles/AMR.html b/docs/articles/AMR.html index 7a45e304..6e853524 100644 --- a/docs/articles/AMR.html +++ b/docs/articles/AMR.html @@ -40,7 +40,7 @@ AMR (for R) - 0.7.1.9026 + 0.7.1.9027 @@ -185,7 +185,7 @@

How to conduct AMR analysis

Matthijs S. Berends

-

06 August 2019

+

07 August 2019

@@ -194,7 +194,7 @@ -

Note: values on this page will change with every website update since they are based on randomly created values and the page was written in R Markdown. However, the methodology remains unchanged. This page was generated on 06 August 2019.

+

Note: values on this page will change with every website update since they are based on randomly created values and the page was written in R Markdown. However, the methodology remains unchanged. This page was generated on 07 August 2019.

Introduction

@@ -210,21 +210,21 @@ -2019-08-06 +2019-08-07 abcd Escherichia coli S S -2019-08-06 +2019-08-07 abcd Escherichia coli S R -2019-08-06 +2019-08-07 efgh Escherichia coli R @@ -320,71 +320,71 @@ -2017-03-10 -Q5 -Hospital C -Escherichia coli -S +2017-01-02 +V8 +Hospital A +Klebsiella pneumoniae S +I S S F -2011-12-26 -G6 -Hospital C +2013-05-08 +W2 +Hospital B +Staphylococcus aureus +R +S +S +S +F + + +2015-08-15 +R1 +Hospital B +Streptococcus pneumoniae +S +S +S +R +F + + +2012-01-09 +X9 +Hospital D +Staphylococcus aureus +S +I +R +S +F + + +2014-06-18 +Z7 +Hospital D Streptococcus pneumoniae S S S S -M - - -2011-07-01 -L6 -Hospital C -Escherichia coli -S -S -S -S -M - - -2015-08-22 -A9 -Hospital B -Escherichia coli -R -S -S -S -M - - -2017-03-16 -H1 -Hospital A -Staphylococcus aureus -S -I -S -S -M - - -2017-06-20 -V2 -Hospital D -Escherichia coli -I -S -R -S F + +2015-02-18 +F4 +Hospital A +Escherichia coli +R +S +S +S +M +

Now, let’s start the cleaning and the analysis!

@@ -406,8 +406,8 @@ # # Item Count Percent Cum. Count Cum. Percent # --- ----- ------- -------- ----------- ------------- -# 1 M 10,366 51.8% 10,366 51.8% -# 2 F 9,634 48.2% 20,000 100.0%
+# 1 M 10,401 52.0% 10,401 52.0% +# 2 F 9,599 48.0% 20,000 100.0%

So, we can draw at least two conclusions immediately. From a data scientists perspective, the data looks clean: only values M and F. From a researchers perspective: there are slightly more men. Nothing we didn’t already know.

The data is already quite clean, but we still need to transform some variables. The bacteria column now consists of text, and we want to add more variables based on microbial IDs later on. So, we will transform this column to valid IDs. The mutate() function of the dplyr package makes this really easy:

data <- data %>%
@@ -423,56 +423,56 @@
 # http://eucast.org/
 # 
 # EUCAST Clinical Breakpoints (v9.0, 2019)
-# Aerococcus sanguinicola (no new changes)
-# Aerococcus urinae (no new changes)
-# Anaerobic Gram-negatives (no new changes)
-# Anaerobic Gram-positives (no new changes)
-# Campylobacter coli (no new changes)
-# Campylobacter jejuni (no new changes)
-# Enterobacteriales (Order) (no new changes)
-# Enterococcus (no new changes)
-# Haemophilus influenzae (no new changes)
-# Kingella kingae (no new changes)
-# Moraxella catarrhalis (no new changes)
-# Pasteurella multocida (no new changes)
-# Staphylococcus (no new changes)
-# Streptococcus groups A, B, C, G (no new changes)
-# Streptococcus pneumoniae (1,439 new changes)
-# Viridans group streptococci (no new changes)
+# Aerococcus sanguinicola (no values changed)
+# Aerococcus urinae (no values changed)
+# Anaerobic Gram-negatives (no values changed)
+# Anaerobic Gram-positives (no values changed)
+# Campylobacter coli (no values changed)
+# Campylobacter jejuni (no values changed)
+# Enterobacteriales (Order) (no values changed)
+# Enterococcus (no values changed)
+# Haemophilus influenzae (no values changed)
+# Kingella kingae (no values changed)
+# Moraxella catarrhalis (no values changed)
+# Pasteurella multocida (no values changed)
+# Staphylococcus (no values changed)
+# Streptococcus groups A, B, C, G (no values changed)
+# Streptococcus pneumoniae (1,456 values changed)
+# Viridans group streptococci (no values changed)
 # 
 # EUCAST Expert Rules, Intrinsic Resistance and Exceptional Phenotypes (v3.1, 2016)
-# Table 01: Intrinsic resistance in Enterobacteriaceae (1,329 new changes)
-# Table 02: Intrinsic resistance in non-fermentative Gram-negative bacteria (no new changes)
-# Table 03: Intrinsic resistance in other Gram-negative bacteria (no new changes)
-# Table 04: Intrinsic resistance in Gram-positive bacteria (2,679 new changes)
-# Table 08: Interpretive rules for B-lactam agents and Gram-positive cocci (no new changes)
-# Table 09: Interpretive rules for B-lactam agents and Gram-negative rods (no new changes)
-# Table 11: Interpretive rules for macrolides, lincosamides, and streptogramins (no new changes)
-# Table 12: Interpretive rules for aminoglycosides (no new changes)
-# Table 13: Interpretive rules for quinolones (no new changes)
+# Table 01: Intrinsic resistance in Enterobacteriaceae (1,287 values changed)
+# Table 02: Intrinsic resistance in non-fermentative Gram-negative bacteria (no values changed)
+# Table 03: Intrinsic resistance in other Gram-negative bacteria (no values changed)
+# Table 04: Intrinsic resistance in Gram-positive bacteria (2,759 values changed)
+# Table 08: Interpretive rules for B-lactam agents and Gram-positive cocci (no values changed)
+# Table 09: Interpretive rules for B-lactam agents and Gram-negative rods (no values changed)
+# Table 11: Interpretive rules for macrolides, lincosamides, and streptogramins (no values changed)
+# Table 12: Interpretive rules for aminoglycosides (no values changed)
+# Table 13: Interpretive rules for quinolones (no values changed)
 # 
 # Other rules
-# Non-EUCAST: amoxicillin/clav acid = S where ampicillin = S (2,347 new changes)
-# Non-EUCAST: ampicillin = R where amoxicillin/clav acid = R (114 new changes)
-# Non-EUCAST: piperacillin = R where piperacillin/tazobactam = R (no new changes)
-# Non-EUCAST: piperacillin/tazobactam = S where piperacillin = S (no new changes)
-# Non-EUCAST: trimethoprim = R where trimethoprim/sulfa = R (no new changes)
-# Non-EUCAST: trimethoprim/sulfa = S where trimethoprim = S (no new changes)
+# Non-EUCAST: amoxicillin/clav acid = S where ampicillin = S (2,330 values changed)
+# Non-EUCAST: ampicillin = R where amoxicillin/clav acid = R (108 values changed)
+# Non-EUCAST: piperacillin = R where piperacillin/tazobactam = R (no values changed)
+# Non-EUCAST: piperacillin/tazobactam = S where piperacillin = S (no values changed)
+# Non-EUCAST: trimethoprim = R where trimethoprim/sulfa = R (no values changed)
+# Non-EUCAST: trimethoprim/sulfa = S where trimethoprim = S (no values changed)
 # 
 # --------------------------------------------------------------------------
-# EUCAST rules affected 6,589 out of 20,000 rows, making a total of 7,908 edits
+# EUCAST rules affected 6,604 out of 20,000 rows, making a total of 7,940 edits
 # => added 0 test results
 # 
-# => changed 7,908 test results
-#    - 110 test results changed from S to I
-#    - 4,680 test results changed from S to R
-#    - 1,068 test results changed from I to S
-#    - 326 test results changed from I to R
-#    - 1,711 test results changed from R to S
-#    - 13 test results changed from R to I
+# => changed 7,940 test results
+#    * 110 test results changed from S to I
+#    * 4,715 test results changed from S to R
+#    * 1,119 test results changed from I to S
+#    * 307 test results changed from I to R
+#    * 1,664 test results changed from R to S
+#    * 25 test results changed from R to I
 # --------------------------------------------------------------------------
 # 
-# Use verbose = TRUE (on your original data) to get a data.frame with all specified edits instead.
+# Use eucast_rules(..., verbose = TRUE) (on your original data) to get a data.frame with all specified edits instead.

@@ -497,7 +497,7 @@ # NOTE: Using column `bacteria` as input for `col_mo`. # NOTE: Using column `date` as input for `col_date`. # NOTE: Using column `patient_id` as input for `col_patient_id`. -# => Found 5,681 first isolates (28.4% of total)

+# => Found 5,676 first isolates (28.4% of total)

So only is suitable for resistance analysis! We can now filter on it with the filter() function, also from the dplyr package:

data_1st <- data %>% 
   filter(first == TRUE)
@@ -508,7 +508,7 @@

First weighted isolates

-

We made a slight twist to the CLSI algorithm, to take into account the antimicrobial susceptibility profile. Have a look at all isolates of patient C10, sorted on date:

+

We made a slight twist to the CLSI algorithm, to take into account the antimicrobial susceptibility profile. Have a look at all isolates of patient M4, sorted on date:

@@ -524,21 +524,21 @@ - - + + + - - + - - + + - + @@ -546,10 +546,10 @@ - - + + - + @@ -557,10 +557,10 @@ - - + + - + @@ -568,21 +568,21 @@ - - + + + + - - - - + + - + @@ -590,43 +590,43 @@ - - + + - - + + - - + + - - - + + + - - + + - + - - + + - + @@ -645,7 +645,7 @@ # NOTE: Using column `patient_id` as input for `col_patient_id`.# NOTE: Using column `keyab` as input for `col_keyantibiotics`. Use col_keyantibiotics = FALSE to prevent this.# [Criterion] Inclusion based on key antibiotics, ignoring I. -# => Found 15,027 first weighted isolates (75.1% of total) +# => Found 14,973 first weighted isolates (74.9% of total)
isolate
12010-07-24C102010-04-02M4 B_ESCHR_COLR SSSR S TRUE
22010-12-03C102010-05-26M4 B_ESCHR_COLRS S S S
32010-12-19C102010-07-15M4 B_ESCHR_COLSR S S S
42011-02-18C102010-09-11M4 B_ESCHR_COLRS S S S
52011-06-26C102010-11-13M4 B_ESCHR_COLRS S SRR FALSE
62011-06-28C102010-11-16M4 B_ESCHR_COLSR S S S
72011-06-30C102011-01-03M4 B_ESCHR_COLSS R SSS FALSE
82011-08-29C102011-03-16M4 B_ESCHR_COL S SRRTRUESSFALSE
92011-10-03C102011-04-21M4 B_ESCHR_COL S S S SFALSETRUE
102011-12-30C102011-04-24M4 B_ESCHR_COLRS S S S
@@ -662,22 +662,22 @@ - - + + + - - + - - + + - + @@ -686,10 +686,10 @@ - - + + - + @@ -698,10 +698,10 @@ - - + + - + @@ -710,83 +710,83 @@ - - + + + + - - - - + + - + - + - - + + - - + + + - - - + + - - - + + + - - + + - + - - + + - + - +
isolate
12010-07-24C102010-04-02M4 B_ESCHR_COLR SSSR S TRUE TRUE
22010-12-03C102010-05-26M4 B_ESCHR_COLRS S S S
32010-12-19C102010-07-15M4 B_ESCHR_COLSR S S S
42011-02-18C102010-09-11M4 B_ESCHR_COLRS S S S
52011-06-26C102010-11-13M4 B_ESCHR_COLRS S SRR FALSE TRUE
62011-06-28C102010-11-16M4 B_ESCHR_COLSR S S S FALSETRUEFALSE
72011-06-30C102011-01-03M4 B_ESCHR_COLSS R SSSFALSE FALSETRUE
82011-08-29C102011-03-16M4 B_ESCHR_COL S SRRTRUESSFALSE TRUE
92011-10-03C102011-04-21M4 B_ESCHR_COL S S S SFALSETRUE TRUE
102011-12-30C102011-04-24M4 B_ESCHR_COLRS S S S FALSETRUEFALSE
-

Instead of 2, now 10 isolates are flagged. In total, of all isolates are marked ‘first weighted’ - more than when using the CLSI guideline. In real life, this novel algorithm will yield 5-10% more isolates than the classic CLSI guideline.

+

Instead of 2, now 7 isolates are flagged. In total, of all isolates are marked ‘first weighted’ - more than when using the CLSI guideline. In real life, this novel algorithm will yield 5-10% more isolates than the classic CLSI guideline.

As with filter_first_isolate(), there’s a shortcut for this new algorithm too:

-

So we end up with 15,027 isolates for analysis.

+

So we end up with 14,973 isolates for analysis.

We can remove unneeded columns:

@@ -811,64 +811,64 @@ -1 -2017-03-10 -Q5 -Hospital C -B_ESCHR_COL -S -S -S -S -F -Gram-negative -Escherichia -coli -TRUE - - -2 -2011-12-26 -G6 -Hospital C +3 +2015-08-15 +R1 +Hospital B B_STRPT_PNE S S S R -M +F Gram-positive Streptococcus pneumoniae TRUE - -3 -2011-07-01 -L6 -Hospital C -B_ESCHR_COL -S -S -S -S -M -Gram-negative -Escherichia -coli -TRUE - -6 -2017-06-20 -V2 +4 +2012-01-09 +X9 Hospital D -B_ESCHR_COL -I +B_STPHY_AUR +S S R S F +Gram-positive +Staphylococcus +aureus +TRUE + + +5 +2014-06-18 +Z7 +Hospital D +B_STRPT_PNE +S +S +S +R +F +Gram-positive +Streptococcus +pneumoniae +TRUE + + +6 +2015-02-18 +F4 +Hospital A +B_ESCHR_COL +R +S +S +S +M Gram-negative Escherichia coli @@ -876,34 +876,34 @@ 7 -2012-02-10 -D1 -Hospital A -B_STRPT_PNE +2010-11-14 +W5 +Hospital D +B_STPHY_AUR S S R -R -M +S +F Gram-positive -Streptococcus -pneumoniae +Staphylococcus +aureus TRUE 8 -2010-05-01 -E1 -Hospital A -B_STRPT_PNE +2010-12-25 +F4 +Hospital B +B_ESCHR_COL +S S S S -R M -Gram-positive -Streptococcus -pneumoniae +Gram-negative +Escherichia +coli TRUE @@ -925,7 +925,7 @@
data_1st %>% freq(genus, species)

Frequency table

Class: character
-Length: 15,027 (of which NA: 0 = 0.00%)
+Length: 14,973 (of which NA: 0 = 0.00%)
Unique: 4

Shortest: 16
Longest: 24

@@ -942,33 +942,33 @@ Longest: 24

1 Escherichia coli -7,456 -49.6% -7,456 -49.6% +7,385 +49.3% +7,385 +49.3% 2 Staphylococcus aureus 3,709 -24.7% -11,165 -74.3% +24.8% +11,094 +74.1% 3 Streptococcus pneumoniae -2,267 -15.1% -13,432 -89.4% +2,340 +15.6% +13,434 +89.7% 4 Klebsiella pneumoniae -1,595 -10.6% -15,027 +1,539 +10.3% +14,973 100.0% @@ -979,7 +979,7 @@ Longest: 24

Resistance percentages

The functions portion_S(), portion_SI(), portion_I(), portion_IR() and portion_R() can be used to determine the portion of a specific antimicrobial outcome. As per the EUCAST guideline of 2019, we calculate resistance as the portion of R (portion_R()) and susceptibility as the portion of S and I (portion_SI()). These functions can be used on their own:

+# [1] 0.4646363

Or can be used in conjuction with group_by() and summarise(), both from the dplyr package:

data_1st %>% 
   group_by(hospital) %>% 
@@ -992,19 +992,19 @@ Longest: 24

Hospital A -0.4574111 +0.4721851 Hospital B -0.4789054 +0.4554570 Hospital C -0.4639895 +0.4683715 Hospital D -0.4705098 +0.4663838 @@ -1022,23 +1022,23 @@ Longest: 24

Hospital A -0.4574111 -4473 +0.4721851 +4494 Hospital B -0.4789054 -5262 +0.4554570 +5186 Hospital C -0.4639895 -2291 +0.4683715 +2229 Hospital D -0.4705098 -3001 +0.4663838 +3064 @@ -1058,27 +1058,27 @@ Longest: 24

Escherichia -0.9244903 -0.8964592 -0.9936964 +0.9243060 +0.8958700 +0.9932295 Klebsiella -0.8244514 -0.9059561 -0.9811912 +0.8401559 +0.8992853 +0.9837557 Staphylococcus -0.9266649 -0.9201941 -0.9935293 +0.9280129 +0.9263953 +0.9967646 Streptococcus -0.6060873 +0.6303419 0.0000000 -0.6060873 +0.6303419 diff --git a/docs/articles/AMR_files/figure-html/plot 1-1.png b/docs/articles/AMR_files/figure-html/plot 1-1.png index 6ce2bb20..e466e422 100644 Binary files a/docs/articles/AMR_files/figure-html/plot 1-1.png and b/docs/articles/AMR_files/figure-html/plot 1-1.png differ diff --git a/docs/articles/AMR_files/figure-html/plot 3-1.png b/docs/articles/AMR_files/figure-html/plot 3-1.png index 6846b987..deb7c09f 100644 Binary files a/docs/articles/AMR_files/figure-html/plot 3-1.png and b/docs/articles/AMR_files/figure-html/plot 3-1.png differ diff --git a/docs/articles/AMR_files/figure-html/plot 4-1.png b/docs/articles/AMR_files/figure-html/plot 4-1.png index df4d8e0d..c2732d0b 100644 Binary files a/docs/articles/AMR_files/figure-html/plot 4-1.png and b/docs/articles/AMR_files/figure-html/plot 4-1.png differ diff --git a/docs/articles/AMR_files/figure-html/plot 5-1.png b/docs/articles/AMR_files/figure-html/plot 5-1.png index c3abeceb..04bc4bf0 100644 Binary files a/docs/articles/AMR_files/figure-html/plot 5-1.png and b/docs/articles/AMR_files/figure-html/plot 5-1.png differ diff --git a/docs/articles/MDR.html b/docs/articles/MDR.html index c17496e8..74ab07e9 100644 --- a/docs/articles/MDR.html +++ b/docs/articles/MDR.html @@ -40,7 +40,7 @@ AMR (for R) - 0.7.1.9026 + 0.7.1.9027
@@ -185,7 +185,7 @@

How to determine multi-drug resistance (MDR)

Matthijs S. Berends

-

06 August 2019

+

07 August 2019

@@ -228,16 +228,16 @@

The data set looks like this now:

@@ -272,39 +272,39 @@ Unique: 5

1 Mono-resistance -3264 -65.3% -3264 -65.3% +3271 +65.4% +3271 +65.4% 2 Negative -627 -12.5% -3891 -77.8% +643 +12.9% +3914 +78.3% 3 Multidrug resistance -607 -12.1% -4498 -90.0% +582 +11.6% +4496 +89.9% 4 Poly-resistance -288 -5.8% -4786 +287 +5.7% +4783 95.7% 5 Extensive drug resistance -214 +217 4.3% 5000 100.0% diff --git a/docs/articles/WHONET.html b/docs/articles/WHONET.html index cf72f871..41eb2e1e 100644 --- a/docs/articles/WHONET.html +++ b/docs/articles/WHONET.html @@ -40,7 +40,7 @@ AMR (for R) - 0.7.1.9026 + 0.7.1.9027 @@ -185,7 +185,7 @@

How to work with WHONET data

Matthijs S. Berends

-

06 August 2019

+

07 August 2019

diff --git a/docs/articles/index.html b/docs/articles/index.html index f839a116..3683dc1f 100644 --- a/docs/articles/index.html +++ b/docs/articles/index.html @@ -78,7 +78,7 @@ AMR (for R) - 0.7.1.9026 + 0.7.1.9027 diff --git a/docs/authors.html b/docs/authors.html index 44c9d901..df56c1c3 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -78,7 +78,7 @@ AMR (for R) - 0.7.1.9026 + 0.7.1.9027 diff --git a/docs/index.html b/docs/index.html index e5fedb31..b455e745 100644 --- a/docs/index.html +++ b/docs/index.html @@ -42,7 +42,7 @@ AMR (for R) - 0.7.1.9026 + 0.7.1.9027 diff --git a/docs/news/index.html b/docs/news/index.html index 49872dce..ad64ee33 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -78,7 +78,7 @@ AMR (for R) - 0.7.1.9026 + 0.7.1.9027 @@ -225,9 +225,9 @@ -
+

-AMR 0.7.1.9026 Unreleased +AMR 0.7.1.9027 Unreleased

@@ -273,7 +273,18 @@

Changed

@@ -307,14 +319,14 @@

All these lead to the microbial ID of E. coli:

- +
  • Function mo_info() as an analogy to ab_info(). The mo_info() prints a list with the full taxonomy, authors, and the URL to the online database of a microorganism
  • Function mo_synonyms() to get all previously accepted taxonomic names of a microorganism

  • @@ -436,14 +448,14 @@ Please
    septic_patients %>% 
    -  freq(age) %>% 
    -  boxplot()
    -# grouped boxplots:
    -septic_patients %>% 
    -  group_by(hospital_id) %>% 
    -  freq(age) %>%
    -  boxplot()
    + @@ -528,32 +540,32 @@ This data is updated annually - check the included version with the new function
  • New filters for antimicrobial classes. Use these functions to filter isolates on results in one of more antibiotics from a specific class:

    - +

    The antibiotics data set will be searched, after which the input data will be checked for column names with a value in any abbreviations, codes or official names found in the antibiotics data set. For example:

    - +
  • All ab_* functions are deprecated and replaced by atc_* functions:

    - + These functions use as.atc() internally. The old atc_property has been renamed atc_online_property(). This is done for two reasons: firstly, not all ATC codes are of antibiotics (ab) but can also be of antivirals or antifungals. Secondly, the input must have class atc or must be coerable to this class. Properties of these classes should start with the same class name, analogous to as.mo() and e.g. mo_genus.
  • New functions set_mo_source() and get_mo_source() to use your own predefined MO codes as input for as.mo() and consequently all mo_* functions
  • Support for the upcoming dplyr version 0.8.0
  • @@ -565,20 +577,20 @@ These functions use as.atc()New function age_groups() to split ages into custom or predefined groups (like children or elderly). This allows for easier demographic antimicrobial resistance analysis per age group.
  • New function ggplot_rsi_predict() as well as the base R plot() function can now be used for resistance prediction calculated with resistance_predict():

    -
    x <- resistance_predict(septic_patients, col_ab = "amox")
    -plot(x)
    -ggplot_rsi_predict(x)
    +
    x <- resistance_predict(septic_patients, col_ab = "amox")
    +plot(x)
    +ggplot_rsi_predict(x)
  • Functions filter_first_isolate() and filter_first_weighted_isolate() to shorten and fasten filtering on data sets with antimicrobial results, e.g.:

    - +

    is equal to:

    -
    septic_patients %>%
    -  mutate(only_firsts = first_isolate(septic_patients, ...)) %>%
    -  filter(only_firsts == TRUE) %>%
    -  select(-only_firsts)
    +
    septic_patients %>%
    +  mutate(only_firsts = first_isolate(septic_patients, ...)) %>%
    +  filter(only_firsts == TRUE) %>%
    +  select(-only_firsts)
  • New function availability() to check the number of available (non-empty) results in a data.frame
  • @@ -607,33 +619,33 @@ These functions use as.atc()
  • Now handles incorrect spelling, like i instead of y and f instead of ph:

    - +
  • Uncertainty of the algorithm is now divided into four levels, 0 to 3, where the default allow_uncertain = TRUE is equal to uncertainty level 2. Run ?as.mo for more info about these levels.

    -
    # equal:
    -as.mo(..., allow_uncertain = TRUE)
    -as.mo(..., allow_uncertain = 2)
    -
    -# also equal:
    -as.mo(..., allow_uncertain = FALSE)
    -as.mo(..., allow_uncertain = 0)
    +
    # equal:
    +as.mo(..., allow_uncertain = TRUE)
    +as.mo(..., allow_uncertain = 2)
    +
    +# also equal:
    +as.mo(..., allow_uncertain = FALSE)
    +as.mo(..., allow_uncertain = 0)
    Using as.mo(..., allow_uncertain = 3) could lead to very unreliable results.
  • Implemented the latest publication of Becker et al. (2019), for categorising coagulase-negative Staphylococci
  • All microbial IDs that found are now saved to a local file ~/.Rhistory_mo. Use the new function clean_mo_history() to delete this file, which resets the algorithms.
  • Incoercible results will now be considered ‘unknown’, MO code UNKNOWN. On foreign systems, properties of these will be translated to all languages already previously supported: German, Dutch, French, Italian, Spanish and Portuguese:

    - +
  • Fix for vector containing only empty values
  • Finds better results when input is in other languages
  • @@ -679,19 +691,19 @@ Using as.mo(..., allow_uncertain = 3)
  • Support for tidyverse quasiquotation! Now you can create frequency tables of function outcomes:

    - +
  • Header info is now available as a list, with the header function
  • The parameter header is now set to TRUE at default, even for markdown
  • @@ -766,10 +778,10 @@ Using as.mo(..., allow_uncertain = 3)Fewer than 3 characters as input for as.mo will return NA
  • Function as.mo (and all mo_* wrappers) now supports genus abbreviations with “species” attached

    -
    as.mo("E. species")        # B_ESCHR
    -mo_fullname("E. spp.")     # "Escherichia species"
    -as.mo("S. spp")            # B_STPHY
    -mo_fullname("S. species")  # "Staphylococcus species"
    +
    as.mo("E. species")        # B_ESCHR
    +mo_fullname("E. spp.")     # "Escherichia species"
    +as.mo("S. spp")            # B_STPHY
    +mo_fullname("S. species")  # "Staphylococcus species"
  • Added parameter combine_IR (TRUE/FALSE) to functions portion_df and count_df, to indicate that all values of I and R must be merged into one, so the output only consists of S vs. IR (susceptible vs. non-susceptible)
  • Fix for portion_*(..., as_percent = TRUE) when minimal number of isolates would not be met
  • @@ -782,15 +794,15 @@ Using as.mo(..., allow_uncertain = 3)
  • Support for grouping variables, test with:

    - +
  • Support for (un)selecting columns:

    - +
  • Check for hms::is.hms
  • @@ -870,18 +882,18 @@ Using as.mo(..., allow_uncertain = 3)

    They also come with support for German, Dutch, French, Italian, Spanish and Portuguese:

    -
    mo_gramstain("E. coli")
    -# [1] "Gram negative"
    -mo_gramstain("E. coli", language = "de") # German
    -# [1] "Gramnegativ"
    -mo_gramstain("E. coli", language = "es") # Spanish
    -# [1] "Gram negativo"
    -mo_fullname("S. group A", language = "pt") # Portuguese
    -# [1] "Streptococcus grupo A"
    +
    mo_gramstain("E. coli")
    +# [1] "Gram negative"
    +mo_gramstain("E. coli", language = "de") # German
    +# [1] "Gramnegativ"
    +mo_gramstain("E. coli", language = "es") # Spanish
    +# [1] "Gram negativo"
    +mo_fullname("S. group A", language = "pt") # Portuguese
    +# [1] "Streptococcus grupo A"

    Furthermore, former taxonomic names will give a note about the current taxonomic name:

    - +
  • Functions count_R, count_IR, count_I, count_SI and count_S to selectively count resistant or susceptible isolates @@ -1206,7 +1218,7 @@ Using as.mo(..., allow_uncertain = 3)

    Contents