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 sir
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 sir, 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))
.
SIR values (see as.sir()
) 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
sir <- random_sir(10)
sir
#> Class 'sir'
#> [1] R I S S S S S I S R
mean_amr_distance(sir)
#> [1] 1.8973666 -0.4743416 -0.4743416 -0.4743416 -0.4743416 -0.4743416
#> [7] -0.4743416 -0.4743416 -0.4743416 1.8973666
mic <- random_mic(10)
mic
#> Class 'mic'
#> [1] 0.005 <=0.002 0.005 0.005 <=0.002 2 64 256 0.025
#> [10] 16
mean_amr_distance(mic)
#> [1] -0.7319933 -0.9284550 -0.7319933 -0.7319933 -0.9284550 0.5526353
#> [7] 1.2957230 1.5929580 -0.3869141 0.9984879
# equal to the Z-score of their log2:
(log2(mic) - mean(log2(mic))) / sd(log2(mic))
#> [1] -0.7319933 -0.9284550 -0.7319933 -0.7319933 -0.9284550 0.5526353
#> [7] 1.2957230 1.5929580 -0.3869141 0.9984879
disk <- random_disk(10)
disk
#> Class 'disk'
#> [1] 43 18 9 46 21 44 15 7 12 40
mean_amr_distance(disk)
#> [1] 1.1037355 -0.4730295 -1.0406649 1.2929473 -0.2838177 1.1668061
#> [7] -0.6622413 -1.1668061 -0.8514531 0.9145237
y <- data.frame(
id = LETTERS[1:10],
amox = random_sir(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 <=1 2
#> 2 B S 19 >=4 1
#> 3 C S 19 2 0.25
#> 4 D I 27 >=4 >=8
#> 5 E R 24 <=1 2
#> 6 F S 30 2 1
#> 7 G I 31 <=1 0.5
#> 8 H I 18 2 0.25
#> 9 I I 27 <=1 0.25
#> 10 J S 22 2 >=8
mean_amr_distance(y)
#> ℹ Calculating mean AMR distance based on columns "amox", "cipr", "gent" and
#> "tobr"
#> [1] 0.10395052 0.01413264 -0.56431322 0.81427297 0.35886977 0.25802215
#> [7] -0.13868335 -0.61529708 -0.47337526 0.24242087
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
#> 9 I I 27 <=1 0.25 -1.0562699
#> 7 G I 31 <=1 0.5 -0.7947569
#> 3 C S 19 2 0.25 -0.4224042
#> 8 H I 18 2 0.25 -0.4224042
#> 1 A R 19 <=1 2 -0.2717308
#> 5 E R 24 <=1 2 -0.2717308
#> 6 F S 30 2 1 0.1006218
#> 2 B S 19 >=4 1 0.7344875
#> 10 J S 22 2 >=8 0.8851609
#> 4 D I 27 >=4 >=8 1.5190266
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 19 2 0.25 -0.56431322 0.00000000
#> 2 H I 18 2 0.25 -0.61529708 0.05098385
#> 3 I I 27 <=1 0.25 -0.47337526 0.09093796
#> 4 G I 31 <=1 0.5 -0.13868335 0.42562987
#> 5 B S 19 >=4 1 0.01413264 0.57844586
#> 6 A R 19 <=1 2 0.10395052 0.66826374
#> 7 J S 22 2 >=8 0.24242087 0.80673409
#> 8 F S 30 2 1 0.25802215 0.82233538
#> 9 E R 24 <=1 2 0.35886977 0.92318299
#> 10 D I 27 >=4 >=8 0.81427297 1.37858619
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> <sir> <sir> <sir> <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