View source: R/predict.ivarpro.R
| predict.ivarpro | R Documentation |
Apply an iVarPro analysis to new observations, or recover its out-of-bag training scores. The result gives a local gradient for each case and processed predictor.
## S3 method for class 'ivarpro'
predict(object, newdata = NULL, model = NULL,
noise.na = NULL, path.store.membership = FALSE,
save.data = TRUE, ...)
object |
An object returned by |
newdata |
A data frame containing the predictors for new cases.
Outcome columns are optional. For a VarPro model, supply the
original predictor columns; the training hot-encoding is applied
automatically. If |
model |
The VarPro object or random forest used to obtain
|
noise.na |
How to average unavailable rule scores: |
path.store.membership |
Retain the case-to-rule membership indices with the result? These are optional diagnostic information. |
save.data |
Store the processed predictor data for subsequent
plots? Default is |
... |
Additional arguments passed to the forest prediction method when memberships are obtained. |
New cases are routed through the original forest. For each predictor, their scores average the stored gradients of applicable rules that release that predictor. The local slopes and neighborhood choices remain those learned in the original iVarPro analysis.
Scores retain the target, scaling and signed or absolute convention
chosen in ivarpro. Multiple targets produce a named list of
score tables. Use plot or shap.ivarpro to inspect the
result, with target selecting a response or class when needed.
Omitting newdata recovers training scores using out-of-bag
membership. Passing the training predictors explicitly treats those
observations as new cases and uses all applicable forest rules, so
the two calls can give different scores.
An object of class ivarpro, with one row per prediction case
and one column per processed predictor. A single target produces a
data frame; multiple targets produce a named list of data frames.
Row names identify the prediction cases.
The result retains the target labels, original model and rule
information needed for further prediction. With save.data = TRUE,
its data attribute contains aligned plotting data. For new
cases this contains processed predictors; the original explanation
targets are not recalculated. See ivarpro for score
interpretation.
ivarpro, plot.ivarpro,
shap.ivarpro
## Train on one sample and explain new observations.
set.seed(137)
n <- 400
sim <- data.frame(x1 = runif(n, -1, 1), x2 = runif(n, -1, 1))
sim$y <- 6 * sim$x1 * sim$x2 + rnorm(n, sd = 0.3)
trn <- sample.int(n, 300)
vp <- varpro(y ~ ., sim[trn, , drop = FALSE],
split.weight = FALSE, ntree = 100,
parallel = FALSE)
iv <- ivarpro(vp, scale = "none", nmin = 10)
## Outcome values are not needed for new-case gradients.
test <- sim[-trn, c("x1", "x2"), drop = FALSE]
iv.test <- predict(iv, newdata = test)
print(head(iv.test))
plot(iv.test, var = "x1", col.var = "x2")
## Recover the original out-of-bag training scores.
iv.oob <- predict(iv)
print(all.equal(as.matrix(iv.oob), as.matrix(iv),
check.attributes = FALSE))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.