Skip to content
Prev 69689 / 398502 Next

randomForest partialPlot x.var through function

All,

I'm trying to set up a function which calls the partialPlot function but 
am getting an error that I can't seem to solve.  Here's a simplified 
version of the function and error...

 > pplot <- 
function(rf,pred.var){partialPlot(x=rf,pred.data=acoust,x.var=pred.var)}
 >
 > attach(acoust)
 > acoust.rf <- 
randomForest(VocalTF~Cruise+Spot+Spin+Delph+Stripe+Steno+Turs+Gramp+Lags+
+       Lisso+FerPsu+Glob+mixspotspin+mixother+mixed+Size,
+       data=acoust,importance=TRUE,keep.forest=TRUE)
 > cruise.pp <- pplot(acoust.rf,Cruise)
Error in "[.data.frame"(pred.data, , xname) :
        undefined columns selected

Here's the traceback call...

 > traceback()
6: stop("undefined columns selected")
5: "[.data.frame"(pred.data, , xname)
4: pred.data[, xname]
3: partialPlot.randomForest(x = rf, pred.data = acoust, x.var = pred.var)
2: partialPlot(x = rf, pred.data = acoust, x.var = pred.var)
1: pplot(acoust.rf, Cruise)

 From the partialPlot help file, the "pred.data[, xname]" seems to apply 
to the "n.pt" argument which reads, "if 'x.var' is continuous, the 
number of points on the grid for evaluating partial dependence."

It is not clear to me what "xname" is or where it is defined.  If it is 
supposed to be "x.var" where does it get assigned?  I've tried 
help.search("xname") to no useful avail that I could tell.
Second, in my case, "acoust$Cruise" is a factor and the line

 > cruise.pp <- partialPlot(acoust.rf,acoust,Cruise)

produces the proper plot.

Which leads me to belive that I'm doing something wrong in how I'm 
passing "Cruise" through "pred.var" in the function call to "x.var", but 
I can't figure out how to properly correct it.

Thanks in advance for any pointers.

e.