Skip to content

Instantly share code, notes, and snippets.

@riqbal-k
Created March 18, 2023 18:03
Show Gist options
  • Save riqbal-k/7fe098013b967d44d1ddce6b4f14b5ee to your computer and use it in GitHub Desktop.
Save riqbal-k/7fe098013b967d44d1ddce6b4f14b5ee to your computer and use it in GitHub Desktop.
split_data <- function(stock, lookback) {
data_raw <- as.matrix(stock) # convert to matrix
data <- array(dim = c(0, lookback, ncol(data_raw)))
# create all possible sequences of length lookback
for (index in 1:(nrow(data_raw) - lookback)) {
data <- rbind(data, data_raw[index:(index + lookback - 1), ])
}
test_set_size <- round(0.2 * nrow(data))
train_set_size <- nrow(data) - test_set_size
x_train <- data[1:train_set_size, 1:(lookback - 1), drop = FALSE]
y_train <- data[1:train_set_size, lookback, drop = FALSE]
x_test <- data[(train_set_size + 1):nrow(data), 1:(lookback - 1), drop = FALSE]
y_test <- data[(train_set_size + 1):nrow(data), lookback, drop = FALSE]
return(list(x_train = x_train, y_train = y_train,
x_test = x_test, y_test = y_test))
}
#divide data into train and test
lookback <- 8 # choose sequence length
split_data <- split_data(returns, lookback) # assuming "returns" is a data frame
x_train <- split_data$x_train
y_train <- split_data$y_train
x_test <- split_data$x_test
y_test <- split_data$y_test
cat(paste('x_train.shape = ', dim(x_train), '\n'))
cat(paste('y_train.shape = ', dim(y_train), '\n'))
cat(paste('x_test.shape = ', dim(x_test), '\n'))
cat(paste('y_test.shape = ', dim(y_test), '\n'))
#decide hyperparameters
input_dim <- 1
hidden_dim <- 32
num_layers <- 2
output_dim <- 1
num_epochs <- 100
# Reshape the training and test data to have a 3D tensor shape
x_train <- array_reshape(x_train, c(dim(x_train)[1], lookback-1, input_dim))
x_test <- array_reshape(x_test, c(dim(x_test)[1], lookback-1, input_dim))
# Define the LSTM model using Keras
model <- keras_model_sequential() %>%
layer_lstm(units = hidden_dim, return_sequences = TRUE, input_shape = c(lookback-1, input_dim)) %>%
layer_lstm(units = hidden_dim) %>%
layer_dense(units = output_dim)
# Compile the model using the mean squared error loss and the Adam optimizer
model %>% compile(loss = "mean_squared_error", optimizer = optimizer_adam(learning_rate = 0.01))
# Train the model on the training data
history <- model %>% fit(x_train, y_train, epochs = num_epochs, batch_size = 16, validation_data = list(x_test, y_test))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment