0

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.

leppie
  • 115,091
  • 17
  • 196
  • 297
Nim J
  • 993
  • 2
  • 9
  • 15
  • Thanks all for looking in. I got it rectified after researching a bit on caret package. I need to train the model and then use the varimp(). – Nim J Jun 10 '15 at 09:41

1 Answers1

2

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

Nim J
  • 993
  • 2
  • 9
  • 15