Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Stata's reporting of rows and columns in matrices


From   Elmer Villanueva <[email protected]>
To   Statalist <[email protected]>
Subject   st: Stata's reporting of rows and columns in matrices
Date   Sat, 26 Mar 2005 11:35:01 +1100

Hi All!

I note that Stata has some quirky results when reporting the contents of matrices derived from operations involving transposes of other matrices. I speak of the row and column markers. Watch carefully.

I create two matrices, A and B:

. matrix A = blah blah

. matrix B = blah blah

. matrix list A

A[2,3]
c1 c2 c3
r1 1 -1 2
r2 0 3 4

. matrix list B

B[2,3]
c1 c2 c3
r1 4 0 -3
r2 -1 -2 3

Everything's okay so far. Watch what happens when I ask for a product.

. matrix Q=A*B'

. matrix list Q

Q[2,2]
r1 r2
r1 -2 7
r2 -12 6

Note how the column markers are defined.

There seems to be no consistency, too. Watch what happens when I calculate another matrix:

. matrix R=A'*B

. matrix list R

R[3,3]
c1 c2 c3
c1 4 0 -3
c2 -7 -6 12
c3 4 -8 6


Now, it's the row markers that have changed!

This gets really confusing when the result isn't a square matrix. Watch:

. matrix C = blah blah

. matrix list C

C[3,4]
c1 c2 c3 c4
r1 2 -3 0 1
r2 5 -1 -4 2
r3 -1 0 0 3

If I don't have a transpose, everything is fine:

. matrix S=B*C

. matrix list S

S[2,4]
c1 c2 c3 c4
r1 11 -12 0 -5
r2 -15 5 8 4

However,

. matrix T=C'*B'

. matrix list T

T[4,2]
r1 r2
c1 11 -15
c2 -12 5
c3 0 8
c4 -5 4

The row and column labels have swapped completely!

Pretty neat, eh?

Now, I realise that rows and columns can be called anything and "r" and "c" don't NEED to stand for rows and columns. In fact, I regularly use such nonsense markers as "cow" and "chicken" to stand for rows and columns in 2x2 tables to emphasise the need to concentrate on the process and not the particular elements. However, this is fascinating behaviour from Stata. Anyone care to comment?

Have a safe Easter break.


Thanks,

Elmer


Elmer V Villanueva, MD ScM FRIPH
Epidemiologist

National Breast Cancer Centre
Locked Bag 16 Camperdown NSW 1450
Level 4, 92 Parramatta Road�Camperdown NSW 2050
Telephone 61 2 9036 3035 Facsimile 61 2 9036 3077 Mobile 0439 947 544 www.nbcc.org.au





© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index