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 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 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) - 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] S R R S I I R S S R
mean_amr_distance(sir)
#>  [1] -0.7745967  1.1618950  1.1618950 -0.7745967 -0.7745967 -0.7745967
#>  [7]  1.1618950 -0.7745967 -0.7745967  1.1618950

mic <- random_mic(10)
mic
#> Class 'mic'
#>  [1] 0.002   >=64    0.125   >=64    2       16      0.005   <=0.001 16     
#> [10] 0.25   
mean_amr_distance(mic)
#>  [1] -1.2773949  1.1399840 -0.3137594  1.1399840  0.3323488  0.8169299
#>  [7] -1.0638678 -1.4389220  0.8169299 -0.1522324
# equal to the Z-score of their log2:
(log2(mic) - mean(log2(mic))) / sd(log2(mic))
#>  [1] -1.2773949  1.1399840 -0.3137594  1.1399840  0.3323488  0.8169299
#>  [7] -1.0638678 -1.4389220  0.8169299 -0.1522324

disk <- random_disk(10)
disk
#> Class 'disk'
#>  [1] 34 26 14 50  7 45 18 40 19 24
mean_amr_distance(disk)
#>  [1]  0.4467074 -0.1205401 -0.9714114  1.5812024 -1.4677529  1.2266727
#>  [7] -0.6877876  0.8721430 -0.6168817 -0.2623520

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   30     8     2
#> 2   B    I   24 <=0.5     4
#> 3   C    S   19     2 <=0.5
#> 4   D    I   21 <=0.5     1
#> 5   E    I   22  >=16     1
#> 6   F    I   28     8     2
#> 7   G    I   19     4 <=0.5
#> 8   H    I   29 <=0.5 <=0.5
#> 9   I    I   26  >=16 <=0.5
#> 10  J    R   21     8     1
mean_amr_distance(y)
#> ℹ Calculating mean AMR distance based on columns "amox", "cipr", "gent",
#>   and "tobr"
#>  [1]  1.23945841  0.03375462 -0.73801392 -0.62254823  0.04563698  0.52522389
#>  [7] -0.61650763 -0.37448920  0.05108107  0.45640401
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    S   19     2 <=0.5  -0.73801392  0.0000000
#> 2   D    I   21 <=0.5     1  -0.62254823  0.1154657
#> 3   G    I   19     4 <=0.5  -0.61650763  0.1215063
#> 4   H    I   29 <=0.5 <=0.5  -0.37448920  0.3635247
#> 5   B    I   24 <=0.5     4   0.03375462  0.7717685
#> 6   E    I   22  >=16     1   0.04563698  0.7836509
#> 7   I    I   26  >=16 <=0.5   0.05108107  0.7890950
#> 8   J    R   21     8     1   0.45640401  1.1944179
#> 9   F    I   28     8     2   0.52522389  1.2632378
#> 10  A    R   30     8     2   1.23945841  1.9774723
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