MDS Usage in R

We will use the daisy() function in package cluster; isoMDS(), sammon() functions in package MASS; smacofSym() function in package smacof.

library(cluster)
library(MASS)
library(smacof)

Multidimensional Scaling

In this example, we use the Europe data from the UCI Repository of Machine Learning Databases for classification.

data("eurodist")
eurodistmatrix = as.matrix(eurodist)
eurodist2 = eurodistmatrix[c(1,3,7,8,12,14,17:21), c(1,3,7,8,12,14,17:21)]
eurodist2matrix = as.dist(eurodist2)

Perform complete linkage hierarchical clustering using the eurodist2 matrix

out.complete.euc <- hclust(eurodist2matrix,method="complete")
out.complete.euc <- cutree(out.complete.euc, k=5)
clusvec <- out.complete.euc

Classical scaling

out.Euc <- cmdscale(eurodist2matrix)
# create empty plot
plot (out.Euc)
#ensure you list enough colours for the number of clusters
colvec <- c("green","gold","blue","red","black")
for (i in 1:length(out.Euc[,1]))
  text (out.Euc[i,1],out.Euc[i,2],rownames(eurodist2)[i],col=colvec[clusvec[i]],cex=0.85)

Metric SMACOF

out.Euc <- smacofSym(eurodist2matrix,type="ratio")$conf
# create empty plot
plot (out.Euc, type="n", xlab="", ylab="", xaxt="n", yaxt="n", asp=1)
#ensure you list enough colours for the number of clusters
colvec <- c("green","gold","blue","red","black")
for (i in 1:length(out.Euc[,1]))
  text (out.Euc[i,1],out.Euc[i,2],rownames(eurodist2)[i],col=colvec[clusvec[i]],cex=0.85)

Non-metric SMACOF

out.Euc <- smacofSym(eurodist2matrix,type="ordinal")$conf
# create empty plot
plot (out.Euc, type="n", xlab="", ylab="", xaxt="n", yaxt="n", asp=1)
#ensure you list enough colours for the number of clusters
colvec <- c("green","gold","blue","red","black")
for (i in 1:length(out.Euc[,1]))
  text (out.Euc[i,1],out.Euc[i,2],rownames(eurodist2)[i],col=colvec[clusvec[i]],cex=0.85)

Sammon

out.Euc <- sammon(eurodist2matrix)$points
## Initial stress        : 0.01324
## stress after  10 iters: 0.00588, magic = 0.500
## stress after  20 iters: 0.00583, magic = 0.500
# create empty plot
plot (out.Euc, type="n", xlab="", ylab="", xaxt="n", yaxt="n", asp=1)
#ensure you list enough colours for the number of clusters
colvec <- c("green","gold","blue","red","black")
for (i in 1:length(out.Euc[,1]))
  text (out.Euc[i,1],out.Euc[i,2],rownames(eurodist2)[i],col=colvec[clusvec[i]],cex=0.85)

Kruskal’s non-metric MDS

out.Euc <- isoMDS(eurodist2matrix)$points
## initial  value 5.067772 
## final  value 5.067703 
## converged
# create empty plot
plot (out.Euc, type="n", xlab="", ylab="", xaxt="n", yaxt="n", asp=1)
#ensure you list enough colours for the number of clusters
colvec <- c("green","gold","blue","red","black")
for (i in 1:length(out.Euc[,1]))
  text (out.Euc[i,1],out.Euc[i,2],rownames(eurodist2)[i],col=colvec[clusvec[i]],cex=0.85)

For R Markdown: https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf