Stata User's Guide
Enviado por amerika101284 • 10 de Diciembre de 2013 • 1.774 Palabras (8 Páginas) • 300 Visitas
14.8 Matrix functions
In addition to the functions listed below, see [P] matrix svd for singular value decomposition,
[P] matrix symeigen for eigenvalues and eigenvectors of symmetric matrices, and see [P] matrix
eigenvalues for eigenvalues of nonsymmetric matrices. For a full description of the matrix functions,
see [D] functions.
Matrix functions returning matrices:
cholesky(M) I(n) nullmat(matname)
corr(M) inv(M) sweep(M,i)
diag(v) invsym(M) vec(M)
get(systemname) J(r,c,z) vecdiag(M)
hadamard(M,N) matuniform(r,c)
Matrix functions returning scalars:
colnumb(M,s) el(M,i,j) rownumb(M,s)
colsof(M) issymmetric(M) rowsof(M)
det(M) matmissing(M) trace(M)
diag0cnt(M) mreldif(X,Y )
[ U ] 14.9 Subscripting 173
14.9 Subscripting
1. In matrix and scalar expressions, you may refer to matname[r,c], where r and c are scalar
expressions, to obtain one element of matname as a scalar.
Examples:
matrix A = A / A[1,1]
generate newvar = oldvar / A[2,2]
2. In matrix expressions, you may refer to matname[sr,sc], where sr and sc are string expressions,
to obtain a submatrix with one element. The element returned is based on searching the row and
column names.
Examples:
matrix B = V["price","price"]
generate sdif = dif / sqrt(V["price","price"])
3. In matrix expressions, you may mix these two syntaxes and refer to matname[r,sc] or to
matname[sr,c].
Example:
matrix b = b * R[1,"price"]
4. In matrix expressions, you may use matname[r1..r2,c1..c2] to refer to submatrices; r1, r2, c1,
and c2 may be scalar expressions. If r2 evaluates to missing, it is taken as referring to the last
row of matname; if c2 evaluates to missing, it is taken as referring to the last column of matname.
Thus matname[r1...,c1...] is allowed.
Examples:
matrix S = Z[1..4, 1..4]
matrix R = Z[5..., 5...]
5. In matrix expressions, you may refer to matname[sr1..sr2,sc1..sc2] to refer to submatrices
where sr1, sr2, sc1, and sc2, are string expressions. The matrix returned is based on looking up
the row and column names.
If the string evaluates to an equation name only, all the rows or columns for the equation are
returned.
Examples:
matrix S = Z["price".."weight", "price".."weight"]
matrix L = D["mpg:price".."mpg:weight", "mpg:price".."mpg:weight"]
matrix T1 = C["mpg:", "mpg:"]
matrix T2 = C["mpg:", "price:"]
6. In matrix expressions, any of the above syntaxes may be combined.
Examples:
matrix T1 = C["mpg:", "price:weight".."price:displ"]
matrix T2 = C["mpg:", "price:weight"...]
matrix T3 = C["mpg:price", 2..5]
matrix T4 = C["mpg:price", 2]
174 [ U ] 14 Matrix expressions
7. When defining an element of a matrix, use
matrix matname[i,j] = expression
where i and j are scalar expressions. The matrix matname must already exist.
Example:
matrix A = J(2,2,0)
matrix A[1,2] = sqrt(2)
8. To replace a submatrix within a matrix, use the same syntax. If the expression on the right evaluates
to a scalar or 11 matrix, the element is replaced. If it evaluates to a matrix, the submatrix with
top-left element at (i; j) is replaced. The matrix matname must already exist.
Example:
matrix A = J(4,4,0)
matrix A[2,2] = C'*C
14.10 Using matrices in scalar expressions
Scalar expressions are documented as exp in the Stata manuals:
generate newvar = exp if exp : : :
replace newvar = exp if exp : : :
regress : : : if exp : : :
if exp {: : : }
while exp {: : : }
Most importantly, scalar expressions occur in generate and replace, in the if exp modifier allowed
on the end of many commands, and in the if and while commands for program control.
You will rarely need to refer to a matrix in any of these situations except when using the if and
while commands.
In any case, you may refer to matrices in any of these situations, but the expression cannot require
evaluation of matrix expressions returning matrices. Thus you could refer to trace(A) but not to
trace(A+B).
It can be difficult to predict when an evaluation of an expression requires evaluating a matrix;
even experienced users can be surprised. If you get the error message “matrix operators that return
matrices not allowed in this context”, r(509), you have encountered such a situation.
The solution is to split the line in two. For instance, you would change
if trace(A+B)==0 {
: : :
}
to
matrix AplusB = A+B
if trace(AplusB)==0 {
: : :
}
or even to
matrix Trace = trace(A+B)
if Trace[1,1]==0 {
: : :
}
15 Saving and printing output—log files
Contents
15.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
15.1.1 Starting and closing logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
15.1.2 Appending to an existing log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
15.1.3 Temporarily suspending and resuming logging . . . . . . . . . . . . . . . . . . . . . . . . 178
15.2 Placing comments in logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
15.3 Logging only what you type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
15.4 The log-button alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
15.5 Printing logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
15.6 Creating multiple log files simultaneously . . . . . . . . . . . . . . . . . . . . . . .
...