Description Usage Arguments Value Examples
View source: R/rclust.kmedoids.R
k-Medoids is a generally applicable clustering algorithm
as long as we have concept of dissimilarity. We adopt pam
algorithm
by cluster package. See pam
for more details.
1 | rclust.kmedoids(input, k = 2, type = c("extrinsic", "intrinsic"))
|
input |
a S3 object of |
k |
the number of clusters. |
type |
type of distance, either |
an object of class pam
. See pam
for details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | ## generate 50 points near (0,0,1) and
## 50 points near (0,0,-1) on Sphere S^2
ndata = 50
theta = seq(from=-0.99,to=0.99,length.out=ndata)*pi
tmpx = cos(theta) + rnorm(ndata,sd=0.1)
tmpy = sin(theta) + rnorm(ndata,sd=0.1)
## wrap it as 'riemdata' class
data = list()
for (i in 1:ndata){
tgt = c(tmpx[i],tmpy[i],1)
data[[i]] = tgt/sqrt(sum(tgt^2)) # project onto Sphere
}
for (i in 1:ndata){
tgt = c(tmpx[i],tmpy[i],-1)
data[[i+ndata]] = tgt/sqrt(sum(tgt^2)) # project onto Sphere
}
data = RiemBase::riemfactory(data, name="sphere")
## compare k-medoids with intrinsic distance for different k's
k2 <- rclust.kmedoids(data, k=2, type="intrinsic")
k3 <- rclust.kmedoids(data, k=3, type="intrinsic")
k4 <- rclust.kmedoids(data, k=4, type="intrinsic")
## let's visualize the results via MDS in R^2
pdist = stats::as.dist(RiemBase::rbase.pdist(data))
dat2d = stats::cmdscale(pdist, k=2)
opar <- par(no.readonly=TRUE)
par(mfrow=c(1,3))
plot(dat2d[,1], dat2d[,2], col=k2$clustering, pch=19, main="k=2")
plot(dat2d[,1], dat2d[,2], col=k3$clustering, pch=19, main="k=3")
plot(dat2d[,1], dat2d[,2], col=k4$clustering, pch=19, main="k=4")
par(opar)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.