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] I S I S R R R I I R
mean_amr_distance(rsi)
#> [1] -0.7745967 -0.7745967 -0.7745967 -0.7745967 1.1618950 1.1618950
#> [7] 1.1618950 -0.7745967 -0.7745967 1.1618950
mic <- random_mic(10)
mic
#> Class 'mic'
#> [1] 32 2 32 0.005 0.25 0.0625 1 8 64
#> [10] <=0.001
mean_amr_distance(mic)
#> [1] 0.97437642 0.24736060 0.97437642 -1.32369446 -0.29790127 -0.66140918
#> [7] 0.06560664 0.61086851 1.15613038 -1.74571407
# equal to the Z-score of their log2:
(log2(mic) - mean(log2(mic))) / sd(log2(mic))
#> [1] 0.97437642 0.24736060 0.97437642 -1.32369446 -0.29790127 -0.66140918
#> [7] 0.06560664 0.61086851 1.15613038 -1.74571407
disk <- random_disk(10)
disk
#> Class 'disk'
#> [1] 39 47 22 16 48 33 29 18 27 33
mean_amr_distance(disk)
#> [1] 0.7018456 1.4216873 -0.8278179 -1.3676991 1.5116675 0.1619644
#> [7] -0.1979565 -1.1877387 -0.3779169 0.1619644
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 19 >=8 1
#> 2 B S 22 <=0.5 1
#> 3 C R 26 <=0.5 1
#> 4 D S 22 2 <=0.25
#> 5 E S 30 4 4
#> 6 F R 19 >=8 16
#> 7 G R 18 2 4
#> 8 H S 20 2 1
#> 9 I R 29 <=0.5 2
#> 10 J R 28 <=0.5 0.5
mean_amr_distance(y)
#> ℹ Calculating mean AMR distance based on columns "amox", "cipr", "gent" and
#> "tobr"
#> [1] 0.221045968 -0.709243910 -0.005209427 -0.695630697 0.480841798
#> [6] 0.804920176 0.152454865 -0.513649375 0.305692799 -0.041222197
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
#> 10 J R 28 <=0.5 0.5 -0.98653490
#> 2 B S 22 <=0.5 1 -0.69459780
#> 3 C R 26 <=0.5 1 -0.69459780
#> 4 D S 22 2 <=0.25 -0.66737138
#> 9 I R 29 <=0.5 2 -0.40266070
#> 8 H S 20 2 1 -0.08349717
#> 7 G R 18 2 4 0.50037704
#> 1 A R 19 >=8 1 0.52760347
#> 5 E S 30 4 4 0.80592736
#> 6 F R 19 >=8 16 1.69535188
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 R 26 <=0.5 1 -0.005209427 0.00000000
#> 2 J R 28 <=0.5 0.5 -0.041222197 0.03601277
#> 3 G R 18 2 4 0.152454865 0.15766429
#> 4 A R 19 >=8 1 0.221045968 0.22625539
#> 5 I R 29 <=0.5 2 0.305692799 0.31090223
#> 6 E S 30 4 4 0.480841798 0.48605122
#> 7 H S 20 2 1 -0.513649375 0.50843995
#> 8 D S 22 2 <=0.25 -0.695630697 0.69042127
#> 9 B S 22 <=0.5 1 -0.709243910 0.70403448
#> 10 F R 19 >=8 16 0.804920176 0.81012960
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