*! version 1.2.0 29 jan 1997 STB-38 sg70 program define iqreg version 5.0 local options "Level(integer $S_level)" if substr("`*'",1,1)=="," | "`*'"=="" { if "$S_E_cmd"!="iqreg" { 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' local q0 $S_1 local q1 $S_2 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 local j 1 while "``i''" != "" { if _se[``i''] == 0 { di in blu /* */ "note: ``i'' dropped due to collinearity" local `i' " " } else { local result "`result' `coefs1'[1,`j']" local j = `j' + 1 } local i = `i' + 1 } local vl "`*'" local result "`result' `coefs1'[1,`j']" preserve `log' di in gr "(estimating base model)" qui { keep if `touse' keep `depv' `vl' qreg `depv' `vl', `opts' q(`q0') } if $S_E_nobs==0 | $S_E_nobs==. { exit } local nobs $S_E_nobs local tdf $S_E_tdf local q $S_E_q local rsd1 $S_E_rsd local msd1 $S_E_msd if "`cons'"=="" { local vle "_cons" local vli "`bcons'" } mat `coefs0' = get(_b) qui qreg `depv' `vl', `opts' q(`q1') if $S_E_nobs != `nobs' { di in red /* */ "`q0' quantile: `nobs' obs. used" _n /* */ "`q1' 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 /* */ "`q1' 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 msd0 $S_E_msd local rsd0 $S_E_rsd mat `coefs' = get(_b) mat `coefs' = `coefs' - `coefs0' qui gen double `bwt' = . `log' di in gr "(bootstrapping " _c qui postfile `handle' `vl' `vli' using "`BOOTRES'", double capture noisily { local j 1 while `j'<=`reps' { bsampl_w `bwt' /* `recnum' */ capture noisily { qreg_c `depv' `vl', /* */ `opts' q(`q0') wvar(`bwt') mat `coefs0' = get(_b) qreg_c `depv' `vl', /* */ `opts' q(`q1') wvar(`bwt') mat `coefs1' = get(_b) mat `coefs1' = `coefs1' - `coefs0' } 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' `vli', dev nocons mat rownames `VCE' = `vl' `vle' mat colnames `VCE' = `vl' `vle' 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' global S_E_q0 `q0' global S_E_q1 `q1' global S_E_rsd0 `rsd0' global S_E_rsd1 `rsd1' global S_E_msd0 `msd0' global S_E_msd1 `msd1' global S_E_vce "Bootstrap" global S_E_frc 0 global S_E_cmd "iqreg" } if `level'<10 | `level'>99 { local level 95 } PrForm $S_E_q0 local q0 $S_1 PrForm $S_E_q1 local q1 $S_1 di _n in gr "$S_E_q1-$S_E_q0 Interquantile Regression" _col(54) _c di in gr "Number of obs =" in ye %10.0g $S_E_nobs di in gr " bootstrap(" in ye "$S_E_reps" in gr ") SEs" /* */ _col(54) "`q1' Pseudo R2 =" in ye %10.4f 1-($S_E_msd0/$S_E_rsd0) di in gr /* */ _col(54) "`q0' Pseudo R2 =" in ye %10.4f 1-($S_E_msd1/$S_E_rsd1) di mat mlout, level(`level') error $S_E_frc end program define SetQ /* | # [,] # */ if "`*'"=="" { global S_1 .25 global S_2 .75 exit } local orig "`*'" parse "`*'", parse(" ,") FixNumb "`orig'" `1' global S_1 $S_3 mac shift if "`1'"=="," { mac shift } FixNumb "`orig'" `1' global S_2 $S_3 mac shift if "`*'"!="" { Invalid "`orig'" } if $S_1 >= $S_2 { Invalid "`orig'" "$S_1 >= $S_2" } 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