Plot 如何在Stata中绘制两个变量线性组合的标准误差

Plot 如何在Stata中绘制两个变量线性组合的标准误差,plot,stata,linear-regression,confidence-interval,Plot,Stata,Linear Regression,Confidence Interval,我试图在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,它将为您提供三个协变量,以包括在模型中,即权重1、权重2和权重3 此示例使用calcspl样条线生成 re

我试图在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,它将为您提供三个协变量,以包括在模型中,即权重1、权重2和权重3

此示例使用calcspl样条线生成

regress mpg weight weight_1 foreign price

gen yfit = _b[weight]*weight + _b[weight_1]*weight_1

sort weight

twoway line yfit weight

请注意,样条曲线为您提供了非整数值,因此您不能使用“边距”命令,并且我的模型中还有其他协变量,因此我不能使用正常的后估计命令

这可能会奏效:

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))
边际,超重给你们一个预测价格在每一个价值的重量和边际曲线图给你们这些预测与95%置信区间在一个绘图。重铸使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。

感谢您的回复!但是,“边距”命令仅适用于整数,样条曲线将提供非整数值。因此,不幸的是,我不认为这种巧妙的技巧对样条曲线不起作用。我想我需要用方差-协方差矩阵来做这件事,但不确定如何在Stata中调用/使用它。@Ashley我编辑了代码来处理非整数问题。你应该用这个约束条件更新原始问题。谢谢你,@dimitry。我编辑了我的原始帖子,以指定样条线为您提供非整数值。我考虑过使用后估计命令,但我正在调整模型中的其他协变量,所以我实际上只需要对样条曲线进行部分预测,这样我就可以绘制样条曲线和95%CI的图形。因此,使用predict se,stdp可以为整个模型提供预测,而不仅仅是样条曲线。有什么想法吗?你必须把剩下的变量固定在某个常数上;仅对样条曲线进行预测意味着将剩余系数设置为0,这可能没有意义。你可以从这里得到一些灵感:
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