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],
})
patient

Each 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.

Figure 24.1: The two families of variables and their subtypes, each with a clinical example. Generated by 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 labels

Categorical 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.

Figure 24.2: Blood type, a nominal variable, for 200 hypothetical patients. The bars may be ordered arbitrarily; there is no “next” blood type after O. Generated by 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).

Figure 24.3: Number of prior ulcers, a discrete variable, for 200 hypothetical patients. Unlike Figure 24.2, the bars fall along a number line, so their left-to-right order is fixed rather than arbitrary. Generated by 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.

NoteThe data-type taxonomy
  • 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).
ImportantKnowledge check

A cancer trial records, for each patient, four variables: the treatment arm (drug or placebo), the tumor stage (I, II, III, or IV), the number of prior surgeries, and the change in tumor volume in \(\text{cm}^3\). Classify each variable, and state which, if any, has a mean that is directly meaningful.

  • Treatment arm — binary categorical. It has a meaningful mean only once coded \(0\)/\(1\), and then only as a proportion (the fraction on the drug).
  • Tumor stage — ordinal categorical. The stages are ordered, so a median exists, but their spacing is undefined, so no mean.
  • Number of prior surgeries — discrete numerical. A count; its mean is meaningful.
  • Change in tumor volume — continuous numerical. A measurement; its mean is meaningful.

Only the two numerical variables have a directly meaningful mean. The binary arm has one solely as a proportion, and the ordinal stage has none.

Exercises

TipProblem 1

A wound-care study records four variables across eight patients:

  • healed within 12 weeks (Y/N): Y, Y, N, Y, N, N, Y, Y
  • tumor stage: II, I, III, II, IV, III, I, II
  • number of prior surgeries: 0, 1, 2, 0, 3, 1, 0, 2
  • change in wound area (\(\text{cm}^2\)): \(-4.2, -1.1, +0.5, -3.8, +1.2, -0.4, -5.0, -2.6\)
  1. Classify each variable by data type. (b) Compute the proportion of patients who healed. (c) Give the median tumor stage, and explain why its mean is not defined. (d) Compute the mean number of prior surgeries.
  1. Healed — binary categorical; tumor stage — ordinal categorical; number of prior surgeries — discrete numerical; change in wound area — continuous numerical.

  2. Coding healed as \(1\) and not-healed as \(0\) gives \(1,1,0,1,0,0,1,1\): five ones out of eight, a proportion \(5/8 = 0.625\) (which is the mean of the coded column).

  3. Sorted, the stages are I, I, II, II, II, III, III, IV; the two middle values (4th and 5th) are both II, so the median is stage II. A mean is undefined because the stages are ordered labels with no defined numeric spacing. Averaging II and IV has no meaning.

  4. \((0+1+2+0+3+1+0+2)/8 = 9/8 = 1.125\) prior surgeries.

TipProblem 2

Classify each of the following as categorical (nominal, ordinal, or binary) or numerical (discrete or continuous), and for each categorical variable state whether it has a directly meaningful mean:

  1. A microbiology report’s result: positive or negative.
  2. A hospital’s patient satisfaction rating, on a scale of 1 (poor) to 5 (excellent).
  3. The department a patient was admitted to: cardiology, oncology, or orthopedics.
  4. The number of pills remaining in a prescription bottle.
  5. A patient’s body temperature, in degrees Celsius.
  1. Binary categorical. Coded \(0\)/\(1\), its mean is a proportion (the fraction positive), so the mean is meaningful.

  2. Ordinal categorical. The ratings are ordered, but the step from \(1\) to \(2\) is not guaranteed equal to the step from \(4\) to \(5\); a median exists, a mean does not.

  3. Nominal categorical. The departments carry no order; only counts and a mode are defined.

  4. Discrete numerical. A count, with gaps between possible values; its mean is meaningful.

  5. Continuous numerical. A measurement that can take any value in a range; its mean is meaningful.

NoteFurther reading
  • Diez, Çetinkaya-Rundel and Barr, OpenIntro Statistics, 4th ed. A free open textbook covering the topics of this chapter (Diez et al. 2019).
  • Illowsky, B. & Dean, S., Introductory Statistics 2e (OpenStax), Section 1.3 (“Frequency, Frequency Tables, and Levels of Measurement”): a complementary taxonomy (nominal/ordinal/interval/ratio) organized around what arithmetic each level of measurement supports.

References

Diez, David M., Mine Çetinkaya-Rundel, and Christopher D. Barr. 2019. OpenIntro Statistics. 4th ed. OpenIntro. https://www.openintro.org/book/os/.