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

st: -tabcond- available from SSC


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: -tabcond- available from SSC
Date   Thu, 7 Nov 2002 15:30:35 -0000

On Saturday, Kazi Ali Toufique posted a problem about tabulating
all codes, specifically a desire to see all zero counts made explicit.

In response, a -tabcount- hack was posted.

Kit Baum reminded me privately that one way of seeing this problem
was as a special case of a problem we call the -tabcond- problem,
namely

	You want a table showing how many
	observations satisfy one or more conditions.
	That is, you have a series of true or false
	conditions which, in general, need not be
	mutually exclusive and need not refer
	to any single variable.

A couple of programs we produced a while back, although producing
more or less the tables desired, did not seem to be worth
publicising for various reasons, including an ugly syntax.
But Kazi's question was the trigger to have another go, and as
the headline indicates, a -tabcond- based on a quite different
approach is now available from SSC, thanks to Kit.

What's much more noteworthy than the program itself is
the approach it employs: user-programmers may write a
wrapper for -tabdisp-, generating the contents of a table
on the fly.

Examples will indicate what -tabcond- does.

-tabcond groups- gives, for the distinct groups
of a variable, the numbers of observations
satisfying specified conditions. I sometimes
like to see how tail frequencies go down
as you go outwards:

. tabcond g rep78 , cond(mpg>15 mpg>20 mpg>25 mpg>30 mpg>35)

----------------------------------------------------------------------
Repair    |
Record    |
1978      |     mpg>15      mpg>20      mpg>25      mpg>30      mpg>35
----------+-----------------------------------------------------------
        1 |          2           1           0           0           0
        2 |          7           3           0           0           0
        3 |         25          10           3           0           0
        4 |         15          12           3           0           0
        5 |         11           7           6           5           1
----------------------------------------------------------------------

Here the conditions can all be written tightly without spaces.
If you want spaces to be echoed to column headings, or
the condition cannot be written without embedded spaces,
you must bind a condition in " ":

. tabcond g rep78 , cond(mpg>25 price<5000 "mpg>25 & price<5000")

----------------------------------------------------------------------
---
Repair    |
Record    |
1978      |              mpg>25           price<5000  mpg>25 &
price<5000
----------+-----------------------------------------------------------
---
        1 |                   0                    2
0
        2 |                   0                    4
0
        3 |                   3                   17
3
        4 |                   3                    6
3
        5 |                   6                    5
5
----------------------------------------------------------------------
---

-tabcond variables- gives for each of a specified varlist how many
observations satisfy a generic condition, expressed using a wildcard
@:

. tabcond var price-foreign, cond("@ < 0" "@ == 0" "@ > 0 & @ < ." "@
== .") listwise

----------------------------------------------------------------------
-------------
              Variable |         @ < 0         @ == 0  @ > 0 & @ < .
@ == .
-----------------------+----------------------------------------------
-------------
                 Price |             0              0             74
0
         Mileage (mpg) |             0              0             74
0
    Repair Record 1978 |             0              0             69
5
        Headroom (in.) |             0              0             74
0
 Trunk space (cu. ft.) |             0              0             74
0
         Weight (lbs.) |             0              0             74
0
          Length (in.) |             0              0             74
0
    Turn Circle (ft.)  |             0              0             74
0
Displacement (cu. in.) |             0              0             74
0
            Gear Ratio |             0              0             74
0
              Car type |             0             52             22
0
----------------------------------------------------------------------
-------------

That may suggest an application to multiple response data. Given
say five variables which are the first, second, ...
choices from a list of five, then the cross-tabulation
can be done by something resembling

. tabcond var r1-r5, cond(@==1 @==2 @==3 @==4 @==5)

However, -tabm- from the -tab_chi- package on SSC
is better for that purpose.

A limitation on -tabcond- is that no more than 5 conditions
can be specified. This is inherited from a documented limit
in -tabdisp-.

To download -tabcond- in an up-to-date Stata, type

. ssc inst tabcond

Nick
n.j.cox@durham.ac.uk

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