运行varImp时出错(gbm_model)

时间:2015-06-10 06:57:01

标签: r gbm

我正在为分类问题运行gbm模型。我的代码是& amp;输出

library(gbm)
library(caret)
set.seed(123)
train=read.csv("train.csv")
gbm_model= gbm(DV~., 
                data=train,
                distribution = "bernoulli",
                n.trees = 9,
                interaction.depth = 9,
                n.minobsinnode = 1,
                shrinkage = 0.2,
                bag.fraction = 0.9)

打印输出(gbm1)

gbm(formula = DP ~ ., distribution = "bernoulli", 
data = train, n.trees = 9, interaction.depth = 9, n.minobsinnode = 1, 
shrinkage = 0.2, bag.fraction = 0.9)
A gradient boosted model with bernoulli loss function.
9 iterations were performed.
There were 100 predictors of which 67 had non-zero influence.

当我尝试打印顶部变量时,会抛出错误。

varImp(gbm_model)
Error in 1:n.trees : argument of length 0

有任何建议如何纠正此错误。

1 个答案:

答案 0 :(得分:2)

在研究了一些关于插入包的问题后,我纠正了错误。首先,我需要训练模型,然后使用varImp()。

gbm1= train(as.factor(DV)~., data=train,method="gbm",
            distribution ="bernoulli",trControl=trainControl(number=200),
            tuneGrid=expand.grid(.interaction.depth = 9,.n.trees = 9, .shrinkage = .1), n.minobsinnode = 1,
                bag.fraction = 0.9)

然后运行

plot(varImp(gbm1),top=20) 

获得前20个变量