我试图在Stata中绘制样条函数的95%CI。我可以很容易地绘制拟合值,但我不知道如何计算SE。有人可以帮忙吗?
sysuse auto.dta, clear
centile weight, centile(10 50 90)
calcspl weight, nknots(3) knotsat(r(c_1) r(c_2) r(c_3))
或者您可以使用其他样条线程序,例如mkspline
,它会为您提供三个协方差,以包含在模型weight1
,weight2
和weight3
mkspline weight 3 = weight, pctile
此示例使用calcspl
样条线生成
regress mpg weight weight_1 foreign price
gen yfit = _b[weight]*weight + _b[weight_1]*weight_1
sort weight
twoway line yfit weight
请注意,样条线为您提供非整数值,因此您无法使用margins命令,并且我的模型中还有其他协变量,因此我无法使用正常的后估计命令。
答案 0 :(得分:0)
这可能会起到作用:
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的预测值。 recast
使我的眼睛看起来更漂亮。
这是我如何获得部分效果。它使用hack将所有其他变量设置为零,然后再预测并恢复数据。
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。