Skip to content

Overampling for ML

Smutin Daniil edited this page Apr 15, 2025 · 2 revisions

Machine Learning with samova.R

A guide to enhancing ML models using synthetic metagenomic data generation for better classification, regression, and data augmentation.


Why Use samova.R for Machine Learning?

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


Workflow Overview

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]
Loading

Step-by-Step Tutorial

1. Prepare Your Data

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)

2. Generate Synthetic Samples

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)

3. Build ML Training Set

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 rows

4. Train a Classifier

Example 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
)

5. Evaluate Performance

# Predictions
preds <- predict(rf_model, test_data)

# Confusion matrix
caret::confusionMatrix(preds, as.factor(test_data$label))

Key Applications

1. Addressing Class Imbalance

---|--- |

# Check class distribution
table(ml_data$label)

2. Feature Importance Analysis

varImpPlot(rf_model, main = "Top Predictive Taxa")

Feature Importance

3. Dimensionality Reduction (UMAP/PCA)

library(umap)
umap_results <- umap(select(ml_data, -label))
plot(umap_results$layout, col = as.factor(ml_data$label))

UMAP


Pro Tips

🔥 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

FAQ

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.


Next Steps

  1. Try the examples
  2. Experiment with different ML models (XGBoost, SVM)
  3. Contribute your use cases to the GitHub wiki!

💡 Found a bug? Open an issue on GitHub