A partial dependence curve marginalizes the forest's prediction over all other predictors: for each evaluation point of the target variable, the forest scores every training observation with that value substituted in, then averages the result. What you get is the average effect of the target variable after "integrating out" the rest – a curve that would be flat if the variable carried no signal.
Usage
gg_partial_rfsrc(
rf_model,
xvar.names = NULL,
xvar2.name = NULL,
newx = NULL,
partial.time = NULL,
partial.type = c("surv", "chf", "mort"),
cat_limit = 10,
n_eval = 25
)Arguments
- rf_model
A fitted
rfsrcobject.- xvar.names
Character vector of predictor names for which partial dependence should be computed. Must be a subset of
rf_model$xvar.names.- xvar2.name
Optional single character name of a grouping variable in
newx. When supplied, partial dependence is computed separately for each unique level of this variable and agrpcolumn is appended.- newx
Optional
data.frameof predictor values to evaluate partial effects at. Defaults to the training data stored inrf_model$xvar. All column names must matchrf_model$xvar.names.- partial.time
Numeric vector of desired time points for survival forests (ignored for regression/classification). Values are automatically snapped to the nearest entry in
rf_model$time.interest; see the Survival forests section below. WhenNULL(default), three quartile points oftime.interestare used.- partial.type
Character; type of predicted value for survival forests, passed through to
partial.rfsrc. One of"surv"(default),"chf", or"mort". Ignored for non-survival forests.partial.rfsrc()requires a non-NULLvalue for survival families; supplying it here avoids a cryptic “argument is of length zero” error from the underlying C code.- cat_limit
Variables with fewer than
cat_limitunique values innewxare treated as categorical; all others are continuous. Defaults to 10.- n_eval
Number of evaluation points for continuous variables. Instead of passing all observed values (which can be slow, especially for survival forests), continuous predictors are evaluated on a quantile grid of this many points. Categorical variables always use all unique levels. Defaults to 25.
Value
A named list with two elements:
- continuous
A
data.framewith columnsx(numeric),yhat,name(variable name), and optionallygrp(the level ofxvar2.name) andtime(survival forests only) for all continuous predictors.- categorical
A
data.framewith the same columns butxkept as character, for low-cardinality predictors.
Details
This function builds those curves for one or more predictors by calling
partial.rfsrc and then tidy-stacking the
results into separate data frames for continuous and categorical variables.
Unlike gg_partial (which wraps plot.variable), you
pass the fitted rfsrc object directly – no intermediate
plot.variable step.
For survival forests, the marginalized quantity depends on
partial.type: survival probability ("surv"), cumulative
hazard function ("chf"), or expected mortality ("mort").
You can request the curve at one or more time horizons via
partial.time; the resulting data have a time column so the
plot layers them as separate coloured lines.
Survival forests and partial.time
partial.rfsrc expects every value in
partial.time to be an exact member of the model's
time.interest vector, the unique observed event times stored in the
fitted object. Pass an arbitrary time, even a plausible one such as
c(1, 3) for a study measured in years, and you get a C-level
prediction error from inside partial.rfsrc.
gg_partial_rfsrc takes care of this: every element of
partial.time is silently snapped to its nearest
time.interest value before the call. To target a specific
follow-up horizon, find the closest grid point yourself and pass it
explicitly:
ti <- rf_model$time.interest
t1 <- ti[which.min(abs(ti - 1))] # nearest to 1 year
pd <- gg_partial_rfsrc(rf_model, xvar.names = "x", partial.time = t1)Logical predictor columns
partial.rfsrc does not handle
logical predictor columns correctly in survival forests
(randomForestSRC <= 3.5.1). If your training data contains binary 0/1
columns, convert them to factor rather than logical
before fitting the model.
Examples
## ------------------------------------------------------------
##
## regression
##
## ------------------------------------------------------------
airq.obj <- randomForestSRC::rfsrc(Ozone ~ ., data = airquality)
## partial effect for wind
prt_dta <- gg_partial_rfsrc(airq.obj,
xvar.names = c("Wind"))