[R] substitute games with randomForest::partialPlot

Johann Hibschman jhibschman at gmail.com
Wed Sep 14 18:44:58 CEST 2011


I'm having trouble calling randomForest::partialPlot programmatically.
It tries to use name of the (R) variable as the data column name.

Example:

  library(randomForest)
  iris.rf <- randomForest(Species ~ ., data=iris, importance=TRUE, proximity=TRUE)
  partialPlot(iris.rf, iris, Sepal.Width)   # works
  partialPlot(iris.rf, iris, "Sepal.Width") # works
  (function(var.name)
    partialPlot(iris.rf, iris, var.name))("Sepal.Width") # fails

I can get around this with the following hack:

  (function(var.name)
    eval(substitute(partialPlot(iris.rf, iris, var.name),
                    list(var.name=var.name)))
  )("Sepal.Width") # works

But this seems like a horrible hack.  Is there a "nicer" way to do this?

The relevant code is:

  function (x, pred.data, x.var, which.class, w, plot = TRUE, add = FALSE, 
      n.pt = min(length(unique(pred.data[, xname])), 51), rug = TRUE, 
      xlab = deparse(substitute(x.var)), ylab = "", main = paste("Partial Dependence on", 
          deparse(substitute(x.var))), ...) 
  {
  ...
      x.var <- substitute(x.var)
      xname <- if (is.character(x.var)) 
          x.var
      else {
          if (is.name(x.var)) 
              deparse(x.var)
          else {
              eval(x.var)
          }
      }
      xv <- pred.data[, xname]
  ...
  }

Thanks,
Johann



More information about the R-help mailing list