Tutorials › Generalized Linear Models › Covariate Values as Design-Matrix Entries

Design Matrices and Predictors · Tutorial 81 of 981

Covariate Values as Design-Matrix Entries

You will learn to translate each observation’s measured numerical covariates into correctly indexed design-matrix entries and check the assignments against the original records.

Beginner 10 min read

What You'll Learn

  • Define a design-matrix entry using observation and covariate indices
  • Match each measurement to its designated covariate
  • Preserve units and measurement occasions when recording values
  • Distinguish numerical covariates from outcomes and numerical identifiers
  • Audit entries against source records and detect common indexing errors
  • Reorder covariate labels without mixing up their measured values

From Measurements to Entries

A generalized linear model uses measured covariates to describe the predictors associated with observations. Before any model calculation, each covariate measurement must be placed in the right location in the design matrix. The central task here is not to calculate a linear predictor; it is to translate the recorded data faithfully into indexed entries.

For this task, suppose there are \(n\) observations and \(p\) numerical covariates. Use \(i\) to label an observation and \(j\) to label a covariate. A covariate is the numerical variable measured for each observation, such as age in years or a laboratory concentration. The recorded value, not the variable’s name or its model coefficient, goes into the corresponding entry.

Definition: Let \(c_j\) denote the \(j\)th covariate in a declared ordering, and let \(y(i,c_j)\) denote the value recorded for observation \(i\) on that covariate. The design-matrix entry \(x_{ij}\) is that recorded value: $$ x_{ij}=y(i,c_j), \qquad i=1,\ldots,n,\quad j=1,\ldots,p. $$ Thus \(i\) identifies which observation’s record to consult, and \(j\) identifies which covariate value to take from that record. This definition assumes the relevant measurement is available and has been recorded as a numerical value.

The index pair matters. The number \(x_{ij}\) is not just “a value from the data”; it is the value for a particular observation and a particular covariate. For example, \(x_{23}\) refers to the third covariate measured on the second observation, if there are at least three covariates. The entry exists only when both indices fall within the stated ranges.

A design-matrix entry is not the observation’s response, a coefficient, or a covariate’s name. The response is the outcome being modelled; a coefficient is a model parameter; a covariate is the measured predictor variable. Those are distinct objects, even when their values happen to be numerical.

Worked Examples of Entry Assignment

Worked Example: Age and Baseline Blood Pressure

A study records two numerical covariates for each of three patients: age in years and baseline systolic blood pressure in mmHg. The declared covariate order is age first, blood pressure second. The source records are:

ObservationAge (years)Baseline systolic blood pressure (mmHg)
145118
262142
337126

There are \(n=3\) observations and \(p=2\) covariates. Since age is covariate 1 and blood pressure is covariate 2, the first observation gives \(x_{11}=45\) and \(x_{12}=118\). The second gives \(x_{21}=62\) and \(x_{22}=142\). The third gives \(x_{31}=37\) and \(x_{32}=126\). Therefore, the entries are:

$$ X= \begin{pmatrix} 45 & 118\\ 62 & 142\\ 37 & 126 \end{pmatrix}. $$

For example, \(x_{23}= {}\) is not an entry of this matrix because there are only two covariates; the indexed entries that do exist for observation 2 are \(x_{21}=62\) and \(x_{22}=142\). The first column’s entries all have age units, while the second column’s entries all have blood-pressure units.

Worked Example: Keeping Measurement Occasions Distinct

A patient contributes one observation at each of two clinic visits. At each visit, the study records time since treatment began, in days, and the prescribed dose, in mg. For this example, the two visits are the two observations. The covariate order is time first, dose second.

ObservationVisitTime since treatment began (days)Dose (mg)
1First visit720
2Second visit2130

For the first visit, the time measurement is the first covariate value and the dose is the second, so \(x_{11}=7\) and \(x_{12}=20\). For the second visit, \(x_{21}=21\) and \(x_{22}=30\). The resulting entries are:

$$ X= \begin{pmatrix} 7 & 20\\ 21 & 30 \end{pmatrix}. $$

The patient is the same, but the observation records are different because they refer to different visits. Using the first visit’s time with the second visit’s dose would produce \(x_{21}=7,\ x_{22}=30\), which does not match either visit’s record. Correct assignment requires both the right covariate and the right measurement occasion.

Worked Example: Including Covariates, Not the Outcome or an Identifier

A clinical data set has three observations. For each, the investigators have recorded age in years, serum creatinine in mg/dL, a binary outcome indicating readmission, and a hospital code. The intended numerical covariates are age followed by creatinine. The outcome and hospital code are not among these two covariates.

ObservationAge (years)Creatinine (mg/dL)Readmitted?Hospital code
1541.1Yes12
2681.8No7
3430.9No12

The first observation supplies \(x_{11}=54\) and \(x_{12}=1.1\); the second supplies \(x_{21}=68\) and \(x_{22}=1.8\); the third supplies \(x_{31}=43\) and \(x_{32}=0.9\). The entries are therefore:

$$ X= \begin{pmatrix} 54 & 1.1\\ 68 & 1.8\\ 43 & 0.9 \end{pmatrix}. $$

The readmission value is an outcome, not one of the two specified covariates. The hospital code is an identifier represented by digits; it is not automatically a numerical covariate simply because it looks numeric. If either were intended as a predictor, that would need to be specified and represented appropriately. Under the stated covariate list, neither belongs among these entries.

Two Results for Checking Assignments

The entry definition yields a precise way to check whether a proposed matrix faithfully represents the measurements. The covariate order must be declared, and every entry must match the corresponding source record. No statistical assumptions about the distribution of the response are needed for this bookkeeping result.

Proposition: Unique assignment from a fixed record and ordering. Suppose there are \(n\) observations, \(p\) named numerical covariates, and a fixed ordering \(c_1,\ldots,c_p\). Suppose every covariate value \(y(i,c_j)\) is defined. There is exactly one \(n\)-by-\(p\) array whose entry for observation \(i\) and covariate \(c_j\) is the recorded value \(y(i,c_j)\).

Proof. For each allowed index pair \((i,j)\), the fixed record and covariate label specify one value \(y(i,c_j)\). Assigning that value to \(x_{ij}\) defines every entry, so an array with the required property exists. If two arrays both have the required property, then for each \(i=1,\ldots,n\) and \(j=1,\ldots,p\), their entries at \((i,j)\) must both equal \(y(i,c_j)\). Hence those entries agree at every position, so the arrays are identical. The assignment is therefore unique. \(\square\)

This proposition explains why both labels and values belong in an entry audit. A number copied from the wrong covariate does not meet the definition even if it appears elsewhere in the data. Nor does a value from the wrong observation or measurement occasion.

Proposition: Reordering covariate labels reorders their assigned entries. Suppose the original covariate ordering is \(c_1,\ldots,c_p\), and a new ordering places the old covariate \(c_{\pi(j)}\) in position \(j\), where \(\pi\) is a permutation of \(1,\ldots,p\). If \(X\) and \(X'\) are assigned from the same records using the original and new orderings, respectively, then $$ x'_{ij}=x_{i,\pi(j)} $$ for every observation \(i\) and new position \(j\).

Proof. Under the new ordering, the covariate in position \(j\) is \(c_{\pi(j)}\). By the entry definition, \(x'_{ij}=y(i,c_{\pi(j)})\). Under the original ordering, \(x_{i,\pi(j)}=y(i,c_{\pi(j)})\). The two expressions identify the same recorded measurement, so \(x'_{ij}=x_{i,\pi(j)}\) for every permitted pair of indices. Thus changing the declared covariate order changes which position holds each value, not which value belongs to each named covariate. \(\square\)

For instance, if age and blood pressure are swapped in the first example, the first observation’s entries become \(x'_{11}=118\) and \(x'_{12}=45\). The blood-pressure measurement remains 118 mmHg and the age measurement remains 45 years; only their positions under the new ordering change.

A Practical Entry Audit

A short audit can catch many transcription and indexing errors before the matrix is used in a model. For each proposed entry, check its observation label, its covariate label, and its value against the source record. Also verify that the covariate order is the one declared for the task.

1
Identify the observation.
Use the record that corresponds to the intended observation. If observations represent visits, measurements from different visits must not be interchanged.
2
Identify the covariate position.
Use the declared covariate ordering to determine which named numerical variable belongs at index \(j\).
3
Copy the recorded measurement.
Set \(x_{ij}\) equal to the value for that observation and covariate. Preserve its meaning and units; do not substitute an outcome or a numerical identifier.
4
Check the entry and its indices.
Confirm that the value matches the source record and that \(i\) and \(j\) are within the stated observation and covariate ranges.

Measurements should be compared with their units in mind. An age recorded in years and a laboratory value recorded in mg/dL are different quantities, even when both are stored as ordinary numbers. A column with values that appear plausible can still be wrong if its entries came from a different covariate. The entry labels and source records, rather than numerical plausibility alone, determine whether the assignment is correct.

Common Mistakes

  • Putting a response into a covariate entry. A recorded outcome does not become a covariate simply because it is numerical. Include it only if it is explicitly designated as a predictor for the model in question.
  • Treating numeric codes as measurements. A hospital code, patient number, or other identifier uses digits to label something. The digits alone do not make it a numerical covariate.
  • Using the right value for the wrong observation. A measurement must match both indices. Copying a value from another patient or visit assigns the wrong entry.
  • Leaving an index undefined as if it were a blank entry. If there are only two covariates, \(x_{i3}\) is not an entry in the matrix. It is not a missing or empty value in an existing position.
  • Forgetting the declared order. The value for a named covariate must be placed in the position assigned to that covariate. Changing the order changes positions, not the source measurement associated with the covariate.
Key takeaway: For every design-matrix entry \(x_{ij}\), read \(i\) as the observation and \(j\) as the named covariate in the declared order. Then copy precisely that measurement from the corresponding source record.

Check Your Understanding

For each question, use the entry definition and check both indices against the stated records and covariate order.

  1. There are four observations and three numerical covariates, ordered as age, dose, and time since treatment. What does \(x_{32}\) refer to?
  2. A source record gives an observation’s age as 51 years and creatinine as 1.4 mg/dL. If age is covariate 1 and creatinine is covariate 2, state the two entries for this observation, using its observation index \(i\).
  3. A matrix has two covariate columns. Is \(x_{14}\) a missing entry or not an entry of that matrix? Explain.
  4. A data set includes a numerical hospital code and a measured blood concentration. Explain why the hospital code is not automatically a numerical covariate.
  5. If two covariates exchange positions in the declared ordering, what happens to the entries for each observation, and what does not change?
Clinical Biostats