diff --git a/DESCRIPTION b/DESCRIPTION index dcb6027d..5a6953db 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR Version: 0.5.0.9017 -Date: 2019-02-11 +Date: 2019-02-12 Title: Antimicrobial Resistance Analysis Authors@R: c( person( diff --git a/R/freq.R b/R/freq.R index 4158bdc9..109ddffe 100755 --- a/R/freq.R +++ b/R/freq.R @@ -564,8 +564,16 @@ format_header <- function(x, markdown = FALSE, decimal.mark = ".", big.mark = ", # FORMATTING # rsi if (has_length == TRUE & any(x_class == "rsi")) { + if (header$count_S < header$count_IR) { + ratio <- paste0(green(1), ":", red(format(header$count_IR / header$count_S, + digits = 1, nsmall = 1, decimal.mark = decimal.mark, big.mark = big.mark))) + } else { + ratio <- paste0(green(format(header$count_S / header$count_IR, + digits = 1, nsmall = 1, decimal.mark = decimal.mark, big.mark = big.mark)), + ":", red(1)) + } header$`%IR` <- paste((header$count_IR / header$length) %>% percent(force_zero = TRUE, round = digits, decimal.mark = decimal.mark), - paste0("(ratio S : IR = 1.0 : ", (header$count_IR / header$count_S) %>% format(digits = 1, nsmall = 1, decimal.mark = decimal.mark, big.mark = big.mark), ")")) + paste0("(ratio ", ratio, ")")) header <- header[!names(header) %in% c("count_S", "count_IR")] } # dates @@ -872,11 +880,20 @@ print.frequency_tbl <- function(x, if (opt$tbl_format == "pandoc") { title <- bold(title) } else if (opt$tbl_format == "markdown") { - title <- paste0("\n**", title, "** ") # two space for newline + title <- paste0("\n\n**", title, "** ") # two space for newline + } + + cat(title, "\n\n") + + if (NROW(x) == 0) { + cat("No observations.\n") + if (opt$tbl_format == "markdown") { + cat("\n") + } + return(invisible()) } if (opt$header == TRUE) { - cat(title, "\n") if (!is.null(opt$header_txt)) { if (is.null(opt$digits)) { opt$digits <- 2 @@ -884,14 +901,6 @@ print.frequency_tbl <- function(x, cat(format_header(x, digits = opt$digits, markdown = (opt$tbl_format == "markdown"), decimal.mark = decimal.mark, big.mark = big.mark)) } - } else if (opt$tbl_format == "markdown") { - # do print title as caption in markdown - cat("\n", title, sep = "") # two trailing spaces for markdown - } - - if (NROW(x) == 0) { - cat("\n\nNo observations.\n") - return(invisible()) } # save old NA setting for kable diff --git a/docs/articles/AMR.html b/docs/articles/AMR.html index 0fcd39d7..29aa831d 100644 --- a/docs/articles/AMR.html +++ b/docs/articles/AMR.html @@ -185,7 +185,7 @@

How to conduct AMR analysis

Matthijs S. Berends

-

11 February 2019

+

12 February 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 RMarkdown. However, the methodology remains unchanged. This page was generated on 11 February 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 RMarkdown. However, the methodology remains unchanged. This page was generated on 12 February 2019.

Introduction

@@ -210,21 +210,21 @@ -2019-02-11 +2019-02-12 abcd Escherichia coli S S -2019-02-11 +2019-02-12 abcd Escherichia coli S R -2019-02-11 +2019-02-12 efgh Escherichia coli R @@ -285,25 +285,25 @@

Put everything together

-

Using the sample() function, we can randomly select items from all objects we defined earlier. To let our fake data reflect reality a bit, we will also approximately define the probabilities of bacteria and the antibiotic results with the prob parameter.

+

Using the sample() function, we can randomly select items from all objects we defined earlier. To let our fake data reflect reality a bit, we will also approximately define the probabilities of bacteria and the antibiotic results with the prob parameter.

sample_size <- 20000
-data <- data.frame(date = sample(dates, size = sample_size, replace = TRUE),
-                   patient_id = sample(patients, size = sample_size, replace = TRUE),
-                   hospital = sample(hospitals, size = sample_size, replace = TRUE,
+data <- data.frame(date = sample(dates, size = sample_size, replace = TRUE),
+                   patient_id = sample(patients, size = sample_size, replace = TRUE),
+                   hospital = sample(hospitals, size = sample_size, replace = TRUE,
                                      prob = c(0.30, 0.35, 0.15, 0.20)),
-                   bacteria = sample(bacteria, size = sample_size, replace = TRUE,
+                   bacteria = sample(bacteria, size = sample_size, replace = TRUE,
                                      prob = c(0.50, 0.25, 0.15, 0.10)),
-                   amox = sample(ab_interpretations, size = sample_size, replace = TRUE,
+                   amox = sample(ab_interpretations, size = sample_size, replace = TRUE,
                                  prob = c(0.60, 0.05, 0.35)),
-                   amcl = sample(ab_interpretations, size = sample_size, replace = TRUE,
+                   amcl = sample(ab_interpretations, size = sample_size, replace = TRUE,
                                  prob = c(0.75, 0.10, 0.15)),
-                   cipr = sample(ab_interpretations, size = sample_size, replace = TRUE,
+                   cipr = sample(ab_interpretations, size = sample_size, replace = TRUE,
                                  prob = c(0.80, 0.00, 0.20)),
-                   gent = sample(ab_interpretations, size = sample_size, replace = TRUE,
+                   gent = sample(ab_interpretations, size = sample_size, replace = TRUE,
                                  prob = c(0.92, 0.00, 0.08))
                    )
-

Using the left_join() function from the dplyr package, we can ‘map’ the gender to the patient ID using the patients_table object we created earlier:

- +

Using the left_join() function from the dplyr package, we can ‘map’ the gender to the patient ID using the patients_table object we created earlier:

+

The resulting data set contains 5,000 blood culture isolates. With the head() function we can preview the first 6 values of this data set:

head(data)
@@ -320,64 +320,31 @@ - - - - - - - - - - - - - + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + @@ -385,6 +352,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
2013-12-28M2Hospital BStreptococcus pneumoniaeRSSSM
2017-03-17Z52012-04-28X6 Hospital B Escherichia coli R SSR S F
2011-05-13G5Hospital BStreptococcus pneumoniaeSSSSM
2011-03-06X3Hospital DEscherichia coliSSSSF
2016-11-01C9Hospital DEscherichia coliRSRSM
2010-12-16E22012-12-23Z1 Hospital AEscherichia coliISRSF
2011-05-27G10Hospital B Streptococcus pneumoniae S SS M
2012-08-19Q1Hospital DEscherichia coliSRSSF
2016-05-06Y2Hospital DKlebsiella pneumoniaeSSSSF
2010-08-27I8Hospital AEscherichia coliRSSSM

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

@@ -396,6 +396,7 @@

Use the frequency table function freq() to look specifically for unique values in any variable. For example, for the gender variable:

data %>% freq(gender) # this would be the same: freq(data$gender)
#> Frequency table of `gender` from a data.frame (20,000 x 9) 
+#> 
 #> Class:   factor (numeric)
 #> Levels:  F, M
 #> Length:  20,000 (of which NA: 0 = 0.00%)
@@ -403,15 +404,15 @@
 #> 
 #>      Item     Count   Percent   Cum. Count   Cum. Percent
 #> ---  -----  -------  --------  -----------  -------------
-#> 1    M       10,390     52.0%       10,390          52.0%
-#> 2    F        9,610     48.1%       20,000         100.0%
+#> 1 M 10,303 51.5% 10,303 51.5% +#> 2 F 9,697 48.5% 20,000 100.0%

So, we can draw at least two conclusions immediately. From a data scientist perspective, the data looks clean: only values M and F. From a researcher 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:

+

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 %>%
-  mutate(bacteria = as.mo(bacteria))
-

We also want to transform the antibiotics, because in real life data we don’t know if they are really clean. The as.rsi() function ensures reliability and reproducibility in these kind of variables. The mutate_at() will run the as.rsi() function on defined variables:

+ mutate(bacteria = as.mo(bacteria))
+

We also want to transform the antibiotics, because in real life data we don’t know if they are really clean. The as.rsi() function ensures reliability and reproducibility in these kind of variables. The mutate_at() will run the as.rsi() function on defined variables:

data <- data %>%
-  mutate_at(vars(amox:gent), as.rsi)
+ mutate_at(vars(amox:gent), as.rsi)

Finally, we will apply EUCAST rules on our antimicrobial results. In Europe, most medical microbiological laboratories already apply these rules. Our package features their latest insights on intrinsic resistance and exceptional phenotypes. Moreover, the eucast_rules() function can also apply additional rules, like forcing ampicillin = R when amoxicillin/clavulanic acid = R.

Because the amoxicillin (column amox) and amoxicillin/clavulanic acid (column amcl) in our data were generated randomly, some rows will undoubtedly contain amox = S and amcl = R, which is technically impossible. The eucast_rules() fixes this:

data <- eucast_rules(data, col_mo = "bacteria")
@@ -435,10 +436,10 @@
 #> Kingella kingae (no changes)
 #> 
 #> EUCAST Expert Rules, Intrinsic Resistance and Exceptional Phenotypes (v3.1, 2016)
-#> Table 1:  Intrinsic resistance in Enterobacteriaceae (1288 changes)
+#> Table 1:  Intrinsic resistance in Enterobacteriaceae (1278 changes)
 #> Table 2:  Intrinsic resistance in non-fermentative Gram-negative bacteria (no changes)
 #> Table 3:  Intrinsic resistance in other Gram-negative bacteria (no changes)
-#> Table 4:  Intrinsic resistance in Gram-positive bacteria (2832 changes)
+#> Table 4:  Intrinsic resistance in Gram-positive bacteria (2750 changes)
 #> Table 8:  Interpretive rules for B-lactam agents and Gram-positive cocci (no changes)
 #> Table 9:  Interpretive rules for B-lactam agents and Gram-negative rods (no changes)
 #> Table 10: Interpretive rules for B-lactam agents and other Gram-negative bacteria (no changes)
@@ -454,16 +455,16 @@
 #> Non-EUCAST: piperacillin/tazobactam = S where piperacillin = S (no changes)
 #> Non-EUCAST: trimethoprim/sulfa = S where trimethoprim = S (no changes)
 #> 
-#> => EUCAST rules affected 7,405 out of 20,000 rows
+#> => EUCAST rules affected 7,442 out of 20,000 rows
 #>    -> added 0 test results
-#>    -> changed 4,120 test results (0 to S; 0 to I; 4,120 to R)
+#> -> changed 4,028 test results (0 to S; 0 to I; 4,028 to R)

Adding new variables

Now that we have the microbial ID, we can add some taxonomic properties:

data <- data %>% 
-  mutate(gramstain = mo_gramstain(bacteria),
+  mutate(gramstain = mo_gramstain(bacteria),
          genus = mo_genus(bacteria),
          species = mo_species(bacteria))
@@ -477,14 +478,14 @@

This AMR package includes this methodology with the first_isolate() function. It adopts the episode of a year (can be changed by user) and it starts counting days after every selected isolate. This new variable can easily be added to our data:

-

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

+#> => Found 5,652 first isolates (28.3% of total)
+

So only 28.3% 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)
+ filter(first == TRUE)

For future use, the above two syntaxes can be shortened with the filter_first_isolate() function:

data_1st <- data %>% 
   filter_first_isolate()
@@ -508,30 +509,30 @@ 1 -2010-01-02 -F5 +2010-01-15 +V8 B_ESCHR_COL -R S +R S S TRUE 2 -2010-04-18 -F5 +2010-02-08 +V8 B_ESCHR_COL S -I +S R S FALSE 3 -2010-05-05 -F5 +2010-02-24 +V8 B_ESCHR_COL R S @@ -541,10 +542,10 @@ 4 -2010-11-03 -F5 +2010-04-28 +V8 B_ESCHR_COL -S +R S S S @@ -552,32 +553,32 @@ 5 -2010-12-16 -F5 +2010-05-13 +V8 B_ESCHR_COL -S -S R S +S +S FALSE 6 -2011-02-23 -F5 +2010-05-14 +V8 B_ESCHR_COL S S S S -TRUE +FALSE 7 -2011-04-26 -F5 +2010-08-04 +V8 B_ESCHR_COL -S +R S S S @@ -585,8 +586,19 @@ 8 -2011-08-01 -F5 +2010-09-12 +V8 +B_ESCHR_COL +S +S +R +S +FALSE + + +9 +2010-10-03 +V8 B_ESCHR_COL R I @@ -594,42 +606,31 @@ S FALSE - -9 -2011-09-09 -F5 -B_ESCHR_COL -S -S -S -S -FALSE - 10 -2011-09-11 -F5 +2011-01-09 +V8 B_ESCHR_COL -S R S +R S FALSE -

Only 2 isolates are marked as ‘first’ according to CLSI guideline. But when reviewing the antibiogram, it is obvious that some isolates are absolutely different strains and should be included too. This is why we weigh isolates, based on their antibiogram. The key_antibiotics() function adds a vector with 18 key antibiotics: 6 broad spectrum ones, 6 small spectrum for Gram negatives and 6 small spectrum for Gram positives. These can be defined by the user.

+

Only 1 isolates are marked as ‘first’ according to CLSI guideline. But when reviewing the antibiogram, it is obvious that some isolates are absolutely different strains and should be included too. This is why we weigh isolates, based on their antibiogram. The key_antibiotics() function adds a vector with 18 key antibiotics: 6 broad spectrum ones, 6 small spectrum for Gram negatives and 6 small spectrum for Gram positives. These can be defined by the user.

If a column exists with a name like ‘key(…)ab’ the first_isolate() function will automatically use it and determine the first weighted isolates. Mind the NOTEs in below output:

data <- data %>% 
-  mutate(keyab = key_antibiotics(.)) %>% 
-  mutate(first_weighted = first_isolate(.))
+  mutate(keyab = key_antibiotics(.)) %>% 
+  mutate(first_weighted = first_isolate(.))
 #> NOTE: Using column `bacteria` as input for `col_mo`.
 #> 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`.
 #> 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,789 first weighted isolates (78.9% of total)
+#> => Found 15,783 first weighted isolates (78.9% of total) @@ -646,11 +647,11 @@ - - + + - + @@ -658,11 +659,11 @@ - - + + - + @@ -670,8 +671,8 @@ - - + + @@ -682,8 +683,32 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -693,9 +718,21 @@ - - - + + + + + + + + + + + + + + + @@ -704,34 +741,10 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + @@ -740,45 +753,32 @@ - - - - - - - - - - - - - - + + - +
isolate
12010-01-02F52010-01-15V8 B_ESCHR_COLR SR S S TRUE
22010-04-18F52010-02-08V8 B_ESCHR_COL SIS R S FALSE
32010-05-05F52010-02-24V8 B_ESCHR_COL R S
42010-11-03F52010-04-28V8B_ESCHR_COLRSSSFALSEFALSE
52010-05-13V8B_ESCHR_COLRSSSFALSEFALSE
62010-05-14V8 B_ESCHR_COL S STRUE
52010-12-16F572010-08-04V8B_ESCHR_COLRSSSFALSETRUE
82010-09-12V8 B_ESCHR_COL S SFALSE TRUE
62011-02-23F5B_ESCHR_COLSSSSTRUETRUE
72011-04-26F5B_ESCHR_COLSSSSFALSEFALSE
82011-08-01F592010-10-03V8 B_ESCHR_COL R IFALSE TRUE
92011-09-09F5B_ESCHR_COLSSSSFALSETRUE
102011-09-11F52011-01-09V8 B_ESCHR_COLS R SR S FALSE TRUE
-

Instead of 2, now 9 isolates are flagged. In total, 78.9% of all isolates are marked ‘first weighted’ - 50.7% 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 1, now 8 isolates are flagged. In total, 78.9% of all isolates are marked ‘first weighted’ - 50.7% 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:

data_1st <- data %>% 
   filter_first_weighted_isolate()
-

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

+

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

We can remove unneeded columns:

data_1st <- data_1st %>% 
-  select(-c(first, keyab))
+ select(-c(first, keyab))

Now our data looks like:

head(data_1st)
- @@ -795,12 +795,41 @@ - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -811,13 +840,12 @@ - - - - + + + - + @@ -827,30 +855,28 @@ - - - - - - - - + + + + - - - + + + + + + - - - - + + + - + @@ -858,38 +884,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
date patient_id hospital
12013-12-28M22012-04-28X6Hospital BB_ESCHR_COLRSRSFGram negativeEscherichiacoliTRUE
2012-12-23Z1Hospital AB_ESCHR_COLISRSFGram negativeEscherichiacoliTRUE
2011-05-27G10 Hospital B B_STRPTC_PNERS S S RTRUE
22017-03-17Z5Hospital B2012-08-19Q1Hospital D B_ESCHR_COLR SR S S FTRUE
32011-05-13G5Hospital BB_STRPTC_PNESSS2016-05-06Y2Hospital DB_KLBSL_PNE RMGram positiveStreptococcusSSSFGram negativeKlebsiella pneumoniae TRUE
52016-11-01C9Hospital D2010-08-27I8Hospital A B_ESCHR_COL R SRS S M Gram negativecoli TRUE
62010-12-16E2Hospital AB_STRPTC_PNESSSRMGram positiveStreptococcuspneumoniaeTRUE
72013-06-03G1Hospital AB_STRPTC_PNERSSRMGram positiveStreptococcuspneumoniaeTRUE

Time for the analysis!

@@ -907,9 +901,9 @@
freq(paste(data_1st$genus, data_1st$species))

Or can be used like the dplyr way, which is easier readable:

data_1st %>% freq(genus, species)
-

Frequency table of genus and species from a data.frame (15,789 x 13)
-Columns: 2
-Length: 15,789 (of which NA: 0 = 0.00%)
+

Frequency table of genus and species from a data.frame (15,783 x 13)

+

Columns: 2
+Length: 15,783 (of which NA: 0 = 0.00%)
Unique: 4

Shortest: 16
Longest: 24

@@ -926,33 +920,33 @@ Longest: 24

1 Escherichia coli -7,787 -49.3% -7,787 -49.3% +7,825 +49.6% +7,825 +49.6% 2 Staphylococcus aureus -3,922 -24.8% -11,709 -74.2% +3,979 +25.2% +11,804 +74.8% 3 Streptococcus pneumoniae -2,567 -16.3% -14,276 -90.4% +2,449 +15.5% +14,253 +90.3% 4 Klebsiella pneumoniae -1,513 -9.6% -15,789 +1,530 +9.7% +15,783 100.0% @@ -963,11 +957,11 @@ Longest: 24

Resistance percentages

The functions portion_R, portion_RI, portion_I, portion_IS and portion_S can be used to determine the portion of a specific antimicrobial outcome. They can be used on their own:

data_1st %>% portion_IR(amox)
-#> [1] 0.4746342
-

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

+#> [1] 0.4756383 +

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

data_1st %>% 
-  group_by(hospital) %>% 
-  summarise(amoxicillin = portion_IR(amox))
+ group_by(hospital) %>% + summarise(amoxicillin = portion_IR(amox)) @@ -976,26 +970,26 @@ Longest: 24

- + - + - + - +
hospital
Hospital A0.48245050.4697326
Hospital B0.46894690.4770958
Hospital C0.46898580.4799670
Hospital D0.47727990.4785082
-

Of course it would be very convenient to know the number of isolates responsible for the percentages. For that purpose the n_rsi() can be used, which works exactly like n_distinct() from the dplyr package. It counts all isolates available for every group (i.e. values S, I or R):

+

Of course it would be very convenient to know the number of isolates responsible for the percentages. For that purpose the n_rsi() can be used, which works exactly like n_distinct() from the dplyr package. It counts all isolates available for every group (i.e. values S, I or R):

data_1st %>% 
-  group_by(hospital) %>% 
-  summarise(amoxicillin = portion_IR(amox),
+  group_by(hospital) %>% 
+  summarise(amoxicillin = portion_IR(amox),
             available = n_rsi(amox))
@@ -1006,30 +1000,30 @@ Longest: 24

- - + + - - + + - - + + - - + +
Hospital A0.482450547010.46973264675
Hospital B0.468946955550.47709585523
Hospital C0.468985823860.47996702421
Hospital D0.477279931470.47850823164

These functions can also be used to get the portion of multiple antibiotics, to calculate co-resistance very easily:

data_1st %>% 
-  group_by(genus) %>% 
-  summarise(amoxicillin = portion_S(amcl),
+  group_by(genus) %>% 
+  summarise(amoxicillin = portion_S(amcl),
             gentamicin = portion_S(gent),
             "amox + gent" = portion_S(amcl, gent))
@@ -1042,67 +1036,67 @@ Longest: 24

- - - + + + - - - + + + - - - + + + - + - +
Escherichia0.73031980.90561190.97457300.73456870.90517570.9773802
Klebsiella0.73430270.89557170.96430930.73071900.90718950.9771242
Staphylococcus0.73049460.91968380.98215200.73536060.92108570.9816537
Streptococcus0.73081420.7268273 0.00000000.73081420.7268273

To make a transition to the next part, let’s see how this difference could be plotted:

data_1st %>% 
-  group_by(genus) %>% 
-  summarise("1. Amoxicillin" = portion_S(amcl),
+  group_by(genus) %>% 
+  summarise("1. Amoxicillin" = portion_S(amcl),
             "2. Gentamicin" = portion_S(gent),
             "3. Amox + gent" = portion_S(amcl, gent)) %>% 
-  tidyr::gather("Antibiotic", "S", -genus) %>%
-  ggplot(aes(x = genus,
+  tidyr::gather("Antibiotic", "S", -genus) %>%
+  ggplot(aes(x = genus,
              y = S,
              fill = Antibiotic)) +
-  geom_col(position = "dodge2")
+ geom_col(position = "dodge2")

Plots

To show results in plots, most R users would nowadays use the ggplot2 package. This package lets you create plots in layers. You can read more about it on their website. A quick example would look like these syntaxes:

-
ggplot(data = a_data_set,
-       mapping = aes(x = year,
+
ggplot(data = a_data_set,
+       mapping = aes(x = year,
                      y = value)) +
-  geom_col() +
-  labs(title = "A title",
+  geom_col() +
+  labs(title = "A title",
        subtitle = "A subtitle",
        x = "My X axis",
        y = "My Y axis")
 
-ggplot(a_data_set,
-       aes(year, value) +
-  geom_bar()
+ggplot(a_data_set, + aes(year, value) + + geom_bar()

The AMR package contains functions to extend this ggplot2 package, for example geom_rsi(). It automatically transforms data with count_df() or portion_df() and show results in stacked bars. Its simplest and shortest example:

-
ggplot(data_1st) +
+
ggplot(data_1st) +
   geom_rsi(translate_ab = FALSE)

Omit the translate_ab = FALSE to have the antibiotic codes (amox, amcl, cipr, gent) translated to official WHO names (amoxicillin, amoxicillin and betalactamase inhibitor, ciprofloxacin, gentamicin).

If we group on e.g. the genus column and add some additional functions from our package, we can create this:

+ theme(axis.text.y = element_text(face = "italic"))

To simplify this, we also created the ggplot_rsi() function, which combines almost all above functions:

data_1st %>% 
-  group_by(genus) %>%
+  group_by(genus) %>%
   ggplot_rsi(x = "genus",
              facet = "Antibiotic",
              breaks = 0:4 * 25,
              datalabels = FALSE) +
-  coord_flip()
+ coord_flip()

@@ -1158,12 +1152,12 @@ Longest: 24

We can transform the data and apply the test in only a couple of lines:

septic_patients %>%
-  filter(hospital_id %in% c("A", "D")) %>% # filter on only hospitals A and D
-  select(hospital_id, fosf) %>%            # select the hospitals and fosfomycin
-  group_by(hospital_id) %>%                # group on the hospitals
+  filter(hospital_id %in% c("A", "D")) %>% # filter on only hospitals A and D
+  select(hospital_id, fosf) %>%            # select the hospitals and fosfomycin
+  group_by(hospital_id) %>%                # group on the hospitals
   count_df(combine_IR = TRUE) %>%          # count all isolates per group (hospital_id)
-  tidyr::spread(hospital_id, Value) %>%    # transform output so A and D are columns
-  select(A, D) %>%                         # and select these only
+  tidyr::spread(hospital_id, Value) %>%    # transform output so A and D are columns
+  select(A, D) %>%                         # and select these only
   as.matrix() %>%                          # transform to good old matrix for fisher.test()
   fisher.test()                            # do Fisher's Exact Test
 #> 
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 eef839e2..f78b6151 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 989e41bf..9b94b88c 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 4bda66c7..430bdd63 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 995be60a..13afa548 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/EUCAST.html b/docs/articles/EUCAST.html
index 2187de6e..bc96c8ae 100644
--- a/docs/articles/EUCAST.html
+++ b/docs/articles/EUCAST.html
@@ -185,7 +185,7 @@
       

How to apply EUCAST rules

Matthijs S. Berends

-

11 February 2019

+

12 February 2019

diff --git a/docs/articles/atc_property.html b/docs/articles/atc_property.html index 80e89e8f..4c63ca08 100644 --- a/docs/articles/atc_property.html +++ b/docs/articles/atc_property.html @@ -185,7 +185,7 @@

How to get properties of an antibiotic

Matthijs S. Berends

-

11 February 2019

+

12 February 2019

diff --git a/docs/articles/mo_property.html b/docs/articles/mo_property.html index b6d725b1..812e42df 100644 --- a/docs/articles/mo_property.html +++ b/docs/articles/mo_property.html @@ -185,7 +185,7 @@

How to get properties of a microorganism

Matthijs S. Berends

-

11 February 2019

+

12 February 2019

diff --git a/docs/articles/resistance_predict.html b/docs/articles/resistance_predict.html index 07ec214e..b547d194 100644 --- a/docs/articles/resistance_predict.html +++ b/docs/articles/resistance_predict.html @@ -185,7 +185,7 @@

How to predict antimicrobial resistance

Matthijs S. Berends

-

11 February 2019

+

12 February 2019

@@ -295,7 +295,7 @@ Choosing the right model

Resistance is not easily predicted; if we look at vancomycin resistance in Gram positives, the spread (i.e. standard error) is enormous:

septic_patients %>%
-  filter(mo_gramstain(mo) == "Gram positive") %>%
+  filter(mo_gramstain(mo) == "Gram positive") %>%
   resistance_predict(col_ab = "vanc", year_min = 2010, info = FALSE) %>% 
   ggplot_rsi_predict()
 #> NOTE: Using column `date` as input for `col_date`.
@@ -340,7 +340,7 @@

For the vancomycin resistance in Gram positive bacteria, a linear model might be more appropriate since no (left half of a) binomial distribution is to be expected based on the observed years:

septic_patients %>%
-  filter(mo_gramstain(mo) == "Gram positive") %>%
+  filter(mo_gramstain(mo) == "Gram positive") %>%
   resistance_predict(col_ab = "vanc", year_min = 2010, info = FALSE, model = "linear") %>% 
   ggplot_rsi_predict()
 #> NOTE: Using column `date` as input for `col_date`.
diff --git a/docs/news/index.html b/docs/news/index.html index 201f3864..e1dcf835 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -798,7 +798,7 @@ These functions use as.atc()
  • Full support for Windows, Linux and macOS
  • Full support for old R versions, only R-3.0.0 (April 2013) or later is needed (needed packages may have other dependencies)
  • -
  • Function n_rsi to count cases where antibiotic test results were available, to be used in conjunction with dplyr::summarise, see ?rsi
  • +
  • Function n_rsi to count cases where antibiotic test results were available, to be used in conjunction with dplyr::summarise, see ?rsi
  • Function guess_bactid to determine the ID of a microorganism based on genus/species or known abbreviations like MRSA
  • Function guess_atc to determine the ATC of an antibiotic based on name, trade name, or known abbreviations
  • Function freq to create frequency tables, with additional info in a header