Announcement

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

  • etable of multiple mlogit results: how to create multi-level column header

    The following code runs two mlogit models and then creates a table where the coefficients for two different outcomes are shown side-by-side rather than stacked. Each pair of outcomes is labeled (Model 1 and Model 2 respectively.) How can I add a row which labels each of the four columns with the label for the appropriate level of the dependent variable?

    Thanks,
    Devra

    Code:
    clear all
    use http://www.stata-press.com/data/r15/nmihs.dta
    
    mlogit bwgrp marital age highbp, baseoutcome(3)
    estimates store m1
    
    mlogit bwgrp marital age highbp race, baseoutcome(3)
    estimates store m2
    
    etable, estimates (m1 m2) showstars
    collect label levels cmdset 1 "Model 1" 2 "Model 2", modify
    collect layout (colname#result[_r_b _r_se] result[N]) ///
                    (cmdset#coleq[]#stars[])
    Devra Golbe
    Professor Emerita, Dept. of Economics
    Hunter College, CUNY

  • #2
    Thanks for the data example.

    Code:
    clear all
    use http://www.stata-press.com/data/r15/nmihs.dta
    
    mlogit bwgrp marital age highbp, baseoutcome(3)
    estimates store m1
    
    mlogit bwgrp marital age highbp race, baseoutcome(3)
    estimates store m2
    
    
    
    etable, estimates (m1 m2) showstars
    collect label levels cmdset 1 "Model 1" 2 "Model 2", modify
    collect label levels coleq 1 "`:lab (bwgrp) 1'" 2 "`:lab (bwgrp) 2'" 3 "`:lab (bwgrp) 3'", modify
    collect style header coleq, title(hide) level(value)
    collect layout (colname#result[_r_b _r_se] result[N]) ///
                    (cmdset#coleq[]#stars[])
    Res.:

    Code:
    . collect layout (colname#result[_r_b _r_se] result[N]) ///
    >                 (cmdset#coleq[]#stars[])
    
    Collection: ETable
          Rows: colname#result[_r_b _r_se] result[N]
       Columns: cmdset#coleq[]#stars[]
       Table 1: 10 x 8
    
    ---------------------------------------------------------------------------
                                           Model 1               Model 2       
                                       bw_15     bw15_25     bw_15     bw15_25 
    ---------------------------------------------------------------------------
    0=single, 1=married              -0.423 **  -0.346 **  -0.490 **  -0.419 **
                                    (0.062)    (0.062)    (0.069)    (0.069)   
    Mother's age in years             0.012 *    0.003      0.012 *    0.002   
                                    (0.005)    (0.005)    (0.005)    (0.005)   
    High blood pressure: 1=yes,0=no   0.664 **   0.635 **   0.664 **   0.634 **
                                    (0.107)    (0.108)    (0.107)    (0.108)   
    Race: 1=black, 0=white/other                           -0.145 *   -0.157 * 
                                                          (0.065)    (0.065)   
    Intercept                        -1.617 **  -1.427 **  -1.493 **  -1.292 **
                                    (0.129)    (0.129)    (0.140)    (0.141)   
    ---------------------------------------------------------------------------
    Last edited by Andrew Musau; 26 Oct 2023, 08:15.

    Comment


    • #3
      Andrew,

      Thanks so much! This is exactly what I need. In an effort to understand how your code works, I tried adding the additional lines one at a time. It appears that the following code produces the same results as your code. What am I missing?

      Code:
      clear all
      use http://www.stata-press.com/data/r15/nmihs.dta
      
      mlogit bwgrp marital age highbp, baseoutcome(3)
      estimates store m1
      
      mlogit bwgrp marital age highbp race, baseoutcome(3)
      estimates store m2
      
      etable, estimates (m1 m2) showstars
      collect label levels cmdset 1 "Model 1" 2 "Model 2", modify
      *collect label levels coleq  ///
      *     1 "`:lab (bwgrp) 1'" 2 "`:lab (bwgrp) 2'" 3 "`:lab (bwgrp) 3'", modify
      *collect style header coleq, title(label)
      collect style header coleq, title(hide) level(value)
      collect layout (colname#result[_r_b _r_se] result[N]) ///
                      (cmdset#coleq[]#stars[])

      Code:
      Collection: ETable
            Rows: colname#result[_r_b _r_se] result[N]
         Columns: cmdset#coleq[]#stars[]
         Table 1: 10 x 8
      
      ---------------------------------------------------------------------------
                                             Model 1               Model 2       
                                         bw_15     bw15_25     bw_15     bw15_25
      ---------------------------------------------------------------------------
      0=single, 1=married              -0.423 **  -0.346 **  -0.490 **  -0.419 **
                                      (0.062)    (0.062)    (0.069)    (0.069)   
      Mother's age in years             0.012 *    0.003      0.012 *    0.002   
                                      (0.005)    (0.005)    (0.005)    (0.005)   
      High blood pressure: 1=yes,0=no   0.664 **   0.635 **   0.664 **   0.634 **
                                      (0.107)    (0.108)    (0.107)    (0.108)   
      Race: 1=black, 0=white/other                           -0.145 *   -0.157 *
                                                            (0.065)    (0.065)   
      Intercept                        -1.617 **  -1.427 **  -1.493 **  -1.292 **
                                      (0.129)    (0.129)    (0.140)    (0.141)   
      ---------------------------------------------------------------------------
      Devra Golbe
      Professor Emerita, Dept. of Economics
      Hunter College, CUNY

      Comment


      • #4
        You are not missing anything. The following is unnecessary as the variable's values are already labeled.

        collect label levels coleq 1 "`:lab (bwgrp) 1'" 2 "`:lab (bwgrp) 2'" 3 "`:lab (bwgrp) 3'", modify
        You would only need to define the labels if the variable's values were not labeled or you wanted to change the labels on the fly.

        Comment


        • #5
          Thanks! I'm sure I will find that code useful not too far down the road. Not to impose on your time and expertise, but what does this line do?
          Code:
           
           collect style header coleq, title(label)
          Devra Golbe
          Professor Emerita, Dept. of Economics
          Hunter College, CUNY

          Comment


          • #6
            Explained in

            Code:
            help collect style header

            Comment


            • #7
              Thanks. I'll read that again, more carefully. :-)
              Devra Golbe
              Professor Emerita, Dept. of Economics
              Hunter College, CUNY

              Comment


              • #8
                So the description reads

                collect style header edits the content of the table headers. With this style, you can specify how the dimensions and levels are displayed in row, column, and table headers. For each dimension, the name of the dimension, the label of the dimension,
                or nothing may be displayed. Likewise, for levels within a dimension, the label of that level, the value of the level, or nothing may be displayed.
                You can see the dimensions in your table by running

                Code:
                collect dims

                Res.:

                Code:
                . collect dims
                
                Collection dimensions
                Collection: ETable
                -----------------------------------------
                                   Dimension   No. levels
                -----------------------------------------
                Layout, style, header, label
                                      cmdset   2        
                                       coleq   3        
                                     colname   13        
                               etable_depvar   2        
                              etable_dvlabel   2        
                            etable_estimates   2        
                                etable_title   2        
                               program_class   2        
                                      result   49        
                                 result_type   3        
                                     rowname   1        
                                       stars   2        
                
                Style only
                                border_block   4        
                                   cell_type   4        
                -----------------------------------------

                So "coleq" is a dimension with 3 levels. We know that these are the levels of the outcome in the multinomial logit. The option -level(value)- in

                collect style header coleq, title(hide) level(value)
                is described by the second part of the paragraph above:

                Likewise, for levels within a dimension, the label of that level, the value of the level, or nothing may be displayed
                Here, I opt for values of the levels to be displayed. Other possibilities would be

                Code:
                level(label)
                and

                Code:
                level(hide)
                corresponding to the label of the dimension and nothing, respectively. Finally


                title(tstyle) specifies the dimension title header style to be used in row, column, and table headers. tstyle may be label, name, or hide.
                In #2, I have -title(hide)- as the default is -title(label)- and I don't want the title of the dimension to be displayed. The other possibility is

                Code:
                title(name)
                corresponding to the name of the dimension as the title. Note that we can have other dimensions displayed (as well as their titles) in the column or row, using collect style header, as we have done for "coleq". Hope this helps.
                Last edited by Andrew Musau; 27 Oct 2023, 02:09.

                Comment


                • #9
                  Andrew: Thanks yet again for this detailed response. It's been very helpful to work through the options.
                  Devra Golbe
                  Professor Emerita, Dept. of Economics
                  Hunter College, CUNY

                  Comment


                  • #10
                    Originally posted by Andrew Musau View Post
                    Thanks for the data example.

                    Code:
                    clear all
                    use http://www.stata-press.com/data/r15/nmihs.dta
                    
                    mlogit bwgrp marital age highbp, baseoutcome(3)
                    estimates store m1
                    
                    mlogit bwgrp marital age highbp race, baseoutcome(3)
                    estimates store m2
                    
                    
                    
                    etable, estimates (m1 m2) showstars
                    collect label levels cmdset 1 "Model 1" 2 "Model 2", modify
                    collect label levels coleq 1 "`:lab (bwgrp) 1'" 2 "`:lab (bwgrp) 2'" 3 "`:lab (bwgrp) 3'", modify
                    collect style header coleq, title(hide) level(value)
                    collect layout (colname#result[_r_b _r_se] result[N]) ///
                    (cmdset#coleq[]#stars[])
                    Res.:

                    Code:
                    . collect layout (colname#result[_r_b _r_se] result[N]) ///
                    > (cmdset#coleq[]#stars[])
                    
                    Collection: ETable
                    Rows: colname#result[_r_b _r_se] result[N]
                    Columns: cmdset#coleq[]#stars[]
                    Table 1: 10 x 8
                    
                    ---------------------------------------------------------------------------
                    Model 1 Model 2
                    bw_15 bw15_25 bw_15 bw15_25
                    ---------------------------------------------------------------------------
                    0=single, 1=married -0.423 ** -0.346 ** -0.490 ** -0.419 **
                    (0.062) (0.062) (0.069) (0.069)
                    Mother's age in years 0.012 * 0.003 0.012 * 0.002
                    (0.005) (0.005) (0.005) (0.005)
                    High blood pressure: 1=yes,0=no 0.664 ** 0.635 ** 0.664 ** 0.634 **
                    (0.107) (0.108) (0.107) (0.108)
                    Race: 1=black, 0=white/other -0.145 * -0.157 *
                    (0.065) (0.065)
                    Intercept -1.617 ** -1.427 ** -1.493 ** -1.292 **
                    (0.129) (0.129) (0.140) (0.141)
                    ---------------------------------------------------------------------------
                    Hello Andrew.

                    I'm bumping this older thread because I noticed that in your code you do call for N to be displayed, but ultimately it is not displayed. In my understanding it happens because it is not tagged with cmdset#coleq[]#stars[] .
                    So what would be the code to display it in the last row in each of the columns? I guess that would involve the duplication of result[N], and I'm not sure if it's possible in collect machinery without collecting/getting the result first and tagging it with coleq levels.
                    Or better yet, is it possible to display N once for each model, but place the result in between the two equations of each model for later -collect export- ?

                    Thanks.

                    Comment


                    • #11
                      Originally posted by Evgeny Saburov View Post
                      I'm bumping this older thread because I noticed that in your code you do call for N to be displayed, but ultimately it is not displayed. In my understanding it happens because it is not tagged with cmdset#coleq[]#stars[] .
                      So what would be the code to display it in the last row in each of the columns? I guess that would involve the duplication of result[N], and I'm not sure if it's possible in collect machinery without collecting/getting the result first and tagging it with coleq levels.
                      Or better yet, is it possible to display N once for each model, but place the result in between the two equations of each model for later -collect export- ?
                      The code is not mine, but from the OP. In the revised layout, each column is a level of the DV. The command mlogit AFAIK stores only the aggregate number of observations, not the observation count by levels of the DV. So my guess is that you will have to calculate these counts and use collect get to put them into your collection.

                      Comment


                      • #12
                        Originally posted by Evgeny Saburov View Post
                        So what would be the code to display it in the last row in each of the columns?
                        Following #11, here is code that implements the procedure based on the example in #2:

                        Code:
                        clear all
                        use http://www.stata-press.com/data/r15/nmihs.dta
                        
                        mlogit bwgrp marital age highbp, baseoutcome(3)
                        estimates store m1
                        
                        qui levelsof bwgrp, local(levs)
                        foreach l in `levs'{
                            qui count if bwgrp==`l' & e(sample)
                            local N1_`l'= r(N)
                        }
                        
                        mlogit bwgrp marital age highbp race, baseoutcome(3)
                        estimates store m2
                        
                        qui levelsof bwgrp, local(levs)
                        foreach l in `levs'{
                            qui count if bwgrp==`l' & e(sample)
                            local N2_`l'= r(N)
                        }
                        
                        etable, estimates (m1 m2) showstars
                        collect label levels cmdset 1 "Model 1" 2 "Model 2", modify
                        collect label levels coleq 1 "`:lab (bwgrp) 1'" 2 "`:lab (bwgrp) 2'" 3 "`:lab (bwgrp) 3'", modify
                        collect style header coleq, title(hide) level(value)
                        
                        collect get nobs= `N1_1', tags(nobs[1] cmdset[1] coleq[bw_15])
                        collect get nobs= `N1_2', tags(nobs[1] cmdset[1] coleq[bw15_25])
                        collect get nobs= `N2_1', tags(nobs[1] cmdset[2] coleq[bw_15])
                        collect get nobs= `N2_2', tags(nobs[1] cmdset[2] coleq[bw15_25])
                        collect label levels nobs 1 "Number of observations", replace
                        collect style header nobs, title(hide) level(label)
                        collect style cell nobs, nformat(%13.0f)
                        
                        collect layout (colname#result[_r_b _r_se] nobs[1]) ///
                                        (cmdset#coleq[]#stars[])
                        Note that to see the levels of dimensions cmdset and coleq so that you know how to tag the added statistics, run:

                        Code:
                        collect levelsof cmdset
                        collect levelsof coleq
                        Res.:


                        Code:
                        . collect layout (colname#result[_r_b _r_se] nobs[1]) ///
                        >                 (cmdset#coleq[]#stars[])
                        
                        Collection: ETable
                              Rows: colname#result[_r_b _r_se] nobs[1]
                           Columns: cmdset#coleq[]#stars[]
                           Table 1: 11 x 8
                        
                        ---------------------------------------------------------------------------
                                                               Model 1               Model 2       
                                                           bw_15     bw15_25     bw_15     bw15_25 
                        ---------------------------------------------------------------------------
                        0=single, 1=married              -0.423 **  -0.346 **  -0.490 **  -0.419 **
                                                        (0.062)    (0.062)    (0.069)    (0.069)   
                        Mother's age in years             0.012 *    0.003      0.012 *    0.002   
                                                        (0.005)    (0.005)    (0.005)    (0.005)   
                        High blood pressure: 1=yes,0=no   0.664 **   0.635 **   0.664 **   0.634 **
                                                        (0.107)    (0.108)    (0.107)    (0.108)   
                        Race: 1=black, 0=white/other                           -0.145 *   -0.157 * 
                                                                              (0.065)    (0.065)   
                        Intercept                        -1.617 **  -1.427 **  -1.493 **  -1.292 **
                                                        (0.129)    (0.129)    (0.140)    (0.141)   
                        Number of observations             1525       1519       1525       1519   
                        ---------------------------------------------------------------------------

                        Comment


                        • #13
                          Originally posted by Andrew Musau View Post

                          The code is not mine, but from the OP.
                          I missed that, my apologies.

                          Originally posted by Andrew Musau View Post
                          In the revised layout, each column is a level of the DV. The command mlogit AFAIK stores only the aggregate number of observations, not the observation count by levels of the DV. So my guess is that you will have to calculate these counts and use collect get to put them into your collection.
                          I see. I actually did want to display the aggregate number of observation in the model for each equation, although I understand it's redundant. I can see how I can modify the code you provided for this task. Thank you for it.
                          Last edited by Evgeny Saburov; 14 May 2025, 01:13.

                          Comment


                          • #14
                            Originally posted by Evgeny Saburov View Post
                            I actually did want to display the aggregate number of observation in the model for each equation, although I understand it's redundant.
                            In that case, then it's just a matter of adding tags to a particular level of the dimension coleq.


                            Code:
                            clear all
                            collect clear
                            use http://www.stata-press.com/data/r15/nmihs.dta
                            
                            mlogit bwgrp marital age highbp, baseoutcome(3)
                            estimates store m1
                            
                            
                            mlogit bwgrp marital age highbp race, baseoutcome(3)
                            estimates store m2
                            
                            etable, estimates (m1 m2) showstars
                            collect label levels cmdset 1 "Model 1" 2 "Model 2", modify
                            collect label levels coleq 1 "`:lab (bwgrp) 1'" 2 "`:lab (bwgrp) 2'" 3 "`:lab (bwgrp) 3'", modify
                            collect style header coleq, title(hide) level(value)
                            collect addtags coleq[bw_15], fortags(result[N])
                            collect layout (colname#result[_r_b _r_se] result[N]) ///
                                            (cmdset#coleq[]#stars[])
                            Res.:

                            Code:
                             collect layout (colname#result[_r_b _r_se] result[N]) ///
                            >                 (cmdset#coleq[]#stars[])
                            
                            Collection: ETable
                                  Rows: colname#result[_r_b _r_se] result[N]
                               Columns: cmdset#coleq[]#stars[]
                               Table 1: 11 x 8
                            
                            ---------------------------------------------------------------------------
                                                                   Model 1               Model 2       
                                                               bw_15     bw15_25     bw_15     bw15_25 
                            ---------------------------------------------------------------------------
                            0=single, 1=married              -0.423 **  -0.346 **  -0.490 **  -0.419 **
                                                            (0.062)    (0.062)    (0.069)    (0.069)   
                            Mother's age in years             0.012 *    0.003      0.012 *    0.002   
                                                            (0.005)    (0.005)    (0.005)    (0.005)   
                            High blood pressure: 1=yes,0=no   0.664 **   0.635 **   0.664 **   0.634 **
                                                            (0.107)    (0.108)    (0.107)    (0.108)   
                            Race: 1=black, 0=white/other                           -0.145 *   -0.157 * 
                                                                                  (0.065)    (0.065)   
                            Intercept                        -1.617 **  -1.427 **  -1.493 **  -1.292 **
                                                            (0.129)    (0.129)    (0.140)    (0.141)   
                            Number of observations             9946                  9946              
                            ---------------------------------------------------------------------------

                            Comment


                            • #15
                              Thanks again Andrew.

                              Comment

                              Working...
                              X