wants <- c("vegan")
has <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])
cities <- c("Augsburg", "Berlin", "Dresden", "Hamburg", "Hannover",
"Karlsruhe", "Kiel", "Muenchen", "Rostock", "Stuttgart")
N <- length(cities)
dstMat <- matrix(numeric(N^2), nrow=N)
## B DD HH H KA KI M HRO S
cityDst <- c(596, 467, 743, 599, 226, 838, 65, 782, 160, ## AUG
194, 288, 286, 673, 353, 585, 231, 633, ## B
477, 367, 550, 542, 465, 420, 510, ## DD
157, 623, 96, 775, 187, 665, ## HH
480, 247, 632, 330, 512, ## H
723, 298, 805, 80, ## KA
872, 206, 752, ## KI
777, 220, ## M
824) ## HRO
dstMat[upper.tri(dstMat)] <- rev(cityDst)
dstMat <- t(dstMat[ , N:1])[ , N:1]
dstMat[lower.tri(dstMat)] <- t(dstMat)[lower.tri(dstMat)]
dimnames(dstMat) <- list(city=cities, city=cities)
[,1] [,2]
Augsburg 399.60802 -70.51443
Berlin -200.20462 -183.39465
Dresden -18.47337 -213.01950
Hamburg -316.39991 130.72245
Hannover -161.38209 120.21761
Karlsruhe 333.38724 212.12523
Kiel -409.08703 147.62226
Muenchen 408.55752 -144.46446
Rostock -401.19605 -126.20651
Stuttgart 365.19030 126.91200
set.seed(123)
P <- 3
obj <- matrix(sample(-20:20, N*P, replace=TRUE), ncol=P)
dst <- dist(obj, diag=TRUE, upper=TRUE)
cmdscale(dst, k=2)
# not shown
xLims <- range(mds[ , 1]) + c(0, 250)
plot(mds, xlim=xLims, xlab="North-South", ylab="East-West", pch=16,
main="City locations according to MDS")
text(mds[ , 1]+50, mds[ , 2], adj=0, labels=cities)
Call:
monoMDS(dist = dstMat, k = 2)
Non-metric Multidimensional Scaling
10 points, dissimilarity 'unknown'
Dimensions: 2
Stress: 0.06803376
Stress type 1, weak ties
Scores scaled to unit root mean square, rotated to principal components
Stopped after 55 iterations: Stress nearly unchanged (ratio > sratmax)
MDS1 MDS2
Augsburg -1.20797386 -0.01719197
Berlin 0.59341232 -0.41182940
Dresden 0.09538295 -0.73450992
Hamburg 0.85903495 0.30137581
Hannover 0.49243793 0.26880268
Karlsruhe -1.00749610 -0.18900784
Kiel 1.16050702 0.01464720
Muenchen -1.17092083 0.34936915
Rostock 1.17197472 0.23940430
Stuttgart -0.98635909 0.17894000
attr(,"pc")
[1] TRUE
R markdown - markdown - R code - all posts