Steve Olson
Steve Olson

Reputation: 323

Caret Package: All Functions Don't Work for All Models

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'

Upvotes: 2

Views: 1172

Answers (2)

J.M.
J.M.

Reputation: 267

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

Upvotes: 1

topepo
topepo

Reputation: 14331

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.

Upvotes: 0

Related Questions