我有两组变量来解释结果(运动和大小),我有兴趣发现预测结果的哪一部分属于哪一组。 local motor
可以包含1个或多个变量。为此,我需要一个代码,该代码可以了解motor
中的哪些变量,并将它们的影响添加到一个名为predict_motor_smart
的变量中。在此示例中,predict_motor_smart
应等于predict_motor
。从本质上讲,我需要为代码的最后一行找到一个表达式,该表达式对于更改local motor
变量的规范具有鲁棒性。
sysuse auto, clear
local outcome "price"
local motor "mpg rep78"
local size "weight length"
regress `outcome' `motor' `size'
gen predict_motor = _b[_cons] + mpg * _b[mpg] + rep78*_b[rep78]
* gen predict_motor_smart = _b[_cons] + var1 of motor * beta 1 of motor + var2 of motor * beta2 of motor
sysuse auto, clear
local outcome "price"
local motor "mpg rep78"
local size "weight length"
regress `outcome' `motor' `size'
predict outcome_predict
gen predict_motor = .
replace predict_motor = _b[_cons] + mpg * _b[mpg] + rep78*_b[rep78] if
outcome_predict!=.
gen predict_motor_smart =.
replace predict_motor_smart = _b[_cons] if outcome_predict!=.
foreach l of local circumstances{
replace predict_motor_smart = predict_motor_smart +`l'*_b[`l']
}