Skip to contents

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 and where(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 S R S R I S R I S
mean_amr_distance(rsi)
#>  [1] -0.621059 -0.621059  1.449138 -0.621059  1.449138 -0.621059 -0.621059
#>  [8]  1.449138 -0.621059 -0.621059

mic <- random_mic(10)
mic
#> Class 'mic'
#>  [1] 64      0.5     <=0.025 128     4       0.0625  256     2       128    
#> [10] 0.125  
mean_amr_distance(mic)
#>  [1]  0.84443028 -0.55224804 -1.41458279  1.04395575  0.04632838 -1.15082446
#>  [7]  1.24348122 -0.15319709  1.04395575 -0.95129899
# equal to the Z-score of their log2:
(log2(mic) - mean(log2(mic))) / sd(log2(mic))
#>  [1]  0.84443028 -0.55224804 -1.41458279  1.04395575  0.04632838 -1.15082446
#>  [7]  1.24348122 -0.15319709  1.04395575 -0.95129899

disk <- random_disk(10)
disk
#> Class 'disk'
#>  [1] 31 19 25 45 43 45 10 42 38 47
mean_amr_distance(disk)
#>  [1] -0.2747505 -1.2167523 -0.7457514  0.8242516  0.6672513  0.8242516
#>  [7] -1.9232536  0.5887511  0.2747505  0.9812518

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    S   20 <=0.25    4
#> 2   B    R   25      1    2
#> 3   C    I   24    0.5    4
#> 4   D    S   17      2  >=8
#> 5   E    S   25 <=0.25    1
#> 6   F    R   28    >=4 0.25
#> 7   G    I   29      2    1
#> 8   H    I   22      1  0.5
#> 9   I    I   18      2 0.25
#> 10  J    I   21    0.5    1
mean_amr_distance(y)
#> ℹ Calculating mean AMR distance based on columns "amox", "cipr", "gent" and
#>   "tobr"
#>  [1] -0.39633734  0.72619085  0.03532415  0.11086821 -0.37841945  0.83757068
#>  [7]  0.41812023 -0.34726311 -0.56085146 -0.44520275
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
#> 5   E    S   25 <=0.25    1  -0.78135160
#> 10  J    I   21    0.5    1  -0.41647341
#> 8   H    I   22      1  0.5  -0.34520532
#> 9   I    I   18      2 0.25  -0.27393724
#> 1   A    S   20 <=0.25    4  -0.19413138
#> 6   F    R   28    >=4 0.25   0.09094095
#> 3   C    I   24    0.5    4   0.17074681
#> 2   B    R   25      1    2   0.24201490
#> 7   G    I   29      2    1   0.31328298
#> 4   D    S   17      2  >=8   1.19411331

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   24    0.5    4   0.03532415 0.00000000
#> 2   D    S   17      2  >=8   0.11086821 0.07554406
#> 3   H    I   22      1  0.5  -0.34726311 0.38258727
#> 4   G    I   29      2    1   0.41812023 0.38279607
#> 5   E    S   25 <=0.25    1  -0.37841945 0.41374361
#> 6   A    S   20 <=0.25    4  -0.39633734 0.43166149
#> 7   J    I   21    0.5    1  -0.44520275 0.48052690
#> 8   I    I   18      2 0.25  -0.56085146 0.59617562
#> 9   B    R   25      1    2   0.72619085 0.69086670
#> 10  F    R   28    >=4 0.25   0.83757068 0.80224652
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