2

我有下面的脚本可以很好地与 ctree 模型/派对包一起使用。当我将它与 NNET 模型/包交换时,varImp 和 plot(final model) 会引发错误。我的“假设”是 caret 包中的辅助函数适用于所有支持的模型。

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'
4

2 回答 2

1

今天使用caret::train(method = "nnet"). 的输出caret::varImp()很好,但是(据我所知)因为它还包含 的列overall,所以我无法通过caret::plot(varImp()). 但是,我能够通过以下方式绘制变量重要性:

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")

来自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

迈克尔

于 2016-08-06T21:33:40.980 回答
0

您正在“尝试” plot tree.fit$finalModel,这与它无关,caret因为它是原始模型类。如果这不存在,那么这是一个问题。

对于可变重要性问题似乎是print.varImp.train. 如果将其分配给对象并查看importance零件,则可以看到它。

于 2015-01-26T19:09:39.553 回答