Vectors and Matrices in STATA

 

This session is intended to introduce some of the features in STATA for dealing with vectors and matrices.

 

/* Inputting a Matrix */

 

matrix A = (2,1\3,2\-2,2)

matrix list A

 

/* Multiplication by a Scalar */

 

mat B = 3*A

mat lis B

 

/* Matrix Addition & Subtraction */

 

mat B = (1,1\4,2\-2,1)

mat C = A + B

mat lis C

mat D = A - B

mat lis D

/* Matrix Multiplication */

 

mat D = (2,1,3\-2,2,1)

mat lis D

mat C = D*A

mat lis C

mat C = A*D

mat lis C

mat D = (2,1,3)

mat C = D*A

mat lis C

mat C = A*D††††††††††† /* Note the conformability error in the last command. Order makes a difference in matrix multiplication. */

 

/*Element by Element multiplication of two matrices */

 

mat D = (2,1,3\-2,2,1)

mat E = (3,2,4\-1,1,0)

mat F = hadamard(D,E)

mat lis F

 

/* Transpose of a Matrix */

 

mat AT = A'

mat lis AT

mat ATT = AT'

mat lis ATT

 

/* Commonly Used Vector */

 

/*Unit Vector*/

 

mat i = J(3,1,1)

mat lis i

 

mat N = ií*i

mat lis N

 

/* Common Matrices */

 

/*Unit Matrix*/

 

mat U = J(3,2,1)

mat lis U

 

/*Diagonal Matrix and Vector*/

 

mat S = (2,1,4\3,2,2\-2,2,3)

mat lis S

 

/* Extract the diagonal of S into a vector */

 

matrix d=vecdiag(S)

mat lis d

 

/* Create a diagonal matrix from d */

 

mat D = diag(vecdiag(S))

mat lis D

 

/* Another Example */

 

mat V = (3,1,2)

mat D = diag(V)

mat lis D

 

/*Identity Matrix*/

 

mat I = I(3)

mat lis I

 

/* Trace of a matrix */

 

mat T = trace(I)

mat lis T

 

/*Symmetric Matrix*/

 

mat C = (2,1,5\1,3,4\5,4,-2)

mat lis C

mat CT = C'

mat lis CT

 

/*Inverse of a Matrix*/

 

matrix A = (4,2,2 \ 4,6,8 \ -2,2,4)

matrix list A

matrix A1 = inv(A)

matrix list A1

 

/*Inverse & Determinant of a Matrix*/

 

mat C = (2,1,6\1,3,4\6,4,-2)

mat CI = syminv(C)

mat lis CI

 

scalar d = det(C)

display d

 

/* Eigenvalues and Eigenvectors of a Symmetric Matrix */

 

matrix r = (1,.7\.7,1)

matrix list r

matrix symeigen a l = r

/* eigenvalues */

 

matrix list l

 

/* eigenvectors */

 

matrix list a

 

/*Number of Rows & Columns*/

 

mat X = (3,2\2,-2\4,6\3,1)

mat lis X

scalar r = rowsof(X)

scalar c = colsof(X)

 

/*Computing Column & Row Sums*/

 

mat A = (2,1\3,2\-2,2)

mat lis A

mat U = J(rowsof(A),1,1)

mat list U

mat c = U'*A

mat list c

 

/*Computing Column & Row Means*/

 

mat cm = c/rowsof(A)

mat lis cm

 

/*Horizontal Concatenation*/

 

mat A = (2,1\3,2\-2,2)

mat lis A

 

mat B = (1,1\3,4\2,2)

mat lis B

mat C = A,B

mat lis C

 

/*Vertical Concatenation (Appending)*/

 

mat C = A\B

mat lis C

 

/*Matrix Subsetting*/

/* first create large matrix to be subset */

 

mat A = (2,1\3,2\-2,2)

mat B = (1,1\3,4\5,0)

mat C = A,B\B,A

mat lis C

 

/*create a matrix with 3kcolumns */

 

mat D = C[1...,1..3]

mat lis D

 

/* create a matrix with 2 rows */

 

mat D = C[1..2,1...]

mat lis D

 

/* Choleski Factorization */

 

matrix m = (5,1\1,3)

mat lis m

matrix cm = cholesky(m)

mat lis cm

 

/*Check the choleski factorization of m. cm*cm'=m */

 

matrix testm = cm * cm'

mat lis testm

 

/*Check the choleski factorization of m. cm*cm'=m */

 

matrix testm = cm * cm'

mat lis testm

 

/* Singular Value Decomposition */

 

matrix A = (1,2,9\2,7,5\2,4,18)

matrix svd U w V = A

matrix list U

matrix list w

matrix list V

 

/* Check the Singular Value Decomposition */

 

matrix newA = U*diag(w)*V'

matrix list newA