【发布时间】:2015-06-01 09:47:43
【问题描述】:
我运行一个简单的回归并找到这样的拟合值:
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
-------------------------------------------------------------------------------
出于反事实的目的(在时间序列中尤其重要),我可能想使用 this 回归中的系数子集找到拟合值。例如,我可能想使用 this 回归中的所有系数来查找拟合值,但 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)
这会为我简化这个过程吗?
【问题讨论】:
标签: regression stata