Announcement

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

  • adjusted means after xtreg

    Hi - I ran a regression model looking at the effect of a treatment (Group) on an outcome variable (Var1) with one covariate (Var2) using XTREG Var1 Var2 Group.
    I'd like to use the MARGINS command to obtain the adjusted means to communicate the effect of the treatment.

    If the predictor variable Group is code 0 or 1, how can I write a command to get the adjusted means? Intuitively, I would guess that I could write MARGINS, at(Group=1). Any advice?
    Last edited by R LaChausse; 30 Mar 2015, 17:29.

  • #2
    Hi,

    I wonder whether you wish this:

    Code:
    . margins, over(group) atmeans
    If not - or even if yes! - , please take a look at this benchmark text: https://www3.nd.edu/~rwilliam/stats/Margins01.pdf

    Best,

    Marcos
    Best regards,

    Marcos

    Comment


    • #3
      Thanks Marcos! That worked.

      Comment


      • #4
        Careful, that may not be what you wanted. When -over(group)- is used, you are getting adjusted means calculated only using the subpopulations defined by the groups. That is, you are getting the group-specific mean predicted values of your outcome variable. See this illustration (I'm omitting the -atmeans- part because it just clouds the picture by making other changes to the data:
        Code:
        . clear*
        
        . sysuse auto
        (1978 Automobile Data)
        
        .
        . regress price mpg i.foreign
        
              Source |       SS       df       MS              Number of obs =      74
        -------------+------------------------------           F(  2,    71) =   14.07
               Model |   180261702     2  90130850.8           Prob > F      =  0.0000
            Residual |   454803695    71  6405685.84           R-squared     =  0.2838
        -------------+------------------------------           Adj R-squared =  0.2637
               Total |   635065396    73  8699525.97           Root MSE      =  2530.9
        
        ------------------------------------------------------------------------------
               price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
                 mpg |  -294.1955   55.69172    -5.28   0.000    -405.2417   -183.1494
                     |
             foreign |
            Foreign  |   1767.292    700.158     2.52   0.014     371.2169    3163.368
               _cons |   11905.42   1158.634    10.28   0.000     9595.164    14215.67
        ------------------------------------------------------------------------------
        
        . predict xb, xb
        
        . by foreign, sort: summarize xb
        
        ---------------------------------------------------------------------------------------------------------------------------------------------------------
        -> foreign = Domestic
        
            Variable |       Obs        Mean    Std. Dev.       Min        Max
        -------------+--------------------------------------------------------
                  xb |        52    6072.423    1395.457   1902.767   8375.069
        
        ---------------------------------------------------------------------------------------------------------------------------------------------------------
        -> foreign = Foreign
        
            Variable |       Obs        Mean    Std. Dev.       Min        Max
        -------------+--------------------------------------------------------
                  xb |        22    6384.682    1944.982   1610.691    9553.97
        
        
        .
        . margins, over(foreign)
        
        Predictive margins                                Number of obs   =         74
        Model VCE    : OLS
        
        Expression   : Linear prediction, predict()
        over         : foreign
        
        ------------------------------------------------------------------------------
                     |            Delta-method
                     |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
             foreign |
           Domestic  |   6072.423    350.979    17.30   0.000     5372.591    6772.255
            Foreign  |   6384.682   539.5994    11.83   0.000     5308.751    7460.613
        ------------------------------------------------------------------------------


        There is also
        Code:
        margins group, atmeans
        which is different but is what people usually have in mind when they speak of adjusted means.


        When the code I have suggested here is used, every observation in the estimation sample enters into the calculations of each margin, but with the group variable set to the corresponding value and all other variables set to their means. See this example:

        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . regress price mpg i.foreign
        
              Source |       SS       df       MS              Number of obs =      74
        -------------+------------------------------           F(  2,    71) =   14.07
               Model |   180261702     2  90130850.8           Prob > F      =  0.0000
            Residual |   454803695    71  6405685.84           R-squared     =  0.2838
        -------------+------------------------------           Adj R-squared =  0.2637
               Total |   635065396    73  8699525.97           Root MSE      =  2530.9
        
        ------------------------------------------------------------------------------
               price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
                 mpg |  -294.1955   55.69172    -5.28   0.000    -405.2417   -183.1494
                     |
             foreign |
            Foreign  |   1767.292    700.158     2.52   0.014     371.2169    3163.368
               _cons |   11905.42   1158.634    10.28   0.000     9595.164    14215.67
        ------------------------------------------------------------------------------
        
        . clonevar original_foreign = foreign
        
        . levelsof foreign, local(foreign_values)
        0 1
        
        . foreach f of local foreign_values {
          2.         replace foreign = `f'
          3.         predict xb`f', xb
          4. }
        (22 real changes made)
        (74 real changes made)
        
        . summ xb*
        
            Variable |       Obs        Mean    Std. Dev.       Min        Max
        -------------+--------------------------------------------------------
                 xb0 |        74    5639.846    1702.069  -156.6016   8375.069
                 xb1 |        74    7407.138    1702.069   1610.691   10142.36
        
        . replace foreign = original_foreign
        (52 real changes made)
        
        . margins foreign
        
        Predictive margins                                Number of obs   =         74
        Model VCE    : OLS
        
        Expression   : Linear prediction, predict()
        
        ------------------------------------------------------------------------------
                     |            Delta-method
                     |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
             foreign |
           Domestic  |   5639.846   360.4051    15.65   0.000     4921.218    6358.473
            Foreign  |   7407.138   573.2627    12.92   0.000     6264.084    8550.191
        ------------------------------------------------------------------------------
        Do read the pdf by Richard Williams that Marcos gave you the link for, and if need be re-read it until you understand it. I find it by far the clearest explanation of how margins works. The term "adjusted means" is quite ambiguous and used differently in different contexts and even by the same people at different times (mea culpa!). When using the -margins- command, you have to be clear on just which meaning of "adjusted means" you intend.

        Comment


        • #5
          Excellent explanation, Clyde. Indeed, as you implicitly remarked, - margins, over() - would only provide the same results when the regression model has just the group variable:

          Code:
          . sysuse auto
          (1978 Automobile Data)
          
          . regress price i.foreign
          
                Source |       SS       df       MS              Number of obs =      74
          -------------+------------------------------           F(  1,    72) =    0.17
                 Model |  1507382.66     1  1507382.66           Prob > F      =  0.6802
              Residual |   633558013    72  8799416.85           R-squared     =  0.0024
          -------------+------------------------------           Adj R-squared = -0.0115
                 Total |   635065396    73  8699525.97           Root MSE      =  2966.4
          
          ------------------------------------------------------------------------------
                 price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
               foreign |
              Foreign  |   312.2587   754.4488     0.41   0.680    -1191.708    1816.225
                 _cons |   6072.423    411.363    14.76   0.000     5252.386     6892.46
          ------------------------------------------------------------------------------
          
          . margins foreign, atmeans
          
          Adjusted predictions                              Number of obs   =         74
          Model VCE    : OLS
          
          Expression   : Linear prediction, predict()
          
          ------------------------------------------------------------------------------
                       |            Delta-method
                       |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
               foreign |
             Domestic  |   6072.423    411.363    14.76   0.000     5252.386     6892.46
              Foreign  |   6384.682   632.4346    10.10   0.000     5123.947    7645.417
          ------------------------------------------------------------------------------
          
          . margins, over(foreign) atmeans
          
          Predictive margins                                Number of obs   =         74
          Model VCE    : OLS
          
          Expression   : Linear prediction, predict()
          over         : foreign
          
          ------------------------------------------------------------------------------
                       |            Delta-method
                       |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
               foreign |
             Domestic  |   6072.423    411.363    14.76   0.000     5252.386     6892.46
              Foreign  |   6384.682   632.4346    10.10   0.000     5123.947    7645.417
          ------------------------------------------------------------------------------

          With regards to the ambigous use of "adjusted", perhaps we could say the option - over() - provides, quite on the contrary, the "unadjusted" means. In the example below, with an additional covariate, the - margins, over() - gives still the same results as the mean by group:

          Code:
          . regress price mpg i.foreign
          
                Source |       SS       df       MS              Number of obs =      74
          -------------+------------------------------           F(  2,    71) =   14.07
                 Model |   180261702     2  90130850.8           Prob > F      =  0.0000
              Residual |   454803695    71  6405685.84           R-squared     =  0.2838
          -------------+------------------------------           Adj R-squared =  0.2637
                 Total |   635065396    73  8699525.97           Root MSE      =  2530.9
          
          ------------------------------------------------------------------------------
                 price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
                   mpg |  -294.1955   55.69172    -5.28   0.000    -405.2417   -183.1494
                       |
               foreign |
              Foreign  |   1767.292    700.158     2.52   0.014     371.2169    3163.368
                 _cons |   11905.42   1158.634    10.28   0.000     9595.164    14215.67
          ------------------------------------------------------------------------------
          
          . mean price, over(foreign)
          
          Mean estimation                     Number of obs    =      74
          
               Domestic: foreign = Domestic
                Foreign: foreign = Foreign
          
          --------------------------------------------------------------
                  Over |       Mean   Std. Err.     [95% Conf. Interval]
          -------------+------------------------------------------------
          price        |
              Domestic |   6072.423   429.4911      5216.449    6928.398
               Foreign |   6384.682   558.9942      5270.608    7498.756
          --------------------------------------------------------------
          Thank you very much again for having shed light on this matter.

          Best,

          Marcos
          Last edited by Marcos Almeida; 31 Mar 2015, 05:26.
          Best regards,

          Marcos

          Comment


          • #6
            Great discussion and tutorial through instructional examples. Thank you Clyde and Marcos. I learned a lot.

            As a follow-up question, do either of you have a sense of which derivation of the "adjusted mean" is equivalent to the "least squares mean" generated by SAS users?

            I'm guessing the STATA equivalent would be derived from the two commands below. Thoughts?

            regress price mpg i.foreign
            margins foreign
            Last edited by Christopher Rowan; 05 Apr 2016, 20:10.

            Comment


            • #7
              Try
              Code:
              margins foreign, asbalanced
              Open the Stata user's manual at the entry for margins and search for "least squares means". Also, see here.

              Comment


              • #8
                Thank you, Joseph. It's interesting that the following code - margins randomise_, "" "atmeans" and "asbalanced" - provide the exact same result. Perhaps it's because the covariate (varx1) is not a factor variable?

                Code:
                . regress vary i.randomise_ varx1 if pp_==1
                
                . margins randomise_
                ------------------------------------------------------------------------------
                | Delta-method
                | Margin Std. Err. t P>|t| [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                randomise_ |
                Sham | 2.113091 .7432442 2.84 0.005 .6438376 3.582344
                Active | 4.174334 .512992 8.14 0.000 3.160246 5.188422
                ------------------------------------------------------------------------------
                
                . margins randomise_, atmeans
                ------------------------------------------------------------------------------
                | Delta-method
                | Margin Std. Err. t P>|t| [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                randomise_ |
                Sham | 2.113091 .7432443 2.84 0.005 .6438379 3.582345
                Active | 4.174334 .512992 8.14 0.000 3.160246 5.188422
                ------------------------------------------------------------------------------
                
                . margins randomise_, asbalanced
                ------------------------------------------------------------------------------
                | Delta-method
                | Margin Std. Err. t P>|t| [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                randomise_ |
                Sham | 2.113091 .7432442 2.84 0.005 .6438376 3.582344
                Active | 4.174334 .512992 8.14 0.000 3.160246 5.188422
                ------------------------------------------------------------------------------
                Last edited by Christopher Rowan; 06 Apr 2016, 10:46.

                Comment


                • #9
                  If the two randomization groups are (already) balanced, then the asbalanced option won't make any difference.

                  Comment


                  • #10
                    Hello all, similar to the above, I'm trying to establish the adjusted means (but for a difference-in-difference regression).

                    I've done a fixed effects regression to calculate the difference-in-difference coefficient for a policy change. My command is:

                    reg Achievement Time Treated Time##Treated i.indicator i.practice

                    This gives me the DiD coefficient for relative change in 'Achievement', adjusted by time-invariant 'practice' and 'indicator' effects.

                    But to communicate my results more clearly, I'd like to show the mean 'Achievement' levels in each group, and in each time period, adjusted by practice and indicator. What's the best command the use? Many thanks.

                    Comment

                    Working...
                    X