R编程与矩阵运算

本文梳理和总结了R编程与矩阵语言的相关知识。R编程是计量经济学中常用的编程语言,矩阵语言是计量经济学中常用的数学语言。
R
矩阵运算
Author

胡华平

Published

September 28, 2025

(vinod2011?): Hands-on matrix algebra using R: active and motivated learning with applications.

(fieller2015?): Basics of Matrix Algebra for Statistics with R.

1 基础矩阵

1.1 记号表达

(fieller2015?, sec 1.3)

  • 矩阵(matrix):以加粗大写英文字母表达,例如\(\boldsymbol{A}, \boldsymbol{B}, \boldsymbol{C}, \boldsymbol{D}, \boldsymbol{U}, \boldsymbol{V}, \boldsymbol{W}, \boldsymbol{X}, \boldsymbol{Y}, \boldsymbol{Z}\).

  • 列向量(column vectors):以加粗小写英文字母表达,例如\(\boldsymbol{a}, \boldsymbol{b}, \boldsymbol{c}, \boldsymbol{d}, \boldsymbol{u}, \boldsymbol{v}, \boldsymbol{w}, \boldsymbol{x}, \boldsymbol{y}, \boldsymbol{z}\).

  • 向量的元素(elements):一般用小写英文字母(单下标)表达,例如列向量\(\boldsymbol{x}\)的元素记为\(x_1, x_2, \ldots, x_p\).

  • 矩阵的元素(elements):一般用小写英文字母(双下标)表达,例如矩阵\(\boldsymbol{X}\)的元素记为\(x_{11}, x_{12}, \ldots, x_{1 n}, x_{21}, \ldots, \ldots, x_{p n}\).

  • 矩阵的列(columns):一般用加粗小写英文字母(单下标)表达,例如矩阵\(\boldsymbol{X}\)的列分别记为\(\boldsymbol{x}_1, \boldsymbol{x}_2, \ldots, \boldsymbol{x}_n\).

  • 整数(integers):英文字母表的一些中间字母一般用作整数,例如\(i, j, k, l, m, n, p, q, r, s, t\)

    • \(i, j, k, l\)一般用作虚拟整数或索引整数(dummy or indexing integers)

    • \(m, n, p, q, r, s, t\)则往往用作固定整数(fixed integers)

    • 例子:\(i=1,2, \ldots, n\); 或者\(\sum_{i = 1}^{n}{X_i}\)

1.2 矩阵运算

R函数的矩阵运算及基本介绍:

  • A * B: Element-wise multiplication.

  • A %*% B: Matrix multiplication.

  • A %o% B: Outer product. \(AB'\)

  • crossprod(A,B), crossprod(A): \(A'B\) and \(A'A\) respectively.

  • t(A): Transpose

  • diag(x): Creates diagonal matrix with elements of x in the principal diagonal

  • diag(A): Returns a vector containing the elements of the principal diagonal

  • diag(k): If \(k\) is a scalar, this creates a \(k \times k\) identity matrix. Go figure.

  • solve(A, b): Returns vector \(\text{x}\) in the equation \(b = A\text{x} (i.e., A^{-1}b)\)

  • solve(A): Inverse of A where A is a square matrix.

  • ginv(A): Moore-Penrose Generalized Inverse of A.

  • ginv(A): requires loading the MASS package.

  • y <- eigen(A): y$val are the eigenvalues of A

  • y$vec: are the eigenvectors of A

  • y <- svd(A): Single value decomposition of A.

    • y$d = vector containing the singular values of A

    • y$u = matrix with columns contain the left singular vectors of A

    • y$v = matrix with columns contain the right singular vectors of A

  • R <- chol(A): Choleski factorization of A. Returns the upper triangular factor, such that R’R = A.

  • y <- qr(A): QR decomposition of A.

    • y$qr has an upper triangle that contains the decomposition and a lower triangle that contains information on the Q decomposition.

    • y$rank is the rank of A.

    • y$qraux a vector which contains additional information on Q.

    • y$pivot contains information on the pivoting strategy used.

  • cbind(A,B,...): Combine matrices(vectors) horizontally. Returns a matrix.

  • rbind(A,B,...): Combine matrices(vectors) vertically. Returns a matrix.

  • rowMeans(A): Returns vector of row means.

  • rowSums(A): Returns vector of row sums.

  • colMeans(A): Returns vector of column means.

  • colSums(A): Returns vector of column sums.

2 实操杂音

2.1 数值近似问题

      x1 x2
[1,]  14 -4
[2,] -34 24

矩阵乘积\(\boldsymbol{X X'=I}\),但实际计算时可能会有浮点值差异(见左边),把精确度稍微减低则可以得到单位矩阵(见右边)。

     [,1]          [,2]
[1,]    1 -1.110223e-16
[2,]    0  1.000000e+00
     [,1] [,2]
[1,]    1    0
[2,]    0    1