I am running randomForest regressions in a loop, passing a different data.frame each time, and trying to plot importance and partial dependency plots for all variables in the data.frame. The commands all run OK when typed at the prompt, but when I wrap them into a function, the partialPlot function fails with:
 Error in eval(x.var) : object 'impvar' not found

It seems that the x.var parameter is getting the variable name (impvar[i] in this case) rather than the value of the variable.
What am I missing here?


The easiest way to see this is using the example right from the partialPlot help page, but wrapped into a function:

##--------------------------
library(randomForest)
## Looping over variables ranked by importance:
do_pdp <- function(dta) {
        dta <- na.omit(dta)
        set.seed(131)
        ozone.rf <- randomForest(Ozone ~ ., dta, importance=TRUE)
        imp <- importance(ozone.rf)
        impvar <- rownames(imp)[order(imp[, 1], decreasing=TRUE)]
        op <- par(mfrow=c(2, 3))
        for (i in seq_along(impvar)) {
             partialPlot(ozone.rf, dta, impvar[i], xlab=impvar[i],
                     main=paste("Partial Dependence on", impvar[i]),
                     ylim=c(30, 70))
        }
        par(op)
}
data(airquality)
do_pdp(airquality)
##--------------------------

Fails with the above message above for me. Running the commands directly, without the "do_pdp" function works fine, of course.


sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 19

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods base

other attached packages:
[1] randomForest_4.6-14

loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1

Thanks

--
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to