Closed amaraabara closed 9 years ago
Hi Amara,
You're using a model averaging method from the train function that averages multiple nnet models to create the final output. As far as I can tell, the output doesn't include enough information to use the plotnet, garson, or olden functions from my package, nor would it be appropriate since it's a combined model. I would suggest recreating the individual models that were used to make the averaged models, then look at each model separately. I would imagine they are not very different. The output from the train function does include these models but unfortunately some of the variables needed to use the NeuralNetTools functions are tied up in a temporary environment that I couldn't access. Try this code instead, it isolates individual models from the output, recreates them, then uses the NeuralNetTools functions. Hope that helps...
library(NeuralNetTools)
library(caret)
mod <- train(Y1 ~ X1 + X2 + X3, method = 'avNNet', data = neuraldat,
linout = TRUE)
allmods <- mod$finalModel$model
mod <- allmods[[1]] # first model, change this number to look at the other models
wts <- mod$wts
decay <- mod$decay
struct <- mod$n
# recreate
recmod <- nnet(Y1 ~ X1 + X2 + X3, data = neuraldat, Wts = wts, decay = decay,
size = struct[2], maxit = 0)
# use the functions to look at the individual model
plotnet(recmod)
garson(recmod)
olden(recmod, 'Y1')
lekprofile(recmod)
Hey Marcus,
Thanks for your response - the code you posted worked fine. Although, I seem to have a problem with lekprofile(). The plot is pretty much blank apart from one variable (see plot below). Do you think this might have something to do with the data or do I need to include additional arguments in the function?
Also, I noticed that the lek.fun() function allows you to select specific variables to plot using var.sens. Does lekprofile() allow you to do this? Based on the manual, it doesn't seem so but just wanted to check.
Hi Amara,
Could you please provide a reproducible example that illustrates the problem? This is hard to solve if I can't reproduce the problem on my end.
Also, you are right about the difference between lekprofile vs. lek.fun. I don't remember why I removed that feature... However, you can return the actual values used in the plot by setting the val_out argument to TRUE. These are in long format that you can plot with ggplot. Something like this:
library(NeuralNetTools)
library(nnet)
library(dplyr)
library(ggplot2)
set.seed(123)
mod <- nnet(Y1 ~ X1 + X2 + X3, data = neuraldat, size = 5)
vals <- lekprofile(mod, val_out = TRUE)
toplo <- filter(vals, exp_name %in% c('X1', 'X3'))
ggplot(toplo, aes(x = Explanatory, y = Response, group = Splits, colour = Splits)) +
geom_line() +
facet_grid(resp_name ~ exp_name)
This is tedious though. You could open another issue with this request if you want this feature back in the function.
Best,
Marcus
Hi Marcus,
Apologies for the delay in sending this across to you. Please find below a sample of the code I used to produce the charts above. Here's a link to the dataset I used as well: https://www.dropbox.com/s/l6it7mq5bo0aa6l/FGD_Data_FY_TAvg.csv?dl=0. Any assistance in figuring out what the issue might be would be much appreciated.
I think it would be really cool to have the feature in lek.fun which allows you to plot sensitivity analysis for selected variables in lekprofile. I'll go ahead and raise a new issue as suggested, hopefully it isn't too much of a hassle to have it included.
Thanks, Amara.
fgdDataTAvg <- read.csv("FGD_Data_FY_TAvg.csv", stringsAsFactors=FALSE)
fgdDataTAvg1 <- na.omit(fgdDataTAvg)
library(lubridate)
# Converting the DateTime variable into POSIXct format
fgdDataTAvg1$DateTime <- dmy_hms(fgdDataTAvg1$DateTime, truncated = 3)
# Extracting training set from data
fgdDataTAvg2Train <- filter(fgdDataTAvg2, DateTime > "2014-03-01 00:00:00" & DateTime
< "2014-07-01 00:00:00")
# Removing AdipicAcid, SAG, SO2AfterFGD and DateTime from the training set
fgdDataTAvg2TrainY <- fgdDataTAvg2Train$SO2AfterFGD
fgdDataTAvg2TrainX <- fgdDataTAvg2Train[,-17]
fgdDataTAvg2TrainX <- fgdDataTAvg2TrainX[,-16]
fgdDataTAvg2TrainX <- fgdDataTAvg2TrainX[,-8]
fgdDataTAvg2TrainX <- fgdDataTAvg2TrainX[,-1]
# Checking for in-between predictor correlations
library(caret)
corThresh <- 0.75
tooHigh <- findCorrelation(cor(fgdDataTAvg2TrainX), corThresh)
names(fgdDataTAvg2TrainX)[tooHigh]
drops <- c("Suspension","SO2BeforeFGD","FlueGasFlowBeforeFGD","Carbon","Silicon","Aluminium")
fgdDataTAvg2TrainXD <- fgdDataTAvg2TrainX[, !(names(fgdDataTAvg2TrainX) %in% drops)]
nnetGrid <- expand.grid(.decay = c(0.01),
.bag = FALSE,
.size = c(2))
set.seed(2)
library(nnet)
system.time(NNmod <- train(fgdDataTAvg2TrainXD, fgdDataTAvg2TrainY,
method = "avNNet",
tuneGrid = nnetGrid,
preProc = c("center", "scale"),
maxit = 500,
linout = TRUE,
trace = FALSE))
# Model extraction to enable neural plots
mod <- NNmod
allmods <- mod$finalModel$model
mod <- allmods[[1]] # first model, change this number to look at the other models
wts <- mod$wts
decay <- mod$decay
struct <- mod$n
# recreate
Y <- as.data.frame(fgdDataTAvg2TrainY)
recmod <- nnet(fgdDataTAvg2TrainXD, Y, Wts = wts, decay = decay, size = struct[2], maxit = 0)
names(fgdDataTAvg2TrainXD)
# use the functions to look at the individual model
library(NeuralNetTools)
plotnet(recmod)
garson(recmod)
olden(recmod, 'Y')
lekprofile(recmod)
Hi Amara,
You're missing some steps when you recreate a single model from the total model list. Pay attention to the arguments you used in your initial call to train
. Make sure you use the same ones when you recreate the model with nnet
. Specifically, you forgot to set a linear output and to scale/center the input variables. Try it again with these lines for the model recreation.
X <- scale(fgdDataTAvg2TrainXD)
X <- as.data.frame(X)
recmod <- nnet(X, Y, Wts = wts, decay = decay, size = struct[2],
maxit = 0, linout = T)
Thanks for your help Marcus, it worked!
Hi Marcus,
I tried using NeuralNetTools to interpret a Caret Neural Network model but encountered the error stated in the above subject. I'm new to R so the error could well be my fault, however I thought it might be useful to run this by you.
The same error was received for all functions: plotnet(), garson() and lekprofile().
It seems this error is peculiar to the Caret model as the above 3 function seem to work just fine when fed with a nnet model.
I've also included the model structure below just in case it's useful in identifying the issue. If you require any further information, please let me know.
Btw, awesome job on the package!
Cheers, Amara.
Caret model
Code and error for plotnet (same error encountered for garson and lekprofile)
Caret model structure