1

When I try to crate a grid of parameters for training with caret I get various errors:

> my_grid <- createGrid("rf")
Error in if (p <= len) { : argument is of length zero

> my_grid <- createGrid("rf", 4)
Error in if (p <= len) { : argument is of length zero

> my_grid <- createGrid("rf", len=4)                                        
Error in if (p <= len) { : argument is of length zero

The documentation for createGrid says:

This function creates a data frame that contains a grid of
     complexity parameters specific methods.
Usage:
       createGrid(method, len = 3, data = NULL)
Arguments:
  method: a string specifying which classification model to use. See
          'train' for a full list.
     len: an integer specifying the number of points on the grid for
          each tuning parameter.
    data: the training data (only needed in the case where the 'method'
          is 'cforest', 'earth', 'bagEarth', 'fda', 'bagFDA', 'rpart',
          'svmRadial', 'pam', 'lars2', 'rf' or 'pls'). The outcome
          should be in a column called '.outcome'.

and gives the following examples which work correctly:

 createGrid("rda", 4)
 createGrid("lm")
 createGrid("nnet")
 
 ## data needed for SVM with RBF:
 ## Not run:
 
 tmp <- iris
 names(tmp)[5] <- ".outcome"
 head(tmp)
 createGrid("svmRadial", data = tmp, len = 4)
 ## End(Not run)

With this, what I am doing wrong?

Follow up question:

What is the connection between len as an argument to createGrid and tuneLength in the argument for train? Can len and tuneLength they be used together? What is their relationship?

Other helpful threads:

In case it helps, here is a thread describing how to use createGrid with train in caret: caret::train: specify model-generation-parameters

Community
  • 1
  • 1
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564

1 Answers1

1

The code you pulled from the examples works fine for me (and noting that it fixes the problem that existed when you posted on Rhelp):

tmp <- iris
 names(tmp)[5] <- ".outcome"
 head(tmp)
 createGrid("svmRadial", data = tmp, len = 4)
#-------
     .sigma   .C
1 0.7500934 0.25
2 0.7500934 0.50
3 0.7500934 1.00
4 0.7500934 2.00

Edit:

>  createGrid("rf", data = tmp, len = 4)
randomForest 4.6-7
Type rfNews() to see new features/changes/bug fixes.

Attaching package: ‘randomForest’

The following object(s) are masked from ‘package:Hmisc’:

    combine

note: only 3 unique complexity parameters in default grid. Truncating the grid to 3 .

  .mtry
1     2
2     3
3     4

I say again: What problem remains?

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks. Please see the examples I tried at the top of the OP. I didn't say the examples provided in the documentation don't work. – Amelio Vazquez-Reina Feb 12 '13 at 19:21
  • Ok (+1) thanks! I didn't know I was supposed to pass data to rf . The Usage part of the documentation said `data=NULL`, which made me think it was optional. By the way, do you know the connection between (1) the argument `tuneLength` in `train` and (2) the grid passed to `train` via `tuneGrid`? (see follow up question) – Amelio Vazquez-Reina Feb 12 '13 at 19:24
  • As I read the documentation there would be no connection. It says that one or the other is used to construct candidate parameters. `tuneGrid` lets you specify the locations, while `tuneLength` lets you specify their number. – IRTFM Feb 12 '13 at 19:33
  • 1
    `createGrid()` no longer exists in caret – Steve Bronder Jul 19 '16 at 15:29
  • Yes. This has already been noted on SO: http://stackoverflow.com/questions/22275173/creategrid-function-from-caret-package-was-it-removed However if you want the code it's still here: https://r-forge.r-project.org/scm/viewvc.php/pkg/caret/R/createGrid.R?view=markup&root=caret&pathrev=43 ...or... https://github.com/dalpozz/caretUtils/blob/master/createGrid.R – IRTFM Jul 19 '16 at 18:47