多维缩放
Multi Dimensional Scaling
我正在尝试将 MDS 应用于基于分歧的距离矩阵(即它是 "HSAUR" 包中的 "voting" 数据集。)我正在尝试将其减少到二维并在没有 cmdscale() 函数的情况下进行绘图,但是当我尝试自己做时无法得到相同的结果。这是代码;
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)
我遵循标准的教科书符号。这是数据矩阵 Yhat 我得到:
[,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
我正在尝试将 MDS 应用于基于分歧的距离矩阵(即它是 "HSAUR" 包中的 "voting" 数据集。)我正在尝试将其减少到二维并在没有 cmdscale() 函数的情况下进行绘图,但是当我尝试自己做时无法得到相同的结果。这是代码;
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)
我遵循标准的教科书符号。这是数据矩阵 Yhat 我得到:
[,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