[R] partialPlot within a function [Solved]

Micha Silver t@v|b@r @end|ng |rom gm@||@com
Sun Sep 2 14:56:51 CEST 2018


I found a few old posts on StackOverflow that brought up the same 
problem with partialPlot. Apparently the function refers to the global 
env when looking for x.var, and if it's running within a function, there 
is no global value for that parameter.

The work around was simple: put the partialPlot function into a do.call()

         for (i in seq_along(impvar)) {
                pP_params <- list(x = ozone.rf,  pred.data = dta,
                                 x.var = impvar[i], xlab = impvar[i],
                              main=paste("Partial Dependence on", 
impvar[i]),
                              ylim=c(30, 70))
              do.call("partialPlot", pP_params)
         }

Regards,

On 09/01/2018 11:32 PM, Micha Silver wrote:
> 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




More information about the R-help mailing list