plot.clusterforest: Plot a clusterforest object

View source: R/clusterMethods.R

plot.clusterforestR Documentation

Plot a clusterforest object


A function that can be used to plot a clusterforest object, either by returning plots with information on the cluster solutions (e.g., average silhouette width), or plots of the medoid trees of each solution.


## S3 method for class 'clusterforest'
plot(x, ..., solution = NULL, predictive_plots = FALSE)



A clusterforest object


Additional arguments


The solution to plot the medoid trees from. Default = NULL


Indicating whether predictive plots should be returned: A plot showing the predictive accuracy when making predictions based on the medoid trees, and a plot of the agreement between the class label for each object predicted on the basis of the random forest as a whole versus based on the medoid trees. Default = FALSE.


This function can be used to plot a clusterforest object in two ways. If it's used with as only argument the clusterforest object, then the average silhouette width, agreement in predicted labels between medoids in solution and forest, and within cluster similarity measures are plotted for each solution. These plots may be helpful in deciding how many clusters are needed to summarize the forest (see Sies & Van Mechelen, 2020).

If the function is used with two arguments (the clusterforest object and the number of the solution), then the medoid tree(s) of that solution are plotted.


Sies, A. & Van Mechelen I. (2020). C443: An R-package to see a forest for the trees. Journal of Classification.


#Function to draw a bootstrap sample from a dataset
DrawBoots <- function(dataset, i){
set.seed(2394 + i)
Boot <- dataset[sample(1:nrow(dataset), size = nrow(dataset), replace = TRUE),]

#Function to grow a tree using rpart on a dataset
GrowTree <- function(x,y,BootsSample, minsplit = 40, minbucket = 20, maxdepth =3){
 controlrpart <- rpart.control(minsplit = minsplit, minbucket = minbucket,
 maxdepth = maxdepth, maxsurrogate = 0, maxcompete = 0)
 tree <- rpart(as.formula(paste(noquote(paste(y, "~")),
 noquote(paste(x, collapse="+")))), data = BootsSample,
 control = controlrpart)

#Use functions to draw 20 boostrapsamples and grow a tree on each sample
Boots<- lapply(1:10, function(k) DrawBoots( ,k))
Trees <- lapply(1:10, function (i) GrowTree(x=c("npreg", "glu",  "bp",
 "skin",  "bmi", "ped", "age"), y="type",
Boots[[i]] ))

ClusterForest<- clusterforest(,treedata=Boots,trees=Trees,m=1,
fromclus=1, toclus=5, sameobs=FALSE)

C443 documentation built on Jan. 15, 2023, 5:06 p.m.