Matrix of Stata
Matrix of Stata
- Define a -matrix- or -mat- in short: matrix matrixname = matrix_expression.
- matrix define mymat = (1,2\3,4)
- Or, matrix mymat = (1,2\3,4)
- mat list mymat
- Print a matrix:
- -matlist-: Display a matrix and control its format:
- matlist r(table)'*100, tw(20) format(%8.2f)
- Matrix rename: .matrix rename oldname newname
- Rename col/row names:
- matrix colnames A = names
- matrix rownames A = names
- Convert variables to matrix and vice versa:
- Matrix addition & subtraction:
- mat matrix_a + matrix_b
- mat matrix_a - matrix_b
- Matrix multiplication:
- Transpose of a matrix
- Inverse of a matrix, matrix function are defined by using parentheses ():
- mat matrix_inv = inv(matrix_a)
- A\B adds row of B after the rows of A; A,B adds columns B to matrix A. For example:
- mat RTab=r(table)'
- mat R1=RTab[1...,"b"]
- mat R2=RTab[1...,"ll".."ul"]
- mat RDisp=R1,R2
- The usual way to obtain matrices after a command that produces matrices is simply to refer to the returned matrix in the standard way. (source: Stata manual) Or 'get()' matrix function obtains a copy of an internal Stata system matrix.
- 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. For examples:
- matrix list e(b)
- mattrix myE = e(b)*100
- matrix myV = e(V)*100
- matrix myVget=get(_b)
- Then, you can use
- "ereturn post myE myV" to Change coefficient vector and variance-covariance matrix.
- And "ereturn display" to create and display a coefficient table: "r(table)" that have been previously posted using "ereturn post" or "repost". "r(table)" is a matrix containing all the data displayed in the coefficient table (including b, se, t, pvalue, ll, ul, df, crit, and eform).
- Reset row/column names of matrix
- mat A = (1,2,3\ 4,5,6\ 7,8,9)
- matrix rownames A = myrow1 myrow2 myrow3
- mat colnames A = mycol1 mycol2 mycol3
- refix the column names of A with equation names eq1, eq2, and eq3: mat coleq A = eq1 eq2 eq3
- Matrix utilities:
- 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
- Matrix Subscripting:
- Referring to a matrix's element/cell using numbers of row and column or using the names of row and column or using mixed ways:
- disp matrix_name [r,c]
- matrix A = A/A[1,1]
- gen newvar = oldvar/A[2,2]
- mat B = V["price","price"]
- gen sdif = dif/sqrt(V["price","price"])
- Create a matrix B having the first through fifth rows and first through fifth columns of A, using two periods:
- Create a matrix B having the all rows and first column of A, use three periods:
- matrix B = A[1...,1]
- mat B=A[1...,"b"]
- Define an element of a maxtrix:
- Abstract row and column names from a matrix (-rownames- and -colnames-: Macro extended function related to matrices). Also, we can use -rowfullnames matname-, and -colfullnames matname-
- sysuse auto,clear
- regress mpg price weight length
- local rn :rownames r(table)
- local cn :colnames r(table)
- disp "`w1'" // price
- disp "`:word 1 of `:colnames r(table)''" // price
- disp "rownames = `rn'" _n "columnnames = `cn'"
- disp `:word count `cn'' // 4
- disp "`:word count `:colnames r(table)''" // 4
- disp "`:colsof r(table)'" // 4
- disp "`:rowsof r(table)'" // 9
- forval i = 1/`:word count `cn'' {
- local varn: word `i' of `cn'
- disp "`varn'"
- }
- Find out number of row and column (-colsof(matname)- and -rowsof(matname)-): Matrix functions
- disp "number of row = " rowsof(r(table))
- disp "number of column = " colsof(r(table))
- How to do element-by-element operation
- mata, the new matrix language of Stata (since version 9), can do element-by-element operation easily. (read more here)
- Colon operations perform element-by-element operations: addition(:+), subtraction(:-), multiplication(:*), division(:/), power(:^), equality(:==), inequality(:!=), greater than(:>), greater than or equal to(:>=), less than(:<), less than or equal to(:<=), and(:&), or(:|)
- If you apply the exponential function on each element:
- . mata
- : x=(0,1\2,3)
- : exp(x) // x = (1,2.7\7.4,20.1)
- : exp((0,1\2,3)) // (1,2.7\7.4,20.1)
- : x:/2 // x = (0,0.5\1, 1.5)
- : end
- mata can use a Stata matrix
- matrix A=r(table)'
- mata: exp(st_matrix("A")) // display only
- mata: st_matrix("se",sqrt(diagonal(st_matrix("V")))) // create a matrix of standard deviation
- mata: st_matrix("expA", exp(st_matrix("A"))) // create a new matrix, expA
- matrix eformtab=expA[1..., "b"], expA[1...,"ll".."ul"], expA[1...,"se"]
- matlist eformtab*100, tw(30) format(%8.1f)
- More about mata
- create row vector: row=(1,2,3) or row=(1..3)
- create column vector: col=(1\2\3) or col=(1::3)
- transpose a matrix: m2=(0,1\2,3)'
- scalar functions will operate on elements: sqrtrow=sqrt(row)
- SSCC: an introduction to Mata
No comments:
Post a Comment