# 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(x, col_ab, col_date = NULL, year_min = NULL,
      year_max = NULL, year_every = 1, minimum = 30,
      model = "binomial", I_as_S = TRUE, preserve_measurements = TRUE,
      info = TRUE, ...)

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

    # S3 method for resistance_predict
    plot(x,
      main = paste("Resistance Prediction of", x_name), ...)

    ggplot_rsi_predict(x, main = paste("Resistance Prediction of", x_name),
      ribbon = TRUE, ...)

## Arguments

x | a `data.frame` containing isolates.
col_ab | column name of `x` 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, defaults to the first column of with a date class
year_min | lowest year to use in the prediction model, dafaults to the lowest year in `col_date`
year_max | highest year to use in the prediction model, defaults to 10 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. Defaults to a generalised linear regression model with binomial distribution, assuming that a period of zero resistance was followed by a period of increasing resistance leading slowly to more and more resistance. See Details for valid options.
I_as_S | a logical to indicate whether values `I` should be treated as `S`
preserve_measurements | a logical to indicate whether predictions of years that are actually available in the data should be overwritten by the original data. The standard errors of those years will be `NA`.
info | a logical to indicate whether textual analysis should be printed with the name and `summary` of the statistical model.
... | parameters passed on to functions
main | title of the plot
ribbon | a logical to indicate whether a ribbon should be shown (default) or error bars

## Value

`data.frame` with extra class `"resistance_predict"` 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` (so the standard error will never go below 0%)

- `se_max` the upper bound of the standard error with a maximum of `1` (so the standard error will never go above 100%)

- `observations`, the total number of available observations in that year, i.e. S + I + R

- `observed`, the original observed resistant percentages

- `estimated`, the estimated resistant percentages, calculated by the model

Furthermore, the model itself is available as an attribute: `attributes(x)$model`, see Examples.

## Details

Valid options for the statistical model are:

- `"binomial"` or `"binom"` or `"logit"`: a generalised linear regression model with binomial distribution

- `"loglin"` or `"poisson"`: a generalised log-linear regression model with poisson distribution

- `"lin"` or `"linear"`: a linear regression model

## Read more on our website!

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

## See also

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

## Examples

    # NOT RUN {
    x <- resistance_predict(septic_patients, col_ab = "AMX", year_min = 2010)
    plot(x)
    ggplot_rsi_predict(x)

    # use dplyr so you can actually read it:
    library(dplyr)
    x <- septic_patients %>%
      filter_first_isolate() %>%
      filter(mo_genus(mo) == "Staphylococcus") %>%
      resistance_predict("PEN")
    plot(x)

    # get the model from the object
    mymodel <- attributes(x)$model
    summary(mymodel)

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

      data <- septic_patients %>%
        filter(mo == as.mo("E. coli")) %>%
        resistance_predict(col_ab = "AMX",
                           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)
    }
    # }