run_fnn: Train a Feedforward Neural Network (FNN) in a Counterfactual...

View source: R/counterfactual_model.R

run_fnnR Documentation

Train a Feedforward Neural Network (FNN) in a Counterfactual Scenario.

Description

Trains a feedforward neural network (FNN) model on the specified training dataset and makes predictions on the test dataset in a counterfactual scenario. The model uses meteorological variables and sin/cosine-transformed features. Scales the data before training and rescales predictions, as the model does not converge with unscaled data.

Usage

run_fnn(train, test, params, calc_shaps)

Arguments

train

A data frame or tibble containing the training dataset, including the target variable (value) and meteorological variables specified in params$meteo_variables.

test

A data frame or tibble containing the test dataset on which predictions will be made, using the same meteorological variables as in the training dataset.

params

A list of parameters that define the following:

meteo_variables

A character vector specifying the names of the meteorological variables used as inputs.

fnn

A list of hyperparameters for training the feedforward neural network, including:

  • activation_fun: The activation function for the hidden layers (e.g., "sigmoid", "tanh").

  • momentum: The momentum factor for training.

  • learningrate_scale: Factor for adjusting learning rate.

  • output_fun: The activation function for the output layer

  • batchsize: The size of the batches during training.

  • hidden_dropout: Dropout rate for the hidden layers to prevent overfitting.

  • visible_dropout: Dropout rate for the input layer.

  • hidden_layers: A vector specifying the number of neurons in each hidden layer.

  • num_epochs: Number of epochs (iterations) for training.

  • learning_rate: Initial learning rate.

calc_shaps

Boolean value. If TRUE, calculate SHAP values for the method used and format them so they can be visualised with shapviz:sv_importance() and shapviz:sv_dependence(). The SHAP values are generated for a subset (or all, depending on the size of the dataset) of the test data.

Details

This function provides flexibility for users with their own data pipelines or workflows. For a simplified pipeline, consider using run_counterfactual().

Experiment with hyperparameters such as learning_rate, batchsize, hidden_layers, and num_epochs to improve performance.

Warning: Using many or large hidden layers in combination with a high number of epochs can lead to long training times.

Value

A list with three elements:

dt_predictions

A data frame containing the test data along with the predicted values:

prediction

The predicted values from the FNN model.

prediction_lower

The same predicted values, as no quantile model is available yet for FNN.

prediction_upper

The same predicted values, as no quantile model is available yet for FNN.

model

The trained FNN model object from the deepnet::nn.train() function.

importance

SHAP importance values (if calc_shaps = TRUE). Otherwise, NULL.

Examples

data(mock_env_data)
params <- load_params()
res <- run_fnn(
  train = mock_env_data[1:80, ],
  test = mock_env_data[81:100, ], params,
  calc_shaps = FALSE
)

ubair documentation built on April 12, 2025, 2:12 a.m.