【问题标题】:How to plot the standard error of linear combination of two variables in Stata如何在Stata中绘制两个变量线性组合的标准误差
【发布时间】:2014-08-06 15:43:31
【问题描述】:

我正在尝试在 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,它将为您提供三个协变量以包含在模型中,weight1weight2weight3

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

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

【问题讨论】:

    标签: plot stata linear-regression confidence-interval


    【解决方案1】:

    这可能会奏效:

    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。

    【讨论】:

    • 感谢您的回复! margins 命令仅适用于整数,而样条曲线将为您提供非整数值。因此,不幸的是,我认为这个巧妙的技巧不适用于样条曲线。我认为我需要使用方差协方差矩阵来执行此操作,但不确定如何在 Stata 中调用/使用它。
    • @Ashley 我编辑了代码来处理非整数问题。您应该使用该约束更新原始问题。
    • 谢谢你,@Dimitriy。我编辑了我的原始帖子以指定样条曲线为您提供非整数值。我考虑过使用后估计命令,但正在为其他协变量调整我的模型,所以我实际上只想要我的样条曲线的部分预测,这样我就可以只为样条曲线和 95% CI 绘制图形。因此,使用predict se, stdp 可以为您提供整个模型的预测,而不仅仅是样条曲​​线。有什么想法吗?
    • 您必须将剩余的变量固定为某个常数;预测“仅用于样条曲线”意味着将剩余系数设置为 0,这可能没有意义。您可能会在这里得到一些启发:maartenbuis.nl/wp/inter_quadr/inter_quadr.html
    猜你喜欢
    • 2018-10-08
    • 2015-07-03
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 2015-11-20
    相关资源
    最近更新 更多