Multi step forecasting univariate time series keras,LSTM, Rstudio

I’m new in R and Machine Learning, I want to build an LSTM model for multi step forecasting univariate time series, I have found this code after tried others :

#load the data
TC2$Fecha = as.Date.character(TC2$Fecha,format = "%d.%m.%Y")
fecha = TC2$Fecha[-c(5481:5483)]
tasa = TC2$Tasa[-c(5481:5483)]
serie = data.frame(fecha,tasa) 


#Prepare the data for keras model
make_series <- function(column, start_offset, end_offset) {
  purrr::map(seq_along(column),
             function(x) {
               start <- max(0, x + start_offset)
               end <- max(0, x + end_offset)
               column[start:end]
             })
}

train_range <- serie$fecha[1:5440]
validation_range <- serie$fecha[5441:5460]
testing_range <- serie$fecha[5461:5480]
timesteps <- 20

data <- serie %>%
  as_tibble() %>%
  mutate(key = case_when(
    ymd(fecha) %in% train_range ~ "train",
    ymd(fecha) %in% validation_range ~ "validation",
    ymd(fecha) %in% testing_range ~ "testing"
  )) %>%
  mutate(value = (tasa) %>%
           as.vector()) %>%
  mutate(lookback = make_series(value, -timesteps, -1),
         target = make_series(value, 0, timesteps -1),
         target_lag = make_series(value, -1, timesteps - 2))
glimpse(data)

make_keras_data <- function(data) {
  x <- data$lookback %>%
    array_reshape(c(length(data$lookback), timesteps, 1))
  y_sequence <- data$target %>%
    array_reshape(c(length(data$target), timesteps, 1))
  y_sequence_lag <- data$target_lag %>%
    array_reshape(c(length(data$target_lag), timesteps, 1))
  y <- data$target %>%
    sapply(first)
  list(x = x,y=y, y_sequence = y_sequence,
       y_sequence_lag = y_sequence_lag)
}


training_data <- data %>%
  filter(year(fecha) > 1999, month(fecha) > 1, day(fecha) > 29,
         key == "train") %>%
  make_keras_data()
validation_data <- data %>%
  filter(key == "validation") %>%
  make_keras_data()
full_training_data <- data %>%
  filter(key != "testing", year(fecha) > 1999,
         month(fecha) > 1, day(fecha) > 29) %>%
  make_keras_data()
prediction_data <- data %>%
  filter(year(fecha) == 2019, month(fecha) == 12, day(fecha) ==9) %>%
  make_keras_data()

#create and fit keras model

model <- keras_model_sequential() %>%
  layer_lstm(units = 128, input_shape = c(timesteps, 1))%>%
  layer_dense(1)
model %>% 
  compile(optimizer= optimizer_rmsprop(), loss='mse')
model

history <- model%>% fit(
  training_data$x, 
  training_data$y,
  batch_size = 40,
  epochs = 20,
  validation_data = list(
    validation_data$x,
    validation_data$y)

After this I have been changing the number of neurons to get the model with less loss, and then:

history <- model %>% fit(
  full_training_data$x,
  full_training_data$y,
  batch_size = 40,
  epochs = 20)

forecast_future_values <- function(model, x, steps) {
  forecasted <- numeric(0)
  for (i in seq_len(steps)) {
    x_reshaped <- array_reshape(x, c(1, timesteps, 1))
    next_value <- model %>%
      predict(x_reshaped) %>%
      as.vector()
    
    forecasted <- c(forecasted, next_value)
    x <- c(x[-1], next_value)
  }
  forecasted
}
model %>%
  predict(prediction_data$x)

predictions <- forecast_future_values(model, prediction_data$x, 2*10)
predictions

I have manage, sometimes, to do as good predictions as with ARIMA models, but when I rerun the code it not always perform equal or close to equal. Due to that problem I have been wandering if it is possible to build in an easy way a LSTM model, using keras, that performs close or better that the forecasting using the functions: auto.arima(), arima() or mlp().

To prepossess the data I have tried:

scaling and differencing,

differencing and scaling,

differencing,

scaling, the best option so far

What is wrong with the code?

How can I improve performance on the LSTM model?

Some of the code I have tried:

the sunspot example,

the temperature forecasting from the
book Deep Learning with R,

rwanjohi/Time series forecasting using LSTM in R

Sigrid Keydana/ Rpubs – Time series prediction with Deep Learning,

I have poor internet connection and I have had difficulties to put the links for the codes above. Please explain any answer or recommendation, as easy and detailed as possible, thanks to all.

The data is here data

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.