# Predict antimicrobial resistance — resistance_predict • AMR (for R)
Create a prediction model to predict antimicrobial resistance for the next years on statistical solid ground. Standard errors (SE) will be returned as columns se_min and se_max. See Examples for a real live example.

# Predict antimicrobial resistance

`resistance_predict.Rd`

Create a prediction model to predict antimicrobial resistance for the next years on statistical solid ground. Standard errors (SE) will be returned as columns `se_min` and `se_max`. See Examples for a real live example.

    resistance_predict(tbl, col_ab, col_date, year_min = NULL,
      year_max = NULL, year_every = 1, minimum = 30,
      model = "binomial", I_as_R = TRUE, preserve_measurements = TRUE,
      info = TRUE)

    rsi_predict(tbl, col_ab, col_date, year_min = NULL, year_max = NULL,
      year_every = 1, minimum = 30, model = "binomial", I_as_R = TRUE,
      preserve_measurements = TRUE, info = TRUE)

## Arguments

tbl | a `data.frame` containing isolates.
col_ab | column name of `tbl` with antimicrobial interpretations (`R`, `I` and `S`)
col_date | column name of the date, will be used to calculate years if this column doesn't consist of years already
year_min | lowest year to use in the prediction model, dafaults the lowest year in `col_date`
year_max | highest year to use in the prediction model, defaults to 15 years after today
year_every | unit of sequence between lowest year found in the data and `year_max`
minimum | minimal amount of available isolates per year to include. Years containing less observations will be estimated by the model.
model | the statistical model of choice. Valid values are `"binomial"` (or `"binom"` or `"logit"`) or `"loglin"` or `"linear"` (or `"lin"`).
I_as_R | treat `I` as `R`
preserve_measurements | logical to indicate whether predictions of years that are actually available in the data should be overwritten with the original data. The standard errors of those years will be `NA`.
info | print textual analysis with the name and `summary` of the model.

## Value

`data.frame` with columns:

- `year`

- `value`, the same as `estimated` when `preserve_measurements = FALSE`, and a combination of `observed` and `estimated` otherwise

- `se_min`, the lower bound of the standard error with a minimum of `0`

- `se_max` the upper bound of the standard error with a maximum of `1`

- `observations`, the total number of observations, i.e. S + I + R

- `observed`, the original observed values

- `estimated`, the estimated values, calculated by the model

## 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

The `portion` function to calculate resistance,
`lm` `glm`

## Examples

    # NOT RUN {
    # use it with base R:
    resistance_predict(tbl = tbl[which(first_isolate == TRUE & genus == "Haemophilus"),],
                       col_ab = "amcl", col_date = "date")

    # or use dplyr so you can actually read it:
    library(dplyr)
    tbl %>%
      filter(first_isolate == TRUE,
             genus == "Haemophilus") %>%
      resistance_predict(amcl, date)
    # }# NOT RUN {

    # real live example:
    library(dplyr)
    septic_patients %>%
      # get bacteria properties like genus and species
      left_join_microorganisms("mo") %>%
      # calculate first isolates
      mutate(first_isolate = first_isolate(.)) %>%
      # filter on first E. coli isolates
      filter(genus == "Escherichia",
             species == "coli",
             first_isolate == TRUE) %>%
      # predict resistance of cefotaxime for next years
      resistance_predict(col_ab = "cfot",
                         col_date = "date",
                         year_max = 2025,
                         preserve_measurements = TRUE,
                         minimum = 0)

    # create nice plots with ggplot
    if (!require(ggplot2)) {

      data <- septic_patients %>%
        filter(mo == as.mo("E. coli")) %>%
        resistance_predict(col_ab = "amox",
                           col_date = "date",
                           info = FALSE,
                            minimum = 15)

      ggplot(data,
             aes(x = year)) +
        geom_col(aes(y = value),
                 fill = "grey75") +
        geom_errorbar(aes(ymin = se_min,
                          ymax = se_max),
                      colour = "grey50") +
        scale_y_continuous(limits = c(0, 1),
                           breaks = seq(0, 1, 0.1),
                           labels = paste0(seq(0, 100, 10), "%")) +
        labs(title = expression(paste("Forecast of amoxicillin resistance in ",
                                      italic("E. coli"))),
             y = "%IR",
             x = "Year") +
        theme_minimal(base_size = 13)
    }
    # }