max loglkd; min ICs
1 2 3 4 5 6 7 8 | sp.mixnorm(
x,
k = 2,
init = c("kmeans", "random"),
maxiter = 496,
same.lambda = TRUE,
version = c("soft", "hard", "stochastic")
)
|
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | ## generate two-cluster data
mymu1 = c(0,0,0,1) # center of class 1
mymu2 = c(-1,0,0,0) # center of class 2
mymu3 = c(0,1/sqrt(2),-1/sqrt(2),0) # class 3
x1 = rvmf(50, mymu1, kappa=10)
x2 = rvmf(50, mymu2, kappa=15)
x3 = rvmf(50, mymu3, kappa=20)
xx = rbind(x1,x2,x3)
## apply clustering with different k values
mix2 <- sp.mixnorm(xx, k=2)
mix3 <- sp.mixnorm(xx, k=3)
mix4 <- sp.mixnorm(xx, k=4)
## compute 2-dimensional embedding for visualization
mds2d <- sp.mds(xx, ndim=2)
x <- mds2d$embed[,1]
y <- mds2d$embed[,2]
## compare via visualization
opar <- par(mfrow=c(1,3),pty="s")
plot(x,y,col=mix2$cluster,main="k=2 mixture",pch=19)
plot(x,y,col=mix3$cluster,main="k=2 mixture",pch=19)
plot(x,y,col=mix4$cluster,main="k=2 mixture",pch=19)
par(opar)
## extra visualization
## Not run:
# run more models
mix5 <- sp.mixnorm(xx, k=5)
mix6 <- sp.mixnorm(xx, k=6)
mix7 <- sp.mixnorm(xx, k=7)
mix8 <- sp.mixnorm(xx, k=8)
# information criteria
xmat <- 2:8
ymat <- rbind(mix2$criteria, mix3$criteria, mix4$criteria, mix5$criteria,
mix6$criteria, mix7$criteria, mix8$criteria)
colnames(ymat) = colnames(mix2$criteria)
# plot with x11()
x11(width=12, height=6)
par(mfrow=c(2,4), pty="s")
plot(x, y, col=rainbow(8)[mix2$cluster], main="k=2 mixture", pch=19)
plot(x, y, col=rainbow(8)[mix3$cluster], main="k=3 mixture", pch=19)
plot(x, y, col=rainbow(8)[mix4$cluster], main="k=4 mixture", pch=19)
plot(x, y, col=rainbow(8)[mix5$cluster], main="k=5 mixture", pch=19)
plot(x, y, col=rainbow(8)[mix6$cluster], main="k=6 mixture", pch=19)
plot(x, y, col=rainbow(8)[mix7$cluster], main="k=7 mixture", pch=19)
plot(x, y, col=rainbow(8)[mix8$cluster], main="k=8 mixture", pch=19)
matplot(xmat, ymat, type="b", lwd=2, main="Info. Criteria",
xlab="# clusters", col=1:4, lty=1:4, pch=1)
legend("topright", legend = colnames(ymat), col = 1:4, lty=1:4, pch=1)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.