Announcement

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

  • Cross table with three variables plus mean AND respective number of observations/cases

    Dear Statalist,

    I want to do a cross table, which displays the means of the variable wage. The two other variables are sex and nationality. It works with:

    table sex nationality, c(mean wage)
    But I also want to display the respective obervations/cases for every combination of sex and nationality - accordingly per mean one number of observations/cases. How can I do this?

    Up to now I display the respective number of cases with:
    sum wage if nationality==1 & sex==0|sex==1
    sum wage if nationality==2 & sex==0|sex==1
    sum wage if nationality==3 & sex==0|sex==1
    sum wage if nationality==1|nationality==2|nationality==3 & sex==0
    sum wage if nationality==1|nationality==2|nationality==3 & sex==1
    But I hope there's a more simple solution.

    best regards,
    Sam Peters


  • #2
    if I understand you correctly you already have the basic idea - just add "freq wage" inside your "c()" - this will give you the number of people who have no missing values which, I assume, is what you want

    Comment


    • #3
      If I understood your query right, you may try this:

      Code:
      . sysuse auto
      (1978 Automobile Data)
      
      . tabulate rep78 foreign
      
          Repair |
          Record |       Car type
            1978 |  Domestic    Foreign |     Total
      -----------+----------------------+----------
               1 |         2          0 |         2
               2 |         8          0 |         8
               3 |        27          3 |        30
               4 |         9          9 |        18
               5 |         2          9 |        11
      -----------+----------------------+----------
           Total |        48         21 |        69
      
      
      . tabulate rep78 foreign, summarize(mpg) mean
      
                                Means of Mileage (mpg)
      
          Repair |
          Record |      Car type
            1978 |  Domestic    Foreign |     Total
      -----------+----------------------+----------
               1 |        21          . |        21
               2 |    19.125          . |    19.125
               3 |        19  23.333333 | 19.433333
               4 | 18.444444  24.888889 | 21.666667
               5 |        32  26.333333 | 27.363636
      -----------+----------------------+----------
           Total | 19.541667  25.285714 | 21.289855
      
      . tabulate rep78 foreign, summarize(mpg) freq mean
      
                        Means and Frequencies of Mileage (mpg)
      
          Repair |
          Record |      Car type
            1978 |  Domestic    Foreign |     Total
      -----------+----------------------+----------
               1 |        21          . |        21
                 |         2          0 |         2
      -----------+----------------------+----------
               2 |    19.125          . |    19.125
                 |         8          0 |         8
      -----------+----------------------+----------
               3 |        19  23.333333 | 19.433333
                 |        27          3 |        30
      -----------+----------------------+----------
               4 | 18.444444  24.888889 | 21.666667
                 |         9          9 |        18
      -----------+----------------------+----------
               5 |        32  26.333333 | 27.363636
                 |         2          9 |        11
      -----------+----------------------+----------
           Total | 19.541667  25.285714 | 21.289855
                 |        48         21 |        69
      Last edited by Marcos Almeida; 06 Apr 2018, 06:34. Reason: Crossed with Rich's reply.
      Best regards,

      Marcos

      Comment


      • #4
        Yes, exactly - it works! Thank you very much

        Best regards,

        Sam Peters

        Comment


        • #5
          Another way to approach this:


          Code:
          . sysuse auto, clear
          (1978 Automobile Data)
          
          . egen mean = mean(mpg), by(rep78 foreign) 
          
          . format mean %2.1f 
          
          . bysort rep78 foreign: gen count = _N 
          
          . tabdisp rep78 foreign, c(count mean)
          
          ------------------------------
          Repair    |
          Record    |      Car type     
          1978      | Domestic   Foreign
          ----------+-------------------
                  1 |        2          
                    |     21.0          
                    | 
                  2 |        8          
                    |     19.1          
                    | 
                  3 |       27         3
                    |     19.0      23.3
                    | 
                  4 |        9         9
                    |     18.4      24.9
                    | 
                  5 |        2         9
                    |     32.0      26.3
                    | 
                  . |        4         1
                    |     23.3      14.0
          ------------------------------

          Comment

          Working...
          X