📄 xtregar.ado
字号:
*! version 1.2.15 20dec2004
program define xtregar, eclass sortpreserve byable(recall)
version 7, missing
if replay() {
if `"`e(cmd)'"'!="xtregar" { error 301 }
if _by() { error 190 }
syntax [, Level(cilevel) LBI LBIC ]
if "`lbi'" != "" & "`e(wtype)'"!= "" {
di as error "lbi cannot be specified with weights"
exit 198
}
if "`e(model)'"=="fe" {
if "`lbi'" != "" {
est local dw "lbi"
est local LBI ""
}
DispFE `level'
}
else {
if "`lbi'" != "" {
est local dw "lbi"
est local LBI ""
}
DispRE `level'
}
exit
}
syntax varlist(ts) [if] [in] [fw aw],[ RE FE RHOType(string) /*
*/ TWOstep RHOF(real -2 ) SIGEF(real 0) SIGUF(real 0) /*
*/ LBI Level(cilevel) ]
marksample touse
qui sum `touse'
if `r(sum)'==0 {
error 2000
}
if "`weight'" != "" {
local weights "weights"
if "`lbi'" != "" {
di as error "lbi cannot be specified with weights"
exit 198
}
}
else {
local weights ""
}
/* check that data is tsset */
capture tsset
capture local id "`r(panelvar)'"
if "`id'"=="" {
di as err "must tsset data and specify panelvar"
exit 459
}
capture local t "`r(timevar)'"
if "`t'" == "" {
di as err "must tsset data and specify timevar"
exit 459
}
if "`re'" !="" & "`fe'" != "" {
di as error "specify re or fe, not both"
exit 198
}
/* only allow weights with fe model */
if "`weight'" != "" & "`fe'" == "" {
di as error "weights only allowed with fe estimator"
exit 198
}
markout `touse' `id' `t'
tsrevar `varlist', list
local tvlist "`r(varlist)'"
foreach var of local tvlist {
if "`var'" == "`t'" {
di as err "time variable may not be included in varlist"
exit 198
}
}
tokenize `varlist'
local depvar "`1'"
local depname "`depvar'"
local depname_o "`depvar'"
tsrevar `depvar', substitute
local depvar `r(varlist)'
macro shift
_rmcoll `*'
local indepsn "`r(varlist)'"
local indepsn_o `indepsn'
tsrevar `indepsn', substitute
local varlist "`depvar' `r(varlist)'"
tokenize `varlist'
macro shift
local indeps "`*'"
tempname rho_f
rtparse ,`rhotype' rhof(`rhof')
local rhot "`r(type)'"
scalar `rho_f'=r(rho_f)
if "`rhot'" == "onestep" & "`twostep'" != "" {
di as err "Do not specify both onestep and twostep"
error 198
}
if "`weight'" != "" {
if "`rhotype'" != "regress" & "`rhotype'" != "freg" /*
*/ & reldif(`rho_f',-2) < 1e-8 {
di as error ///
"weights can only be used with regress or freg rhotypes, or with a fixed rho"
exit 198
}
}
tempname cons
gen double `cons'=1
local varlist "`varlist' `cons'"
sort `id' `t'
/* ckeck that weights are constant in panel */
if "`weight'"!= "" {
tempvar myweight
qui gen double `myweight'`exp'
qui xtsum `myweight'
if abs(r(sd_w)) > 1e-8 {
di as error "weights must be constant within panel"
exit 198
}
}
tempname origsmp
gen byte `origsmp'=`touse'
/* estimate rho */
tempname rho
if `rhof'==-2 {
if "`rhot'" =="onestep" {
/* estimate rho by onestep from within model */
qui preserve
capture drop `cons'
qui keep if `touse'
qui sort `id' `t'
local vlist2 : subinstr local varlist "`cons'" " "
qui demean `vlist2', i(`id')
tokenize `vlist2'
macro shift
_rmcoll `*'
if "`fe'" != "" {
local indepsn `r(varlist)'
}
local vlistfe "`depvar' `r(varlist)'"
qui regress `vlist2', nocons
tempname top1 top2
tempvar res_w Lres_w ettm1 et2 mc
qui {
predict double `res_w', residuals
by `id': gen double `Lres_w'=cond(_n>1, /*
*/ `res_w'[_n-1],.)
gen double `ettm1'=`Lres_w'*`res_w'
gen double `et2'=`res_w'*`res_w'
gen long `mc'=(`ettm1' <.)
summ `ettm1', meanonly
scalar `top1'=r(sum)
summ `mc', meanonly
scalar `top2'=r(sum)
summ `et2', meanonly
scalar `rho'=(`top1'/`top2')/(r(sum)/r(N))
restore
}
}
else {
qui {
preserve
capture drop `cons'
local vlist2 : subinstr local /*
*/ varlist "`cons'" " "
keep if `touse'
demean `vlist2', i(`id')
tokenize `vlist2'
macro shift
if "`fe'" != "" {
local noi noi
}
`noi' _rmcoll `*'
if "`fe'" != "" {
local indepsn `r(varlist)'
}
local vlistfe "`depvar' `r(varlist)'"
prais `vlist2' [`weight'`exp'] if `touse', /*
*/ nocons rhotype(`rhot') `twostep' /*
*/ `weights'
scalar `rho'=e(rho)
restore
}
}
}
else {
scalar `rho'=`rhof'
if abs(`rho')>=1 {
di as err "|rho|<1, specify a valid rho"
exit 198
}
qui preserve
qui capture drop `cons'
local vlist2 : subinstr local /*
*/ varlist "`cons'" " "
qui keep if `touse'
demean `vlist2', i(`id')
tokenize `vlist2'
macro shift
_rmcoll `*'
if "`fe'" != "" {
local indepsn `r(varlist)'
}
local vlistfe "`depvar' `r(varlist)'"
restore
}
/* Transform data to remove AR component preserve data and keep if `touse' */
tempvar touse3
gen byte `touse3'=`touse'
preserve
qui keep if `touse'
qui sort `id' `t'
tempvar difft
qui by `id': gen double `difft'=`t'[_n]-`t'[_n-1] if _n>1
foreach x of local varlist {
/* These next two lines implement C_i(rho) transform from
page 816 of Baltagi and Wu 199 */
/* note calculation depends on user defined time */
tempvar tvar
qui by `id': gen `tvar'=(sqrt(1-`rho'^2))*`x' if _n==1
qui by `id': replace `tvar'=(sqrt(1-`rho'^2))* /*
*/( `x'[_n]*(sqrt(1/(1-`rho'^(2*`difft')))) - /*
*/ `x'[_n-1]*(sqrt((`rho'^(2*`difft')/ /*
*/ (1-`rho'^(2*`difft'))))) ) if _n>1
qui replace `x'=`tvar'
}
if "`fe'" != "" {
qui {
by `id': drop if _n==1
keep if `touse'
sort `id' `t'
regress `vlistfe' `cons' [`weight'`exp'], nocons
tempname rrss
scalar `rrss'=e(rss)
capture drop `cons'
/* xtdata `vlistfe', i(`id') fe clear */
fevars `vlistfe' [`weight'`exp'], i(`id')
regress `vlistfe' [`weight'`exp']
tempname df_b df_r r2 rmse_r rmse mss rss
tempname ll ll_0 F
scalar `F' =e(F)
scalar `df_b'=e(df_m)
scalar `rss'=e(rss)
scalar `mss'=e(mss)
scalar `ll'=e(ll)
scalar `ll_0'=e(ll_0)
scalar `r2'=e(r2)
scalar `rmse_r'=e(rmse)
tempname b V V1
mat `b'=e(b)
mat `V'=e(V)
tempname NT N K scale sigma_e
scalar `NT'=e(N)
scalar `K'=e(df_m)+1 /* assumes cons in mod */
tempvar Ti
tempname g_max g_min g_avg Tbar df_m r2_a F_f df_a
by `id': gen `Ti'=_N if _n==_N
summ `Ti' [`weight'`exp']
scalar `N'=r(N)
scalar `df_m'=`K'-1+`N'-1
scalar `df_b'=`K'-1
scalar `df_r'=`NT'-`df_m'-1
scalar `df_a'=`N'-1
scalar `F'=`F'*`df_r'/(`NT'-`K')
scalar `F_f'=`df_r'/(`N'-1) *(`rrss'-`rss')/(`rss')
scalar `sigma_e'=sqrt( (`rmse_r'^2) /*
*/ *((`NT'-`df_b'-1) /*
*/ /(`NT'-`df_m'-1) ) )
scalar `g_max'=r(max)
scalar `g_min'=r(min)
scalar `g_avg'=r(mean)
scalar `r2_a'=1-((`NT'-1)/`df_r')*(1-`r2')
if "`weight'" == "" {
means `Ti'
scalar `Tbar'=r(mean_h)
}
scalar `scale'=(`NT'-`K')/(`NT'-`N'-`K'+1)
mat `V1'=`scale'*`V'
restore
tempname bc Vc
mat `bc'=`b'
mat `Vc'=`V1'
tempvar ftouse
by `id': gen long `ftouse' = sum(`touse')
replace `touse'=0 if `ftouse'==1
/*
mat colnames `b' = `indepsn_o' _cons
mat colnames `V1' = `indepsn_o' _cons
mat rownames `V1' = `indepsn_o' _cons
*/
est post `b' `V1', depname(`depname_o') /*
*/ esample(`touse')
/* now calculate corr(u_i, xb), r2_b and r2_o.
NOte _predict assumes that e(b) exists.
Thus, this section must come after est post */
tempvar touse2
qui by `id': gen byte `touse2'=e(sample) & (_n!=1)
sort `id' `touse2'
tempvar ui xb xib ym xitb
_predict double `xb' if `touse2', xb
by `id' `touse2': gen double `ui' = /*
*/ cond(`touse2' & _n==_N, /*
*/ sum(`depvar')/_n-sum(`xb')/_n,.) /*
*/ if `touse2'
by `id': replace `ui'=`ui'[_N] if `touse2'
sort `id' `touse2'
corr `ui' `xb' [`weight'`exp'] if `touse2'
est scalar corr=r(rho)
by `id' `touse2': gen double `xib' = /*
*/ cond(`touse2' & _n==_N, /*
*/ sum(`xb')/_n,.) if `touse2'
by `id': replace `xib'=`xib'[_N] if `touse2'
by `id' `touse2': gen double `ym' = /*
*/ cond(`touse2' & _n==_N, /*
*/ sum(`depvar')/_n,.) if `touse2'
by `id': replace `ym'=`ym'[_N] if `touse2'
tempname r2_b r2_o
corr `xib' `ym' [`weight'`exp'] if `touse2'
scalar `r2_b'=(r(rho))^2
est scalar r2_b=`r2_b'
corr `xb' `depvar' [`weight'`exp'] if `touse2'
scalar `r2_o'=(r(rho))^2
est scalar r2_o=`r2_o'
by `id' `touse2': replace `ui'=. if _n!=_N
sum `ui' [`weight'`exp']
est scalar rho_ar=`rho'
tempname b2 V2
mat `b2'=e(b)
local tcols : colnames `b2'
local cnt2 1
foreach vt of local indepsn {
local tmpindep : subinstr local tcols /*
*/ "`vt'" "`vt'", /*
*/ word count(local cnt3)
if `cnt3' == 1 {
local wrd2 : word `cnt2' of /*
*/ `indepsn_o'
local indepsn_o2 `indepsn_o2' `wrd2'
}
local cnt2 = `cnt2' + 1
}
mat colnames `b2' = `indepsn_o2' _cons
/* adj cons */
tempname colsb
scalar `colsb'=colsof(`b2')
mat `b2'[1,`colsb']=`b2'[1,`colsb']/((1-`rho') )
est repost b = `b2' , rename esample(`touse3')
est scalar sigma_u = sqrt(r(Var))
est scalar rho_fov=r(Var)/(`sigma_e'^2+r(Var))
est scalar sigma_e=`sigma_e'
est scalar F = `F'
est scalar F_f=`F_f'
est scalar r2_w=`r2'
est scalar r2_a=`r2_a'
est scalar mss=`mss'
est scalar ll=`ll'
est scalar ll_0=`ll_0'
est scalar rss= `rss'
est scalar df_m=`df_m'
est scalar df_r=`df_r'
est scalar df_b=`df_b'
est scalar df_a=`df_a'
est scalar N= `NT'
est scalar N_g= `N'
est scalar g_max= `g_max'
est scalar g_min= `g_min'
est scalar g_avg= `g_avg'
est scalar rmse = `rmse_r'
if "`weight'" == "" {
est scalar Tbar =`Tbar'
}
else {
est local wtype "`weight'"
est local wexp "`exp'"
}
if `g_max'==`g_min' {
est scalar Tcon=1
}
else {
est scalar Tcon=0
}
est scalar df_m =`df_m'
est local rhotype "`rhot'"
est local depvar "`depname'"
est local ivar "`id'"
est local tvar "`t'"
est local model "fe"
est local predict "xtrar_p"
est local dw "`lbi'"
est local cmd "xtregar"
}
DispFE `level'
exit
}
/* FROM HERE ON APPLIES TO RE ONLY */
/* now do OLS on transformed data
and get residuals */
qui {
regress `varlist' , nocons
tempvar mu
predict double `mu', residuals
/* now make g_i */
tempvar g_i
by `id': gen double `g_i'=1 if _n==1
by `id': replace `g_i'=( (1-`rho'^`difft')/ /*
*/ (sqrt(1-`rho'^(2*`difft'))) ) if _n>1
replace `g_i'=sqrt(1-`rho'^2)*`g_i'
tempvar g_i2 g_i2s g_i2ss
gen double `g_i2'=`g_i'*`g_i'
by `id': gen double `g_i2s'=sum(`g_i2')
by `id': replace `g_i2s'=. if _n<_N /* g_i2s is g_i'g_i */
sum `g_i2s', mean
scalar `g_i2ss'=r(sum) /* g_i2ss = sum_i^N(g_i'g_i)*/
by `id': replace `g_i2'=`g_i2s'[_N] /* g_i2 has gi'gi in all
of i's obs */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -