# Stata：如何理解三个变量的交乘项？

Stata连享会   主页 || 视频 || 推文 || 知乎 || Bilibili 站

New！ `lianxh` 命令发布了：

`. ssc install lianxh`

`. help lianxh`

⛳ Stata 系列推文：

## 2. 对三项交互的解释

1. $Hz×Hw$
2. $Hz×Lw$
3. $Lz×Hw$
4. $Lz×Lw$

## 3. Stata 实操

### 3.1 基础模型

``````use https://stats.idre.ucla.edu/stat/stata/notes/hsb2, clear
rename write y
rename math w
rename science z

regress y c.x###c.z###c.w
regress y c.x###c.z###c.w

Source |       SS           df       MS      Number of obs   =       200
-------------+----------------------------------   F(7, 192)       =     26.28
Model |  8748.54751         7   1249.7925   Prob > F        =    0.0000
Residual |  9130.32749       192   47.553789   R-squared       =    0.4893
Total |   17878.875       199   89.843593   Root MSE        =    6.8959

------------------------------------------------------------------------------
y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
x |     -3.014      1.803    -1.67   0.096       -6.570       0.542
z |     -2.435      1.757    -1.39   0.167       -5.902       1.031
c.x#c.z |      0.055      0.033     1.70   0.091       -0.009       0.119
w |     -3.228      1.926    -1.68   0.095       -7.028       0.572
c.x#c.w |      0.074      0.036     2.05   0.041        0.003       0.145
c.z#c.w |      0.061      0.035     1.74   0.083       -0.008       0.130
c.x#c.z#c.w |     -0.001      0.001    -2.00   0.047       -0.002      -0.000
_cons |    166.544     93.128     1.79   0.075      -17.141     350.229
------------------------------------------------------------------------------
``````

``````/* determine range of x for graphing */
sum x
sum w

/* determined high and low values for w */
global Hw=r(mean)+r(sd)
global Lw=r(mean)-r(sd)

sum z

/* determine high and low values for z */
global Hz=r(mean)+r(sd)
global Lz=r(mean)-r(sd)
``````

``````margins, dydx(x) at(w=(\$Hw \$Lw) z=(\$Hz \$Lz)) vsquish

Average marginal effects                        Number of obs     =        200
Model VCE    : OLS

Expression   : Linear prediction, predict()
dy/dx w.r.t. : x
1._at        : z    =    61.75089
w    =    62.01345
2._at        : z    =    61.75089
w    =    43.27655
3._at        : z    =    41.94911
w    =    62.01345
4._at        : z    =    41.94911
w    =    43.27655

------------------------------------------------------------------
|        Delta-method
|  dy/dx   Std. Err.     t    P>|t|  [95% Conf. Interval]
---------+--------------------------------------------------------
x       |
_at |
1  |  0.165      0.095    1.73   0.086    -0.024       0.353
2  |  0.236      0.143    1.65   0.100    -0.046       0.518
3  |  0.612      0.196    3.12   0.002     0.225       0.999
4  |  0.218      0.128    1.71   0.090    -0.034       0.469
------------------------------------------------------------------

``````

``````margins, at(x=(20 80) w=(\$Hw \$Lw) z=(\$Hz \$Lz)) vsquish
margins, at(x=(20 80) w=(\$Hw \$Lw) z=(\$Hz \$Lz)) vsquish

Adjusted predictions      Number of obs = 200
Model VCE    : OLS

Expression   : Linear prediction, predict()
1._at        : x               =          20
z               =    61.75089
w               =    62.01345
2._at        : x               =          20
z               =    61.75089
w               =    43.27655
3._at        : x               =          20
z               =    41.94911
w               =    62.01345
4._at        : x               =          20
z               =    41.94911
w               =    43.27655
5._at        : x               =          80
z               =    61.75089
w               =    62.01345
6._at        : x               =          80
z               =    61.75089
w               =    43.27655
7._at        : x               =          80
z               =    41.94911
w               =    62.01345
8._at        : x               =          80
z               =    41.94911
w               =    43.27655

----------------------------------------------------------------
|         Delta-method
|  Margin   Std. Err.      t    P>|t|  [95% Conf. Interval]
-----+----------------------------------------------------------
_at |
1  |  53.294      4.059    13.13   0.000    45.288      61.301
2  |  44.503      5.033     8.84   0.000    34.575      54.431
3  |  35.420      6.803     5.21   0.000    22.003      48.837
4  |  39.981      3.034    13.18   0.000    33.997      45.965
5  |  63.168      1.927    32.79   0.000    59.367      66.968
6  |  58.678      4.007    14.65   0.000    50.775      66.580
7  |  72.138      5.488    13.14   0.000    61.313      82.963
8  |  53.033      4.787    11.08   0.000    43.591      62.475
----------------------------------------------------------------

``````

``````marginsplot, recast(line) noci addplot(scatter y x, msym(oh) jitter(3))

marginsplot, recast(line) noci addplot(scatter y x, msym(oh) jitter(3))
``````

### 3.2 加入协变量的情况

``````rename socst v1
rename ses v2
regress y c.x###c.z###c.w v1 v2

Source |      SS       df       MS      Number of obs   =       200
---------+-----------------------------   F(9, 190)       =     25.36
Model | 9757.27668     9  1084.14185   Prob > F        =    0.0000
Residual | 8121.59832   190  42.7452543   R-squared       =    0.5457
Total |  17878.875   199   89.843593   Root MSE        =     6.538

----------------------------------------------------------------------
y |   Coef.  Std. Err.     t    P>|t|  [95% Conf. Interval]
-------------+--------------------------------------------------------
x |  -3.463     1.717   -2.02   0.045    -6.849      -0.077
z |  -2.902     1.670   -1.74   0.084    -6.197       0.392
c.x#c.z |   0.062     0.031    2.02   0.045     0.001       0.123
w |  -3.697     1.831   -2.02   0.045    -7.309      -0.084
c.x#c.w |   0.080     0.034    2.33   0.021     0.012       0.147
c.z#c.w |   0.070     0.033    2.09   0.038     0.004       0.135
c.x#c.z#c.w |  -0.001     0.001   -2.32   0.021    -0.003      -0.000
v1 |   0.279     0.058    4.84   0.000     0.165       0.393
v2 |  -0.906     0.691   -1.31   0.192    -2.270       0.458
_cons | 185.313    88.564    2.09   0.038    10.618     360.009
----------------------------------------------------------------------
``````

``````/* compute simple slopes */
margins, dydx(x) at(w=(\$Hw \$Lw) z=(\$Hz \$Lz)) vsquish

Average marginal effects      Number of obs = 200
Model VCE    : OLS

Expression   : Linear prediction, predict()
dy/dx w.r.t. : x
1._at        : z               =    61.75089
w               =    62.01345
2._at        : z               =    61.75089
w               =    43.27655
3._at        : z               =    41.94911
w               =    62.01345
4._at        : z               =    41.94911
w               =    43.27655

----------------------------------------------------------------
|       Delta-method
| dy/dx   Std. Err.    t    P>|t|  [95% Conf. Interval]
---------+------------------------------------------------------
x       |
_at |
1  | 0.036      0.095   0.37   0.708    -0.152       0.223
2  | 0.144      0.137   1.05   0.296    -0.127       0.414
3  | 0.498      0.188   2.64   0.009     0.127       0.870
4  | 0.093      0.124   0.75   0.453    -0.151       0.337
----------------------------------------------------------------
``````

``````margins, at(x=(20 80) w=(\$Hw \$Lw) z=(\$Hz \$Lz)) vsquish

Predictive margins     Number of obs     =  200
Model VCE    : OLS

Expression   : Linear prediction, predict()
1._at        : x               =          20
z               =    61.75089
w               =    62.01345
2._at        : x               =          20
z               =    61.75089
w               =    43.27655
3._at        : x               =          20
z               =    41.94911
w               =    62.01345
4._at        : x               =          20
z               =    41.94911
w               =    43.27655
5._at        : x               =          80
z               =    61.75089
w               =    62.01345
6._at        : x               =          80
z               =    61.75089
w               =    43.27655
7._at        : x               =          80
z               =    41.94911
w               =    62.01345
8._at        : x               =          80
z               =    41.94911
w               =    43.27655

----------------------------------------------------------------
|         Delta-method
|  Margin   Std. Err.      t    P>|t|  [95% Conf. Interval]
-----+----------------------------------------------------------
_at |
1  |  56.987      3.937    14.47   0.000    49.221      64.753
2  |  47.840      4.826     9.91   0.000    38.321      57.359
3  |  38.204      6.501     5.88   0.000    25.381      51.027
4  |  44.624      3.040    14.68   0.000    38.627      50.621
5  |  59.127      2.060    28.70   0.000    55.063      63.190
6  |  56.454      3.835    14.72   0.000    48.888      64.019
7  |  68.113      5.302    12.85   0.000    57.654      78.572
8  |  50.202      4.581    10.96   0.000    41.166      59.237
----------------------------------------------------------------

``````
``````marginsplot, recast(line) noci addplot(scatter y x, msym(oh) jitter(3))

marginsplot, recast(line) noci addplot(scatter y x, msym(oh) jitter(3))
``````

``````/* compute pairwise differences in simple slopes */
margins, dydx(x) at(w=(\$Hw \$Lw) z=(\$Hz \$Lz)) vsquish pwcompare(effects)

Pairwise comparisons of average marginal effects

Model VCE    : OLS                              Number of obs = 200

Expression   : Linear prediction, predict()
dy/dx w.r.t. : x
1._at        : z               =    61.75089
w               =    62.01345
2._at        : z               =    61.75089
w               =    43.27655
3._at        : z               =    41.94911
w               =    62.01345
4._at        : z               =    41.94911
w               =    43.27655

----------------------------------------------------------------------
|    dy/dx   Std. Err.      t    P>|t|  [95% Conf. Interval]
----------+-----------------------------------------------------------
x        |
_at |
2 vs 1  |    0.108      0.156     0.69   0.490    -0.200       0.416
3 vs 1  |    0.463      0.196     2.37   0.019     0.077       0.848
4 vs 1  |    0.057      0.156     0.37   0.713    -0.250       0.365
3 vs 2  |    0.355      0.245     1.45   0.149    -0.128       0.838
4 vs 2  |   -0.051      0.164    -0.31   0.757    -0.373       0.272
4 vs 3  |   -0.406      0.210    -1.93   0.055    -0.819       0.008
----------------------------------------------------------------------
``````

``````/* compute pairwise differences in simple slopes with Bonferroni correction */
margins, dydx(x) at(w=(\$Hw \$Lw) z=(\$Hz \$Lz)) vsquish pwcompare(effects) mcompare(bonferroni)

Pairwise comparisons of average marginal effects

Model VCE    : OLS                              Number of obs = 200

Expression   : Linear prediction, predict()
dy/dx w.r.t. : x
1._at        : z               =    61.75089
w               =    62.01345
2._at        : z               =    61.75089
w               =    43.27655
3._at        : z               =    41.94911
w               =    62.01345
4._at        : z               =    41.94911
w               =    43.27655

---------------------------
|    Number of
|  Comparisons
-------------+-------------
x            |
_at |            6
---------------------------

------------------------------------------------------------------------
| Contrast Delta-method    Bonferroni         Bonferroni
|    dy/dx   Std. Err.      t    P>|t|   [95% Conf. Interval]
-----------+------------------------------------------------------------
x          |
_at |
2 vs 1  |    0.108      0.156     0.69   1.000     -0.308       0.524
3 vs 1  |    0.463      0.196     2.37   0.114     -0.058       0.984
4 vs 1  |    0.057      0.156     0.37   1.000     -0.358       0.473
3 vs 2  |    0.355      0.245     1.45   0.894     -0.298       1.008
4 vs 2  |   -0.051      0.164    -0.31   1.000     -0.487       0.386
4 vs 3  |   -0.406      0.210    -1.93   0.328     -0.965       0.154
------------------------------------------------------------------------
``````

## 4. 参考资料

• How can I understand a 3-way continuous interaction?, UCLA FAQs, -Link-

• Dawson, J.F. & Richter, A.W. (2004) A significance test of slope differences for three-way interactions in moderated multiple regression analysis. RP0422. Aston University, Birmingham, UK.

• Dawson, J. F., A. W. Richter, 2006, Probing three-way interactions in moderated multiple regression: Development and application of a slope difference test, Journal of Applied Psychology, 91 (4): 917. -Link-, -PDF-, -cited-2000+

## 5. 相关推文

Note：产生如下推文列表的 Stata 命令为：
`lianxh 交乘 交互 边际效应`

`ssc install lianxh, replace`

## 相关课程

### 最新课程-直播课

• Note: 部分课程的资料，PPT 等可以前往 连享会-直播课 主页查看，下载。

### 关于我们

• Stata连享会 由中山大学连玉君老师团队创办，定期分享实证分析经验。
• 连享会-主页知乎专栏，700+ 推文，实证分析不再抓狂。直播间 有很多视频课程，可以随时观看。
• 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标，输入简要关键词，以便快速呈现历史推文，获取工具软件和数据下载。常见关键词：`课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法`

✏ 连享会-常见问题解答：
https://gitee.com/lianxh/Course/wikis

New！ `lianxh` 命令发布了：

`. ssc install lianxh`

`. help lianxh`