Tutorials › Linear Algebra › Organizing Observations as Data Matrices

Applications in Networks, Economics and Data · Tutorial 946 of 1000

Organizing Observations as Data Matrices

Represent observations consistently as rows, variables as columns, and responses in a matching vector, then track how reordering or missing values affects matrix calculations.

Intermediate 10 min read

What You'll Learn

  • Arrange measured variables in columns and observations in rows of a data matrix.
  • Keep response values aligned with the observations that produced them.
  • Check dimensions before computing products such as XᵀX and Xᵀy.
  • Show why reordering observations consistently leaves these products unchanged.
  • Handle missing entries without silently replacing them by zero or shifting row pairings.

From Observations to a Matrix

Many applications begin with a collection of records: one record for each person, time point, location, or other observational unit. Before applying matrix calculations, the records need a consistent arrangement. A matrix makes that arrangement explicit, but it does not decide what the rows and columns mean. Those meanings must be specified and kept fixed throughout the calculation.

Suppose there are \(n\) observations and \(p\) measured numerical variables. Use one row per observation and one column per variable. Write the resulting data matrix as \(\mathbf{X}\in\mathbb{R}^{n\times p}\). Its entry \(x_{ij}\) is the recorded value of variable \(j\) for observation \(i\). If each observation also has one numerical response value, place those values in a separate column vector \(\mathbf{y}\in\mathbb{R}^n\), with entry \(y_i\) belonging to the same observation as row \(i\) of \(\mathbf{X}\).

Definition: A data matrix is an \(n\times p\) matrix whose rows correspond to \(n\) observations and whose columns correspond to \(p\) measured variables, under a stated and consistent ordering. A response vector is a column \(\mathbf{y}\in\mathbb{R}^n\) whose \(i\)th entry is the response associated with the \(i\)th row of the data matrix.

The dimensions record an important part of the data structure: \(\mathbf{X}\) has \(n\) rows and \(p\) columns, while \(\mathbf{y}\) has \(n\) entries. This shared row count is not just a formatting preference. A later calculation involving \(\mathbf{X}\) and \(\mathbf{y}\) pairs values from the same observation. A mismatch in row order can therefore change the calculation, even when the two objects separately contain all the right values.

For example, if the columns of \(\mathbf{X}\) are “hours studied” and “practice problems completed,” then the first row must give both values for the same observation. The first entry of \(\mathbf{y}\) must be the response for that same observation. A column heading, a unit, and a row identifier help make these meanings auditable. An identifier can be stored alongside the data for reference, but it is not automatically a numerical variable to include in matrix arithmetic.

Building and Checking the Arrangement

1
Define the observational unit.
Decide what one row represents, such as one participant or one day. Do not mix different kinds of units in the same row sequence without a clear reason.
2
Name and order the variables.
Assign each measured variable one column and record its units. Keep this column order fixed when interpreting the entries.
3
Place responses in matching order.
For every row \(i\), place the associated response in \(y_i\). Use identifiers to check this pairing when records are sorted or filtered.
4
Check shape and entry type.
Confirm that \(\mathbf{X}\) has \(n\) rows and \(p\) columns, and that \(\mathbf{y}\) has \(n\) entries. Ordinary matrix multiplication requires numerical entries; labels and blanks need explicit treatment before such calculations.

Numerical entries in different columns may have different units. The matrix records those values; it does not make them comparable or put them on a common scale. Changing units or rescaling variables is a separate operation. In particular, do not alter entries while organizing the data unless the change is deliberate and documented.

Worked Example: Three Observations and Two Variables

Three observations have values for variables \(x_1\) and \(x_2\), together with a response \(y\): \((1,2;4)\), \((2,1;3)\), and \((3,0;2)\). Arrange the records as \(\mathbf{X}\) and \(\mathbf{y}\), and compute two products that commonly occur in later linear-model calculations.

Keep the observations in the listed order and the variables in the order \(x_1,x_2\). Then

$$ \mathbf{X}=\begin{bmatrix}1&2\\2&1\\3&0\end{bmatrix}, \qquad \mathbf{y}=\begin{bmatrix}4\\3\\2\end{bmatrix}. $$

The dimensions are \(3\times2\) for \(\mathbf{X}\) and \(3\times1\) for \(\mathbf{y}\), so \(\mathbf{X}^{\top}\mathbf{X}\) and \(\mathbf{X}^{\top}\mathbf{y}\) are defined. Compute each entry by multiplying corresponding entries and adding:

$$ \mathbf{X}^{\top}\mathbf{X} = \begin{bmatrix}1&2&3\\2&1&0\end{bmatrix} \begin{bmatrix}1&2\\2&1\\3&0\end{bmatrix} = \begin{bmatrix} 1^2+2^2+3^2&1(2)+2(1)+3(0)\\ 2(1)+1(2)+0(3)&2^2+1^2+0^2 \end{bmatrix} = \begin{bmatrix}14&4\\4&5\end{bmatrix}. $$

Similarly,

$$ \mathbf{X}^{\top}\mathbf{y} = \begin{bmatrix}1&2&3\\2&1&0\end{bmatrix} \begin{bmatrix}4\\3\\2\end{bmatrix} = \begin{bmatrix}1(4)+2(3)+3(2)\\2(4)+1(3)+0(2)\end{bmatrix} = \begin{bmatrix}16\\11\end{bmatrix}. $$

Check: The first component of \(\mathbf{X}^{\top}\mathbf{y}\) uses the first variable values paired with their own responses: \(1(4)+2(3)+3(2)=16\). The second uses \(2(4)+1(3)+0(2)=11\). Both products use all three matched rows, and the dimensions of the results are \(2\times2\) and \(2\times1\), respectively.

Why Consistent Row Order Matters

Changing the order of observations is harmless if every object indexed by observation is reordered in exactly the same way. This can be stated precisely. A permutation matrix is a square matrix obtained by reordering the rows of an identity matrix. Multiplying on the left by a permutation matrix reorders the rows of a matrix or the entries of a column vector. Its rows are distinct standard basis rows, so multiplying it by its transpose gives the identity.

Theorem: Let \(\mathbf{X}\in\mathbb{R}^{n\times p}\), \(\mathbf{y}\in\mathbb{R}^n\), and let \(\mathbf{P}\in\mathbb{R}^{n\times n}\) be a permutation matrix. Reordering the observations in both objects gives \(\widetilde{\mathbf{X}}=\mathbf{P}\mathbf{X}\) and \(\widetilde{\mathbf{y}}=\mathbf{P}\mathbf{y}\). Then \(\widetilde{\mathbf{X}}^{\top}\widetilde{\mathbf{X}}=\mathbf{X}^{\top}\mathbf{X}\) and \(\widetilde{\mathbf{X}}^{\top}\widetilde{\mathbf{y}}=\mathbf{X}^{\top}\mathbf{y}\).

Proof. Because \(\mathbf{P}\) reorders the standard basis rows, each row and column of \(\mathbf{P}\) contains one \(1\) and otherwise zeros. Its transpose reverses that reordering, so \(\mathbf{P}^{\top}\mathbf{P}=\mathbf{I}_n\). Using the definitions of \(\widetilde{\mathbf{X}}\) and \(\widetilde{\mathbf{y}}\), and the rule \((\mathbf{A}\mathbf{B})^{\top}=\mathbf{B}^{\top}\mathbf{A}^{\top}\), we obtain \(\widetilde{\mathbf{X}}^{\top}\widetilde{\mathbf{X}}=(\mathbf{P}\mathbf{X})^{\top}(\mathbf{P}\mathbf{X})=\mathbf{X}^{\top}\mathbf{P}^{\top}\mathbf{P}\mathbf{X}=\mathbf{X}^{\top}\mathbf{X}\). Likewise, \(\widetilde{\mathbf{X}}^{\top}\widetilde{\mathbf{y}}=(\mathbf{P}\mathbf{X})^{\top}(\mathbf{P}\mathbf{y})=\mathbf{X}^{\top}\mathbf{P}^{\top}\mathbf{P}\mathbf{y}=\mathbf{X}^{\top}\mathbf{y}\). Thus simultaneous reordering preserves both products. \(\square\)

Worked Example: Reordering All Records Together

Use the data from the previous example, but list observations in the order third, first, second. The corresponding permutation matrix and reordered objects are

$$ \mathbf{P}=\begin{bmatrix}0&0&1\\1&0&0\\0&1&0\end{bmatrix}, \qquad \widetilde{\mathbf{X}}=\mathbf{P}\mathbf{X} =\begin{bmatrix}3&0\\1&2\\2&1\end{bmatrix}, \qquad \widetilde{\mathbf{y}}=\mathbf{P}\mathbf{y} =\begin{bmatrix}2\\4\\3\end{bmatrix}. $$

Compute the products directly to check the theorem:

$$ \widetilde{\mathbf{X}}^{\top}\widetilde{\mathbf{X}} = \begin{bmatrix}3&1&2\\0&2&1\end{bmatrix} \begin{bmatrix}3&0\\1&2\\2&1\end{bmatrix} = \begin{bmatrix}9+1+4&0+2+2\\0+2+2&0+4+1\end{bmatrix} = \begin{bmatrix}14&4\\4&5\end{bmatrix}. $$
$$ \widetilde{\mathbf{X}}^{\top}\widetilde{\mathbf{y}} = \begin{bmatrix}3&1&2\\0&2&1\end{bmatrix} \begin{bmatrix}2\\4\\3\end{bmatrix} = \begin{bmatrix}3(2)+1(4)+2(3)\\0(2)+2(4)+1(3)\end{bmatrix} = \begin{bmatrix}16\\11\end{bmatrix}. $$

Check: These are exactly the products from the original ordering. In each reordered row, the response stayed attached to the observation whose variables appear in that row.

The theorem does not say that the order of \(\mathbf{X}\) can be changed independently of the order of \(\mathbf{y}\). For instance, pairing the first row of the original \(\mathbf{X}\) with the third response changes which observations are associated. Such a mismatch is not a harmless reordering: it changes the data being represented.

Missing Entries and Usable Rows

A missing value is not the number zero. Zero is a measured numerical value that participates in addition and multiplication; a missing entry has no numerical value to use in those operations. Therefore a table with blanks or missing-value markers is not yet an ordinary real matrix for calculations such as \(\mathbf{X}^{\top}\mathbf{X}\). Replacing a blank by zero without justification changes the recorded information and generally changes the products.

One transparent way to form a numerical matrix is to identify a set of rows for which all required variables and the response are observed, then retain those same rows in both objects. This is often called a complete-case restriction for the variables being used. It gives aligned numerical inputs, but it may discard information; it does not by itself establish that the retained observations are representative of all observations.

Result: If \(I\) is a chosen set of observation indices, form \(\mathbf{X}_I\) by retaining the rows of \(\mathbf{X}\) indexed by \(I\), and form \(\mathbf{y}_I\) by retaining the entries of \(\mathbf{y}\) with those same indices in the same order. Then \(\mathbf{X}_I^{\top}\mathbf{y}_I\) is the sum of the variable-response products over precisely the retained observations.

Proof. Let \(I=(i_1,\ldots,i_k)\) be the retained indices in their chosen order. The \(j\)th entry of \(\mathbf{X}_I^{\top}\mathbf{y}_I\) is, by the definition of matrix multiplication, \(\sum_{\ell=1}^{k}x_{i_\ell j}y_{i_\ell}\). Each term uses the value of variable \(j\) and the response from the same original observation \(i_\ell\), and every retained observation contributes exactly once. No omitted index contributes a term. \(\square\)

Worked Example: Restricting to Complete Observations

Suppose the records are \((x_1,x_2;y)=(1,2;5)\), \((2,\text{missing};6)\), \((3,1;7)\), and \((4,0;\text{missing})\). If both variables and the response are required, only observations 1 and 3 are complete. Retain those rows in their original order:

$$ \mathbf{X}_I=\begin{bmatrix}1&2\\3&1\end{bmatrix}, \qquad \mathbf{y}_I=\begin{bmatrix}5\\7\end{bmatrix}, \qquad I=(1,3). $$

Now calculate:

$$ \mathbf{X}_I^{\top}\mathbf{X}_I = \begin{bmatrix}1&3\\2&1\end{bmatrix} \begin{bmatrix}1&2\\3&1\end{bmatrix} = \begin{bmatrix}1+9&2+3\\2+3&4+1\end{bmatrix} = \begin{bmatrix}10&5\\5&5\end{bmatrix}, $$
$$ \mathbf{X}_I^{\top}\mathbf{y}_I = \begin{bmatrix}1&3\\2&1\end{bmatrix} \begin{bmatrix}5\\7\end{bmatrix} = \begin{bmatrix}1(5)+3(7)\\2(5)+1(7)\end{bmatrix} = \begin{bmatrix}26\\17\end{bmatrix}. $$

Check: The first component \(26\) is \(1(5)+3(7)\), and the second is \(2(5)+1(7)\); both use only observations 1 and 3. The incomplete second and fourth records contribute no terms to these restricted products.

Different calculations may require different sets of observed values. If one product is computed from one subset of rows and another product from a different subset, they do not describe the same collection of observations. Any such choice should be explicit. Methods for addressing missingness beyond restricting to complete rows are a separate statistical question; the matrix notation alone does not determine a missing value.

Common Pitfall: Deleting a Missing Value and Shifting the Pairing

Consider one variable with observed values \(x=(1,\text{missing},3)^{\top}\) and responses \(\mathbf{y}=(10,20,30)^{\top}\). The complete rows are 1 and 3, so the matched vectors are \(\mathbf{x}_I=(1,3)^{\top}\) and \(\mathbf{y}_I=(10,30)^{\top}\). Their product is \(1(10)+3(30)=100\).

A wrong calculation deletes the missing value from \(\mathbf{x}\) but leaves the full response vector unchanged. It then pairs \((1,3)\) with \((10,20)\), producing \(1(10)+3(20)=70\). This is not a harmless shortcut: it pairs the third observation’s \(x\)-value with the second observation’s response. The correction is to apply the same row selection to both objects, giving \((1,3)\) and \((10,30)\), or to use another explicitly defined method that handles missing entries.

Keep the row identity, variable order, response order, and missing-value treatment visible. Once those choices are consistent, matrix dimensions and products become reliable summaries of the observations actually being used.

Check Your Understanding

For each question, state which observations and variables are being used before carrying out any calculation.

  1. A data matrix has \(8\) observations and \(3\) variables. What are its dimensions, and how many entries must the matching response vector have?
  2. Explain why sorting the rows of \(\mathbf{X}\) requires the same reordering of \(\mathbf{y}\).
  3. If a response is missing for one record, explain why inserting zero is not the same as omitting that observation.
  4. For \(\mathbf{X}=\begin{bmatrix}1&2\\2&0\end{bmatrix}\) and \(\mathbf{y}=\begin{bmatrix}3\\4\end{bmatrix}\), calculate \(\mathbf{X}^{\top}\mathbf{y}\).
  5. In the example with \(x=(1,\text{missing},3)^{\top}\), what response entries must accompany the retained \(x\)-values?

Test yourself: Linear Algebra final exam practice

Ready to check what you know? Work a full-length practice final, then open the complete worked solutions.

Want all 1,114 practice problems?

This tutorial covers one topic. Calc Pass covers the full Calculus AB & BC curriculum (the core of Calculus I and II): full review, every practice question, and step-by-step solutions to each one.

QR code to download the Calc Pass app

Get the Calc Pass App

Scan to download and practice on the go, with instant scoring.

Calc Pass Volume 1: 1,114 Practice Questions book cover

Vol. 1: 1,114 Questions

The complete practice question bank, organized by AB & BC topic.

View on Amazon
Calc Pass Volume 2: Solutions Manual book cover

Vol. 2: Solutions Manual

Fully worked solutions to every question in Volume 1.

View on Amazon