transformerForecasting R package

G H Harish Nayak

2025-02-08

Package Requirements

  1. Latest version of Python
  2. TensorFlow
  3. Keras
  4. Pandas

The TRANSFORMER function requires Python with TensorFlow and Keras installed. Without these, the function returns mock results for demonstration. We recommend users install the latest version of Python on their system. Then, create a Conda environment and install TensorFlow, Keras, and Pandas. If unfamiliar with these steps, follow the guide below.

1. Installing TensorFlow

After installing Python, run the R script below. It creates a conda environment and installs TensorFlow. If already installed, it skips the step.

install_r_tensorflow(python_path = "system_path_of_python.exe", env_name = "r-tensorflow")

Example system path for python.exe:

install_r_tensorflow(python_path = "C:/Users/User_name/AppData/Local/Programs/Python/Python312/python.exe", env_name = "r-tensorflow")

2. Installing Keras

After TensorFlow installation, install Keras in the virtual environment:

install_r_keras(tensorflow_python_path = "virtual_environment_path_of_python.exe", env_name = "r-tensorflow")

Example path:

install_r_keras(tensorflow_python_path = "C:/Users/kabil/Documents/.virtualenvs/r-tensorflow/Scripts/python.exe", env_name = "r-tensorflow")

3. Installing Pandas

Install Pandas using the following command:

install_r_pandas(tensorflow_python_path = "virtual_environment_path_of_python.exe", env_name = "r-tensorflow")

Example path:

install_r_pandas(tensorflow_python_path = "C:/Users/kabil/Documents/.virtualenvs/r-tensorflow/Scripts/python.exe", env_name = "r-tensorflow")

Running the Package

Once setup is complete, run the R package using example or custom data.

Using Example Data

library(transformerForecasting)
data("S_P_500_Close_data")
df <- S_P_500_Close_data

Running the TRANSFORMER Function

result <- TRANSFORMER(
  df = df, 
  study_variable = "Price", 
  tensorflow_python_path = "C:/Users/kabil/Documents/.virtualenvs/r-tensorflow/Scripts/python.exe",
  env_name = "r-tensorflow",
  sequence_size = 10, 
  head_size = 128, 
  num_heads = 8, 
  ff_dim = 256, 
  num_transformer_blocks = 4, 
  mlp_units = c(128), 
  mlp_dropout = 0.3, 
  dropout = 0.2, 
  epochs = 100, 
  batch_size = 32, 
  patience = 15
)

Results

Predictions

The predicted values generated by the model.

result$PREDICTIONS

Error Metrics

Root Mean Squared Error (RMSE) – Measures the average prediction error.

result$RMSE

Mean Absolute Error (MAE) – Shows the average absolute difference between actual and predicted values.

result$MAE

Mean Absolute Percentage Error (MAPE) – Represents prediction accuracy as a percentage.

result$MAPE

Symmetric Mean Absolute Percentage Error (sMAPE) – A variant of MAPE considering both over- and under-predictions.

result$sMAPE

Relative Root Mean Squared Error (RRMSE) – RMSE scaled by the mean of the actual values.

result$RRMSE

Quantile Loss – Used for probabilistic forecasting.

result$Quantile_Loss

Visualizations

Loss Plot – Displays the loss curve over epochs.

result$Loss_plot

Actual vs. Predicted – Comparison plot.

result$Actual_vs_Predicted