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

mic <- random_mic(10)
mic
#> Class 'mic'
#>  [1] 8     >=16  0.002 >=16  0.025 >=16  0.002 2     2     2    
mean_amr_distance(mic)
#>  [1]  0.6738475  0.8649910 -1.6133339  0.8649910 -0.9168346  0.8649910
#>  [7] -1.6133339  0.2915606  0.2915606  0.2915606
# equal to the Z-score of their log2:
(log2(mic) - mean(log2(mic))) / sd(log2(mic))
#>  [1]  0.6738475  0.8649910 -1.6133339  0.8649910 -0.9168346  0.8649910
#>  [7] -1.6133339  0.2915606  0.2915606  0.2915606

disk <- random_disk(10)
disk
#> Class 'disk'
#>  [1] 39 15 22 38 19 50 29 46 40 18
mean_amr_distance(disk)
#>  [1]  0.5869812 -1.3167417 -0.7614892  0.5076595 -0.9994545  1.4595209
#>  [7] -0.2062367  1.1422338  0.6663030 -1.0787763

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    I   17    8    4
#> 2   B    S   31    2    4
#> 3   C    R   19   16    2
#> 4   D    S   18    8    1
#> 5   E    S   19   16    2
#> 6   F    I   31   16    4
#> 7   G    I   24  <=1    2
#> 8   H    S   24  <=1    1
#> 9   I    R   22    4    4
#> 10  J    R   20    2  0.5
mean_amr_distance(y)
#> ℹ Calculating mean AMR distance based on columns "amox", "cipr", "gent",
#>   and "tobr"
#>  [1] -0.06628516  0.31654289  0.46727798 -0.49122694 -0.05027119  0.77969806
#>  [7] -0.42081207 -0.65798290  0.54387830 -0.42081897
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    R   19   16    2   0.46727798 0.00000000
#> 2   I    R   22    4    4   0.54387830 0.07660032
#> 3   B    S   31    2    4   0.31654289 0.15073509
#> 4   F    I   31   16    4   0.77969806 0.31242008
#> 5   E    S   19   16    2  -0.05027119 0.51754917
#> 6   A    I   17    8    4  -0.06628516 0.53356314
#> 7   G    I   24  <=1    2  -0.42081207 0.88809005
#> 8   J    R   20    2  0.5  -0.42081897 0.88809695
#> 9   D    S   18    8    1  -0.49122694 0.95850492
#> 10  H    S   24  <=1    1  -0.65798290 1.12526088
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