QAAPT is a free web-based platform developed by CAPTURA for the analysis of AMR data.

How to Predict Antimicrobial Resistance Using PHP and Machine Learning (ML)

Posted: December 8, 2024 || Author: Julhas Sujan

Introduction

Antimicrobial resistance (AMR) poses a growing threat to global health, requiring innovative tools to monitor and predict resistance trends. This article demonstrates how to use PHP and machine learning to predict AMR patterns, leveraging data and predictive models for actionable insights. We’ll walk through a practical example using a function that predicts resistance percentages by year and forecasts trends for the next five years. QAAPT has implemented a linear regression model to predict resistance trends, empowering healthcare systems with data-driven decision-making to combat AMR effectively.

Predict Antimicrobial Resistance Using PHP and Machine Learning

Understanding the Data

To predict AMR, we need a dataset containing:

  • 1. Year of observation
  • 2. Resistance interpretations: Categories such as Resistant (R), Sensitive (S), and Intermediate (I).
  • 3. Require Laravel package: composer require php-ai/php-ml
For this example, our data is stored in a MySQL database, queried using Laravel's DB facade.

SELECT spec_year, amc_nd20 FROM QAAPT_Demo_Dataset WHERE amc_nd20 IN ('R', 'S', 'I');

Function Breakdown

Step 1: Fetch and Validate Data

The function retrieves data and ensures sufficient records for analysis. If no data exists, it returns an error response.

 
if (empty($data)) {
    return response()->json([
        'status' => 400,
        'message' => 'No data available for prediction.',
    ]);
} 
                        

Step 2: Aggregate Data by Year

Data is grouped by year, calculating the percentage of resistance types (R, S, I) for each year.

$yearlyData = [];
foreach ($data as $row) {
    $year = $row->spec_year;
    $label = $row->amc_nd20;

    if (!isset($yearlyData[$year])) {
        $yearlyData[$year] = ['R' => 0, 'S' => 0, 'I' => 0, 'total' => 0];
    }

    $yearlyData[$year][$label]++;
    $yearlyData[$year]['total']++;
}
                        

This creates a structure like:

{
    "2019": {"R": 30, "S": 50, "I": 20, "total": 100}
}
                        

Step 3: Calculate Percentages

Resistance percentages are computed using:

'R' => ($counts['R'] / $counts['total']) * 100
                        

Step 4: Train Predictive Models

Using PHP-ML’s Least Squares Regression, we train three models—one for each resistance category (R, S, I). PHP-ML’s Least Squares Regression is a powerful tool for performing linear regression analysis, a common approach in predictive modeling. This method identifies the best-fitting straight line through a set of data points by minimizing the sum of the squares of the differences between the observed and predicted values. In the context of antimicrobial resistance (AMR) prediction, it allows users to model historical trends and forecast future resistance rates effectively. Its integration into PHP-ML enables developers to implement machine learning models directly within PHP, making it a practical choice for web-based applications. By leveraging this regression technique, one can analyze and predict relationships between variables, delivering valuable insights for decision-making.

$regressionR = new \Phpml\Regression\LeastSquares();
$regressionR->train(array_map(fn($year) => [$year], $years), $resistanceR);
                        

Step 5: Forecast Future Trends

The models predict resistance percentages for the next five years:

$futureYears = range(max($years) + 1, max($years) + 5);
$futurePredictions[] = [
    'year' => $year,
    'R' => $regressionR->predict([$year])
];
                        

Example Output

Historical Data:
[
    {"year": 2019, "R": 30.0, "S": 50.0, "I": 20.0},
    {"year": 2020, "R": 32.0, "S": 48.0, "I": 20.0}
]
                        
Predictions:
[
    {"year": 2024, "R": 35.5, "S": 45.0, "I": 19.5},
    {"year": 2025, "R": 36.7, "S": 44.0, "I": 19.3}
]
                        

Benefits of AMR Prediction

  • 1. Early Warning: Identifies potential resistance surges.
  • 2. Data-Driven Decisions: Guides healthcare policies and interventions.
  • 3. Customized Interventions: Tailors antimicrobial stewardship programs.

Final Thoughts

This example illustrates how PHP and machine learning empower AMR surveillance. By combining data processing, machine learning models, and Laravel, we can transform raw data into actionable forecasts. Explore our solution at qaapt.com for more tools and insights on antimicrobial resistance.

Happy coding! 🚀