Announcement

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

  • Margins: difference between -margins var1#var2- and -margins var2, at(r(levels))-

    I came across a significant difference in computation time between the two lines -margins var1#var2- and -margins var2, at(r(levels)). I use age dummies instead of treating it as a continuous variable (-c.age-), as to plot the crude data.
    The full code is the following:
    HTML Code:
    use https://www.stata-press.com/data/r18/nhanes2, clear
    timer clear 
    timer on 1
    logit highbp sex##age // estimates dummies, at() and margins with interactions is equivalent
    margins age#sex // does not work with continuous age (after specifying -c.-) (but -at() also works in this case)
    marginsplot, name(g2, replace)
    timer off 1 
    gr combine g1 g2 
    timer list 1
    
        **can also compute age#sex using at(), but this is much slower, see comparison below**
        timer on 2 
        logit highbp sex##age // estimates dummies, at() and margins with interactions is equivalent    
        levelsof age
        margins sex, at(age=(`r(levels)')) 
        marginsplot, name(g3, replace)
        timer off 2 
        timer list 2
        gr combine g2 g3
    Why is there such a big difference in computation time? Is any of the two 'wrong'? The resulting plots are identical.

  • #2
    Interesting observation!

    The results produced are the same in either case, neither one is wrong. This is true in general--it is not some quirk of this particular analysis: listing a discrete variable in the -margins- variable list is equivalent to having it in an -at()- option with all of its values.

    Your little experiment actually underestimates the difference between the timings. That is because you included the -logit- regression, which is the same, in both timings. So that adds a constant to two different -margins/marginsplot- timings, resulting in a decrease in the ratio between them. Also, your analysis does not separately assess the timings for -margins- and -marginsplot-. So I modified your code and found:
    Code:
    . use https://www.stata-press.com/data/r18/nhanes2, clear
    
    . quietly logit highbp sex##age // estimates dummies, at() and margins with interactions is equivalent
    
    . timer clear
    
    . timer on 1
    
    . margins age#sex // does not work with continuous age (after specifying -c.-) (but -at() also works in this case)
    
    Adjusted predictions                                    Number of obs = 10,351
    Model VCE: OIM
    
    Expression: Pr(highbp), predict()
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   std. err.      z    P>|z|     [95% conf. interval]
    -------------+----------------------------------------------------------------
         age#sex |
        20#Male  |   .2524272   .0428032     5.90   0.000     .1685345    .3363199
      20#Female  |   .0851064   .0234994     3.62   0.000     .0390484    .1311644
        21#Male  |   .3208955   .0403272     7.96   0.000     .2418557    .3999353
      21#Female  |   .0816327    .022583     3.61   0.000     .0373708    .1258945
        22#Male  |    .231405   .0383391     6.04   0.000     .1562616    .3065483
      22#Female  |   .0307692   .0151461     2.03   0.042     .0010835     .060455
        23#Male  |     .21875   .0365396     5.99   0.000     .1471337    .2903663
      23#Female  |   .0769231   .0246351     3.12   0.002     .0286392     .125207
        24#Male  |   .2719298   .0416738     6.53   0.000     .1902508    .3536089
      24#Female  |   .0892857   .0269447     3.31   0.001     .0364751    .1420963
        25#Male  |        .24   .0427083     5.62   0.000     .1562932    .3237068
      25#Female  |   .0733945   .0249785     2.94   0.003     .0244376    .1223514
        26#Male  |   .2212389   .0390475     5.67   0.000     .1447072    .2977707
      26#Female  |   .0740741   .0252005     2.94   0.003      .024682    .1234662
        27#Male  |   .3020833   .0468629     6.45   0.000     .2102337     .393933
      27#Female  |   .1416667   .0318325     4.45   0.000      .079276    .2040573
        28#Male  |   .2736842   .0457431     5.98   0.000     .1840293    .3633391
      28#Female  |   .0916667   .0263413     3.48   0.001     .0400386    .1432947
        29#Male  |   .2767857   .0422762     6.55   0.000     .1939258    .3596456
      29#Female  |         .1        .03     3.33   0.001     .0412011    .1587989
        30#Male  |   .4090909   .0524118     7.81   0.000     .3063657    .5118161
      30#Female  |   .1538462   .0333561     4.61   0.000     .0884694    .2192229
        31#Male  |   .2857143    .045634     6.26   0.000     .1962732    .3751554
      31#Female  |   .1359223   .0337678     4.03   0.000     .0697386    .2021061
        32#Male  |   .3191489   .0480794     6.64   0.000     .2249151    .4133828
      32#Female  |       .125   .0352548     3.55   0.000      .055902     .194098
        33#Male  |   .3835616   .0569116     6.74   0.000     .2720169    .4951064
      33#Female  |   .2162162   .0478549     4.52   0.000     .1224223    .3100101
        34#Male  |   .4189189   .0573545     7.30   0.000     .3065062    .5313317
      34#Female  |   .1071429   .0337468     3.17   0.001     .0410003    .1732854
        35#Male  |   .3928571   .0532872     7.37   0.000     .2884161    .4972982
      35#Female  |   .2315789     .04328     5.35   0.000     .1467517    .3164062
        36#Male  |   .3333333   .0580259     5.74   0.000     .2196047     .447062
      36#Female  |   .2133333   .0473036     4.51   0.000       .12062    .3060467
        37#Male  |   .4305556   .0583545     7.38   0.000     .3161829    .5449282
      37#Female  |   .2738095    .048653     5.63   0.000     .1784513    .3691677
        38#Male  |         .4   .0632456     6.32   0.000      .276041     .523959
      38#Female  |   .2413793   .0561886     4.30   0.000     .1312516     .351507
        39#Male  |    .442623   .0635955     6.96   0.000      .317978    .5672679
      39#Female  |   .2972973   .0531331     5.60   0.000     .1931583    .4014363
        40#Male  |   .4242424   .0608352     6.97   0.000     .3050076    .5434772
      40#Female  |   .2461538   .0534303     4.61   0.000     .1414323    .3508754
        41#Male  |   .3188406   .0561031     5.68   0.000     .2088806    .4288006
      41#Female  |   .2258065   .0531003     4.25   0.000     .1217317    .3298812
        42#Male  |    .390625   .0609863     6.41   0.000      .271094     .510156
      42#Female  |   .2528736   .0466003     5.43   0.000     .1615386    .3442086
        43#Male  |   .4310345   .0650257     6.63   0.000     .3035865    .5584825
      43#Female  |   .3815789   .0557221     6.85   0.000     .2723656    .4907923
        44#Male  |   .5263158   .0661348     7.96   0.000     .3966939    .6559377
      44#Female  |   .3770492   .0620527     6.08   0.000      .255428    .4986703
        45#Male  |   .3898305   .0634947     6.14   0.000     .2653833    .5142777
      45#Female  |        .44   .0701997     6.27   0.000     .3024111    .5775889
        46#Male  |    .509434   .0686681     7.42   0.000      .374847    .6440209
      46#Female  |   .3432836   .0580067     5.92   0.000     .2295926    .4569746
        47#Male  |   .4347826   .0730912     5.95   0.000     .2915265    .5780387
      47#Female  |    .453125   .0622247     7.28   0.000     .3311668    .5750832
        48#Male  |         .5   .0597614     8.37   0.000     .3828697    .6171303
      48#Female  |         .4   .0632456     6.32   0.000      .276041     .523959
        49#Male  |   .5735294   .0599747     9.56   0.000     .4559812    .6910776
      49#Female  |   .3714286   .0577519     6.43   0.000      .258237    .4846201
        50#Male  |   .5714286   .0591485     9.66   0.000     .4554997    .6873575
      50#Female  |    .483871   .0518206     9.34   0.000     .3823045    .5854375
        51#Male  |         .5      .0625     8.00   0.000     .3775023    .6224977
      51#Female  |   .3793103   .0637119     5.95   0.000     .2544373    .5041834
        52#Male  |   .5820896   .0602558     9.66   0.000     .4639903    .7001888
      52#Female  |   .4133333   .0568611     7.27   0.000     .3018876     .524779
        53#Male  |   .5555556    .062604     8.87   0.000     .4328539    .6782572
      53#Female  |   .4193548   .0626687     6.69   0.000     .2965265    .5421832
        54#Male  |   .5660377   .0680786     8.31   0.000     .4326061    .6994694
      54#Female  |   .6290323   .0613492    10.25   0.000     .5087901    .7492744
        55#Male  |    .509434   .0686681     7.42   0.000      .374847    .6440209
      55#Female  |   .5616438   .0580741     9.67   0.000     .4478207     .675467
        56#Male  |   .5614035   .0657253     8.54   0.000     .4325842    .6902228
      56#Female  |   .6551724   .0624115    10.50   0.000     .5328481    .7774968
        57#Male  |   .6140351   .0644812     9.52   0.000     .4876543    .7404159
      57#Female  |   .5666667    .052234    10.85   0.000     .4642898    .6690435
        58#Male  |   .6393443   .0614821    10.40   0.000     .5188415     .759847
      58#Female  |   .5535714   .0664307     8.33   0.000     .4233697    .6837732
        59#Male  |   .6666667   .0624391    10.68   0.000     .5442884     .789045
      59#Female  |    .483871    .063467     7.62   0.000     .3594779     .608264
        60#Male  |   .5276074   .0391033    13.49   0.000     .4509663    .6042484
      60#Female  |   .5303867    .037096    14.30   0.000     .4576799    .6030936
        61#Male  |         .6   .0406838    14.75   0.000     .5202612    .6797388
      61#Female  |    .477707   .0398647    11.98   0.000     .3995737    .5558403
        62#Male  |   .6213018   .0373125    16.65   0.000     .5481706     .694433
      62#Female  |      .5125   .0395161    12.97   0.000     .4350498    .5899502
        63#Male  |   .5869565   .0419142    14.00   0.000     .5048062    .6691069
      63#Female  |   .5238095   .0411925    12.72   0.000     .4430737    .6045454
        64#Male  |   .5872093   .0375403    15.64   0.000     .5136318    .6607868
      64#Female  |        .62   .0396316    15.64   0.000     .5423234    .6976766
        65#Male  |   .5648855   .0433158    13.04   0.000     .4799881    .6497829
      65#Female  |   .6049383   .0384088    15.75   0.000     .5296584    .6802181
        66#Male  |   .5508475   .0457901    12.03   0.000     .4611005    .6405944
      66#Female  |   .5705521   .0387712    14.72   0.000      .494562    .6465423
        67#Male  |   .6101695   .0448975    13.59   0.000      .522172     .698167
      67#Female  |   .6929134   .0409325    16.93   0.000     .6126872    .7731396
        68#Male  |   .6019417   .0482316    12.48   0.000     .5074095     .696474
      68#Female  |   .6690141   .0394892    16.94   0.000     .5916167    .7464115
        69#Male  |   .6071429   .0461481    13.16   0.000     .5166943    .6975915
      69#Female  |   .6764706   .0463214    14.60   0.000     .5856824    .7672588
        70#Male  |   .6495726   .0441083    14.73   0.000      .563122    .7360233
      70#Female  |   .6068376   .0451574    13.44   0.000     .5183307    .6953446
        71#Male  |   .6886792   .0449738    15.31   0.000     .6005322    .7768263
      71#Female  |   .6370968   .0431805    14.75   0.000     .5524646    .7217289
        72#Male  |   .7241379   .0479178    15.11   0.000     .6302207    .8180551
      72#Female  |         .7    .041833    16.73   0.000     .6180088    .7819912
        73#Male  |   .6818182   .0573324    11.89   0.000     .5694488    .7941876
      73#Female  |   .6888889    .048799    14.12   0.000     .5932446    .7845332
        74#Male  |   .6111111   .0574522    10.64   0.000     .4985069    .7237153
      74#Female  |   .7126437   .0485162    14.69   0.000     .6175536    .8077337
    ------------------------------------------------------------------------------
    
    . timer off 1
    
    . timer on 2
    
    . marginsplot, name(g2, replace)
    
    Variables that uniquely identify margins: age sex
    
    . timer off 2
    
    .
    .     **can also compute age#sex using at(), but this is much slower, see comparison below**
    . timer on 2
    
    . //     logit highbp sex##age // estimates dummies, at() and margins with interactions is equivalent    
    . timer on 3
    
    . levelsof age
    20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 6
    > 4 65 66 67 68 69 70 71 72 73 74
    
    . margins sex, at(age=(`r(levels)'))
    
    Adjusted predictions                                    Number of obs = 10,351
    Model VCE: OIM
    
    Expression: Pr(highbp), predict()
    1._at:  age = 20
    2._at:  age = 21
    3._at:  age = 22
    4._at:  age = 23
    5._at:  age = 24
    6._at:  age = 25
    7._at:  age = 26
    8._at:  age = 27
    9._at:  age = 28
    10._at: age = 29
    11._at: age = 30
    12._at: age = 31
    13._at: age = 32
    14._at: age = 33
    15._at: age = 34
    16._at: age = 35
    17._at: age = 36
    18._at: age = 37
    19._at: age = 38
    20._at: age = 39
    21._at: age = 40
    22._at: age = 41
    23._at: age = 42
    24._at: age = 43
    25._at: age = 44
    26._at: age = 45
    27._at: age = 46
    28._at: age = 47
    29._at: age = 48
    30._at: age = 49
    31._at: age = 50
    32._at: age = 51
    33._at: age = 52
    34._at: age = 53
    35._at: age = 54
    36._at: age = 55
    37._at: age = 56
    38._at: age = 57
    39._at: age = 58
    40._at: age = 59
    41._at: age = 60
    42._at: age = 61
    43._at: age = 62
    44._at: age = 63
    45._at: age = 64
    46._at: age = 65
    47._at: age = 66
    48._at: age = 67
    49._at: age = 68
    50._at: age = 69
    51._at: age = 70
    52._at: age = 71
    53._at: age = 72
    54._at: age = 73
    55._at: age = 74
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   std. err.      z    P>|z|     [95% conf. interval]
    -------------+----------------------------------------------------------------
         _at#sex |
         1#Male  |   .2524272   .0428032     5.90   0.000     .1685345    .3363199
       1#Female  |   .0851064   .0234994     3.62   0.000     .0390484    .1311644
         2#Male  |   .3208955   .0403272     7.96   0.000     .2418557    .3999353
       2#Female  |   .0816327    .022583     3.61   0.000     .0373708    .1258945
         3#Male  |    .231405   .0383391     6.04   0.000     .1562616    .3065483
       3#Female  |   .0307692   .0151461     2.03   0.042     .0010835     .060455
         4#Male  |     .21875   .0365396     5.99   0.000     .1471337    .2903663
       4#Female  |   .0769231   .0246351     3.12   0.002     .0286392     .125207
         5#Male  |   .2719298   .0416738     6.53   0.000     .1902508    .3536089
       5#Female  |   .0892857   .0269447     3.31   0.001     .0364751    .1420963
         6#Male  |        .24   .0427083     5.62   0.000     .1562932    .3237068
       6#Female  |   .0733945   .0249785     2.94   0.003     .0244376    .1223514
         7#Male  |   .2212389   .0390475     5.67   0.000     .1447072    .2977707
       7#Female  |   .0740741   .0252005     2.94   0.003      .024682    .1234662
         8#Male  |   .3020833   .0468629     6.45   0.000     .2102337     .393933
       8#Female  |   .1416667   .0318325     4.45   0.000      .079276    .2040573
         9#Male  |   .2736842   .0457431     5.98   0.000     .1840293    .3633391
       9#Female  |   .0916667   .0263413     3.48   0.001     .0400386    .1432947
        10#Male  |   .2767857   .0422762     6.55   0.000     .1939258    .3596456
      10#Female  |         .1        .03     3.33   0.001     .0412011    .1587989
        11#Male  |   .4090909   .0524118     7.81   0.000     .3063657    .5118161
      11#Female  |   .1538462   .0333561     4.61   0.000     .0884694    .2192229
        12#Male  |   .2857143    .045634     6.26   0.000     .1962732    .3751554
      12#Female  |   .1359223   .0337678     4.03   0.000     .0697386    .2021061
        13#Male  |   .3191489   .0480794     6.64   0.000     .2249151    .4133828
      13#Female  |       .125   .0352548     3.55   0.000      .055902     .194098
        14#Male  |   .3835616   .0569116     6.74   0.000     .2720169    .4951064
      14#Female  |   .2162162   .0478549     4.52   0.000     .1224223    .3100101
        15#Male  |   .4189189   .0573545     7.30   0.000     .3065062    .5313317
      15#Female  |   .1071429   .0337468     3.17   0.001     .0410003    .1732854
        16#Male  |   .3928571   .0532872     7.37   0.000     .2884161    .4972982
      16#Female  |   .2315789     .04328     5.35   0.000     .1467517    .3164062
        17#Male  |   .3333333   .0580259     5.74   0.000     .2196047     .447062
      17#Female  |   .2133333   .0473036     4.51   0.000       .12062    .3060467
        18#Male  |   .4305556   .0583545     7.38   0.000     .3161829    .5449282
      18#Female  |   .2738095    .048653     5.63   0.000     .1784513    .3691677
        19#Male  |         .4   .0632456     6.32   0.000      .276041     .523959
      19#Female  |   .2413793   .0561886     4.30   0.000     .1312516     .351507
        20#Male  |    .442623   .0635955     6.96   0.000      .317978    .5672679
      20#Female  |   .2972973   .0531331     5.60   0.000     .1931583    .4014363
        21#Male  |   .4242424   .0608352     6.97   0.000     .3050076    .5434772
      21#Female  |   .2461538   .0534303     4.61   0.000     .1414323    .3508754
        22#Male  |   .3188406   .0561031     5.68   0.000     .2088806    .4288006
      22#Female  |   .2258065   .0531003     4.25   0.000     .1217317    .3298812
        23#Male  |    .390625   .0609863     6.41   0.000      .271094     .510156
      23#Female  |   .2528736   .0466003     5.43   0.000     .1615386    .3442086
        24#Male  |   .4310345   .0650257     6.63   0.000     .3035865    .5584825
      24#Female  |   .3815789   .0557221     6.85   0.000     .2723656    .4907923
        25#Male  |   .5263158   .0661348     7.96   0.000     .3966939    .6559377
      25#Female  |   .3770492   .0620527     6.08   0.000      .255428    .4986703
        26#Male  |   .3898305   .0634947     6.14   0.000     .2653833    .5142777
      26#Female  |        .44   .0701997     6.27   0.000     .3024111    .5775889
        27#Male  |    .509434   .0686681     7.42   0.000      .374847    .6440209
      27#Female  |   .3432836   .0580067     5.92   0.000     .2295926    .4569746
        28#Male  |   .4347826   .0730912     5.95   0.000     .2915265    .5780387
      28#Female  |    .453125   .0622247     7.28   0.000     .3311668    .5750832
        29#Male  |         .5   .0597614     8.37   0.000     .3828697    .6171303
      29#Female  |         .4   .0632456     6.32   0.000      .276041     .523959
        30#Male  |   .5735294   .0599747     9.56   0.000     .4559812    .6910776
      30#Female  |   .3714286   .0577519     6.43   0.000      .258237    .4846201
        31#Male  |   .5714286   .0591485     9.66   0.000     .4554997    .6873575
      31#Female  |    .483871   .0518206     9.34   0.000     .3823045    .5854375
        32#Male  |         .5      .0625     8.00   0.000     .3775023    .6224977
      32#Female  |   .3793103   .0637119     5.95   0.000     .2544373    .5041834
        33#Male  |   .5820896   .0602558     9.66   0.000     .4639903    .7001888
      33#Female  |   .4133333   .0568611     7.27   0.000     .3018876     .524779
        34#Male  |   .5555556    .062604     8.87   0.000     .4328539    .6782572
      34#Female  |   .4193548   .0626687     6.69   0.000     .2965265    .5421832
        35#Male  |   .5660377   .0680786     8.31   0.000     .4326061    .6994694
      35#Female  |   .6290323   .0613492    10.25   0.000     .5087901    .7492744
        36#Male  |    .509434   .0686681     7.42   0.000      .374847    .6440209
      36#Female  |   .5616438   .0580741     9.67   0.000     .4478207     .675467
        37#Male  |   .5614035   .0657253     8.54   0.000     .4325842    .6902228
      37#Female  |   .6551724   .0624115    10.50   0.000     .5328481    .7774968
        38#Male  |   .6140351   .0644812     9.52   0.000     .4876543    .7404159
      38#Female  |   .5666667    .052234    10.85   0.000     .4642898    .6690435
        39#Male  |   .6393443   .0614821    10.40   0.000     .5188415     .759847
      39#Female  |   .5535714   .0664307     8.33   0.000     .4233697    .6837732
        40#Male  |   .6666667   .0624391    10.68   0.000     .5442884     .789045
      40#Female  |    .483871    .063467     7.62   0.000     .3594779     .608264
        41#Male  |   .5276074   .0391033    13.49   0.000     .4509663    .6042484
      41#Female  |   .5303867    .037096    14.30   0.000     .4576799    .6030936
        42#Male  |         .6   .0406838    14.75   0.000     .5202612    .6797388
      42#Female  |    .477707   .0398647    11.98   0.000     .3995737    .5558403
        43#Male  |   .6213018   .0373125    16.65   0.000     .5481706     .694433
      43#Female  |      .5125   .0395161    12.97   0.000     .4350498    .5899502
        44#Male  |   .5869565   .0419142    14.00   0.000     .5048062    .6691069
      44#Female  |   .5238095   .0411925    12.72   0.000     .4430737    .6045454
        45#Male  |   .5872093   .0375403    15.64   0.000     .5136318    .6607868
      45#Female  |        .62   .0396316    15.64   0.000     .5423234    .6976766
        46#Male  |   .5648855   .0433158    13.04   0.000     .4799881    .6497829
      46#Female  |   .6049383   .0384088    15.75   0.000     .5296584    .6802181
        47#Male  |   .5508475   .0457901    12.03   0.000     .4611005    .6405944
      47#Female  |   .5705521   .0387712    14.72   0.000      .494562    .6465423
        48#Male  |   .6101695   .0448975    13.59   0.000      .522172     .698167
      48#Female  |   .6929134   .0409325    16.93   0.000     .6126872    .7731396
        49#Male  |   .6019417   .0482316    12.48   0.000     .5074095     .696474
      49#Female  |   .6690141   .0394892    16.94   0.000     .5916167    .7464115
        50#Male  |   .6071429   .0461481    13.16   0.000     .5166943    .6975915
      50#Female  |   .6764706   .0463214    14.60   0.000     .5856824    .7672588
        51#Male  |   .6495726   .0441083    14.73   0.000      .563122    .7360233
      51#Female  |   .6068376   .0451574    13.44   0.000     .5183307    .6953446
        52#Male  |   .6886792   .0449738    15.31   0.000     .6005322    .7768263
      52#Female  |   .6370968   .0431805    14.75   0.000     .5524646    .7217289
        53#Male  |   .7241379   .0479178    15.11   0.000     .6302207    .8180551
      53#Female  |         .7    .041833    16.73   0.000     .6180088    .7819912
        54#Male  |   .6818182   .0573324    11.89   0.000     .5694488    .7941876
      54#Female  |   .6888889    .048799    14.12   0.000     .5932446    .7845332
        55#Male  |   .6111111   .0574522    10.64   0.000     .4985069    .7237153
      55#Female  |   .7126437   .0485162    14.69   0.000     .6175536    .8077337
    ------------------------------------------------------------------------------
    
    . timer off 3
    
    . timer on 4
    
    . marginsplot, name(g3, replace)
    
    Variables that uniquely identify margins: age sex
    
    . timer off 4
    
    .         
    . timer list
       1:      2.57 /        1 =       2.5710
       2:      0.89 /        2 =       0.4460
       3:      7.61 /        1 =       7.6120
       4:      1.29 /        1 =       1.2930
    
    .
    end of do-file
    So, it appears that both -margins- and -marginsplot- take longer with the -at()- approach. In absolute terms, the slowdown of -margins- accounts for most of the delay, and that of -marginsplot- rather less.

    As for what is going on internally that accounts for this, I do not know. I suspect one would have to hack deeply into the code of both of those commands to find out what is going on. At a guess, the -at()- syntax sends -margins- into some loop that runs slowly, whereas putting the interaction in the varlist leads to some highly efficient Mata calculations. The slowdown in -marginsplot-, I imagine, is due to the data being passed to it being organized differently. But all of that's just speculation on my part.

    If somebody from StataCorp who knows the inner workings of -margins- and -marginsplot- is following along, it would be interesting to know what is actually happening.
    Last edited by Clyde Schechter; 07 Apr 2024, 11:24.

    Comment


    • #3
      Whenever you put a factor variable in option at(), margins must make a temporary (modified) copy of each of its factor-level variables in the dataset and the stripes of e(b). This happens for each value specified in the at() option. In the above example, that is a lot of zero-valued temporary variables and an indicator variable for each specified value for age.

      When you specify a factor variable in the marginslist, the variable itself is temporarily changed.

      The time difference is mostly due to the amount of changes happening in the dataset.

      I'm not sure why there is a timing difference for marginsplot, but I suspect it has to do with the extra work to translate the _at levels to the values of age.

      Comment


      • #4
        Thanks, Jeff!

        Comment

        Working...
        X