MATA Programming
Stata Interface
- Getting Data from Stata to MATA
st_nobs(), st_nvar(), st_data(), st_matrix(), st_view()
- Getting Data from MATA to Stata
st_addvar(), st_store()
- Executing Stata Commands
stata()
Using MATA Built-In Functions
- Data Generating Functions
e(), I(), J(), runiform(), rnormal()
- Data Conversion Functions
rowshape(), colshape(), diag(), diagonal()
vec(), vech(), lowertriangle(), makesymmetric()
- Basic Matrix Functions
- Matrix Description
rows(), cols(), length()
mean(), variance(), correlation()
- Matrix Operations
rowsum(), colsum(), sum(), sort()
rowmin(), colmin(), min(), rowmax(), colmax(), max()
- Matrix Computation
det(), trace(), cond(), rank(), cross()
cholesky(), lud(), grd(), svd()
cholsolve(), lusolve(), qrsolve(), svsolve()
invsym(), cholinv(), luinv()
- Probability Functions
normal(), normalden(), chi2(), chi2tail(), F(), Fden(), Ftail()
- Calculus Functions
deriv(), eigensystem(), optimize()
Controlling Execution Flow
- If Statement
if (exp) ... else ...
- For Loop
for (exp1; exp2; exp3) ...
- While Loop
while (exp) ...
- Do-While Loop
do ... while (exp)
where exp, exp1, exp2, exp3 must evaluate to real scalars.
Statements in ... may be in multiple lines written as
Example 1
Do you know the accuracy of your computer's numerical calculation?
This example addresses this important problem.
Suppose e is a known small positive number, and the 5x4 matrix X is
defined as follows:
1 1 1 1
e 0 0 0
0 e 0 0
0 0 e 0
0 0 0 e
Verify that the eigenvalues of X'X are 4+e2, e2, e2, and e2.
How small of the value of e your computer will allow so that X'X can be inverted?
Writing Your Own Functions
Object Code and Library
- Saving MATA Function (Compiled Object Code)
See: help mata mosave
- Creating MATA Function Library
See: help mata mlib
Example 2
Write a MATA function to convert one or more quarterly time series into the annual
series by taking the average of every four data points.