Calculates a normalised mean for antimicrobial resistance between multiple observations, to help to identify similar isolates without comparing antibiograms by hand.
Usage
mean_amr_distance(x, ...)
# S3 method for rsi
mean_amr_distance(x, ..., combine_SI = TRUE)
# S3 method for data.frame
mean_amr_distance(x, ..., combine_SI = TRUE)
amr_distance_from_row(amr_distance, row)
Arguments
- x
a vector of class rsi, mic or disk, or a data.frame containing columns of any of these classes
- ...
variables to select (supports tidyselect language such as
column1:column4
andwhere(is.mic)
, and can thus also be antibiotic selectors- combine_SI
a logical to indicate whether all values of S and I must be merged into one, so the input only consists of S+I vs. R (susceptible vs. resistant), defaults to
TRUE
- amr_distance
the outcome of
mean_amr_distance()
- row
an index, such as a row number
Details
The mean AMR distance is effectively the Z-score; a normalised numeric value to compare AMR test results which can help to identify similar isolates, without comparing antibiograms by hand.
MIC values (see as.mic()
) are transformed with log2()
first; their distance is thus calculated as (log2(x) - mean(log2(x))) / sd(log2(x))
.
R/SI values (see as.rsi()
) are transformed using "S"
= 1, "I"
= 2, and "R"
= 3. If combine_SI
is TRUE
(default), the "I"
will be considered to be 1.
For data sets, the mean AMR distance will be calculated per column, after which the mean per row will be returned, see Examples.
Use amr_distance_from_row()
to subtract distances from the distance of one row, see Examples.
Interpretation
Isolates with distances less than 0.01 difference from each other should be considered similar. Differences lower than 0.025 should be considered suspicious.
Examples
rsi <- random_rsi(10)
rsi
#> Class 'rsi'
#> [1] S I S S R S S S I R
mean_amr_distance(rsi)
#> [1] -0.4743416 -0.4743416 -0.4743416 -0.4743416 1.8973666 -0.4743416
#> [7] -0.4743416 -0.4743416 -0.4743416 1.8973666
mic <- random_mic(10)
mic
#> Class 'mic'
#> [1] >=64 0.25 16 0.001 0.125 32 0.125 0.001 0.002 8
mean_amr_distance(mic)
#> [1] 1.26281480 -0.01764874 0.94269891 -1.29263579 -0.17770669 1.10275686
#> [7] -0.17770669 -1.29263579 -1.13257785 0.78264097
# equal to the Z-score of their log2:
(log2(mic) - mean(log2(mic))) / sd(log2(mic))
#> [1] 1.26281480 -0.01764874 0.94269891 -1.29263579 -0.17770669 1.10275686
#> [7] -0.17770669 -1.29263579 -1.13257785 0.78264097
disk <- random_disk(10)
disk
#> Class 'disk'
#> [1] 17 29 32 37 48 40 28 37 40 49
mean_amr_distance(disk)
#> [1] -1.9401430 -0.6951315 -0.3838786 0.1348763 1.2761369 0.4461291
#> [7] -0.7988824 0.1348763 0.4461291 1.3798878
y <- data.frame(
id = LETTERS[1:10],
amox = random_rsi(10, ab = "amox", mo = "Escherichia coli"),
cipr = random_disk(10, ab = "cipr", mo = "Escherichia coli"),
gent = random_mic(10, ab = "gent", mo = "Escherichia coli"),
tobr = random_mic(10, ab = "tobr", mo = "Escherichia coli")
)
y
#> id amox cipr gent tobr
#> 1 A R 23 >=16 2
#> 2 B I 24 1 4
#> 3 C S 24 8 0.25
#> 4 D R 18 2 4
#> 5 E S 17 4 1
#> 6 F S 22 8 2
#> 7 G R 29 2 0.25
#> 8 H S 28 >=16 1
#> 9 I I 31 4 1
#> 10 J S 20 >=16 0.5
mean_amr_distance(y)
#> ℹ Calculating mean AMR distance based on columns "amox", "cipr", "gent" and
#> "tobr"
#> [1] 0.76502575 -0.22559420 -0.39112580 0.14405101 -0.59747772 0.01884049
#> [7] 0.04569124 0.34391737 0.15560237 -0.25893050
y$amr_distance <- mean_amr_distance(y, where(is.mic))
#> ℹ Calculating mean AMR distance based on columns "gent" and "tobr"
y[order(y$amr_distance), ]
#> id amox cipr gent tobr amr_distance
#> 7 G R 29 2 0.25 -1.2141339
#> 3 C S 24 8 0.25 -0.5147553
#> 2 B I 24 1 4 -0.1836920
#> 5 E S 17 4 1 -0.1743790
#> 9 I I 31 4 1 -0.1743790
#> 4 D R 18 2 4 0.1659973
#> 10 J S 20 >=16 0.5 0.1799668
#> 6 F S 22 8 2 0.5203431
#> 8 H S 28 >=16 1 0.5249996
#> 1 A R 23 >=16 2 0.8700324
if (require("dplyr")) {
y %>%
mutate(
amr_distance = mean_amr_distance(y),
check_id_C = amr_distance_from_row(amr_distance, id == "C")
) %>%
arrange(check_id_C)
}
#> ℹ Calculating mean AMR distance based on columns "amox", "cipr", "gent" and
#> "tobr"
#> id amox cipr gent tobr amr_distance check_id_C
#> 1 C S 24 8 0.25 -0.39112580 0.0000000
#> 2 J S 20 >=16 0.5 -0.25893050 0.1321953
#> 3 B I 24 1 4 -0.22559420 0.1655316
#> 4 E S 17 4 1 -0.59747772 0.2063519
#> 5 F S 22 8 2 0.01884049 0.4099663
#> 6 G R 29 2 0.25 0.04569124 0.4368170
#> 7 D R 18 2 4 0.14405101 0.5351768
#> 8 I I 31 4 1 0.15560237 0.5467282
#> 9 H S 28 >=16 1 0.34391737 0.7350432
#> 10 A R 23 >=16 2 0.76502575 1.1561516
if (require("dplyr")) {
# support for groups
example_isolates %>%
filter(mo_genus() == "Enterococcus" & mo_species() != "") %>%
select(mo, TCY, carbapenems()) %>%
group_by(mo) %>%
mutate(dist = mean_amr_distance(.)) %>%
arrange(mo, dist)
}
#> ℹ Using column 'mo' as input for mo_genus()
#> ℹ Using column 'mo' as input for mo_species()
#> ℹ For carbapenems() using columns 'IPM' (imipenem) and 'MEM' (meropenem)
#> ℹ Calculating mean AMR distance based on columns "TCY", "IPM" and "MEM"
#> # A tibble: 63 × 5
#> # Groups: mo [4]
#> mo TCY IPM MEM dist
#> <mo> <rsi> <rsi> <rsi> <dbl>
#> 1 B_ENTRC_AVIM S S NA 0
#> 2 B_ENTRC_AVIM S S NA 0
#> 3 B_ENTRC_CSSL NA S NA NA
#> 4 B_ENTRC_FACM S S NA -2.66
#> 5 B_ENTRC_FACM S R R -0.423
#> 6 B_ENTRC_FACM S R R -0.423
#> 7 B_ENTRC_FACM NA R R 0.224
#> 8 B_ENTRC_FACM NA R R 0.224
#> 9 B_ENTRC_FACM NA R R 0.224
#> 10 B_ENTRC_FACM NA R R 0.224
#> # … with 53 more rows