Announcement

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

  • margins, dydx() question

    Hi,

    Just a quick question.

    Just say you were to find a significant interaction between two categorical variables (each with 2 levels).

    Say one of those variables was sex, which had acted as a moderator on the main variable of interest.

    How would you report the use of margins sex, dydx(X) in the results?

    I am surprised that this command does not produce the same results as a sex-stratified model?

    Thanks for you advice.

    Lucy

  • #2
    The command you post gives you the marginal effect of X on the outcome, which might be different for the two sexes. For example, the effect X has on Y for male is -1, but for women it is +1. This is a quite interesting finding as it shows you hat Xs works differently for the sexes.

    Regarding the second question, I assume that control variables make a difference. In a sex-stratified model (so two regressions for each subsample), there is an implicit interaction included between sex and each other control by design. To get the same results in a single model you need something like
    Code:
    reg y i.x##i.sex i.sex##(c.control1 c.control2)
    Best wishes

    Stata 18.0 MP | ORCID | Google Scholar

    Comment


    • #3
      That's really helpful, thank you.

      Would it be correct then to do:
      reg y i.x##i.sex c.control1 c.control2 margins sex, dydx(X)

      and say these results are "stratified by sex"? (i.e. I am not sure how to report these results in a paper)

      These results provide a significant p value for females, but not for males. But in the 2 separate sex stratified models, the impact of X is not significant for either sex.

      Does the margins command consider the impact of the control variables?

      Comment


      • #4
        p.s. that code was meant to be over two separate lines!

        Comment


        • #5
          The command you show in post 3 is not fully stratified since there is no interaction for control1 and control2. To be explicit, you need

          Code:
          reg y i.x##i.sex i.sex##c.control1 i.sex##c.control2
          To give a concrete example:

          Code:
          . sysuse auto, clear
          (1978 Automobile Data)
          
          . bysort foreign: reg mpg weight price headroom
          
          ------------------------------------------------------------------------------------------------------------------
          -> foreign = Domestic
          
                Source |       SS           df       MS      Number of obs   =        52
          -------------+----------------------------------   F(3, 48)        =     59.75
                 Model |  905.081523         3  301.693841   Prob > F        =    0.0000
              Residual |  242.360785        48  5.04918302   R-squared       =    0.7888
          -------------+----------------------------------   Adj R-squared   =    0.7756
                 Total |  1147.44231        51  22.4988688   Root MSE        =     2.247
          
          ------------------------------------------------------------------------------
                   mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
                weight |  -.0062287   .0006945    -8.97   0.000    -.0076252   -.0048322
                 price |    .000191   .0001412     1.35   0.182    -.0000929    .0004749
              headroom |  -.5451166   .3943986    -1.38   0.173    -1.338108     .247875
                 _cons |   41.04741    1.65969    24.73   0.000     37.71038    44.38444
          ------------------------------------------------------------------------------
          
          ------------------------------------------------------------------------------------------------------------------
          -> foreign = Foreign
          
                Source |       SS           df       MS      Number of obs   =        22
          -------------+----------------------------------   F(3, 18)        =      5.57
                 Model |  441.736499         3    147.2455   Prob > F        =    0.0070
              Residual |  476.127137        18  26.4515076   R-squared       =    0.4813
          -------------+----------------------------------   Adj R-squared   =    0.3948
                 Total |  917.863636        21  43.7077922   Root MSE        =    5.1431
          
          ------------------------------------------------------------------------------
                   mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
                weight |  -.0087963   .0055794    -1.58   0.132    -.0205181    .0029255
                 price |  -.0003344   .0009221    -0.36   0.721    -.0022717    .0016029
              headroom |   1.478554   2.321062     0.64   0.532    -3.397816    6.354925
                 _cons |   43.41484   10.02813     4.33   0.000     22.34653    64.48316
          ------------------------------------------------------------------------------
          
          . reg mpg c.weight##i.foreign i.foreign##(c.price c.headroom)
          
                Source |       SS           df       MS      Number of obs   =        74
          -------------+----------------------------------   F(7, 66)        =     22.64
                 Model |  1724.97154         7  246.424505   Prob > F        =    0.0000
              Residual |  718.487922        66  10.8861806   R-squared       =    0.7060
          -------------+----------------------------------   Adj R-squared   =    0.6748
                 Total |  2443.45946        73  33.4720474   Root MSE        =    3.2994
          
          ------------------------------------------------------------------------------------
                         mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------------+----------------------------------------------------------------
                      weight |  -.0062287   .0010198    -6.11   0.000    -.0082648   -.0041926
                             |
                     foreign |
                    Foreign  |   2.367435   6.879391     0.34   0.732    -11.36771    16.10258
                             |
            foreign#c.weight |
                    Foreign  |  -.0025676   .0037217    -0.69   0.493    -.0099983    .0048631
                             |
                       price |    .000191   .0002073     0.92   0.360    -.0002229     .000605
                    headroom |  -.5451166   .5791121    -0.94   0.350    -1.701351     .611118
                             |
             foreign#c.price |
                    Foreign  |  -.0005254   .0006268    -0.84   0.405    -.0017769    .0007261
                             |
          foreign#c.headroom |
                    Foreign  |   2.023671   1.597667     1.27   0.210    -1.166173    5.213515
                             |
                       _cons |   41.04741   2.436992    16.84   0.000      36.1818    45.91302
          ------------------------------------------------------------------------------------
          
          . margins, dydx(weight) by(foreign)
          
          Average marginal effects                        Number of obs     =         74
          Model VCE    : OLS
          
          Expression   : Linear prediction, predict()
          dy/dx w.r.t. : weight
          over         : foreign
          
          ------------------------------------------------------------------------------
                       |            Delta-method
                       |      dy/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
          weight       |
               foreign |
             Domestic  |  -.0062287   .0010198    -6.11   0.000    -.0082648   -.0041926
              Foreign  |  -.0087963   .0035793    -2.46   0.017    -.0159426     -.00165
          ------------------------------------------------------------------------------
          As you see, what margins gives you is exactly what the separate regressions produce for the coef of weight.


          (The command I showed above with the parentheses is just a shortcut notation)

          And yes, margin will adjust for all control variables in the model when computing the marginal effect. In a linear regression (regress) what margin does is rather straightforward and not even required, you could compute it manually from the regression and interaction coefficients. But ofc this is a bit tedious, especially in more complex models.
          Best wishes

          Stata 18.0 MP | ORCID | Google Scholar

          Comment


          • #6
            Thank you very much - I really appreciating you walking this through with me!

            My follow up questions are:

            1) In the initial stratified model for 'foreign', the regression p-value for weight is 0.132. I can see how the margins command produces the same coefficient (-0.0087963) but now the p value is statistically significant (0.000). Which p value should be reported?

            2) Which model would be best to use to get adjusted means for each group?

            For example, would it be:
            reg mpg c.weight##i.foreign i.foreign##(c.price c.headroom)

            margins foreign#weight

            (of course would need to assume weight was a factor variable for this command to work)

            or, the original model:
            reg mpg c.weight##i.foreign c.price c.headroom margins

            foreign#weight

            3) Similarly, if you were to assume that both weight and foreign were factor variables, to look at posthoc simple effects would you use either:


            reg mpg c.weight##i.foreign i.foreign##(c.price c.headroom)

            contrast foreign@weight

            or the original model:

            reg mpg c.weight##i.foreign c.price c.headroom
            margins

            contrast foreign@weight

            (I assume the latter?)

            Many thanks,
            Lucy



            Last edited by Lucy Hiscox; 23 Nov 2021, 17:00.

            Comment

            Working...
            X