# 交乘项专题：主效应项可以忽略吗？

Stata 连享会   主页 || 视频 || 推文

``````regress y x1 x2 x1#x2
``````

• 类别变量相互交乘：可以去掉主效应项，但系数含义不同。

• 类别变量与连续型变量相互交乘：（1）可以去掉连续型变量主效应项，但系数含义发生改变；（2）一般情况下，不可以去掉类别变量主效应项

• 连续型变量与连续型变量相互交乘：一般情况下，不可以去掉主效应项

## 1. 实例 1：类别变量相互交乘

categorical by categorical interaction

``````. use https://stats.idre.ucla.edu/stat/data/hsbanova, clear
(highschool and beyond (200 cases))
``````
``````. d

variable name   type    format     label      variable label
-----------------------------------------------------------------------------------------------------------------------
id              float   %9.0g
female          float   %9.0g      fl
write           float   %9.0g                 writing score
math            float   %9.0g                 math score
science         float   %9.0g                 science score
socst           float   %9.0g                 social studies score
honors          float   %19.0g     honlab     honors english
grp             float   %9.0g      grp
-----------------------------------------------------------------------------------------------------------------------
Sorted by:
``````

``````. list in 1/10

+----------------------------------------------------------------------------+
|  id   female   read   write   math   science   socst         honors    grp |
|----------------------------------------------------------------------------|
1. |  45   female     34      35     41        29      26   not enrolled   grp1 |
2. | 108     male     34      33     41        36      36   not enrolled   grp2 |
3. |  15     male     39      39     44        26      42   not enrolled   grp1 |
4. |  67     male     37      37     42        33      32   not enrolled   grp1 |
5. | 153     male     39      31     40        39      51   not enrolled   grp1 |
|----------------------------------------------------------------------------|
6. |  51   female     42      36     42        31      39   not enrolled   grp2 |
7. | 164     male     31      36     46        39      46   not enrolled   grp1 |
8. | 133     male     50      31     40        34      31   not enrolled   grp1 |
9. |   2   female     39      41     33        42      41   not enrolled   grp1 |
10. |  53     male     34      37     46        39      31   not enrolled   grp1 |
+----------------------------------------------------------------------------+

``````

• 完整模型
``````. regress write i.female##i.grp

Source |       SS           df       MS      Number of obs   =       200
-------------+----------------------------------   F(7, 192)       =     11.05
Model |  5135.17494         7   733.59642   Prob > F        =    0.0000
Residual |  12743.7001       192  66.3734378   R-squared       =    0.2872
Total |   17878.875       199   89.843593   Root MSE        =     8.147

------------------------------------------------------------------------------
write |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
female |
female  |   9.136876   2.311726     3.95   0.000     4.577236    13.69652
|
grp |
grp2  |    7.31677   2.458951     2.98   0.003     2.466743     12.1668
grp3  |   10.10248   2.292658     4.41   0.000     5.580454    14.62452
grp4  |   16.75286   2.525696     6.63   0.000     11.77119    21.73453
|
female#grp |
female#grp2  |  -5.029733   3.357123    -1.50   0.136    -11.65131    1.591845
female#grp3  |  -3.721697   3.128694    -1.19   0.236    -9.892723    2.449328
female#grp4  |  -9.831208   3.374943    -2.91   0.004    -16.48793   -3.174482
|
_cons |   41.82609   1.698765    24.62   0.000     38.47545    45.17672
------------------------------------------------------------------------------
``````

female == 1, group == 2 为例，group 2 的女性的写作水平为 41.83 + 9.14 + 7.31 - 5.03 = 53.25。

``````| female  | group | _cons |         |         |         | write |
|---------|-------|-------|---------|---------|---------|-------|
| 0       | 1     | 41.83 |         |         |         | 41.83 |
|         |       |       |         |         |         |       |
| 1       | 1     |       | + 9.14  |         |         | 50.97 |
|         |       |       |         |         |         |       |
| 0       | 2     |       | + 7.31  |         |         | 49.14 |
| 0       | 3     |       | + 10.10 |         |         | 51.93 |
| 0       | 4     |       | + 16.75 |         |         | 58.58 |
|         |       |       |         |         |         |       |
| 1       | 2     |       | + 9.14  | + 7.31  | - 5.03  | 53.25 |
| 1       | 3     |       | + 9.14  | + 10.10 | - 3.72  | 57.35 |
| 1       | 4     |       | + 9.14  | + 16.75 | - 9.83  | 57.89 |
``````

``````. margins female##grp
------------------------------------------------------------------------------
|     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
female#grp |
male#grp1  |   41.82609   1.698765    24.62   0.000     38.47545    45.17672
male#grp2  |   49.14286   1.777819    27.64   0.000     45.63629    52.64942
male#grp3  |   51.92857   1.539636    33.73   0.000      48.8918    54.96534
male#grp4  |   58.57895   1.869048    31.34   0.000     54.89244    62.26545
female#grp1  |   50.96296   1.567889    32.50   0.000     47.87046    54.05546
female#grp2  |      53.25   1.662997    32.02   0.000     49.96991    56.53009
female#grp3  |   57.34375   1.440198    39.82   0.000     54.50311    60.18439
female#grp4  |   57.88462   1.597756    36.23   0.000     54.73321    61.03602
-----------------------------------------------------------------------
``````
• 模型 2：去掉主效应项 female

``````. regress write i.grp i.female#i.grp

Source |       SS           df       MS      Number of obs   =       200
-------------+----------------------------------   F(7, 192)       =     11.05
Model |  5135.17494         7   733.59642   Prob > F        =    0.0000
Residual |  12743.7001       192  66.3734378   R-squared       =    0.2872
Total |   17878.875       199   89.843593   Root MSE        =     8.147

------------------------------------------------------------------------------
write |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
grp |
grp2  |    7.31677   2.458951     2.98   0.003     2.466743     12.1668
grp3  |   10.10248   2.292658     4.41   0.000     5.580454    14.62452
grp4  |   16.75286   2.525696     6.63   0.000     11.77119    21.73453
|
female#grp |
female#grp1  |   9.136876   2.311726     3.95   0.000     4.577236    13.69652
female#grp2  |   4.107143   2.434379     1.69   0.093    -.6944172    8.908703
female#grp3  |   5.415179   2.108234     2.57   0.011     1.256906    9.573452
female#grp4  |   -.694332   2.458895    -0.28   0.778    -5.544247    4.155583
|
_cons |   41.82609   1.698765    24.62   0.000     38.47545    45.17672
------------------------------------------------------------------------------
``````

（1） 组内性别差异 （within-group gender difference） group 1, group 2 以及 group 3 的女性的写作水平显著高于同组男性的写作水平。group 4 各成员的写作水平并不存在性别层面上的显著差异。该信息由模型 2 给出。

（2）组间性别差异之差 （across-group difference of gender difference） 以 group 1 组内成员写作水平的性别差异为基准，group 2 和 group 3 组内成员写作水平的性别差异并无显著差异。然而，group 4 组内成员写作水平的性别差异显著低于基准组。

• 模型 3：去掉主效应项 `grp`
``````. regress write i.female i.female#i.grp

Source |       SS           df       MS      Number of obs   =       200
-------------+----------------------------------   F(7, 192)       =     11.05
Model |  5135.17494         7   733.59642   Prob > F        =    0.0000
Residual |  12743.7001       192  66.3734378   R-squared       =    0.2872
Total |   17878.875       199   89.843593   Root MSE        =     8.147

------------------------------------------------------------------------------
write |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
female |
female  |   9.136876   2.311726     3.95   0.000     4.577236    13.69652
|
female#grp |
male#grp2  |    7.31677   2.458951     2.98   0.003     2.466743     12.1668
male#grp3  |   10.10248   2.292658     4.41   0.000     5.580454    14.62452
male#grp4  |   16.75286   2.525696     6.63   0.000     11.77119    21.73453
female#grp2  |   2.287037   2.285571     1.00   0.318    -2.221015     6.79509
female#grp3  |   6.380787   2.128954     3.00   0.003     2.181646    10.57993
female#grp4  |   6.921652   2.238549     3.09   0.002     2.506347    11.33696
|
_cons |   41.82609   1.698765    24.62   0.000     38.47545    45.17672
------------------------------------------------------------------------------
``````

• 模型 4：只保留交乘项
``````.  regress write i.female#i.grp

Source |       SS           df       MS      Number of obs   =       200
-------------+----------------------------------   F(7, 192)       =     11.05
Model |  5135.17494         7   733.59642   Prob > F        =    0.0000
Residual |  12743.7001       192  66.3734378   R-squared       =    0.2872
Total |   17878.875       199   89.843593   Root MSE        =     8.147

------------------------------------------------------------------------------
write |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
female#grp |
male#grp2  |    7.31677   2.458951     2.98   0.003     2.466743     12.1668
male#grp3  |   10.10248   2.292658     4.41   0.000     5.580454    14.62452
male#grp4  |   16.75286   2.525696     6.63   0.000     11.77119    21.73453
female#grp1  |   9.136876   2.311726     3.95   0.000     4.577236    13.69652
female#grp2  |   11.42391   2.377259     4.81   0.000     6.735015    16.11281
female#grp3  |   15.51766   2.227099     6.97   0.000     11.12494    19.91039
female#grp4  |   16.05853   2.332086     6.89   0.000     11.45873    20.65833
|
_cons |   41.82609   1.698765    24.62   0.000     38.47545    45.17672
------------------------------------------------------------------------------

``````

## 2. 实例 2：类别变量与连续变量交乘

categorical by continuous interaction

• 完整模型
``````.  regress write i.female##c.socst

Source |       SS           df       MS      Number of obs   =       200
-------------+----------------------------------   F(3, 196)       =     49.26
Model |  7685.43528         3  2561.81176   Prob > F        =    0.0000
Residual |  10193.4397       196  52.0073455   R-squared       =    0.4299
Total |   17878.875       199   89.843593   Root MSE        =    7.2116

--------------------------------------------------------------------------------
write |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
---------------+----------------------------------------------------------------
female |
female  |   15.00001    5.09795     2.94   0.004     4.946132    25.05389
socst |   .6247968   .0670709     9.32   0.000     .4925236    .7570701
|
female#c.socst |
female  |  -.2047288   .0953726    -2.15   0.033    -.3928171   -.0166405
|
_cons |    17.7619   3.554993     5.00   0.000     10.75095    24.77284
--------------------------------------------------------------------------------

``````

**交乘项的系数报告了不同组别斜率之差。**其系数为 -0.205，说明女性组别 writingsocst 做回归的系数为 0.625 - 0.205 = 0.420。

• 模型 2：去掉主效应项 `c.socst`
``````. reg write i.female i.female#c.socst

Source |       SS           df       MS      Number of obs   =       200
-------------+----------------------------------   F(3, 196)       =     49.26
Model |  7685.43528         3  2561.81176   Prob > F        =    0.0000
Residual |  10193.4397       196  52.0073455   R-squared       =    0.4299
Total |   17878.875       199   89.843593   Root MSE        =    7.2116

--------------------------------------------------------------------------------
write |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
---------------+----------------------------------------------------------------
female |
female  |   15.00001    5.09795     2.94   0.004     4.946132    25.05389
|
female#c.socst |
male  |   .6247968   .0670709     9.32   0.000     .4925236    .7570701
female  |    .420068   .0678044     6.20   0.000     .2863482    .5537878
|
_cons |    17.7619   3.554993     5.00   0.000     10.75095    24.77284
--------------------------------------------------------------------------------
``````

• 模型 3：去掉主效应项 `i.female`
``````. reg write socst i.female#c.socst

Source |       SS           df       MS      Number of obs   =       200
-------------+----------------------------------   F(2, 197)       =     66.96
Model |  7235.18229         2  3617.59115   Prob > F        =    0.0000
Residual |  10643.6927       197   54.028897   R-squared       =    0.4047
Total |   17878.875       199   89.843593   Root MSE        =    7.3504

--------------------------------------------------------------------------------
write |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
---------------+----------------------------------------------------------------
socst |   .4903271   .0500357     9.80   0.000     .3916528    .5890014
|
female#c.socst |
female  |   .0701563   .0195532     3.59   0.000     .0315957    .1087168
|
_cons |    25.0561   2.597064     9.65   0.000     19.93449    30.17772
--------------------------------------------------------------------------------

``````

``````. reg write socst i.female#c.socst
. margins, at(female=(0 1) socst = 0) noatlegend

Adjusted predictions                            Number of obs     =        200
Model VCE    : OLS

Expression   : Linear prediction, predict()

------------------------------------------------------------------------------
|            Delta-method
|     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
_at |
1  |    25.0561   2.597064     9.65   0.000     19.93449    30.17772
2  |    25.0561   2.597064     9.65   0.000     19.93449    30.17772
------------------------------------------------------------------------------
``````

``````. margins, dydx(socst) at(female=(0 1)) noatlegend post

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

Expression   : Linear prediction, predict()
dy/dx w.r.t. : socst

------------------------------------------------------------------------------
|            Delta-method
|      dy/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
socst        |
_at |
1  |   .4903271   .0500357     9.80   0.000     .3916528    .5890014
2  |   .5604834    .049094    11.42   0.000      .463666    .6573007
------------------------------------------------------------------------------
``````

``````reg write socst i.female#c.socst
qui margins female, at(socst=(5(5)70))
marginsplot, recast(line) noci addplot(scatter y x,jitter(3) msym(oh))

``````
• 模型 4：只保留交互项
``````. reg write i.female#c.socst

Source |       SS           df       MS      Number of obs   =       200
-------------+----------------------------------   F(2, 197)       =     66.96
Model |  7235.18229         2  3617.59115   Prob > F        =    0.0000
Residual |  10643.6927       197   54.028897   R-squared       =    0.4047
Total |   17878.875       199   89.843593   Root MSE        =    7.3504

--------------------------------------------------------------------------------
write |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
---------------+----------------------------------------------------------------
female#c.socst |
male  |   .4903271   .0500357     9.80   0.000     .3916528    .5890014
female  |   .5604834    .049094    11.42   0.000      .463666    .6573007
|
_cons |    25.0561   2.597064     9.65   0.000     19.93449    30.17772
--------------------------------------------------------------------------------

``````

## 3. 实例 3：连续型变量相互交乘

Continuous by Continuous Interaction

• 完整模型
``````. reg write c.math##c.socst

Source |       SS           df       MS      Number of obs   =       200
-------------+----------------------------------   F(3, 196)       =     61.55
Model |  8672.71872         3  2890.90624   Prob > F        =    0.0000
Residual |  9206.15628       196  46.9701851   R-squared       =    0.4851
Total |   17878.875       199   89.843593   Root MSE        =    6.8535

--------------------------------------------------------------------------------
write |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
---------------+----------------------------------------------------------------
math |   .6107585   .2871688     2.13   0.035      .044421    1.177096
socst |   .5206108   .2675933     1.95   0.053     -.007121    1.048343
|
c.math#c.socst |  -.0036057   .0051493    -0.70   0.485    -.0137609    .0065494
|
_cons |   3.483233   14.32252     0.24   0.808     -24.7628    31.72927
--------------------------------------------------------------------------------

``````

``````reg write c.math##c.socst
margins, at(math=(30 75) socst=(30(5)70)) vsquish
marginsplot, noci x(math) recast(line)
``````
• 模型 2：去掉主效应项 `c.math`

``````. reg write c.socst##c.math

Source |       SS           df       MS      Number of obs   =       200
-------------+----------------------------------   F(3, 196)       =     61.55
Model |  8672.71872         3  2890.90624   Prob > F        =    0.0000
Residual |  9206.15628       196  46.9701851   R-squared       =    0.4851
Total |   17878.875       199   89.843593   Root MSE        =    6.8535

--------------------------------------------------------------------------------
write |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
---------------+----------------------------------------------------------------
socst |   .5206108   .2675933     1.95   0.053     -.007121    1.048343
math |   .6107585   .2871688     2.13   0.035      .044421    1.177096
|
c.socst#c.math |  -.0036057   .0051493    -0.70   0.485    -.0137609    .0065494
|
_cons |   3.483233   14.32252     0.24   0.808     -24.7628    31.72927
--------------------------------------------------------------------------------
``````

## 4. 总结

``````use https://stats.idre.ucla.edu/stat/data/hsbanova, clear \\数据引入

*- 类别变量相互交乘
regress write i.female##i.grp \\完整模型
margins female##grp

regress write i.grp i.female#i.grp \\模型 2：去掉主效应项 female
*无模型设定问题，但系数含义改变*

regress write i.female i.female#i.grp \\模型 3：去掉主效应项 grp
*无模型设定问题，但系数含义改变*

regress write i.female#i.grp \\模型 4：只保留交乘项
*无模型设定问题，但系数含义改变*

*- 类别变量与连续型变量相互交乘
regress write i.female##c.socst \\完整模型

regress write i.female i.female#c.socst \\模型 2：去掉主效应项 c.socst
*无模型设定问题，但系数含义改变*

reg write socst i.female#c.socst \\模型 3：去掉主效应项 i.female
*可能存在模型设定问题*
margins, at(female=(0 1) socst = 0) noatlegend
margins, dydx(socst) at(female=(0 1)) noatlegend post
reg write socst i.female#c.socst
qui margins female, at(socst=(5(5)70))
marginsplot, recast(line) noci addplot(scatter y x,jitter(3) msym(oh))

reg write i.female#c.socst \\模型 4：只保留交乘项
*可能存在模型设定问题*

*- 连续型变量与连续型变量相互交乘
reg write c.math##c.socst \\完整模型
margins, at(math=(30 75) socst=(30(5)70)) vsquish
marginsplot, noci x(math) recast(line)

reg write c.socst##c.math \\模型 2：去掉主效应项
*可能存在模型设定问题*
``````

## 相关课程

http://lianxh.duanshu.com

### 课程一览

Stata数据清洗 游万海 直播, 2 小时，已上线

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

#### 关于我们

• Stata连享会 由中山大学连玉君老师团队创办，定期分享实证分析经验。直播间 有很多视频课程，可以随时观看。
• 连享会-主页知乎专栏，300+ 推文，实证分析不再抓狂。
• 公众号推文分类： 计量专题 | 分类推文 | 资源工具。推文分成 内生性 | 空间计量 | 时序面板 | 结果输出 | 交乘调节 五类，主流方法介绍一目了然：DID, RDD, IV, GMM, FE, Probit 等。
• 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标，输入简要关键词，以便快速呈现历史推文，获取工具软件和数据下载。常见关键词：`课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法`

✏ 连享会学习群-常见问题解答汇总：
https://gitee.com/arlionn/WD