if (FALSE) {
## Load package:
library("diversityForest")
## Set seed to make results reproducible:
set.seed(1234)
## Construct interaction forest and calculate EIM values:
data(stock)
model <- interactionfor(dependent.variable.name = "company10", data = stock,
num.trees = 20)
# NOTE: num.trees = 20 (in the above) would be much too small for practical
# purposes. This small number of trees was simply used to keep the
# runtime of the example short.
# The default number of trees is num.trees = 20000 if EIM values are calculated
# and num.trees = 2000 otherwise.
## Obtain a first overview by applying the plot() function for
## interactionfor obects:
plot(model)
## Several possible application cases of the plotEffects() function:
# Visualise the estimated bivariable influences of the five variable pairs with the
# largest quantitative EIM values:
plotEffects(model) # type="quant" is default.
# Visualise the estimated bivariable influences of the five pairs with the
# largest qualitative EIM values:
plotEffects(model, type="qual")
# Visualise the estimated bivariable influences of all (eight) pairs that involve
# the variable "company7" sorted in decreasing order according to the
# qualitative EIM values:
plotEffects(model, allwith="company7", type="qual", numpairs=8)
# Visualise the estimated bivariable influences of the pairs with third and fifth
# largest qualitative EIM values:
plotEffects(model, type="qual", indpairs=c(3,5))
# Visualise the estimated bivariable influences of the pairs ("company3", "company5") and
# ("company1", "company9"):
plotEffects(model, pairs=list(c("company3", "company5"), c("company1", "company9")))
## Saving of plots generated with the plotEffects() function (e.g., for use in publications):
# Apply plotEffects() to obtain plots for the five variable pairs
# with the largest qualitative EIM values and store these plots in
# an object 'ps':
ps <- plotEffects(model, type="qual", pvalues=FALSE, twoplots=FALSE, addtitles=FALSE, plotit=FALSE)
# pvalues = FALSE states that no p-values should be shown in the plots,
# because these might not be desired in plots meant for publication.
# twoplots = FALSE ensures that we get one plot for each page instead of two plots per page.
# addtitles = FALSE removes the automatically generated titles, because these are likely
# not desired in publications.
# plotit = FALSE ensures that the plots are not displayed, but only returned (invisibly)
# by plotEffects().
# Save the plot with second largest qualitative EIM value:
p1 <- ps[[2]]
# Add title:
library("ggpubr")
p1 <- annotate_figure(p1, top = text_grob("My descriptive plot title 1", face = "bold", size = 14))
p1
# Save as PDF:
# library("ggplot2")
# ggsave(file="mypathtofolder/FigureXY1.pdf", width=14, height=6)
# Save the plot with fifth largest qualitative EIM value:
p2 <- ps[[5]]
# Add title:
p2 <- annotate_figure(p2, top = text_grob("My descriptive plot title 2", face = "bold", size = 14))
p2
# Save as PDF:
# ggsave(file="mypathtofolder/FigureXY1.pdf", width=14, height=6)
# Combine both of the above plots:
p <- ggarrange(p1, p2, nrow = 2)
p
# Save the combined plot:
# ggsave(file="mypathtofolder/FigureXYcombined.pdf", width=14, height=11)
# NOTE: Using plotEffects() it is not possible to change the plots
# themselves (by e.g., increasing the label sizes or changing the
# axes ranges). However, the function plotPair() can be used to change
# the plots themselves.
}
Run the code above in your browser using DataLab