-
Notifications
You must be signed in to change notification settings - Fork 1
Overampling for ML
A guide to enhancing ML models using synthetic metagenomic data generation for better classification, regression, and data augmentation.
Metagenomic datasets are often:
✅ Small – Limited samples for training robust models
✅ Imbalanced – Rare taxa or conditions are underrepresented
✅ Noisy – Technical biases and missing values
samova.R solves these issues by:
🔹 Generating biologically realistic synthetic samples
🔹 Balancing class distributions for better ML fairness
🔹 Providing augmented training data without overfitting
graph TD
A[Real Data] --> B(Preprocess & Cluster)
B --> C[Generate Synthetic Samples]
C --> D{Machine Learning}
D --> E[Train Model]
D --> F[Validate Performance]
Load real metagenomic data (e.g., from GMrepo):
library(samovaR)
library(tidyverse)
# Download healthy/diseased gut microbiome data
healthy_data <- GMrepo_type2data(mesh_ids = "D006262") %>%
teatree_trim(threshold_amount = 1e-4)
diseased_data <- GMrepo_type2data(mesh_ids = "D003967") %>%
teatree_trim(threshold_amount = 1e-4)Create augmented datasets for each class:
generate_samples <- function(data, n) {
data %>%
tealeaves_pack() %>%
teabag_brew(min_cluster_size = 30) %>%
concotion_pour() %>%
samovar_boil(N = n)
}
syn_healthy <- generate_samples(healthy_data, 500)
syn_diseased <- generate_samples(diseased_data, 500)Combine real and synthetic data with labels:
# Create labeled datasets
healthy_df <- syn_healthy$data %>%
mutate(label = "healthy")
diseased_df <- syn_diseased$data %>%
mutate(label = "diseased")
# Merge and shuffle
ml_data <- bind_rows(healthy_df, diseased_df) %>%
sample_frac(1) # Shuffle rowsExample using Random Forest:
library(randomForest)
# Split data
train_idx <- sample(1:nrow(ml_data), 0.7*nrow(ml_data))
train_data <- ml_data[train_idx, ]
test_data <- ml_data[-train_idx, ]
# Train model
rf_model <- randomForest(
x = select(train_data, -label),
y = as.factor(train_data$label),
importance = TRUE
)# Predictions
preds <- predict(rf_model, test_data)
# Confusion matrix
caret::confusionMatrix(preds, as.factor(test_data$label))---|---
|
# Check class distribution
table(ml_data$label)varImpPlot(rf_model, main = "Top Predictive Taxa")library(umap)
umap_results <- umap(select(ml_data, -label))
plot(umap_results$layout, col = as.factor(ml_data$label))🔥 Hyperparameter Tuning
# Use tidymodels for advanced tuning
library(tidymodels)
rf_spec <- rand_forest(mtry = tune(), trees = 1000) %>%
set_engine("randomForest") %>%
set_mode("classification")📈 Compare With/Without Augmentation
| Metric | Original Data | +samova.R Data |
|---|---|---|
| Accuracy | 0.96 | 0.99 |
| F1-Score | 0.97 | 0.99 |
❓ How many synthetic samples should I generate?
→ Start with 2-5x your original sample size. Monitor validation accuracy.
❓ Can I use this for regression tasks?
→ Yes! Replace classification labels with continuous values.
❓ How to handle overfitting?
→ Use regularization (e.g., LASSO) or reduce synthetic sample complexity.
- Try the examples
- Experiment with different ML models (XGBoost, SVM)
- Contribute your use cases to the GitHub wiki!
💡 Found a bug? Open an issue on GitHub