Announcement

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

  • dtable multiple statistics layout

    Dear all,

    I am trying to figure out dtable layout outputs so I do not have to fidget as much in word anymore.
    Right now, I want to get the mean and median of some variables by a dummy variable. E.g.:
    Code:
    sysuse auto.dta
    dtable, by(foreign, nototals) column( by(label, fvlabel) ) nosample continuous(mpg headroom weight length, statistics(mean median))
    However, while this code gives the exact output I want, the layout is a bit weird. That is, the mean and median values are put in the same cell of the table. I would like to have a subheader for the mean and the median underneath the domestic/foreign labels, a bit like this example:
    Click image for larger version

Name:	example.png
Views:	1
Size:	56.7 KB
ID:	1779568



    I tried something like:
    Code:
     dtable, by(foreign, nototals) column( by(label, fvlabel) ) nosample continuous(mpg headroom weight length, statistics(mean)) continuous(mpg headroom weight length, statistics(median))
    But this gives just the median values.

    Is there some way to achieve what I want within the dtable options or should I use some other table/summary command?

    Kind regards,
    Johannes de Ruig

  • #2
    Yes, when you ask dtable to show multiple statistics, it uses a composite result called _dtable_stats to put them into a single cell.

    So if you start with your dtable:
    Code:
    sysuse auto, clear
    dtable, by(foreign, nototals) ///
        column( by(label, fvlabel) ) ///
        nosample ///
        continuous(mpg headroom weight length, statistics(mean median))
    You can see that the dimension result contains the following levels:
    Code:
    . collect levelsof result
    
    Collection: DTable
     Dimension: result
        Levels: _dtable_stats _dtable_test mean median
    where _dtable_stats is constructed thus:
    Code:
    . collect query composite _dtable_stats
    
    Composite definition
    Collection: DTable
     Composite: _dtable_stats
      Elements: mean
                median
     Delimiter: " "
          Trim: on
      Override: off
    Instead of using this composite result, we can instead use the component elements, mean and median, which are also stored as levels of the result dimension in the collection, as we saw above. Thus you can customize this layout as follows:
    Code:
    collect style header result,  title(hide) level(label)
    collect layout (var) (foreign#result[mean median])
    which produces:
    Code:
    . collect preview
    
    ------------------------------------------------------
                                  Car origin              
                         Domestic            Foreign      
                      Mean     Median     Mean     Median
    ------------------------------------------------------
    Mileage (mpg)     19.827    19.000    24.773    24.500
    Headroom (in.)     3.154     3.500     2.614     2.500
    Weight (lbs.)  3,317.115 3,360.000 2,315.909 2,180.000
    Length (in.)     196.135   200.000   168.545   170.000
    ------------------------------------------------------
    Last edited by Hemanshu Kumar; 07 Jul 2025, 05:08.

    Comment


    • #3
      Note that this is an FAQ: https://www.stata.com/support/faqs/r...ve-statistics/

      Comment


      • #4
        @Hemanshu: Thanks a lot, that is exactly what I wanted!

        @Andrew: My bad. I did search before posting the question, but evidently not good enough. Thanks for pointing the FAQ out

        Comment

        Working...
        X