## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----setup--------------------------------------------------------------------
library(AMRvalid)
d <- amr_example_data()
x <- amr_import_predictions(d)
x

## ----summary------------------------------------------------------------------
s <- amr_data_summary(x)
s[, c("model_id", "drug_name", "n_isolates", "n_S", "n_I_SDD", "n_R", "prevalence_R")]

## ----validate-----------------------------------------------------------------
amr_validate(x)

## ----eval---------------------------------------------------------------------
e <- amr_evaluate(x)
key <- c("sensitivity", "specificity", "categorical_agreement",
         "very_major_error", "major_error", "minor_error")
tab <- as.data.frame(e)[e$metric %in% key, c("drug_name", "model_id", "metric", "estimate", "lower", "upper")]
tab[, c("estimate", "lower", "upper")] <- round(tab[, c("estimate", "lower", "upper")], 3)
rownames(tab) <- NULL
tab

## ----weighting----------------------------------------------------------------
k <- c("sensitivity", "specificity", "very_major_error", "major_error")
iso <- as.data.frame(amr_evaluate(x))
clu <- as.data.frame(amr_evaluate(x, weight_by = "genomic_cluster"))
cmp <- merge(iso[iso$metric %in% k, c("model_id", "metric", "estimate")],
             clu[clu$metric %in% k, c("model_id", "metric", "estimate")],
             by = c("model_id", "metric"), suffixes = c("_isolates", "_clusters"))
cmp$change <- cmp$estimate_clusters - cmp$estimate_isolates
cmp[, 3:5] <- round(cmp[, 3:5], 3)
cmp <- cmp[order(cmp$metric, cmp$model_id), ]
rownames(cmp) <- NULL
cmp

## ----bootstrap----------------------------------------------------------------
mero <- x[x$drug == "MEM", ]
b_iso <- amr_bootstrap(mero, resample_by = "isolate_id", B = 300, seed = 1)
b_clu <- amr_bootstrap(mero, resample_by = "genomic_cluster", B = 300, seed = 1)
vme <- function(b, unit) {
  b <- as.data.frame(b)
  r <- b[b$metric == "very_major_error", ]
  data.frame(model = r$model_id, resampled = unit, groups = r$n_groups,
             estimate = round(r$estimate, 3), lower = round(r$lower, 3), upper = round(r$upper, 3))
}
rbind(vme(b_iso, "isolates"), vme(b_clu, "SNP clusters"))

## ----paired-------------------------------------------------------------------
amr_paired_compare(mero, models = c("carbapenemase genes", "carbapenemase or porin + ESBL/AmpC"),
                   resample_by = "genomic_cluster", B = 300, seed = 1)

## ----time---------------------------------------------------------------------
x$period <- cut(d$collection_year, c(-Inf, 2016, 2020, Inf), labels = c("up to 2016", "2017-2020", "2021 on"))
by_period <- as.data.frame(amr_evaluate(x, by = "period"))
w <- by_period[by_period$metric == "very_major_error" & by_period$period != "(missing)",
               c("model_id", "period", "denominator", "estimate", "lower", "upper")]
names(w)[3] <- "n_resistant"
w[, 4:6] <- round(w[, 4:6], 3)
rownames(w) <- NULL
w

## ----report-------------------------------------------------------------------
dataset_info <- list(source = "NCBI Pathogen Detection PDG000000012.2568",
                     breakpoint_standard = "As submitted to BioSample (not recorded)")
model_info <- list(description = "Rule-based interpretation of AMRFinderPlus calls",
                   training_data = "None (rules, not trained models)",
                   threshold_selection = "Not applicable (binary rules)")
report <- amr_report(x, e, b_clu, dataset_info = dataset_info, model_info = model_info,
                     file = file.path(tempdir(), "ncbi-report.md"))
p <- amr_provenance(x, e, b_clu, dataset_info = dataset_info, model_info = model_info)
table(p$status)
p[p$status == "not assessable", c("tripod_item", "item")]

