# load data
data("banknote")
dat <- banknote[-1]
# EXAMPLE 1: generate Hierarchical Clustering settings
# ----------------------------------------------------
# wrapper for the popular stats::hclust() for Hierarchical Clustering
# Note the usee:
# of the optional arguments '...' passed to the underling clustering function
# the use of 'clust2params' to add cluster parameters to the output
hc_wrapper <- function(data, K, ...){
dm <- dist(data, method = "euclidean")
## ... = hc parameters
hc <- hclust(dm, ...)
cl <- cutree(hc, k = K)
## output with params
res <- list()
res$cluster <- cl
res$params <- clust2params(data, cluster = cl)
return(res)
}
# generate settings for Hierarchical Clustering with varying
# number of clusters K={3,4}, agglomeration method = {ward.D, median}
# see help('stats::hclust')
A <- mset_user(fname="hc_wrapper", K = c(2,3), method = c("ward.D", "complete"))
# get the setting with K=2 and method = "complete"
ma <- A[[4]]
ma
# cluster data with M[[3]]
fit_a1 <- ma$fn(dat)
fit_a1
## if only cluster parameters are needed
fit_a2 <- ma$fn(dat, only_params = TRUE)
fit_a2
if (FALSE) {
# EXAMPLE 2: generate 'mclust' model settings
# -------------------------------------------
# mclust is popular package for performing model based clustering based on
# Gaussian mixture. Please visit
# https://cran.r-project.org/web/packages/mclust/vignettes/mclust.html
require(mclust)
# wrapper for the popular stats::hclust() for Hierarchical Clustering
# Notes:
# * optional arguments '...' are passed to the underling
# 'mclust' clustering function
# * 'mclust' fits Gaussian Mixture models so cluster parameters are
# contained in the mclust object
mc_wrapper <- function(data, K, ...){
y <- Mclust(data, G = K, ...)
y[["params"]] <- list(proportion = y$parameters$pro,
mean = y$parameters$mean,
cov = y$parameters$variance$sigma)
return(y)
}
# generate 'mclust' model settings by varying the number of clusters and
# covariance matrix models (see help('mclust::mclustModelNames'))
B <- mset_user(fname = "mc_wrapper", K = c(2,3), modelNames = c("EEI", "VVV"))
# get the setting with K=3 and covariance model "EEI"
mb <- B[[2]]
mb
# cluster data with M[[3]]
fit_b <- mb$fn(dat)
fit_b ## class(fit_b) = "Mclust"
# if needed one can make sure that 'mclust' package is always available
# by setting the argument 'packages'
B <- mset_user(fname = "mc_wrapper", K = c(2,3), modelNames = c("EEI","VVV"),
packages=c("mclust"))
}
if (FALSE) {
# EXAMPLE 3: generate 'dbscan' settings
# -------------------------------------
# DBSCAN is popular nonparametric method for discovering clusters of
# arbitrary shapes with noise. The number of clusters is implicitly
# determined via two crucial tunings usually called 'eps' and 'minPts'
# See https://en.wikipedia.org/wiki/DBSCAN
require(dbscan)
# wrapper for dbscan::dbscan
db_wrap <- function(data, ...) {
cl <- dbscan(data, borderPoints = TRUE, ...)$cluster
return(params = clust2params(data, cl))
}
D <- mset_user(fname = "db_wrap", eps = c(0.5, 1), minPts=c(5,10))
md <- D[[2]]
fit_d <- md$fn(dat)
fit_d
class(fit_d)
}
Run the code above in your browser using DataLab