Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Table & Relative Frequency within column / row

    I was looking into this and couldn't find an answer: I'm using table to generate several descriptive stats of my data (the mean income by year of males vs female for example). I would like table to also report the relative frequency of males and females in each year.
    This is easily done with tab:
    tab year female, row

    Yet I couldn't find a way to achieve this with table. any help would be greatly appreciated.

  • #2
    The proportion female is just the mean of an indicator variable for which female is 1. So, this seems within easy reach.

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . rename (rep78 mpg foreign) (year income female)
    
    . label var year year
    
    . table year , c(mean income mean female)
    
    --------------------------------------
         year | mean(income)  mean(female)
    ----------+---------------------------
            1 |           21             0
            2 |       19.125             0
            3 |      19.4333            .1
            4 |      21.6667            .5
            5 |      27.3636       .818182
    --------------------------------------
    To see percents, multiply by 100 first.

    To see males as well as females, subtract from 1 or 100 as desired in new variables.

    Further tips.

    1. To grab more control, use tabdisp. That's sometimes billed as a programmer's command, but it is easy to use.

    2. Generate your own summary variables beforehand for flexibility.

    3. A simple tip is that tabdisp lets string variables through unchanged. So, to mix different amounts of rounding, just push your numeric summaries into strings using desired display formats in the conversion.

    See also http://www.stata-journal.com/sjpdf.h...iclenum=pr0010 which although written in 2003 seems to remain pretty much up-to-date.

    Code:
    .  egen mean_income = mean(income), by(year)
    
    . gen show1 = string(mean_income, "%4.3f")
    
    . label var show1 "mean income"
    
    
    . egen pc_female = mean(100 * female), by(year)
    
    . gen show2 = string(pc_female, "%2.1f")
    
    . label var show2 "% female"
    
    
    . tabdisp year, c(show1 show2)
    
    ------------------------------------
         year | mean income     % female
    ----------+-------------------------
            1 |      21.000          0.0
            2 |      19.125          0.0
            3 |      19.433         10.0
            4 |      21.667         50.0
            5 |      27.364         81.8
            . |      21.400         20.0
    ------------------------------------
    The mean income for males and females separately? Just other variables for which egen will help.
    Last edited by Nick Cox; 17 Apr 2016, 04:06.

    Comment

    Working...
    X