Announcement

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

  • mimrgns doesnt give the var distribution with at (var=()) option when used wiith dydx(var)

    Hello all,

    I am trying to create a marginsplot with coefficients using mimrgs. However, mimrgns doesn't give the var distribution with the at (var=()) option when used with dydx(var). I use following command:
    Code:
    mimrgns, dydx(var) at(var=(0.5(0.5)2.5))
    I tried this command using margins and it works fine. My outcome variable is also continuous

    Thank you in advance. Looking forward to suggestions.

    Best regards,
    Rajini

  • #2
    I don't understand the problem. With mimrgns from SSC, I get
    Code:
    . which mimrgns
    c:\ado\plus\m\mimrgns.ado
    *! version 4.0.7 24jul2022 daniel klein
    
    . webuse mheart1s20 , clear
    (Fictional heart attack data; BMI missing)
    
    . mi estimate : regress bmi age i.smoke
    
    Multiple-imputation estimates                   Imputations       =         20
    Linear regression                               Number of obs     =        154
                                                    Average RVI       =     0.1519
                                                    Largest FMI       =     0.1489
                                                    Complete DF       =        151
    DF adjustment:   Small sample                   DF:     min       =     111.65
                                                            avg       =     116.44
                                                            max       =     125.95
    Model F test:       Equal FMI                   F(   2,  138.3)   =       0.35
    Within VCE type:          OLS                   Prob > F          =     0.7023
    
    ------------------------------------------------------------------------------
             bmi | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
             age |  -.0253014   .0306686    -0.82   0.411     -.086069    .0354662
        1.smokes |   .0890813   .7083644     0.13   0.900    -1.312757    1.490919
           _cons |   26.63684   1.783875    14.93   0.000     23.10221    30.17148
    ------------------------------------------------------------------------------
    
    . mimrgns , dydx(age) at(age = (42 73))
    note: option predict() not specified; predict(xb) assumed
    
    Multiple-imputation estimates                   Imputations       =         20
    Average marginal effects                        Number of obs     =        154
                                                    Average RVI       =     0.1694
                                                    Largest FMI       =     0.1487
                                                    Complete DF       =        151
    DF adjustment:   Small sample                   DF:     min       =     111.71
                                                            avg       =     111.71
    Within VCE type: Delta-method                           max       =     111.71
    
    Expression   : Linear prediction, predict(xb)
    dy/dx w.r.t. : age
    
    1._at        : age             =          42
    
    2._at        : age             =          73
    
    ------------------------------------------------------------------------------
                 |      dy/dx   Std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
             _at |
              1  |  -.0253014   .0306686    -0.82   0.411     -.086069    .0354662
              2  |  -.0253014   .0306686    -0.82   0.411     -.086069    .0354662
    ------------------------------------------------------------------------------
    Obviously, in a linear model without interactions, marginal effects, dydx(), are constant across values.

    Perhaps you seek the cmdmargins options? Read the help file for more information or provide a reproducible example of the problem you're trying to solve.

    Comment


    • #3
      Dear Daniel,

      Thank you very much for your reply, and apologies for the lack of clarity in my initial query.

      I've now prepared a reproducible example based on the same dataset you used above. Since I am using restricted cubic splines, I am also using the f_able command from SSC. For comparison purposes, I first ran the analysis on the original dataset using the margins command, and then repeated the same analysis on the imputed dataset using mimrgns. With margins, I get different values with dy/dx, but that's not the case with mimrgns. I'm wondering if I am missing a step or misapplying the commands in the imputed dataset.

      I've included the code and output below for reference.

      Thank you again for your time and help.

      Best regards,
      Rajini

      Code:
      .
      . webuse mheart1s20 , clear
      (Fictional heart attack data; BMI missing)
      
      . 
      . *Creating splines
      . f_rcspline fbmi = bmi, nknots(3)
      
      . 
      . *Linear regression
      . *using margins
      . regress age bmi fbmi2 if _mi_m==0
      
            Source |       SS           df       MS      Number of obs   =       132
      -------------+----------------------------------   F(2, 129)       =      0.35
             Model |  96.5284316         2  48.2642158   Prob > F        =    0.7022
          Residual |  17562.6644       129  136.144685   R-squared       =    0.0055
      -------------+----------------------------------   Adj R-squared   =   -0.0100
             Total |  17659.1928       131  134.802998   Root MSE        =    11.668
      
      ------------------------------------------------------------------------------
               age | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
      -------------+----------------------------------------------------------------
               bmi |  -.3026815   .6598412    -0.46   0.6472    -1.608193     1.00283
             fbmi2 |   .1176063   .7700629     0.15   0.8789    -1.405982    1.641195
             _cons |    64.0834   14.90349     4.30   0.0000     34.59647    93.57033
      ------------------------------------------------------------------------------
      
      . f_able fbmi2, auto
      This is an experimental feature in f_able
      Using this option, you do not need to add nochain or numerical options in margins
      All variables in -nlvar- have been declared
      
      . margins, dydx(bmi) at(bmi=(20(5)30))
      
      Conditional marginal effects                               Number of obs = 132
      Model VCE: OLS
      
      Expression: Fitted values, predict()
      dy/dx wrt:  bmi
      1._at: bmi = 20
      2._at: bmi = 25
      3._at: bmi = 30
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |      dy/dx   std. err.      z    P>|z|     [95% conf. interval]
      -------------+----------------------------------------------------------------
      bmi          |
               _at |
                1  |  -.3026737   .6597937    -0.46   0.6464    -1.595845    .9904982
                2  |  -.2284257   .2815052    -0.81   0.4171    -.7801658    .3233144
                3  |  -.1395786   .5238409    -0.27   0.7899    -1.166288    .8871308
      ------------------------------------------------------------------------------
      
      . 
      . *using mimrgns
      . mi estimate, post: regress age bmi fbmi2
      
      Multiple-imputation estimates                   Imputations       =         20
      Linear regression                               Number of obs     =        154
                                                      Average RVI       =     0.1267
                                                      Largest FMI       =     0.1737
                                                      Complete DF       =        151
      DF adjustment:   Small sample                   DF:     min       =     104.42
                                                              avg       =     108.85
                                                              max       =     111.57
      Model F test:       Equal FMI                   F(   2,  131.5)   =       0.34
      Within VCE type:          OLS                   Prob > F          =     0.7147
      
      ------------------------------------------------------------------------------
               age | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
      -------------+----------------------------------------------------------------
               bmi |  -.2676577   .6387854    -0.42   0.6760     -1.53351    .9981945
             fbmi2 |   .0780632   .7607827     0.10   0.9185    -1.430527    1.586653
             _cons |   63.05682   14.38345     4.38   0.0000     34.55666    91.55698
      ------------------------------------------------------------------------------
      
      . f_able fbmi2, auto
      This is an experimental feature in f_able
      Using this option, you do not need to add nochain or numerical options in margins
      All variables in -nlvar- have been declared
      
      . mimrgns, dydx(bmi) at(bmi=(20(5)30)) predict(default)
      
      Multiple-imputation estimates                   Imputations       =         20
      Average marginal effects                        Number of obs     =        154
                                                      Average RVI       =     0.1747
                                                      Largest FMI       =     0.1527
                                                      Complete DF       =        151
      DF adjustment:   Small sample                   DF:     min       =     110.55
                                                              avg       =     110.55
      Within VCE type: Delta-method                           max       =     110.55
      
      Expression   : Linear prediction, predict()
      dy/dx w.r.t. : bmi
      
      1._at        : bmi             =          20
      
      2._at        : bmi             =          25
      
      3._at        : bmi             =          30
      
      ------------------------------------------------------------------------------
                   |      dy/dx   Std. err.      t    P>|t|     [95% conf. interval]
      -------------+----------------------------------------------------------------
               _at |
                1  |  -.2676577   .6387854    -0.42   0.6760     -1.53351    .9981945
                2  |  -.2676577   .6387854    -0.42   0.6760     -1.53351    .9981945
                3  |  -.2676577   .6387854    -0.42   0.6760     -1.53351    .9981945
      ------------------------------------------------------------------------------

      Comment


      • #4
        Thanks for the reproducible example.

        So you're using community-contributed f_able (from SSC or SJ or GitHub) and try to combine it with community-contributed mimrgns (from SSC or GitHub). The reason your approach fails is that f_able manipulates e() results that mimrgns never uses. Both commands interact on a low level with official Stata's internal machinery, so use them at your own risk. Having said that, here's a workaround:
        Code:
        webuse mheart1s20 , clear
        
        // we need mi data in -flong- style
        mi convert flong
        
        *Creating splines
        // not so sure this should be done in the pooled dataset, but there you go
        f_rcspline fbmi = bmi, nknots(3)
        
        *Linear regression
        // save individual estimation results from -mi-
        tempfile miest
        mi estimate , saving("`miest'") esample(esample) : regress age bmi fbmi2
        
        // now run -f_able- on the individual estimation results
        mi query
        local M = r(M)
        
        tempfile miest_f_able
        forvalues m = 1/`M' {
            estimates use "`miest'" , number(`m')
            f_able fbmi2 , auto
            estimates save "`miest_f_able'" , `append'
            local append append
        }
        estimates use "`miest'" , number(`=`M'+1')
        estimates save "`miest_f_able'" , append
        
        // run -mimrgns- using the manipulated individual estimation results
        mimrgns using "`miest_f_able'" , esample(esample) dydx(bmi) at(bmi=(20(5)30))
        The above yields
        Code:
        . webuse mheart1s20 , clear
        (Fictional heart attack data; BMI missing)
        
        .
        . // we need mi data in -flong- style
        . mi convert flong
        (output omitted)
        // now run -f_able- on the individual estimation results
        . mi query
        (output omitted)
        . local M = r(M)
        
        .
        . tempfile miest_f_able
        
        . forvalues m = 1/`M' {
          2.     estimates use "`miest'" , number(`m')
          3.     f_able fbmi2 , auto
          4.     estimates save "`miest_f_able'" , `append'
          5.     local append append
          6. }
        This is an experimental feature in f_able
        Using this option, you do not need to add nochain or numerical options in margins
        All variables in -nlvar- have been declared
        file C:\Users\***.tmp saved
        This is an experimental feature in f_able
        Using this option, you do not need to add nochain or numerical options in margins
        All variables in -nlvar- have been declared
        file C:\Users\***.tmp saved
        (output omitted)
        This is an experimental feature in f_able
        Using this option, you do not need to add nochain or numerical options in margins
        All variables in -nlvar- have been declared
        file C:\Users\***.tmp saved
        
        . estimates use "`miest'" , number(`=`M'+1')
        
        . estimates save "`miest_f_able'" , append
        file C:\Users\***.tmp saved
        
        .
        . // run -mimrgns- using the manipulated individual estimation results
        . mimrgns using "`miest_f_able'" , esample(esample) dydx(bmi) at(bmi=(20(5)30))
        note: option predict() not specified; predict(xb) assumed
        
        Multiple-imputation estimates                   Imputations       =         20
        Conditional marginal effects                    Number of obs     =        154
                                                        Average RVI       =     0.1889
                                                        Largest FMI       =     0.1890
                                                        Complete DF       =        151
        DF adjustment:   Small sample                   DF:     min       =     100.03
                                                                avg       =     108.56
        Within VCE type: Delta-method                           max       =     114.79
        
        Expression   : Linear prediction, predict(xb)
        dy/dx w.r.t. : bmi
        
        1._at        : bmi             =          20
        
        2._at        : bmi             =          25
        
        3._at        : bmi             =          30
        
        ------------------------------------------------------------------------------
                     |      dy/dx   Std. err.      t    P>|t|     [95% conf. interval]
        -------------+----------------------------------------------------------------
                 _at |
                  1  |  -.2654217   .6295405    -0.42   0.674    -1.512916    .9820727
                  2  |   -.216549   .2680169    -0.81   0.421    -.7474494    .3143515
                  3  |  -.1608407    .528003    -0.30   0.761    -1.208379    .8866981
        ------------------------------------------------------------------------------

        Comment


        • #5
          Here's another workaround that creates the splines for each imputed dataset separately:
          Code:
          webuse mheart1s20 , clear
          
          capture program drop mi_f_able_margins
          program mi_f_able_margins , eclass properties(mi)
              f_rcspline fbmi = bmi, nknots(3)
              regress age bmi fbmi2
              f_able fbmi2 , auto
              margins , dydx(bmi) at(bmi=(20(5)30)) post    
          end
          
          mi estimate : mi_f_able_margins MI_EXPECTS_AN_ARGUMENT_HERE
          For the example above, this yields
          Code:
          . mi estimate : mi_f_able_margins MI_EXPECTS_AN_ARGUMENT_HERE
          
          Multiple-imputation estimates                   Imputations       =         20
          Conditional marginal effects                    Number of obs     =        154
                                                          Average RVI       =     0.2002
                                                          Largest FMI       =     0.1879
          DF adjustment:   Large sample                   DF:     min       =     555.47
                                                                  avg       =     822.20
          Within VCE type: Delta-method                           max       =   1,018.52
          
          ------------------------------------------------------------------------------
                       | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                   _at |
                    1  |  -.2609058   .6251891    -0.42   0.677    -1.487918    .9661061
                    2  |  -.2162253   .2707707    -0.80   0.425    -.7475574    .3151068
                    3  |  -.1635914   .5303295    -0.31   0.758    -1.205288     .878105
          ------------------------------------------------------------------------------

          Comment


          • #6
            Dear Daniel,

            Thank you so much again. I tried your first solution and it worked perfectly. I truly appreciate your generous support on this forum.

            If I may ask a follow-up (and please feel free to disregard this if it’s too much to ask): I’ve now moved on to interpreting the results, and I’m a bit puzzled.

            I plotted two graphs: Graph1 : Predictive margins (using the expression(exp(predict(xb)) option since my outcome was log-transformed),
            Graph 2: Average marginal effects.

            The plots show quite different patterns. I understand that the y-axes are different in the two plots, but the interpretation seems to change quite drastically. In the first plot, it looks like the outcome increases with exposure, while in the second, the association appears inverse.

            I would be very grateful for any thoughts you might have

            Best regards,
            Rajini
            Code:
            . 
            . 
            . f_rcspline flag_abc_  = lag_abc_ , nknots(3)
            
            
            . *random intercept model
            
            . 
            . mi estimate, saving("`miest'") esample(esample) post: mixed c.lnAdip_MSD_T lag_abc_  flag_abc_* c.Age i.sex i.Country i.isced_cat c.AVM_1_w
            > eek_T c.PHYS_ACTIV_T i.season i.migration_T   || ID_cohort: , cov(unstr) iterate(100)
            
            Multiple-imputation estimates                   Imputations       =         25
            Mixed-effects ML regression                     Number of obs     =      3,309
            
            Group variable: ID_cohort                       Number of groups  =      3,007
                                                            Obs per group:
                                                                          min =          1
                                                                          avg =        1.1
                                                                          max =          2
                                                            Average RVI       =     0.0172
                                                            Largest FMI       =     0.1575
            DF adjustment:   Large sample                   DF:     min       =     988.77
                                                                    avg       =   3.20e+07
                                                                    max       =   3.05e+08
            Model F test:       Equal FMI                   F(  17, 1.0e+06)  =      13.76
                                                            Prob > F          =     0.0000
            
            -------------------------------------------------------------------------------
             lnAdip_MSD_T | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
            --------------+----------------------------------------------------------------
                 lag_abc_ |   .5720349   .1337852     4.28   0.0000     .3098207    .8342491
               flag_abc_2 |  -.9536499   .1296851    -7.35   0.0000    -1.207828   -.6994719
                      Age |  -.0212681   .0046757    -4.55   0.0000    -.0304323   -.0121038
                    2.sex |   .0952713   .0258109     3.69   0.0002     .0446828    .1458598
                          |
                  Country |
                       7  |  -.4497822   .0696642    -6.46   0.0000    -.5863216   -.3132428
                       9  |  -.1625098   .1024189    -1.59   0.1126    -.3632473    .0382276
                       7  |  -.3639797    .067258    -5.41   0.0000     -.495803   -.2321564
                       8  |  -.3540813   .0562778    -6.29   0.0000    -.4643838   -.2437788
                       9  |  -.1616511   .1059627    -1.53   0.1271    -.3693343     .046032
                          |
                isced_cat |
                       2  |   .0004751   .0565088     0.01   0.9933    -.1102821    .1112324
                       3  |   .0664331   .0592081     1.12   0.2619    -.0496161    .1824822
                          |
             AVM_1_week_T |   -.003021   .0016176    -1.87   0.0620    -.0061933    .0001514
             PHYS_ACTIV_T |   .0012387   .0014629     0.85   0.3973     -.001632    .0041094
                          |
                   season |
                       2  |  -.0919777   .0328439    -2.80   0.0051    -.1563505   -.0276049
                       3  |  -.1409796   .0688422    -2.05   0.0406    -.2759078   -.0060514
                       4  |   .1745143   .0312191     5.59   0.0000      .113326    .2357027
                          |
            1.migration_T |  -.0443163   .0369121    -1.20   0.2299     -.116663    .0280305
                    _cons |   3.108953   .2020949    15.38   0.0000     2.712854    3.505053
            -------------------------------------------------------------------------------
            
            ------------------------------------------------------------------------------
              Random-effects parameters  |   Estimate   Std. err.     [95% conf. interval]
            -----------------------------+------------------------------------------------
            ID_cohort: Identity          |
                               sd(_cons) |   .3448908   .0468274       .264308     .450042
            -----------------------------+------------------------------------------------
                            sd(Residual) |   .6218511   .0258588       .573179    .6746561
            ------------------------------------------------------------------------------
            
            . 
            . f_able flag_abc_2 , auto
            This is an experimental feature in f_able
            Using this option, you do not need to add nochain or numerical options in margins
            All variables in -nlvar- have been declared
            
            . 
            . mimrgns, at(lag_abc_  =(0.5(0.5)2.5)) expression(exp(predict(xb))) cmdmargins  
            
            Multiple-imputation estimates                   Imputations       =         25
            Predictive margins                              Number of obs     =      3,309
                                                            Average RVI       =     0.0001
                                                            Largest FMI       =     0.0003
            DF adjustment:   Large sample                   DF:     min       =   1.97e+08
                                                                    avg       =   2.39e+08
            Within VCE type: Delta-method                           max       =   3.54e+08
            
            Expression   : exp(predict(xb))
            
            1._at        : lag_abc_        =          .5
            
            2._at        : lag_abc_        =           1
            
            3._at        : lag_abc_        =         1.5
            
            4._at        : lag_abc_        =           2
            
            5._at        : lag_abc_        =         2.5
            
            ------------------------------------------------------------------------------
                         |     Margin   Std. err.      t    P>|t|     [95% conf. interval]
            -------------+----------------------------------------------------------------
                     _at |
                      1  |   16.16735   1.651088     9.79   0.0000     12.93128    19.40342
                      2  |   21.52059   .7920049    27.17   0.0000     19.96828    23.07289
                      3  |   28.64639   1.004105    28.53   0.0000     26.67838     30.6144
                      4  |   38.13171   3.823102     9.97   0.0000     30.63857    45.62485
                      5  |   50.75786   8.466855     5.99   0.0000     34.16313    67.35259
            ------------------------------------------------------------------------------
            
            . 
            . graph save "splinelnAdip_MSD_Tabc.gph", replace
            file splinelnAdip_MSD_Tabc.gph saved
            
            . 
            . 
            . 
            . mi query
            data mi set flong, M = 25
            last mi update 07aug2025 16:39:28, approximately 2 minutes ago
            
            . 
            . local M = r(M)
            
            . 
            . 
            . 
            . tempfile miest_f_able
            
            . 
            . forvalues m = 1/`M' {
              2. 
            .     estimates use "`miest'" , number(`m')
              3. 
            .     f_able flag_abc_2, auto
              4. 
            .     estimates save "`miest_f_able'" , `append'
              5. 
            .     local append append
              6. 
            . }
            This is an experimental feature in f_able
            Using this option, you do not need to add nochain or numerical options in margins
            All variables in -nlvar- have been declared
            file C:\Users\nagrani\AppData\Local\Temp\2156\ST_7340_000004.tmp saved
            This is an experimental feature in f_able
            estimates use "`miest'" , number(`=`M'+1')
            
            . 
            . estimates save "`miest_f_able'" , append
            file C:\Users\nagrani\AppData\Local\Temp\2156\ST_7340_000004.tmp saved
            
            . 
            . 
            . 
            . mimrgns using "`miest_f_able'" , esample(esample) dydx(lag_abc_ ) at(lag_abc_  =(0.5(0.5)2.5)) cmdmargins
            note: option predict() not specified; predict(xb) assumed
            
            Multiple-imputation estimates                   Imputations       =         25
            Average marginal effects                        Number of obs     =      3,309
                                                            Average RVI       =     0.0002
                                                            Largest FMI       =     0.0004
            DF adjustment:   Large sample                   DF:     min       =   1.68e+08
                                                                    avg       =   2.34e+08
            Within VCE type: Delta-method                           max       =   2.93e+08
            
            Expression   : Linear prediction, fixed portion, predict(xb)
            dy/dx w.r.t. : lag_abc_
            
            1._at        : lag_abc_        =          .5
            
            2._at        : lag_abc_        =           1
            
            3._at        : lag_abc_        =         1.5
            
            4._at        : lag_abc_        =           2
            
            5._at        : lag_abc_        =         2.5
            
            ------------------------------------------------------------------------------
                         |      dy/dx   Std. err.      t    P>|t|     [95% conf. interval]
            -------------+----------------------------------------------------------------
                     _at |
                      1  |   .5720349   .1337852     4.28   0.0000     .3098207    .8342491
                      2  |   .4462162   .1188695     3.75   0.0002     .2132363    .6791962
                      3  |  -.3254107   .0618409    -5.26   0.0000    -.4466167   -.2042047
                      4  |  -.8027301   .0920529    -8.72   0.0000    -.9831504   -.6223097
                      5  |   -.813067   .0931002    -8.73   0.0000      -.99554    -.630594
            ------------------------------------------------------------------------------
            
            . 
            . marginsplot
            
            Variables that uniquely identify margins: lag_abc_
            Attached Files

            Comment

            Working...
            X