# Create functional data with two groups
t <- seq(0, 1, length.out = 50)
n <- 30
X <- matrix(0, n, 50)
true_cluster <- rep(1:2, each = 15)
for (i in 1:n) {
if (true_cluster[i] == 1) {
X[i, ] <- sin(2*pi*t) + rnorm(50, sd = 0.1)
} else {
X[i, ] <- cos(2*pi*t) + rnorm(50, sd = 0.1)
}
}
fd <- fdata(X, argvals = t)
# Cluster with string metric (fast Rust path)
result <- cluster.kmeans(fd, ncl = 2, metric = "L2")
table(result$cluster, true_cluster)
# Cluster with metric function (also fast - Rust backend)
result2 <- cluster.kmeans(fd, ncl = 2, metric = metric.lp)
# Cluster with semimetric (flexible but slower)
result3 <- cluster.kmeans(fd, ncl = 2, metric = semimetric.pca, ncomp = 3)
Run the code above in your browser using DataLab