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

RE: st: Merging matrices


From   "Scott Merryman" <smerryman@kc.rr.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Merging matrices
Date   Wed, 8 Jun 2005 19:27:41 -0500

Another way is to use Nick Cox's -tabcount- which will tabulate missing
values.

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

tabcount var1, v(1/4) zero mat(m1)
tabcount var2, v(1/4) zero mat(m2)
levels var1, local(l1)
levels var2, local(l2)
local l3 : list l1 | l2
local rows : subinstr local l3 " " ",", all
matrix rows = (`rows')'
matrix freq = rows, m1, m2
matrix list freq

Hope this helps,
Scott


> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-
> statalist@hsphsun2.harvard.edu] On Behalf Of Nick Winter
> Sent: Wednesday, June 08, 2005 7:03 PM
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: Merging matrices
> 
> I'm sure there is something better, but what occurs to me is to use -
> svmat-
> and -merge-.  Something like this, after the code you've generated:
> 
>    drop _all
>    svmat v1
>    rename v11 value
>    sort value
>    save temp1
>    drop _all
>    svmat v2
>    rename v21 value
>    sort value
>    merge value using temp1
> 
>    foreach v in v12 v22 {
>          replace `v' = 0 if mi(`v')
>    }
>    sort value
> 
> 
> This would need to be generalized a bit, and would need to rename/label
> the
> resulting variables better, etc., but might be a viable approach.
> 
> --Nick Winter
> 
> At 07:18 PM 6/8/2005, you wrote:
> >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/
> 
> --------------------------------------------------------
> Nicholas Winter                           607.255.8819 t
> Assistant Professor                       607.255.4530 f
> Department of Government              nw53@cornell.edu e
> 308 White Hall            falcon.arts.cornell.edu/nw53 w
> Cornell University
> Ithaca, NY 14853-4601
> 
> *
> *   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/


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