JaiPizGon / TSLSTMplus

GNU General Public License v3.0
0 stars 1 forks source link

Univariate Time Series implement to tslstm #2

Closed MickoAmoroto closed 1 month ago

MickoAmoroto commented 4 months ago

Greetings!

I would like to know if the package works for univariate time series data (with no regressors/exogenous variables). As stated by the examples given in this package, the model tries to predict the output given there are regressors. Is there any workaround for this?

Hoping for a kind response. :)

JaiPizGon commented 3 months ago

Hi!

Sadly, there is no support for a model without external regressors due to bugs (and lack of developer free time). I would add it in the next version of the package and leave this issue open to remind me to do it.

I would close this issue when I upload a package version that support this.

Sorry for the inconvinience.

JaiPizGon commented 3 months ago

Hi again!

I was thinking about this issue and it was like an itch in my mind to let this as it was. Therefore, I have created version 1.0.4 that includes support for model without external regressors and without EarlyStopping (that I did not realise that was not working without EarlyStopping). You can install this version from github or wait a couple of days to have it available from CRAN.

A working example of this new features can be found below:

# Load necessary library
library(dplyr)
library(plotly)
library(TSLSTMplus)
library(keras)

# Generate synthetic data
set.seed(123) # For reproducibility
n <- 1000 # Number of data points
data <- data.frame(
    time = 1:n,
    Y = sin(1:n/10) + cos(1:n/10) + rnorm(n) * 0.1 # Output variable
)

fdata_ts <- ts(data)

# Split the data into training and test sets
split_index <- round(nrow(fdata_ts) * 0.9) # 90% for training, 10% for testing
fdata_tr <- subset(fdata_ts, end = split_index)
fdata_ts <- subset(fdata_ts, start = split_index + 1)

# Create time series
y_tr <- fdata_tr[,2]
y_ts <- fdata_ts[,2]

# Train the TSLSTM model on the training set
TSLSTM <- ts.lstm(ts=y_tr,
                  xreg = NULL,
                  tsLag = 8,
                  xregLag = NULL,
                  LSTMUnits = c(30, 30, 20),
                  DenseUnits = NULL, #c(5),
                  DropoutRate = 0.20,
                  Epochs = 50,
                  CompLoss = "mse",
                  CompMetrics = "mae",
                  Optimizer = optimizer_rmsprop,
                  ScaleOutput = 'scale', # NULL, 'minmax'
                  ScaleInput = 'scale', # NULL, 'minmax'
                  BatchSize = 1,
                  LSTMActivationFn = 'tanh',
                  DenseActivationFn = 'relu',
                  LagsAsSequences = TRUE,
                  ValidationSplit = 0.2,
                  verbose=1,
                  RandomState=150,
                  EarlyStopping=NULL
)

# Summary of the trained model
summary(TSLSTM)

# Predictions for the train set
pred_tr <- predict(TSLSTM, xreg = NULL, ts = y_tr)
pred_ts <- predict(TSLSTM, xreg = NULL, ts = y_ts)
p <- autoplot(y_tr, series = "Train") +
    forecast::autolayer(pred_tr, series = "Predicted Train") +
    forecast::autolayer(y_ts, series = "Test") +
    forecast::autolayer(pred_ts, series = "Predicted Test")

plotly_plot <- ggplotly(p) ## To zoom the prediction plot
plotly_plot

# Predictions for the test set
# Setting the horizon argument makes using the predictions as output
# for next instant instead of the actual value
pred_ts <- predict(TSLSTM, horizon = length(y_ts), xreg = NULL, ts = y_tr, xreg.new = NULL)
p <- autoplot(y_tr, series = "Train") +
    forecast::autolayer(pred_tr, series = "Predicted Train") +
    forecast::autolayer(y_ts, series = "Test") +
    forecast::autolayer(pred_ts, series = "Predicted Test (1 by 1)")

plotly_plot <- ggplotly(p) ## To zoom the prediction plot
plotly_plot

Please try it out and check if it works.

Thanks in advance!

MickoAmoroto commented 3 months ago

Greetings!

Thank you so much for the response. I will try to get on the newest version of the package right away. I will provide an update pointing out further issues and so on.

Have a great day ahead!

On Mon, 11 Mar 2024, 02:45 JaiPizGon, @.***> wrote:

Hi again!

I was thinking about this issue and it was like an itch in my mind to let this as it was. Therefore, I have created version 1.0.4 that includes support for model without external regressors and without EarlyStopping (that I did not realise that was not working without EarlyStopping). You can install this version from github or wait a couple of days to have it available from CRAN.

A working example of this new features can be found below:

Load necessary library

library(dplyr) library(plotly) library(TSLSTMplus) library(keras)

Generate synthetic data

set.seed(123) # For reproducibilityn <- 1000 # Number of data pointsdata <- data.frame( time = 1:n, Y = sin(1:n/10) + cos(1:n/10) + rnorm(n) * 0.1 # Output variable ) fdata_ts <- ts(data)

Split the data into training and test setssplit_index <- round(nrow(fdata_ts) * 0.9) # 90% for training, 10% for testingfdata_tr <- subset(fdata_ts, end = split_index)fdata_ts <- subset(fdata_ts, start = split_index + 1)

Create time seriesy_tr <- fdata_tr[,2]y_ts <- fdata_ts[,2]

Train the TSLSTM model on the training setTSLSTM <- ts.lstm(ts=y_tr,

              xreg = NULL,
              tsLag = 8,
              xregLag = NULL,
              LSTMUnits = c(30, 30, 20),
              DenseUnits = NULL, #c(5),
              DropoutRate = 0.20,
              Epochs = 50,
              CompLoss = "mse",
              CompMetrics = "mae",
              Optimizer = optimizer_rmsprop,
              ScaleOutput = 'scale', # NULL, 'minmax'
              ScaleInput = 'scale', # NULL, 'minmax'
              BatchSize = 1,
              LSTMActivationFn = 'tanh',
              DenseActivationFn = 'relu',
              LagsAsSequences = TRUE,
              ValidationSplit = 0.2,
              verbose=1,
              RandomState=150,
              EarlyStopping=NULL

)

Summary of the trained model

summary(TSLSTM)

Predictions for the train setpred_tr <- predict(TSLSTM, xreg = NULL, ts = y_tr)pred_ts <- predict(TSLSTM, xreg = NULL, ts = y_ts)p <- autoplot(y_tr, series = "Train") +

forecast::autolayer(pred_tr, series = "Predicted Train") +
forecast::autolayer(y_ts, series = "Test") +
forecast::autolayer(pred_ts, series = "Predicted Test")

plotly_plot <- ggplotly(p) ## To zoom the prediction plotplotly_plot

Predictions for the test set# Setting the horizon argument makes using the predictions as output# for next instant instead of the actual valuepred_ts <- predict(TSLSTM, horizon = length(y_ts), xreg = NULL, ts = y_tr, xreg.new = NULL)p <- autoplot(y_tr, series = "Train") +

forecast::autolayer(pred_tr, series = "Predicted Train") +
forecast::autolayer(y_ts, series = "Test") +
forecast::autolayer(pred_ts, series = "Predicted Test (1 by 1)")

plotly_plot <- ggplotly(p) ## To zoom the prediction plotplotly_plot

Please try it out and check if it works.

Thanks in advance!

— Reply to this email directly, view it on GitHub https://github.com/JaiPizGon/TSLSTMplus/issues/2#issuecomment-1987325808, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6UFGG4ZTXVEENPYI3UEKC3YXSS53AVCNFSM6AAAAABEOSNQNOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOBXGMZDKOBQHA . You are receiving this because you authored the thread.Message ID: @.***>

MickoAmoroto commented 3 months ago

Hello. I have found a slight issue i guess from the prediction of the lstm set. Following the code you have provided, there seems to have a huge difference in prediction when it comes to setting the horizon part. It can be seen from the 2 plots that the horizon argument made the prediction way off than with it not being set. Can you advice what to do on this?

`# Predictions for the train set pred_tr <- predict(TSLSTM, xreg = NULL, ts = y_tr) pred_ts <- predict(TSLSTM, xreg = NULL, ts = y_ts) p <- autoplot(y_tr, series = "Train") + forecast::autolayer(pred_tr, series = "Predicted Train") + forecast::autolayer(y_ts, series = "Test") + forecast::autolayer(pred_ts, series = "Predicted Test")

plotly_plot <- ggplotly(p) ## To zoom the prediction plot plotly_plot

Predictions for the test set

Setting the horizon argument makes using the predictions as output

for next instant instead of the actual value

pred_ts <- predict(TSLSTM, horizon = length(y_ts), xreg = NULL, ts = y_tr, xreg.new = NULL) p <- autoplot(y_tr, series = "Train") + forecast::autolayer(pred_tr, series = "Predicted Train") + forecast::autolayer(y_ts, series = "Test") + forecast::autolayer(pred_ts, series = "Predicted Test (1 by 1)") `

lstmpred1 lstmpred2

JaiPizGon commented 3 months ago

Hi again!

If you use the horizon argument, what you are stating is that the model should not use the real value of the output to predict new values. If horizon is NULL, you are stating that you want to use the real output to predict new samples.

image

In the previous image, purple timesteps are available data and blue timesteps are predictions made by your model. As you can see, the two methods (with and without horizon) should give different results, as you are passing different data to predict a new sample.

In general, you shall be using the model with the horizon set to some value to assess its performance, because when you want to predict new values the real values of the output are not known. Furthermore, and as can be seen in your example, the model works severely worse when using its predictions than when using the real values, as the prediction error would accumulate through each sample.