kogalur / randomForestSRC

DOCUMENTATION:
https://www.randomforestsrc.org/
GNU General Public License v3.0
115 stars 18 forks source link

plot.variable does not re-read the input data now? #339

Closed ehrlinger closed 1 year ago

ehrlinger commented 1 year ago

Using the current CRAN release (R 4.2.1, randomForestSRC 3.1.1), I have a set of pts (rm_pts) that I want to manipulate and call the plot.variable function. Previously, I could edit the xvar data.frame within the rfsrc object to generate a partial plot with a single value and loop over the values of interest. This no longer works.

This is reported in the ggRandomForest issue https://github.com/ehrlinger/ggRandomForests/issues/42 which is demonstrated in the vignette/ggrfRegression.Rmd partial coplots. Both the contour and surface plots are built using the same partial plot curves for all input values that we are submitting.

> rm_pts
 [1] 3.561 4.906 5.093 5.390 5.456 5.572 5.613 5.682 5.713 5.783
[11] 5.822 5.856 5.875 5.889 5.924 5.950 5.968 5.998 6.020 6.051
[21] 6.086 6.114 6.140 6.164 6.195 6.229 6.254 6.310 6.341 6.377
[31] 6.404 6.426 6.453 6.482 6.516 6.563 6.606 6.649 6.718 6.770
[41] 6.833 6.879 6.980 7.079 7.178 7.287 7.454 7.802 8.069 8.780

> partial_boston_surf <- lapply(rm_pts, function(ct) {
+   rfsrc_boston$xvar$rm <- ct
+   plot.variable(rfsrc_boston, xvar = "lstat", time = 1,
+                 npts = 50, show.plots = FALSE, 
+                 partial = TRUE)
+ })
ishwaran commented 1 year ago

We no longer recommend using the plot.variable function for custom partial plot calls. This is meant to be a user friendly helper function and actually is just a wrapper to the more advanced function partial.rfsrc which provides a direct and fast interface to develop partial plots. This latter function should be used for custom calls as illustrated below for Boston Housing.

## boston housing regression example
library(mlbench)
data(BostonHousing)

## run the forest 
o <- rfsrc(medv~., BostonHousing, nodesize=1)

## obtain partial effect for room using specified values
rm.pts <- c(3.561, 4.906, 5.093, 5.390, 5.456, 5.572, 5.613, 5.682, 5.713, 5.783)
partial.obj <- partial(o,
                       partial.xvar = "rm",
                       partial.values = rm.pts)
pdta <- get.partial.plot.data(partial.obj)

## plot partial values
plot(pdta$x, pdta$yhat, type = "b", pch = 16,
     xlab = "room", ylab = "partial effect of room")