| Version: | 1.1.3 |
| Title: | Sensitivity Analysis of Neural Networks |
| Description: | Analysis functions to quantify inputs importance in neural network models. Functions are available for calculating and plotting the inputs importance and obtaining the activation function of each neuron layer and its derivatives. The importance of a given input is defined as the distribution of the derivatives of the output with respect to that input in each training data point <doi:10.18637/jss.v102.i07>. |
| Author: | José Portela González [aut], Antonio Muñoz San Roque [aut], Jaime Pizarroso Gonzalo [aut, ctb, cre] |
| Maintainer: | Jaime Pizarroso Gonzalo <jpizarroso@comillas.edu> |
| Imports: | ggplot2, gridExtra, NeuralNetTools, reshape2, caret,fastDummies, stringr, Hmisc, ggforce, scales, ggnewscale,magrittr, ggrepel, ggbreak, dplyr |
| Suggests: | h2o, RSNNS, nnet, neuralnet, plotly, e1071 |
| RoxygenNote: | 7.3.1 |
| NeedsCompilation: | no |
| URL: | https://github.com/JaiPizGon/NeuralSens |
| BugReports: | https://github.com/JaiPizGon/NeuralSens/issues |
| License: | GPL-2 |GPL-3 [expanded from: GPL (≥ 2)] |
| Encoding: | UTF-8 |
| LazyData: | true |
| Packaged: | 2024-05-11 19:26:10 UTC; jpizarroso |
| Repository: | CRAN |
| Date/Publication: | 2024-05-11 19:43:03 UTC |
NeuralSens: Sensitivity Analysis of Neural Networks
Description
Visualization and analysis tools to aid in the interpretation ofneural network models.
Author(s)
Maintainer: Jaime Pizarroso Gonzalojpizarroso@comillas.edu [contributor]
Authors:
José Portela GonzálezJose.Portela@iit.comillas.edu
Antonio Muñoz San Roqueantonio.munoz@iit.comillas.edu
See Also
Useful links:
Report bugs athttps://github.com/JaiPizGon/NeuralSens/issues
Activation function of neuron
Description
Evaluate activation function of a neuron
Usage
ActFunc(type = "sigmoid", ...)Arguments
type |
|
... | extra arguments needed to calculate the functions |
Value
numeric output of the neuron
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
Examples
# Return the sigmoid activation function of a neuronActivationFunction <- ActFunc("sigmoid")# Return the tanh activation function of a neuronActivationFunction <- ActFunc("tanh")# Return the activation function of several layers of neuronsactfuncs <- c("linear","sigmoid","linear")ActivationFunctions <- sapply(actfuncs, ActFunc)Sensitivity alpha-curve associated to MLP function
Description
Obtain sensitivity alpha-curves associated to MLP function obtained fromthe sensitivities returned bySensAnalysisMLP.
Usage
AlphaSensAnalysis( sens, tol = NULL, max_alpha = 15, curve_equal_origin = FALSE, inp_var = NULL, line_width = 1, title = "Alpha curve of Lp norm values", alpha_bar = 1, kind = "line")Arguments
sens | sensitivity object returned by |
tol | difference between M_alpha and maximum sensitivity of the sensitivity of each input variable |
max_alpha | maximum alpha value to analyze |
curve_equal_origin | make all the curves begin at (1,0) |
inp_var |
|
line_width |
|
title |
|
alpha_bar |
|
kind |
|
Value
alpha-curves of the MLP function
Examples
mod <- RSNNS::mlp(simdata[, c("X1", "X2", "X3")], simdata[, "Y"], maxit = 1000, size = 15, linOut = TRUE)sens <- SensAnalysisMLP(mod, trData = simdata, output_name = "Y", plot = FALSE)AlphaSensAnalysis(sens)Sensitivity alpha-curve associated to MLP function of an input variable
Description
Obtain sensitivity alpha-curve associated to MLP function obtained fromthe sensitivities returned bySensAnalysisMLP of an input variable.
Usage
AlphaSensCurve(sens, tol = NULL, max_alpha = 100)Arguments
sens | raw sensitivities of the MLP output with respect to input variable. |
tol | difference between M_alpha and maximum sensitivity of the sensitivity of each input variable |
max_alpha | maximum alpha value to analyze |
Value
alpha-curve of the MLP function
Examples
mod <- RSNNS::mlp(simdata[, c("X1", "X2", "X3")], simdata[, "Y"], maxit = 1000, size = 15, linOut = TRUE)sens <- SensAnalysisMLP(mod, trData = simdata, output_name = "Y", plot = FALSE)AlphaSensCurve(sens$raw_sens[[1]][,1])Change significance of boot SensMLP Class
Description
For a SensMLP Class object, change the significance level of the statistical tests
Usage
ChangeBootAlpha(x, boot.alpha)Arguments
x |
|
boot.alpha |
|
Value
SensMLP object with changed significance level. All boot relatedmetrics are changed
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000## TRAIN nnet NNET --------------------------------------------------------set.seed(150)nnetmod <- caret::train(DEM ~ ., data = fdata.Reg.tr, method = "nnet", tuneGrid = expand.grid(size = c(1), decay = c(0.01)), trControl = caret::trainControl(method="none"), preProcess = c('center', 'scale'), linout = FALSE, trace = FALSE, maxit = 300)# Try SensAnalysisMLPsens <- NeuralSens::SensAnalysisMLP(nnetmod, trData = fdata.Reg.tr, plot = FALSE, boot.R=2, output_name='DEM')NeuralSens::ChangeBootAlpha(sens, boot.alpha=0.1)Sensitivity analysis plot over time of the data
Description
Plot of sensitivity of the neural network output respectto the inputs over the time variable from the data provided
Usage
CombineSens(object, comb_type = "mean")Arguments
object |
|
comb_type | Function to combine the matrixes of the |
Value
SensMLP object with the sensitivities combined
Examples
fdata <- iris## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata)[1:ncol(fdata)-1], collapse = " + ")form <- formula(paste(names(fdata)[5], form, sep = " ~ "))set.seed(150)mod <- nnet::nnet(form, data = fdata, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# mod should be a neural network classification modelsens <- SensAnalysisMLP(mod, trData = fdata, output_name = 'Species')combinesens <- CombineSens(sens, "sqmean")Plot sensitivities of a neural network model
Description
Function to plot the sensitivities created bySensAnalysisMLP.
Usage
ComputeHessMeasures(sens)Arguments
sens |
|
Value
SensAnalysisMLP object with the sensitivities calculated
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try SensAnalysisMLPsens <- NeuralSens::SensAnalysisMLP(nnetmod, trData = nntrData, plot = FALSE)Plot sensitivities of a neural network model
Description
Function to plot the sensitivities created bySensAnalysisMLP.
Usage
ComputeSensMeasures(sens)Arguments
sens |
|
Value
SensAnalysisMLP object with the sensitivities calculated
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try SensAnalysisMLPsens <- NeuralSens::SensAnalysisMLP(nnetmod, trData = nntrData, plot = FALSE)Data frame with 4 variables
Description
Training dataset with values of temperature and working day to predict electrical demand
Format
A data frame with 1980 rows and 4 variables:
- DATE
date of the measure
- DEM
electrical demand
- WD
Working Day: index which express how much work is made that day
- TEMP
weather temperature
Author(s)
Jose Portela Gonzalez
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
Data frame with 3 variables
Description
Validation dataset with values of temperature and working day to predict electrical demand
Format
A data frame with 7 rows and 3 variables:
- DATE
date of the measure
- WD
Working Day: index which express how much work is made that day
- TEMP
weather temperature
Author(s)
Jose Portela Gonzalez
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
Second derivative of activation function of neuron
Description
Evaluate second derivative of activation function of a neuron
Usage
Der2ActFunc(type = "sigmoid", ...)Arguments
type |
|
... | extra arguments needed to calculate the functions |
Value
numeric output of the neuron
Examples
# Return derivative of the sigmoid activation function of a neuronActivationFunction <- Der2ActFunc("sigmoid")# Return derivative of the tanh activation function of a neuronActivationFunction <- Der2ActFunc("tanh")# Return derivative of the activation function of several layers of neuronsactfuncs <- c("linear","sigmoid","linear")ActivationFunctions <- sapply(actfuncs, Der2ActFunc)Third derivative of activation function of neuron
Description
Evaluate third derivative of activation function of a neuron
Usage
Der3ActFunc(type = "sigmoid", ...)Arguments
type |
|
... | extra arguments needed to calculate the functions |
Value
numeric output of the neuron
Examples
# Return derivative of the sigmoid activation function of a neuronActivationFunction <- Der3ActFunc("sigmoid")# Return derivative of the tanh activation function of a neuronActivationFunction <- Der3ActFunc("tanh")# Return derivative of the activation function of several layers of neuronsactfuncs <- c("linear","sigmoid","linear")ActivationFunctions <- sapply(actfuncs, Der3ActFunc)Derivative of activation function of neuron
Description
Evaluate derivative of activation function of a neuron
Usage
DerActFunc(type = "sigmoid", ...)Arguments
type |
|
... | extra arguments needed to calculate the functions |
Value
numeric output of the neuron
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
Examples
# Return derivative of the sigmoid activation function of a neuronActivationFunction <- DerActFunc("sigmoid")# Return derivative of the tanh activation function of a neuronActivationFunction <- DerActFunc("tanh")# Return derivative of the activation function of several layers of neuronsactfuncs <- c("linear","sigmoid","linear")ActivationFunctions <- sapply(actfuncs, DerActFunc)Second derivatives 3D scatter or surface plot against input values
Description
3D Plot of second derivatives of the neural network output respectto the inputs. This function useplotly instead ofggplot2 toachieve better visualization
Usage
HessDotPlot( object, fdata = NULL, input_vars = "all", input_vars2 = "all", output_vars = "all", surface = FALSE, grid = FALSE, color = NULL, ...)Arguments
object | fitted neural network model or |
fdata |
|
input_vars |
|
input_vars2 |
|
output_vars |
|
surface |
|
grid |
|
color |
|
... | further arguments that should be passed to |
Value
list of 3Dgeom_point plots for the inputs variables representing thesensitivity of each output respect to the inputs
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try HessDotPlotNeuralSens::HessDotPlot(nnetmod, fdata = nntrData, surface = TRUE, color = "WD")Feature sensitivity plot
Description
Show the distribution of the sensitivities of the outputingeom_sina() plot which color depends on the input values
Usage
HessFeaturePlot(object, fdata = NULL, ...)Arguments
object | fitted neural network model or |
fdata |
|
... | further arguments that should be passed to |
Value
list of Feature sensitivity plot as described inhttps://www.r-bloggers.com/2019/03/a-gentle-introduction-to-shap-values-in-r/
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try SensAnalysisMLPhess <- NeuralSens::HessianMLP(nnetmod, trData = nntrData, plot = FALSE)NeuralSens::HessFeaturePlot(hess)Constructor of the HessMLP Class
Description
Create an object of HessMLP class
Usage
HessMLP( sens = list(), raw_sens = list(), mlp_struct = numeric(), trData = data.frame(), coefnames = character(), output_name = character())Arguments
sens |
|
raw_sens |
|
mlp_struct |
|
trData |
|
coefnames |
|
output_name |
|
Value
HessMLP object
Convert a HessMLP to a SensMLP object
Description
Auxiliary function to turn a HessMLP object to a SensMLP object in order to use the plot-related functionsassociated with SensMLP
Usage
HessToSensMLP(x)Arguments
x |
|
Value
SensMLP object
Sensitivity of MLP models
Description
Function for evaluating the sensitivities of the inputsvariables in a mlp model
Usage
HessianMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, ...)## Default S3 method:HessianMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, trData, actfunc = NULL, deractfunc = NULL, der2actfunc = NULL, preProc = NULL, terms = NULL, output_name = NULL, ...)## S3 method for class 'train'HessianMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, ...)## S3 method for class 'H2OMultinomialModel'HessianMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, ...)## S3 method for class 'H2ORegressionModel'HessianMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, ...)## S3 method for class 'list'HessianMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, trData, actfunc, ...)## S3 method for class 'mlp'HessianMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, trData, preProc = NULL, terms = NULL, ...)## S3 method for class 'nn'HessianMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, preProc = NULL, terms = NULL, ...)## S3 method for class 'nnet'HessianMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, trData, preProc = NULL, terms = NULL, ...)## S3 method for class 'nnetar'HessianMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, ...)## S3 method for class 'numeric'HessianMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, trData, actfunc = NULL, preProc = NULL, terms = NULL, ...)Arguments
MLP.fit | fitted neural network model |
.returnSens | DEPRECATED |
plot |
|
.rawSens | DEPRECATED |
sens_origin_layer |
|
sens_end_layer |
|
sens_origin_input |
|
sens_end_input |
|
... | additional arguments passed to or from other methods |
trData |
|
actfunc |
|
deractfunc |
|
der2actfunc |
|
preProc | preProcess structure applied to the training data. See also |
terms | function applied to the training data to create factors. Seealso |
output_name |
|
Details
In case of using an input of classfactor and a package whichneed to enter the input data as matrix, the dummies must be created beforetraining the neural network.
After that, the training data must be given to the function using thetrData argument.
Value
SensMLP object with the sensitivity metrics and sensitivities ofthe MLP model passed to the function.
Plots
Plot 1: colorful plot with the classificationof the classes in a 2D map
Plot 2: b/w plot with probability of thechosen class in a 2D map
Plot 3: plot with the stats::predictions ofthe data provided
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 100decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try HessianMLPNeuralSens::HessianMLP(nnetmod, trData = nntrData, plot = FALSE)# Try HessianMLP to calculate sensitivities with respect to output of hidden neuronesNeuralSens::HessianMLP(nnetmod, trData = nntrData, sens_origin_layer = 2, sens_end_layer = "last", sens_origin_input = FALSE, sens_end_input = FALSE)## Train caret NNET ------------------------------------------------------------# Create trainControlctrl_tune <- caret::trainControl(method = "boot", savePredictions = FALSE, summaryFunction = caret::defaultSummary)set.seed(150) #For replicationcaretmod <- caret::train(form = DEM~., data = fdata.Reg.tr, method = "nnet", linout = TRUE, tuneGrid = data.frame(size = 3, decay = decay), maxit = iters, preProcess = c("center","scale"), trControl = ctrl_tune, metric = "RMSE")# Try HessianMLPNeuralSens::HessianMLP(caretmod)## Train h2o NNET --------------------------------------------------------------# Create a cluster with 4 available coresh2o::h2o.init(ip = "localhost", nthreads = 4)# Reset the clusterh2o::h2o.removeAll()fdata_h2o <- h2o::as.h2o(x = fdata.Reg.tr, destination_frame = "fdata_h2o")set.seed(150)h2omod <-h2o:: h2o.deeplearning(x = names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], y = names(fdata.Reg.tr)[1], distribution = "AUTO", training_frame = fdata_h2o, standardize = TRUE, activation = "Tanh", hidden = c(hidden_neurons), stopping_rounds = 0, epochs = iters, seed = 150, model_id = "nnet_h2o", adaptive_rate = FALSE, rate_decay = decay, export_weights_and_biases = TRUE)# Try HessianMLPNeuralSens::HessianMLP(h2omod)# Turn off the clusterh2o::h2o.shutdown(prompt = FALSE)rm(fdata_h2o)## Train RSNNS NNET ------------------------------------------------------------# Normalize data using RSNNS algorithmstrData <- as.data.frame(RSNNS::normalizeData(fdata.Reg.tr))names(trData) <- names(fdata.Reg.tr)set.seed(150)RSNNSmod <-RSNNS::mlp(x = trData[,2:ncol(trData)], y = trData[,1], size = hidden_neurons, linOut = TRUE, learnFuncParams=c(decay), maxit=iters)# Try HessianMLPNeuralSens::HessianMLP(RSNNSmod, trData = trData, output_name = "DEM")## USE DEFAULT METHOD ----------------------------------------------------------NeuralSens::HessianMLP(caretmod$finalModel$wts, trData = fdata.Reg.tr, mlpstr = caretmod$finalModel$n, coefnames = caretmod$coefnames, actfun = c("linear","sigmoid","linear"), output_name = "DEM")######################################################################################################### CLASSIFICATION NNET ################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.cl <- fdata[,2:ncol(fdata)]fdata.Reg.cl[,2:3] <- fdata.Reg.cl[,2:3]/10fdata.Reg.cl[,1] <- fdata.Reg.cl[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.cl, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.cl)# Factorize the outputfdata.Reg.cl$DEM <- factor(round(fdata.Reg.cl$DEM, digits = 1))# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.cl, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.cl)## Train caret NNET ------------------------------------------------------------# Create trainControlctrl_tune <- caret::trainControl(method = "boot", savePredictions = FALSE, summaryFunction = caret::defaultSummary)set.seed(150) #For replicationcaretmod <- caret::train(form = DEM~., data = fdata.Reg.cl, method = "nnet", linout = FALSE, tuneGrid = data.frame(size = hidden_neurons, decay = decay), maxit = iters, preProcess = c("center","scale"), trControl = ctrl_tune, metric = "Accuracy")# Try HessianMLPNeuralSens::HessianMLP(caretmod)## Train h2o NNET --------------------------------------------------------------# Create local cluster with 4 available coresh2o::h2o.init(ip = "localhost", nthreads = 4)# Reset the clusterh2o::h2o.removeAll()fdata_h2o <- h2o::as.h2o(x = fdata.Reg.cl, destination_frame = "fdata_h2o")set.seed(150)h2omod <- h2o::h2o.deeplearning(x = names(fdata.Reg.cl)[2:ncol(fdata.Reg.cl)], y = names(fdata.Reg.cl)[1], distribution = "AUTO", training_frame = fdata_h2o, standardize = TRUE, activation = "Tanh", hidden = c(hidden_neurons), stopping_rounds = 0, epochs = iters, seed = 150, model_id = "nnet_h2o", adaptive_rate = FALSE, rate_decay = decay, export_weights_and_biases = TRUE)# Try HessianMLPNeuralSens::HessianMLP(h2omod)# Apaga el clusterh2o::h2o.shutdown(prompt = FALSE)rm(fdata_h2o)## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try HessianMLPNeuralSens::HessianMLP(nnetmod, trData = nntrData)Neural network structure sensitivity plot
Description
Plot a neural interpretation diagram colored by sensitivitiesof the model
Usage
PlotSensMLP( MLP.fit, metric = "mean", sens_neg_col = "red", sens_pos_col = "blue", ...)Arguments
MLP.fit | fitted neural network model |
metric | metric to plot in the NID. It can be "mean" (default), "median or "sqmean".It can be any metric to combine the raw sensitivities |
sens_neg_col |
|
sens_pos_col |
|
... | additional arguments passed toplotnet and/orSensAnalysisMLP |
Value
A graphics object
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 100decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try SensAnalysisMLPNeuralSens::PlotSensMLP(nnetmod, trData = nntrData)Sensitivity of MLP models
Description
Function for evaluating the sensitivities of the inputsvariables in a mlp model
Usage
SensAnalysisMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, ...)## Default S3 method:SensAnalysisMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, trData, actfunc = NULL, deractfunc = NULL, preProc = NULL, terms = NULL, output_name = NULL, ...)## S3 method for class 'train'SensAnalysisMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, boot.R = NULL, boot.seed = 1, boot.alpha = 0.05, ...)## S3 method for class 'H2OMultinomialModel'SensAnalysisMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, ...)## S3 method for class 'H2ORegressionModel'SensAnalysisMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, ...)## S3 method for class 'list'SensAnalysisMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, trData, actfunc, ...)## S3 method for class 'mlp'SensAnalysisMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, trData, preProc = NULL, terms = NULL, ...)## S3 method for class 'nn'SensAnalysisMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, preProc = NULL, terms = NULL, ...)## S3 method for class 'nnet'SensAnalysisMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, trData, preProc = NULL, terms = NULL, ...)## S3 method for class 'nnetar'SensAnalysisMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, ...)## S3 method for class 'numeric'SensAnalysisMLP( MLP.fit, .returnSens = TRUE, plot = TRUE, .rawSens = FALSE, sens_origin_layer = 1, sens_end_layer = "last", sens_origin_input = TRUE, sens_end_input = FALSE, trData, actfunc = NULL, preProc = NULL, terms = NULL, ...)Arguments
MLP.fit | fitted neural network model |
.returnSens | DEPRECATED |
plot |
|
.rawSens | DEPRECATED |
sens_origin_layer |
|
sens_end_layer |
|
sens_origin_input |
|
sens_end_input |
|
... | additional arguments passed to or from other methods |
trData |
|
actfunc |
|
deractfunc |
|
preProc | preProcess structure applied to the training data. See also |
terms | function applied to the training data to create factors. Seealso |
output_name |
|
boot.R |
|
boot.seed |
|
boot.alpha |
|
Details
In case of using an input of classfactor and a package whichneed to enter the input data as matrix, the dummies must be created beforetraining the neural network.
After that, the training data must be given to the function using thetrData argument.
Value
SensMLP object with the sensitivity metrics and sensitivities ofthe MLP model passed to the function.
Plots
Plot 1: colorful plot with the classificationof the classes in a 2D map
Plot 2: b/w plot with probability of thechosen class in a 2D map
Plot 3: plot with the stats::predictions ofthe data provided
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 100decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try SensAnalysisMLPNeuralSens::SensAnalysisMLP(nnetmod, trData = nntrData)# Try SensAnalysisMLP to calculate sensitivities with respect to output of hidden neuronesNeuralSens::SensAnalysisMLP(nnetmod, trData = nntrData, sens_origin_layer = 2, sens_end_layer = "last", sens_origin_input = FALSE, sens_end_input = FALSE)## Train caret NNET ------------------------------------------------------------# Create trainControlctrl_tune <- caret::trainControl(method = "boot", savePredictions = FALSE, summaryFunction = caret::defaultSummary)set.seed(150) #For replicationcaretmod <- caret::train(form = DEM~., data = fdata.Reg.tr, method = "nnet", linout = TRUE, tuneGrid = data.frame(size = 3, decay = decay), maxit = iters, preProcess = c("center","scale"), trControl = ctrl_tune, metric = "RMSE")# Try SensAnalysisMLPNeuralSens::SensAnalysisMLP(caretmod)## Train h2o NNET --------------------------------------------------------------# Create a cluster with 4 available coresh2o::h2o.init(ip = "localhost", nthreads = 4)# Reset the clusterh2o::h2o.removeAll()fdata_h2o <- h2o::as.h2o(x = fdata.Reg.tr, destination_frame = "fdata_h2o")set.seed(150)h2omod <-h2o:: h2o.deeplearning(x = names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], y = names(fdata.Reg.tr)[1], distribution = "AUTO", training_frame = fdata_h2o, standardize = TRUE, activation = "Tanh", hidden = c(hidden_neurons), stopping_rounds = 0, epochs = iters, seed = 150, model_id = "nnet_h2o", adaptive_rate = FALSE, rate_decay = decay, export_weights_and_biases = TRUE)# Try SensAnalysisMLPNeuralSens::SensAnalysisMLP(h2omod)# Turn off the clusterh2o::h2o.shutdown(prompt = FALSE)rm(fdata_h2o)## Train RSNNS NNET ------------------------------------------------------------# Normalize data using RSNNS algorithmstrData <- as.data.frame(RSNNS::normalizeData(fdata.Reg.tr))names(trData) <- names(fdata.Reg.tr)set.seed(150)RSNNSmod <-RSNNS::mlp(x = trData[,2:ncol(trData)], y = trData[,1], size = hidden_neurons, linOut = TRUE, learnFuncParams=c(decay), maxit=iters)# Try SensAnalysisMLPNeuralSens::SensAnalysisMLP(RSNNSmod, trData = trData, output_name = "DEM")## USE DEFAULT METHOD ----------------------------------------------------------NeuralSens::SensAnalysisMLP(caretmod$finalModel$wts, trData = fdata.Reg.tr, mlpstr = caretmod$finalModel$n, coefnames = caretmod$coefnames, actfun = c("linear","sigmoid","linear"), output_name = "DEM")######################################################################################################### CLASSIFICATION NNET ################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.cl <- fdata[,2:ncol(fdata)]fdata.Reg.cl[,2:3] <- fdata.Reg.cl[,2:3]/10fdata.Reg.cl[,1] <- fdata.Reg.cl[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.cl, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.cl)# Factorize the outputfdata.Reg.cl$DEM <- factor(round(fdata.Reg.cl$DEM, digits = 1))# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.cl, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.cl)## Train caret NNET ------------------------------------------------------------# Create trainControlctrl_tune <- caret::trainControl(method = "boot", savePredictions = FALSE, summaryFunction = caret::defaultSummary)set.seed(150) #For replicationcaretmod <- caret::train(form = DEM~., data = fdata.Reg.cl, method = "nnet", linout = FALSE, tuneGrid = data.frame(size = hidden_neurons, decay = decay), maxit = iters, preProcess = c("center","scale"), trControl = ctrl_tune, metric = "Accuracy")# Try SensAnalysisMLPNeuralSens::SensAnalysisMLP(caretmod)## Train h2o NNET --------------------------------------------------------------# Create local cluster with 4 available coresh2o::h2o.init(ip = "localhost", nthreads = 4)# Reset the clusterh2o::h2o.removeAll()fdata_h2o <- h2o::as.h2o(x = fdata.Reg.cl, destination_frame = "fdata_h2o")set.seed(150)h2omod <- h2o::h2o.deeplearning(x = names(fdata.Reg.cl)[2:ncol(fdata.Reg.cl)], y = names(fdata.Reg.cl)[1], distribution = "AUTO", training_frame = fdata_h2o, standardize = TRUE, activation = "Tanh", hidden = c(hidden_neurons), stopping_rounds = 0, epochs = iters, seed = 150, model_id = "nnet_h2o", adaptive_rate = FALSE, rate_decay = decay, export_weights_and_biases = TRUE)# Try SensAnalysisMLPNeuralSens::SensAnalysisMLP(h2omod)# Apaga el clusterh2o::h2o.shutdown(prompt = FALSE)rm(fdata_h2o)## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try SensAnalysisMLPNeuralSens::SensAnalysisMLP(nnetmod, trData = nntrData)Sensitivity scatter plot against input values
Description
Plot of sensitivities of the neural network output respectto the inputs
Usage
SensDotPlot( object, fdata = NULL, input_vars = "all", output_vars = "all", smooth = FALSE, nspline = NULL, color = NULL, grid = FALSE, ...)Arguments
object | fitted neural network model or |
fdata |
|
input_vars |
|
output_vars |
|
smooth |
|
nspline |
|
color |
|
grid |
|
... | further arguments that should be passed to |
Value
list ofgeom_point plots for the inputs variables representing thesensitivity of each output respect to the inputs
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try SensDotPlotNeuralSens::SensDotPlot(nnetmod, fdata = nntrData)Feature sensitivity plot
Description
Show the distribution of the sensitivities of the outputingeom_sina() plot which color depends on the input values
Usage
SensFeaturePlot(object, fdata = NULL, ...)Arguments
object | fitted neural network model or |
fdata |
|
... | further arguments that should be passed to |
Value
list of Feature sensitivity plot as described inhttps://www.r-bloggers.com/2019/03/a-gentle-introduction-to-shap-values-in-r/
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try SensAnalysisMLPsens <- NeuralSens::SensAnalysisMLP(nnetmod, trData = nntrData, plot = FALSE)NeuralSens::SensFeaturePlot(sens)Constructor of the SensMLP Class
Description
Create an object of SensMLP class
Usage
SensMLP( sens = list(), raw_sens = list(), mlp_struct = numeric(), trData = data.frame(), coefnames = character(), output_name = character(), cv = NULL, boot = NULL, boot.alpha = NULL)Arguments
sens |
|
raw_sens |
|
mlp_struct |
|
trData |
|
coefnames |
|
output_name |
|
cv |
|
boot |
|
boot.alpha |
|
Value
SensMLP object
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
Plot sensitivities of a neural network model
Description
Function to plot the sensitivities created byHessianMLP.
Usage
SensMatPlot( hess, sens = NULL, output = 1, metric = c("mean", "std", "meanSensSQ"), senstype = c("matrix", "interactions"), ...)Arguments
hess |
|
sens |
|
output |
|
metric |
|
senstype |
|
... | further argument passed similar to |
Details
Most of the code of this function is based onggcorrplot() function from packageggcorrplot. However, due to theinhability of changing the limits of the color scale, it keeps giving a warningif that function is used and the color scale overwritten.
Value
a list ofggplots, one for each output neuron.
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 100decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try HessianMLPH <- NeuralSens::HessianMLP(nnetmod, trData = nntrData, plot = FALSE)NeuralSens::SensMatPlot(H)S <- NeuralSens::SensAnalysisMLP(nnetmod, trData = nntrData, plot = FALSE)NeuralSens::SensMatPlot(H, S, senstype = "interactions")Sensitivity analysis plot over time of the data
Description
Plot of sensitivity of the neural network output respectto the inputs over the time variable from the data provided
Usage
SensTimePlot( object, fdata = NULL, date.var = NULL, facet = FALSE, smooth = FALSE, nspline = NULL, ...)Arguments
object | fitted neural network model or |
fdata |
|
date.var |
|
facet |
|
smooth |
|
nspline |
|
... | further arguments that should be passed to |
Value
list ofgeom_line plots for the inputs variables representing thesensitivity of each output respect to the inputs over time
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TRfdata[,3] <- ifelse(as.data.frame(fdata)[,3] %in% c("SUN","SAT"), 0, 1)## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try SensTimePlotNeuralSens::SensTimePlot(nnetmod, fdata = nntrData, date.var = NULL)Plot sensitivities of a neural network model
Description
Function to plot the sensitivities created bySensAnalysisMLP.
Usage
SensitivityPlots( sens = NULL, der = TRUE, zoom = TRUE, quit.legend = FALSE, output = 1, plot_type = NULL, inp_var = NULL, title = "Sensitivity Plots", dodge_var = FALSE)Arguments
sens |
|
der |
|
zoom |
|
quit.legend |
|
output |
|
plot_type |
|
inp_var |
|
title |
|
dodge_var |
|
Value
List with the following plot for each output:
Plot 1: colorful plot with theclassification of the classes in a 2D map
Plot 2: b/w plot withprobability of the chosen class in a 2D map
Plot 3: plot with thestats::predictions of the data provided if param
derisFALSE
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try SensAnalysisMLPsens <- NeuralSens::SensAnalysisMLP(nnetmod, trData = nntrData, plot = FALSE)NeuralSens::SensitivityPlots(sens)Define function to create a 'diagonal' array or get the diagonal of an array
Description
Define function to create a 'diagonal' array or get the diagonal of an array
Usage
diag3Darray(x = 1, dim = length(x), out = "vector")Arguments
x |
|
dim |
|
out |
|
Details
The diagonal of a 3D array has been defined as those elements in positions c(int,int,int),i.e., the three digits are the same.
If the diagonal should be returned,out specifies if it should return a"vector" withthe elements of position c(int,int,int), or"matrix" with the elements of position c(int,dim,int),i.e.,dim = 2 -> elements (1,1,1),(2,1,2),(3,1,3),(1,2,1),(2,2,2),(3,2,3),(3,1,3),(3,2,3),(3,3,3).
Value
array with all elements zero except the diagonal, with dimensions c(dim,dim,dim)
Examples
x <- diag3Darray(c(1,4,6), dim = 3)x# , , 1## [,1] [,2] [,3]# [1,] 1 0 0# [2,] 0 0 0# [3,] 0 0 0## , , 2## [,1] [,2] [,3]# [1,] 0 0 0# [2,] 0 4 0# [3,] 0 0 0## , , 3## [,1] [,2] [,3]# [1,] 0 0 0# [2,] 0 0 0# [3,] 0 0 6diag3Darray(x)# 1, 4, 6Define function to change the diagonal of array
Description
Define function to change the diagonal of array
Usage
diag3Darray(x) <- valueArguments
x |
|
value |
|
Details
The diagonal of a 3D array has been defined as those elements in positions c(int,int,int),i.e., the three digits are the same.
Value
array with all elements zero except the diagonal, with dimensions c(dim,dim,dim)
Examples
x <- array(1, dim = c(3,3,3))diag3Darray(x) <- c(2,2,2)x# , , 1## [,1] [,2] [,3]# [1,] 2 1 1# [2,] 1 1 1# [3,] 1 1 1## , , 2## [,1] [,2] [,3]# [1,] 1 1 1# [2,] 1 2 1# [3,] 1 1 1## , , 3## [,1] [,2] [,3]# [1,] 1 1 1# [2,] 1 1 1# [3,] 1 1 2Define function to create a 'diagonal' array or get the diagonal of an array
Description
Define function to create a 'diagonal' array or get the diagonal of an array
Usage
diag4Darray(x = 1, dim = length(x))Arguments
x |
|
dim |
|
Details
The diagonal of a 4D array has been defined as those elements in positions c(int,int,int,int),i.e., the four digits are the same.
Value
array with all elements zero except the diagonal, with dimensions c(dim,dim,dim)
Examples
x <- diag4Darray(c(1,3,6,2), dim = 4)x# , , 1, 1## [,1] [,2] [,3] [,4]# [1,] 1 0 0 0# [2,] 0 0 0 0# [3,] 0 0 0 0# [4,] 0 0 0 0## , , 2, 1## [,1] [,2] [,3] [,4]# [1,] 0 0 0 0# [2,] 0 0 0 0# [3,] 0 0 0 0# [4,] 0 0 0 0## , , 3, 1## [,1] [,2] [,3] [,4]# [1,] 0 0 0 0# [2,] 0 0 0 0# [3,] 0 0 0 0# [4,] 0 0 0 0## , , 4, 1## [,1] [,2] [,3] [,4]# [1,] 0 0 0 0# [2,] 0 0 0 0# [3,] 0 0 0 0# [4,] 0 0 0 0## , , 1, 2## [,1] [,2] [,3] [,4]# [1,] 0 0 0 0# [2,] 0 0 0 0# [3,] 0 0 0 0# [4,] 0 0 0 0## , , 2, 2## [,1] [,2] [,3] [,4]# [1,] 0 0 0 0# [2,] 0 3 0 0# [3,] 0 0 0 0# [4,] 0 0 0 0## , , 3, 2## [,1] [,2] [,3] [,4]# [1,] 0 0 0 0# [2,] 0 0 0 0# [3,] 0 0 0 0# [4,] 0 0 0 0## , , 4, 2## [,1] [,2] [,3] [,4]# [1,] 0 0 0 0# [2,] 0 0 0 0# [3,] 0 0 0 0# [4,] 0 0 0 0## , , 1, 3## [,1] [,2] [,3] [,4]# [1,] 0 0 0 0# [2,] 0 0 0 0# [3,] 0 0 0 0# [4,] 0 0 0 0## , , 2, 3## [,1] [,2] [,3] [,4]# [1,] 0 0 0 0# [2,] 0 0 0 0# [3,] 0 0 0 0# [4,] 0 0 0 0## , , 3, 3## [,1] [,2] [,3] [,4]# [1,] 0 0 0 0# [2,] 0 0 0 0# [3,] 0 0 6 0# [4,] 0 0 0 0## , , 4, 3## [,1] [,2] [,3] [,4]# [1,] 0 0 0 0# [2,] 0 0 0 0# [3,] 0 0 0 0# [4,] 0 0 0 0## , , 1, 4## [,1] [,2] [,3] [,4]# [1,] 0 0 0 0# [2,] 0 0 0 0# [3,] 0 0 0 0# [4,] 0 0 0 0## , , 2, 4## [,1] [,2] [,3] [,4]# [1,] 0 0 0 0# [2,] 0 0 0 0# [3,] 0 0 0 0# [4,] 0 0 0 0## , , 3, 4## [,1] [,2] [,3] [,4]# [1,] 0 0 0 0# [2,] 0 0 0 0# [3,] 0 0 0 0# [4,] 0 0 0 0## , , 4, 4## [,1] [,2] [,3] [,4]# [1,] 0 0 0 0# [2,] 0 0 0 0# [3,] 0 0 0 0# [4,] 0 0 0 2diag4Darray(x)# 1, 3, 6, 2Define function to change the diagonal of array
Description
Define function to change the diagonal of array
Usage
diag4Darray(x) <- valueArguments
x |
|
value |
|
Details
The diagonal of a 3D array has been defined as those elements in positions c(int,int,int),i.e., the three digits are the same.
Value
array with all elements zero except the diagonal, with dimensions c(dim,dim,dim)
Examples
x <- array(1, dim = c(4,4,4,4))diag4Darray(x) <- c(2,2,2,2)x# , , 1, 1## [,1] [,2] [,3] [,4]# [1,] 2 1 1 1# [2,] 1 1 1 1# [3,] 1 1 1 1# [4,] 1 1 1 1## , , 2, 1## [,1] [,2] [,3] [,4]# [1,] 1 1 1 1# [2,] 1 1 1 1# [3,] 1 1 1 1# [4,] 1 1 1 1## , , 3, 1## [,1] [,2] [,3] [,4]# [1,] 1 1 1 1# [2,] 1 1 1 1# [3,] 1 1 1 1# [4,] 1 1 1 1## , , 4, 1## [,1] [,2] [,3] [,4]# [1,] 1 1 1 1# [2,] 1 1 1 1# [3,] 1 1 1 1# [4,] 1 1 1 1## , , 1, 2## [,1] [,2] [,3] [,4]# [1,] 1 1 1 1# [2,] 1 1 1 1# [3,] 1 1 1 1# [4,] 1 1 1 1## , , 2, 2## [,1] [,2] [,3] [,4]# [1,] 1 1 1 1# [2,] 1 2 1 1# [3,] 1 1 1 1# [4,] 1 1 1 1## , , 3, 2## [,1] [,2] [,3] [,4]# [1,] 1 1 1 1# [2,] 1 1 1 1# [3,] 1 1 1 1# [4,] 1 1 1 1## , , 4, 2## [,1] [,2] [,3] [,4]# [1,] 1 1 1 1# [2,] 1 1 1 1# [3,] 1 1 1 1# [4,] 1 1 1 1## , , 1, 3## [,1] [,2] [,3] [,4]# [1,] 1 1 1 1# [2,] 1 1 1 1# [3,] 1 1 1 1# [4,] 1 1 1 1## , , 2, 3## [,1] [,2] [,3] [,4]# [1,] 1 1 1 1# [2,] 1 1 1 1# [3,] 1 1 1 1# [4,] 1 1 1 1## , , 3, 3## [,1] [,2] [,3] [,4]# [1,] 1 1 1 1# [2,] 1 1 1 1# [3,] 1 1 2 1# [4,] 1 1 1 1## , , 4, 3## [,1] [,2] [,3] [,4]# [1,] 1 1 1 1# [2,] 1 1 1 1# [3,] 1 1 1 1# [4,] 1 1 1 1## , , 1, 4## [,1] [,2] [,3] [,4]# [1,] 1 1 1 1# [2,] 1 1 1 1# [3,] 1 1 1 1# [4,] 1 1 1 1## , , 2, 4## [,1] [,2] [,3] [,4]# [1,] 1 1 1 1# [2,] 1 1 1 1# [3,] 1 1 1 1# [4,] 1 1 1 1## , , 3, 4## [,1] [,2] [,3] [,4]# [1,] 1 1 1 1# [2,] 1 1 1 1# [3,] 1 1 1 1# [4,] 1 1 1 1## , , 4, 4## [,1] [,2] [,3] [,4]# [1,] 1 1 1 1# [2,] 1 1 1 1# [3,] 1 1 1 1# [4,] 1 1 1 2Find Critical Value
Description
This function finds the smallest x such that the probability of a random variablebeing less than or equal to x is greater than or equal to 1 - alpha.It uses the uniroot function to find where the empirical cumulative distribution function (ECDF)crosses 1 - alpha.
Usage
find_critical_value(ecdf_func, alpha)Arguments
ecdf_func | An ECDF function representing the distribution of a random variable. |
alpha | A numeric value specifying the significance level. |
Value
The smallest x such that P(X <= x) >= 1 - alpha.
Examples
data <- rnorm(100)ecdf_data <- ecdf(data)critical_val <- find_critical_value(ecdf_data, 0.05)Check if object is of classHessMLP
Description
Check if object is of classHessMLP
Usage
is.HessMLP(object)Arguments
object |
|
Value
TRUE ifobject is aHessMLP object
Check if object is of classSensMLP
Description
Check if object is of classSensMLP
Usage
is.SensMLP(object)Arguments
object |
|
Value
TRUE ifobject is aSensMLP object
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
k-StepM Algorithm for Hypothesis Testing
Description
This function implements the k-stepM algorithm for multiple hypothesis testing.It tests each hypothesis using the critical value calculated from the ECDFof the k-max differences, updating the critical value, and iterating until all hypothesesare tested.
Usage
kStepMAlgorithm(original_stats, bootstrap_stats, num_hypotheses, alpha, k)Arguments
original_stats | A numeric vector of original test statistics for each hypothesis. |
bootstrap_stats | A numeric matrix of bootstrap test statistics, with rows representingbootstrap samples and columns representing hypotheses. |
num_hypotheses | An integer specifying the total number of hypotheses. |
alpha | A numeric value specifying the significance level. |
k | An integer specifying the threshold number for controlling the k-familywise error rate. |
Value
A list containing two elements: 'signif', a logical vector indicating which hypothesesare rejected, and 'cv', a numeric vector of critical values used for each hypothesis.
References
Romano, Joseph P., Azeem M. Shaikh, and Michael Wolf. "Formalized data snoopingbased on generalized error rates." Econometric Theory 24.2 (2008): 404-447.
Examples
original_stats <- rnorm(10)bootstrap_stats <- matrix(rnorm(1000), ncol = 10)result <- kStepMAlgorithm(original_stats, bootstrap_stats, 10, 0.05, 1)Plot method for the HessMLP Class
Description
Plot the sensitivities and sensitivity metrics of aHessMLP object.
Usage
## S3 method for class 'HessMLP'plot( x, plotType = c("sensitivities", "time", "features", "matrix", "interactions"), ...)Arguments
x |
|
plotType |
|
... | additional parameters passed to plot function of the |
Value
list of graphic objects created byggplot
Examples
#' ## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try HessianMLPsens <- NeuralSens::HessianMLP(nnetmod, trData = nntrData, plot = FALSE)plot(sens)plot(sens,"time")Plot method for the SensMLP Class
Description
Plot the sensitivities and sensitivity metrics of aSensMLP object.
Usage
## S3 method for class 'SensMLP'plot(x, plotType = c("sensitivities", "time", "features"), ...)Arguments
x |
|
plotType |
|
... | additional parameters passed to plot function of the |
Value
list of graphic objects created byggplot
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
Examples
#' ## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try SensAnalysisMLPsens <- NeuralSens::SensAnalysisMLP(nnetmod, trData = nntrData, plot = FALSE)plot(sens)plot(sens,"time")plot(sens,"features")Print method for the HessMLP Class
Description
Print the sensitivities of aHessMLP object.
Usage
## S3 method for class 'HessMLP'print(x, n = 5, round_digits = NULL, ...)Arguments
x |
|
n |
|
round_digits |
|
... | additional parameters |
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try HessianMLPsens <- NeuralSens::HessianMLP(nnetmod, trData = nntrData, plot = FALSE)sensPrint method for the SensMLP Class
Description
Print the sensitivities of aSensMLP object.
Usage
## S3 method for class 'SensMLP'print(x, n = 5, round_digits = NULL, ...)Arguments
x |
|
n |
|
round_digits |
|
... | additional parameters |
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try SensAnalysisMLPsens <- NeuralSens::SensAnalysisMLP(nnetmod, trData = nntrData, plot = FALSE)sensPrint method of the summary HessMLP Class
Description
Print the sensitivity metrics of aHessMLP object.This metrics are the mean sensitivity, the standard deviationof sensitivities and the mean of sensitivities square
Usage
## S3 method for class 'summary.HessMLP'print(x, round_digits = NULL, ...)Arguments
x |
|
round_digits |
|
... | additional parameters |
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try HessianMLPsens <- NeuralSens::HessianMLP(nnetmod, trData = nntrData, plot = FALSE)print(summary(sens))Print method of the summary SensMLP Class
Description
Print the sensitivity metrics of aSensMLP object.This metrics are the mean sensitivity, the standard deviationof sensitivities and the mean of sensitivities square
Usage
## S3 method for class 'summary.SensMLP'print(x, round_digits = NULL, boot.alpha = NULL, ...)Arguments
x |
|
round_digits |
|
boot.alpha |
|
... | additional parameters |
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try SensAnalysisMLPsens <- NeuralSens::SensAnalysisMLP(nnetmod, trData = nntrData, plot = FALSE)print(summary(sens))Simulated data to test the package functionalities
Description
data.frame with 2000 rows of 4 columns with 3input variablesX1, X2, X3 and one output variableY.The data is already scaled, and has been generated using the following code:
set.seed(150)
simdata <- data.frame( "X1" = rnorm(2000, 0, 1), "X2" = rnorm(2000, 0, 1), "X3" = rnorm(2000, 0, 1) )
simdata$Y <- simdata$X1^2 + 0.5*simdata$X2 + 0.1*rnorm(2000, 0, 1)
Format
A data frame with 2000 rows and 4 variables:
- X1
Random input 1
- X2
Random input 2
- X3
Random input 3
- Y
Output
Author(s)
Jaime Pizarroso Gonzalo
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
Summary Method for the HessMLP Class
Description
Print the sensitivity metrics of aHessMLP object.This metrics are the mean sensitivity, the standard deviationof sensitivities and the mean of sensitivities square
Usage
## S3 method for class 'HessMLP'summary(object, ...)Arguments
object |
|
... | additional parameters |
Value
summary object of theHessMLP object passed
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try HessianMLPsens <- NeuralSens::HessianMLP(nnetmod, trData = nntrData, plot = FALSE)summary(sens)Summary Method for the SensMLP Class
Description
Print the sensitivity metrics of aSensMLP object.This metrics are the mean sensitivity, the standard deviationof sensitivities and the mean of sensitivities square
Usage
## S3 method for class 'SensMLP'summary(object, ...)Arguments
object |
|
... | additional parameters |
Value
summary object of theSensMLP object passed
References
Pizarroso J, Portela J, Muñoz A (2022). NeuralSens: Sensitivity Analysis ofNeural Networks. Journal of Statistical Software, 102(7), 1-36.
Examples
## Load data -------------------------------------------------------------------data("DAILY_DEMAND_TR")fdata <- DAILY_DEMAND_TR## Parameters of the NNET ------------------------------------------------------hidden_neurons <- 5iters <- 250decay <- 0.1######################################################################################################### REGRESSION NNET ####################################################################################################################### Regression dataframe --------------------------------------------------------# Scale the datafdata.Reg.tr <- fdata[,2:ncol(fdata)]fdata.Reg.tr[,3] <- fdata.Reg.tr[,3]/10fdata.Reg.tr[,1] <- fdata.Reg.tr[,1]/1000# Normalize the data for some modelspreProc <- caret::preProcess(fdata.Reg.tr, method = c("center","scale"))nntrData <- predict(preProc, fdata.Reg.tr)#' ## TRAIN nnet NNET --------------------------------------------------------# Create a formula to train NNETform <- paste(names(fdata.Reg.tr)[2:ncol(fdata.Reg.tr)], collapse = " + ")form <- formula(paste(names(fdata.Reg.tr)[1], form, sep = " ~ "))set.seed(150)nnetmod <- nnet::nnet(form, data = nntrData, linear.output = TRUE, size = hidden_neurons, decay = decay, maxit = iters)# Try SensAnalysisMLPsens <- NeuralSens::SensAnalysisMLP(nnetmod, trData = nntrData, plot = FALSE)summary(sens)