24 Types of Data
Individuals, variables, and values
The table below shows three patients, each with a blood type, a ZIP code, and a wound area.
import numpy as np
import pandas as pd
patient = pd.DataFrame({
"blood type": ["O", "A", "AB"],
"zip code": [94720, 94110, 10001],
"wound area (cm^2)": [3.2, 5.8, 2.1],
})
patientEach row of this table is a case, or an individual: here, a patient. Each column is a measured characteristic, called a variable: blood type, ZIP code, wound area. The entry a variable takes for one particular individual (O, or \(5.8\ \text{cm}^2\)) is that variable’s value.
Categorical and numerical variables
Variables split into two kinds (Figure 24.1). A variable is numerical when it takes a range of numbers on which it is sensible to add, subtract, or average. Wound area is numerical, because the average area of several wounds is itself a meaningful area. A variable is categorical when its values are labels that sort individuals into groups. The possible values of a variable are its levels. Blood type is categorical, because there is no sensible average blood type.
scripts/03_statistics/03_data_types/gen_data_types.py.
A variable is not numerical just because its values are written as digits. ZIP code shows this: \(94720\) and \(10001\) are numbers on the page, but averaging them identifies no place, so ZIP code is categorical despite looking numerical. A patient ID number is the same kind of case. The test is not how a value is written, but whether averaging it produces something meaningful.
# the arithmetic test, applied to the record above
print("mean zip code =", patient["zip code"].mean(), "-- a place? no")
print("mean wound area =", patient["wound area (cm^2)"].mean(), "cm^2 -- a real quantity")
# patient["blood type"].mean() would raise an error: there is no mean of a column of labelsCategorical variables: nominal, ordinal, and binary
A categorical variable is nominal when its values have no natural order. Blood type is nominal: the categories O, A, B, and AB could be listed in any order without losing anything. The mode, the most frequent value, and the count of each level are the usual summaries of a nominal variable.
scripts/03_statistics/03_data_types/gen_data_types.py.
A categorical variable is ordinal when its values do have a natural order. Pain recorded as none, mild, moderate, or severe is ordinal: the four levels rank unambiguously, even though the difference between “none” and “mild” is not necessarily the same size as the difference between “moderate” and “severe.” Tumor stage I through IV is ordinal for the same reason. The median is the middle value once the values are sorted. Sorting needs only an order, so an ordinal variable can have a median even though it has no mean.
A categorical variable with exactly two values is binary. Whether an ulcer healed within twelve weeks is binary: it is either “yes” or “no.” Coding those two values as \(1\) and \(0\) makes the variable’s mean meaningful, because the mean of a column of ones and zeros is the proportion of ones. Thirty healed ulcers out of a hundred treated gives a mean of \(0.30\). A categorical variable has a genuine mean in exactly this one case.
Numerical variables: discrete and continuous
A numerical variable is discrete when it can only take values with jumps between them, typically a count. The number of prior ulcers on a patient’s foot is discrete: it can be \(0\), \(1\), or \(2\), never \(1.5\) (see Figure 24.3).
scripts/03_statistics/03_data_types/gen_data_types.py.
A numerical variable is continuous when it can take any value in a range. Wound area is continuous: between \(3.2\ \text{cm}^2\) and \(3.3\ \text{cm}^2\), another value is possible, limited only by how precisely the wound is measured. The distinction determines how a numerical variable is binned into a histogram.
- Categorical: values are labels; arithmetic is meaningless.
- Nominal: unordered labels (blood type, studio). Summaries: counts, mode.
- Ordinal: ordered labels, spacing undefined (none / mild / moderate / severe). Summaries: counts, mode, median.
- Binary: two classes (healed / not healed). Coded \(0\)/\(1\), its mean is a proportion.
- Numerical: values are numbers; arithmetic is meaningful. Summaries: mean, median, variance, standard deviation.
- Discrete: isolated values, typically counts (number of ulcers).
- Continuous: any value in a range, from measurement (wound area, recovery time).