Wednesday, October 06, 2010

Metric MDS starting from eigen()

this is an exercise to figure the details of MDS, or more specifically, what the coordinates are that are used in plotting. More explanations can be found here.


R function cmdscale() works as follows.

require(graphics)

loc = cmdscale(eurodist)
x = loc[,1]
y = -loc[,2]
plot(x, y, type="n", xlab="", ylab="", main="cmdscale(eurodist)")
text(x, y, rownames(loc), cex=0.8)


Here is the procedure to achieve the same without using the function directly, following the notation in Applied Multivariate Statistical Analysis.


require(graphics)

# get the centering matrix, H
n = 21
one = rep(1, times=n)
h = diag(1,nrow=n,ncol=n) - outer(one,one)/n 

# get matrix A
d2 = as.matrix(eurodist)
d2 = d2**2/(-2)

# get matrix B
b = h %*% d2 %*% h

ei = eigen(b)
vec = ei$'vectors'
lamda = ei$'values'

par(mfrow=c(1,2))

plot(vec[,1], -1* vec[,2], type="n", xlab="", ylab="", main="MDR using eigen")
text(vec[,1], -1* vec[,2],labels(eurodist), cex=0.8)

loc = cmdscale(eurodist)
x = loc[,1]
y = -loc[,2]
plot(x, y, type="n", xlab="", ylab="", main="cmdscale(eurodist)")
text(x, y, rownames(loc), cex=0.8)

No comments: