这可能会奏效:
set more off
sysuse auto, clear
mkspline weight 3 = weight, pctile
reg price weight?
/* Manual Way That Will Also Work With Non-Integer Splined Variable */
predict yhat
predict se, stdp
gen lb = yhat - 1.96*se
gen ub = yhat + 1.96*se
tw (line yhat weight, sort lpatter(dash)) (rarea lb ub weight, sort fcolor(none))
/* Margins Way for Integer Splined Variable */
margins, over(weight)
/* Compare the Two In A Graph */
marginsplot, recast(line) recastci(rarea) addplot(line yhat weight, sort lpatter(dash) || rarea lb ub weight, sort fcolor(none))
margins, over(weight) 为您提供每个权重值的预测价格,marginsplot 为您提供图表中 95%CI 的预测。 recasts 让 CI 在我看来更好看。
以下是我获得部分效果的方法。它使用在预测然后恢复数据之前将所有其他变量设置为零的技巧。
set more off
sysuse auto, clear
mkspline weight 3 = weight, pctile
gen constant = 1
reg price constant weight? i.foreign c.mpg, nocons
/* check one point using lincom */
list weight* if weight == 4840
lincom _b[weight1]*2640 + _b[weight2]*760 + _b[weight3]*1440
preserve
replace constant = 0
replace foreign = 0
replace mpg = 0
predict yhat
predict se, stdp
gen lb = yhat - invttail(`e(df_r)',0.025)*se
gen ub = yhat + invttail(`e(df_r)',0.025)*se
/* confirm that predict matches lincom's output for one point */
list yhat lb ub if weight == 4840
tw (line yhat weight, sort lpatter(dash)) (rarea lb ub weight, sort fcolor(none))
restore
我使用了基于 t 分布的临界值,而不是上面使用的通常的 1.96。