From 5b2e2c9477f7920c45f7131fda4afc6309cefd98 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 28 Mar 2025 15:59:05 +0000 Subject: [PATCH] Built site for AMR@2.1.1.9230: b972bbb --- 404.html | 5 +- LICENSE-text.html | 5 +- articles/AMR.html | 5 +- articles/AMR_for_Python.html | 5 +- articles/AMR_with_tidymodels.html | 317 ++++++++++++++++-- .../figure-html/unnamed-chunk-14-1.png | Bin 0 -> 54352 bytes .../figure-html/unnamed-chunk-15-1.png | Bin 0 -> 160405 bytes articles/EUCAST.html | 5 +- articles/MDR.html | 5 +- articles/PCA.html | 5 +- articles/WHONET.html | 5 +- articles/datasets.html | 5 +- articles/index.html | 7 +- articles/resistance_predict.html | 283 ---------------- .../figure-html/unnamed-chunk-4-1.png | Bin 53370 -> 0 bytes .../figure-html/unnamed-chunk-5-1.png | Bin 76927 -> 0 bytes .../figure-html/unnamed-chunk-5-2.png | Bin 74713 -> 0 bytes .../figure-html/unnamed-chunk-6-1.png | Bin 79145 -> 0 bytes .../figure-html/unnamed-chunk-7-1.png | Bin 70820 -> 0 bytes articles/welcome_to_AMR.html | 5 +- authors.html | 5 +- index.html | 7 +- news/index.html | 20 +- pkgdown.yml | 3 +- reference/AMR-deprecated.html | 5 +- reference/AMR-options.html | 5 +- reference/AMR.html | 5 +- reference/WHOCC.html | 5 +- reference/WHONET.html | 5 +- reference/ab_from_text.html | 5 +- reference/ab_property.html | 5 +- reference/add_custom_antimicrobials.html | 5 +- reference/add_custom_microorganisms.html | 5 +- reference/age.html | 5 +- reference/age_groups.html | 5 +- reference/antibiogram.html | 5 +- reference/antimicrobial_selectors.html | 5 +- reference/antimicrobials.html | 5 +- reference/as.ab.html | 5 +- reference/as.av.html | 5 +- reference/as.disk.html | 5 +- reference/as.mic.html | 5 +- reference/as.mo.html | 5 +- reference/as.sir.html | 23 +- reference/atc_online.html | 5 +- reference/av_from_text.html | 5 +- reference/av_property.html | 5 +- reference/availability.html | 5 +- reference/bug_drug_combinations.html | 5 +- reference/clinical_breakpoints.html | 7 +- reference/count.html | 5 +- reference/custom_eucast_rules.html | 5 +- reference/dosage.html | 5 +- reference/eucast_rules.html | 5 +- reference/example_isolates.html | 5 +- reference/example_isolates_unclean.html | 5 +- reference/export_ncbi_biosample.html | 5 +- reference/first_isolate.html | 5 +- reference/g.test.html | 5 +- reference/get_episode.html | 5 +- reference/ggplot_pca.html | 5 +- reference/ggplot_sir.html | 5 +- reference/guess_ab_col.html | 5 +- reference/index.html | 5 +- reference/intrinsic_resistant.html | 5 +- reference/italicise_taxonomy.html | 5 +- reference/join.html | 5 +- reference/key_antimicrobials.html | 5 +- reference/kurtosis.html | 5 +- reference/like.html | 5 +- reference/mdro.html | 5 +- reference/mean_amr_distance.html | 5 +- reference/microorganisms.codes.html | 5 +- reference/microorganisms.groups.html | 5 +- reference/microorganisms.html | 13 +- reference/mo_matching_score.html | 5 +- reference/mo_property.html | 5 +- reference/mo_source.html | 5 +- reference/pca.html | 5 +- reference/plot.html | 5 +- reference/proportion.html | 5 +- reference/random.html | 5 +- reference/resistance_predict.html | 10 +- reference/skewness.html | 5 +- reference/top_n_microorganisms.html | 5 +- reference/translate.html | 5 +- search.json | 2 +- sitemap.xml | 1 - 88 files changed, 477 insertions(+), 561 deletions(-) create mode 100644 articles/AMR_with_tidymodels_files/figure-html/unnamed-chunk-14-1.png create mode 100644 articles/AMR_with_tidymodels_files/figure-html/unnamed-chunk-15-1.png delete mode 100644 articles/resistance_predict.html delete mode 100644 articles/resistance_predict_files/figure-html/unnamed-chunk-4-1.png delete mode 100644 articles/resistance_predict_files/figure-html/unnamed-chunk-5-1.png delete mode 100644 articles/resistance_predict_files/figure-html/unnamed-chunk-5-2.png delete mode 100644 articles/resistance_predict_files/figure-html/unnamed-chunk-6-1.png delete mode 100644 articles/resistance_predict_files/figure-html/unnamed-chunk-7-1.png diff --git a/404.html b/404.html index e9c8f51bc..e713ced71 100644 --- a/404.html +++ b/404.html @@ -32,7 +32,7 @@ AMR (for R) - 2.1.1.9229 + 2.1.1.9230
+Antimicrobial resistance (AMR) is a global health crisis, and
understanding resistance patterns is crucial for managing effective
treatments. The AMR
R package provides robust tools for
-analysing AMR data, including convenient antibiotic selector functions
-like aminoglycosides()
and betalactams()
. In
-this post, we will explore how to use the tidymodels
-framework to predict resistance patterns in the
+analysing AMR data, including convenient antimicrobial selector
+functions like aminoglycosides()
and
+betalactams()
. In this post, we will explore how to use the
+tidymodels
framework to predict resistance patterns in the
example_isolates
dataset.
By leveraging the power of tidymodels
and the
AMR
package, we’ll build a reproducible machine learning
@@ -147,6 +150,29 @@ package.
step_corr()
removes predictors (i.e., antibiotic
columns) that have a higher correlation than 90%.
-Notice how the recipe contains just the antibiotic selector functions
-- no need to define the columns specifically. In the preparation
-(retrieved with prep()
) we can see that the columns or
-variables ‘AMX’ and ‘CTX’ were removed as they correlate too much with
-existing, other variables.
Notice how the recipe contains just the antimicrobial selector
+functions - no need to define the columns specifically. In the
+preparation (retrieved with prep()
) we can see that the
+columns or variables ‘AMX’ and ‘CTX’ were removed as they correlate too
+much with existing, other variables.
# Specify a logistic regression model
logistic_model <- logistic_reg() %>%
- set_engine("glm") # Use the Generalized Linear Model engine
+ set_engine("glm") # Use the Generalised Linear Model engine
logistic_model
#> Logistic Regression Model Specification (classification)
#>
@@ -273,7 +299,7 @@ engine.
3. Building the Workflow
We bundle the recipe and model together into a workflow
,
-which organizes the entire modeling process.
+which organises the entire modeling process.
# Combine the recipe and model into a workflow
resistance_workflow <- workflow() %>%
@@ -320,9 +346,9 @@ testing sets.
fit()
trains the workflow on the training set.
-Notice how in fit()
, the antibiotic selector functions
-are internally called again. For training, these functions are called
-since they are stored in the recipe.
+Notice how in fit()
, the antimicrobial selector
+functions are internally called again. For training, these functions are
+called since they are stored in the recipe.
Next, we evaluate the model on the testing data.
# Make predictions on the testing set
@@ -363,7 +389,22 @@ since they are stored in the recipe.
#> .metric .estimator .estimate
#> <chr> <chr> <dbl>
#> 1 accuracy binary 0.995
-#> 2 kap binary 0.989
+#> 2 kap binary 0.989
+
+
+# To assess some other model properties, you can make our own `metrics()` function
+our_metrics <- metric_set(accuracy, kap, ppv, npv) # add Positive Predictive Value and Negative Predictive Value
+metrics2 <- predictions %>%
+ our_metrics(truth = mo, estimate = .pred_class) # run again on our `our_metrics()` function
+
+metrics2
+#> # A tibble: 4 × 3
+#> .metric .estimator .estimate
+#> <chr> <chr> <dbl>
+#> 1 accuracy binary 0.995
+#> 2 kap binary 0.989
+#> 3 ppv binary 0.987
+#> 4 npv binary 1
Explanation:
-
@@ -373,9 +414,9 @@ set.
metrics()
computes evaluation metrics like accuracy and
kappa.
-It appears we can predict the Gram based on AMR results with a 99.5%
-accuracy based on AMR results of aminoglycosides and beta-lactam
-antibiotics. The ROC curve looks like this:
+It appears we can predict the Gram stain with a 99.5% accuracy based
+on AMR results of only aminoglycosides and beta-lactam antibiotics. The
+ROC curve looks like this:
predictions %>%
roc_curve(mo, `.pred_Gram-negative`) %>%
@@ -392,9 +433,241 @@ pipeline with the tidymodels
framework and the
aminoglycosides()
and betalactams()
with
tidymodels
, we efficiently prepared data, trained a model,
and evaluated its performance.
-This workflow is extensible to other antibiotic classes and
+
This workflow is extensible to other antimicrobial classes and
resistance patterns, empowering users to analyse AMR data systematically
and reproducibly.
+
+
+
In this second example, we aim to predict antimicrobial resistance
+(AMR) trends over time using tidymodels
. We will model
+resistance to three antibiotics (amoxicillin AMX
,
+amoxicillin-clavulanic acid AMC
, and ciprofloxacin
+CIP
), based on historical data grouped by year and hospital
+ward.
Our goal is to:
+tidymodels
to preprocess, train, and evaluate the
+model.We start by transforming the example_isolates
dataset
+into a structured time-series format.
+# Load required libraries
+library(AMR)
+library(tidymodels)
+
+# Transform dataset
+data_time <- example_isolates %>%
+ top_n_microorganisms(n = 10) %>% # Filter on the top #10 species
+ mutate(year = as.integer(format(date, "%Y")), # Extract year from date
+ gramstain = mo_gramstain(mo)) %>% # Get taxonomic names
+ group_by(year, gramstain) %>%
+ summarise(across(c(AMX, AMC, CIP),
+ function(x) resistance(x, minimum = 0),
+ .names = "res_{.col}"),
+ .groups = "drop") %>%
+ filter(!is.na(res_AMX) & !is.na(res_AMC) & !is.na(res_CIP)) # Drop missing values
+#> ℹ Using column 'mo' as input for col_mo.
+
+data_time
+#> # A tibble: 32 × 5
+#> year gramstain res_AMX res_AMC res_CIP
+#> <int> <chr> <dbl> <dbl> <dbl>
+#> 1 2002 Gram-negative 1 0.105 0.0606
+#> 2 2002 Gram-positive 0.838 0.182 0.162
+#> 3 2003 Gram-negative 1 0.0714 0
+#> 4 2003 Gram-positive 0.714 0.244 0.154
+#> 5 2004 Gram-negative 0.464 0.0938 0
+#> 6 2004 Gram-positive 0.849 0.299 0.244
+#> 7 2005 Gram-negative 0.412 0.132 0.0588
+#> 8 2005 Gram-positive 0.882 0.382 0.154
+#> 9 2006 Gram-negative 0.379 0 0.1
+#> 10 2006 Gram-positive 0.778 0.333 0.353
+#> # ℹ 22 more rows
Explanation: - mo_name(mo)
: Converts
+microbial codes into proper species names. - resistance()
:
+Converts AMR results into numeric values (proportion of resistant
+isolates). - group_by(year, ward, species)
: Aggregates
+resistance rates by year and ward.
We now define the modeling workflow, which consists of a +preprocessing step, a model specification, and the fitting process.
+
+# Define the recipe
+resistance_recipe_time <- recipe(res_AMX ~ year + gramstain, data = data_time) %>%
+ step_dummy(gramstain, one_hot = TRUE) %>% # Convert categorical to numerical
+ step_normalize(year) %>% # Normalise year for better model performance
+ step_nzv(all_predictors()) # Remove near-zero variance predictors
+
+resistance_recipe_time
+#>
+#> ── Recipe ──────────────────────────────────────────────────────────────────────
+#>
+#> ── Inputs
+#> Number of variables by role
+#> outcome: 1
+#> predictor: 2
+#>
+#> ── Operations
+#> • Dummy variables from: gramstain
+#> • Centering and scaling for: year
+#> • Sparse, unbalanced variable filter on: all_predictors()
Explanation: - step_dummy()
: Encodes
+categorical variables (ward
, species
) as
+numerical indicators. - step_normalize()
: Normalises the
+year
variable. - step_nzv()
: Removes near-zero
+variance predictors.
We use a linear regression model to predict resistance trends.
+
+# Define the linear regression model
+lm_model <- linear_reg() %>%
+ set_engine("lm") # Use linear regression
+
+lm_model
+#> Linear Regression Model Specification (regression)
+#>
+#> Computational engine: lm
Explanation: - linear_reg()
: Defines a
+linear regression model. - set_engine("lm")
: Uses R’s
+built-in linear regression engine.
We combine the preprocessing recipe and model into a workflow.
+
+# Create workflow
+resistance_workflow_time <- workflow() %>%
+ add_recipe(resistance_recipe_time) %>%
+ add_model(lm_model)
+
+resistance_workflow_time
+#> ══ Workflow ════════════════════════════════════════════════════════════════════
+#> Preprocessor: Recipe
+#> Model: linear_reg()
+#>
+#> ── Preprocessor ────────────────────────────────────────────────────────────────
+#> 3 Recipe Steps
+#>
+#> • step_dummy()
+#> • step_normalize()
+#> • step_nzv()
+#>
+#> ── Model ───────────────────────────────────────────────────────────────────────
+#> Linear Regression Model Specification (regression)
+#>
+#> Computational engine: lm
We split the data into training and testing sets, fit the model, and +evaluate performance.
+
+# Split the data
+set.seed(123)
+data_split_time <- initial_split(data_time, prop = 0.8)
+train_time <- training(data_split_time)
+test_time <- testing(data_split_time)
+
+# Train the model
+fitted_workflow_time <- resistance_workflow_time %>%
+ fit(train_time)
+
+# Make predictions
+predictions_time <- fitted_workflow_time %>%
+ predict(test_time) %>%
+ bind_cols(test_time)
+
+# Evaluate model
+metrics_time <- predictions_time %>%
+ metrics(truth = res_AMX, estimate = .pred)
+
+metrics_time
+#> # A tibble: 3 × 3
+#> .metric .estimator .estimate
+#> <chr> <chr> <dbl>
+#> 1 rmse standard 0.0774
+#> 2 rsq standard 0.711
+#> 3 mae standard 0.0704
Explanation: - initial_split()
: Splits
+data into training and testing sets. - fit()
: Trains the
+workflow. - predict()
: Generates resistance predictions. -
+metrics()
: Evaluates model performance.
We plot resistance trends over time for Amoxicillin.
+
+library(ggplot2)
+
+# Plot actual vs predicted resistance over time
+ggplot(predictions_time, aes(x = year)) +
+ geom_point(aes(y = res_AMX, color = "Actual")) +
+ geom_line(aes(y = .pred, color = "Predicted")) +
+ labs(title = "Predicted vs Actual AMX Resistance Over Time",
+ x = "Year",
+ y = "Resistance Proportion") +
+ theme_minimal()
Additionally, we can visualise resistance trends in
+ggplot2
and directly adding linear models there:
+ggplot(data_time, aes(x = year, y = res_AMX, color = gramstain)) +
+ geom_line() +
+ labs(title = "AMX Resistance Trends",
+ x = "Year",
+ y = "Resistance Proportion") +
+ # add a linear model directly in ggplot2:
+ geom_smooth(method = "lm",
+ formula = y ~ x,
+ alpha = 0.25) +
+ theme_minimal()
In this example, we demonstrated how to analyze AMR trends over time
+using tidymodels
. By aggregating resistance rates by year
+and hospital ward, we built a predictive model to track changes in
+resistance to amoxicillin (AMX
), amoxicillin-clavulanic
+acid (AMC
), and ciprofloxacin (CIP
).
This method can be extended to other antibiotics and resistance +patterns, providing valuable insights into AMR dynamics in healthcare +settings.
+DUcmwH_2nd+1TCej~#P+2K-fx
zEQ*pWx=ysTH6*H@N}kkm6XxFbHO@8|g#(|;2frq}XuGST7D>Wqa~l-&h@<^mxhpIA
z2u(JwlB7*rX>y1?H5tZ%Qrt$3>{#>?7P}ELcQJ!XTBef@)iXie{I#ZaHr)x$Lxmpg
zp;urQCvyyJCiER1@U*0bK1fUALU=a!gSwrcs-#*+3t+v&KWm$gaf{o?eUnC7i@n_3
z0JZV1X`O|oY;O750w>skhHL$7=o!7XQK~6q_$?*JaCf^q!cSwzfLw1z-?64hE4%aF
zajZ<%Zzx~AbuG6KEwdz~ifZ1Q$jau{xV1e>L`=2vbtjgXHSTGOJ5P8%VrZcR4TR1$
zZ%!i?a@v7P478Ma{^2=v4jXZ$5s(ggMGM=hiTozsKl0U6j{te$3O)?7F_II-$a@=a
zW-CRAfhF%^dLbHR2%&)LmQ#Ud=j+D=|