| Income $ per capita | Illiteracy % | Life exp. years | Murder per 100k | HS grad % | Frost days | |
|---|---|---|---|---|---|---|
| 1 Alabama | 3,624 | 2.1 | 69.0 | 15.1 | 41.3 | 20 |
| 2 Alaska | 6,315 | 1.5 | 69.3 | 11.3 | 66.7 | 152 |
| 3 Arizona | 4,530 | 1.8 | 70.5 | 7.8 | 58.1 | 15 |
| 4 Arkansas | 3,378 | 1.9 | 70.7 | 10.1 | 39.9 | 65 |
| 5 California | 5,114 | 1.1 | 71.7 | 10.3 | 62.6 | 20 |
| ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
| 43 Texas | 4,188 | 2.2 | 70.9 | 12.2 | 47.4 | 35 |
| ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
| 50 Wyoming | 4,566 | 0.6 | 70.3 | 6.9 | 62.9 | 173 |
Vectors are bold: \(\mathbf x_1\) is the first observation; plain \(x_1\) is the first variable. The prime \('\) is the transpose.
A \(50\times 6\) data matrix \(\mathbf X\) (Rโs state.x77, 1970s): the sample.
All 300 numbers at once, standardised \(z_{tj}=(x_{tj}-\bar x_j)/s_j\): how many standard deviations a state is above (purple) or below (orange) the average.
Which state stands out? Single extreme values are easy to spot; unusual combinations are not. We answer at the end.
Euclidean distance: lay a ruler between the two points. By Pythagoras:
\[\lVert \mathbf x_s-\mathbf x_t\rVert=\sqrt{\sum_{j=1}^p (x_{sj}-x_{tj})^2}\]
In vector form, as a row times a column (the inner product):
\[\lVert \mathbf x_s-\mathbf x_t\rVert=\sqrt{(\mathbf x_s-\mathbf x_t)'(\mathbf x_s-\mathbf x_t)}\]
Same formula for any \(p\). It depends on the units: the variable with the widest range dominates.
Binary: a nominal variable with two categories (coded 0/1, its mean is a share). Count: ratio-scaled, whole numbers only.
Numbers on a scale: means, covariances, axes, sizes. Categories: count, colour or split.
island <- factor(c("Dream", "Biscoe",
"Dream", "Torgersen"))
levels(island) # stored once
## [1] "Biscoe" "Dream" "Torgersen"
as.integer(island) # codes per row
## [1] 2 1 2 3A factor stores the categories once (its levels) and a whole-number code per observation.
NAordered = TRUE for ordinalTwo categorical: a scatterplot shows 333 penguins as 5 dots. Show counts.
Categorical + numeric: points pile up on lines. Use boxplots (plus jittered points).
Two numeric: the scatterplot is the right tool.
\(p\) variables, \(p(p-1)/2\) pairs: every pair once below the diagonal, correlations above.
Pairs are 2D. What about three variables at once?
Humans read 2D well. Keep two axes; show the third variable in a way that fits its type: categorical โ colour or panels, numeric โ size or colour gradient.
Two numeric variables on the axes; the third (flipper length, also numeric) as a colour gradient.
One flat picture, values can be read off: three groups, which no single 3D view showed clearly.
aes(colour = species). Hues have no order: fine for nominal.Hues cannot show how much. A numeric variable needs an order and a scale: size or a colour gradient.
Bubble plot: position = illiteracy, murder; size = population; gradient = income.
aes(size = Population, fill = Income). Crowds quickly; areas are hard to compare.
Conditioning plot: one panel per category (species ร sex), same axes everywhere.
Also called trellis plots (lattice), facet plots (ggplot2: facet_wrap(), facet_grid()) or small multiples.
Facet plot, one variable:
facet_wrap(~ species)
Trellis plot (
lattice):xyplot(mass ~ flipper | species * sex)
Sample mean of variable \(j\) and the mean vector, the centre of the cloud:
\[\bar x_j=\frac1n\sum_{t=1}^n x_{tj},\qquad \bar{\mathbf x}=\begin{pmatrix}\bar x_1\\ \vdots\\ \bar x_p\end{pmatrix}=\frac1n\sum_{t=1}^n \mathbf x_t\]
Sample variance and standard deviation, the typical distance from the mean:
\[s_j^2=\frac{1}{n-1}\sum_{t=1}^n (x_{tj}-\bar x_j)^2,\qquad s_j=\sqrt{s_j^2}\]
Murder: \(\bar{\mathbf x}=7.4\), \(s=3.69\) per 100,000. \(s\) is in the units of the variable.
\[ s_{ij}=\frac{1}{n-1}\sum_{t=1}^n (x_{ti}-\bar x_i)(x_{tj}-\bar x_j) \qquad\text{illiteracy and murder: } s = 1.58 \]
All variances (diagonal) and covariances (off the diagonal):
\[ S=\begin{pmatrix} s_1^2 & s_{12} & \cdots & s_{1p}\\ s_{21} & s_2^2 & \cdots & s_{2p}\\ \vdots & & \ddots & \vdots\\ s_{p1} & s_{p2} & \cdots & s_p^2\end{pmatrix} =\frac{1}{n-1}\sum_{t=1}^n (\mathbf x_t-\bar{\mathbf x})(\mathbf x_t-\bar{\mathbf x})' \]
Covariance carries the units of both variables (dollars times daysโฆ). Divide by the standard deviations:
\[r_{ij}=\frac{s_{ij}}{s_i\,s_j}\in[-1,1]\]
Same as the covariance of standardised values \(z=(x-\bar{\mathbf x})/s\).
All correlations form the matrix \(R\), ones on the diagonal:
\[R=\operatorname{diag}(s_1,\dots,s_p)^{-1}\,S\,\operatorname{diag}(s_1,\dots,s_p)^{-1}\]
Move the slider. Each point is coloured by its product \(z_{t1}z_{t2}\):
The thinner the ellipse, the larger \(|r|\).
Hawaii: \(z = 1.20\) and \(1.09\), within the whiskers, so no outlier. But high illiteracy usually comes with few graduates (\(r=-0.66\)); Hawaii has many.
Euclidean distance from the centre treats every direction alike:
\[\sqrt{(\mathbf x_t-\bar{\mathbf x})'(\mathbf x_t-\bar{\mathbf x})}\]
Mahalanobis distance puts \(S^{-1}\) in the middle:
\[d_t=\sqrt{(\mathbf x_t-\bar{\mathbf x})'S^{-1}(\mathbf x_t-\bar{\mathbf x})}\]
solve(S)) shrinks differences where the cloud is wide and stretches them where it is narrow.Distance from the centre in units of the cloudโs own shape.
Equal \(d_t\): one ellipse. Further-out ellipse, larger \(d_t\): same order as in the scatterplot.
Hawaii and Alaska lie across the cloud; Louisiana beyond its end.
The same formula with \(p=6\): no picture, but the same idea.
Alaska is farthest, then Hawaii, New Mexico and Nevada.
Is a distance of 4.7 surprising? That needs a model of where the data come from: next lecture.