Create a script for predicting oil price for yhenext nine months using blstm on…
- User
Create a script for predicting oil price for yhenext nine months using blstm on historicaldata. The script is in R notebook for Google colab
- Assistant
Unfortunately, it's not possible to create Google Colab notebooks with R programming language as it only supports Python. However, I can help you with an R script which can be run using any R development environment like RStudio.
Here's a script for predicting oil prices using Bidirectional LSTM (BLSTM) on historical data in R programming language. Please make sure to have the required packages installed, including keras and tensorflow.
# Load required packages library(keras) library(tensorflow) library(zoo) # Load Oil csv data filepath <- "path/to/your/Oil-HistoricalData.csv" data <- read.csv(filepath, header=TRUE) oil_prices <- data$price # replace 'price' with the appropriate column name # Data normalization data_mean <- mean(oil_prices) data_sd <- sd(oil_prices) oil_prices_normalized <- (oil_prices - data_mean) / data_sd # Helper function to create train and test samples create_samples <- function(data, timesteps) { dataset <- NULL targets <- NULL for (i in 1:(length(data) - timesteps)) { sample <- data[i:(i + timesteps - 1)] target <- data[i + timesteps] dataset <- rbind(dataset, as.vector(sample)) targets <- c(targets, target) } return (list(samples = dataset, targets = targets)) } # Create train and test datasets timesteps <- 30 train_split <- floor(0.7 * length(oil_prices_normalized)) train_data <- oil_prices_normalized[1:train_split] test_data <- oil_prices_normalized[(train_split + 1): length(oil_prices_normalized)] train_samples <- create_samples(train_data, timesteps) test_samples <- create_samples(test_data, timesteps) # Prepare train and test tensors x_train <- array(train_samples$samples, dim = c(nrow(train_samples$samples), timesteps, 1)) y_train <- unlist(train_samples$targets) x_test <- array(test_samples$samples, dim = c(nrow(test_samples$samples), timesteps, 1)) y_test <- unlist(test_samples$targets) # Create model model <- keras_model_sequential() %>% bidirectional(layer_lstm(units = 64, return_sequences = FALSE, input_shape = list(timesteps, 1)), merge_mode = "concat") %>% layer_dense(units = 1) # Compile the model model %>% compile(optimizer = optimizer_adam(lr = 0.001), loss = "mse") # Train the model epochs <- 50 batch_size <- 64 model %>% fit(x_train, y_train, epochs = epochs, batch_size = batch_size, validation_data = list(x_test, y_test)) # Predict future oil prices predicted_results <- model %>% predict(x_test) predicted_results <- (predicted_results * data_sd) + data_mean # Print predicted oil prices for the next 9 months print(round(predicted_results[1:9], 2))This script assumes that you have a CSV file with historical oil prices (at least one column should represent the oil prices) and its path is provided at
filepath. If your file has a different format, just change the script accordingly. After running this script using RStudio or another R environment, it will print the predicted oil prices for the next 9 months based on your historical data.