Error while running varImp(gbm_model)

1.3k views Asked by At

I'm running gbm model for a classification problem.Below is my code & output

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)

output of print(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.

When I try to print top variables, it throws error.

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

Any suggestion how to rectify this error.

1

There are 1 answers

0
Nim J On

I got the error rectified after researching a bit more on caret package. First I needed to train the model and then use the 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)

then run

plot(varImp(gbm1),top=20) 

to get top 20 variables