[P] matrix -- Summary of matrix commands
Comments are provided below under the following headings.
Subject Also see help
1. Inputting matrices by hand matrix define
2. Setting the maximum matrix size matsize
3. Matrix expressions
Matrix operators matrix operators
Matrix functions matrix functions
4. Matrix subscripting matrix subscripting
5. Submatrix extraction matrix extraction
6. Submatrix substitution matrix substitution
7. Data <---> Matrix conversion mkmat
8. Obtaining copies of system matrices matrix get
9. Matrix decomposition
Eigenvalues & vectors of symmetric matrices matrix symeigen
Eigenvalues of nonsymmetric matrices matrix eigenvalues
Singular value decomposition matrix svd
10. Setting row and column names matrix rownames
11. Macro extended functions regarding matrices matmacfunc
12. Accumulating cross-product matrices matrix accum
13. Generating scores from coefficient vectors matrix score
14. (Dis)similarity measures matrix dissimilarity
15. Constraint processing makecns
16. Matrix utilities matrix utility
In the syntax diagrams, capital letters A, B, ..., Z stand for matrix
names. Full details can be found in [P] matrix.
Beyond the matrix commands, Stata has a complete matrix programming
language, Mata, that provides more advanced matrix functions, support for
complex matrices, fast execution speed, and the ability to directly
access Stata's data, macros, matrices, and returned results. Mata can be
used interactively as a matrix calculator, but it is even more useful for
programming; see [M-0] intro.
1. Inputting matrices by hand (see [P] matrix define)
matrix input A = (#[,#...] [\ #[,#...] [\ [...]]])
. matrix input mymat = (1,2\3,4)
. matrix input myvec = (1.7, 2.93, -5, 3)
. matrix input mycol = (1.7\ 2.93\ -5\ 3)
The above would also work if you omitted the input subcommand:
. matrix X = (1+1, 2*3/4 \ 5/2, 3)
is understood but
. matrix input X = (1+1, 2*3/4 \ 5/2, 3)
would produce an error.
matrix input, however, has two other advantages. First, it allows input
of large matrices. (The expression parser is limited because it must
"compile" the expression and, if it is too long, will produce an error.)
Second, matrix input allows you to omit the commas.
2. Setting the maximum matrix size (see [R] matsize)
Your maximum matrix size currently is 400 by 400. This can be increased
to 800 x 800 if you are using Stata/IC or 11,000 x 11,000 if you are
using Stata/SE or Stata/MP. The details of changing this limit are found
in [R] matsize.
The maximum matrix size limit may seem restrictive. Because Stata does
not need to load the data into a matrix to compute estimation results
this does not pose a problem in practice. See "Accumulating
cross-product matrices" below.
3. Matrix expressions (see [P] matrix define;
and matrix functions)
Complex matrix expressions are allowed within Stata.
matrix A = matrix_expression
. matrix D = B
. matrix beta = invsym(X'*X)*X'*y
. matrix C = (C+C')/2
. matrix sub = x[1..., 2..5]/2
. matrix L = cholesky(0.1*I(rowsof(X)) + 0.9*X)
The available matrix operators and functions are detailed in [P] matrix
define; see matrix operators and matrix functions.
4. Matrix subscripting (see [P] matrix define;
matrix A = ... B[r,c] ...
where r and c are numeric or string scalar expressions.
. matrix A = A/A[1,1]
. matrix B = A["weight","displ"]
. matrix D = G[1,"eq1:l1.gnp"]
Subscripting with numeric expressions may be used in any expression
context (such as generate and replace). Subscripting by row/column name
may be used only in a matrix context. (This latter is not a constraint;
see the rownumb() and colnumb() matrix functions returning scalar in [P]
matrix define and matrix functions; they may be used in any expression
5. Submatrix extraction (see [P] matrix define;
matrix A = ... B[r0..r1, c0..c1] ...
where r0, r1, c0, and c1 are numeric or string scalar expressions.
. matrix A = B[2..4, 3..6]
. matrix A = B[2..., 2...]
. matrix A = B[1, "price".."mpg"]
. matrix A = B["eq1:", "eq1:"]
6. Submatrix substitution (see [P] matrix define;
matrix A[r,c] = ...
where r and c are numeric scalar expressions.
If the matrix expression to the right of the equal sign evaluates to a
scalar or 1 x 1 matrix, the indicated element of A is replaced. If the
matrix expression evaluates to a matrix, the resulting matrix is placed
in A with its upper left corner at (r,c).
. matrix A[2,2] = B
. matrix A[rownumb(A,"price"), colnumb(A,"mpg")] = sqrt(2)
7. Data <---> Matrix conversion (see [P] matrix mkmat)
Variables can be converted into matrices and likewise matrices can be
converted into variables. The details are found in [P] matrix mkmat.
8. Obtaining copies of system matrices (see [P] matrix get)
The usual way to obtain matrices after a command that produces matrices
is simply to refer to the returned matrix in the standard way. For
instance all estimation commands return
e(b) coefficient vector
e(V) variance-covariance matrix of the estimates (VCE)
And these matrices can be referenced directly.
. matrix list e(b)
. matrix myV = e(V)
Other matrices are returned by various commands. They are obtained in
the same way. The get() function also obtains matrices after certain
commands; see [P] matrix get.
9. Matrix decomposition (see [P] matrix symeigen,
[P] matrix eigenvalues,
and [P] matrix svd)
Obtaining the eigenvalues and eigenvectors from a symmetric matrix is
detailed in [P] matrix symeigen. Obtaining the real and imaginary parts
of the eigenvalues of a square matrix is detailed in [P] matrix
eigenvalues. Obtaining the singular value decomposition of a matrix is
detailed in [P] matrix svd. If you desire the Cholesky factorization or
the matrix sweep function, then see the cholesky() and sweep() matrix
functions returning matrices in [P] matrix define; also see matrix
10. Setting row and column names (see [P] matrix rownames)
Row and column names of matrices in Stata have special meaning -- they
tell the names of the variables, equations, and time-series operators
that helped create the matrix. Stata automatically carries these names
along during matrix operations and uses the names to produce
appropriately labeled command output.
In most cases you do not need to worry about setting matrix row and
column names yourself. See [P] matrix rownames for details of how to
manually set matrix row and column names.
11. Macro extended functions regarding matrices (see [P] matrix define;
The following extended macro functions are allowed with local and global:
: rowfullnames A
: colfullnames A
: rownames A
: colnames A
: roweq A
: coleq A
. local names : rownames mymat
. local names : rowfullnames mymat
. local names : colfullnames e(b)
12. Accumulating cross-product matrices (see [P] matrix accum)
Most statistical computations involve matrix operations such as X'X or
X'WX. In these cases X may have a very large number of rows and usually
a small to moderate number of columns. W usually takes on a restricted
form (diagonal, block diagonal, or is known in some functional form and
need not be stored). Computing X'X or X'WX by storing the matrices and
then directly performing the matrix multiplications is inefficient and
wasteful. Stata has matrix cross-product accumulation commands that will
compute these results efficiently. See [P] matrix accum.
13. Generating scores from coefficient vectors (see [P] matrix score)
Scoring refers to forming linear combinations of variables in the data
with respect to a coefficient vector. This is easily accomplished using
the matrix score command; see [P] matrix score.
14. (Dis)similarity measures (see [P] matrix dissimilarity)
Many similarity, dissimilarity, and distance measures for continuous or
binary data are available; see [MV] measure_option. matrix dissimilarity
allows you to compute these (dis)similarities between observations or
variables; see [P] matrix dissimilarity.
15. Constraint processing (see [P] makecns)
Estimation commands that allow constrained estimation define constraints
using the constraint command. Program writers can incorporate these same
features using the commands in [P] makecns.
16. Matrix utilities (see [P] matrix utility)
There are matrix utilities to
matrix dir List the currently defined matrices
matrix list Display the contents of a matrix
matrix rename Rename a matrix
matrix drop Drop a matrix
See [P] matrix utility for details.