# \donttest{
## ------------------------------------------------------------
##
## survival example with shap-like plot
##
## ------------------------------------------------------------
data(peakVO2, package = "randomForestSRC")
o <- varpro(Surv(ttodead, died)~., peakVO2, ntree = 50)
imp <- ivarpro(o, adaptive = TRUE)
ivarpro.shap(imp, o$x)
## ------------------------------------------------------------
##
## synthetic regression example
##
## ------------------------------------------------------------
## true regression function
true.function <- function(which.simulation) {
if (which.simulation == 1) {
function(x1, x2) { 1 * (x2 <= .25) +
15 * x2 * (x1 <= .5 & x2 > .25) +
(7 * x1 + 7 * x2) * (x1 > .5 & x2 > .25) }
}
else if (which.simulation == 2) {
function(x1, x2) { r <- x1^2 + x2^2; 5 * r * (r <= .5) }
}
else {
function(x1, x2) { 6 * x1 * x2 }
}
}
## simulation function
simfunction <- function(n = 1000, true.function, d = 20, sd = 1) {
d <- max(2, d)
X <- matrix(runif(n * d, 0, 1), ncol = d)
dta <- data.frame(list(
x = X,
y = true.function(X[, 1], X[, 2]) + rnorm(n, sd = sd)
))
colnames(dta)[1:d] <- paste("x", 1:d, sep = "")
dta
}
## iVarPro importance plot
ivarpro.plot <- function(dta, release = 1, combined.range = TRUE,
cex = 1.0, cex.title = 1.0, sc = 5.0,
gscale = 30, title = NULL) {
x1 <- dta[, "x1"]
x2 <- dta[, "x2"]
x1n <- expression(x^{(1)})
x2n <- expression(x^{(2)})
if (release == 1) {
if (is.null(title))
title <- bquote("iVarPro Case-Specific Gradient " ~ x^{(1)})
cex.pt <- dta[, "Importance.x1"]
}
else {
if (is.null(title))
title <- bquote("iVarPro Case-Specific Gradient " ~ x^{(2)})
cex.pt <- dta[, "Importance.x2"]
}
if (combined.range) {
cex.pt <- cex.pt / max(dta[, c("Importance.x1", "Importance.x2")],
na.rm = TRUE)
}
rng <- range(c(x1, x2))
oldpar <- par(mar = c(4, 5, 5, 1),
mgp = c(2.25, 1.0, 0),
bg = "white")
gscalev <- gscale
gscale <- paste0("gray", gscale)
plot(x1, x2, xlab = x1n, ylab = x2n,
ylim = rng, xlim = rng,
col = "#FFA500", pch = 19,
cex = (sc * cex.pt), cex.axis = cex, cex.lab = cex,
panel.first = rect(par("usr")[1], par("usr")[3],
par("usr")[2], par("usr")[4],
col = gscale, border = NA))
abline(a = 0, b = 1,
lty = 2, col = if (gscalev < 50) "white" else "black")
mtext(title, cex = cex.title, line = .5)
par(oldpar)
}
## simulate the data
which.simulation <- 1
df <- simfunction(n = 500, true.function(which.simulation))
## varpro analysis
o <- varpro(y ~ ., df)
## canonical ivarpro analysis (default cut.max = 1)
imp1 <- ivarpro(o)
## sharper local analysis using a smaller cut.max
imp2 <- ivarpro(o, cut.max = 0.5)
## data-adaptive analysis
imp3 <- ivarpro(o, adaptive = TRUE)
## build data for plotting the results (using x1 and x2)
df.imp1 <- data.frame(Importance = imp1, df[, c("x1", "x2")])
df.imp2 <- data.frame(Importance = imp2, df[, c("x1", "x2")])
df.imp3 <- data.frame(Importance = imp3, df[, c("x1", "x2")])
## plot the case-specific importance surfaces
oldpar <- par(mfrow = c(3, 2))
ivarpro.plot(df.imp1, 1); ivarpro.plot(df.imp1, 2)
ivarpro.plot(df.imp2, 1); ivarpro.plot(df.imp2, 2)
ivarpro.plot(df.imp3, 1); ivarpro.plot(df.imp3, 2)
par(oldpar)
# }Run the code above in your browser using DataLab