Announcement

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

  • Using asdoc & tabstat to compute means by multiple groupvars

    Dear all,

    I would like to create nice descriptive statistics with asdoc and tabstat commands. I would like to compute the means for different variables and groups in my dataset. Here is my dataset

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long country float idno str15 party float(vote dist_lr dist_red dist_immpoor) byte gender float age
     3  3 "AfD"    0 4.2380953         6       9.9 0 2
     3  3 "SPD"    0 1.3809524 3.0526316      4.15 0 2
     3  3 "CDU"    1  .8571429  5.947369         6 0 2
     3  3 "LINKE"  0 3.5714285  .8947368       2.7 0 2
     3  3 "FDP"    0 1.4285712  8.578947      5.95 0 2
     3  3 "GRUNEN" 0 1.7619047  3.421053       1.7 0 2
    10  6 "GP"     0  1.857143        .5  3.333333 1 1
    10  6 "FF"     0  .4285712  2.357143  1.833333 1 1
    10  6 "Lab"    0 1.4285715  .7857144  2.833333 1 1
    10  6 "SF"     0 2.4285715  .2142856  2.333333 1 1
    10  6 "FG"     1  .8571429  3.357143         2 1 1
     6  7 "SFP"    0         0 2.2142859 1.0476189 1 3
     6  7 "SDP"    0  2.857143  .7857143  .3809524 1 3
     6  7 "VIHR"   0 2.2857144  .3571429 2.0476189 1 3
     6  7 "KESK"   0 .14285707  2.428571 2.2380955 1 3
     6  7 "KD"     0  .8571429 2.2857141 2.4358976 1 3
     6  7 "PS"     0 1.5714288  2.785714  6.452381 1 3
     6  7 "KOK"    1  1.714286  4.357143  1.095238 1 3
     6  7 "VAS"    0       4.5 1.4285715 1.1190476 1 3
     7  9 "PS"     1         1      2.25 .58333325 1 3
     7  9 "LR"     0     5.875     7.375  4.541667 1 3
     7  9 "LREM"   0 4.3333335  5.833333 2.3333333 1 3
     7  9 "MoDem"  0     4.125  5.857143 2.0416667 1 3
     7  9 "FI"     0       .75      .625  .6666667 1 3
     7  9 "EELV"   0        .5     2.375 1.7083333 1 3
     7  9 "RN"     0      7.75         4  6.541667 1 3
     8 10 "LIBDEM" 0 .23529434  .9705882  .7450979 1 1
     8 10 "UKIP"   0    4.6875 4.5666666  6.137256 1 1
     8 10 "LAB"    0 2.0588236  .9117647 .19607854 1 1
     8 10 "SNP"    0        .5      .625         1 1 1
     8 10 "GREEN"  0         2      .625  1.627451 1 1
     8 10 "CONS"   1  3.117647  4.147059  4.254902 1 1
    12 11 "VVD"    0 2.6153846       .75 4.5897436 0 1
    12 11 "D66"    1 .15384626      1.75 .33333325 0 1
    12 11 "SP"     0 3.6153846      6.75 1.9166667 0 1
    12 11 "PVV"    0 3.6923075 1.6666665  6.589744 0 1
    12 11 "PvdA"   0 1.3846154  4.666667  .8333333 0 1
    12 11 "CDA"    0 1.8461537       1.5  3.666667 0 1
    12 11 "CU"     0  .0769229  3.318182 1.1282051 0 1
    12 11 "GL"     0  2.692308  5.318182 1.7179487 0 1
     1 13 "SPO"    0         5       2.4 1.3666666 1 1
     1 13 "OVP"    0       2.1       6.2  5.266667 1 1
     1 13 "Grune"  0       6.5       2.5  .6333332 1 1
     1 13 "FPO"    1 .10000038       5.6  6.466667 1 1
     1 13 "NEOS"   0       3.1       6.9  .9666669 1 1
    11 14 "FdI"    0 2.0526314  3.029412 3.1754384 1 0
    11 14 "LN"     0 1.7894735   4.81579  3.280702 1 0
    11 14 "FI"     1 .05263138  4.388889  .3333335 1 0
    11 14 "PD"     0  3.789474 .22222233  3.614035 1 0
    11 14 "M5S"    0 2.2222223  .6052631 .11111116 1 0
    11 14 "SI"     0  5.555555       1.5  5.979167 1 0
     7 17 "RN"     0      2.75         1 3.2083335 0 0
     7 17 "PS"     0         4      2.75 3.9166665 0 0
     7 17 "EELV"   0       4.5     2.625  5.041667 0 0
     7 17 "FI"     0      5.75     4.375 2.6666665 0 0
     7 17 "LREM"   0  .6666665  .8333335         1 0 0
     7 17 "MoDem"  0      .875  .8571429 1.2916665 0 0
     7 17 "LR"     1      .875     2.375 1.2083335 0 0
     6 21 "VIHR"   0 1.2857144  .3571429  5.380952 1 0
     6 21 "KOK"    1  2.714286  4.357143 2.2380953 1 0
     6 21 "PS"     0  2.571429  2.785714 3.1190476 1 0
     6 21 "SFP"    0         1 2.2142859  4.380952 1 0
     6 21 "VAS"    0       3.5 1.4285715  4.452381 1 0
     6 21 "SDP"    0  1.857143  .7857143  2.952381 1 0
     6 21 "KESK"   0  .8571429  2.428571 1.0952377 1 0
     6 21 "KD"     0  1.857143 2.2857141  .8974357 1 0
    11 22 "FdI"    0  7.052631  3.029412  9.842105 0 0
    11 22 "LN"     0  6.789474   4.81579  9.947369 0 0
    11 22 "SI"     0  .5555556       1.5     .6875 0 0
    11 22 "M5S"    0  2.777778  .6052631  6.555555 0 0
    11 22 "PD"     1 1.2105262 .22222233 3.0526316 0 0
    11 22 "FI"     0  4.947369  4.388889         7 0 0
     3 23 "CDU"    0  2.857143  5.947369         6 0 3
     3 23 "LINKE"  0 1.5714285  .8947368       2.7 0 3
     3 23 "AfD"    0  6.238095         6       9.9 0 3
     3 23 "GRUNEN" 0  .2380953  3.421053       1.7 0 3
     3 23 "SPD"    1  .6190476 3.0526316      4.15 0 3
     3 23 "FDP"    0  3.428571  8.578947      5.95 0 3
     3 24 "SPD"    1 1.6190476  .5526316  .8166668 1 2
     3 24 "GRUNEN" 0 1.2380953  .9210527 1.6333332 1 2
     3 24 "AfD"    0  7.238095       3.5  6.566667 1 2
     3 24 "FDP"    0  4.428571  6.078947 2.6166666 1 2
     3 24 "LINKE"  0 .57142854 1.6052632  .6333332 1 2
     3 24 "CDU"    0  3.857143 3.4473686  2.666667 1 2
    10 25 "GP"     0  3.142857         3  3.333333 0 1
    10 25 "SF"     0 2.5714285 2.2857144  2.333333 0 1
    10 25 "FG"     0  5.857143  5.857143         2 0 1
    10 25 "FF"     1  5.428571  4.857143  1.833333 0 1
    10 25 "Lab"    0 3.5714285 3.2857144  2.833333 0 1
    10 26 "FG"     0  .8571429  1.642857  5.333333 1 2
    10 26 "Lab"    0 1.4285715  4.214286  6.166667 1 2
    10 26 "FF"     1  .4285712  2.642857  5.166667 1 2
    10 26 "GP"     0  1.857143       4.5  6.666667 1 2
    10 26 "SF"     0 2.4285715  5.214286  5.666667 1 2
     5 27 "Cs"     0       7.2  6.533333  6.428571 1 2
     5 27 "Vox"    0  9.714286  8.333333       9.8 1 2
     5 27 "Pais"   0  2.733333 2.3333333 2.2857144 1 2
     5 27 "IU"     0 1.8666667       1.4  2.533333 1 2
     5 27 "PSOE"   1       3.6       2.8 3.9333334 1 2
     5 27 "PPP"    0  8.066667  7.266667       7.6 1 2
    end
    label values country country_label
    label def country_label 1 "1. Austria", modify
    label def country_label 3 "3. Germany", modify
    label def country_label 5 "5. Spain", modify
    label def country_label 6 "6. Finland", modify
    label def country_label 7 "7. France", modify
    label def country_label 8 "8. Great Britain", modify
    label def country_label 10 "10. Ireland", modify
    label def country_label 11 "11. Italy", modify
    label def country_label 12 "12. Netherlands", modify
    label values gender gender
    label def gender 0 "0. Female", modify
    label def gender 1 "1. Male", modify
    label values age age_groups
    label def age_groups 0 "0. 30 and Under", modify
    label def age_groups 1 "1. 31-50", modify
    label def age_groups 2 "2. 51-69", modify
    label def age_groups 3 "3. 70 and over", modify

    I am not finding a way (or an alternative) to compute means for different groups of socio-demographics. Indeed, I am forced to create multiple tables for each group, making then very sloppy putting all tables together in a nice format.

    Here is the code I am using now (example for one groupvar):

    Code:
    asdoc tabstat dist_lr dist_red dist_immpoor dist_immdfrace dist_eu dist_gendiss if vote==1, by(gender) save(Gender_descri.doc) format(%9.2f)
    Hopefully, I would like something like this:

    Code:
    asdoc tabstat dist_lr dist_red dist_immpoor dist_immdfrace dist_eu dist_gendiss if vote==1, by(gender age education) save(Gender_descri.doc) format(%9.2f)
    Is there a way to include multiple group variables?

    Sincerely
    Mattia

  • #2
    If you have Stata 17 or newer, you can use the new collect
    features with the updated table command to build your table and
    export it to Word (docx).

    Using the dataset your provided, here is how I accomplished this.
    Code:
    * variables to compute means from
    local vlist dist_lr dist_red dist_immpoor
    * group variables
    local gvars gender age
    
    collect clear
    
    * compute means from the total sample
    table if vote==1,        ///
        statistic(mean `vlist')    ///
        nformat(%9.2f mean)    ///
        name(Total)
    collect addtags Total[_hide], fortags(result[mean])
    
    foreach gvar of local gvars {
        * compute group-level means
        table (`gvar') (var result)    ///
            if vote==1        ///
            ,            ///
            statistic(mean `vlist')    ///
            nformat(%9.2f mean)    ///
            nototals        ///
            name(`gvar')
    }
    
    * combine the collections into a new collection named 'all'
    collect combine all = `gvars' Total
    
    * arrange the items into a table
    collect layout (`gvars' Total) (var#result)
    Here is the resulting table.
    Code:
    ------------------------------------------------------
                      |  dist_lr   dist_red   dist_immpoor
    ------------------+-----------------------------------
    gender            |
      0. Female       |     1.52       3.03           2.76
      1. Male         |     1.52       3.45           2.69
    age               |
      0. 30 and Under |     1.21       2.84           1.71
      1. 31-50        |     1.93       3.94           2.98
      2. 51-69        |     1.63       2.99           3.98
      3. 70 and over  |     1.11       3.22           1.94
    Total             |     1.52       3.29           2.72
    ------------------------------------------------------
    See the documentation for collect export for details on how to
    export this table to Word.

    Comment


    • #3
      Sorry Jeff, I just saw the reply now. Thanks a lot, I tried it and it works perfectly!

      Comment

      Working...
      X