Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R caret and gbm can't find ntrees input

Tags:

r

r-caret

I'm trying to train a gbm using the caret package in R. I initially got the following error and thought it was due to lack of an input, so I created the gbmGrid but am still getting the same error message.

sub4Collect1 <- data.frame(testing$row_id)
> 
> cl <- makeCluster(10, type = "SOCK")
> registerDoSNOW(cl)
> ptm <- proc.time()
> 
> for(i in 2:7){
+ trainClass <- postPrior1[,i]
+ testClass <- postTest1[,i]
+ gbmGrid <- expand.grid(.interaction.depth = (1:5) * 2, .n.trees = (1:5)*50, .shrinkage = .1)
+ bootControl <- trainControl(number = 1)
+ set.seed(2)
+ gbmFit <- train(prePrior1[,-c(2,60,61,161)], trainClass, method = "gbm", tuneLength = 5,
+ trControl = bootControl
+ ##, scaled = FALSE
+ , tuneGrid = gbmGrid 
+ )
+ pred1 <- predict(gbmFit$finalModel, newdata = preTest1[,-c(2,60,61,161)])
+ sub4Collect1 <- cbind(sub4Collect1, pred1)
+ print(i)
+ flush.console()
+ }
Iter   TrainDeviance   ValidDeviance   StepSize   Improve
     1        0.0000            -nan     0.1000    0.0000
     2        0.0000            -nan     0.1000    0.0000
     3        0.0000            -nan     0.1000    0.0000
     4        0.0000            -nan     0.1000    0.0000
     5        0.0000            -nan     0.1000    0.0000
     6        0.0000            -nan     0.1000    0.0000
     7        0.0000            -nan     0.1000    0.0000
     8        0.0000            -nan     0.1000    0.0000
     9        0.0000            -nan     0.1000    0.0000
    10        0.0000            -nan     0.1000    0.0000
    50        0.0000            -nan     0.1000    0.0000

Error in n.trees[n.trees > object$n.trees] <- object$n.trees : 
  argument "n.trees" is missing, with no default
> stopCluster(cl)
> timee4 <- proc.time() - ptm
> timee4 
   user  system elapsed 
  3.563   0.306  14.472 

Any suggestions?

like image 591
screechOwl Avatar asked Jan 04 '12 05:01

screechOwl


2 Answers

The proper code for the predict() function requires feeding in the .n.trees parameter manually from the gbmFit$finalModel object as such:

    pred1 <- predict(gbmFit$finalModel, newdata = preTest1[,-c(2,60,61,161)], 
              n.trees=gbmFit1$bestTune$.n.trees)
like image 131
screechOwl Avatar answered Sep 22 '22 19:09

screechOwl


If this is not working :

pred1 <- predict(gbmFit$finalModel, newdata = preTest1[,-c(2,60,61,161)], 
          n.trees=gbmFit1$bestTune$.n.trees)

you can use this:

pred1 <- predict(gbmFit, newdata = preTest1[,-c(2,60,61,161)], 
          n.trees=gbmFit1$n.trees)
like image 41
TheMI Avatar answered Sep 22 '22 19:09

TheMI