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) - the default is
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] I R I I R R R I R I
mean_amr_distance(sir)
#> [1] -0.9486833 0.9486833 -0.9486833 -0.9486833 0.9486833 0.9486833
#> [7] 0.9486833 -0.9486833 0.9486833 -0.9486833
mic <- random_mic(10)
mic
#> Class 'mic'
#> [1] 256 0.01 4 64 8 1 1 0.002 0.125 256
mean_amr_distance(mic)
#> [1] 1.2331707 -1.2886091 0.1999266 0.8887560 0.3721339 -0.1444882
#> [7] -0.1444882 -1.6884622 -0.6611102 1.2331707
# equal to the Z-score of their log2:
(log2(mic) - mean(log2(mic))) / sd(log2(mic))
#> [1] 1.2331707 -1.2886091 0.1999266 0.8887560 0.3721339 -0.1444882
#> [7] -0.1444882 -1.6884622 -0.6611102 1.2331707
disk <- random_disk(10)
disk
#> Class 'disk'
#> [1] 25 6 45 23 39 45 20 49 8 43
mean_amr_distance(disk)
#> [1] -0.3319640 -1.5220235 0.9207303 -0.4572334 0.5449220 0.9207303
#> [7] -0.6451375 1.1712691 -1.3967541 0.7954608
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 27 1 0.5
#> 2 B R 29 1 0.5
#> 3 C I 19 <=0.5 4
#> 4 D I 27 1 <=0.25
#> 5 E S 30 <=0.5 4
#> 6 F I 22 1 1
#> 7 G I 26 1 1
#> 8 H R 22 1 2
#> 9 I I 21 4 0.5
#> 10 J R 29 1 <=0.25
mean_amr_distance(y)
#> ℹ Calculating mean AMR distance based on columns "amox", "cipr", "gent",
#> and "tobr"
#> [1] 0.27087972 0.39969323 -0.52646026 -0.38265093 0.18201405 -0.36586924
#> [7] -0.10824222 0.28766141 0.01268871 0.23028550
y$amr_distance <- mean_amr_distance(y, where(is.mic))
#> Error in .subset(x, j): invalid subscript type 'list'
y[order(y$amr_distance), ]
#> Error in order(y$amr_distance): argument 1 is not a vector
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 I 19 <=0.5 4 -0.52646026 0.0000000
#> 2 D I 27 1 <=0.25 -0.38265093 0.1438093
#> 3 F I 22 1 1 -0.36586924 0.1605910
#> 4 G I 26 1 1 -0.10824222 0.4182180
#> 5 I I 21 4 0.5 0.01268871 0.5391490
#> 6 E S 30 <=0.5 4 0.18201405 0.7084743
#> 7 J R 29 1 <=0.25 0.23028550 0.7567458
#> 8 A R 27 1 0.5 0.27087972 0.7973400
#> 9 H R 22 1 2 0.28766141 0.8141217
#> 10 B R 29 1 0.5 0.39969323 0.9261535
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
#> # ℹ 53 more rows