Unfortunately, our intellectual abilities as humans, even though quite superior to other creatures, are still extremely limited. If we were asked to imagine the craziest version of the universe, our ideas would be nothing more than a random collection of things that we’ve already seen/known, nothing new. We do not even need to go that far, we cannot even imagine things in more than three dimensions.

But just like our ancestors managed to survive the harsh realities of the past, we(the humankind) found a workaround for this matter as well.One of those workarounds is “Multidimensional Scaling”(MDS).

It is a statistical technique used to plot multivariate data points into two dimensions, giving us an idea about the structure of the dataset & the relations(dissimilarities/distance) between the datapoints. Hence, if the observations are similar, they’ll be close to each other and vice-versa.

It is widely used in geography, in creating maps, that is why to explain how MDS works I decided to try and recreate a map.

For that I will need only the “wordcloud” package, as the MDS(cmdscale) function that i will need is already included in the base package “stats”

## Loading required package: RColorBrewer

To make it a bit more interesting, I decided to recreate a map of the Westeros(Game Of Thrones) and check, weather the distances mentioned in the books and the show add up to the map shown in the intro.:)

Fortunately for me, Game of Thrones has a huge fanbase that have alredy created a distance matrix with the main locations on asoiaf.westeros.org spreadsheet:

got <- read.csv2('gotmap.csv', header = TRUE, row.names = 1)
got
##               CastleBlack Winterfell WhiteHarbor MoatCailin TheTwins TheEyrie
## CastleBlack             0        680        1105       1080     1610     2650
## Winterfell            680          0         490        590     1120     1970
## WhiteHarbor          1105        490           0        180      710     1460
## MoatCailin           1080        590         180          0      530     1280
## TheTwins             1610       1120         710        530        0      930
## TheEyrie             2650       1970        1460       1280      930        0
## Gulltown             2970       2290        1880       1700     1350      450
## KneelingMan          2175       1495        1185        905      375      680
## TheTrident           2040       1360         950        770      420      545
## Riverrun             2295       1615        1205       1025      495      740
## HarrowaysTown        2240       1560        1150        970      555      440
## Harrenhal            2335       1655        1245       1065      615      535
## StoneySept           2515       1835        1525       1245      715      805
## Lannisport           2885       2205        1795       1615     1085     1330
## KingsLanding         2690       2010        1600       1420     1070      990
## Bitterbridge         2965       2285        1875       1695     1125     1165
## StormsEnd            3170       2490        2080       1900     1350     1370
## Summerhall           3150       2470        2060       1880     1555     1350
## Ashford              3185       2505        2095       1915     1345     1385
## Highgarden           3630       2950        2540       2360     1740     1830
## TowerofJoy           3365       2685        2275       2095     1525     1565
## Oldtown              3750       3070        2660       2480     1910     1950
## Nightsong            3395       2715        2305       2125     1555     1595
## Sunspear             4565       3885        3475       3295     2725     2765
##               Gulltown KneelingMan TheTrident Riverrun HarrowaysTown Harrenhal
## CastleBlack       2970        2175       2040     2295          2240      2335
## Winterfell        2290        1495       1360     1615          1560      1655
## WhiteHarbor       1880        1185        950     1205          1150      1245
## MoatCailin        1700         905        770     1025           970      1065
## TheTwins          1350         375        420      495           555       615
## TheEyrie           450         680        545      740           440       535
## Gulltown             0         930        830     1050           750       845
## KneelingMan        930           0        135      120           180       240
## TheTrident         830         135          0      255           100       195
## Riverrun          1050         120        255        0           300       285
## HarrowaysTown      750         180        100      300             0        95
## Harrenhal          845         240        195      285            95         0
## StoneySept        1115         340        475      260           365       270
## Lannisport        1640         710        845      590           890       795
## KingsLanding      1200         630        550      750           450       380
## Bitterbridge      1475         750        825      670           725       630
## StormsEnd         1680        1110       1030     1230           930       860
## Summerhall        1660        1180       1010     1100           910       840
## Ashford           1695         970       1045      890           945       850
## Highgarden        2140        1365       1490     1285          1390      1295
## TowerofJoy        1875        1150       1225     1070          1125      1030
## Oldtown           2260        1535       1610     1455          1510      1415
## Nightsong         1905        1180       1255     1100          1155      1060
## Sunspear          3075        2350       2425     2270          2325      2230
##               StoneySept Lannisport KingsLanding Bitterbridge StormsEnd
## CastleBlack         2515       2885         2690         2965      3170
## Winterfell          1835       2205         2010         2285      2490
## WhiteHarbor         1525       1795         1600         1875      2080
## MoatCailin          1245       1615         1420         1695      1900
## TheTwins             715       1085         1070         1125      1350
## TheEyrie             805       1330          990         1165      1370
## Gulltown            1115       1640         1200         1475      1680
## KneelingMan          340        710          630          750      1110
## TheTrident           475        845          550          825      1030
## Riverrun             260        590          750          670      1230
## HarrowaysTown        365        890          450          725       930
## Harrenhal            270        795          380          630       860
## StoneySept             0        650          470          410       950
## Lannisport           650          0          990          730      1470
## KingsLanding         470        990            0          520       480
## Bitterbridge         410        730          520            0       730
## StormsEnd            950       1470          480          730         0
## Summerhall           840       1210          460          410       350
## Ashford              630        950          650          220       650
## Highgarden          1025        730          915          395       990
## TowerofJoy           810       1100          755          400       645
## Oldtown             1195       1120         1305          785      1380
## Nightsong            840       1015          885          430       775
## Sunspear            2010       2185         2055         1600      1945
##               Summerhall Ashford Highgarden TowerofJoy Oldtown Nightsong
## CastleBlack         3150    3185       3630       3365    3750      3395
## Winterfell          2470    2505       2950       2685    3070      2715
## WhiteHarbor         2060    2095       2540       2275    2660      2305
## MoatCailin          1880    1915       2360       2095    2480      2125
## TheTwins            1555    1345       1740       1525    1910      1555
## TheEyrie            1350    1385       1830       1565    1950      1595
## Gulltown            1660    1695       2140       1875    2260      1905
## KneelingMan         1180     970       1365       1150    1535      1180
## TheTrident          1010    1045       1490       1225    1610      1255
## Riverrun            1100     890       1285       1070    1455      1100
## HarrowaysTown        910     945       1390       1125    1510      1155
## Harrenhal            840     850       1295       1030    1415      1060
## StoneySept           840     630       1025        810    1195       840
## Lannisport          1210     950        730       1100    1120      1015
## KingsLanding         460     650        915        755    1305       885
## Bitterbridge         410     220        395        400     785       430
## StormsEnd            350     650        990        645    1380       775
## Summerhall             0     300        640        295    1030       425
## Ashford              300       0        340        180     730       210
## Highgarden           640     340          0        370     390       285
## TowerofJoy           295     180        370          0     760       130
## Oldtown             1030     730        390        760       0       675
## Nightsong            425     210        285        130     675         0
## Sunspear            1595    1380       1455       1300    1845      1170
##               Sunspear
## CastleBlack       4565
## Winterfell        3885
## WhiteHarbor       3475
## MoatCailin        3295
## TheTwins          2725
## TheEyrie          2765
## Gulltown          3075
## KneelingMan       2350
## TheTrident        2425
## Riverrun          2270
## HarrowaysTown     2325
## Harrenhal         2230
## StoneySept        2010
## Lannisport        2185
## KingsLanding      2055
## Bitterbridge      1600
## StormsEnd         1945
## Summerhall        1595
## Ashford           1380
## Highgarden        1455
## TowerofJoy        1300
## Oldtown           1845
## Nightsong         1170
## Sunspear             0

Now that we have the distance matrix,we will execute the simplest version of the MDS funtions available - cmdscale() also known as the principal coordinates analysis, which will perform the eigen-decomposition of the matrix above and will give us the eigenvectors that we can later plot into a map:

mds <- as.data.frame(cmdscale(got))
mds
##                        V1          V2
## CastleBlack   -2483.13521   630.90508
## Winterfell    -1779.72337   360.22798
## WhiteHarbor   -1367.38325   176.18218
## MoatCailin    -1215.25999   153.84275
## TheTwins       -668.53358   148.36160
## TheEyrie       -279.03859  -849.49698
## Gulltown       -274.60262 -1380.37007
## KneelingMan    -260.34798   -87.34328
## TheTrident     -360.34946  -113.16542
## Riverrun       -170.06636   -44.08446
## HarrowaysTown  -217.87059  -204.34004
## Harrenhal      -121.39511  -169.18892
## StoneySept       81.99725   -27.27818
## Lannisport      260.69970    68.90649
## KingsLanding    191.64518  -104.35596
## Bitterbridge    521.53225    70.64874
## StormsEnd       562.40317   -94.82452
## Summerhall      670.22199    48.04302
## Ashford         737.23067   136.25348
## Highgarden     1140.15980   214.72837
## TowerofJoy      902.72741   171.32107
## Oldtown        1160.07722   132.96142
## Nightsong       944.92148   208.95578
## Sunspear       2024.08997   553.10986
textplot(mds$V1,mds$V2, rownames(mds))

Now that we have our map, we can compare it to the one available in the internet. I chose the one that is most readable, though some titles are missing there we can compare a couple of the main ones. Westeros

We can rotate our plot to make it easier to compare:

textplot(-mds$V2, -mds$V1,rownames(mds), xlim = c(-1000, 1500), ylim = c(-2500, 2500))

As we can see, the map turned out to be quite okay when it comes to the relative locations of the houses.

Which is quite impressive, as the data was collected by the fans from the distance references in the books such as the time that characters/ravens spent travelling from one place to another. Shows how the books are well-thought out to the very minor detail and how we could use the MDS in our day to day life, in case we ever have trouble imagining our favorite fiction universe.:)