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

st: RE: Frequencies and proportions side by side in tables


From   "Nick J. Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Frequencies and proportions side by side in tables
Date   Fri, 23 Jan 2004 15:16:55 -0000

There is some discussion of related
problems in the Speaking Stata columns 
in Stata Journal 3(3) and 3(4) 2003, 
and pointers to pertinent software, but 
that did not extend to any canned solution 
yielding precisely this. 

An alternative route starts from Roger Newson's 
-pcontract- and -xcontract- (Roger: 
why are there are two commands?). 
This example will give the flavour; the
commands offer a lot more flexibility. 

The key, however, to "side-by-side" tabulations
is often that you need to create composite
_string_ variables with the cell contents
you want. 

. sysuse auto
(1978 Automobile Data)

. pcontract for rep78, zero f(freq) p(percent) 

. l

     +-----------------------------------+
     |  foreign   rep78   freq   percent |
     |-----------------------------------|
  1. | Domestic       1      2      2.70 |
  2. | Domestic       2      8     10.81 |
  3. | Domestic       3     27     36.49 |
  4. | Domestic       4      9     12.16 |
  5. | Domestic       5      2      2.70 |
     |-----------------------------------|
  6. | Domestic       .      4      5.41 |
  7. |  Foreign       1      0      0.00 |
  8. |  Foreign       2      0      0.00 |
  9. |  Foreign       3      3      4.05 |
 10. |  Foreign       4      9     12.16 |
     |-----------------------------------|
 11. |  Foreign       5      9     12.16 |
 12. |  Foreign       .      1      1.35 |
     +-----------------------------------+

. gen show = string(freq) + " (" + string(percent, "%3.2f") + ")" 
(12 real changes made)

. tabdisp for rep78, c(show) 

------------------------------------------------------------------------
----------
          |                           Repair Record 1978

 Car type |          1           2           3           4           5
.
----------+-------------------------------------------------------------
----------
 Domestic |   2 (2.70)   8 (10.81)  27 (36.49)   9 (12.16)    2 (2.70)
4 (5.41)
  Foreign |   0 (0.00)    0 (0.00)    3 (4.05)   9 (12.16)   9 (12.16)
1 (1.35)
------------------------------------------------------------------------
----------

Nick 
[email protected] 

> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]]On Behalf Of n p
> Sent: 23 January 2004 14:33
> To: [email protected]
> Subject: st: Frequencies and proportions side by side in tables
> 
> 
> Hi statalisters,
> Is there a way to show tables of frequencies and
> proportions (row or column) like in the tabulate
> command but with the proportions side by side with the
> frequencies?
> 
> Something like (row proportions case):
> 
> 
>          Education Level
>          High      |  Low
> Gender   N (%)     |  N(%)
> -------------------------------
> Male     4 (40.0)  |  6  (60.0)
> Female   3 (15.0)  |  17 (85.0) 
> --------------------------------
> 
> I tried to implement it using something like
> 
> preserve
> gen cons=1
> bysort gender:egen N=sum(cons)
> bysort gender education:egen n=sum(cons)
> bysort gender education:keep if _n==1
> gen perc=n/N*100
> .
> .
> reshape ....
> list ....
> restore
> 
> 
> but I had problems dealing with cells with zero
> frequencies. I also did a quick search (findit,
> archives) without success.
> The primary use of such a command would be
> copy-pasting in word processors.

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index