*! version 1.0.0 29 jan 1997 STB-38 sg70 program define sqreg version 5.0 local options "Level(integer $S_level)" if substr("`*'",1,1)=="," | "`*'"=="" { if "$S_E_cmd"!="sqreg" { error 301 } parse "`*'" } else { local varlist "req ex" local if "opt" local in "opt" local options /* */ "`options' Quantiles(string) WLSiter(integer 1) Reps(integer 20) noLOg" parse "`*'" if "`log'"!="" { local log "*" } SetQ `quantil' parse "$S_1", parse(" ") local nq 1 while "``nq''" != "" { local q`nq' ``nq'' local nq = `nq' + 1 } local nq = `nq' - 1 if (`reps')<2 { error 198 } global S_E_cmd tempname coefs VCE adj coefs0 coefs1 handle tempvar e bcons touse /* recnum */ bwt tempfile BOOTRES mark `touse' `if' `in' markout `touse' `varlist' quietly count if `touse' if _result(1)<4 { di in red "insufficient observations" exit 2001 } local opts "wls(`wlsiter')" parse "`varlist'", parse(" ") local depv "`1'" mac shift qui regress `depv' `*' if `touse' local i 1 while "``i''" != "" { if _se[``i''] == 0 { di in blu /* */ "note: ``i'' dropped due to collinearity" local `i' " " } local i = `i' + 1 } parse "`*'", parse(" ") local rhs "`*'" local k 1 while `k' <= `nq' { tempname coef`k' local myeq = "q" + string((`q`k'')*100) local i 1 while "``i''" != "" { local result "`result' `coef`k''[1,`i']" local eqnams "`eqnams' `myeq'" local conams "`conams' ``i''" tempvar v local vl "`vl' `v'" local i = `i' + 1 } local result "`result' `coef`k''[1,`i']" local eqnams "`eqnams' `myeq'" local conams "`conams' _cons" tempvar v local vl "`vl' `v'" local k = `k' + 1 } preserve `log' di in gr "(estimating base model)" qui { keep if `touse' keep `depv' `rhs' qreg `depv' `rhs', `opts' q(`q1') } if $S_E_nobs==0 | $S_E_nobs==. { exit } local nobs $S_E_nobs local tdf $S_E_tdf local rsd1 $S_E_rsd local msd1 $S_E_msd local vle "_cons" local vli "`bcons'" mat `coefs' = get(_b) local k 2 while `k' <= `nq' { qui qreg `depv' `rhs', `opts' q(`q`k'') if $S_E_nobs != `nobs' { di in red /* */ "`q0' quantile: `nobs' obs. used" _n /* */ "`q`k'' quantile: $S_E_nobs obs. used" _n /* */ "Same sample cannot be used to estimate both quantiles." /* */ "Sample size probably too small." exit 498 } if $S_E_tdf != `tdf' { di in red /* */ "`q0' quantile: " `nobs'-`tdf' " coefs estimated" _n /* */ "`q`k'' quantile: " $S_E_nobs-$S_E_tdf coefs estimated" _n /* */ "Same model cannot be used to estimate both quantiles." /* */ "Sample size probably too small." exit 498 } local msd`k' $S_E_msd local rsd`k' $S_E_rsd mat `coef1' = get(_b) mat `coefs' = `coefs', `coef1' local k = `k' + 1 } mat colnames `coefs' = `conams' mat coleq `coefs' = `eqnams' qui gen double `bwt' = . `log' di in gr "(bootstrapping " _c qui postfile `handle' `vl' using "`BOOTRES'", double quietly noisily { local j 1 while `j'<=`reps' { bsampl_w `bwt' /* `recnum' */ capture noisily { local k 1 while `k'<=`nq' { qreg_c `depv' `rhs', /* */ `opts' q(`q`k'') wvar(`bwt') mat `coef`k'' = get(_b) local k =`k' + 1 } } local rc = _rc if (`rc'==0) { post `handle' `result' `log' di in gr "." _c local j=`j'+1 } else { if _rc == 1 { exit 1 } `log' di in gr "*" _c } } } local rc = _rc postclose `handle' if `rc' { exit `rc' } qui use "`BOOTRES'", clear quietly mat accum `VCE' = `vl', dev nocons mat rownames `VCE' = `conams' mat roweq `VCE' = `eqnams' mat colnames `VCE' = `conams' mat coleq `VCE' = `eqnams' scalar `adj' = 1/(`reps'-1) mat `VCE'=`VCE'*`adj' mat post `coefs' `VCE', obs(`nobs') dof(`tdf') depn(`depv') `log' noi di in gr ")" restore capture erase "`BOOTRES'" global S_E_depv "`depv'" global S_E_reps `reps' global S_E_vl "`vl'" global S_E_nobs `nobs' global S_E_tdf `tdf' local k 1 while `k' <= `nq' { global S_E_q`k' `q`k'' global S_E_r`k' `rsd`k'' global S_E_m`k' `msd`k'' local k = `k' + 1 } global S_E_nq `nq' global S_E_vce "Bootstrap" global S_E_frc 0 global S_E_cmd "sqreg" } if `level'<10 | `level'>99 { local level 95 } di _n in gr "Simultaneous quantile Regression" _col(54) _c di in gr "Number of obs =" in ye %10.0g $S_E_nobs PrForm $S_E_q1 di in gr " bootstrap(" in ye "$S_E_reps" in gr ") SEs" /* */ _col(54) "$S_1 Pseudo R2 =" in ye %10.4f 1-($S_E_m1/$S_E_r1) local k 2 while `k' <= $S_E_nq { PrForm ${S_E_q`k'} di in gr /* */ _col(54) "$S_1 Pseudo R2 =" /* */ in ye %10.4f 1-(${S_E_m`k'}/${S_E_r`k'}) local k = `k' + 1 } di mat mlout, level(`level') error $S_E_frc end program define SetQ /* | # [,] # ... */ if "`*'"=="" { di in red "option quantiles() required" exit 198 } local orig "`*'" parse "`*'", parse(" ,") global S_1 while "`1'" != "" { FixNumb "`orig'" `1' global S_1 "$S_1 $S_3" mac shift if "`1'"=="," { mac shift } } end program define FixNumb /* # */ local orig "`1'" mac shift capture confirm number `1' if _rc { Invalid "`orig'" "`1' not a number" } if `1' >= 1 { global S_3 = `1'/100 } else global S_3 `1' if $S_3<=0 | $S_3 >=1 { Invalid "`orig'" "$S_3 out of range" } end program define Invalid /* "" "" */ di in red "quantiles(`1') invalid" if "`2'" != "" { di in red "`2'" } exit 198 end program define PrForm /* # */ global S_1 : di %8.2f `1' global S_1 $S_1 if substr("$S_1",1,1)=="0" { global S_1 = substr("$S_1",2,.) } end exit