AMR/vignettes/AMR.Rmd

334 lines
14 KiB
Plaintext
Raw Normal View History

---
title: "How to conduct AMR data analysis"
output:
rmarkdown::html_vignette:
2018-12-29 22:24:19 +01:00
toc: true
2019-01-02 23:24:07 +01:00
toc_depth: 3
vignette: >
%\VignetteIndexEntry{How to conduct AMR data analysis}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: console
---
```{r setup, include = FALSE, results = 'markup'}
knitr::opts_chunk$set(
warning = FALSE,
collapse = TRUE,
2023-02-18 11:57:17 +01:00
comment = "#>",
2019-01-02 23:24:07 +01:00
fig.width = 7.5,
2019-02-11 10:27:10 +01:00
fig.height = 5
)
```
2019-03-27 11:22:36 +01:00
**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](https://rmarkdown.rstudio.com/). However, the methodology remains unchanged. This page was generated on `r format(Sys.Date(), "%d %B %Y")`.
2019-02-01 16:55:55 +01:00
# Introduction
Conducting AMR data analysis unfortunately requires in-depth knowledge from different scientific fields, which makes it hard to do right. At least, it requires:
2019-12-20 21:06:39 +01:00
2023-02-26 21:26:58 +01:00
* Good questions (always start with those!) and reliable data
2019-12-21 10:56:06 +01:00
* A thorough understanding of (clinical) epidemiology, to understand the clinical and epidemiological relevance and possible bias of results
2020-05-16 13:05:47 +02:00
* A thorough understanding of (clinical) microbiology/infectious diseases, to understand which microorganisms are causal to which infections and the implications of pharmaceutical treatment, as well as understanding intrinsic and acquired microbial resistance
2023-01-21 23:47:20 +01:00
* Experience with data analysis with microbiological tests and their results, to understand the determination and limitations of MIC values and their interpretations to SIR values
2019-12-21 10:56:06 +01:00
* Availability of the biological taxonomy of microorganisms and probably normalisation factors for pharmaceuticals, such as defined daily doses (DDD)
* Available (inter-)national guidelines, and profound methods to apply them
2019-12-20 21:06:39 +01:00
2019-12-21 10:56:06 +01:00
Of course, we cannot instantly provide you with knowledge and experience. But with this `AMR` package, we aimed at providing (1) tools to simplify antimicrobial resistance data cleaning, transformation and analysis, (2) methods to easily incorporate international guidelines and (3) scientifically reliable reference data, including the requirements mentioned above.
The `AMR` package enables standardised and reproducible AMR data analysis, with the application of evidence-based rules, determination of first isolates, translation of various codes for microorganisms and antimicrobial agents, determination of (multi-drug) resistant microorganisms, and calculation of antimicrobial resistance, prevalence and future trends.
2019-12-20 21:06:39 +01:00
# Preparation
For this tutorial, we will create fake demonstration data to work with.
You can skip to [Cleaning the data](#cleaning-the-data) if you already have your own data ready. If you start your analysis, try to make the structure of your data generally look like this:
```{r example table, echo = FALSE, results = 'asis'}
2023-01-23 15:01:21 +01:00
knitr::kable(
data.frame(
date = Sys.Date(),
patient_id = c("abcd", "abcd", "efgh"),
mo = "Escherichia coli",
AMX = c("S", "S", "R"),
CIP = c("S", "R", "S"),
stringsAsFactors = FALSE
),
align = "c"
2022-08-28 10:31:50 +02:00
)
```
## Needed R packages
2023-02-26 21:26:58 +01:00
As with many uses in R, we need some additional packages for AMR data analysis. Our package works closely together with the [tidyverse packages](https://www.tidyverse.org) [`dplyr`](https://dplyr.tidyverse.org/) and [`ggplot2`](https://ggplot2.tidyverse.org) by RStudio. The tidyverse tremendously improves the way we conduct data science - it allows for a very natural way of writing syntaxes and creating beautiful plots in R.
2020-05-19 14:16:45 +02:00
We will also use the `cleaner` package, that can be used for cleaning data and creating frequency tables.
2020-05-16 20:08:21 +02:00
```{r lib packages, message = FALSE, warning = FALSE, results = 'asis'}
2019-01-26 23:22:56 +01:00
library(dplyr)
library(ggplot2)
library(AMR)
# (if not yet installed, install with:)
2023-02-26 21:26:58 +01:00
# install.packages(c("dplyr", "ggplot2", "AMR"))
2020-05-16 13:05:47 +02:00
```
2023-02-26 21:26:58 +01:00
The `AMR` package contains a data set `example_isolates_unclean`, which might look data that users have extracted from their laboratory systems:
2023-02-26 21:26:58 +01:00
```{r}
example_isolates_unclean
2023-02-26 21:26:58 +01:00
# we will use 'our_data' as the data set name for this tutorial
our_data <- example_isolates_unclean
```
2023-02-26 21:26:58 +01:00
For AMR data analysis, we would like the microorganism column to contain valid, up-to-date taxonomy, and the antibiotic columns to be cleaned as SIR values as well.
2018-12-31 10:35:26 +01:00
2023-02-26 21:26:58 +01:00
## Taxonomy of microorganisms
2018-12-31 10:35:26 +01:00
2023-02-26 21:26:58 +01:00
With `as.mo()`, users can transform arbitrary microorganism names or codes to current taxonomy. The `AMR` package contains up-to-date taxonomic data. To be specific, currently included data were retrieved on `r format(AMR:::TAXONOMY_VERSION$LPSN$accessed_date, "%d %b %Y")`.
2023-02-26 21:26:58 +01:00
The codes of the AMR packages that come from `as.mo()` are short, but still human readable. More importantly, `as.mo()` supports all kinds of input:
2023-02-26 21:26:58 +01:00
```{r, message = FALSE}
as.mo("Klebsiella pneumoniae")
as.mo("K. pneumoniae")
as.mo("KLEPNE")
as.mo("KLPN")
```
2023-02-26 21:26:58 +01:00
The first character in above codes denote their taxonomic kingdom, such as Bacteria (B), Fungi (F), and Protozoa (P).
2023-02-26 21:26:58 +01:00
The `AMR` package also contain functions to directly retrieve taxonomic properties, such as the name, genus, species, family, order, and even Gram-stain. They all start with `mo_` and they use `as.mo()` internally, so that still any arbitrary user input can be used:
2023-02-26 21:26:58 +01:00
```{r, message = FALSE}
mo_family("K. pneumoniae")
mo_genus("K. pneumoniae")
mo_species("K. pneumoniae")
2023-02-26 21:26:58 +01:00
mo_gramstain("Klebsiella pneumoniae")
2023-02-26 21:26:58 +01:00
mo_ref("K. pneumoniae")
2018-12-31 10:35:26 +01:00
2023-02-26 21:26:58 +01:00
mo_snomed("K. pneumoniae")
2018-12-31 10:35:26 +01:00
```
2023-02-26 21:26:58 +01:00
Now we can thus clean our data:
2019-02-11 10:27:10 +01:00
2023-02-26 21:26:58 +01:00
```{r, echo = FALSE, message = FALSE}
mo_reset_session()
```
2023-02-26 21:26:58 +01:00
```{r, message = TRUE}
our_data$bacteria <- as.mo(our_data$bacteria, info = TRUE)
```
2023-02-26 21:26:58 +01:00
Apparently, there was some uncertainty about the translation to taxonomic codes. Let's check this:
2019-07-29 13:33:48 +02:00
2023-02-26 21:26:58 +01:00
```{r}
mo_uncertainties()
```
2023-02-26 21:26:58 +01:00
That's all good.
2023-02-26 21:26:58 +01:00
## Antibiotic results
2023-02-26 21:26:58 +01:00
The column with antibiotic test results must also be cleaned. The `AMR` package comes with three new data types to work with such test results: `mic` for minimal inhibitory concentrations (MIC), `disk` for disk diffusion diameters, and `sir` for SIR data that have been interpreted already. This package can also determine SIR values based on MIC or disk diffusion values, read more about that on the `as.sir()` page.
2023-02-26 21:26:58 +01:00
For now, we will just clean the SIR columns in our data using dplyr:
2021-05-24 15:29:17 +02:00
2023-02-26 21:26:58 +01:00
```{r}
# method 1, be explicit about the columns:
2023-03-11 14:24:34 +01:00
our_data <- our_data %>%
2023-02-26 21:26:58 +01:00
mutate_at(vars(AMX:GEN), as.sir)
2023-02-26 21:26:58 +01:00
# method 2, let the AMR package determine the eligible columns
2023-03-11 14:24:34 +01:00
our_data <- our_data %>%
2023-02-26 21:26:58 +01:00
mutate_if(is_sir_eligible, as.sir)
2023-02-26 21:26:58 +01:00
# result:
our_data
```
2023-02-26 21:26:58 +01:00
This is basically it for the cleaning, time to start the data inclusion.
2019-02-01 16:55:55 +01:00
## First isolates
2023-02-26 21:26:58 +01:00
We need to know which isolates we can *actually* use for analysis without repetition bias.
2020-10-08 11:16:03 +02:00
To conduct an analysis of antimicrobial resistance, you must [only include the first isolate of every patient per episode](https:/pubmed.ncbi.nlm.nih.gov/17304462/) (Hindler *et al.*, Clin Infect Dis. 2007). If you would not do this, you could easily get an overestimate or underestimate of the resistance of an antibiotic. Imagine that a patient was admitted with an MRSA and that it was found in 5 different blood cultures the following weeks (yes, some countries like the Netherlands have these blood drawing policies). The resistance percentage of oxacillin of all \emph{S. aureus} isolates would be overestimated, because you included this MRSA more than once. It would clearly be [selection bias](https://en.wikipedia.org/wiki/Selection_bias).
The Clinical and Laboratory Standards Institute (CLSI) appoints this as follows:
> *(...) When preparing a cumulative antibiogram to guide clinical decisions about empirical antimicrobial therapy of initial infections, **only the first isolate of a given species per patient, per analysis period (eg, one year) should be included, irrespective of body site, antimicrobial susceptibility profile, or other phenotypical characteristics (eg, biotype)**. The first isolate is easily identified, and cumulative antimicrobial susceptibility test data prepared using the first isolate are generally comparable to cumulative antimicrobial susceptibility test data calculated by other methods, providing duplicate isolates are excluded.*
2019-01-02 23:24:07 +01:00
<br>[M39-A4 Analysis and Presentation of Cumulative Antimicrobial Susceptibility Test Data, 4th Edition. CLSI, 2014. Chapter 6.4](https://clsi.org/standards/products/microbiology/documents/m39/)
2023-02-26 21:26:58 +01:00
This `AMR` package includes this methodology with the `first_isolate()` function and is able to apply the four different methods as defined by [Hindler *et al.* in 2007](https://academic.oup.com/cid/article/44/6/867/364325): phenotype-based, episode-based, patient-based, isolate-based. The right method depends on your goals and analysis, but the default phenotype-based method is in any case the method to properly correct for most duplicate isolates. Read more about the methods on the `first_isolate()` page.
2021-05-24 15:29:17 +02:00
The outcome of the function can easily be added to our data:
```{r 1st isolate}
2023-02-26 21:26:58 +01:00
our_data <- our_data %>%
2021-05-24 15:29:17 +02:00
mutate(first = first_isolate(info = TRUE))
```
2023-02-26 21:26:58 +01:00
So only `r round((sum(our_data$first) / nrow(our_data) * 100))`% is suitable for resistance analysis! We can now filter on it with the `filter()` function, also from the `dplyr` package:
```{r 1st isolate filter}
2023-02-26 21:26:58 +01:00
our_data_1st <- our_data %>%
filter(first == TRUE)
```
2021-05-24 15:29:17 +02:00
For future use, the above two syntaxes can be shortened:
2019-02-11 10:27:10 +01:00
2021-05-24 15:29:17 +02:00
```{r 1st isolate filter 2}
2023-02-26 21:26:58 +01:00
our_data_1st <- our_data %>%
filter_first_isolate()
```
2023-02-26 21:26:58 +01:00
So we end up with `r format(nrow(our_data_1st), big.mark = " ")` isolates for analysis. Now our data looks like:
2023-02-26 21:26:58 +01:00
```{r preview data set 3}
our_data_1st
```
2023-02-26 21:26:58 +01:00
Time for the analysis.
2019-02-01 16:55:55 +01:00
# Analysing the data
2019-01-02 23:24:07 +01:00
2023-02-26 21:26:58 +01:00
The base R `summary()` function gives a good first impression, as it comes with support for the new `mo` and `sir` classes that we now have in our data set:
2019-01-02 23:24:07 +01:00
```{r}
2023-02-26 21:26:58 +01:00
summary(our_data_1st)
2019-01-02 23:24:07 +01:00
2023-02-26 21:26:58 +01:00
glimpse(our_data_1st)
2019-01-02 23:24:07 +01:00
2023-02-26 21:26:58 +01:00
# number of unique values per column:
sapply(our_data_1st, n_distinct)
2019-01-02 23:24:07 +01:00
```
2023-02-26 21:26:58 +01:00
## Availability of species
2019-01-02 23:24:07 +01:00
2023-02-26 21:26:58 +01:00
To just get an idea how the species are distributed, create a frequency table with `count()` based on the name of the microorganisms:
2019-01-02 23:24:07 +01:00
2023-02-26 21:26:58 +01:00
```{r freq 1}
2023-03-11 14:24:34 +01:00
our_data %>%
2023-02-26 21:26:58 +01:00
count(mo_name(bacteria), sort = TRUE)
2019-01-02 23:24:07 +01:00
2023-03-11 14:24:34 +01:00
our_data_1st %>%
2023-02-26 21:26:58 +01:00
count(mo_name(bacteria), sort = TRUE)
2019-01-02 23:24:07 +01:00
```
2023-02-26 21:26:58 +01:00
## Select and filter with antibiotic selectors
2023-02-26 21:26:58 +01:00
Using so-called antibiotic class selectors, you can select or filter columns based on the antibiotic class that your antibiotic results are in:
2023-02-26 21:26:58 +01:00
```{r bug_drg 2a}
our_data_1st %>%
select(date, aminoglycosides())
2019-01-02 23:24:07 +01:00
2023-02-26 21:26:58 +01:00
our_data_1st %>%
select(bacteria, betalactams())
2019-01-02 23:24:07 +01:00
2023-02-26 21:26:58 +01:00
our_data_1st %>%
select(bacteria, where(is.sir))
2019-02-11 10:27:10 +01:00
2023-02-26 21:26:58 +01:00
# filtering using AB selectors is also possible:
our_data_1st %>%
filter(any(aminoglycosides() == "R"))
2019-01-02 23:24:07 +01:00
2023-02-26 21:26:58 +01:00
our_data_1st %>%
filter(all(betalactams() == "R"))
2019-01-02 23:24:07 +01:00
2023-02-26 21:26:58 +01:00
# even works in base R (since R 3.0):
our_data_1st[all(betalactams() == "R"), ]
2019-01-02 23:24:07 +01:00
```
2023-02-26 21:26:58 +01:00
## Generate antibiograms
2023-02-26 21:26:58 +01:00
This package comes with `antibiogram()`, a function that automatically generates traditional, combined, syndromic, and even weighted-incidence syndromic combination antibiograms (WISCA). For R Markdown (such as this page) it automatically prints in the right table format.
2023-02-26 21:26:58 +01:00
Below are some suggestions for how to generate the different antibiograms:
2023-02-26 21:26:58 +01:00
```{r}
# traditional:
antibiogram(our_data_1st)
antibiogram(our_data_1st,
2023-03-11 14:24:34 +01:00
ab_transform = "name"
)
2023-02-26 21:26:58 +01:00
antibiogram(our_data_1st,
2023-03-11 14:24:34 +01:00
ab_transform = "name",
language = "es"
) # support for 20 languages
```
2023-02-26 21:26:58 +01:00
```{r}
# combined:
antibiogram(our_data_1st,
2023-03-11 14:24:34 +01:00
antibiotics = c("AMC", "AMC+CIP", "AMC+GEN")
)
```
2023-02-26 21:26:58 +01:00
```{r}
# for a syndromic antibiogram, we must fake some clinical conditions:
our_data_1st$condition <- sample(c("Cardial", "Respiratory", "Rheumatic"),
2023-03-11 14:24:34 +01:00
size = nrow(our_data_1st),
replace = TRUE
)
2023-02-26 21:26:58 +01:00
# syndromic:
antibiogram(our_data_1st,
2023-03-11 14:24:34 +01:00
syndromic_group = "condition"
)
2023-02-26 21:26:58 +01:00
antibiogram(our_data_1st,
2023-03-11 14:24:34 +01:00
# you can use AB selectors here as well:
antibiotics = c(penicillins(), aminoglycosides()),
syndromic_group = "condition",
mo_transform = "gramstain"
)
```
2023-02-26 21:26:58 +01:00
```{r}
2023-03-11 14:24:34 +01:00
# WISCA:
2023-02-26 21:26:58 +01:00
# (we lack some details, but it could contain a filter on e.g. >65 year-old males)
wisca <- antibiogram(our_data_1st,
2023-03-11 14:24:34 +01:00
antibiotics = c("AMC", "AMC+CIP", "AMC+GEN"),
syndromic_group = "condition",
mo_transform = "gramstain"
)
2023-02-26 21:26:58 +01:00
wisca
```
2023-02-26 21:26:58 +01:00
Antibiograms can be plotted using `autoplot()` from the `ggplot2` packages, since this package provides an extension to that function:
2023-02-26 21:26:58 +01:00
```{r}
autoplot(wisca)
```
2023-02-26 21:26:58 +01:00
To calculate antimicrobial resistance in a more sensible way, also by correcting for too few results, we use the `resistance()` and `susceptibility()` functions.
2019-01-02 23:24:07 +01:00
2023-02-26 21:26:58 +01:00
## Resistance percentages
2019-01-02 23:24:07 +01:00
2023-02-26 21:26:58 +01:00
The functions `resistance()` and `susceptibility()` can be used to calculate antimicrobial resistance or susceptibility. For more specific analyses, the functions `proportion_S()`, `proportion_SI()`, `proportion_I()`, `proportion_IR()` and `proportion_R()` can be used to determine the proportion of a specific antimicrobial outcome.
2019-01-02 23:24:07 +01:00
2023-02-26 21:26:58 +01:00
All these functions contain a `minimum` argument, denoting the minimum required number of test results for returning a value. These functions will otherwise return `NA`. The default is `minimum = 30`, following the [CLSI M39-A4 guideline](https://clsi.org/standards/products/microbiology/documents/m39/) for applying microbial epidemiology.
2019-11-11 10:46:39 +01:00
2023-02-26 21:26:58 +01:00
As per the EUCAST guideline of 2019, we calculate resistance as the proportion of R (`proportion_R()`, equal to `resistance()`) and susceptibility as the proportion of S and I (`proportion_SI()`, equal to `susceptibility()`). These functions can be used on their own:
2023-02-26 21:26:58 +01:00
```{r}
our_data_1st %>% resistance(AMX)
```
2023-02-26 21:26:58 +01:00
Or can be used in conjunction with `group_by()` and `summarise()`, both from the `dplyr` package:
```{r}
2023-02-26 21:26:58 +01:00
our_data_1st %>%
group_by(hospital) %>%
summarise(amoxicillin = resistance(AMX))
2019-01-02 23:24:07 +01:00
```
2023-02-18 11:57:17 +01:00
----
2023-02-26 21:26:58 +01:00
*Author: Dr. Matthijs Berends, 26th Feb 2023*