## multi-dimensional scaling example ## a.k.a. principal coordinates analysis ## use 'UScitiesD' data ## matrix of pairwise distances between cities ## (as the crow flies, in miles) View(as.matrix(UScitiesD)) ## do MDS to find two features mds <- cmdscale(UScitiesD, k=2) ## plot the x <- -mds[, 1] # reflect so West is left y <- -mds[, 2] # reflect so North is at the top ## note asp = 1, to ensure Euclidean distances are represented correctly plot(x, y, type = "n", xlab = "", ylab = "", asp = 1, axes=FALSE, main = "MDS of US Cities") axis(1, labels=FALSE); mtext('MDS Component 1', 1, 2) axis(2, labels=FALSE); mtext('MDS Component 2', 2, 2) points(x, y, pch=20) text(x, y, rownames(loc), cex = 0.6, xpd='NA', adj=c(0.5, -0.75)) library('ElemStatLearn') ## zipcode training data ## first column gives digit ## remaining columns (2:257) give 16x16 pixels columnwise data(zip.train) dim(zip.train) ## plot the first training example image(z=matrix(zip.train[1,-1],16,16)[,16:1], col=gray.colors(12,1,0), axes=FALSE) axis(1); axis(2); ## plot first ten sets of each digit plot_digits <- function(dat) { ## 10x10 figure, no spacing between panels, 1in outer margin par(mfrow=c(10,10), mar=rep(0,4),omi=rep(1,4)) for(i in 1:10) { ## match next 0:9 digits idx <- match(0:9, dat[,1]) ## plot each using 'image' function for(i in idx) image(z=matrix(dat[i,-1],16,16)[,16:1], col=gray.colors(12,1,0), axes=FALSE) ## remove digits that were plotted dat <- dat[-idx,] } } plot_digits(zip.train) ## use MDS to represent digits in 2 dimensions instead of 256 mds <- cmdscale(dist(zip.train[1:100,-1]), k=2) plot(mds[,1], mds[,2], type = "n", xlab = "", ylab = "", axes=FALSE, main = "MDS of Zipcodes") axis(1, labels=FALSE); mtext('MDS Component 1', 1, 2) axis(2, labels=FALSE); mtext('MDS Component 2', 2, 2) points(mds[,1], mds[,2], pch=as.character(zip.train[,1]))