# Completing MDS manually in R

Given a matrix A, I want to complete Multidimensional Scaling by hand, instead of using any given R functions.

As such, I have calculated the centered matrix B with the following code:

 n<-nrow(A)
id<-diag(n)
e<-diag(id)
H <- id - (1/n)*e %*% etranspose
B <- (-1/2)* H %*% A %*% H


My question is: how can I use my B matrix to complete multidimensional scaling on my A matrix, without the cmdscale function or anything along those lines?

If you're interested in the classic MDS algorithm, it is spelled out very nicely on the Wikipedia page:

Classical MDS uses the fact that the coordinate matrix can be derived by eigenvalue decomposition from $$B=XX'$$. And the matrix $$B$$ can be computed from proximity matrix $$D$$ by using double centering.

1. Set up the squared proximity matrix $$D^{(2)}=[d_{ij}^{2}]$$
2. Apply double centering: $$B=-{\frac {1}{2}}JD^{(2)}J$$ using the centering matrix $$J=I-{\frac {1}{n}}11'$$, where $$n$$ is the number of objects.

3. Determine the $$m$$ largest eigenvalues $$\lambda _{1},\lambda _{2},...,\lambda _{m}$$ and corresponding eigenvectors $$e_{1},e_{2},...,e_{m}$$ of $$B$$ (where $$m$$ is the number of dimensions desired for the output).

4. Now, $$X=E_{m}\Lambda _{m}^{1/2}$$, where $$E_{m}$$ is the matrix of $$m$$ eigenvectors and $$\Lambda _{m}$$ is the diagonal matrix of $$m$$ eigenvalues of $$B$$.

I can't really tell what your $$A$$ matrix is, but if each entry is a measure of the distance from the $$i^{th}$$ to $$j^{th}$$ entry, then that would be your proximity matrix.

Also, $$11'$$ is an n-by-n matrix of all 1's. This should be fairly self-explanatory, however most of these terms can be Googled if you're unsure. It really shouldn't be any more than about 10 lines of code.