【问题标题】:Stata - How to get residuals for original equation using estimates for differenced equationStata - 如何使用差分方程的估计值获得原始方程的残差
【发布时间】:2016-10-22 08:43:57
【问题描述】:

我有变量yx1x2。我使用reg d.(y x1 x2), nocons 估计了一个差分方程(没有截距)。现在我想使用估计的系数获得原始变量的残差。我可以这样做

reg d.(y x1 x2), nocons
matrix b = e(b)
gen resid = y - b[1,1]*x1 - b[1,2]*x2

但是有更简单的方法吗?我需要保留这些生成的残差以供将来使用。这是一个完整的最小示例。

clear all
set obs 100
gen id = floor((_n-1)/5)+1
by id, sort: gen year = 1990+_n
xtset id year
set seed 1
gen x1 = rnormal()
gen x2 = rnormal()
gen y = rnormal()
*** Data generated ***
reg d.(y x1 x2), nocons
matrix b = e(b)
gen resid = y - b[1,1]*x1 - b[1,2]*x2

我想知道是否有一种灵活的方法,因为有时我想完全更改回归的变量名称(例如,reg dy dx1 dx2, nocons 而不仅仅是reg d.(y x1 x2))。我想predict 可能会有所帮助,但我不知道。是否可以避免显式输入变量名?

【问题讨论】:

    标签: stata


    【解决方案1】:

    predict 将不起作用,因为它会在不同的尺度上产生残差。您需要原始 y 的残差,这是不寻常的,因此没有现成的解决方案。

    我认为最简单的方法是这样做:

    reg d.(y x1 x2), nocons coefl
    local vars:colnames e(b) // get a list of coefficients
    foreach x of local vars {
        local xvar = subinstr("`x'","D.","",1) // strip out the D. prefix from the coefficient names
        local diff "`diff' - _b[`x']*`xvar'"
    }
    gen resid = y `diff'
    

    如果你有像 dx1 和 dx2 这样的协变量,你可以像这样修改前缀提示器:

    local xvar = subinstr("`x'","d","",1) // strip out the first d prefix
    

    【讨论】:

      猜你喜欢
      • 2018-08-17
      • 2019-03-28
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      • 1970-01-01
      • 2018-06-05
      • 2022-01-21
      • 2017-05-20
      相关资源
      最近更新 更多