partial_qrf | R Documentation |
Get a data frame for partial dependence plots from a quantile random forest. The plots can be obtained using plotting functions from other packages.
partial_qrf(object, pred.var, Q = c(0.05, 0.5, 0.95), ...)
object |
a |
pred.var |
character string giving the names of the predictor variables of
interest (see |
Q |
a numeric vector of probabilities for which the plot is desired. |
... |
other arguments passed to |
A data.frame
with the following columns:
the predictor variables supplied in pred.var
,
response variable (the name is extracted from the ranger
function call),
and, if length(Q) > 1
, one more column named "Quantile"
(for an appropriate order of labels in the plots, Quantile
is a factor).
partial
, ranger
## Not run:
# Example 1: Swiss
library(dplyr)
library(ggplot2)
# fit a quantile random forest
qrf <- ranger::ranger(Examination ~ ., data = swiss, quantreg = TRUE, num.trees = 50)
# 1.1) a plot for one quantile
partial_qrf(qrf, pred.var = "Agriculture", Q = 0.5) %>%
ggplot(aes(Agriculture, Examination)) +
geom_line()
# 1.2) a plot for several quantiles, with decorations
library(hrbrthemes)
library(viridis)
partial_qrf(qrf, pred.var = "Agriculture") %>%
ggplot(aes(Agriculture, Examination, group = Quantile, color = Quantile)) +
geom_line() +
scale_color_viridis(discrete = TRUE) +
theme_ipsum()
# 1.3) a plot for one quantile for 2 predictors (takes longer, so
# save as an object, can also redefine pred.grid or decrease grid.resolution)
df <- partial_qrf(qrf, pred.var = c("Agriculture", "Catholic"), Q = 0.5,
grid.resolution = 20)
ggplot(df, aes(Agriculture, Catholic)) +
geom_tile(aes(fill = Examination)) +
scale_fill_viridis() +
theme_ipsum() +
labs(fill = "Median\nexamination")
# 1.4) a plot for each variable
# define a vector of variable names and desired quantiles
varnames <- qrf$forest$independent.variable.names
qs <- c(0.01, 0.025, 0.05)
# save outputs in a list
ddf <- lapply(varnames, function(vn) partial_qrf(qrf, pred.var = vn, Q = qs))
# use your preferred plotting functions, e.g., ggplot2 with patchwork
# for a common y-axis, get ranges
yrange <- range(sapply(ddf, function(x) range(x[,2])))
# then create a list of ggplots
plist <- lapply(seq_along(varnames), function(vi) {
ggplot(ddf[[vi]], aes_string(x = varnames[vi], y = "Examination",
group = "Quantile", color = "Quantile")) +
geom_line() +
scale_color_viridis(discrete = TRUE) +
theme_ipsum(plot_margin = ggplot2::margin(10, 10, 10, 10)) +
ylim(yrange[1], yrange[2]) + theme(axis.title.y = element_blank())
})
# then organize the plots
library(patchwork)
pl <- wrap_plots(plist) + plot_layout(guides = "collect")
pl
# can stop here or continue grouping
gpl <- patchwork::patchworkGrob(pl)
gridExtra::grid.arrange(gpl, left = "Examination")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.