我运行一个简单的回归,找到这样的拟合值:
sysuse auto, clear
reg price mpg c.mpg#foreign i.rep78 headroom trunk
predict fitted_price, xb
这给了我这些系数:
-------------------------------------------------------------------------------
price | Coef. Std. Err. t P>|t| [95% Conf. Interval]
--------------+----------------------------------------------------------------
mpg | -306.1891 77.01548 -3.98 0.000 -460.243 -152.1352
|
foreign#c.mpg |
Foreign | 60.58403 37.24129 1.63 0.109 -13.90964 135.0777
|
rep78 |
2 | 999.7779 2150.269 0.46 0.644 -3301.4 5300.956
3 | 1200.741 2001.853 0.60 0.551 -2803.561 5205.043
4 | 1032.778 2070.513 0.50 0.620 -3108.864 5174.42
5 | 2081.128 2200.998 0.95 0.348 -2321.523 6483.779
|
headroom | -611.7201 502.3401 -1.22 0.228 -1616.55 393.1097
trunk | 134.4143 110.8262 1.21 0.230 -87.27118 356.0998
_cons | 10922.46 2803.271 3.90 0.000 5315.082 16529.84
-------------------------------------------------------------------------------
出于反事实的目的(在时间序列中尤其重要),我可能想要使用该回归中的系数子集来找到拟合值 . 例如,我可能想要使用该回归中的所有系数找到拟合值,除了来自 mpg
和 foreign
之间的相互作用的系数,即 c.mpg#foreign
. (请注意,这与仅在没有交互的情况下再次运行回归不同,因为这将产生不同的系数) .
截至目前,我这样做:
sysuse auto, clear
reg price mpg c.mpg#foreign i.rep78 headroom trunk
matrix betas = e(b)
local names: colnames betas
foreach name of local names {
if strpos("`name'", "#") > 0 {
scalar define col_idx = colnumb(betas, "`name'")
matrix betas[1, col_idx] = 0
}
}
matrix score fitted_price_no_interact = betas
这不是一个强大的解决方案,因为它依赖于系数矩阵的列名称中的 #
的命名约定,如果我想要包含一组交互而不是另一组交互,则会分解 . 我可以通过手动指定名称来为特定的回归编写类似的代码,但如果我更改回归,则必须手动更改代码 .
是否有更强大的方法来做到这一点,例如
predict fitted_price, xb exclude(c.mpg#foreign trunk)
这会简化这个过程吗?
1 回答
Edit 2015-03-29: Use the original method on one subset of interactions, but retain others
原始方法的一大优点是它可以处理任何复杂性的交互 . 主要缺陷是它不会忽略您希望保留在模型中的交互 . 但是如果使用
xi
来创建它们,#
将不会出现在它们的名称中 .Edit 2015-03-28
不需要
xi
前缀,因此,例如,这适用于Stata 13 .Previous Answer
甚至
我提供了外部的主要效果,在您的示例中省略了 .