6

I'm using the randomForest package (v 4.6-7) in R (v 2.15.3) and can easily use the function randomForest to create a model. However, when I try to predict on my test set, the predict.randomForest function cannot be found. I've also tried plotting with plot.randomForest only to get the same error, "could not find function."

I've already tried reinstalling the package (figuring maybe it was out of date) and made sure the spelling is absolutely correct. I cannot figure out what's causing this error, any ideas?

paulsef11
  • 628
  • 1
  • 8
  • 11
  • Probably redundant, but make sure you've loaded the package with `library(randomForest)`. Otherwise, there's no reason you should be getting this error. Even if the functions aren't exported, they're still registered as S3 methods so R should know where to find them. – Hong Ooi Jun 21 '13 at 15:47

1 Answers1

7

It appears that the functions of interest are not exported from the package.

If you use ls(package:randomForest) you'll get a list of the exported functions.

If you want to see all the functions available the use: ls(getNamespace("randomForest"), all.names=TRUE). Thanks @Joshua. You'll see the functions you want there.

In order to refer to one of them explicitly, use: randomForest:::predict.randomForest() or just make a object which inherits the class 'randomForest' and call predict() on it directly.

Community
  • 1
  • 1
dardisco
  • 5,086
  • 2
  • 39
  • 54
  • Thanks, `getAnywhere()` is indeed v. handy in this regard. `??"predict.randomForest"` can also be helpful in finding the namespace. – dardisco Jun 21 '13 at 00:47
  • 1
    Thanks for this, I figured the library(someRpackage) exported all the elements of someRpackage. Is there a reason this is not always the default behavior? – paulsef11 Jun 21 '13 at 01:02
  • In some cases it saves the author/developer having to document all the functions they have written, particularly those that are called by other functions in the package and not intended for the end-user to call explicitly. Also it's common for generic S3 methods like `plot`, where the default behavior is fairly obvious (make a graph) but they want a specific method for a class defined in their package. I tend to agree that all functions should be available by default but this is a matter of style. Also a function could have unpleasant consequences if called inadvertently. – dardisco Jun 21 '13 at 01:09