rdata-miningmdsmulti-dimensional-scaling

Multi Dimensional Scaling


I'm trying to apply MDS to a distance matrix based on disagreements (namely it is the "voting" dataset in the "HSAUR" package.) I'm trying to reduce it to 2 dimensions and plot without the cmdscale() function, but can't get the same result when I try to do it myself. Here is the code;

library(HSAUR)

n <- 15

deltaD = voting
deltaDstar = deltaD^2

I = matrix(0,n,n)
diag(I) <- 1

J = matrix(1,n,n)

H = I-n^-1*J

Q = -0.5*H%*%deltaDstar%*%H

reseigen = eigen(Q)
lambda = reseigen$values
E = reseigen$vectors
Lambda = matrix(0,n,n)
diag(Lambda) <- lambda

Yhat = E[,1:2]%*%Lambda[1:2,1:2]^1/2
Yhat

x1 <- Yhat[,1]
x2 <- Yhat[,2]

plot(x1, x2, type = "n", xlim=c(-10,5), ylim=c(-6,8), xlab = "Coordinate 1", 
ylab = "Coordinate 2", asp=1)

text(x1, x2, rownames(deltaD), cex = 0.6)

I'm following the standart textbook notation. This is the data matrix Yhat I get:

            [,1]        [,2]
 [1,] -102.227945   0.1306901
 [2,]  -93.369153  46.4283081
 [3,]   62.778582  -1.6069442
 [4,]   30.708488  39.6033985
 [5,]  -59.614466 -17.4749816
 [6,]  -41.422942 -21.1382075
 [7,]  -94.185208  -5.0311437
 [8,]   63.513501  -1.3529431
 [9,]   72.856275  -0.3352204
[10,]   49.323040  -0.1241045
[11,]   54.595017  -4.7480531
[12,]   67.283718  -4.3435477
[13,]  -53.094269 -28.0575071
[14,]    2.341299  -2.5952789
[15,]   40.514064   0.6455349

Compared to the one from cmdscale():

                        [,1]        [,2]
Hunt(R)           -9.1640883  0.02161894
Sandman(R)        -8.3699537  7.68023459
Howard(D)          5.6277025 -0.26582292
Thompson(D)        2.7528216  6.55124865
Freylinghuysen(R) -5.3440596 -2.89073549
Forsythe(R)       -3.7133046 -3.49671135
Widnall(R)        -8.4431079 -0.83225871
Roe(D)             5.6935834 -0.22380571
Heltoski(D)        6.5311040 -0.05545261
Rodino(D)          4.4214984 -0.02052953
Minish(D)          4.8940977 -0.78542948
Rinaldo(R)         6.0315595 -0.71851563
Maraziti(R)       -4.7595652 -4.64131141
Daniels(D)         0.2098827 -0.42931460
Patten(D)          3.6318295  0.10678526

They seem correlated but I don't understand what causes the different results. I would be glad to get a correction on the code. Thanks a lot in advance.


Solution

  • It's just about operator precedence: you need to change the line:

    Yhat = E[,1:2]%*%Lambda[1:2,1:2]^1/2 # it's computing half of the dominant eigenvalues matrix
    

    to

    Yhat = E[,1:2]%*%Lambda[1:2,1:2]^(1/2) # take square-root of the dominant  eigenvalues matrix
    

    and then you get exactly same results as cmdscale:

    Yhat
                [,1]        [,2]
     [1,] -9.1640883  0.02161894
     [2,] -8.3699537  7.68023459
     [3,]  5.6277025 -0.26582292
     [4,]  2.7528216  6.55124865
     [5,] -5.3440596 -2.89073549
     [6,] -3.7133046 -3.49671135
     [7,] -8.4431079 -0.83225871
     [8,]  5.6935834 -0.22380571
     [9,]  6.5311040 -0.05545261
    [10,]  4.4214984 -0.02052953
    [11,]  4.8940977 -0.78542948
    [12,]  6.0315595 -0.71851563
    [13,] -4.7595652 -4.64131141
    [14,]  0.2098827 -0.42931460
    [15,]  3.6318295  0.10678526