Announcement

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

  • Trouble with tables

    Hi
    I'm trying to use Stata's Tables.
    But somehow I am getting the standard deviation twice:
    Code:
    . sysuse auto
    (1978 automobile data)
    
    . table rep78, stat(mean price) stat(sd price) command(_r_ci:mean price)
    
    ------------------------------------------------------------------------------------------------
                       |      Mean   Standard deviation                   mean price                
                       |      Mean   Standard deviation   Standard deviation           95% CI       
    -------------------+----------------------------------------------------------------------------
    Repair record 1978 |                                                                            
      1                |    4564.5             522.5519             522.5519   -130.4427    9259.443
      2                |  5967.625             3579.357             3579.357    2975.208    8960.042
      3                |  6429.233              3525.14              3525.14    5112.924    7745.542
      4                |    6071.5             1709.608             1709.608    5221.332    6921.668
      5                |      5913             2615.763             2615.763    4155.707    7670.293
      Total            |  6146.043              2912.44              2912.44    5446.399    6845.688
    ------------------------------------------------------------------------------------------------
    What am I doing wrong?
    Kind regards

    nhb

  • #2
    Hallo Niels,

    actually, I am not sure why standard deviation is printed twice. However, you could use "collect layout" for arranging the table (in Stata 17 or 18).

    Code:
    sysuse auto, clear
    
    table rep78, stat(mean price) stat(sd price) command(_r_ci:mean price)
    
    collect layout (rep78)(result[mean] result[sd]  result[_r_ci])
    Last edited by Simon Pfaff; 14 Nov 2023, 06:00.

    Comment


    • #3
      To see what is going on with your layout, change the header labeling
      to show dimension level values.

      Code:
      * get a report of the dimensions used in the layout
      collect layout
      * show the level labels of the column dimensions
      collect label list statcmd, all
      collect label list result, all
      * show the level values in the column headers
      collect style header statcmd result, level(value)
      * replay layout
      collect layout
      This is what I get with your example.
      Code:
      . * get a report of the dimensions used in the layout
      . collect layout
      
      Collection: Table
            Rows: rep78
         Columns: statcmd#result
         Table 1: 7 x 4
      
      ------------------------------------------------------------------------------------------------
                         |      Mean   Standard deviation                   mean price
                         |      Mean   Standard deviation   Standard deviation           95% CI
      -------------------+----------------------------------------------------------------------------
      Repair record 1978 |
        1                |    4564.5             522.5519             522.5519   -130.4427    9259.443
        2                |  5967.625             3579.357             3579.357    2975.208    8960.042
        3                |  6429.233              3525.14              3525.14    5112.924    7745.542
        4                |    6071.5             1709.608             1709.608    5221.332    6921.668
        5                |      5913             2615.763             2615.763    4155.707    7670.293
        Total            |  6146.043              2912.44              2912.44    5446.399    6845.688
      ------------------------------------------------------------------------------------------------
      
      . * show the level labels of the column dimensions
      . collect label list statcmd, all
      
        Collection: Table
         Dimension: statcmd
             Label: Statistic/command option index
      Level labels:
                 1  Mean
                 2  Standard deviation
                 3  mean price
      
      . collect label list result, all
      
        Collection: Table
         Dimension: result
             Label: Result
      Level labels:
                 N  Number of observations
            N_over  Number of subpopulations
                _N  Nonmissing observations
              _r_b  Coefficient
             _r_ci  __LEVEL__% CI
             _r_df  df
             _r_lb  __LEVEL__% lower bound
              _r_p  p-value
             _r_se  Std. error
             _r_ub  __LEVEL__% upper bound
              _r_z  t
          _r_z_abs  |t|
               cmd  Command
           cmdline  Command line as typed
              df_r  Sample DF
             error
         estat_cmd  Program used to implement estat
              k_eq  Number of equations
      marginsnotok  Predictions disallowed by margins
              mean  Mean
        properties  Command properties
              rank  Rank of VCE
                sd  Standard deviation
             title  Title of output
           varlist  Variables
               vce  SE method
      
      . * show the level values in the column headers
      . collect style header statcmd result, level(value)
      
      . * replay layout
      . collect layout
      
      Collection: Table
            Rows: rep78
         Columns: statcmd#result
         Table 1: 7 x 4
      
      ----------------------------------------------------------------------------
                         |         1          2                   3
                         |      mean         sd         sd           _r_ci
      -------------------+--------------------------------------------------------
      Repair record 1978 |
        1                |    4564.5   522.5519   522.5519   -130.4427    9259.443
        2                |  5967.625   3579.357   3579.357    2975.208    8960.042
        3                |  6429.233    3525.14    3525.14    5112.924    7745.542
        4                |    6071.5   1709.608   1709.608    5221.332    6921.668
        5                |      5913   2615.763   2615.763    4155.707    7670.293
        Total            |  6146.043    2912.44    2912.44    5446.399    6845.688
      ----------------------------------------------------------------------------
      From the labeling information we see that level 2 of dimension
      statcmd has label "Standard deviation", as does level sd
      of dimension result.

      Note that result sd shows up from option stat() for
      statcmd=2, but also shows up from matrix e(sd) from the
      mean command for statcmd=3. This is the source of your
      duplicate column of standard deviations.

      Code:
      . * -table- left behind the results of -mean price- for the sample where
      . * rep78=5
      . matrix list e(sd)
      
      symmetric e(sd)[1,1]
              price
      y1  2615.7628
      Here is the table, restoring the result level labels, and
      assuming you want just the statistic names in the column header.
      Code:
      . * restore the header styles
      . collect style header statcmd result, level(label)
      
      . collect style header statcmd, level(hide)
      
      .
      . * a more selective layout
      . collect layout (rep78) (statcmd[1 2]#result statcmd[3]#result[_r_ci])
      
      Collection: Table
            Rows: rep78
         Columns: statcmd[1 2]#result statcmd[3]#result[_r_ci]
         Table 1: 7 x 3
      
      ---------------------------------------------------------------------------
                         |      Mean   Standard deviation           95% CI      
      -------------------+-------------------------------------------------------
      Repair record 1978 |                                                      
        1                |    4564.5             522.5519   -130.4427    9259.443
        2                |  5967.625             3579.357    2975.208    8960.042
        3                |  6429.233              3525.14    5112.924    7745.542
        4                |    6071.5             1709.608    5221.332    6921.668
        5                |      5913             2615.763    4155.707    7670.293
        Total            |  6146.043              2912.44    5446.399    6845.688
      ---------------------------------------------------------------------------

      Comment


      • #4
        Simon Pfaff Thank you for your suggestion
        Jeff Pitblado (StataCorp) I have a bad habit of not reading the manual before too late.
        So what is happening is that the information, I want to show is already in the ereturn and I could just as well have done:
        Code:
        . qui table (rep78), command(e(N) e(b) _r_b e(sd) _r_ci:mean price) nformat(%9.1f)
        
        . collect style cell result[N], nformat(%9.0f)
        
        . collect preview
        
        --------------------------------------------------------------------------------------------------------------
                           |  Number of observations   Coefficient   Standard deviation estimates         95% CI      
                           |                                 Price                          Price          Price      
        -------------------+------------------------------------------------------------------------------------------
        Repair record 1978 |                                                                                          
          1                |                       2        4564.5                          522.6   -130.4      9259.4
          2                |                       8        5967.6                         3579.4   2975.2      8960.0
          3                |                      30        6429.2                         3525.1   5112.9      7745.5
          4                |                      18        6071.5                         1709.6   5221.3      6921.7
          5                |                      11        5913.0                         2615.8   4155.7      7670.3
          Total            |                      69        6146.0                         2912.4   5446.4      6845.7
        --------------------------------------------------------------------------------------------------------------
        PS Tables is a fantastic addition to Stata
        Kind regards

        nhb

        Comment

        Working...
        X