Announcement

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

  • How does at() option of margins work?

    Hi everyone,

    I have been working using margins and at() option for a while. These days I have been trying to replicate manually what I thought the at() option does, but I can't get the same results as I get using margins. I always assumed that at() fixes the value of a variable and then computes the fitted values. What I found out is that when I compute the fitted values manually the results differ. It happened in a real dataset and I created a simplified one to make it clear. In the following example there are 4 variables (i.e. x1, x2, x3, x4 and y) and 6 observations. First I regress y on x's and use margins with at() option - then I use the regression coefficient to "manually" predict the fitted values and compute the mean for the variable of interest. When x1 is fixed at x2 == 1, the results differ from margins. Conversely, when I predict the average fitted values for x2 fixing at x1 == 1 I get the same values as in the margins command. I'm sure this difference is due to a basic but couldn't find anything, would appreciate if someone can explain why this difference happens. Thanks.

    Code:
    . list
    
         +--------------------+
         | x1   x2   x3     y |
         |--------------------|
      1. |  1    1    1   3.1 |
      2. |  1    2    2   2.8 |
      3. |  1    3    1   2.5 |
      4. |  2    1    2   4.3 |
      5. |  2    2    1     4 |
         |--------------------|
      6. |  2    3    2   3.5 |
         +--------------------+
    
    . 
    . regress y i.x1 i.x2 i.x3
    
          Source |       SS       df       MS              Number of obs =       6
    -------------+------------------------------           F(  4,     1) =   60.58
           Model |  2.42333377     4  .605833441           Prob > F      =  0.0960
        Residual |  .010000029     1  .010000029           R-squared     =  0.9959
    -------------+------------------------------           Adj R-squared =  0.9795
           Total |  2.43333379     5  .486666759           Root MSE      =      .1
    
    ------------------------------------------------------------------------------
               y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
            2.x1 |       1.15   .0866027    13.28   0.048     .0496089    2.250391
                 |
              x2 |
              2  |  -.3000001   .1000001    -3.00   0.205    -1.570622    .9706222
              3  |        -.7   .1000001    -7.00   0.090    -1.970622    .5706222
                 |
            2.x3 |       -.05   .0866027    -0.58   0.667    -1.150391    1.050391
           _cons |       3.15   .0866027    36.37   0.017     2.049609    4.250391
    ------------------------------------------------------------------------------
    
    . margins i.x1, at(x2 == 1)
    
    Predictive margins                                Number of obs   =          6
    Model VCE    : OLS
    
    Expression   : Linear prediction, predict()
    at           : x2              =           1
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
              x1 |
              1  |      3.125   .0829157    37.69   0.017     2.071456    4.178544
              2  |      4.275   .0829157    51.56   0.012     3.221456    5.328544
    ------------------------------------------------------------------------------
    
    . margins i.x2, at(x1 == 1)
    
    Predictive margins                                Number of obs   =          6
    Model VCE    : OLS
    
    Expression   : Linear prediction, predict()
    at           : x1              =           1
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
              x2 |
              1  |      3.125   .0829157    37.69   0.017     2.071456    4.178544
              2  |      2.825   .0829157    34.07   0.019     1.771456    3.878544
              3  |      2.425   .0829157    29.25   0.022     1.371456    3.478544
    ------------------------------------------------------------------------------
    
    . 
    . //generate variables for predict at
    . gen predx1_2 = 0
    
    . replace predx1_2 = 1.15 if x1 == 2
    (3 real changes made)
    
    . gen predx2_2 = 0
    
    . replace predx2_2 = -.3000001 if x2 == 2
    (2 real changes made)
    
    . gen predx2_3 = 0
    
    . replace predx2_3 =  -.7 if x2 == 3
    (2 real changes made)
    
    . gen predx3_2 = 0
    
    . replace predx3_2 = -.05 if x3 == 2
    (3 real changes made)
    
    . 
    . gen pred_x1 = 3.15 + predx1_2 + predx3_2 //generate fitted values at x2==1
    
    . gen pred_x2 = 3.15 + predx2_2 + predx2_3 + predx3_2 // generate fitted values at x1==1
    
    . 
    . table x1, contents(mean pred_x1)
    
    -------------------------
           x1 | mean(pred_x1)
    ----------+--------------
            1 |      3.133333
            2 |      4.266667
    -------------------------
    
    . table x2, contents(mean pred_x2)
    
    -------------------------
           x2 | mean(pred_x2)
    ----------+--------------
            1 |         3.125
            2 |         2.825
            3 |         2.425
    -------------------------

  • #2
    Cross-posted in a somewhat different form at https://stackoverflow.com/questions/...-stata-margins

    Comment


    • #3
      Granted, it is only 6 cases, but you'll make it easier fot people if you use dataex so they can easily reproduce the data and the analyses:
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(x1 x2 x3 y)
      1 1 1 3.1
      1 2 2 2.8
      1 3 1 2.5
      2 1 2 4.3
      2 2 1   4
      2 3 2 3.5
      end
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      Stata Version: 17.0 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://www3.nd.edu/~rwilliam

      Comment


      • #4
        The problem with the Pablos' code is that it doesn't obtain the adjusted mean properly. It treats the third variable of the model as observed, but it doesn't apply the model for the whole sample for every condition of the variable whose margins are calculated (x1 in this case).

        There are two procedures: the easiest is at mean; but you can also apply the Stata default asobserved.
        Codes for those two procedures are the following:

        Code:
        . clear
        
        . input float(x1 x2 x3 y)
        
                    x1         x2         x3          y
          1. 1 1 1 3.1
          2. 1 2 2 2.8
          3. 1 3 1 2.5
          4. 2 1 2 4.3
          5. 2 2 1   4
          6. 2 3 2 3.5
          7. end
        
        . * Obtain mean
        . sum 2.x3, meanonly
        
        . scalar x3mean=r(mean)
        
        .
        . quietly: regress y i.x1 i.x2 i.x3
        
        .
        . margins x1, at(x2=1) nopvalues
        
        Predictive margins                                Number of obs   =          6
        Model VCE    : OLS
        
        Expression   : Linear prediction, predict()
        at           : x2              =           1
        
        --------------------------------------------------------------
                     |            Delta-method
                     |     Margin   Std. Err.     [95% Conf. Interval]
        -------------+------------------------------------------------
                  x1 |
                  1  |      3.125   .0829157      2.071456    4.178544
                  2  |      4.275   .0829157      3.221456    5.328544
        --------------------------------------------------------------
        
        
        
        . ** Obtain predicted values in case x2=1 and omitted x3 atmeans
        . ** The second procedure is equivalent to margins x1, at(x2=1 means(x3))
        . ** Here there are only a prediction with covariates at mean (x3m
        . ** x2 is not in the equation because at(x2==1) is the base category (0).
        
        . gen yHat1b=_b[_cons]+_b[2.x1]*2.x1+          _b[2.x3]*x3mean  // at x2==1 atmeans
        
        . mean yHat1b, over(x1)
        
        Mean estimation                     Number of obs    =       6
        
                    1: x1 = 1
                    2: x1 = 2
        
        --------------------------------------------------------------
                Over |       Mean   Std. Err.     [95% Conf. Interval]
        -------------+------------------------------------------------
        yHat1b       |
                   1 |      3.125          0             .           .
                   2 |      4.275          0             .           .
        --------------------------------------------------------------
        
        . ** Obtain predicted values in case x2=1 and omitted x3 asobserved
        . ** Equivalent to margins x1, at(x2=1 asobserved(x3)).
        . ** There are k predictions. One for each value of x1.
        . ** In this case *0 for x1==1 and *1 for x1==2
        . ** x2 is neither in the equation because at(x2==1) is the base category (0).
        
        . gen yHat1a1=_b[_cons]+_b[2.x1]*0+              _b[2.x3]*2.x3  // & asobserved x3  
        
        . gen yHat1a2=_b[_cons]+_b[2.x1]*1+              _b[2.x3]*2.x3  // & asobserved x3
        
        . sum yHat1a?
        
            Variable |       Obs        Mean    Std. Dev.       Min        Max
        -------------+--------------------------------------------------------
             yHat1a1 |         6       3.125    .0273862        3.1       3.15
             yHat1a2 |         6       4.275    .0273862       4.25        4.3
        For more details see Williams (2012).

        Comment


        • #5
          Thank you Modesto, I was looking for this. Then thing is that the procedure used by margins at(), as you show here, is not 100% intuitive...

          Comment

          Working...
          X