Multivariate Analysis
Matrix Tricks


How to turn a vector into a matrix

This is the same method we used to create the mean matrix in Mean Vectors and Matrices.

Consider the Vector v

Create a Unit Vector

Multiply u*v
Using Stata
mat v = (3,7,-2)

mat u = J(4,1,1)

mat X = u*v

mat lis X

X[4,3]
    c1  c2  c3
r1   3   7  -2
r2   3   7  -2
r3   3   7  -2
r4   3   7  -2


Dividing Column Elements

In this example we will divide the elements in Column 1 by 2 and the elements in Column 2 by 3.

Consider the Matrix X

Create a Vector with Division Elements

Create a Diagonal Matrix of the Elements of v

Take the Inverse of the Diagonal Matrix

Note: The inverse of a diagonal matrix contains the reciprocals of the diagonal elements.

Compute X * D-1

Using Stata
mat X = (4,9\10,12\2,9)

mat v = (2,3)

mat list v

v[1,2]
    c1  c2
r1   2   3

mat D = diag(v)

mat list D

symmetric D[2,2]
    c1  c2
c1   2
c2   0   3

mat D = syminv(D)

mat list D

symmetric D[2,2]
           c1         c2
c1         .5
c2          0  .33333333

mat Q = X*D

mat list Q

Q[3,2]
    c1  c2
r1   2   3
r2   5   4
r3   1   3

/* quick way with one command */

mat Q2 = X*syminv(diag(v))

mat list Q2

Q2[3,2]
    c1  c2
r1   2   3
r2   5   4
r3   1   3


Compute the Square Root of the Elements of a Diagonal Matrix

Stata & Mata Side-by-side

Stata                               Mata
mat D = (64,0,0\0,144,0\0,0,20)     : D = (64,0,0\0,144,0\0,0,20)

mat list D                          : D

symmetric D[3,3]                  
     c1   c2   c3                        
r1   64
r2    0  144
r3    0    0   20

mat S = cholesky(D)                 : S = sqrt(D)

mat list S                          : S

symmetric S[3,3]
          c1        c2        c3
r1         8
r2         0        12
r3         0         0  4.472136


Compute a Correlation Matrix from a Covariance Matrix

The long (most general) way that makes use of the formula rxy = cov(xy)/(sx*sy).

Using Stata

mat C = (7, -2.5\-2.5,1)
mat list C, title(covariance matrix)

symmetric S[2,2]:  covariance matrix
     c1   c2
c1    7
c2  -2.5   1

mat S = diag(vecdiag(C))
mat S = cholesky(S)
mat list S            /* diagonal matrix of standard deviations */

symmetric S[2,2]
           c1         c2
c1  2.6457513
c2          0          1

mat R = syminv(S)*C*syminv(S)
mat list R

symmetric R[2,2]
            c1          c2
c1           1
c2  -.94491118           1
Now for the really fast way.
mat R = corr(C)
mat list R

symmetric R[2,2]
            c1          c2
c1           1
c2  -.94491118           1


Converting a Matrix to a Scalar & vice versa

Using Stata
mat X = (32)

mat list X

symmetric X[1,1]
    c1
r1  32

display "X = ", X

X =  type mismatch

/* using the el function */

scalar x = el(X,1,1)

display "x = ", x

x =  32

/* also try this */

scalar z = X[1,1]

display "z = ", z

z =  32

/* matrix Y must exist */

mat Y = (0)

mat list Y

symmetric Y[1,1]
    c1
r1   0

mat Y[1,1] = x

mat list Y

symmetric Y[1,1]
    c1
r1  32


Means of Multiple Groups

Say that our X (7x4) matrix is really composed of three groups. The first group has three subjects while the groups two and three have two each. Here is how we can compute the group total and mean vector and the mean matrix.

Using Stata

mat X = (3,9,17,24\7,8,11,25\6,5,13,29\4,7,15,32\7,9,13,24\8,8,1,23\7,9,12,23)
mat list X

X[7,4]
    c1  c2  c3  c4
r1   3   9  17  24
r2   7   8  11  25
r3   6   5  13  29
r4   4   7  15  32
r5   7   9  13  24
r6   8   8   1  23
r7   7   9  12  23

mat u2=(1,0,0\1,0,0\1,0,0\0,1,0\0,1,0\0,0,1\0,0,1)
mat list u2

u2[7,3]
    c1  c2  c3
r1   1   0   0
r2   1   0   0
r3   1   0   0
r4   0   1   0
r5   0   1   0
r6   0   0   1
r7   0   0   1

/* compute column sums for each group */

mat t=u2'*X
mat list t

t[3,4]
    c1  c2  c3  c4
c1  16  22  41  78
c2  11  16  28  56
c3  15  17  13  46

/* compute the n for each group */

mat n = u2'*u2
mat list n

symmetric n[3,3]
    c1  c2  c3
c1   3
c2   0   2
c3   0   0   2

/* compute column means for each group */

mat m = syminv(n)*t
mat list m

m[3,4]
           c1         c2         c3         c4
c1  5.3333333  7.3333333  13.666667         26
c2        5.5          8         14         28
c3        7.5        8.5        6.5         23

/* compute mean matrix containing the column means for each group */

mat M = u2*m
mat list M

M[7,4]
           c1         c2         c3         c4
r1  5.3333333  7.3333333  13.666667         26
r2  5.3333333  7.3333333  13.666667         26
r3  5.3333333  7.3333333  13.666667         26
r4        5.5          8         14         28
r5        5.5          8         14         28
r6        7.5        8.5        6.5         23
r7        7.5        8.5        6.5         23


Quick Correlation

Here is a quick way to get the correlation between two vectors.

Stata & Mata Side-by-side

Stata                                                  Mata
mat    x = (3\6\9\8\4\8\6\7)                           : x  = (3\6\9\8\4\8\6\7)
mat    y = (4\4\5\6\5\6\7\5)                           : y  = (4\4\5\6\5\6\7\5)
scalar n = rowsof(x)                                   : xy = x,y
mat    u = J(n,1,1)                                    

/*  deviation scores  */
mat x = x - u*((u'*x)/n)                               
mat y = y - u*((u'*y)/n)     
mat xy = x,y

/* variance-covariance matrix */
mat cov = (xy'*xy)/(n-1)

mat c = corr(cov)                                      : c = correlation(xy, 1)

display c[2,1]                                         : c[1,2]

.41753744                                                .4175374443


Multivariate Course Page

Phil Ender, 15jul07, 27oct05, 11oct05, 30Jun98