1 The data matrix

The data: 50 states, 6 variables

Income
$ per capita
Illiteracy
%
Life exp.
years
Murder
per 100k
HS grad
%
Frost
days
1 Alabama3,6242.169.015.141.320
2 Alaska6,3151.569.311.366.7152
3 Arizona4,5301.870.57.858.115
4 Arkansas3,3781.970.710.139.965
5 California5,1141.171.710.362.620
⋮⋮⋮⋮⋮⋮⋮
43 Texas4,1882.270.912.247.435
⋮⋮⋮⋮⋮⋮⋮
50 Wyoming4,5660.670.36.962.9173
  • Row \(t\): one state, the column vector \(\mathbf x_t=(x_{t1},\dots,x_{tp})'\) ( Texas). Column \(j\): one variable ( murder).
  • Entry \(x_{tj}\): Texas has \(x_{43,4}=12.2\) murders per 100,000.

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.

Which US state is the most unusual?

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.

Observations are points

  • One variable: a point on a line. Two: in the plane. Three: in space. \(p\): a point in \(p\) dimensions.
  • The data matrix is a cloud of \(n\) points; this course is about its shape.

Distance between two observations

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, with \(a'b=\sum_j a_jb_j\) (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.

2 Types of variables

Not every column is a number on a line

  • Nominal: labels without order (island, species).
  • Ordinal: ordered labels, gaps not comparable (diamond cut, survey answers).
  • Interval: equal steps, arbitrary zero (temperature in ยฐF).
  • Ratio: true zero, so ratios make sense (body mass, income, murder rate).

Binary: a nominal variable with two categories (coded 0/1, its mean is a share). Count: ratio-scaled, whole numbers only.

Why the type matters

Numbers on a scale: means, covariances, axes, sizes. Categories: count, colour or split.

Categorical variables in R: factors

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 3

A factor stores the categories once (its levels) and a whole-number code per observation.

  • Smaller and faster than repeated text
  • A fixed set of categories: typos become NA
  • Level order sets table and legend order; ordered = TRUE for ordinal
  • Models create the dummy variables automatically

3 A plot for each type

One plot does not fit all types