Abstract
The OSTSC package is a powerful oversampling approach for classifying univariant, but multinomial time series data in R. This article provides a brief overview of the oversampling methodology implemented by the package. A tutorial of the OSTSC package is provided. We begin by providing three test cases for the user to quickly validate the functionality in the package. To demonstrate the performance impact of OSTSC, we then provide two medium size imbalanced time series datasets. Each example applies a TensorFlow implementation of a Long Short-Term Memory (LSTM) classifier - a type of a Recurrent Neural Network (RNN) classifier - to imbalanced time series. The classifier performance is compared with and without oversampling. Finally, larger versions of these two datasets are evaluated to demonstrate the scalability of the package. The examples demonstrate that the OSTSC package improves the performance of RNN classifiers applied to highly imbalanced time series data. In particular, OSTSC is observed to increase the AUC of LSTM from 0.543 to 0.784 on a high frequency trading dataset consisting of 30,000 time series observations.
Introduction
In many real-world scenarios, the events of greatest interest are often the rarest. Whether detecting a "flash crash" in high-frequency trading, identifying a rogue trader, or spotting a life-threatening arrhythmia in an ECG, researchers frequently encounter the class imbalance problem. In these cases, the majority class dominates the dataset, causing standard machine learning models to become biased toward the frequent events and fail to identify the critical minority signals.
While standard statistical fixes like SMOTE (Synthetic Minority Over-sampling Technique) exist for static data, time series data presents a unique challenge: observations are not independent. They possess a specific temporal structure and covariance. Applying traditional oversampling methods can break these temporal correlations, introducing "noise" that lacks physical or financial logic. The OSTSC (Over Sampling for Time Series Classification) package for R addresses this by providing a specialized framework that respects the time dimension during data generation.
Methodology
The OSTSC package implements a two-stage algorithmic engine designed to augment minority classes while preserving the underlying signal structure.
1. Enhanced Structure Preserving Oversampling (EPSO)
EPSO serves as the core innovation of the package. Rather than simply duplicating rare observations, EPSO estimates the covariance structure of the minority class. It utilizes a spectral filter to isolate the "signal" from the noise, focusing on the eigenvectors that define the rare event's temporal characteristics. This ensures that any generated data mathematically mimics the original observations in terms of their flow and correlation over time.
2. Adaptive Synthetic Sampling (ADASYN)
Following the structure preservation of EPSO, the package employs ADASYN—a member of the SMOTE family—to generate synthetic positive samples. ADASYN uses nearest-neighbor interpolation to fill gaps in the feature space where data is sparse, providing a more diverse and balanced training set for the classifier.
Experimental Setup and Datasets
The authors evaluated the package using a variety of benchmarks to test functionality, performance, and scalability:
- Validation Cases: Small-scale tests using
Dataset_Synthetic_Control(sensor measurements),Dataset_Adiac(diatom identification), and a 300-observation slice of high-frequency trading data (HFT300). - Medium-Scale Benchmarks: Behavioral electricity usage (
Electrical Devices) and heartbeat sequences from heart failure patients (ECG). - Large-Scale Evaluation: Multimodal body sensing for human behavior (
MHEALTH) and a large-scale high-frequency trading (HFT) dataset containing 30,000 observations.
For classification, the package integrates with keras and TensorFlow to implement Long Short-Term Memory (LSTM) networks. LSTMs are highly effective at capturing temporal dependencies but are notoriously sensitive to class imbalance, making them ideal candidates for testing the efficacy of OSTSC.
Key Results
The application of OSTSC led to significant quantitative improvements across all tested domains:
- High-Frequency Trading (HFT): On the 30,000-observation dataset, the Area Under the Curve (AUC) for the LSTM classifier rose from 0.543 to 0.784. This represents a shift from a model that is barely better than random chance to one with strong predictive power.
- Electrocardiogram (ECG) Data: The F1 score for the rarest class (Class 2) improved from 0.36 to nearly 0.70, demonstrating the package's ability to help models identify rare medical anomalies.
- Electrical Devices: The ROC curves showed a clear performance gain, with the AUC increasing from 0.894 to 0.953.
Discussion and Scalability
A central insight of this research is that for time series, covariance is king. If a sampling scheme fails to mimic the "flow" of the original data, the model learns incorrect patterns. OSTSC acts as a vital pre-processing step that unlocks the potential of deep learning models like RNNs in imbalanced contexts.
Furthermore, the package is designed for practical use. Recognizing that oversampling can be computationally intensive, OSTSC supports multi-threaded execution via the doParallel and foreach R packages. This allows the framework to scale to datasets with tens of thousands of observations, though the authors note that very large-scale processing still benefits from high-performance computing environments.
Limitations and Future Work
Currently, the OSTSC package is limited to univariate time series classification, where only one variable is measured over time. The logical progression for this work is the expansion into multivariate time series. This will require more complex tensor computations to preserve cross-correlations between multiple simultaneous variables, a feature intended for future releases.
Summary updated