help with a varaible matrix...

#1
I am trying to report mean values based on values taken by two varaibles.
both of variables take values 1,2 or 3 and i would like to make a table that reports mean such that:

| var1=1 | var1=2 | var1=3
------------------------------------------------------
var2=1 | mean(1_1) | mean(1_2) | mean(1_3)
| | |
var2=2 | mean(2_1) | mean(2_2) | mean(2_3)
| | |
var2=3 | mean(3_1) | mean(3_2) | mean(3_3)
| | |

ive been playing around with estpost, esttab, etc but cant figure it out. please help if possible...
 
#2
Hi there,

You do not necessarily need matrices to do it. Simply write:

tab var2 var1, s(xvar) nof nost


If you want to use matrices it's a bit more complex. Let's say that you want to store this in a matrix called A. First write the different lines of the matrix :

forv i = 1 / 3 {
forv j = 1 / 3 {
sum xvar if var2 == `i' & var1 == `j'

// now you need to intialize your matrix
if `j' == 1 {
matrix A`i' = (r(mean))
}

// for column 2 and 3 just add it to matrix A`i'
if `j' > 1 {
matrix A`i' = (A`i', r(mean))

}
}
}

now stack the matrices A1 to A3 :

matrix A = (A1 \ A2 \ A3)

and use estout to display the table :

estout matrix(A)

Hope this helps!
 
Last edited: