我试图将MDS应用于基于分歧的距离矩阵(即它是"HSAUR"包中的"投票"数据集)。我试图将其减少到2维,并在没有cmmdscale()函数的情况下进行绘图,但当我尝试自己做时,无法获得相同的结果。下面是代码;
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)
我遵循标准的教科书符号。这是数据矩阵我得到的:
[,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
与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
它们似乎是相关的,但我不明白是什么导致了不同的结果。我很高兴能得到对代码的更正。
这只是操作符优先级的问题:您需要更改行:
Yhat = E[,1:2]%*%Lambda[1:2,1:2]^1/2 # it's computing half of the dominant eigenvalues matrix
Yhat = E[,1:2]%*%Lambda[1:2,1:2]^(1/2) # take square-root of the dominant eigenvalues matrix
然后你会得到和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