Skip to content

Conversation

@Erennn7
Copy link
Contributor

@Erennn7 Erennn7 commented Oct 18, 2025

PR: LSTM Time Series Forecasting in R

This PR introduces a fully documented implementation of a Long Short-Term Memory (LSTM) neural network in R for time series prediction and forecasting.
The implementation leverages the keras and tensorflow packages to build, train, and evaluate LSTM models capable of learning temporal dependencies in sequential data.

Overview

The provided LSTM workflow covers:

  • Data Preprocessing:

    • Normalization of time series data to [0,1] range
    • Sequence creation for supervised learning (input sequences and target values)
  • Model Architecture:

    • Single-layer LSTM network with configurable units and dropout
    • Dense output layer for regression tasks
    • Compilation with Adam optimizer and Mean Squared Error loss
  • Training and Evaluation:

    • Training with configurable epochs and batch size
    • Validation split and monitoring of training history
    • Evaluation on test data with metrics: MSE, RMSE, MAE, and R-squared
  • Prediction Capabilities:

    • Single-step and multi-step ahead predictions
    • Denormalization of predictions for interpretation
    • Visualization of actual vs. predicted series using ggplot2
  • Best Practices:

    • Sequence length selection based on temporal dependencies
    • Dropout regularization to prevent overfitting
    • Early stopping, model checkpointing, and hyperparameter tuning

This LSTM implementation is suitable for:

  • Synthetic or real-world time series (e.g., sine waves, stock prices, sensor data)
  • Forecasting applications requiring learning of sequential patterns
  • Multi-step ahead prediction and analysis of temporal trends

Complexity

  • Training Complexity: O(n_samples × seq_length × lstm_units) per epoch
  • Inference Complexity: O(n_samples × seq_length × lstm_units)

The approach demonstrates how LSTMs can model sequential dependencies more effectively than traditional regression or moving-average methods.

@Erennn7 Erennn7 requested a review from siriak as a code owner October 18, 2025 10:40
Copilot AI review requested due to automatic review settings October 18, 2025 10:40
@Erennn7 Erennn7 requested a review from acylam as a code owner October 18, 2025 10:40
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a comprehensive implementation of Long Short-Term Memory (LSTM) neural networks for time series forecasting in R, providing a complete workflow from data preprocessing to prediction visualization.

  • Implements LSTM model architecture with configurable parameters for time series prediction
  • Provides comprehensive data preprocessing including normalization and sequence generation
  • Includes evaluation metrics, visualization capabilities, and multi-step ahead prediction functionality

@Erennn7
Copy link
Contributor Author

Erennn7 commented Oct 19, 2025

LSTM script improvements:
• Fixed train/test split to maintain temporal order for time series (sequential split instead of random sampling).
• Updated geom_line() in ggplot2 to use linewidth instead of deprecated size argument.
• Added optional note about using tidyr::pivot_longer() instead of reshape2::melt() for modern, dependency-safe data reshaping.
• Minor formatting and readability improvements in plotting and metrics sections.

All changes maintain original functionality; the model workflow and outputs remain the same.

@Erennn7
Copy link
Contributor Author

Erennn7 commented Oct 20, 2025

@siriak please have a look

Copilot AI review requested due to automatic review settings October 24, 2025 16:05
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Copilot AI review requested due to automatic review settings October 24, 2025 16:06
@siriak siriak enabled auto-merge (squash) October 24, 2025 16:06
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

# Ensure consistent column names with pivot_longer
names(plot_data_long) <- c("Index", "variable", "value")
} else {
stop("Please install 'tidyr' or 'reshape2' to create the plot (install.packages('tidyr')).")
Copy link

Copilot AI Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message references only 'tidyr' in the install command, but the message states both packages are options. Consider clarifying: stop(\"Please install 'tidyr' or 'reshape2' to create the plot: install.packages('tidyr') or install.packages('reshape2')\") to avoid confusion.

Suggested change
stop("Please install 'tidyr' or 'reshape2' to create the plot (install.packages('tidyr')).")
stop("Please install 'tidyr' or 'reshape2' to create the plot: install.packages('tidyr') or install.packages('reshape2').")

Copilot uses AI. Check for mistakes.
Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@siriak siriak disabled auto-merge October 24, 2025 16:06
@siriak siriak merged commit 84d5d78 into TheAlgorithms:master Oct 24, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants