1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-28 04:46:11 +01:00
AMR/vignettes/WHONET.Rmd

100 lines
3.6 KiB
Plaintext
Raw Normal View History

2019-01-29 00:06:50 +01:00
---
title: "How to work with WHONET data"
output:
rmarkdown::html_vignette:
toc: true
toc_depth: 3
vignette: >
%\VignetteIndexEntry{How to work with WHONET data}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: console
---
```{r setup, include = FALSE, results = 'markup'}
knitr::opts_chunk$set(
collapse = TRUE,
2023-02-18 11:57:17 +01:00
comment = "#>",
2019-01-29 00:06:50 +01:00
fig.width = 7.5,
fig.height = 4.5
)
```
2019-11-09 11:33:22 +01:00
### Import of data
2019-01-29 20:20:09 +01:00
This tutorial assumes you already imported the WHONET data with e.g. the [`readxl` package](https://readxl.tidyverse.org/). In RStudio, this can be done using the menu button 'Import Dataset' in the tab 'Environment'. Choose the option 'From Excel' and select your exported file. Make sure date fields are imported correctly.
An example syntax could look like this:
```{r, eval = FALSE}
library(readxl)
data <- read_excel(path = "path/to/your/file.xlsx")
```
This package comes with an [example data set `WHONET`](https://msberends.github.io/AMR/reference/WHONET.html). We will use it for this analysis.
2019-01-29 20:20:09 +01:00
2019-11-09 11:33:22 +01:00
### Preparation
2019-01-29 20:20:09 +01:00
First, load the relevant packages if you did not yet did this. I use the tidyverse for all of my analyses. All of them. If you don't know it yet, I suggest you read about it on their website: https://www.tidyverse.org/.
```{r, message = FALSE}
2022-08-28 10:31:50 +02:00
library(dplyr) # part of tidyverse
2019-01-29 21:25:16 +01:00
library(ggplot2) # part of tidyverse
2022-08-28 10:31:50 +02:00
library(AMR) # this package
2020-05-19 14:16:45 +02:00
library(cleaner) # to create frequency tables
2019-01-29 20:20:09 +01:00
```
We will have to transform some variables to simplify and automate the analysis:
2021-06-01 15:33:06 +02:00
* Microorganisms should be transformed to our own microorganism codes (called an `mo`) using [our Catalogue of Life reference data set](https://msberends.github.io/AMR/reference/catalogue_of_life), which contains all ~70,000 microorganisms from the taxonomic kingdoms Bacteria, Fungi and Protozoa. We do the tranformation with `as.mo()`. This function also recognises almost all WHONET abbreviations of microorganisms.
2023-01-21 23:47:20 +01:00
* Antimicrobial results or interpretations have to be clean and valid. In other words, they should only contain values `"S"`, `"I"` or `"R"`. That is exactly where the `as.sir()` function is for.
2019-01-29 20:20:09 +01:00
```{r}
# transform variables
data <- WHONET %>%
# get microbial ID based on given organism
2022-08-28 10:31:50 +02:00
mutate(mo = as.mo(Organism)) %>%
2023-01-21 23:47:20 +01:00
# transform everything from "AMP_ND10" to "CIP_EE" to the new `sir` class
mutate_at(vars(AMP_ND10:CIP_EE), as.sir)
2019-01-29 20:20:09 +01:00
```
2019-07-29 13:33:48 +02:00
No errors or warnings, so all values are transformed succesfully.
2020-05-19 14:16:45 +02:00
We also created a package dedicated to data cleaning and checking, called the `cleaner` package. Its `freq()` function can be used to create frequency tables.
2019-07-29 13:33:48 +02:00
So let's check our data, with a couple of frequency tables:
2019-01-29 20:20:09 +01:00
```{r, results = 'asis'}
2019-11-29 19:43:23 +01:00
# our newly created `mo` variable, put in the mo_name() function
data %>% freq(mo_name(mo), nmax = 10)
```
```{r, results = 'asis'}
2019-01-29 20:20:09 +01:00
# our transformed antibiotic columns
# amoxicillin/clavulanic acid (J01CR02) as an example
data %>% freq(AMC_ND2)
```
2019-11-09 11:33:22 +01:00
### A first glimpse at results
2019-01-29 20:20:09 +01:00
2023-01-21 23:47:20 +01:00
An easy `ggplot` will already give a lot of information, using the included `ggplot_sir()` function:
2019-01-29 20:20:09 +01:00
2019-11-29 19:43:23 +01:00
```{r, eval = FALSE}
data %>%
group_by(Country) %>%
select(Country, AMP_ND2, AMC_ED20, CAZ_ED10, CIP_ED5) %>%
2023-01-21 23:47:20 +01:00
ggplot_sir(translate_ab = "ab", facet = "Country", datalabels = FALSE)
2019-11-29 19:43:23 +01:00
```
```{r, echo = FALSE}
# on very old and some new releases of R, this may lead to an error
2023-01-23 15:01:21 +01:00
tryCatch(
data %>%
group_by(Country) %>%
select(Country, AMP_ND2, AMC_ED20, CAZ_ED10, CIP_ED5) %>%
ggplot_sir(translate_ab = "ab", facet = "Country", datalabels = FALSE) %>%
print(),
error = function(e) base::invisible()
2022-08-28 10:31:50 +02:00
)
2019-11-09 11:33:22 +01:00
```