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

st: Merging matrices


From   John-Paul Ferguson <jpferg@MIT.EDU>
To   statalist@hsphsun2.harvard.edu
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:

**********
clear
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

v1[3,2]
    c1  c1
r1   1  15
r2   2  28
r3   3  57

. mat list v2

v2[3,2]
    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:

v3[4,3]
    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?

Thanks,
John-Paul Ferguson
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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