A Guided Path Through the Large Deviations Series

  This post serves as a short guide to the four-part series on large deviations and their applications to stochastic processes, biology, and weak-noise dynamical systems. Each article can be read independently, but together they form a coherent narrative that moves from foundational principles to modern applications. 1. Sanov’s Theorem and the Geometry of Rare Events The series begins with an intuitive introduction to Sanov’s theorem , highlighting how empirical distributions deviate from their expected behavior and how the Kullback-Leibler divergence emerges as the natural rate functional. This post lays the conceptual groundwork for understanding rare events in high-dimensional systems. Read the post → 2. Sanov’s Theorem in Living Systems The second article explores how Sanov’s theorem applies to biological and neural systems . Empirical measures, population variability, and rare transitions in gene expression or neural activity are framed through ...

Inside cycleTrendR: Methods, Architecture, and Design Choices

 Irregular time series are everywhere: wearable sensors, physiological recordings, ecological monitoring, behavioral experiments, and more. Yet most analytical tools still assume regular sampling, clean signals, and stable measurement conditions. cycleTrendR was designed to bridge this gap, not by reinventing time‑series analysis, but by integrating robust, domain‑agnostic methods into a single coherent pipeline.

This post provides a technical overview of how cycleTrendR works under the hood: the algorithms it uses, the architectural decisions behind the package, and the principles that guided its design.

1. A unified pipeline for irregular time‑series analysis

cycleTrendR is built around a simple idea: cycles, trends, and structural changes should be extracted together, not in isolation.

The internal workflow follows a modular but integrated structure:

Raw signal

   ↓

Preprocessing (dates, gaps, normalization)

   ↓

Trend estimation (LOESS/GAM)

   ↓

Detrending

   ↓

Spectral analysis (Lomb–Scargle)

   ↓

Cycle extraction (significant peaks)

   ↓

Change‑point detection

   ↓

Output (plots + structured results)

Each block is independent, but the pipeline ensures that the output of one step is consistent with the next.

Figure 1. Internal Architecture of cycleTrendR


Legend of Figure 1. The diagram summarizes the analytical pipeline: from preprocessing and robust trend estimation (LOESS/GAM), through detrending and Lomb–Scargle spectral analysis, to cycle extraction and changepoint detection. Each module is designed to handle irregular sampling, gaps, and noise, and integrates seamlessly into a reproducible workflow.

2. Trend estimation: robust LOESS/GAM for irregular data

Trend estimation is the backbone of the analysis. cycleTrendR uses robust LOESS (or optionally GAM) because:

  • LOESS adapts naturally to local data density
  • it does not assume regular sampling
  • it handles gaps without interpolation
  • the robust version reduces the influence of outliers
  • it provides smooth, interpretable trends with confidence intervals

The smoothing span is chosen to balance flexibility and stability. In practice, this means the trend follows slow physiological or behavioral changes without reacting to short‑term oscillations.

The trend is then subtracted from the original signal to obtain a detrended series suitable for spectral analysis.

3. Spectral analysis: Lomb–Scargle for uneven sampling

Classical FFT‑based methods fail when sampling is irregular. cycleTrendR instead uses the Lomb–Scargle periodogram, a method originally developed in astronomy for unevenly spaced observations.

Key advantages:

  • no need for interpolation
  • mathematically valid for irregular sampling
  • high sensitivity to periodic components
  • robust to missing data
  • interpretable frequency axis (cycles per time unit)

cycleTrendR constructs a frequency grid adapted to the time span of the data and applies optional smoothing to stabilize the spectrum.

Significant peaks are identified using a combination of:

  • local maxima
  • prominence thresholds
  • spectral power ratios

These peaks form the basis for cycle extraction.

4. Cycle extraction: from spectral peaks to interpretable oscillations

Once significant frequencies are identified, cycleTrendR computes:

  • the corresponding periods
  • the relative strength of each cycle
  • the dominant oscillation
  • secondary cycles (e.g., slow oscillations + faster rhythms)

The goal is not to reconstruct the signal, but to provide interpretable summaries of its periodic structure.

This is particularly useful for physiological data, where cycles often correspond to:

  • respiratory rhythms
  • autonomic oscillations
  • activity‑related fluctuations
  • slow behavioral cycles

cycleTrendR filters out weak or unstable peaks to avoid over‑interpretation.

5. Change‑point detection: identifying macro‑phases

Irregular time series often contain state transitions: rest → activity, stable → unstable, baseline → perturbation.

cycleTrendR uses change‑point detection to identify these macro‑phases. The default method detects changes in both mean and variance, which is well‑suited for physiological and wearable data.

To avoid over‑segmentation:

  • very short segments are merged
  • change points too close to each other are filtered
  • the penalty can be adjusted for sensitivity

The result is a set of interpretable segments that reflect the underlying dynamics of the system.

6. Data structures and API design

cycleTrendR exposes a single high‑level function:

Codice

adaptive_cycle_trend_analysis()

This function returns a structured list containing:

  • the estimated trend
  • the detrended signal
  • the Lomb–Scargle spectrum
  • significant frequencies
  • cycle summaries
  • change points
  • ready‑to‑use ggplot2 visualizations

The design philosophy is:

  • simple for beginners (one function)
  • flexible for experts (access to all intermediate objects)
  • domain‑agnostic (no assumptions about the nature of the signal)
  • side‑effect free (no automatic plotting unless requested)

This makes the package suitable for both exploratory analysis and reproducible research workflows.

7. Performance considerations

Although cycleTrendR is written in pure R, several choices improve performance:

  • vectorized operations
  • efficient smoothing and spectral routines
  • minimal copying of large objects
  • optional downsampling for very long signals
  • modular design for future parallelization

For typical wearable or physiological signals, performance is more than adequate.

8. Updates and Limitations

cycleTrendR continues to evolve, and version 0.3.0 already addresses several limitations of earlier releases — most notably, it now supports multiple sampling frequencies through a unified internal time index. This allows the package to handle Date, POSIXct, and numeric time seamlessly, adapting trend estimation, spectral analysis, and changepoint detection to the appropriate temporal scale.

A full set of worked examples — including wearable heart‑rate data from PhysioNet and neuroscience‑style numeric signals — is available in this vignette

Despite these advances, cycleTrendR is not intended to replace specialized tools. Current limitations include:

  • no multivariate analysis (yet)
  • no Bayesian or state‑space modeling
  • limited support for extremely long signals (>1e6 points), although downsampling options help mitigate this.

cycleTrendR aims to remain a general, principled framework for irregular time series analysis — robust enough for real‑world signals, yet flexible enough to grow with the needs of the community.

9. Conclusion

cycleTrendR is built on a simple idea: irregular time series deserve methods that respect their structure.

By combining robust trend estimation, Lomb–Scargle spectral analysis, cycle extraction, and change‑point detection into a unified pipeline, the package provides a principled and practical framework for analyzing real‑world signals. Additionally, it performs unit root tests (ADF and KPSS) to facilitate further analysis of the time series under investigation. 

In the next post, I will demonstrate an application of the trend and cycle analysis in simulated adherent cell count time series using cycleTrendR.

Comments

Popular posts from this blog

Understanding Anaerobic Threshold (VT2) and VO2 Max in Endurance Training

Owen's Function: A Simple Solution to Complex Problems

Cell Count Analysis with cycleTrendR