You are on page 1of 17

MODULE 5

Multidimensional Scaling
El Escalamiento Multidimensional es una técnica de análisis
multivariante que, partiendo de una matriz de distancias (o bien
de similitudes) entre individuos, produce una representación de
los individuos en una escala euclidea ordinaria de modo que las
distancias en dicha escala se aproximen lo mejor posible a las
distancias de partida.

Multidimensional scaling (MDS) is a multivariate data analysis


approach that is used to visualize the similarity/dissimilarity
between samples by plotting points in two dimensional plots.
MDS returns an optimal solution to represent the data in a
lower-dimensional space, where the number of dimensions k is
pre-specified by the analyst. For example, choosing k = 2
optimizes the object locations for a two-dimensional scatter
plot.
An MDS algorithm takes as an input data the dissimilarity
matrix, representing the distances between pairs of objects. The
input data for MDS is a dissimilarity matrix representing the
distances between pairs of objects.
Types of MDS algorithms
1. Classical multidimensional scaling: Preserves the original
distance metric, between points, as well as possible. That
is the fitted distances on the MDS map and the original
distances are in the same metric. Classic MDS belongs to
the so-called metric multidimensional scaling category.
It’s also known as principal coordinates analysis. It’s
suitable for quantitative data.
2. Non-metric multidimensional scaling: It’s also known as
ordinal MDS. Here, it’s not the metric of a distance value
that is important or meaningful, but its value in relation
to the distances between other pairs of objects. Ordinal
MDS constructs fitted distances that are in the same rank
order as the original distance. For example, if the
distance of apart objects 1 and 5 rank fifth in the
original distance data, then they should also rank fifth
in the MDS configuration. It’s suitable for qualitative
data.
Compute MDS in R. R functions
cmdscale() [stats package]: Compute classical (metric)
multidimensional scaling.
isoMDS() [MASS package]: Compute Kruskal’s non-metric
multidimensional scaling (one form of non-metric MDS).
sammon() [MASS package]: Compute sammon’s non-linear mapping
(one form of non-metric MDS).
All these functions take a distance object as the main argument
and k is the desired number of dimensions in the scaled output.
By default, they return two dimension solutions, but we can
change that through the parameter k which defaults to 2.

Example
> spot = dist(USArrests, method = "euclidean")
> ?dist()
This function computes and returns the distance matrix computed
by using the specified distance measure to compute the distances
between the rows of a data matrix.
dist(x, method = "meth.name", diag =FALSE, upper = FALSE, p = 2)
x a numeric matrix, data frame or "dist" object.
method the distance measure to be used. This must be one of
"euclidean", "maximum", "manhattan", "canberra", "binary" or
"minkowski". Any unambiguous substring can be given.
diag logical value indicating whether the diagonal of the
distance matrix should be printed by print.dist.
p The power of the Minkowski distance.

Available distance measures are (written for two vectors x and


y):
euclidean: Usual distance between the two vectors (2 norm aka
L_2), sqrt(sum((x_i - y_i)^2)).
maximum: Maximum distance between two components of x and y
(supremum norm)
manhattan: Absolute distance between the two vectors (1 norm aka
L_1).
canberra: sum(|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. This is intended for non-negative
values (e.g., counts), in which case the denominator can be
written in various equivalent ways; Originally, R used x_i +
y_i, then from 1998 to 2017, |x_i + y_i|, and then the correct |
x_i| + |y_i|.
binary: (aka asymmetric binary): The vectors are regarded as
binary bits, so non-zero elements are ‘on’ and zero elements are
‘off’. The distance is the proportion of bits in which only one
is on amongst those in which at least one is on.
minkowski: The p norm, the pth root of the sum of the pth powers
of the differences of the components.
> what = cmdscale(spot)
> ?cmdscale()
Classical (Metric) Multidimensional Scaling
Classical multidimensional scaling (MDS) of a data matrix. Also
known as principal coordinates analysis. Multidimensional
scaling takes a set of dissimilarities and returns a set of
points such that the distances between the points are
approximately equal to the dissimilarities. (It is a major part
of what ecologists call ‘ordination’.)
cmdscale(d, k = 2, eig = FALSE, add = FALSE, x.ret = FALSE,list.
= eig || add || x.ret)
d a distance structure such as that returned by dist or a
full symmetric matrix containing the dissimilarities.
k the maximum dimension of the space which the data are to
be represented in; must be in {1, 2, …, n-1}.
eig indicates whether eigenvalues should be returned.

> x11()
> plot(what[,1], what[,2], xlab = "Axis 1", ylab = "Axis 2",
main = "US Arrests")
> head(what)
[,1] [,2]
Alabama -64.80216 11.448007
Alaska -92.82745 17.982943
Arizona -124.06822 -8.830403
Arkansas -18.34004 16.703911
California -107.42295 -22.520070
Colorado -34.97599 -13.719584

> identify(what[,1], what[,2], row.names(USArrests))


Example 2 (SU FORMA)
http://www.sthda.com/english/articles/31-principal-component-methods-in-r-
practical-guide/122-multidimensional-scaling-essentials-algorithms-and-r-code/

swiss data that contains fertility and socio-economic data on 47


French speaking provinces in Switzerland.
> data("swiss")
> head(swiss)
Fertility Agriculture Examination Education Catholic Infant.Mortality

Courtelary 80.2 17.0 15 12 9.96 22.2

Delemont 83.1 45.1 6 9 84.84 22.2

Franches-Mnt 92.5 39.7 5 5 93.40 20.2

Moutier 85.8 36.5 12 7 33.77 20.3

Neuveville 76.9 43.5 17 15 5.16 20.6

Porrentruy 76.1 35.3 9 7 90.57 26.6

Classical MDS
> library(magrittr)
> library(dplyr)
> library(ggpubr)
# Cmpute MDS
> mds <- swiss %>%
+ dist() %>%
+ cmdscale() %>%
+ as_tibble()
> colnames(mds) <- c("Dim.1", "Dim.2")
# Plot MDS
> x11()
> ggscatter(mds, x = "Dim.1", y = "Dim.2",
+ label = rownames(swiss),
+ size = 1,
+ repel = TRUE)
Create 3 groups using k-means clustering. Color points by groups
# K-means clustering
> clust <- kmeans(mds, 3)$cluster %>%
+ as.factor()
> mds <- mds %>%
+ mutate(groups = clust)
# Plot and color by groups
> x11()
> ggscatter(mds, x = "Dim.1", y = "Dim.2",
+ label = rownames(swiss),
+ color = "groups",
+ palette = "jco",
+ size = 1,
+ ellipse = TRUE,
+ ellipse.type = "convex",
+ repel = TRUE)
Example 2 (MI FORMA)
> data("swiss")
> spot = dist(swiss, method = "euclidean")
> what = cmdscale(spot)
> x11()
> plot(what[,1], what[,2], xlab = "Dim.1", ylab = "Dim.2")

> identify(what[,1], what[,2], row.names(USArrests))


En vez de identify:
text(what[,1], what[,2], labels = row.names(swiss))

> text(what[,1], what[,2], labels = row.names(swiss), cex = 0.6,


pos = 4, col = "red")
> ?text
text draws the strings given in the vector labels at the
coordinates given by x and y. text(x, ...)
x, y numeric vectors of coordinates where the text labels
should be written. If the length of x and y differs, the shorter
one is recycled.

Labels a character vector or expression specifying the text


to be written. An attempt is made to coerce other language
objects (names and calls) to expressions, and vectors and other
classed objects to character vectors by as.character. If labels
is longer than x and y, the coordinates are recycled to the
length of labels.

adj one or two values in [0, 1] which specify the x (and


optionally y) adjustment (‘justification’) of the labels, with 0
for left/bottom, 1 for right/top, and 0.5 for centered. On most
devices values outside [0, 1] will also work. See below.

pos a position specifier for the text. If specified this


overrides any adj value given. Values of 1, 2, 3 and 4,
respectively indicate positions below, to the left of, above and
to the right of the specified (x,y) coordinates.

offset when pos is specified, this value controls the


distance (‘offset’) of the text label from the specified
coordinate in fractions of a character width.

vfont NULL for the current font family, or a character


vector of length 2 for Hershey vector fonts. The first element
of the vector selects a typeface and the second element selects
a style. Ignored if labels is an expression.

cex numeric character expansion factor; multiplied by


par("cex") yields the final character size. NULL and NA are
equivalent to 1.0.

col, font the color and (if vfont = NULL) font to be used,
possibly vectors. These default to the values of the global
graphical parameters in par().
Example 3
> dist.au <-
read.csv("http://rosetta.reltech.org/TC/v15/Mapping/data/dist-
Aus.csv")
> dist.au
X A AS B D H M P S
1 A 0 1328 1600 2616 1161 653 2130 1161
2 AS 1328 0 1962 1289 2463 1889 1991 2026
3 B 1600 1962 0 2846 1788 1374 3604 732
4 D 2616 1289 2846 0 3734 3146 2652 3146
5 H 1161 2463 1788 3734 0 598 3008 1057
6 M 653 1889 1374 3146 598 0 2720 713
7 P 2130 1991 3604 2652 3008 2720 0 3288
8 S 1161 2026 732 3146 1057 713 3288 0

> row.names(dist.au) <- dist.au[, 1]


> dist.au <- dist.au[, -1]
> dist.au
A AS B D H M P S
A 0 1328 1600 2616 1161 653 2130 1161
AS 1328 0 1962 1289 2463 1889 1991 2026
B 1600 1962 0 2846 1788 1374 3604 732
D 2616 1289 2846 0 3734 3146 2652 3146
H 1161 2463 1788 3734 0 598 3008 1057
M 653 1889 1374 3146 598 0 2720 713
P 2130 1991 3604 2652 3008 2720 0 3288
S 1161 2026 732 3146 1057 713 3288 0

> fit <- cmdscale(dist.au)


> x11()
> plot(fit[,1], fit[,2], pch = 19)
> city.names <- c("Adelaide", "Alice Springs",
+ "Brisbane", "Darwin", "Hobart",
+ "Melbourne", "Perth", "Sydney")
> text(fit[,1], fit[,2], pos = 4, labels = city.names)
(Pos = 1 es abajo, 2 es izquierda, 3 es arriba y 4 derecha).
Alternatively, we can use the library igraph:
> g <- graph.full(nrow(dist.au))
> V(g)$label <- city.names
> layout <- layout.mds(g, dist = as.matrix(dist.au))
> x11()
> plot(g, layout = layout, vertex.size = 3)
Perceptual Mappings in R
Perceptual mapping / Market mapping is a diagrammatic technique
used by asset marketers that attempts to visually display the
perceptions of customers or potential customers. The positioning
of a brand is influenced by customer perceptions rather than by
those of businesses.
Example:
> library(plfm)
> data("car")
As the data base is very large, we are going to select a few
car-models:
> selected.cars=t(car$freq1[c(14,11,7,5,1,4),])
> pca <- princomp(selected.cars)
> x11()
> biplot(pca,xlim=c(-0.75,0.75))
And now we can "zoom in" to have a closer look at "Volkswagen
Golf" and "Toyota Prius" using xlim and ylim and also use the
scaling argument expand.
biplot(pca, expand=1, xlim=c(-.3,0.1),ylim=c(-.2,0))

> library(anacor)
> ca=anacor(selected.cars)
> plot(ca, conf=NULL)
> x11()
> plot(ca, conf=NULL)
This ought to look familiar to anyone working in the automotive
industry. Let's work our way around the four quadrants: Quadrant
I Sporty, Quadrant II Economical, Quadrant III Family, and
Quadrant IV Luxury. Another perspective is to see an economy-
luxury dimension running from the upper left to the lower right
and a family-sporty dimension moving from the lower left to the
upper right (i.e., drawing a large X through the graph).

You might also like