Announcement

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

  • Categorical variable not found when exporting margins to LaTeX using esttab

    Hi,

    I am running an ordered logit model. The outcome variable takes 5 values. When calculating the margins and exporting the results, I get an error that i.agegrp is not found. The codes that I have used:

    Code:
    global controls heartatk houssiz bmi diabetes i.agegrp black female    
    qui ologit hlthstat $controls [pweight=finalwgt]
    eststo m1: margins , dydx(*) predict(outcome(1))
    qui ologit hlthstat $controls [pweight=finalwgt]
    eststo m2: margins , dydx(*) predict(outcome(2))
    qui ologit hlthstat $controls [pweight=finalwgt]
    eststo m3: margins , dydx(*) predict(outcome(3))
    qui ologit hlthstat $controls [pweight=finalwgt]
    eststo m4: margins , dydx(*) predict(outcome(4))
    qui ologit hlthstat $controls [pweight=finalwgt]
    eststo m5: margins , dydx(*) predict(outcome(5))
    
    esttab m1 m2 m3 m4 m5 using "omargins.tex", replace b(3) se(3) ///
     keep($controls) star(* 0.10 ** 0.05 *** 0.01) ///
     label booktabs noobs nonotes collabels(none) compress alignment(D{.}{.}{-1}) ///
     mtitle("Excellent" "V.Good" "Good" "Fair" "Poor") nobaselevels stats(N)
    Example of the data:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(hlthstat heartatk houssiz) float bmi byte(diabetes agegrp black female)
    2 0 4 20.495686 0 4 0 0
    2 0 6 21.022337 0 3 0 1
    3 0 6  24.97386 0 1 0 1
    4 0 9  35.72872 1 5 0 1
    2 0 3 27.923803 0 5 0 1
    5 0 1 30.502144 0 5 0 1
    2 0 2  23.03185 0 5 0 1
    1 0 1 23.035156 0 4 0 1
    2 0 3  25.63578 0 5 0 1
    5 0 4  20.97999 0 5 1 0
    2 0 1 31.465527 0 6 0 1
    2 0 3 20.184334 0 5 1 1
    3 0 4  28.54312 0 2 0 1
    4 0 2   20.4144 0 1 0 1
    1 0 4   24.9466 0 2 1 0
    5 0 1 23.976437 1 5 1 0
    5 0 7  38.44014 1 5 0 1
    1 0 1 25.925926 0 6 0 1
    4 0 1  20.86453 0 6 0 1
    3 0 2 18.145163 0 5 0 0
    2 0 2  29.09791 0 4 0 1
    5 0 3  38.68736 1 4 1 0
    1 0 2 21.926546 0 2 0 0
    3 0 2 34.226315 0 4 0 1
    4 0 2 26.902664 0 5 0 0
    1 0 4  23.07461 0 2 0 0
    3 0 1  24.33593 0 5 0 1
    3 0 2  27.91223 0 4 0 1
    3 0 3  26.32063 0 1 1 1
    3 0 2  32.59102 0 5 0 0
    4 0 2 25.753584 0 5 0 0
    1 0 7  30.10884 0 2 1 1
    1 0 1 21.084726 0 1 0 1
    3 0 3   27.6745 0 5 0 0
    4 0 1  22.43288 0 4 0 0
    3 0 2  26.54097 0 6 0 1
    2 0 4  20.00867 0 1 0 1
    2 0 6 25.502905 0 1 1 1
    3 0 2  26.94583 0 5 0 1
    4 0 2  24.40318 0 5 0 0
    2 0 3 24.241745 0 6 0 0
    1 0 1  24.19344 0 1 0 0
    3 0 8  28.98996 0 1 1 0
    4 0 1  21.93411 0 6 0 1
    3 0 1  28.57907 0 6 0 1
    3 1 2 30.112375 0 5 0 0
    3 0 2 20.318064 0 4 0 0
    1 0 1 21.385134 0 3 0 0
    5 0 2  35.72315 0 5 1 1
    3 0 2 21.161545 0 2 0 0
    3 0 3 22.207384 0 3 0 1
    3 0 4 19.039673 0 2 0 0
    1 0 3   26.5538 0 1 0 0
    2 0 4  23.75451 0 1 0 0
    2 0 3 28.274496 0 5 0 1
    1 0 4 22.546875 0 2 0 1
    3 0 5 34.772602 0 3 1 0
    2 0 2  19.61235 0 5 0 0
    2 0 2  30.13859 0 5 1 0
    2 0 2 31.379545 0 5 0 1
    1 0 1  20.84969 0 1 0 1
    3 0 3   21.9425 0 3 0 0
    3 0 1 20.241163 0 1 1 0
    4 0 8  35.12988 0 4 1 1
    3 0 2  28.21959 0 6 0 0
    1 0 3 24.421795 0 4 0 0
    2 0 4 21.581364 0 1 1 0
    4 0 4 37.740726 1 6 0 1
    3 0 2  30.77501 0 5 1 0
    5 0 5 21.030283 0 6 0 1
    4 1 2  25.53069 0 6 0 0
    5 0 1  25.24272 0 6 0 0
    2 0 2 34.823967 1 5 0 1
    4 0 6 33.614716 0 5 1 1
    3 0 2  27.63608 0 6 0 0
    1 0 3 18.712408 0 1 1 0
    3 0 2 23.012417 0 3 0 0
    1 0 5  27.02887 0 3 0 0
    4 0 2 26.086987 0 5 0 0
    1 0 1  30.42313 0 1 0 1
    2 1 2  25.67467 0 6 0 1
    2 0 2  29.43503 0 5 0 1
    2 0 6 25.418964 0 3 0 0
    1 0 6 25.101645 0 4 0 1
    3 0 4 20.557127 0 3 0 1
    4 0 1  26.41101 1 6 1 0
    3 0 2 26.817894 0 6 0 1
    1 0 1 27.240175 0 3 0 1
    4 0 4 36.435555 0 3 0 1
    2 0 2  28.42347 0 2 1 1
    3 0 3 28.850655 0 4 1 1
    1 0 2  37.04592 0 4 0 1
    3 1 1 25.452404 0 4 0 1
    1 0 2 17.432068 0 6 0 0
    3 0 1   31.6263 1 5 1 1
    1 0 2  44.01949 0 4 0 1
    2 0 2 18.321362 0 5 0 0
    1 0 3  21.32432 1 1 0 1
    5 0 1  18.99932 0 6 0 1
    4 0 4  28.93709 1 5 0 0
    end
    label values hlthstat hlth
    label def hlth 1 "Excellent", modify
    label def hlth 2 "Very good", modify
    label def hlth 3 "Good", modify
    label def hlth 4 "Fair", modify
    label def hlth 5 "Poor", modify
    label values heartatk heartlbl
    label def heartlbl 0 "No heart attack", modify
    label def heartlbl 1 "Had heart attack", modify
    label values diabetes diabetes
    label def diabetes 0 "Not diabetic", modify
    label def diabetes 1 "Diabetic", modify
    label values agegrp agegrp
    label def agegrp 1 "20–29", modify
    label def agegrp 2 "30–39", modify
    label def agegrp 3 "40–49", modify
    label def agegrp 4 "50–59", modify
    label def agegrp 5 "60–69", modify
    label def agegrp 6 "70+", modify
    label values black black
    label def black 0 "Not Black", modify
    label def black 1 "Black", modify
    label values female female
    label def female 0 "Male", modify
    label def female 1 "Female", modify

  • #2
    Here is one way of doing it with the etable and collect command. This requires Stata 17, but since you did not mention which version of Stata you are using, we know that you are using Stata 17. (The default assumption on Statalist is that you have the latest version of Stata, unless you tell us otherwise)

    Code:
    . // estimate the model and marginal effects (note the post option for margins)
    . global controls heartatk houssiz bmi diabetes i.agegrp black female    
    
    . qui ologit hlthstat $controls
    
    . margins , dydx(*) post
    
    Average marginal effects                                   Number of obs = 100
    Model VCE: OIM
    
    dy/dx wrt: heartatk houssiz bmi diabetes 2.agegrp 3.agegrp 4.agegrp 5.agegrp 6.agegrp black female
    
    1._predict: Pr(hlthstat==1), predict(pr outcome(1))
    2._predict: Pr(hlthstat==2), predict(pr outcome(2))
    3._predict: Pr(hlthstat==3), predict(pr outcome(3))
    4._predict: Pr(hlthstat==4), predict(pr outcome(4))
    5._predict: Pr(hlthstat==5), predict(pr outcome(5))
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]
    -------------+----------------------------------------------------------------
    heartatk     |
        _predict |
              1  |  -.0528757   .1292074    -0.41   0.682    -.3061176    .2003663
              2  |  -.0177194   .0434391    -0.41   0.683    -.1028584    .0674196
              3  |   .0138805   .0349349     0.40   0.691    -.0545906    .0823517
              4  |   .0304151   .0740619     0.41   0.681    -.1147436    .1755737
              5  |   .0262995   .0644634     0.41   0.683    -.1000465    .1526455
    -------------+----------------------------------------------------------------
    houssiz      |
        _predict |
              1  |  -.0241479   .0166315    -1.45   0.147    -.0567451    .0084493
              2  |  -.0080923   .0058622    -1.38   0.167    -.0195821    .0033974
              3  |   .0063391   .0050924     1.24   0.213    -.0036419    .0163201
              4  |   .0138903   .0099184     1.40   0.161    -.0055495    .0333301
              5  |   .0120108   .0087535     1.37   0.170    -.0051458    .0291673
    -------------+----------------------------------------------------------------
    bmi          |
        _predict |
              1  |   .0012805   .0064033     0.20   0.841    -.0112696    .0138307
              2  |   .0004291   .0021525     0.20   0.842    -.0037897     .004648
              3  |  -.0003362   .0016755    -0.20   0.841      -.00362    .0029477
              4  |  -.0007366   .0036951    -0.20   0.842    -.0079788    .0065056
              5  |  -.0006369    .003195    -0.20   0.842    -.0068991    .0056252
    -------------+----------------------------------------------------------------
    diabetes     |
        _predict |
              1  |  -.1536617   .1011835    -1.52   0.129    -.3519777    .0446544
              2  |  -.0514943   .0358122    -1.44   0.150    -.1216849    .0186964
              3  |   .0403381    .036976     1.09   0.275    -.0321334    .1128097
              4  |    .088389    .057326     1.54   0.123    -.0239679    .2007459
              5  |   .0764288     .04964     1.54   0.124    -.0208638    .1737214
    -------------+----------------------------------------------------------------
    1.agegrp     |  (base outcome)
    -------------+----------------------------------------------------------------
    2.agegrp     |
        _predict |
              1  |   .0565957   .1880645     0.30   0.763     -.312004    .4251954
              2  |  -.0172235    .059923    -0.29   0.774    -.1346703    .1002234
              3  |  -.0255551   .0842112    -0.30   0.762    -.1906061    .1394959
              4  |  -.0101145   .0328487    -0.31   0.758    -.0744968    .0542678
              5  |  -.0037026   .0120408    -0.31   0.758    -.0273022    .0198969
    -------------+----------------------------------------------------------------
    3.agegrp     |
        _predict |
              1  |   -.234376   .1502245    -1.56   0.119    -.5288106    .0600586
              2  |   .0128061   .0404654     0.32   0.752    -.0665046    .0921167
              3  |   .1186274   .0756151     1.57   0.117    -.0295756    .2668303
              4  |   .0712504   .0538957     1.32   0.186    -.0343831     .176884
              5  |   .0316922   .0284412     1.11   0.265    -.0240516    .0874359
    -------------+----------------------------------------------------------------
    4.agegrp     |
        _predict |
              1  |  -.2758463   .1433084    -1.92   0.054    -.5567255    .0050329
              2  |   .0000508   .0442305     0.00   0.999    -.0866394     .086741
              3  |   .1394357   .0709684     1.96   0.049     .0003402    .2785311
              4  |   .0927441   .0557569     1.66   0.096    -.0165375    .2020256
              5  |   .0436158   .0315643     1.38   0.167    -.0182491    .1054806
    -------------+----------------------------------------------------------------
    5.agegrp     |
        _predict |
              1  |  -.3881318   .1208778    -3.21   0.001    -.6250479   -.1512157
              2  |     -.0768   .0527458    -1.46   0.145    -.1801798    .0265799
              3  |   .1707594    .062019     2.75   0.006     .0492043    .2923144
              4  |   .1820166   .0557233     3.27   0.001      .072801    .2912322
              5  |   .1121558   .0442339     2.54   0.011      .025459    .1988527
    -------------+----------------------------------------------------------------
    6.agegrp     |
        _predict |
              1  |  -.4250665   .1222928    -3.48   0.001    -.6647561    -.185377
              2  |  -.1224547   .0609017    -2.01   0.044    -.2418199   -.0030896
              3  |   .1517725   .0662054     2.29   0.022     .0220124    .2815326
              4  |   .2268306    .066288     3.42   0.001     .0969086    .3567527
              5  |   .1689181   .0740365     2.28   0.023     .0238093    .3140269
    -------------+----------------------------------------------------------------
    black        |
        _predict |
              1  |  -.1374816   .0696338    -1.97   0.048    -.2739613   -.0010019
              2  |  -.0460721   .0265301    -1.74   0.082    -.0980701    .0059259
              3  |   .0360907   .0246947     1.46   0.144    -.0123101    .0844915
              4  |    .079082    .041495     1.91   0.057    -.0022468    .1604107
              5  |   .0683811    .039152     1.75   0.081    -.0083553    .1451176
    -------------+----------------------------------------------------------------
    female       |
        _predict |
              1  |   .0418097   .0571218     0.73   0.464     -.070147    .1537664
              2  |    .014011   .0193569     0.72   0.469    -.0239277    .0519498
              3  |  -.0109756   .0158667    -0.69   0.489    -.0420736    .0201225
              4  |  -.0240497   .0330139    -0.73   0.466    -.0887558    .0406564
              5  |  -.0207955   .0287448    -0.72   0.469    -.0771342    .0355432
    ------------------------------------------------------------------------------
    Note: dy/dx for factor levels is the discrete change from the base level.
    
    .
    . // creat a table
    . etable, showeq showstars showstarsnote ///
    >     cstat(_r_b, nformat(%9.3f))        ///
    >     cstat(_r_se, nformat(%9.3f))
    
    ---------------------------------
                                1    
    ---------------------------------
    heartatk                         
      _predict                       
        1                   -0.053   
                           (0.129)   
        2                   -0.018   
                           (0.043)   
        3                    0.014   
                           (0.035)   
        4                    0.030   
                           (0.074)   
        5                    0.026   
                           (0.064)   
    houssiz                          
      _predict                       
        1                   -0.024   
                           (0.017)   
        2                   -0.008   
                           (0.006)   
        3                    0.006   
                           (0.005)   
        4                    0.014   
                           (0.010)   
        5                    0.012   
                           (0.009)   
    bmi                              
      _predict                       
        1                    0.001   
                           (0.006)   
        2                    0.000   
                           (0.002)   
        3                   -0.000   
                           (0.002)   
        4                   -0.001   
                           (0.004)   
        5                   -0.001   
                           (0.003)   
    diabetes                         
      _predict                       
        1                   -0.154   
                           (0.101)   
        2                   -0.051   
                           (0.036)   
        3                    0.040   
                           (0.037)   
        4                    0.088   
                           (0.057)   
        5                    0.076   
                           (0.050)   
    agegrp                           
      2                              
      _predict                       
        1                    0.057   
                           (0.188)   
        2                   -0.017   
                           (0.060)   
        3                   -0.026   
                           (0.084)   
        4                   -0.010   
                           (0.033)   
        5                   -0.004   
                           (0.012)   
      3                              
        1                   -0.234   
                           (0.150)   
        2                    0.013   
                           (0.040)   
        3                    0.119   
                           (0.076)   
        4                    0.071   
                           (0.054)   
        5                    0.032   
                           (0.028)   
      4                              
        1                   -0.276   
                           (0.143)   
        2                    0.000   
                           (0.044)   
        3                    0.139 *
                           (0.071)   
        4                    0.093   
                           (0.056)   
        5                    0.044   
                           (0.032)   
      5                              
        1                   -0.388 **
                           (0.121)   
        2                   -0.077   
                           (0.053)   
        3                    0.171 **
                           (0.062)   
        4                    0.182 **
                           (0.056)   
        5                    0.112 *
                           (0.044)   
      6                              
        1                   -0.425 **
                           (0.122)   
        2                   -0.122 *
                           (0.061)   
        3                    0.152 *
                           (0.066)   
        4                    0.227 **
                           (0.066)   
        5                    0.169 *
                           (0.074)   
    black                            
      _predict                       
        1                   -0.137 *
                           (0.070)   
        2                   -0.046   
                           (0.027)   
        3                    0.036   
                           (0.025)   
        4                    0.079   
                           (0.041)   
        5                    0.068   
                           (0.039)   
    female                           
      _predict                       
        1                    0.042   
                           (0.057)   
        2                    0.014   
                           (0.019)   
        3                   -0.011   
                           (0.016)   
        4                   -0.024   
                           (0.033)   
        5                   -0.021   
                           (0.029)   
    Number of observations     100   
    ---------------------------------
    ** p<.01, * p<.05
    
    .
    . // reorder such that the predictions are the columns
    . collect layout (coleq#result[_r_b _r_se]) ///
    >                 (colname[]#stars)
    
    Collection: ETable
          Rows: coleq#result[_r_b _r_se]
       Columns: colname[]#stars
       Table 1: 23 x 10
    
    -------------------------------------------------------------
                                   _predict                      
                  1         2          3          4         5    
    -------------------------------------------------------------
    heartatk  -0.053     -0.018     0.014      0.030      0.026  
             (0.129)    (0.043)   (0.035)    (0.074)    (0.064)  
    houssiz   -0.024     -0.008     0.006      0.014      0.012  
             (0.017)    (0.006)   (0.005)    (0.010)    (0.009)  
    bmi        0.001      0.000    -0.000     -0.001     -0.001  
             (0.006)    (0.002)   (0.002)    (0.004)    (0.003)  
    diabetes  -0.154     -0.051     0.040      0.088      0.076  
             (0.101)    (0.036)   (0.037)    (0.057)    (0.050)  
    agegrp                                                       
      2        0.057     -0.017    -0.026     -0.010     -0.004  
             (0.188)    (0.060)   (0.084)    (0.033)    (0.012)  
      3       -0.234      0.013     0.119      0.071      0.032  
             (0.150)    (0.040)   (0.076)    (0.054)    (0.028)  
      4       -0.276      0.000     0.139 *    0.093      0.044  
             (0.143)    (0.044)   (0.071)    (0.056)    (0.032)  
      5       -0.388 **  -0.077     0.171 **   0.182 **   0.112 *
             (0.121)    (0.053)   (0.062)    (0.056)    (0.044)  
      6       -0.425 **  -0.122 *   0.152 *    0.227 **   0.169 *
             (0.122)    (0.061)   (0.066)    (0.066)    (0.074)  
    black     -0.137 *   -0.046     0.036      0.079      0.068  
             (0.070)    (0.027)   (0.025)    (0.041)    (0.039)  
    female     0.042      0.014    -0.011     -0.024     -0.021  
             (0.057)    (0.019)   (0.016)    (0.033)    (0.029)  
    -------------------------------------------------------------
    ** p<.01, * p<.05
    
    .
    . // give age and predictions labels                
    . collect label levels coleq    ///
    >     2.agegrp "30-39"          ///
    >     3.agegrp "40-49"          ///
    >     4.agegrp "50-59"          ///
    >     5.agegrp "60-69"          ///
    >     6.agegrp "70+"
    
    .     
    . collect label levels colname  ///
    >     1._predict "Excellent"    ///
    >     2._predict "V.Good"       ///
    >     3._predict "Good"         ///
    >     4._predict "Fair"         ///
    >     5._predict "Poor"             
    
    .
    . // admire the result    
    . collect preview
    
    -------------------------------------------------------------
              Excellent   V.Good     Good       Fair       Poor  
                                                                 
    -------------------------------------------------------------
    heartatk  -0.053     -0.018     0.014      0.030      0.026  
             (0.129)    (0.043)   (0.035)    (0.074)    (0.064)  
    houssiz   -0.024     -0.008     0.006      0.014      0.012  
             (0.017)    (0.006)   (0.005)    (0.010)    (0.009)  
    bmi        0.001      0.000    -0.000     -0.001     -0.001  
             (0.006)    (0.002)   (0.002)    (0.004)    (0.003)  
    diabetes  -0.154     -0.051     0.040      0.088      0.076  
             (0.101)    (0.036)   (0.037)    (0.057)    (0.050)  
    30-39      0.057     -0.017    -0.026     -0.010     -0.004  
             (0.188)    (0.060)   (0.084)    (0.033)    (0.012)  
    40-49     -0.234      0.013     0.119      0.071      0.032  
             (0.150)    (0.040)   (0.076)    (0.054)    (0.028)  
    50-59     -0.276      0.000     0.139 *    0.093      0.044  
             (0.143)    (0.044)   (0.071)    (0.056)    (0.032)  
    60-69     -0.388 **  -0.077     0.171 **   0.182 **   0.112 *
             (0.121)    (0.053)   (0.062)    (0.056)    (0.044)  
    70+       -0.425 **  -0.122 *   0.152 *    0.227 **   0.169 *
             (0.122)    (0.061)   (0.066)    (0.066)    (0.074)  
    black     -0.137 *   -0.046     0.036      0.079      0.068  
             (0.070)    (0.027)   (0.025)    (0.041)    (0.039)  
    female     0.042      0.014    -0.011     -0.024     -0.021  
             (0.057)    (0.019)   (0.016)    (0.033)    (0.029)  
    -------------------------------------------------------------
    ** p<.01, * p<.05
    
    .
    . // export to LaTeX
    . // (I like it without \begin{table} so I can add \label{})
    . collect style tex , nobegintable
    
    . collect export c:\temp\foo.tex, replace tableonly
    (collection ETable exported to file c:\temp\foo.tex)
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment

    Working...
    X