Announcement

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

  • Output descriptive table in a custom format

    Hi, Is there a way to output a custom table that has specific fields after running a chi squared test. I have several variables labelled Yes/No or with specific categories such as gender (Male/Female). I would like to output a table similar to the one below.

    ----------------------- copy starting from the next line -----------------------
    [CODE]
    tab2 married wagecat, chi2 row
    tab2 married union, chi2 row

    Click image for larger version

Name:	Screenshot 2023-06-30 at 3.59.42 PM.png
Views:	1
Size:	53.1 KB
ID:	1718986


    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int idcode byte(age race married never_married grade collgrad south smsa c_city industry occupation union) float wage byte hours float(ttl_exp tenure wagecat)
      1 37 2 0 0 12 0 0 1 0  5  6 1 11.739125 48 10.333334  5.333333 1
      2 37 2 0 0 12 0 0 1 1  4  5 1  6.400963 40 13.621795      5.25 0
      3 42 2 0 1 12 0 0 1 1  4  3 .  5.016723 40  17.73077      1.25 0
      4 43 1 1 0 17 1 0 1 0 11 13 1 9.0338125 42 13.211537      1.75 1
      6 42 1 1 0 12 0 0 1 0  4  6 0  8.083731 48 17.820513     17.75 1
      7 39 1 1 0 12 0 0 1 0 11  3 0 4.6296296 30  7.326923      2.25 0
      9 37 1 0 0 12 0 0 1 1  5  2 1 10.491142 40  19.04487        19 1
     12 40 1 1 0 18 1 0 1 0 11  2 0 17.206116 45 15.557693 14.166667 1
     13 40 1 1 0 14 0 0 1 0 11  3 0 13.083735  8     14.25       5.5 1
     14 40 1 1 0 15 0 0 1 0 11  1 0  7.745568 50  7.384615      2.25 1
     15 39 1 1 0 16 1 0 1 0 11  1 1 16.795479 16  15.76923  8.416667 1
     16 40 1 1 0 15 0 0 1 0 11  1 0  15.48309 40 16.384615 13.833333 1
     18 40 1 1 0 15 0 0 1 0  6  5 0  5.233495 40      7.75      7.75 0
     19 40 1 0 0 15 0 0 1 0 11  1 1 10.161026 40 14.211538  5.583333 1
     20 39 1 1 0 15 0 0 1 0 11  1 1  9.661837  4 15.858974 4.6666665 1
     22 41 1 1 0 15 0 0 1 0 11  1 0  9.057972 32  15.01282         3 1
     23 42 1 1 0 15 1 0 0 0 11  1 0   8.05153 45 15.346155  8.083333 1
     24 41 1 1 0 14 1 0 1 0 11  1 0 11.095006 24 16.467947  3.416667 1
     25 42 1 1 0 14 1 0 1 1 11  1 0  9.581316 40     17.75  5.416667 1
     36 37 1 0 1 12 0 0 1 0  8  4 .  4.180602 40 15.935898  .9166667 0
     39 44 1 0 0 16 1 0 1 0 11  1 0  9.790657 40  8.839744  7.916667 1
     44 41 1 1 0 18 1 0 1 0 12  1 1  28.45666 35 16.205128       3.5 1
     45 35 1 1 0 12 0 0 1 0  5  3 1  10.18518 38     17.25     17.25 1
     46 44 1 1 0 18 1 0 1 0 11  3 0  3.051529 25  5.544872  2.916667 0
     47 35 1 0 0 12 0 0 1 0  5  5 0  3.526568 40  8.737179       .75 0
     48 35 1 0 0 15 0 0 1 1  7  3 .  5.852843 40  5.557693  .3333333 0
     50 36 1 0 0 16 1 0 1 1 11  1 . 35.731617 45  9.423077  6.416667 1
     51 38 1 1 0 12 0 0 1 0 11  1 0 4.4283414 18  10.26923      3.25 0
     54 40 1 0 1 12 0 0 1 1 11  8 0  5.032206 55 10.397436       .75 0
     57 42 1 1 0 12 0 0 1 1  6  3 0  5.780996 40 12.846154      7.25 0
     62 38 1 1 0 10 0 0 0 0  6  3 0   5.11272 40  7.365385  5.916667 0
     63 44 1 0 0 15 1 0 1 1 11  1 1   25.1606 18 10.384615 2.0833333 1
     64 38 1 1 0 12 0 0 1 0 11  8 1  4.404185 30  7.384615  3.333333 0
     66 39 1 1 0 13 0 0 1 1  6  4 0 4.7101436  9  11.48077      9.75 0
     67 40 1 1 0 15 0 0 0 0 11  1 0   8.05153 24 11.038462  3.166667 1
     70 36 1 1 0 13 0 0 0 0  5  3 0 16.739124 38 16.083334 16.083334 1
     71 34 1 1 0 12 0 0 0 0  4  3 0  5.724636 38      10.5       2.5 0
     72 36 1 1 0 12 0 0 1 0 11  3 1  7.458193 40  9.442307 1.0833334 1
     73 36 1 0 1 14 0 0 0 0  4  3 .  6.793478 40 14.442307        .5 0
     75 39 2 0 0 11 0 0 0 0  4  6 1      12.5 40      14.5  9.333333 1
     78 40 1 1 0 12 0 0 1 1  7  3 0 17.697258 35 16.326923  9.083333 1
     80 45 1 1 0 14 0 0 1 0 11  3 0 12.077295  8  5.717949         0 1
     85 38 1 0 0 11 0 0 1 1  4  3 .  9.406354 40  6.192307       1.5 1
     86 44 1 0 1 17 1 0 1 0 10  8 1  13.17229 44  16.26923      1.25 1
    103 36 1 1 0 18 1 0 1 1 11 13 1   11.0628 35  9.833333         0 1
    104 41 1 1 0 17 1 0 1 0 11 13 0 3.7439594 18  8.948718      2.75 0
    105 41 1 1 0 16 1 0 1 0  6  3 0  4.025765  8  8.788462  5.583333 0
    106 38 2 0 0 12 0 1 1 1 12  3 0 10.837358 40 17.416666 17.416666 1
    107 43 2 0 0  8 0 0 1 1  4  6 1  4.347824 40  7.544871 1.0833334 0
    110 45 1 1 0 12 0 0 1 0 11  3 1 4.7101436 20  6.660256  2.583333 0
    121 39 2 1 0 12 0 0 1 1  5  3 1  9.726243 38  20.69872 20.666666 1
    123 41 3 0 0  6 0 0 1 1  8  6 1  2.898549 37  7.320513       .75 0
    126 39 2 0 0 12 0 0 1 1 11  1 0 10.064413 40     19.75     19.75 1
    128 36 1 1 0 12 0 0 1 0  7  1 0  8.518515 40 19.032053        19 1
    129 43 1 0 0 12 0 0 1 0 11  3 0   8.49436 37  14.23077 10.166667 1
    130 43 1 1 0 12 0 0 1 0  4  3 0  5.600358 40  6.153846  .6666667 0
    131 41 1 0 0 12 0 0 1 1 11  3 1  4.227053 38  12.26282       .25 0
    132 36 1 0 0 10 0 0 1 1  4  6 0  5.619967 43  10.24359  2.666667 0
    134 42 1 1 0 12 0 0 1 0 12  2 0   4.64573 30  6.128205       1.5 0
    137 36 2 1 0 17 1 0 1 0 11 13 1  15.48309 30  9.833333  9.833333 1
    139 44 2 0 0  9 0 0 1 1 11  3 1  4.830918 35       8.5       8.5 0
    141 43 1 0 0 14 0 0 1 0 11  3 .  40.19808 40 12.910256 1.1666666 1
    142 43 1 1 0 18 1 0 1 0 11  1 1  15.48309 40 15.115384      5.75 1
    143 37 1 1 0 15 0 0 1 0 11  1 0 10.033444 40  7.076923  .5833333 1
    144 43 1 1 0 10 0 1 1 0  1  3 0 3.6231885 40 14.326923  .3333333 0
    147 40 1 1 0 12 0 0 1 0  7  4 0  9.299514 20  7.666667  3.083333 1
    152 37 1 1 0 18 1 0 1 0 11 13 1 10.217388 50 13.673077  4.833333 1
    159 36 1 1 0 11 0 0 1 0  7  4 0 10.008045 37 17.397436 17.333334 1
    166 38 1 1 0 14 0 0 1 0 11 13 0  4.227053  4 10.602565 1.0833334 0
    167 37 1 1 0 13 0 1 0 0  3  3 0  4.830918 25 16.980768       7.5 0
    168 41 1 1 0 17 1 1 1 0 11 13 0  7.745568 40 11.301282  3.833333 1
    169 36 1 0 0 16 1 0 1 0 11 13 1  9.847019 37 14.833333 14.833333 1
    172 44 1 1 0 15 1 0 1 1 11  1 0 10.869566 30 17.833334  3.416667 1
    173 43 2 0 0 12 0 0 1 1  9  7 .  3.220612 35  11.50641  2.916667 0
    176 38 2 0 1 12 0 0 1 1 12  1 1  8.164251 40 12.846153       3.5 1
    183 43 1 0 0 12 0 0 1 0  4  2 1  8.454107 40 17.121796     13.25 1
    184 41 1 1 0 12 0 0 1 0 11  3 0  8.293073 40 13.788462 12.083333 1
    188 41 1 1 0 12 0 0 1 0  4  3 1  9.677936 40  13.26923  8.416667 1
    195 36 1 1 0 12 0 0 1 0  1  3 0 4.5330095 40 12.801282 2.1666667 0
    202 36 1 0 0 14 0 1 0 0  6  8 .  3.344482 30 11.551282       .25 0
    203 41 1 1 0 11 0 1 1 1  7  4 0  8.454107 24  19.00641  3.166667 1
    204 38 2 1 0 10 0 0 1 1  6  2 0  6.038648 40  5.596154         4 0
    206 37 1 1 0 14 0 0 1 1 11  1 0  10.32206 18 10.333333 1.3333334 1
    207 38 2 0 1 18 1 0 1 1 11  1 1 11.610305 50  13.26923 2.0833333 1
    210 37 2 0 0 11 0 0 1 0  4  6 .  5.016723 40 15.641026 10.916667 0
    213 43 1 0 0 10 0 0 1 1  4  6 0  6.441224 40 16.903847 14.416667 0
    215 36 1 0 1 16 1 0 1 0  6  3 0  5.032206 40 15.852564      3.75 0
    218 35 1 1 0 12 0 0 0 0 11  2 0  4.830918 38 10.115384 2.0833333 0
    219 35 1 0 1 13 0 0 1 1 11  8 0 11.070854 40 16.858974  8.916667 1
    221 40 1 1 0 16 1 0 1 0  4  1 0  8.317226 35  9.256411 1.8333334 1
    223 37 1 0 0 16 1 0 1 0 10  1 0 13.083735 40 15.916667      8.75 1
    224 38 1 1 0 13 0 0 1 0 11  3 1  5.628018 40 10.788462 4.6666665 0
    225 35 1 0 0 16 1 0 1 0  4  2 0 12.560386 45 16.692308     15.75 1
    226 34 1 1 0 16 1 1 1 0 12  1 0  6.682766 40  7.692307  6.666667 0
    227 42 1 0 0 15 0 1 1 1  4  3 0  5.636071 40 18.788462 1.3333334 0
    228 42 1 1 0 16 1 1 1 0  7  3 0  4.025765 15  4.980769  .3333333 0
    231 42 1 1 0 16 1 0 1 0  8  3 0 18.067625 30 10.589743  9.833333 1
    233 36 1 1 0 16 1 0 0 0 11  1 0  5.805152 40 13.923077 13.916667 0
    236 36 1 1 0 18 1 0 1 1 11  1 0  24.66183 20  9.480769      3.75 1
    237 36 1 0 1 17 1 0 1 1 11  3 1   7.21417 40 12.096154      1.75 1
    end
    label values race racelbl
    label def racelbl 1 "White", modify
    label def racelbl 2 "Black", modify
    label def racelbl 3 "Other", modify
    label values married marlbl
    label def marlbl 0 "Single", modify
    label def marlbl 1 "Married", modify
    label values never_married nev_mar
    label def nev_mar 0 "Has been married", modify
    label def nev_mar 1 "Never married", modify
    label values collgrad gradlbl
    label def gradlbl 0 "Not college grad", modify
    label def gradlbl 1 "College grad", modify
    label values south southlbl
    label def southlbl 0 "Not south", modify
    label def southlbl 1 "South", modify
    label values smsa smsalbl
    label def smsalbl 0 "Not SMSA", modify
    label def smsalbl 1 "SMSA", modify
    label values c_city ccitylbl
    label def ccitylbl 0 "Not central city", modify
    label def ccitylbl 1 "Central city", modify
    label values industry indlbl
    label def indlbl 1 "Ag/Forestry/Fisheries", modify
    label def indlbl 3 "Construction", modify
    label def indlbl 4 "Manufacturing", modify
    label def indlbl 5 "Transport/Comm/Utility", modify
    label def indlbl 6 "Wholesale/Retail trade", modify
    label def indlbl 7 "Finance/Ins/Real estate", modify
    label def indlbl 8 "Business/Repair svc", modify
    label def indlbl 9 "Personal services", modify
    label def indlbl 10 "Entertainment/Rec svc", modify
    label def indlbl 11 "Professional services", modify
    label def indlbl 12 "Public administration", modify
    label values occupation occlbl
    label def occlbl 1 "Professional/Technical", modify
    label def occlbl 2 "Managers/Admin", modify
    label def occlbl 3 "Sales", modify
    label def occlbl 4 "Clerical/Unskilled", modify
    label def occlbl 5 "Craftsmen", modify
    label def occlbl 6 "Operatives", modify
    label def occlbl 7 "Transport", modify
    label def occlbl 8 "Laborers", modify
    label def occlbl 13 "Other", modify
    label values union unionlbl
    label def unionlbl 0 "Nonunion", modify
    label def unionlbl 1 "Union", modify
    ------------------ copy up to and including the previous line ------------------

  • #2

    If you have Stata 18, the new dtable command does this kind of table.
    In the following I use dtable to build the table, and use the
    collect suite to make some style changes to get a table that looks
    closer to the May Blake's example.
    Code:
    * add missing value label
    label define highwage 0 "No" 1 "Yes"
    label values wagecat highwage
    
    * -dtable- is new in Stata 18
    dtable i.wagecat i.union, by(married, tests) nosample
    
    * add variable labels that include statistic hints
    collect label levels var ///
        wagecat "High wage, n (%)" ///
        union "Union worker, n (%)"
    
    * hide by variable title
    collect style header married, title(hide)
    
    * make Total the first column
    collect levelsof married
    collect style autolevels married .m `s(levels)', clear
    
    * remove % symbol from factor percents, but keep the parens
    collect style cell result[fvpercent], sformat("(%s)")
    
    * change the label for the p-values
    collect levelsof married
    collect label levels married _dtable_test "P", modify
    
    collect preview
    Here is the resulting table.
    Code:
    . collect preview
    
    -------------------------------------------------------
                          Total     Single   Married    P  
    -------------------------------------------------------
    High wage, n (%)                                       
      No                42 (42.0) 18 (46.2) 24 (39.3) 0.501
      Yes               58 (58.0) 21 (53.8) 37 (60.7)      
    Union worker, n (%)                                    
      Nonunion          59 (65.6) 13 (44.8) 46 (75.4) 0.004
      Union             31 (34.4) 16 (55.2) 15 (24.6)      
    -------------------------------------------------------

    Comment


    • #3

      If you have Stata 17, you can still build this table, it just takes a few more steps. In the following I use table and the collect suite to build the table and style it to look like the original example.
      Code:
      * add variable labels that include statistic hints
      collect label levels var ///
          wagecat "High wage, n (%)" ///
          union "Union worker, n (%)"
      
      * hide by variable title
      collect style header married, title(hide)
      
      * make Total the first column
      collect levelsof married
      collect style autolevels married .m `s(levels)', clear
      
      * define composite result and hide it from the header
      collect composite define stats = fvfrequency fvpercent, trim
      collect style header result, title(hide) level(hide)
      
      * stack the row headers
      collect style row stack, nobinder
      collect layout (var) (married#result[stats])
      
      * add p-values from chi2 tests
      tabulate married wagecat, chi2
      collect get p=(r(p)), tag(var[0.wagecat])
      tabulate married union, chi2
      collect get p=(r(p)), tag(var[0.union])
      
      * add styles for the p-values
      collect label levels result p "P"
      collect style header result[p], level(label)
      collect style cell result[p], nformat("%5.4f")
      
      collect layout (var) (married#result[stats] result[p])
      Here is the resulting table.
      Code:
      . collect layout (var) (married#result[stats] result[p])
      
      Collection: Table
            Rows: var
         Columns: married#result[stats] result[p]
         Table 1: 6 x 4
      
      -----------------------------------------------------------------
                          |      Total      Single     Married        P
      --------------------+--------------------------------------------
      High wage, n (%)    |                                            
        No                |  42 (42.0)   18 (46.2)   24 (39.3)   0.5010
        Yes               |  58 (58.0)   21 (53.8)   37 (60.7)         
      Union worker, n (%) |                                            
        Nonunion          |  59 (65.6)   13 (44.8)   46 (75.4)   0.0043
        Union             |  31 (34.4)   16 (55.2)   15 (24.6)         
      -----------------------------------------------------------------

      Comment


      • #4
        In either case you can use collect export to publish the table to MS Word, MS Excel, PDF, HTML, LaTeX, Markdown, or plain text.

        Comment


        • #5
          Thank you so much Jeff, this worked amazingly!

          Comment

          Working...
          X