# Farm-Specific Effects in Predicting Mastitis by Applying Machine Learning Models to Automated Milking System and Other Farm Management Data

**Authors:** Muhammad N. Dharejo, Olivier Kashongwe, Thomas Amon, Tina Kabelitz, Marcus G. Doherr

PMC · DOI: 10.3390/ani15192825 · 2025-09-28

## TL;DR

This study shows that predicting mastitis in dairy cows using machine learning works best when models are customized for each farm due to unique farm conditions.

## Contribution

The study demonstrates that farm-specific machine learning models outperform general models in predicting mastitis.

## Key findings

- Machine learning models achieved 83–92% accuracy in predicting mastitis across four German farms.
- Farm-specific models reached up to 98% AUC, but generalization to new farms was poor.
- Farm-specific factors like herd size and management practices strongly influence prediction accuracy.

## Abstract

Mastitis is a common disease in dairy cows that can cause major losses for farmers. Predicting it early can help prevent problems. This study used computer programs called ‘machine learning models’ to see how well they could predict mastitis in cows using data from automatic milking robots and farm records, with a focus on differences between farms. We looked at information from four farms in Germany, covering nearly 6 million records from 2019 to 2024. The machine learning models were pretty accurate overall—in many cases, they predicted mastitis correctly around 83–92% of the time. But the accuracy changed depending on which farm the data came from. Each farm had its own unique setup, like different herd sizes and management styles. When the models were tested on combined data from all farms and on the data of each individual farm, they worked well—but when trying to predict mastitis on a farm not included in the dataset for training the models, the results were not as good. This shows that each farm is different, and using a one-size-fits-all model might not work. To achieve the best results, it is better to customize the prediction model to each farm.

Early and accurate prediction of mastitis is crucial in effective herd management and minimizing economic losses. This study investigated the effects of farm-specific factors on the accuracy of mastitis predictions by applying machine learning (ML) models to an automated milking system (AMS) and farm management data. We analyzed a large dataset consisting of 5.88 million observations over the period of 2019–2024 from four dairy farms in Germany. Six ML algorithms were applied to predict mastitis occurrence, with a focus on understanding how farm-specific factors like herd size, management practices, and farm environment may influence prediction accuracy. For training and testing on combined data, the accuracy, sensitivity and specificity ranged between 83 and 92%, 78 and 93% and 83 and 92%, respectively, with an area under curve (AUC) between 91 and 96%. However, under mixed-to-individual farm effects analysis, results exposed weaknesses in the generalization. Models adapted well to internal patterns when analyzing each individual farm separately, reaching very high AUCs of up to 98%, but the results were significantly different again when analyzed with a leave-one-out approach. The analysis determined that data from each farm carries variable underlying patterns, suggesting that a tailored approach to each farm’s unique characteristics might improve mastitis prediction through ML.

## Linked entities

- **Diseases:** mastitis (MONDO:0006849)
- **Species:** Bos taurus (taxon 9913)

## Full-text entities

- **Diseases:** Mastitis (MESH:D008413)

## Figures

2 figures with captions in the complete paper: https://tomesphere.com/paper/PMC12523250/full.md

---
Source: https://tomesphere.com/paper/PMC12523250