📄 ivtobit.ado
字号:
eret scalar p = chiprob(r(df), r(chi2))
eret scalar endog_ct = `end1_ct'
eret local method "twostep"
eret local instd "`end1'"
eret local insts "`exog' `inst'"
eret local depvar `lhsname'
TSDisplay, level(`level')
}
else {
local noi "noi"
if "`log'" != "" {
local noi ""
}
if "`from'" == "" {
// Starting values
tempname b0 se0 bfrom V0 cholV
qui `noi' di as text _n "Fitting exogenous tobit model"
// Using intreg since tobit doesn't take pweights
tempvar intregl intregr
qui gen double `intregl' = `lhs'
if "`llopt'" != "" {
qui replace `intregl' = . ///
if `intregl' <= `tobitll'
}
qui gen double `intregr' = `lhs'
if "`ulopt'" != "" {
qui replace `intregr' = . ///
if `intregr' >= `tobitul'
}
cap qui intreg `intregl' `intregr' `end1' ///
`exog' `wgt' if `touse'
if _rc {
di as error "could not find initial values"
exit 198
}
mat `b0' = e(b)
mat `se0' = exp(`b0'[1, (`end1_ct'+`exog_ct'+2)])
mat `b0' = `b0'[1, 1..(`end1_ct'+`exog_ct'+1)]
cap qui sureg (`end1' = `exog' `inst')
if _rc {
di as error "could not find initial values"
exit 198
}
mat `V0' = e(Sigma)
cap mat `cholV' = cholesky(`V0')
if _rc {
di as error "could not find initial values"
exit 198
}
loc nchol = `end1_ct'*(`end1_ct' + 1) / 2
mat `V0' = J(1, `nchol', 0)
loc m = 1
forv i = 1/`end1_ct' {
forv j = `i'/`end1_ct' {
mat `V0'[1, `m'] = `cholV'[`i',`j']
loc m = `m' + 1
}
}
if `end1_ct' == 1 {
mat `bfrom' = ///
`b0', e(b), 0, ln(`se0'[1,1]), ln(`V0'[1,1])
}
else {
mat `bfrom' = ///
`b0', e(b), `se0', J(1,`end1_ct', 0), `V0'
}
local init "`bfrom', copy"
}
else {
local init "`from'"
}
loc iveqns ""
loc covterms "/s11 "
loc testcmd ""
loc i = 1
foreach var of varlist `end1' {
loc iveqns "`iveqns' (`var' : `var' = `exog' `inst')"
loc ip1 = `i' + 1
// Only for multiple endog vars:
loc covterms "`covterms' /s`ip1'1"
loc testcmd "`testcmd' [s`ip1'1]_b[_cons]"
loc i = `i' + 1
}
if `end1_ct' > 1 {
forv j = 1/`end1_ct' {
loc jp1 = `j' + 1
forv i = `j'/`end1_ct' {
loc ip1 = `i' + 1
loc covterms "`covterms' /s`ip1'`jp1'"
}
}
}
else { // Fix things up for the one endog var model
loc covterms "/alpha /lns /lnv"
loc testcmd "[alpha]_b[_cons]"
}
qui `noi' di as text _n "Fitting full model"
// sort so that we can get the cov terms from the
// last obs. in dataset in lf
tempvar currsort
gen long `currsort' = _n
sort `touse' `currsort'
glo IVT_NEND = `end1_ct'
glo IVT_ll = `tobitll'
glo IVT_ul = `tobitul'
if `end1_ct' == 1 {
ml model lf ivtob_1_lf ///
(`lhsstr' : `lhs' = `end1' `exog') ///
`iveqns' `covterms' ///
`wgt' if `touse' , ///
title(Tobit model with endogenous regressors) ///
maximize `mlopts' `robust' `clusopt' ///
search(off) init(`init') `log' `scoreml'
}
else {
ml model lf ivtob_lf ///
(`lhsstr' : `lhs' = `end1' `exog') ///
`iveqns' `covterms' ///
`wgt' if `touse', ///
title(Tobit model with endogenous regressors) ///
maximize `mlopts' `robust' `clusopt' ///
search(off) init(`init') `log' `scoreml' ///
nrtolerance(`nrtolerance')
}
macro drop IVT_NEND
macro drop IVT_ll
macro drop IVT_ul
qui test `testcmd'
eret scalar chi2_exog = r(chi2)
eret scalar endog_ct = `end1_ct'
eret scalar p_exog = chiprob(1, e(chi2_exog))
eret local method "ml"
if "`scorestr'" != "" {
tokenize `scorestr'
local i = 1
while "``i''" != "" {
rename `score`i'' ``i''
local i = `i' + 1
}
eret local scorevars `scorestr'
}
// rename b and V -- time-series operators
mat `b0' = e(b)
mat `V0' = e(V)
local names : colfullnames `b0'
// Need to do end1 separately, because if an eqn name,
// must replace . with _
local newend1 `end1'
foreach y of local end1 {
local j : list posof "`y'" in oldend1
local y2 : word `j' of `end1name'
local newend1 : subinstr local newend1 "`y'" "`y2'"
local y2s : subinstr local y2 "." "_"
local names : ///
subinstr local names ":`y'" ":`y2'", all
local names : ///
subinstr local names "`y':" "`y2s':", all
}
local end1 `newend1'
foreach x in exog inst {
local new`x' ``x''
foreach y of local `x' {
local j : list posof "`y'" in old`x'
local y2 : word `j' of ``x'name'
local new`x' : ///
subinstr local new`x' "`y'" "`y2'"
local names : ///
subinstr local names "`y'" "`y2'", all
}
local `x' `new`x''
}
mat colnames `b0' = `names'
mat colnames `V0' = `names'
mat rownames `V0' = `names'
eret repost b = `b0' V = `V0', rename
eret local depvar "`lhsname' `end1'"
eret local instd "`end1'"
eret local insts "`exog' `inst'"
// Return Sigma matrix
tempname sigma
mat `sigma' = I(`end1_ct'+1)
if `end1_ct' == 1 {
tempname s a v
scalar `s' = exp([lns]_b[_cons])
scalar `a' = [alpha]_b[_cons]
scalar `v' = exp([lnv]_b[_cons])
mat `sigma'[1,1] = `s'^2 + `a'^2 * `v'^2
mat `sigma'[1,2] = `a'*`v'^2
mat `sigma'[2,1] = `sigma'[1,2]
mat `sigma'[2,2] = `v'^2
}
else {
loc endp1 = `end1_ct' + 1
forvalues j = 1/`endp1' {
forvalues i = `j'/`endp1' {
mat `sigma'[`i',`j'] = ///
[s`i'`j']_b[_cons]
}
}
mat `sigma' = `sigma'*`sigma''
}
eret matrix Sigma = `sigma'
MLDisplay , level(`level') `first'
}
// Count number of censored observations
// Need to account for fweights if supplied
tempvar lone
qui gen double `lone' = (`lhs' <= `tobitll')
if "`weight'" == "fweight" {
qui replace `lone' = `lone'*`exp'
}
qui summ `lone' if e(sample), meanonly
local llcens = r(sum)
qui replace `lone' = (`lhs' >= `tobitul')
if "`weight'" == "fweight" {
qui replace `lone' = `lone'*`exp'
}
qui summ `lone' if e(sample), meanonly
loc ulcens = r(sum)
loc notcens = e(N) - `llcens' - `ulcens'
eret scalar N_unc = `notcens'
eret scalar N_lc = `llcens'
eret scalar N_rc = `ulcens'
if "`weight'" != "" {
eret local wtype "`weight'"
eret local wexp "= `exp'"
}
if "`llopt'" != "" {
ereturn scalar llopt = `tobitll'
}
if "`ulopt'" != "" {
ereturn scalar ulopt = `tobitul'
}
// Undocumented, but needed for -predict , scores-
ereturn scalar tobitll = `tobitll'
ereturn scalar tobitul = `tobitul'
eret local predict "ivtobit_p"
eret local cmd "ivtobit"
CensDisplay
end
program define MLDisplay
syntax , [ level(cilevel) first ]
local en = `e(endog_ct)'
if "`first'" == "" {
local neq = 1
}
else {
local neq = 1 + `en'
}
if `en' == 1 {
ml display, level(`level') neq(`neq') ///
diparm(alpha, label("/alpha")) ///
diparm(lns, label("/lns")) ///
diparm(lnv, label("/lnv")) ///
diparm(__sep__) ///
diparm(lns, exp label("s")) ///
diparm(lnv, exp label("v"))
}
else {
ml display, level(`level') neq(`neq')
}
di as text "Instrumented: " _c
Disp `e(instd)'
di as text "Instruments: " _c
Disp `e(insts)'
di as text "{hline 78}"
if `en' == 1 {
di as text "Wald test of exogeneity (/alpha = 0): chi2(" ///
as res e(endog_ct) as text ") = " as res %8.2f ///
e(chi2_exog) as text _col(59) "Prob > chi2 = " ///
as res %6.4f chiprob(e(endog_ct), e(chi2_exog))
}
else {
di as text "Wald test of exogeneity: " _col(30) as text ///
"chi2(" as res e(endog_ct) as text ") = " as res ///
%8.2f e(chi2_exog) as text _col(59) ///
"Prob > chi2 = " as res %6.4f ///
chiprob(e(endog_ct), e(chi2_exog))
}
end
program define TSDisplay
syntax , [ level(cilevel) ]
di
di as text "Two-step tobit with endogenous regressors" _c
di as text _col(51) "Number of obs = " as res %9.0f e(N)
di as text _col(51) "Wald chi2(" as res e(df_m) as text ") = " _c
di as result %9.2f e(chi2)
di as text _col(51) "Prob > chi2 = " as res %9.4f ///
chiprob(e(df_m), e(chi2))
di
eret display, level(`level')
di as text "Instrumented: " _c
Disp `e(instd)'
di as text "Instruments: " _c
Disp `e(insts)'
di as text "{hline 78}"
di as text "Wald test of exogeneity: " _col(30) as text ///
"chi2(" as res e(endog_ct) ///
as text ") = " as res %8.2f e(chi2_exog) as text ///
_col(59) "Prob > chi2 = " as res %6.4f ///
chiprob(e(endog_ct), e(chi2_exog))
end
// Borrowed from ivreg.ado
program define IsStop, sclass
if `"`0'"' == "[" {
sret local stop 1
exit
}
if `"`0'"' == "," {
sret local stop 1
exit
}
if `"`0'"' == "if" {
sret local stop 1
exit
}
if `"`0'"' == "in" {
sret local stop 1
exit
}
if `"`0'"' == "" {
sret local stop 1
exit
}
else {
sret local stop 0
}
end
// Borrowed from ivreg.ado
program define Subtract /* <cleaned> : <full> <dirt> */
args cleaned /* macro name to hold cleaned list
*/ colon /* ":"
*/ full /* list to be cleaned
*/ dirt /* tokens to be cleaned from full */
tokenize `dirt'
local i 1
while "``i''" != "" {
local full : subinstr local full "``i''" "", word all
local i = `i' + 1
}
tokenize `full' /* cleans up extra spaces */
c_local `cleaned' `*'
end
// Borrowed from ivreg.ado
program define Disp
local first ""
local piece : piece 1 64 of `"`0'"'
local i 1
while "`piece'" != "" {
di as text "`first'`piece'"
local first " "
local i = `i' + 1
local piece : piece `i' 64 of `"`0'"'
}
if `i'==1 {
di
}
end
program define CensDisplay
local dep : word 1 of `e(depvar)'
local dep = abbrev("`dep'", 12)
di _n as text " Obs. summary:" _continue
if `e(N_lc)' > 1 {
di _col(20) as res %7.0f e(N_lc) ///
as text " left-censored observations at " ///
as text "`dep'<=" as res `e(llopt)'
}
else if `e(N_lc)' > 0 {
di _col(20) as res %7.0f e(N_lc) ///
as text " left-censored observation at " ///
as text "`dep'<=" as res `e(llopt)'
}
di _col(20) as res %7.0f e(N_unc) ///
as text " uncensored observations"
if `e(N_rc)' > 1 {
di _col(20) as res %7.0f e(N_rc) ///
as text " right-censored observations at " ///
as text "`dep'>=" as res `e(ulopt)'
}
else if `e(N_rc)' > 0 {
di _col(20) as res %7.0f e(N_rc) ///
as text " right-censored observations at " ///
as text "`dep'>=" as res `e(ulopt)'
}
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -