--- title: "Distance, Similarity, and Multidimensional Scaling, Self-Organizing Maps" author: "Shane T. Mueller shanem@mtu.edu" date: "`r Sys.Date()`" output: rmdformats::readthedown: gallery: yes highlight: kate self_contained: no pdf_document: default html_document: df_print: paged word_document: reference_docx: ../template.docx always_allow_html: yes --- ```{r knitr_init, echo=FALSE, cache=FALSE} library(knitr) library(rmdformats) ## Global options options(max.print="75") opts_chunk$set(echo=TRUE, cache=TRUE, prompt=FALSE, tidy=TRUE, comment=NA, message=FALSE, warning=FALSE) opts_knit$set(width=75) ``` ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` Multidimensional scaling (MDS) is used to map objects into a space based on measures of the (dis)similarity or distance between examples. In other words, MDS is a dimension-reduction algorithm to discover the underlying structure of a space, based on distance measures between objects or cases. The goal is to place observations in a space based on a (dis)similarity scores between them, rather than on features themselves. This seems a little like eigen decomposition, Principal Components Analysis (PCA) or factor analysis, some of which we have already covered and other parts of which we will cover in greater detail later in this course. However, there is one major difference. MDS starts with the similarities, dissimilarities, or distances, while PCA and the like start with a feature representation. Thus, for PCA, you already have everything in a high-dimensional space, and you want to map it into a lower dimensional one. For MDS, you don't really have a space; just a measure of similarity, and you want to induce the space from that. For the most part, scaling is used in psychological and perceptual applications, and is a very useful visualization technique. That is, we very often have multiple dimensions we can assess things on, and if we do, MDS is generally a round-about and probably inexact way of doing factor analysis. But in some cases, we can only measure the similarity or distance between examples. In these cases MDS is useful, especially for visualization, identifying significant factors and dimensions. As such, MDS can be used to infer features/dimensions when you don't have them. # Similarity, Distance and Dissimilarity Measures The input for MDS is something that behaves like a distance measure between all cases, in the form of a matrix. That is, for N items, you have an NxN matrix where each entry specifies a non-negative distance between items. Mathematically, if we are treating a measure as a distance, we are asserting that our measures have metric properties: * ``Identity''. The distance between something and itself is 0. Alternately, two things that have a distance measure of 0 are identical. * ``Symmetry''. The distance between A and B is the same as the distance between B and A. * ``Triangle Inequality''. The distance between A and C must be less than or equal to the distance between A and B plus the distance between B and C. These are axioms of distance--assumptions that completely define what a distance is. These place strong constraints on our measures. Many times, we might treat dissimilarity measures as a distance even when it violates one or more of these properties. In fact, most psychological measures will violate at least one of these. These include most direct and indirect measures of similarity. For example, you may directly measure similarity by asking someone to give a rating of the similarity between two objects; or you might infer their similarity by timing how long it takes to choose between them. In the ratings, case, we might not get symmetry--the rating might depend on which option was given first. The RT case we might not get identity--it takes time to identify whether something is the same as itself. Even if we subtract RT for same judgments so that we force the identity property, we might then find that other judgments are faster, and thus negative, which is also a violation of assumptions that distances are positive values. Although if we had features or a set of numeric predictors, we might use other approaches to map them into smaller dimensions, it is useful to understand how we might map between distance and spatial representation. There are a number of ways to compute distance measures based on numeric features/dimensions. The following are all true distance metrics insofar as they conform to these three axioms, and they are all available within the R dist function (and they are easy to calculate by hand). 1. Euclidean Distance. This is sometimes referred to as an $L_2$ norm, and is a generalization of the Pythagorean formula to multiple dimensions. For observations $a$ and $b$ measured on many dimensions, this is $\sqrt{\sum_i((a_i - b_i)^2)}$. Note that even if you use scaling, normalizing, or weighting of dimensions, a distance metric will still result. This is a good default distance measure to use if it makes sense to combine dimensions. 2. Manhattan or city-block Distance. This is known as the $L_1$ norm, and is the measure of the sum of the distances along each dimension. For a 2-feature space, it is like walking along streets, rather than as the crow flies. This may be better for categorically distinct features, especially when they are all binary. If they are all binary, this is identical to the Hamming distance--the sum of the differences on each dimension`. 3. Maximum. This is the largest distance on any one dimensions. This makes sense when the largest difference is the critical predictor. In situations where it doesn't matter how many aspects match, but rather if there are any big mismatches, this would be a good measure. For many decisions, you might care about worst-case scenarios, and so largest mismatch could be a good measure of how similar you think two cases are. 4. Asymmetric Binary. Useful for binary feature representations in which presence and absence of a feature is not arbitrary. This measures the proportion of features that mismatch, in which at least one feature is positive. Despite its name, this measure does not violate the symmetry axiom. 5. Minkowski Distance. This is similar to the 'softmax' rule, and is also known as the p norm--the pth root of the sum of the pth powers of the differences of the components. You can specify p, and as p=1, this is the manhattan; as p=2, this is euclidean, as p -> inf this becomes max. 6. Canberra: This is the absolute difference divided by the sum of the values. This is useful when features are non-negative magnitudes such as counts. $\sum_i{(|x_i - y_i| / |x_i + y_i|)}$. Terms with zero numerator and denominator are omitted from the sum and treated as if the values were missing. These are all ways in which a set of features or predictors can be transformed into a distance measure. Oftentimes, you might find that although you have collected dozens or hundreds of features, once you calculate all the pairwise differences, the resulting structure can easily be embedded within a smaller-dimensional space. If so, knowing where the items are within that space can be useful to invert this distance-calculation process. Although these metrics allow you to go from dimensions to distance, sometimes you may only be able to compute a similarity or distance measure, without knowing the underlying features or dimensions. For example: * You can judge the similarity of concepts, words, products, ideas, etc., but do not have an easy way of representing their features * You may have incidental data like mistakes, errors, or discrimination times, that can imply a similarity space, but not a feature space * In competitive games, you make have good measures of which teams/players are about as good as each other, but not what makes them good. So, there are many situations in which we may have distance-like measures between pairs of things (similarity or dissimilarity, confusion, etc.). In these cases, we'd sometimes like to be able to reverse the distance formula; getting the coordinates out by putting the distances in. This is what MDS provides solutions for. Before we go into it, let's consider the differences between similarity and distance. # Similarity versus Distance Similarity is the opposite of dissimilarity, which is can be interpreted as a distance. However, the notion of dissimilarity does not require satisfying the same metric axioms. For example, similarity/dissimilarity does not need to define what the identity is--what it means to be identical. Similarity measures do not need to be symmetric. Many ways in which similarity is measured produce asymmetric values (see Tversky, 1975). Finally, similarity can violate the triangle inequality. For example, people might agree that North Korea is similar to South Korea, and that North Korea is also similar to Iran, but feel that South Korea and Iran are very different. If these were distances, it would be faster to go from South Korea to Iran through North Korea than directly, which violates spatial axioms implicit in the triangle inequality. There are many ways of transforming similarities to dissimilarities, and treat them as distances. One way is to subtract from the $max+\delta$, where $\delta$ might be 0 if the maximum value is obtained by comparing an item to itself. In general, you may have to add an additive factor to a dissimilarity score for it to work with some of the scaling algorithms we will look at. Another common way is to use a formula like this: $distance_j = exp({-\gamma x_j})/\sum_i{exp({ -\gamma x_i})}$. This operates a little like Minkowski distance, where $\gamma$ will pick out a range of measures between min to mean to max. ```{r} simtodist <- function(sims,gamma=1) { base <- exp(-gamma*sims) base/sum(base) } print(round(simtodist(c(3,5,10,2,0),gamma=1),3)) ##low similarity becomes large distance print(round(simtodist(c(3,5,10,2,0),gamma=10),3)) ##max vs 0 print(round(simtodist(c(3,5,10,2,0), gamma=.001),3)) ##equal weight print(round(simtodist(c(3,5,10,2,0), gamma=-1),3)) ##min rule; no longer a distance. ``` This formula gives us a flexible way of transforming a measure of similarity to a measure of dissimilarity, which might be treated as a distance in an MDS algorithm. There is a function within the ```smacof``` library called ```sim2diss```, which could be also be used. It has many methods, but defaults to 'corr' which is only appropriate if you have a correlation matrix, but has about a dozen different principled ways of creating dissimilarity data from similarity data. The reverse and exp methods are essentially what we did above, but there are others as well. Notice that each of these transforms are essentially identical except for scale, although the d3 is curved, but monotonically related to the original. ```{r} library(smacof) sim2diss(c(3,5,10,2,0),method="exp") ``` # Multi-dimensional Scaling For any set of objects, ideas, concepts, people, etc., we may have a set of pairwise measures that we can interpret as a distance metric--possibly from a high-dimensional set of independent measures (e.g., features or predictors), or with some sort of data that is interpretable directly as a distance. For many types of concepts or objects, we may only be able to make comparisons between pairs, because they may not fall nicely on dimensions. Think about the similarity of words, sounds, paths, shapes, and other complex things. Think about teams in head-to-head competition. Let's suppose we knew the only the pairwise distances between locations in a 2-dimensional plane. How might we reconstruct the configuration? This is not too different from how GPS triangulation works. Essentially, we have $N*2$ values but $N^(N-1)/2$ data points, and it should be possible to take each triple of points and determine their relative locations, eventually reconstructing the grid. But what if there were errors in measurement, noise, or space had more than two dimensions (or was somehow curved or used a different measure than we are considering? This would be more difficult, because we would never be able to completely trust the location of each point. In these cases and others, a set of methods called Multi-Dimensional Scaling (MDS) can be thought of as a way of creating a feature-based or spatial representation from similarity data. There are several related algorithms that are used to do this. Simple MDS uses eigen decomposition of the distance measure. More advanced MDS is typically implemented as an iterative process that essentially does triangulation until the points are each in balance in a dimensional space. These are sort of like point connected by springs the size of the measured distance. If there is little error in the distances, everything will align correctly. If there is error, some springs will be under greater tension than others. And if you try to take a 3-dimensional object and compress it by flattening it, you might expect a lot of the springs to be stretched or compressed. The basic amount of stretch the system is under is referred to as 'stress' or 'strain' in MDS lingo, and is a measure of error. If a high-dimensional system can easily be compressed into a lower-dimensional representation (like layouts of cities on a section of the globe), it will have low stress and be well approximated by a 2-dimensional solution. The stress is also thus a measure of goodness-of-fit, and con be used heuristically to assess how appropriate the solution is. ## Classical Metric MDS. Classical MDS is best applied to metric variables. Torgerson (1958) initially developed this method. It assumes that the data obey distance axioms--they are like a proximity or distance matrix on a map. It uses eigendecomposition of the distance to identify major components and axes, and represents any point as a linear combination of dimensions. Note that this is very similar to PCA or factor analysis, but it uses the distance matrix rather than a correlation matrix as input. Furthermore, its dimensions will be the most important dimensions induced. That is, PCA is able to identify as many dimensions as exist in our original data (up to $N-1$), but will retain only the most important ones. ### Example: Random points Look what happens when we choose 6 points on a plane, compute their distances, and then apply classic MDS via the ```cmdscale``` function: ```{r} library(MASS) dummydata <- cbind(runif(6),runif(6))*10 dist(dummydata) par(mfrow=c(1,2)) plot((dummydata),pch=paste(1:6,"",sep=""),type="o") d2 <- cmdscale(dist(dummydata), eig = TRUE) plot(d2$points,pch=paste(1:6,"",sep=""),type="o") ``` Notice that the absolute values are not recovered in terms of the dimensions or the scale, but the relative positions are. Here the solution could be rotated, reflected, and it is centered around 0,0. Next, notice that the x axis has a size of about 9, whereas the y axis has a size of about 4. This has automatically rotated to the 'principal' components. The original data was two dimensional, and we can recover them almost perfectly. But if the data had some noise or more dimensions, we could only get an approximate solution in 2 dimensions, and the rest would be ignored. ### Example: Distances between European Cities The ```eurodist``` data is a matrix of *road* distances in km between major cities in Europe. We analyze this matrix, specifying that to reproduce the distance based on two dimensions. As a result we can have a two-dimensional representation of the locations of the cities. ```{r, echo=FALSE} data(eurodist) eurodist euro.mds <- cmdscale(eurodist) euro.mds plot(euro.mds[,1],-euro.mds[,2]) text(euro.mds[,1],-euro.mds[,2],rownames(euro.mds)) ``` This clearly recreates the geography of Europe, albiet the north-south axis is flipped. Because of the layout of the cities, it sort of accidentally recovers north-south accurately as the second dimension. The distances at this scale are impacted by the curvature of the earth, but the method finds a 2D embedding just like a map projection. If you would like the full output, including Goodness-of-fit (GOF); add the 'eig = TRUE' statement. ```{r} eur.mds <- cmdscale(eurodist, eig = TRUE) eur.mds plot(eur.mds$eig) abline(0,0) ``` The ```$eig``` argument gives us how much variance is contributed by each of 21 dimensions the solution produces. We can see that the first two capture almost everything. However, these don't capture everything, for a couple reasons. First, this was road distances, which means that there will be additional distance for many of the pairs, and these will not be a constant. Second, even if we had exact distances, the curvature of the earth would require at least one extra dimension to incorporate. But nevertheless, a third dimension looks unneccessary. # Assign names (dimension numbers) to the result vectors. MDS attempts to arrange "objects" (cities) in a space with a particular number of dimensions. As a result of the MDS analysis, we would explain the distances in terms of underlying dimensions. In this case, we could explain distances in terms of the two geographical dimensions:north/south and west east. The axes are not exactly aligned with the cardinal directions (once dim2 is reversed), but it is close. Notice how the general distances are preserved. We can tell that Gibralter to Athens is about 4000 km, and that turns out to be true. ```{r, fig.width=10,fig.height=7} Dim1 <- euro.mds [,1] Dim2 <- euro.mds [,2] library(ggrepel) # Plot the solution. plot(Dim1, -Dim2, type="n", xlab="", ylab="", main="cmdscale(eurodist)") text(Dim1, -Dim2, rownames(euro.mds), cex=0.8) # Same plot as above; but with different markers and color. plot(Dim1, -Dim2, xlab="", ylab="", main="cmdscale(eurodist)") text(Dim1, -Dim2, rownames(euro.mds), cex=0.8, col="red") df <- data.frame(city=rownames(euro.mds),x=euro.mds[,1], y=euro.mds[,2]) df |> ggplot(aes(x=x,y=-y))+geom_point()+geom_text_repel(aes(label=city))+ theme_bw() ``` The problem with classic scaling (cmdscale) is that it assumes metric distances, and fits spatial representation to this. This isn't quite true for even the eurodist data, so we'd like to use methods that make fewer assumptions about the data we have. For example, maybe we think we have error in our measurement, and those errors may be non-linear. Or maybe we can't count on anything but the ordinal values of our dissimilarity measure. Several methods have been developed to support this. ## Sammon's Non-linear scaling Method An alternative (non-linear mapping) is the Sammon's method. Instead of PCA, it tries to fit the samples into a specified space, and then adjusts points within that space to find the best configuration. This chooses (by default) a 2-dimensional configuration based on minimizing something it calls stress. It uses an underlying notion of the metric distances, so the exact measure matters, but it permits the weight of error to be non-linear. This is referred to as non-linear to distinguish it from things like PCA and the corresponding classic mds (```cmdscale```). In addition, we will look at how we can use this in conjunction with a simple clustering method such as k-means (which we will learn about later in this course), we can use mds as a means for visualizing the clustering. As an example, we will use the a measure of the dissimilarity between letters of the alphabet. Then, by applying clustering, we will color nearby elements the same color. The ```eqscplot()``` function in MASS makes it easy to plot the matrix in the MDS solution. ```{r} library(MASS) distmat0 <- as.matrix(read.csv("plaindist.csv")[,-1]) colnames(distmat0) <- LETTERS rownames(distmat0) <- LETTERS plain.dist<-as.dist(distmat0) ##notice as.dist, not dist(). plainsam<-sammon(plain.dist) plainsam plainsam$points plainsam$stress plot(plainsam$points,type="n") text(plainsam$points, labels=LETTERS) ##Clustering will be done next lesson! k <- kmeans(plain.dist,centers=5) eqscplot(plainsam$points, type="p",cex=3,pch=16, main="Sammon Mapping:\n Letter Similarity with K-Means clustering", col=k$cluster) text(plainsam$points, labels=LETTERS, cex=1.2,col="white") df2 <-data.frame(Letter=LETTERS,x=plainsam$points[,1],y=plainsam$points[,2],group=as.factor(k$cluster)) df2 |> ggplot(aes(x=x,y=y,label=Letter,group=group)) + geom_point(size=8,aes(color=group)) + geom_text() + theme_bw() ``` ## Non-metric scaling The assumption that proximities behave like distance might be too restrictive when it comes to employing MDS for exploring the perceptual or subjective similarity spaces of human subjects. Non-metric MDS, introduced by Kruskal, therefore only assumes that the order of proximities is meaningful. Now, the 'stress' scores just count to determine whether the rank order of all the pairwise distances in the solution are the same as the rank order in the similarity. The order of the distances in a non-metric MDS configuration reflects the order of proximities as good as possible while intervals and ration information is of no relevance. Input data usually are equal interval/ratio variables. This is probably the most famous and widely-used version of MDS. Here, we will transform our distance matrix into a rank-order matrix, to illustrate how we can use MDS on just the rank order points and still recover a reasonable spatial pattern ```{r} library(MASS) euro.mds2 <- isoMDS(eurodist) plot(euro.mds2$points,type="n") text(euro.mds2$points,rownames(euro.mds2$points)) plot(rank(as.matrix(eurodist)),as.matrix(eurodist)) eurorank <- matrix((rank(as.matrix(eurodist))),nrow=21) euro.mds3 <- isoMDS(eurorank) par(mfrow=c(1,2)) plot(euro.mds2$points,type="n",main="MDS from distance",xlab="",ylab="") text(euro.mds2$points,rownames(euro.mds2$points)) plot(euro.mds3$points,type="n",main="MDS from rank distance",xlab="",ylab="") text(euro.mds3$points,rownames(euro.mds2$points)) ``` The two spatial mappings above are essentially the same, showing that the isoMDS only uses rank distance. Furthermore, they are quite similar to the metric scaling solution, even though they use non-metric scaling, showing there is a lot of information even in the rank order distances. The one thing the classic scaling preserves is the original distance scale. We've lost the scale in the non-metric scaling, but we've preserved the relative distances. This is a good example of how MDS can be used to recover a spatial representation from a set of dissimilarity measures, even when the dissimilarity measures are not metric. Let's compare ```isoMDS``` and ```cmdscale``` for the letters: Here is a set of dissimilarity ratings for uppercase letters: ```{r} par(mfrow=c(1,2)) plaindistcmds<-cmdscale(plain.dist) plot(plaindistcmds,cex=3,main="Metric scaling of letters",xaxt="n",yaxt="n",col="gold",pch=16,xlab="",ylab="") text(plaindistcmds,LETTERS) nms <- isoMDS(plain.dist) plot(nms$points,cex=3, main="Non-metric scaling of letters",xaxt="n",yaxt="n",col="gold",pch=16,xlab="",ylab="") text(nms$points[,1],nms$points[,2],LETTERS) ``` There are some small differences between these two spaces, especially the location of A and V. # Determining the number of dimensions in non-metric scaling. Oftentimes, we use MDS just for visualization, but there are occasions where we are trying to induce features or a space for some other purpose. For example, we might find that a set of concepts needs four dimensions to represent, indicating that some theory that involves only two aspects is insufficient. In these cases, we'd like to determine how many features are needed, and compare the number of dimensions. MDS produces a goodness-of-fit metric called stress, which we can evaluate using a scree plot on the stress. ```{r} #scree plot mds1 <- isoMDS(distmat0, k=1) #non-metric MDS in 1 dimension mds2 <- isoMDS(distmat0, k=2) #non-metric MDS in 2 dimensions mds3 <- isoMDS(distmat0, k=3) #non-metric MDS in 3 dimensions mds4 <- isoMDS(distmat0, k=4) #non-metric MDS in 4 dimensions mds5 <- isoMDS(distmat0, k=5) #non-metric MDS in 5 dimensions stress = c(mds1$stress, mds2$stress, mds3$stress,mds4$stress,mds5$stress ) dimensions = 1:5 plot (dimensions, stress, type="b", xlab="Number of Dimensions", ylab="Stress") ``` Evaluating these is fairly ad hoc and qualitative. Here, the biggest jump happens between 1 and 2 dimensions, after which it levels off. But maybe it is reasonable to use up to 4 dimensions, which would make sense given the number of features the alphabet has. ## The ```smacof``` library. The ```smacof``` library includes many more advanced tools and example data sets for MDS. This includes an implementation of ```indscal```, which handles multiple participants giving similarity measures, tools for fitting one MDS to another, distance functions, plotting and fitting algorithms, unfolding analysis, svm-based MDS, and jacknife/bootstrapping, and other modern tools. ```{r} library(smacof) mds <- mds(distmat0,type="ratio") summary(mds) ``` Here, we see that a number of different mds option are available, and we get a per-item stress score. The plotting functions will plot the object, unlike isoMDS which had no plotting methods. ```{r} plot(mds) plot(mds,plot.type="Shepard") plot(mds,plot.type="bubbleplot") plot(mds,plot.type="stressplot") plot(mds,plot.type="resplot") ``` # Example: Country data These are ratings of similarity across several countries, from Kruskal & Wish (1978) https://www.academia.edu/figures/1104665/table-9-mean-similarity-ratings-for-twelve-nations-kruskal. There are one or more errors in this. Can you identify what is wrong, and how we can fix it? ```{r} countries <- c("Brazil","Congo","Cuba","Egypt","France","India","Israel", "Japan","China", "Russia","USA", "Yugoslavia") sims <- c(4.83, 5.28,4.56, 3.44,5.0,5.17, 4.72,4.0,4.11,4.78, 4.5,4.83,4.0,5.83,3.44, 3.83,3.33,3.61,4.67,4.0,4.11, 3.5,3.39,2.94,3.83,4.22,4.5,4.83, 2.39,4.0,5.5,4.39,3.67,4.11,3.0,4.17, 3.06,3.39,5.44,4.39,5.06,4.5,4.17,4.61,5.72, 5.39,2.39,3.17,3.33,5.94,4.28,5.94,6.06,2.56,5.00, 3.17,3.5,5.11,4.28,4.72,4.0,4.44,4.28,5.06,6.67,3.56 ) cdist <- matrix(0,nrow=length(countries),ncol=length(countries)) cdist[upper.tri(cdist)] <-sims colnames(cdist) <- countries rownames(cdist) <- countries cdist ``` Notice that the ratings are similarity scores. We can tell by looking at Yugoslavia, and during the cold war this would have been rated most similar to Russia and Cuba--which have the high values. We can convert these to dissimilarity scores by subtracting from the maximum value and adding a small value to avoid 0 distances. This is a common trick to avoid 0 distances, which can cause problems in MDS algorithms. We could use one of the other ways of converting similarities to distances, but because this is non-metric MDS, only the rank order matters and so it probably won't make a difference. We can then use the isoMDS function to map these into a 2-dimensional space. ```{r} cdist2 <- (cdist + t(cdist)) ## add the transpose to make sure we have a symmetric matrix cdist2 <- max(cdist2) - cdist2 + .1 diag(cdist2) <- 0 #make sure the self-similarity is all 0. library(MASS) mds1 <- isoMDS(cdist2) plot(mds1$points,type="n") text(mds1$points,countries) ``` Had we used the similarity data on its own, we still get a solution, as we can see below. The cases are not generally clear enough to tell that it is wrong. Here, Yugoslavia and Russia are on opposite ends of the space, which is a clue, but not perfect. This is partly because NONE of the cases are particularly similar to one another. It might have been a good idea to include a few pairs that are much closer to one another (e.g., USA and Canada, or France and Germany) to help anchor the solution and help us interpret the space. ```{r} cdist <- cdist + t(cdist) mds2 <- isoMDS(cdist) plot(mds2$points,type="n") text(mds2$points,countries) ``` # Example: Michigan Politics Let's consider the voting patterns of of Michigan lawmakers. We can use the voting patterns to compute a similarity metric, and then use MDS to place the politicians in a political space. There is a common variation of MDS called Unfolding Analysis which could be useful if your cases tend to fall on a single dimension, but we may suspect that these cases will require more dimensions, so we will use MDS. ```{r} library(MASS) senate <- read.csv("misenate.csv") votes <- senate[,-(1:2)] rownames(votes) <- paste(senate$X,senate$Party) sdistance <- dist(votes,method="manhattan") sum(is.na(sdistance)) ##this won't work: # isoMDS(sdistance,k=2) ``` Notice that we have 11 NA cases here, probably because of missing data. We have several people with many NA values, including #22, 60, 62, 98, 107, and 112. Because we simply need to create a similarity value, we could try some partial scoring distance methods, but for ease let's remove them and try again: ```{r} senate <- senate[-c(22,60,62,98,107,112),] votes <- senate[,-(1:2)] rownames(votes) <- paste(senate$X,senate$Party) sendist <- dist(votes,method="manhattan") sum(is.na(sendist)) ``` Now, we have removed NA values, but this fails as well, warning us that we have zero distance between two objects: (22 and 27) ```{r,fig.width=8,fig.height=8} #d2 <- isoMDS(sendist,k=2) #this fails! #d2 #table(sdistance) ##not run: causes an error ##isoMDS(sendist,k=2) #Error in isoMDS(sdistance, k = 2) : # zero or negative distance between objects 22 and 27 image(as.matrix(sendist)) ``` In fact, there are five pairings that have distance 0. These show up as white squares in the image plot. In our case, this happens because we have two individuals who voted the same on every issue. The problem is that isoMDS can have problems with this. If the distance between two things is the same, then they are the same thing, so it doesn't make sense to have separate things that are the same. In these cases, there are ties in rank order, and the like. We can fix this by just adding a small positive value to every distance. Thus, no two individuals will be exactly identical, and the algorithm works fine. Let's try looking at 2,3, and 4-dimensional solutions: ```{r} nsen <- nrow(sendist) ##add a little noise because sammon complains about identical cases. nsdistance <- as.matrix(sendist) + .01 #+ matrix(runif(nsen*nsen)*.01,nsen) ``` ```{r} d2 <- isoMDS(nsdistance,k=2) d3 <- isoMDS(nsdistance,k=3) d4 <- isoMDS(nsdistance,k=4) ``` The stress is substantially reduced by moving to 3 dimensions, but then it does not improve much. This might suggest a 3-dimensional solution can describe the political landscape of Michigan politicians. 3-dimensional is difficult to visualize, so let's start with the 2-D solution: ```{r} library(ggplot2) dat <- data.frame(d2$points) dat$name <- rownames(dat) dat$party <- senate$Party colnames(dat) <- c("x","y","name","party") ggplot(dat,aes(x=x,y=y,col=party)) + geom_point() + theme_bw() ``` This suggests that although republicans and democrats cluster together and separate well, there is at least one additional dimension that divides members within each party. If we knew more about issues, members, geography, and voting patterns, we might be able to label this second axis. It would also be useful to look at the 3rd dimension. ```{r} dat <- data.frame(d3$points) dat$name <- rownames(dat) dat$party <- senate$Party colnames(dat) <- c("x","y","z","name","party") ggplot(dat,aes(x=x,y=y,col=party)) + geom_point()+ theme_bw() ggplot(dat,aes(x=x,y=z,col=party)) + geom_point()+ theme_bw() ``` Non-linear Sammon scaling will also give a solution (and this also fails if the min distance is 0). Notice that this tends to space individual points out a bit more. ```{r} s2 <- sammon(nsdistance, k = 2) dat2 <- dat dat2$x <- s2$points[,1] dat2$y <- s2$points[,2] ggplot(dat2,aes(x=x,y=y,col=party)) + geom_point()+ theme_bw() ``` # Boundary Conditions What if we really don't have a good metric structure? These algorithms will still work, but what will they tell us? Let's choose a set of random pairwise distances for 50 points. Play with different values for min and noise to see how the resulting space looks: ```{r} min <- 20 noise <- 0.001 n <- 500 distance <- matrix(runif(n*n)*noise+min,n,n) distance <- (distance + t(distance))/2 diag(distance) <- 0 par(mfrow=c(1,2)) m <- cmdscale(distance,k=2) plot(m,main="Classic scaling") m2 <- isoMDS(distance,k=2) plot(m2$points,main="Non-metric scaling") ``` Oftentimes, the result will form a ring or circle. Although this is sometimes an indication of what the space is like, it is also a marker for a similarity space that does not fit into a metric space well. If everything in the space is equally distant to everything else, the best way to map that into a 2-dimensional space is as a circle. # Correlation as distance and the Personality Circumplex Correlation is not a distance, but 2-cor(x) or 1- abs(cor(x)) might be reasonable. Now, identical things will have a measure of 0, and independent things or completely negatively correlated things will have a measure of 1.0. ```{r} big5 <-read.csv("bigfive.csv")[,-1] qtype <- as.factor(c("E","A","C","N","O", "E","A","C","N","O", "E","A","C","N","O", "E","A","C","N","O", "E","A","C","N","O", "E","A","C","N","O", "E","A","C","N","O", "E","A","C","N","O", "O","A","C","O")) cb <- cor(big5,use="pairwise.complete") d1 <-1-abs(cb) d2 <- 2-cb m1 <- isoMDS(d1,k=2) m2 <- isoMDS(d2,k=2) par(mfrow=c(1,2)) plot(m1$points,col=qtype,pch=16,cex=3,main="Personality trajectories") text(m1$points[,1],m1$points[,2],qtype,col="white") plot(m2$points,col=qtype,pch=16,cex=3,main="Personality dimensions") text(m2$points[,1],m2$points[,2],qtype,col="white") ``` Like with the random points, we see a circumplex here, probably indicating we have more than two dimensions. But depending on the approach, we see like questions clustering along axes or within common areas of space. The different personality dimensions cluster together, allowing some validation of the method. You could also score every person by how they answered questions to put them somewhere within this space. ## Summary of Scaling Approaches We have reviewed three basic methods that have been used to essentially generate features (the metric space) from similarity, dissimilarity, or distance data. In the bigger picture, these are often used for visualizing data as much as for analytics, but it can be useful for inferring or creating a feature space when one didn't exist. The library ```smacof``` has many more advanced tools and example data sets for MDS, and is probably the most modern library for performing scaling. It is worth exploring if you are interested in this area. # Self-organizing maps or Kohenen maps Another crude approach to scaling is what is known as the self-organizing map of Kohonen. This is a bit different from scaling, as it starts with the assumption that your cases are on a grid or lattice (a map), and then has the individual cases organize themselves by moving around the map until you get a stable solution. To visualize this, we will create little radial plots for each case so you can visualize them. We need to specify a grid that is no larger than the number of cases as well. There are 112 Michigan senators, which is 8 * 14, so that will work. If we specify a smaller number, multiple individuals will be placed on a single point in the grid. ```{r} library(class) votes2 <- votes votes2[is.na(votes2)] <- .5 rn <- rownames(votes) party <- substr(rn,nchar(rn),nchar(rn)) gr <- somgrid(topo="hex",xdim=10,ydim=10) b <- SOM(votes2,gr,1:54/54) plot(b) points(b$grid$pts[,1],b$grid$pts[,2],pch=16,col="white",cex=2) text(b$grid$pts[,1] ,b$grid$pts[,2],party,cex=.75,col=c("red","blue")[(party=="D")+1]) ``` This looks to group like things together, but for SOMs have to worry about initial configuration. Note that the cases are already organized by party, so that might put them in a good starting configuration. Let's shuffle and try again. ```{r} library(class) ord <- order(runif(nrow(votes))) votes2 <- votes[ord,] votes2[is.na(votes2)] <- .5 rn <- rownames(votes) party <- substr(rn,nchar(rn),nchar(rn)) gr <- somgrid(topo="hex",xdim=10,ydim=10) b <- SOM(votes2,gr,1:54/54) plot(b) points(b$grid$pts[,1],b$grid$pts[,2],pch=16,col="white",cex=2) text(b$grid$pts[,1] ,b$grid$pts[,2],party,cex=.75,col=c("red","blue")[(party=="D")+1]) ``` This seems to do about as well. This approach starts with features and ends with a 2D map, which is sort of like what PCA does. If you only have similarity data to start with, you might have to be creative to make it work, but it focuses on local similarities rather than the overall space, and is a handy alternative to scaling algorithms. # A Comparative example Suppose we create a grid of 16 points in 2d Space (labelled A-A through D-D), and measure distance between each pair. but add a little noise to make it interesting: ```{r} library(ggplot2) ord <- order(runif(16)) x <- rep(1:4,each=4)[ord] y <- rep(1:4,4)[ord] label <- paste(LETTERS[1:4][x],LETTERS[1:4][y],sep="-") xydistance <- dist(cbind(x,y)) +abs(rnorm(120)*.5) data.frame(x,y,label) |> ggplot(aes(x=x,y=y)) + geom_point(size=13)+ geom_text(aes(label=label),color="white") + theme_bw() ``` ## PCA and variable reduction We earlier learned about PCA via eigen decomposition, and how it can form the basis for scaling via ```cmdscale```. But suppose we just treat the distances as features and do an eigen decomposition: ```{r} e <- eigen(as.matrix(xydistance)) data.frame(x=e$vectors[,1],y=e$vectors[,2],labels=label,xgroup=factor(x),ygroup=factor(y)) |> ggplot(aes(x=x,y=y, group=xgroup,color=xgroup)) + geom_line() + geom_point(size=13)+ geom_text(aes(label=label),color="white") + theme_bw() + ggtitle("Eigen decomposition of distance matrix") e <- eigen(cor(as.matrix(xydistance))) data.frame(x=e$vectors[,1],y=e$vectors[,2],labels=label,xgroup=factor(x),ygroup=factor(y)) |> ggplot(aes(x=x,y=y, group=xgroup,color=xgroup)) + geom_line() + geom_point(size=13)+ geom_text(aes(label=label),color="white") + theme_bw() + ggtitle("Eigen decomposition of distance correlation matrix") e <- eigen(cov(as.matrix(xydistance))) data.frame(x=e$vectors[,1],y=e$vectors[,2],labels=label,xgroup=factor(x),ygroup=factor(y)) |> ggplot(aes(x=x,y=y, group=xgroup,color=xgroup)) + geom_line() + geom_point(size=13)+ geom_text(aes(label=label),color="white") + theme_bw() + ggtitle("Eigen decomposition of distance Covariance matrix") ``` Eigen on the raw distances doesn't do well, but curiously doing so on the correlation matrix of the distances does a good job--although it is slightly curved. This is because the correlation matrix is a metric, and the eigen decomposition is able to recover the grid structure. The covariance eigen decomposition reduces the curvature, probably because by not normalizing everything to 1, it can capture larger-scale geometry of the information. ## Classical scaling The classical scaling approach does a great job of recovering the grid shape. ```{r} cmds <- cmdscale(xydistance) data.frame(x=cmds[,1],y=cmds[,2],labels=label,xgroup=factor(x),ygroup=factor(y)) |> ggplot(aes(x=x,y=y, group=xgroup,color=xgroup)) + geom_line() + geom_point(size=13)+ geom_text(aes(label=label),color="white") + theme_bw() + ggtitle("Classic MDS (cmdscale)") ``` This produces a rotation, and the basic layout is reasonable, but a bit wobbly. ## Sammon mapping ```{r} sam <- sammon(xydistance) data.frame(x=sam$points[,1],y=sam$points[,2],labels=label,xgroup=factor(x),ygroup=factor(y)) |> ggplot(aes(x=x,y=y, group=xgroup,color=xgroup)) + geom_line() + geom_point(size=13)+ geom_text(aes(label=label),color="white") + theme_bw() + ggtitle("Sammon scaling") ``` ## Non-metric Kruskal scaling ```{r} iso <- isoMDS(xydistance) data.frame(x=iso$points[,1],y=iso$points[,2],labels=label,xgroup=factor(x),ygroup=factor(y)) |> ggplot(aes(x=x,y=y, group=xgroup,color=xgroup)) + geom_line() + geom_point(size=13)+ geom_text(aes(label=label),color="white") + theme_bw() + ggtitle("Non-metric (isoMDS) scaling") ``` The sammon and isoMDS have much more regular outcomes, because the rank-order distance erases some of the noisiness. ## Self-organizing maps ```{r} library(class) gr <- somgrid(topo="rectangular",xdim=4,ydim=4) b <- SOM(as.matrix(xydistance), gr,radii=1000:1/10, alpha=1000:1/5) plot(b) data.frame(x=b$grid$pts[,1],y=b$grid$pts[,2],labels=label,xgroup=factor(x),ygroup=factor(y)) |> ggplot(aes(x=x,y=y, group=xgroup,color=xgroup)) + geom_line() + geom_point(size=13)+ geom_text(aes(label=label),color="white") + theme_bw() + ggtitle("Self-organizing map (SOM)") ``` The SOM does well sometimes, but its accuracy is a bit deceptive, because it assumes the points lie on a grid, and then finds a good organization of the cases in that grid. It tends to get stuck in reasonable but suboptimal configurations. ```{r} newx <- rep(x,10) newy <- rep(y,10) newxy <- cbind(newx + rnorm(160)*.2,newy+rnorm(160)*.2) newxydistance <- dist(newxy) newlabel <- rep(paste(LETTERS[1:4][newx],LETTERS[1:4][newy],sep="-"),10) gr <- somgrid(topo="rectangular",xdim=5,ydim=5) b <- SOM(as.matrix(newxydistance), gr,radii=100:1/10, alpha=100:1/5) plot(b) data.frame(x=b$grid$pts[,1],y=b$grid$pts[,2],labels=newlabel,xgroup=factor(newx),ygroup=factor(newy)) |> ggplot(aes(x=x,y=y, group=xgroup,color=xgroup)) + geom_line() + geom_point(size=13,alpha=.5)+ geom_text(aes(label=newlabel),color="white") + theme_bw() + ggtitle("Self-organizing map (SOM)") ```