【问题标题】:Prediction after Boxcox model in StataStata中Boxcox模型后的预测
【发布时间】:2014-08-31 21:08:56
【问题描述】:

我正在尝试使用 Stata manual(第 5 页)中描述的步骤将 Stata 13 中 boxcox 之后的预测选项与我的代码匹配。

以下是我使用的示例代码:

sysuse auto,clear
local indepvar weight foreign length  
qui boxcox price `indepvar' ,model(lhsonly)lrtest
qui predict yhat1 
qui predict resid1, residuals


//yhat2 and resid2 computed using the procedure described in Stata manual
set more off
set type double
mat coef=e(b)
local nosvar=colsof(coef)-2

qui gen constant=1
local varname weight foreign length  constant
local coefname weight foreign length  _cons

//step 1: compute residuals first
forvalues k = 1/`nosvar'{
local varname1 : word `k' of `varname'
local coefname1 : word `k' of `coefname'
qui gen xb`varname1'=`varname1'*_b[`coefname1']
}
qui egen xb=rowtotal(xb*)
qui gen resid=(price^(_b[theta:_cons]))-xb

//step 2: compute predicted value

qui gen yhat2=.
local noobs=_N
local theta=_b[theta:_cons]
forvalues j=1/`noobs'{
qui gen temp`j'=.
forvalues i=1/`noobs'{
qui replace temp`j'=((`theta'*(xb[`j']+resid[`i']))+1)^(1/`theta') if _n==`i'
}
qui sum temp`j'
local tempmean`j'=r(mean)
qui replace yhat2=`tempmean`j'' if _n==`j'
drop temp`j' 
}
drop resid
qui gen double resid2=price-yhat2



 sum yhat* resid*

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
       yhat1 |        74    6254.224    2705.175   3428.361   21982.45
       yhat2 |        74    1.000035    8.13e-06   1.000015   1.000054
      resid1 |        74   -88.96723    2094.162  -10485.45   6980.013
      resid2 |        74    6164.257    2949.496       3290      15905

注意:yhat1 和 resid1 是基于 Stata predict,而 yhat2 和 resid2 是基于我的示例代码。需要进行比较以确保我计算的边际效应是正确的(margins 不会计算 boxcox 之后的边际效应)。

【问题讨论】:

    标签: stata


    【解决方案1】:

    您对第一个残差的定义是错误的,因为您错过了手册第 3 页上 y^(\lambda) 的定义。另请参阅 boxcox 本身的手册条目中的方法和公式部分。

    翻译成你的问题,在行

     qui gen resid=(price^(_b[theta:_cons]))-xb
    

    术语

     price^(_b[theta:_cons])
    

    应该是:

     (price^(_b[theta:_cons])-1)/_b[theta:_cons]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-10
      • 2020-10-01
      • 2018-04-15
      • 2018-11-07
      • 1970-01-01
      • 2016-07-23
      • 2016-06-11
      • 1970-01-01
      相关资源
      最近更新 更多