2

I have the script below that works fine with the ctree model/party package. When I swap it with the NNET model/package, the varImp and plot(final model) throws an error. I was under the 'assumption' that the helper functions in the caret package worked with all supported models.

library(caret)
library(nnet)

#read in data
data = iris

#split data into training, test, and final test samples
trainIndex <- createDataPartition(data$Species, p=.80, list=F)
train = data[trainIndex,]
test = data[-trainIndex,]

#train and plot model fit
tree.fit = train(Species~., data=train, method="nnet")
varImp(tree.fit)
plot(tree.fit$finalModel)

#predict test data
tree.pred.test = predict(tree.fit, newdata=test)
confusionMatrix(tree.pred.test, test$Species)


> varImp(tree.fit)
nnet variable importance

  variables are sorted by maximum importance across the classes
Error in data.frame(`NA` = character(0), `NA` = character(0), `NA` = character(0),  : 
  row names supplied are of the wrong length
In addition: Warning message:
In format.data.frame(x, digits = digits, na.encode = FALSE) :
  corrupt data frame: columns will be truncated or padded with NAs
> plot(tree.fit$finalModel)
Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' is a list, but does not have components 'x' and 'y'
ci_
  • 8,594
  • 10
  • 39
  • 63
Steve Olson
  • 183
  • 2
  • 12

2 Answers2

1

Ran into a similar issue today using caret::train(method = "nnet"). Output of caret::varImp() was fine, but (from what I can tell) since it also contains a column for overall, I was not able to plot via caret::plot(varImp()). However, I was able to plot variable importance with:

nn.m1.vi = varImp(nn.m1)
nn.m1.vi$importance = as.data.frame(nn.m1.vi$importance)[, -1]
plot(nn.m1.vi, main = "Var Imp: nn.m1")

From sessionInfo():

R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
caret_6.0-71

Michael

J.M.
  • 257
  • 1
  • 2
  • 13
0

You are 'trying' to plot tree.fit$finalModel, which has nothing to do with caret since it is the original model class. If that doesn't exist, then it is a problem.

For the variable importance issue seems to be a bug with print.varImp.train. If you assign it to a object and look at the importance part, you can see it.

topepo
  • 13,534
  • 3
  • 39
  • 52
  • Your first point makes sense. Again, my assumption was that the train() function is what creates the fit.finalModel and as such would be built compatible with any models it supports. On the second part, I cannot find any elements with varImp in the name. I have expanded the environment on the right in R studio and used the attributes function - not seeing it. Thanks, again – Steve Olson Jan 27 '15 at 17:16