A side-by-side comparison of a custom SMOTE implementation against the smotefamily R library, with statistical similarity metrics and visualisations.
SMOTE (Synthetic Minority Over-sampling Technique) addresses class imbalance by generating synthetic samples for the minority class. For each minority observation, it interpolates between the point and one of its k nearest neighbours:
synthetic = x_i + λ · (x_nn − x_i), λ ~ Uniform(0, 1)
.
├── smote_main.R # Entry point — run this file only
├── smote_function.R # Custom SMOTE implementation
├── smote_data.R # Data preparation & synthetic sample generation
├── smote_plots.R # All plot definitions
└── smote_compose.R # patchwork layout & render
smote_main.R
└── smote_function.R (defines smote_custom())
└── smote_data.R (depends on smote_function.R)
└── smote_plots.R (depends on smote_data.R)
└── smote_compose.R (depends on smote_plots.R)
All files must be in the same directory. Open smote_main.R and run it:
source("smote_main.R")The script auto-installs any missing packages, runs all modules in order, and renders the final plot in the RStudio viewer.
| Package | Role |
|---|---|
smotefamily |
Library SMOTE baseline |
ggplot2 |
All plots |
patchwork |
Multi-panel layout |
dplyr |
Data wrangling |
tidyr |
Reshaping for density plot |
png / grid |
Fixed-dimension rendering in RStudio |
Install all at once:
install.packages(c("smotefamily", "ggplot2", "patchwork", "dplyr", "tidyr", "png"))Iris (datasets::iris) — setosa species used as the minority class (50 observations, 4 numeric features).
| Parameter | Value |
|---|---|
| Over-sampling rate | 400% (4×) |
| Nearest neighbours k | 3 |
| Random seed | 42 |
The final plot (smote_comparison.png) contains 5 panels arranged in two rows.
Row 1 — Scatter plots (Petal Length × Petal Width)
| Panel | Description |
|---|---|
| Custom SMOTE | Synthetic points only. Annotation box shows n, mean ± SD for Petal Length and Petal Width. |
| Overlay | Both implementations overlaid with original points in black. High overlap between red (▲) and blue (■) indicates consistent behaviour. |
| smotefamily Library | Synthetic points only. Same annotation as Custom SMOTE panel for direct comparison. |
Row 2 — Distributional analysis
| Panel | Description |
|---|---|
| Similarity Metrics | Per-feature heatmap with two descriptive metrics (see below). Green = similar, red = divergent. |
| Feature-wise Density | Kernel density curves for all 4 features across Original, Custom, and Library sets. |
| Metric | Formula | Interpretation |
|---|---|---|
| Mean diff | abs(mean(custom) − mean(library)) |
Distance between central tendencies. Near 0 = both methods centre synthetic points in the same region. |
| SD diff | abs(sd(custom) − sd(library)) |
Distance between spreads. Near 0 = both methods produce similar variability. |
A well-implemented custom SMOTE should show:
- Scatter plots with similar point density and range to the library output
- Overlay panel with high overlap between custom and library points
- Density curves that closely track each other and the original distribution
- Mean diff and SD diff values close to 0 across all features
Large deviations in any of these indicate a discrepancy in interpolation logic, neighbour selection, or random sampling behaviour.
- Chawla, N. V., et al. (2002). SMOTE: Synthetic Minority Over-sampling Technique. Journal of Artificial Intelligence Research, 16, 321–357.
- Siriseriwan, W. (2019). smotefamily: A Family of SMOTE Based Over-Sampling Techniques. R package version 1.3.1.
- Fisher, R. A. (1936). The use of multiple measurements in taxonomic problems. Annals of Eugenics, 7(2), 179–188.
