[R] partialPlot within a function

Micha Silver t@v|b@r @end|ng |rom gm@||@com
Sun Sep 2 08:38:08 CEST 2018



On 09/02/2018 09:28 AM, Amit Mittal wrote:
>> partialPlot(ozone.rf, dta, impvar[i],
>
> Is there a close bracket here. Have you tried naming each of the 
> parameters, like data = ozone.rf etc. Dry if I am rushing this . But 
> it looks a basic syntax problem
>

It's just copy-paste from the help example. I don't think there's a 
syntax problem.
And If I just take the same commands *out* of the function, it works fine.
:-(

> ————————————
> Amit Mittal
> PhD in Finance and Accounting (tbd)
> IIM Lucknow
> http://ssrn.com/author=2665511
> *Top 10%, downloaded author since July 2017
> ————————————
> Sent from my Outlook for Android
> https://aka.ms/ghei36
>
> ------------------------------------------------------------------------
> *From:* Micha Silver <tsvibar using gmail.com>
> *Sent:* Sunday, September 2, 2018 11:54:44 AM
> *To:* Amit Mittal
> *Cc:* R-help
> *Subject:* Re: [R] partialPlot within a function
>
>
> On 09/02/2018 09:17 AM, Amit Mittal wrote:
>> Could be just the assignment of a new xlab
>>
>> `xlab=impvar[i],`
>>
>
> The error message refers to the x.var, not the axis label.
> Besides, the x axis label has a default value. x.var does not)
> ??
> Thanks
>
>> Try xlab[i]=impvar[i]
>> Or initialising it xlab <-0
>>
>> ————————————
>> Amit Mittal
>> PhD in Finance and Accounting (tbd)
>> IIM Lucknow
>> http://ssrn.com/author=2665511
>> *Top 10%, downloaded author since July 2017
>> ————————————
>> Sent from my Outlook for Android
>> https://aka.ms/ghei36
>>
>> ------------------------------------------------------------------------
>> *From:* R-help <r-help-bounces using r-project.org> on behalf of Micha 
>> Silver <tsvibar using gmail.com>
>> *Sent:* Sunday, September 2, 2018 2:02:26 AM
>> *To:* R-help using r-project.org
>> *Subject:* [R] partialPlot within a function
>> 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 using 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 
>> <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>
> -- 
> Micha Silver
> Ben Gurion Univ.
> Sde Boker, Remote Sensing Lab
> cell: +972-523-665918

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




More information about the R-help mailing list