fawda123 / NeuralNetTools

R package of generic neural network tools
https://fawda123.github.io/NeuralNetTools/
Creative Commons Zero v1.0 Universal
71 stars 20 forks source link

Error when using olden() on nnet model #24

Closed hermandr closed 2 years ago

hermandr commented 2 years ago

I have a trained nnet model called nnet and using olden(nnet) it generates an error:

Error in inp_hid[[max_i]] : attempt to select less than one element in integerOneIndex

The link to my Rda file is here

Would appreciate help on how to do a Variable Importance on nnet model.

fawda123 commented 2 years ago

Hi @hermandr, it looks like this is a multinomial log-linear model. The olden method does not work with these types of models from the nnet package and unfortunately I have not indicated this in the documentation. Because there is no "hidden" layer for these models, you could just look at the model weights to infer variable importance.

Here's an example using the model in the nnet package documentation.

library(nnet)
library(MASS)
library(NeuralNetTools)

example("birthwt")
bwt.mu <- multinom(low ~ ., bwt)
plotnet(bwt.mu)

image

neuralweights(bwt.mu)
# $struct
# [1] 11  0  1
# 
# $wts
# $wts$`out 1`
# [1]  0.00000000  0.82320102 -0.03723828 -0.01565359  1.19240391  0.74065606  0.75550487  1.34375901  1.91320116  0.68020207 -0.43638470
# [12]  0.17900392

In your case, the output from neuralweights is split by the different categorical outputs.

More generally, this is just a fancy version of logistic regression. I would look elsewhere to figure out variable importance for multinomial GLMs.