# \donttest{
## ------------------------------------------------------------
##
## 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)
}
# }Run the code above in your browser using DataLab