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)
#> Error in .Call(Cchin, x, table): NULL value passed as symbol address
sir
#> Error in eval(expr, envir, enclos): object 'sir' not found
mean_amr_distance(sir)
#> Error in eval(expr, envir, enclos): object 'sir' not found

mic <- random_mic(10)
#> Error in .Call(Cchin, x, table): NULL value passed as symbol address
mic
#> Error in eval(expr, envir, enclos): object 'mic' not found
mean_amr_distance(mic)
#> Error in eval(expr, envir, enclos): object 'mic' not found
# equal to the Z-score of their log2:
(log2(mic) - mean(log2(mic))) / sd(log2(mic))
#> Error in eval(expr, envir, enclos): object 'mic' not found

disk <- random_disk(10)
#> Error in .Call(Cchin, x, table): NULL value passed as symbol address
disk
#> Error in eval(expr, envir, enclos): object 'disk' not found
mean_amr_distance(disk)
#> Error in eval(expr, envir, enclos): object 'disk' not found

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")
)
#> Error in .Call(Cchin, x, table): NULL value passed as symbol address
y
#> Error in eval(expr, envir, enclos): object 'y' not found
mean_amr_distance(y)
#> Error in eval(expr, envir, enclos): object 'y' not found
y$amr_distance <- mean_amr_distance(y, where(is.mic))
#> Error in eval(expr, envir, enclos): object 'y' not found
y[order(y$amr_distance), ]
#> Error in eval(expr, envir, enclos): object 'y' not found

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)
}
#> Error in eval(expr, envir, enclos): object 'y' not found
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)
}
#> Error in filter(., mo_genus() == "Enterococcus" & mo_species() != ""):  In argument: `mo_genus() == "Enterococcus" & mo_species() != ""`.
#> Caused by error:
#> ! in mo_genus(): argument x is missing and no column with info about
#> microorganisms could be found.