Mean vectors, also known as mean value vectors, and mean matrices are fundamental structures in a number of multivariate techniques.
In Stata
/* raw score matrix w/ 2 columns (variables) and 5 rows (observtions or subjects) */ mat X = (3,7\1,5\2,3\5,4\4,6) mat list X X[5,2] c1 c2 r1 3 7 r2 1 5 r3 2 3 r4 5 4 r5 4 6 /* create unit vector */ mat U = J(rowsof(X),1,1) mat list U U[5,1] c1 r1 1 r2 1 r3 1 r4 1 r5 1 /* create vector of column (variable) sums */ mat sum = U'*X mat lis sum sum[1,2] c1 c2 c1 15 25 /* create vector of column (variable) means */ mat meanvec = sum/rowsof(X) mat list meanvec meanvec[1,2] c1 c2 c1 3 5 /* use unit vector to create matrix of column (variable) means */ mat meanmat = U*meanvec mat list meanmat meanmat[5,2] c1 c2 r1 3 5 r2 3 5 r3 3 5 r4 3 5 r5 3 5In Mata
/* raw score matrix w/ 2 columns (variables) and 5 rows (observtions) */ X = (3,7\1,5\2,3\5,4\4,6) X 1 2 +---------+ 1 | 3 7 | 2 | 1 5 | 3 | 2 3 | 4 | 5 4 | 5 | 4 6 | +---------+ /* create vector of column (variable) means */ meanvec = mean(X) meanvec 1 2 +---------+ 1 | 3 5 | +---------+ /* create unit vector */ U = J(rows(X),1,1) U 1 +-----+ 1 | 1 | 2 | 1 | 3 | 1 | 4 | 1 | 5 | 1 | +-----+ /* use unit vector to create matrix of column (variable) means */ meanmat = U*meanvec meanmat 1 2 +---------+ 1 | 3 5 | 2 | 3 5 | 3 | 3 5 | 4 | 3 5 | 5 | 3 5 | +---------+What if you wanted the row (subject) means instead of the column (variable) means. In Mata just use the transpose of the matrix.
rowmean = mean(X') rowmean 1 2 3 4 5 +-------------------------------+ 1 | 5 3 2.5 4.5 5 | +-------------------------------+
Multivariate Course Page
Phil Ender, 15jul07