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.
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 -------------------------
Comment