Announcement

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

  • How to stack marginal effects across outcomes in one columm for multinomial logit output?

    Hello,
    This is fairly simple question, but one I have not been able to find an answer to (although I have found a few unanswered threads). This is also my first time posting, so I apologize if I don't get this completely right (i have read the FAQs!).

    I am running multinomial logits, and would like to include in my regression output a column for coefficients and a column for the marginal effects next to it. While I have no problem running the regression or calculating the marginal effects, the -margins- output will only report horizontally for each outcome. I have to manually drag the marginal effects for each outcome to match where the coefficients are in the column.

    To help illustrate, my code:

    Code:
    mlogit zero2_end_cat_0015 di pi, base(1)
    estimates store m3, title("1900-1915")
    estpost margins, dydx(*) predict(outcome(1)) /* marginal effects - outcome 1 */
    estimates store marg3a, title(1)
    mlogit zero2_end_cat_0015 di pi, base(1)
    estpost margins, dydx(*) predict(outcome(3)) /* marginal effects - increasing to 100% */
    estimates store marg3b, title(3)
    mlogit zero2_end_cat_0015 di pi, base(1)
    estpost margins, dydx(*) predict(outcome(5)) /* marginal effects - partial increase */
    estimates store marg3c, title(5)
    esttab m3 marg3a marg3b marg3c using marginals1cq.csv, label se replace
    What I get from this is a table with 4 columns, instead of a table with 2 columns (I would love marg3a, marg3b and marg3c to stack on top of each other in one column, corresponding to their relevant outcome from m3.

    I have read everything that I can find, but nothing enables me to report them in one column. I can get the rrr reported in one column, and ultimately it would be great to have the coefficient, rrr, and margin listed together. Finally, I can see how this is done for logits or probits, but once there are multiple outcomes I don't see how I can get this to stack. Any help is much appreciated!


  • #2
    esttab is from SSC by Ben Jann, as you are asked to explain.

    -margins- output will only report horizontally for each outcome.
    You should have a look at the structure of the matrix that you obtain after running either mlogit or margins.

    Code:
    mat list e(b)
    This is what esttab works with, so unfortunately, manipulating these matrices to suit the desired format in esttab, while doable, is no easy task. On the other hand, esttab does not allow you to easily merge estimates from different models into one, which in principle I agree with as it will do more harm than good. In future, please consider presenting a reproducible example. In this case, that would entail enclosing a data example. What you could do is to output both coefficients and marginal effects using margins as the matrices from these will be of the same dimensions. Thereafter, present the results in multiple columns using esttab as illustrated below.

    Code:
    webuse sysdsn1
    forval i=2/3{
    mlogit insure age male nonwhite i.si
    estpost margins, dydx(*) predict(xb outcome(`i'))
    est sto ma`i'
    mlogit insure age male nonwhite i.si
    estpost margins, dydx(*) predict(pr outcome(`i'))
    est sto mb`i'
    }
    
    esttab ma2 mb2 ma3 mb3, nobaselevels noobs nonumbers ///
    mlabels("Coef." "Marg. eff." "Coef." "Marg. eff.") ///
    mgroups("Prepaid" "Uninsured", pattern(1 0 1 0))
    Res.:

    Code:
    . esttab ma2 mb2 ma3 mb3, nobaselevels noobs nonumbers mlabels("Coef.">
    "Marg. eff." "Coef." "Marg. eff.") mgroups("Prepaid" "Uninsured", pattern(1 0 1 0))
    
    ----------------------------------------------------------------------------
                      Prepaid                       Uninsured                  
                        Coef.      Marg. eff.           Coef.      Marg. eff.  
    ----------------------------------------------------------------------------
    age               -0.0117        -0.00251        -0.00780       -0.000151  
                      (-1.90)         (-1.80)         (-0.68)         (-0.21)  
    
    male                0.562**         0.118**         0.452          0.0125  
                       (2.77)          (2.64)          (1.23)          (0.54)  
    
    nonwhite            0.975***        0.222***        0.217         -0.0161  
                       (4.12)          (4.44)          (0.51)         (-0.61)  
    
    2.site              0.113          0.0608          -1.212*        -0.0679**
                       (0.54)          (1.26)         (-2.57)         (-2.82)  
    
    3.site             -0.588**        -0.126*         -0.208         0.00482  
                      (-2.58)         (-2.57)         (-0.57)          (0.15)  
    ----------------------------------------------------------------------------
    t statistics in parentheses
    * p<0.05, ** p<0.01, *** p<0.001

    Comment


    • #3
      Thank you for your response, Andrew. I will get together a set of example data, and thank you for taking the time to provide what you did. My issue is when there are more than 2 outcome categories, they are stacked vertically for the mlogit results (for example - which I know I didn't provide initially! - I have 5 outcome categories). In this sense, reporting the results horizontally is just hard to read, or requires me to manually move the margins to be together with the correct outcome afterwards.

      I am starting to thing estout might be better, and will try to come up with some solutions using that paired with the equations() option. Have you worked with this at all?
      Thank you!
      Laura

      Comment


      • #4
        To have the models stacked vertically, you will have to append sets of models. There is no straight merge solution as I stated before. From the method in #2, you can do something like the following. If you need to report the constants, store the results after running mlogit and use the equation option of esttab to match columns.

        Code:
        webuse sysdsn1
        forval i=1/3{
        mlogit insure age male nonwhite i.si
        estpost margins, dydx(*) predict(xb outcome(`i'))
        est sto ma`i'
        mlogit insure age male nonwhite i.si
        estpost margins, dydx(*) predict(pr outcome(`i'))
        est sto mb`i'
        }
        
        esttab ma1 mb1 using myfile.doc, replace eqlabels(none) ///
        nonumber nobaselevels noobs nonotes mlabel("Coef." "Marg.Eff.", lhs("Outcome 1"))
        forval i=2/3{
        esttab ma`i' mb`i' using myfile.doc,append eqlabels(none) ///
        nonumber nobaselevels noobs nonotes mlabel("Coef." "Marg.Eff.", lhs("Outcome `i'"))
        }

        Res.:

        Code:
        --------------------------------------------
        Outcome 1          Coef.        Marg.Eff.  
        --------------------------------------------
        age                     0         0.00267   
                              (.)          (1.91)   
        
        male                    0          -0.130** 
                              (.)         (-2.86)   
        
        nonwhite                0          -0.206***
                              (.)         (-3.96)   
        
        2.site                  0         0.00710   
                              (.)          (0.15)   
        
        3.site                  0           0.122*  
                              (.)          (2.40)   
        --------------------------------------------
        --------------------------------------------
        Outcome 2          Coef.        Marg.Eff.   
        --------------------------------------------
        age               -0.0117        -0.00251   
                          (-1.90)         (-1.80)   
        
        male                0.562**         0.118** 
                           (2.77)          (2.64)   
        
        nonwhite            0.975***        0.222***
                           (4.12)          (4.44)   
        
        2.site              0.113          0.0608   
                           (0.54)          (1.26)   
        
        3.site             -0.588**        -0.126*  
                          (-2.58)         (-2.57)   
        --------------------------------------------
        --------------------------------------------
        Outcome 3          Coef.        Marg.Eff.   
        --------------------------------------------
        age              -0.00780       -0.000151   
                          (-0.68)         (-0.21)   
        
        male                0.452          0.0125   
                           (1.23)          (0.54)   
        
        nonwhite            0.217         -0.0161   
                           (0.51)         (-0.61)   
        
        2.site             -1.212*        -0.0679** 
                          (-2.57)         (-2.82)   
        
        3.site             -0.208         0.00482   
                          (-0.57)          (0.15)   
        --------------------------------------------

        Comment


        • #5
          Hi Prof. Andrew Musau.

          I have used Mlogit. I want to create a comparative table of Coefficients and Marginal effects of three regions: Kenya, Kisii and Eldoret. Kindly assist on how to go about this. Thank you.

          Comment


          • #6
            Francis, can you show me your mlogit command and a tabulation of your dependent variable?

            Code:
            tabulate dv1
            where you replace dv1 with the name of your dependent variable.
            Last edited by Andrew Musau; 18 Mar 2022, 05:16.

            Comment


            • #7
              I have three regions.

              forval i=1(1)8{
              mlogit CSA1 County2 $X2list, vce(robust) base(1)
              estpost margins, dydx(*) predict(xb outcome(`i'))
              est sto ka`i'
              mlogit CSA1 County2 $X2list, vce(robust) base(1)
              estpost margins, dydx(*) predict(pr outcome(`i'))
              est sto kb`i'
              }

              Comment


              • #8
                This is how my intended table should be including the stats

                forval i=1(1)8{
                esttab ma`i' mb`i' ka`i' kb`i' ua`i' ub`i',append eqlabels(none)nonumber nobaselevels noobs nonotes mlabel("Coef." "Marg.Eff." "Coef." "Marg.Eff." "Coef." "Marg.Eff.", lhs("Outcome `i'")) mgroups("Kenya" "Kisii" "Eldoret", pattern(1 0 1 0 1 0))
                }

                However, I cannot proceed from "forval ..." to incorporate the three regions

                Comment


                • #9
                  What variable defines regions? You need an -if- condition to run the model on a subsample of the data. In the code below, I assume that you have a string variable called "region" with only the 2 regions specified.

                  Code:
                  forval i=1/8{
                  mlogit CSA1 County2 $X2list, vce(robust) base(1)
                  estpost margins, dydx(*) predict(xb outcome(`i'))
                  est sto ma`i'
                  mlogit CSA1 County2 $X2list, vce(robust) base(1)
                  estpost margins, dydx(*) predict(pr outcome(`i'))
                  est sto mb`i'
                  }
                  
                  
                  foreach region in Kisii Eldoret
                      forval i=1/8{
                          mlogit CSA1 County2 $X2list if region=="`region'", vce(robust) base(1)
                          estpost margins, dydx(*) predict(xb outcome(`i'))
                          local prefix= cond("`region'"=="Kisii", "ka", "ua")
                          est sto `prefix'`i'
                          mlogit CSA1 County2 $X2list if region=="`region'", vce(robust) base(1)
                          estpost margins, dydx(*) predict(pr outcome(`i'))
                          local prefix= cond("`region'"=="Kisii", "kb", "ub")
                          est sto `prefix'`i'
                      }
                  }

                  If you cannot make progress with this, post a data example using the dataex command, including the variable defining regions.

                  Code:
                  dataex CSA1 County2 $X2list region 
                  Last edited by Andrew Musau; 18 Mar 2022, 06:07.

                  Comment


                  • #10
                    dataex CSA Sex Age HHSize Farmsize Region in 1/10

                    ----------------------- copy starting from the next line -----------------------
                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input float CSA byte(Sex Age HHSize) double Farmsize long Region
                    4 0 35 6 .25 1
                    4 1 40 5   2 1
                    4 0 35 3   1 2
                    4 0 35 3   1 1
                    4 0 35 3   2 1
                    4 1 52 7   1 2
                    4 0 64 2   1 1
                    3 0 72 1  .5 1
                    4 0 60 4   1 1
                    1 0 52 4 .75 1
                    end
                    label values Region Region
                    label def Region 1 "Kisii", modify
                    ------------------ copy up to and including the previous line ------------------

                    Listed 10 out of 447 observations


                    Comment


                    • #11
                      Region 1 "Kisii" ; Region 2 "Eldoret"

                      Comment


                      • #12
                        Code:
                        * Example generated by -dataex-. To install: ssc install dataex
                        clear
                        input float CSA byte(Sex Age HHSize) double Farmsize long Region
                        4 0 35 6 .25 1
                        4 1 40 5   2 1
                        4 0 35 3   1 2
                        4 0 35 3   1 1
                        4 0 35 3   2 1
                        4 1 52 7   1 2
                        4 0 64 2   1 1
                        3 0 72 1  .5 1
                        4 0 60 4   1 1
                        1 0 52 4 .75 1
                        end
                        label values Region Region
                        label def Region 1 "Kisii", modify
                        label def Region 2 "Eldoret", modify
                        
                        set seed 03212022
                        expand 20
                        replace CSA= runiformint(1, 8)
                        replace Sex= runiformint(0, 1)
                        replace HHSize= runiformint(1, 6)
                        replace Farmsize= abs(rnormal())
                        replace Region= runiformint(1, 2)
                        
                        forval i=1/8{
                        mlogit CSA Sex Age HHSize Farmsize, vce(robust) base(1)
                        estpost margins, dydx(*) predict(xb outcome(`i'))
                        est sto ma`i'
                        mlogit CSA Sex Age HHSize Farmsize, vce(robust) base(1)
                        estpost margins, dydx(*) predict(pr outcome(`i'))
                        est sto mb`i'
                        }
                        
                        
                        forval region= 1/2{
                            forval i=1/8{
                                mlogit CSA Sex Age HHSize Farmsize if Region==`region', vce(robust) base(1)
                                estpost margins, dydx(*) predict(xb outcome(`i'))
                                local prefix= cond(`region'==1, "ka", "ua")
                                est sto `prefix'`i'
                                mlogit CSA Sex Age HHSize Farmsize if Region==`region', vce(robust) base(1)
                                estpost margins, dydx(*) predict(pr outcome(`i'))
                                local prefix= cond(`region'==1, "kb", "ub")
                                est sto `prefix'`i'
                            }
                        }
                        
                        esttab ma1 mb1 ka1 kb1 ua1 ub1 using myfile.csv, replace eqlabels(none) ///
                        nonumber nobaselevels noobs nonotes mlabel("Coef." "Marg.Eff.", lhs("Outcome 1"))
                        forval i=2/8{
                            esttab ma`i' mb`i' ka`i' kb`i' ua`i' ub`i' using myfile.csv, compress append eqlabels(none) ///
                            nonumber nobaselevels noobs nonotes mlabel("Coef." "Marg.Eff.", lhs("Outcome `i'"))
                        }

                        Output in CSV format. You need to change the model titles and create headings. See

                        Code:
                        help estout
                        Res.:

                        Outcome 1 Coef. Marg.Eff. ka1 kb1 ua1 ub1
                        Sex 0 -0.00630 0 -0.0135 0 0.0183
                        (.) (-0.13) (.) (-0.22) (.) (0.25)
                        Age 0 -0.000237 0 -0.00323 0 0.00213
                        (.) (-0.13) (.) (-1.16) (.) (0.84)
                        HHSize 0 -0.0207 0 -0.0326* 0 -0.00887
                        (.) (-1.49) (.) (-2.01) (.) (-0.43)
                        Farmsize 0 -0.00114 0 0.0587 0 -0.0715
                        (.) (-0.03) (.) (1.19) (.) (-1.16)
                        Outcome 2 Coef. Marg.Eff. ka2 kb2 ua2 ub2
                        Sex -0.253 -0.0287 -0.131 -0.00617 -0.393 -0.0331
                        (-0.43) (-0.69) (-0.13) (-0.14) (-0.51) (-0.52)
                        Age 0.0184 0.00147 0.0610 0.00174 -0.00245 0.00151
                        (0.85) (1.01) (1.50) (1.13) (-0.08) (0.56)
                        HHSize 0.274 0.0108 0.711 0.0182 0.107 0.00560
                        (1.59) (0.91) (1.80) (0.97) (0.49) (0.30)
                        Farmsize 0.383 0.0349 0.315 0.0454 0.645 0.0211
                        (0.80) (1.06) (0.40) (1.23) (0.97) (0.38)
                        Outcome 3 Coef. Marg.Eff. ka3 kb3 ua3 ub3
                        Sex -0.166 -0.0284 -0.374 -0.0500 -0.339 -0.0285
                        (-0.30) (-0.57) (-0.42) (-0.75) (-0.43) (-0.39)
                        Age -0.00599 -0.00111 0.0336 0.000466 -0.0396 -0.00328
                        (-0.30) (-0.63) (0.91) (0.18) (-1.47) (-1.37)
                        HHSize 0.0960 -0.00788 0.255 -0.0117 0.0658 -0.000102
                        (0.63) (-0.59) (0.97) (-0.72) (0.29) (-0.00)
                        Farmsize 0.246 0.0323 -0.498 -0.00293 1.052 0.0807
                        (0.54) (0.80) (-0.67) (-0.05) (1.70) (1.46)
                        Outcome 4 Coef. Marg.Eff. ka4 kb4 ua4 ub4
                        Sex -0.358 -0.0387 -1.560 -0.138 0.544 0.0571
                        (-0.58) (-0.88) (-1.52) (-1.96) (0.64) (1.00)
                        Age -0.0152 -0.00168 0.0156 -0.00194 -0.0184 -0.000331
                        (-0.65) (-1.03) (0.38) (-0.91) (-0.57) (-0.17)
                        HHSize 0.322 0.0163 0.873** 0.0416* -0.135 -0.0166
                        (1.66) (1.15) (2.62) (2.03) (-0.52) (-0.94)
                        Farmsize -0.282 -0.0289 -0.930 -0.0490 0.287 -0.0159
                        (-0.48) (-0.66) (-1.17) (-0.91) (0.34) (-0.29)
                        Outcome 5 Coef. Marg.Eff. ka5 kb5 ua5 ub5
                        Sex 0.124 0.00917 0.623 0.0552 -0.422 -0.0325
                        (0.21) (0.21) (0.65) (0.88) (-0.52) (-0.49)
                        Age -0.0168 -0.00201 0.0213 -0.00104 -0.0428 -0.00300
                        (-0.69) (-1.06) (0.55) (-0.45) (-1.17) (-0.98)
                        HHSize 0.108 -0.00465 0.338 -0.00121 0.0267 -0.00441
                        (0.63) (-0.35) (1.27) (-0.08) (0.11) (-0.22)
                        Farmsize 0.110 0.0112 0.0553 0.0462 0.370 -0.0132
                        (0.27) (0.44) (0.09) (1.30) (0.61) (-0.30)
                        Outcome 6 Coef. Marg.Eff. ka6 kb6 ua6 ub6
                        Sex 0.405 0.0410 0.828 0.113 -0.402 -0.0224
                        (0.68) (0.88) (0.94) (1.61) (-0.41) (-0.38)
                        Age 0.0260 0.00253 0.0520 0.00264 0.0293 0.00320
                        (1.23) (1.54) (1.46) (1.08) (0.80) (1.38)
                        HHSize 0.211 0.00556 0.487* 0.0148 0.0143 -0.00323
                        (1.36) (0.48) (2.05) (0.91) (0.06) (-0.23)
                        Farmsize 0.182 0.0177 0.216 0.0847 0.138 -0.0243
                        (0.37) (0.47) (0.34) (1.83) (0.13) (-0.36)
                        Outcome 7 Coef. Marg.Eff. ka7 kb7 ua7 ub7
                        Sex 0.400 0.0383 0.941 0.0905 -0.629 -0.0405
                        (0.66) (0.84) (1.08) (1.48) (-0.65) (-0.64)
                        Age -0.0170 -0.00194 0.00391 -0.00246 -0.0540 -0.00314
                        (-0.72) (-1.09) (0.11) (-1.10) (-1.48) (-1.26)
                        HHSize -0.0496 -0.0209 -0.341 -0.0656*** 0.388 0.0274
                        (-0.28) (-1.59) (-1.16) (-3.60) (1.36) (1.39)
                        Farmsize -0.529 -0.0570 -1.708* -0.137** 0.537 0.00338
                        (-1.18) (-1.79) (-2.31) (-2.62) (0.90) (0.11)
                        Outcome 8 Coef. Marg.Eff. ka8 kb8 ua8 ub8
                        Sex 0.116 0.0137 -0.405 -0.0508 0.307 0.0816
                        (0.22) (0.25) (-0.48) (-0.74) (0.44) (1.06)
                        Age 0.0196 0.00297 0.0567 0.00382 0.00154 0.00293
                        (1.00) (1.46) (1.52) (1.32) (0.06) (1.07)
                        HHSize 0.279 0.0215 0.652** 0.0366 0.0589 0.000170
                        (1.78) (1.28) (2.59) (1.84) (0.28) (0.01)
                        Farmsize -0.0260 -0.00905 -0.699 -0.0457 0.575 0.0198
                        (-0.06) (-0.21) (-1.02) (-0.76) (1.06) (0.35)

                        Comment


                        • #13
                          Thank you so much Prof. Musau. This has perfectly worked. Asante sana.

                          Comment


                          • #14
                            Hello Prof. Musau

                            I'm interested in combining outcome variables after running the command: mlogtest, combine

                            My data is as follows
                            . dataex CSA $X2list in 1/10

                            ----------------------- copy starting from the next line -----------------------
                            Code:
                            * Example generated by -dataex-. To install: ssc install dataex
                            clear
                            input float CSA double(LnRainfall LnTempMax) byte(ReceiveEW DairySystm CoopMember)
                            4 7.30459072871209  3.2576040571261 1 1 1
                            4  7.3047311520909 3.25763483969567 1 1 0
                            4 7.30484719693626 3.25765065610546 1 1 0
                            4 7.30485105190397 3.25765132788405 1 1 0
                            4 7.30486224337103 3.25765461388177 1 1 0
                            4 7.30486569715882 3.26845600368271 0 3 0
                            4 7.30502120808853 3.25768208097711 0 2 0
                            3 7.30547671743472 3.26833016211082 0 1 0
                            4 7.30557414347333 3.26833833888383 0 2 0
                            1 7.30587754809625 3.25783774133195 1 1 0
                            end
                            label values CSA CSA
                            label def CSA 1 "FoVoCo", modify
                            label def CSA 3 "FoV1Co", modify
                            label def CSA 4 "FoVoC1", modify

                            The results of mlogtest, combine command is as follows;

                            . mlogtest, combine

                            Wald tests for combining alternatives (N=447)

                            Ho: All coefficients except intercepts associated with a given pair
                            of alternatives are 0 (i.e., alternatives can be combined)

                            | chi2 df P>chi2
                            -----------------+-------------------------
                            FoVoCo & F1VoCo | 14.035 6 0.029
                            FoVoCo & FoV1Co | 5.612 6 0.468
                            FoVoCo & FoVoC1 | 13.790 6 0.032
                            FoVoCo & F1V1Co | 6.722 6 0.347
                            FoVoCo & F1VoC1 | 32.485 6 0.000
                            FoVoCo & FoV1C1 | 38.973 6 0.000
                            FoVoCo & F1V1C1 | 34.625 6 0.000
                            F1VoCo & FoV1Co | 14.468 6 0.025
                            F1VoCo & FoVoC1 | 20.372 6 0.002
                            F1VoCo & F1V1Co | 11.974 6 0.063
                            F1VoCo & F1VoC1 | 6.962 6 0.324
                            F1VoCo & FoV1C1 | 30.602 6 0.000
                            F1VoCo & F1V1C1 | 26.501 6 0.000
                            FoV1Co & FoVoC1 | 10.893 6 0.092
                            FoV1Co & F1V1Co | 4.004 6 0.676
                            FoV1Co & F1VoC1 | 22.222 6 0.001
                            FoV1Co & FoV1C1 | 10.876 6 0.092
                            FoV1Co & F1V1C1 | 12.591 6 0.050
                            FoVoC1 & F1V1Co | 3.701 6 0.717
                            FoVoC1 & F1VoC1 | 50.407 6 0.000
                            FoVoC1 & FoV1C1 | 39.307 6 0.000
                            FoVoC1 & F1V1C1 | 31.340 6 0.000
                            F1V1Co & F1VoC1 | 9.935 6 0.127
                            F1V1Co & FoV1C1 | 2.042 6 0.916
                            F1V1Co & F1V1C1 | 1.371 6 0.968
                            F1VoC1 & FoV1C1 | 63.720 6 0.000
                            F1VoC1 & F1V1C1 | 49.980 6 0.000
                            FoV1C1 & F1V1C1 | 8.244 6 0.221

                            This results indeed indicates that some outcomes must be combined. Kindly assist me learn how to combine the outcomes.

                            Thank you

                            Comment


                            • #15
                              Can you post the question in a new thread titled "how to combine categories after mlogtest"? This thread concerns exporting the results of mlogit.

                              Comment

                              Working...
                              X