View source: R/predict.ivarpro.R
| predict.ivarpro | R Documentation |
Calculates case-specific iVarPro gradients on a new feature matrix by reusing the rule-level gradients stored in a previously computed iVarPro object and recomputing rule membership for the new cases.
predict.ivarpro(object,
newdata = NULL,
model = NULL,
noise.na = NULL,
path.store.membership = FALSE,
save.data = TRUE,
...)
object |
An object returned by |
newdata |
Optional data.frame of predictors for which to compute
case-specific gradients. If supplied, gradients are computed for
|
model |
Optional model override. If |
noise.na |
Logical controlling how cells with no usable
contributing rules are handled. If |
path.store.membership |
Logical. If |
save.data |
Logical. If |
... |
Additional arguments passed to
|
A previously computed iVarPro object contains (i) a set of retained
VarPro rules identified by tree and node/branch IDs, (ii) a
release-variable index for each rule, and (iii) an estimated rule-level
gradient. For new cases, the function obtains terminal node membership
for each tree using randomForestSRC::predict.rfsrc(membership =
TRUE) and determines which stored rules apply to each case by matching
the case's terminal node to the stored rule node/branch ID for that
tree. Case-specific gradients are then computed by aggregating the
stored rule-level gradients over all rules that apply to the case and
release the corresponding variable.
When newdata is not supplied, predict.ivarpro() attempts
to return OOB case-specific gradients for the original training
data. When the iVarPro object was created with membership information
stored in its path (path.store.membership = TRUE, the default for
ivarpro()), the restore prediction will reproduce the original
iVarPro matrix.
If save.data = TRUE and newdata is supplied,
attr(out,"data") contains the predictor matrix used for
prediction, so downstream wrappers such as partial.ivarpro
can be used directly. If you want ladder bands on predicted gradients,
set path.store.membership = TRUE when predicting.
Returns an object with the same shape as object:
For univariate outcomes, a numeric data.frame of
dimension n_{test} \times p containing predicted
case-specific gradients.
For multivariate outcomes, a named list of such
data.frames, one per outcome coordinate.
The returned object includes an "ivarpro.path" attribute
containing rule metadata and (optionally) prediction-time membership
indices.
ivarpro,
partial.ivarpro,
varpro
## ------------------------------------------------------------
##
## Restore mode example (training OOB gradients are reproduced)
##
## ------------------------------------------------------------
data(peakVO2, package = "randomForestSRC")
## Fit VarPro and iVarPro
vp <- varpro(Surv(ttodead, died) ~ ., peakVO2, ntree = 50)
imp <- ivarpro(vp)
## Restore prediction: should match the original iVarPro matrix
p.restore <- predict.ivarpro(imp)
all.equal(p.restore, imp, check.attributes = FALSE)
## Downstream wrapper: partial plot using the restored gradients
partial.ivarpro(p.restore, var = "peak.vo2",
col.var = "interval", size.var = "y", x = attr(imp, "data"))
## ------------------------------------------------------------
##
## Synthetic example (Friedman #1) with prediction on new data
##
## ------------------------------------------------------------
if (requireNamespace("mlbench", quietly = TRUE)) {
set.seed(123)
## training data
tr <- mlbench::mlbench.friedman1(500, sd = 1)
train <- data.frame(tr$x, y = tr$y)
colnames(train)[1:ncol(tr$x)] <- paste0("x", 1:ncol(tr$x))
## ivarpro fit on training data
vp <- varpro(y ~ ., train, ntree = 100)
imp <- ivarpro(vp)
## test data
te <- mlbench::mlbench.friedman1(1e4, sd = 1)
test <- data.frame(te$x)
colnames(test) <- paste0("x", 1:ncol(te$x))
## predicted gradients on test data
p.test <- predict.ivarpro(imp, newdata = test)
## partial plot directly on predicted object
partial.ivarpro(p.test, var = "x1", col.var = "x2")
## push x1 outside the original Friedman support [0, 1]
## a heuristic way to test out-of-distribution (OOD)
test.ood <- test
test.ood$x1 <- runif(nrow(test), min = -0.5, max = 1.5)
## predicted gradients on test data
p.test.ood <- predict.ivarpro(imp, newdata = test.ood)
## partial plot showing support for x1
partial.ivarpro(p.test.ood, var = "x1", col.var = "x2",
x.dist = c("density", "rug"))
## reference lines for the original training support of x1
abline(v = c(0, 1), lty = 2)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.