# 设计矩阵

设计矩阵是用于拟合统计模型的自变量的取值矩阵，每行对应一个观测，每列对应一个待估计系数的模型项<sup>[1](https://mathworld.wolfram.com/DesignMatrix.html)</sup>。它是线性回归与一般线性模型的核心数据结构：一旦观测与模型项被排布成这样的矩形阵列，拟合、推断与诊断就全部归结为矩阵代数<sup>[2](https://www.stats.ox.ac.uk/~snijders/ISS_1_tr.pdf)</sup>。

| 关键事实 | 详细说明 |
|---|---|
| 定义 | 行对应观测，列对应模型项；通常包含一列全为 1 的列以表示截距<sup>[1](https://mathworld.wolfram.com/DesignMatrix.html)</sup> |
| 规模 | 对于 \( n \) 个观测和 \( p \) 个模型项，\( X \) 为 \( n \times p \)；第 \( i \) 行存放第 \( i \) 个观测处 \( p \) 个项的取值<sup>[3](https://locusmath.org/books/lay-linear-algebra/6-6)</sup> |
| 模型方程 | \( y = X\beta + \varepsilon \)，其中 \( \beta \) 是回归系数列向量，\( \varepsilon \) 是误差列向量<sup>[1](https://mathworld.wolfram.com/DesignMatrix.html)</sup> |
| 最小二乘估计 | \( b = (X^{\prime}X)^{-1}X^{\prime}Y \)，当 \( X^{\prime}X \) 可逆时存在，这意味着不存在完全多重共线性<sup>[4](https://stats.libretexts.org/Courses/Knox_College/Linear_Models_and_Rurita_Kralovstvi/04%3A_Matrices_and_Linear_Regression/4.01%3A_Matrix_Representation)</sup> |
| 拟合值 | 当 \( X \) 具有满列秩时，\( \hat{y} = X(X^{T}X)^{-1}X^{T}y \)<sup>[1](https://mathworld.wolfram.com/DesignMatrix.html)</sup> |
| 分类变量编码 | 一个有 \( k \) 个水平的分类因子占据 \( k - 1 \) 列；Minitab 的列顺序为常数项、协变量、区组、因子、交互作用<sup>[5](https://support.minitab.com/minitab/help-and-how-to/statistical-modeling/doe/how-to/factorial/analyze-factorial-design/methods-and-formulas/model-information/)</sup> |
| 共线性诊断 | 对缩放后的 \( X \) 计算的条件指数介于 30 与 100 之间，表明存在中等至较强的多重共线性<sup>[6](https://www.itl.nist.gov/div898/software/dataplot/refman2/auxillar/condind.htm)</sup> |

## 在线性模型中的作用

多元线性回归模型指出，每个观测 \( y_n \) 等于 \( P \) 个项的加权和加上一个误差：

\[ y_n = \beta_1 x_{n1} + \beta_2 x_{n2} + \ldots + \beta_P x_{nP} + \varepsilon_n, \quad n = 1, \ldots, N. \]

将这 \( N \) 个方程堆叠起来，即得到紧凑的矩阵形式 \( y = X\beta + \varepsilon \)<sup>[7](https://stat154.berkeley.edu/spring-2026/lectures/unit1/unit1_ols_matrix_notation.html)</sup>。\( X \) 的元素 \( x_{ij} \) 位于第 \( i \) 行第 \( j \) 列，行号写在前面<sup>[2](https://www.stats.ox.ac.uk/~snijders/ISS_1_tr.pdf)</sup>。一列全为 1 的列提供截距，因为每个方程中的项 \( \beta_0 \cdot 1 \) 变成一个常数列乘以其系数<sup>[4](https://stats.libretexts.org/Courses/Knox_College/Linear_Models_and_Rurita_Kralovstvi/04%3A_Matrices_and_Linear_Regression/4.01%3A_Matrix_Representation)</sup>。

若视为线性变换，\( X \) 将 \( p \) 个参数的向量 \( \beta \) 映射为 \( n \) 个期望结果的向量 \( X\beta \)<sup>[2](https://www.stats.ox.ac.uk/~snijders/ISS_1_tr.pdf)</sup>。普通最小二乘选择 \( \beta \) 以最小化偏差平方和 \( (Y - X\beta)^{\prime}(Y - X\beta) \)；当扰动相互独立、服从方差恒定的正态分布时，该估计量同时也是最大似然估计量<sup>[2](https://www.stats.ox.ac.uk/~snijders/ISS_1_tr.pdf)</sup>。

## 为何使用矩阵形式

教科书论述中反复出现三条实用理由。第一，用标量代数求解正规方程组，一旦超出简单回归的范围就变得难以实行，而矩阵解法十分紧凑<sup>[4](https://stats.libretexts.org/Courses/Knox_College/Linear_Models_and_Rurita_Kralovstvi/04%3A_Matrices_and_Linear_Regression/4.01%3A_Matrix_Representation)</sup>。第二，矩阵代数在计算机上易于实现。第三，矩阵方法可以直接从一个自变量推广到多个自变量<sup>[8](https://www.math.wm.edu/~leemis/otext3.pdf)</sup>。

矩阵形式还揭示了拟合的假设与输出。在残差假设 \( E[\mathbf{E}] = 0 \) 与 \( V[\mathbf{E}] = \sigma^2 I \) 下，估计量 \( \mathbf{b} \) 是无偏的，其方差-协方差矩阵为 \( V[\mathbf{b}] = \sigma^2 (\mathbf{X}^{\prime}\mathbf{X})^{-1} \)<sup>[4](https://stats.libretexts.org/Courses/Knox_College/Linear_Models_and_Rurita_Kralovstvi/04%3A_Matrices_and_Linear_Regression/4.01%3A_Matrix_Representation)</sup>。

## 构建设计矩阵

**连续项与分类项。** 常数项、协变量或连续因子各贡献一列。一个有 \( k \) 个水平的分类因子贡献 \( k - 1 \) 列，因为其中一个水平作为基线；对于一个四水平因子 A，三列将水平 1 编码为 (1, 0, 0)，水平 2 为 (0, 1, 0)，水平 3 为 (0, 0, 1)，基线水平 4 为 (−1, −1, −1)<sup>[5](https://support.minitab.com/minitab/help-and-how-to/statistical-modeling/doe/how-to/factorial/analyze-factorial-design/methods-and-formulas/model-information/)</sup>。交互作用列由相互作用的因子的对应列相乘得到；一个涉及水平数分别为 6、3、4 的因子的项 A * C * D 有 \( 5 \times 2 \times 3 = 30 \) 列<sup>[5](https://support.minitab.com/minitab/help-and-how-to/statistical-modeling/doe/how-to/factorial/analyze-factorial-design/methods-and-formulas/model-information/)</sup>。

**公式语言。** 统计软件会自动完成这一构造。在 statsmodels 中，公式接口模型接受 `patsy` 公式字符串，而大写开头的模型则直接以 `endog` 和 `exog` 参数接收响应变量与设计矩阵<sup>[9](https://www.statsmodels.org/stable/examples/notebooks/generated/formulas.html)</sup>。运算符对应到列：“~”将响应与项分开，“+”添加列，“:”添加一列存放两列的乘积，“*”在添加乘积的同时添加各自的列，“-1”移除截距<sup>[9](https://www.statsmodels.org/stable/examples/notebooks/generated/formulas.html)</sup>。Patsy 默认将文本值变量视为分类变量，并在存在截距时丢弃一个类别<sup>[9](https://www.statsmodels.org/stable/examples/notebooks/generated/formulas.html)</sup>。

统计学课程、回归教科书与机器学习库所用的术语各不相同，但矩阵还是同一个矩阵<sup>[3](https://locusmath.org/books/lay-linear-algebra/6-6)</sup>。

## 秩、共线性与条件数

估计的质量取决于 \( X \) 的列结构。如果特征之间存在相关，使得设计矩阵的某些列呈现近似线性依赖，矩阵便接近奇异，由此得到的最小二乘估计对观测目标中的随机误差反应强烈，产生很大的方差；这种情形称为多重共线性，例如在数据并非通过实验设计收集时可能出现。<sup>[10](https://scikit-learn.org/stable/modules/linear_model.html)</sup>

条件指数可以量化这一问题。其计算方法是：将 \( X \) 的各列缩放至单位平方和，然后用缩放后矩阵的最大奇异值除以其各个奇异值；取值介于 30 与 100 之间表明存在中等至较强的共线性<sup>[6](https://www.itl.nist.gov/div898/software/dataplot/refman2/auxillar/condind.htm)</sup><sup> • </sup><sup>[13](https://www.mathworks.com/help/econ/collintest.html)</sup>。两两共线性可以从自变量的相关矩阵中看出，但相关矩阵无法揭示高阶共线性，即某一列接近其他若干列的线性组合<sup>[6](https://www.itl.nist.gov/div898/software/dataplot/refman2/auxillar/condind.htm)</sup>。补救办法包括删除一个或多个自变量、主成分回归，或用奇异值分解计算回归<sup>[6](https://www.itl.nist.gov/div898/software/dataplot/refman2/auxillar/condind.htm)</sup>。

在计算层面，scikit-learn 通过 \( X \) 的奇异值分解计算最小二乘解，当 \( n_{\text{samples}} \geq n_{\text{features}} \) 时，代价为 \( O(n_{\text{samples}} \cdot n_{\text{features}}^2) \)<sup>[10](https://scikit-learn.org/stable/modules/linear_model.html)</sup>。QR 分解是求解非方阵最小二乘方程组的标准替代方法<sup>[11](https://math.nist.gov/javanumerics/jama/doc/Jama/QRDecomposition.html)</sup>。

## 由设计矩阵得到的诊断

有些回归诊断只依赖于 \( X \)，而不依赖于观测到的结果。将 \( Y \) 投影到拟合值上的帽子矩阵完全由设计矩阵决定；其对角元素 \( h_{ii} \) 表示每个观测的杠杆值，均为正数，且总和为 \( p \)。\( h_{ii} \) 较大的观测可视为自变量空间中的离群点<sup>[2](https://www.stats.ox.ac.uk/~snijders/ISS_1_tr.pdf)</sup>。

## 历史

回归模型的矩阵记号在二十世纪中叶变得普遍；它出现在 J. Durbin 与 G. S. Watson 的《最小二乘回归中的序列相关检验：一》（Biometrika，1950）以及 O. Kempthorne 的 《实验设计与分析》（1952）中<sup>[12](https://mathshistory.st-andrews.ac.uk//Miller/mathsym/matrices/)</sup>。这一语境下的矩阵记号最早于 1920 年代被使用，其中最受关注的早期贡献是 A. C. Aitken 的论文《论最小二乘法与观测的线性组合》（Proceedings of the Royal Society of Edinburgh，1935，第 42–48 页）<sup>[12](https://mathshistory.st-andrews.ac.uk//Miller/mathsym/matrices/)</sup>。R. W. Farebrother 在 1997 年发表于 *Linear Algebra and its Applications* 的一篇文章中，描述了 Aitken 之后矩阵理论在统计学中的整合过程<sup>[12](https://mathshistory.st-andrews.ac.uk//Miller/mathsym/matrices/)</sup>。

## References

1. [Weisstein, Eric W. "Design Matrix." MathWorld – A Wolfram Resource](https://mathworld.wolfram.com/DesignMatrix.html)
2. [Snijders, T. Intermediate Social Statistics, Lecture 1: Matrices and Linear Regression, University of Oxford](https://www.stats.ox.ac.uk/~snijders/ISS_1_tr.pdf)
3. [Lay, D. C., Linear Algebra and Its Applications §6.6 study guide, Locus](https://locusmath.org/books/lay-linear-algebra/6-6)
4. [Matrix Representation, Statistics LibreTexts, Knox College](https://stats.libretexts.org/Courses/Knox_College/Linear_Models_and_Rurita_Kralovstvi/04%3A_Matrices_and_Linear_Regression/4.01%3A_Matrix_Representation)
5. [Methods and formulas for the model information in Analyze Factorial Design, Minitab Support](https://support.minitab.com/minitab/help-and-how-to/statistical-modeling/doe/how-to/factorial/analyze-factorial-design/methods-and-formulas/model-information/)
6. [Condition Indices, NIST/SEMATECH Dataplot Reference Manual](https://www.itl.nist.gov/div898/software/dataplot/refman2/auxillar/condind.htm)
7. [Giordano, R. Linear regression in matrix notation (review), Stat 154, UC Berkeley](https://stat154.berkeley.edu/spring-2026/lectures/unit1/unit1_ols_matrix_notation.html)
8. [Leemis, L. Probability textbook chapter on matrix regression, William & Mary](https://www.math.wm.edu/~leemis/otext3.pdf)
9. [Fitting models using R-style formulas, statsmodels 0.15.0 documentation](https://www.statsmodels.org/stable/examples/notebooks/generated/formulas.html)
10. [Linear Models, scikit-learn 1.9.1 documentation](https://scikit-learn.org/stable/modules/linear_model.html)
11. [QRDecomposition, JAMA Java Matrix Package documentation, NIST](https://math.nist.gov/javanumerics/jama/doc/Jama/QRDecomposition.html)
12. [Miller, J. J. Earliest Uses of Symbols for Matrices and Vectors, MacTutor History of Mathematics](https://mathshistory.st-andrews.ac.uk//Miller/mathsym/matrices/)
13. [mathworks.com](https://www.mathworks.com/help/econ/collintest.html)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 统计学与概率论 › Statistical inference, estimation, sampling, and testing › Regression analysis

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/design-matrix

English original: https://www.edgepedia.org/design-matrix
