我运行一个简单的回归并找到如下所示的拟合值:
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)
这会为我简化这个过程吗?
最佳答案
编辑 2015-03-29:对交互的一个子集使用原始方法,但保留其他方法
原始方法的一大优点是它可以处理任何复杂性的交互。主要缺陷是它不会忽略您想要保留在模型中的交互。但如果您使用 xi
创建它们,#
将不会出现在它们的名称中。
sysuse auto, clear
recode rep78 1 = 2 //combine small categories
xi, prefix("") i.rep78*mpg // mpg*i.rep78 won't work
des _I*
reg price mpg foreign c.mpg#foreign _I* 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 fit_sans_mpgXforeign = betas
编辑于2015-03-28
不需要 xi
前缀,因此,例如,这适用于 Stata 13。
sysuse auto, clear
gen intx = c.mpg#foreign
reg price mpg foreign i.rep78 headroom trunk intx
predict mhat
gen fitted_sans_interaction = mhat -_b[intx]*intx
上一个答案
sysuse auto, clear
xi: gen intx = c.mpg#foreign
reg price mpg foreign i.rep78 headroom trunk intx
predict mhat
gen fitted_sans_interaction = mhat -_b[intx]*intx
甚至
sysuse auto, clear
xi: gen intx = c.mpg#foreign
reg price c.mpg##foreign i.rep78 headroom trunk intx
predict mhat
gen fitted_sans_interaction = mhat -_b[intx]*intx
我已经提供了您的示例中省略的外国的主要效果。
关于regression - 如何使用回归系数的子集代替所有系数创建拟合值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29311305/