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

st: Merging matrices

From   John-Paul Ferguson <[email protected]>
To   [email protected]
Subject   st: Merging matrices
Date   Wed, 8 Jun 2005 19:18:19 -0400

Hello all,

I am trying to write a program that (in part) displays a special table showing
the frequency counts of two variables. I am currently using -tabulate- on the
two variables and extracting the necessary row and frequency names. It's then
easy to show TWO tables with the frequencies, via simple matrix subscripting.
The difficulty that showing them in one table imposes is that, unless the list
of row names is the same in the two variables, displaying the stored
frequencies via simple matrix subscripting will line up the rows incorrectly.

To make this clearer, consider the following code:

set obs 100
set seed 1234567
forv i=1/2 {
     g var`i'=1
     replace var`i'=2 if uniform()>.4
     replace var`i'=3 if uniform()>.3
replace var2=4 if var2==3
tab var1, matcell(freqs1) matrow(nums1)
mat v1=(nums1,freqs1)
tab var2, matcell(freqs2) matrow(nums2)
mat v2=(nums2,freqs2)

The two resulting matrices will look as follows:

. mat list v1

    c1  c1
r1   1  15
r2   2  28
r3   3  57

. mat list v2

    c1  c1
r1   1  21
r2   2  25
r3   4  54

What I would like to create is a hypothetical v3, which looks like this:

    c1  c1  c1
r1   1  15  21
r2   2  28  25
r3   3  57   0
r4   4   0  54

I've been poring over the -matrix- commands, and I just can't figure out a way
to create this matrix. I've also looked at -tabcount- and -groups-, without
success (I know that Nick Cox wrote on these recently, but our library's copy
of the SJ is at the bindery, worse luck!).

Does anyone have any ideas for producing v3, as described above?

John-Paul Ferguson
*   For searches and help try:

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