# \donttest{
## Load package:
library("unityForest")
## Set seed to make results reproducible:
set.seed(1234)
## Load wine dataset:
data(wine)
## Construct unity forest and calculate unity VIM values:
model <- unityfor(dependent.variable.name = "C", data = wine,
importance = "unity", num.trees = 2000)
# NOTE: num.trees = 2000 (in the above) would be too small for practical
# purposes. This quite small number of trees was simply used to keep the
# runtime of the example short.
# The default number of trees is num.trees = 20000.
## Visualize the CRTRs for the five variables with the largest unity VIM
## values:
reprTrees(model, box_plots = FALSE, density_plots = FALSE)
## Visualize the CRTRs for the variables with the largest and third-largest
## unity VIM values:
reprTrees(model, indvars = c(2, 3), box_plots = FALSE, density_plots = FALSE)
## Visualize the CRTRs for the variables with the largest and third-largest
## unity VIM values, where density plots are shown to visualize the
## outcome class-specific distributions of the variables values in the
## nodes with top-scoring splits:
reprTrees(model, indvars = c(2, 3), box_plots = FALSE, density_plots = TRUE)
## Visualize the CRTRs for the variables with the largest and third-largest
## unity VIM values, where both density plots and boxplots are shown to
## visualize the outcome class-specific distributions of the variables values
## in the top-scoring splits; the split points are not indicated in these
## plots:
ps <- reprTrees(model, indvars = c(2, 3), add_split_line = FALSE)
## Save one of the CRTRs with the corresponding density plot:
library("patchwork")
library("ggplot2")
p <- ps$plots[[1]]$tree_plot / ps$plots[[1]]$density_plot +
patchwork::plot_layout(heights = c(2, 1))
p
# outfile <- file.path(tempdir(), "figure_xy.pdf")
# ggsave(outfile, device = cairo_pdf, plot = p, width = 18,
# height = 14)
# Note: The plots can be manipulated with the usual ggplot2 syntax, e.g.:
ps$plots[[1]]$density_plot + xlab("Proline") + labs(title = NULL, y = NULL) +
theme(
legend.position = c(0.95, 0.95),
legend.justification = c(1, 1)
)
# }
Run the code above in your browser using DataLab