# Determine first (weighted) isolates — first_isolate • AMR (for R)
Determine first (weighted) isolates of all microorganisms of every patient per episode and (if needed) per specimen type.

# Determine first (weighted) isolates

`first_isolate.Rd`

Determine first (weighted) isolates of all microorganisms of every patient per episode and (if needed) per specimen type.

    first_isolate(tbl, col_date = NULL, col_patient_id = NULL,
      col_mo = NULL, col_testcode = NULL, col_specimen = NULL,
      col_icu = NULL, col_keyantibiotics = NULL, episode_days = 365,
      testcodes_exclude = NULL, icu_exclude = FALSE,
      specimen_group = NULL, type = "keyantibiotics", ignore_I = TRUE,
      points_threshold = 2, info = TRUE, ...)

    filter_first_isolate(tbl, col_date = NULL, col_patient_id = NULL,
      col_mo = NULL, ...)

    filter_first_weighted_isolate(tbl, col_date = NULL,
      col_patient_id = NULL, col_mo = NULL, col_keyantibiotics = NULL,
      ...)

## Arguments

tbl | a `data.frame` containing isolates.
col_date | column name of the result date (or date that is was received on the lab), defaults to the first column of class `Date`
col_patient_id | column name of the unique IDs of the patients, defaults to the first column that starts with 'patient' or 'patid' (case insensitive)
col_mo | column name of the unique IDs of the microorganisms (see `mo`), defaults to the first column of class `mo`. Values will be coerced using `as.mo`.
col_testcode | column name of the test codes. Use `col_testcode = NULL` to not exclude certain test codes (like test codes for screening). In that case `testcodes_exclude` will be ignored.
col_specimen | column name of the specimen type or group
col_icu | column name of the logicals (`TRUE`/`FALSE`) whether a ward or department is an Intensive Care Unit (ICU)
col_keyantibiotics | column name of the key antibiotics to determine first weighted isolates, see `key_antibiotics`. Defaults to the first column that starts with 'key' followed by 'ab' or 'antibiotics' (case insensitive). Use `col_keyantibiotics = FALSE` to prevent this.
episode_days | episode in days after which a genus/species combination will be determined as 'first isolate' again
testcodes_exclude | character vector with test codes that should be excluded (case-insensitive)
icu_exclude | logical whether ICU isolates should be excluded (rows with value `TRUE` in column `col_icu`)
specimen_group | value in column `col_specimen` to filter on
type | type to determine weighed isolates; can be `"keyantibiotics"` or `"points"`, see Details
ignore_I | logical to determine whether antibiotic interpretations with `"I"` will be ignored when `type = "keyantibiotics"`, see Details
points_threshold | points until the comparison of key antibiotics will lead to inclusion of an isolate when `type = "points"`, see Details
info | print progress
... | parameters passed on to the `first_isolate` function

## Source

Methodology of this function is based on: M39 Analysis and Presentation of Cumulative Antimicrobial Susceptibility Test Data, 4th Edition, 2014, Clinical and Laboratory Standards Institute (CLSI). https://clsi.org/standards/products/microbiology/documents/m39/.

## Value

Logical vector

## Details

WHY THIS IS SO IMPORTANT
To conduct an analysis of antimicrobial resistance, you should only include the first isolate of every patient per episode [1]. If you would not do this, you could easily get an overestimate or underestimate of the resistance of an antibiotic. Imagine that a patient was admitted with an MRSA and that it was found in 5 different blood cultures the following week. The resistance percentage of oxacillin of all S. aureus isolates would be overestimated, because you included this MRSA more than once. It would be selection bias.

The function `filter_first_isolate` is essentially equal to:

     tbl %>%
       mutate(only_firsts = first_isolate(tbl, ...)) %>%
       filter(only_firsts == TRUE) %>%
       select(-only_firsts)

The function `filter_first_weighted_isolate` is essentially equal to:

     tbl %>%
       mutate(keyab = key_antibiotics(.)) %>%
       mutate(only_weighted_firsts = first_isolate(tbl,
                                                   col_keyantibiotics = "keyab", ...)) %>%
       filter(only_weighted_firsts == TRUE) %>%
       select(-only_weighted_firsts)

## Key antibiotics

There are two ways to determine whether isolates can be included as first weighted isolates which will give generally the same results:

1. Using `type = "keyantibiotics"` and parameter `ignore_I`
Any difference from S to R (or vice versa) will (re)select an isolate as a first weighted isolate. With `ignore_I = FALSE`, also differences from I to S|R (or vice versa) will lead to this. This is a reliable method and 30-35 times faster than method 2.

2. Using `type = "points"` and parameter `points_threshold`
A difference from I to S|R (or vice versa) means 0.5 points, a difference from S to R (or vice versa) means 1 point. When the sum of points exceeds `points_threshold`, an isolate will be (re)selected as a first weighted isolate.

## Read more on our website!

On our website https://msberends.gitlab.io/AMR you can find a omprehensive tutorial about how to conduct AMR analysis and find the complete documentation of all functions, which reads a lot easier than in R.

## See also

`key_antibiotics`

## Examples

    # NOT RUN {
    # septic_patients is a dataset available in the AMR package. It is true, genuine data.
    ?septic_patients

    library(dplyr)
    # Filter on first isolates:
    septic_patients %>%
      mutate(first_isolate = first_isolate(.,
                                           col_date = "date",
                                           col_patient_id = "patient_id",
                                           col_mo = "mo")) %>%
      filter(first_isolate == TRUE)

    # Which can be shortened to:
    septic_patients %>%
      filter_first_isolate()
    # or for first weighted isolates:
    septic_patients %>%
      filter_first_weighted_isolate()

    # Now let's see if first isolates matter:
    A <- septic_patients %>%
      group_by(hospital_id) %>%
      summarise(count = n_rsi(gent),            # gentamicin availability
                resistance = portion_IR(gent))  # gentamicin resistance

    B <- septic_patients %>%
      filter_first_weighted_isolate() %>%       # the 1st isolate filter
      group_by(hospital_id) %>%
      summarise(count = n_rsi(gent),            # gentamicin availability
                resistance = portion_IR(gent))  # gentamicin resistance

    # Have a look at A and B.
    # B is more reliable because every isolate is only counted once.
    # Gentamicin resitance in hospital D appears to be 5.4% higher than
    # when you (erroneously) would have used all isolates!

    ## OTHER EXAMPLES:

    # }# NOT RUN {
    # set key antibiotics to a new variable
    tbl$keyab <- key_antibiotics(tbl)

    tbl$first_isolate <-
      first_isolate(tbl)

    tbl$first_isolate_weighed <-
      first_isolate(tbl,
                    col_keyantibiotics = 'keyab')

    tbl$first_blood_isolate <-
      first_isolate(tbl,
                    specimen_group = 'Blood')

    tbl$first_blood_isolate_weighed <-
      first_isolate(tbl,
                    specimen_group = 'Blood',
                    col_keyantibiotics = 'keyab')

    tbl$first_urine_isolate <-
      first_isolate(tbl,
                    specimen_group = 'Urine')

    tbl$first_urine_isolate_weighed <-
      first_isolate(tbl,
                    specimen_group = 'Urine',
                    col_keyantibiotics = 'keyab')

    tbl$first_resp_isolate <-
      first_isolate(tbl,
                    specimen_group = 'Respiratory')

    tbl$first_resp_isolate_weighed <-
      first_isolate(tbl,
                    specimen_group = 'Respiratory',
                    col_keyantibiotics = 'keyab')
    # }