# NOT RUN {
# Below vector clearly has 2 groups.
# kluster will identify these groups using kernel density estimation.
kluster(c(0.1, 0.2, 1))
# kluster shines in cases where manually assigning groups via "eyeballing" is impractical.
# Suppose we obtained vector 'x' without knowing how it was generated.
set.seed(1)
nodes <- runif(10, min = 0, max = 100)
x <- lapply(nodes, function(x) rnorm(10, mean = x, sd = 0.1))
x <- unlist(x)
kluster(x) # kluster reveals the natural grouping
kluster(x, bw = 10) # adjust bandwidth depending on application
# Example with faithful dataset
faithful$k <- kluster(faithful$eruptions)
library(ggplot2)
ggplot(faithful, aes(eruptions)) +
geom_density() +
geom_rug(aes(color = factor(k))) +
theme_minimal() +
scale_color_discrete(name = "k")
# }
Run the code above in your browser using DataLab