|
|
|
|
| capture program drop binspwc
|
| program define binspwc, eclass
|
| version 13
|
|
|
| syntax varlist(min=2 numeric fv ts) [if] [in] [fw aw pw] , by(varname) [deriv(integer 0) at(string asis) nolink
|
| estmethod(string) estmethodopt(string asis) absorb(string asis) reghdfeopt(string asis)
|
| pwc(string) testtype(string) lp(string)
|
| bins(numlist integer max=2 >=0) bynbins(string) binspos(string)
|
| pselect(numlist integer >=0) sselect(numlist integer >=0)
|
| binsmethod(string) nbinsrot(string) samebinsby randcut(numlist max=1 >=0 <=1)
|
| nsims(integer 500) simsgrid(integer 20) simsseed(numlist integer max=1 >=0)
|
| dfcheck(numlist integer max=2 >=0) masspoints(string) usegtools(string)
|
| vce(passthru) asyvar(string)
|
| numdist(string) numclust(string)]
|
|
|
|
|
|
|
| local qrot=2
|
|
|
|
|
|
|
| if ("`weight'"!="") {
|
| local wt [`weight'`exp']
|
| local wtype=substr("`weight'",1,1)
|
| }
|
|
|
| if ("`testtype'"=="") {
|
| local testtype "2"
|
| }
|
|
|
|
|
| if ("`absorb'"!="") {
|
| if ("`estmethod'"!="") {
|
| if ("`estmethod'"!="reghdfe") {
|
| di as error "absorb() can only be combined with estmethod(reghdfe)."
|
| exit
|
| }
|
| }
|
| else local estmethod "reghdfe"
|
| }
|
| if ("`estmethod'"=="") local estmethod "reg"
|
| tokenize `estmethod'
|
| local estmethod `1'
|
| if ("`estmethod'"=="reg") {
|
| local estcmd "reg"
|
| }
|
| else if ("`estmethod'"=="qreg") {
|
| local estcmd "qreg"
|
| local quantile `2'
|
| if ("`quantile'"=="") local quantile=0.5
|
| }
|
| else if ("`estmethod'"=="logit") {
|
| local estcmd "logit"
|
| }
|
| else if ("`estmethod'"=="probit") {
|
| local estcmd "probit"
|
| }
|
| else if ("`estmethod'"=="reghdfe") {
|
| local estcmd "reghdfe"
|
| }
|
|
|
|
|
| if ("`link'"!="") local transform "F"
|
| else local transform "T"
|
|
|
|
|
|
|
| if ("`vce'"=="") local vce "vce(robust)"
|
| local vcetemp: subinstr local vce "vce(" "", all
|
| local vcetemp: subinstr local vcetemp ")" "", all
|
| tokenize "`vcetemp'", parse(", ")
|
| if ("`1'"=="cl"|"`1'"=="clu"|"`1'"=="clus"|"`1'"=="clust"|
|
| "`1'"=="cluste"|"`1'"=="cluster") {
|
| if ("`3'"==""|"`3'"==",") local clusterON "T"
|
| local clustervar `2'
|
| if ("`estmethod'"=="qreg") {
|
| local vce "vce(robust)"
|
| di as text in gr "Warning: vce(cluster) not allowed. vce(robust) used instead."
|
| }
|
| }
|
|
|
|
|
| if ("`1'"=="boot" | "`1'"=="bootstrap") {
|
| local boot "on"
|
| local repstemp `3'
|
| if ("`repstemp'"=="") local repstemp reps(20)
|
| local repstemp: subinstr local repstemp "reps(" "", all
|
| local reps: subinstr local repstemp ")" "", all
|
| if ("`estmethod'"=="qreg") {
|
| local estcmd "bsqreg"
|
| if ("`weight'"!="") {
|
| di as error "Weights not allowed for bootstrapping."
|
| exit
|
| }
|
| }
|
| }
|
| else {
|
| local boot "off"
|
| }
|
|
|
| if ("`asyvar'"=="") local asyvar "off"
|
|
|
|
|
|
|
| if ("`estmethod'"=="qreg") {
|
| if ("`vce'"=="vce(iid)") local vce_select "vce(ols)"
|
| else local vce_select "vce(robust)"
|
| }
|
| else if ("`estmethod'"=="logit"|"`estmethod'"=="probit") {
|
| if ("`vce'"=="oim"|"`vce'"=="opg") local vce_select "vce(ols)"
|
| else local vce_select "`vce'"
|
| }
|
| else if ("`estmethod'"=="reg"|"`estmethod'"=="reghdfe") {
|
| local vce_select "`vce'"
|
| }
|
|
|
| if ("`binspos'"=="es") local binspos "ES"
|
| if ("`binspos'"=="qs") local binspos "QS"
|
| if ("`binspos'"=="") local binspos "QS"
|
| if ("`binsmethod'"=="rot") local binsmethod "ROT"
|
| if ("`binsmethod'"=="dpi") local binsmethod "DPI"
|
| if ("`binsmethod'"=="") local binsmethod "DPI"
|
|
|
|
|
| if ("`pwc'"!="T"&"`pwc'"!="F"&"`pwc'"!="") {
|
| numlist "`pwc'", integer max(2) range(>=0)
|
| local pwc=r(numlist)
|
| }
|
|
|
| if ("`pwc'"=="F") local pwc ""
|
| if ("`pwc'"=="T"&"`binspos'"!="ES"&"`binspos'"!="QS") local pwc ""
|
|
|
| local selection ""
|
|
|
|
|
| local lenbynbins=0
|
| if ("`bynbins'"!="T"&"`bynbins'"!="") {
|
| numlist "`bynbins'", integer range(>=0)
|
| local bynbins=r(numlist)
|
| local lenbynbins: word count `bynbins'
|
| if (`lenbynbins'==1) {
|
| local nbins_all=`bynbins'
|
| }
|
| }
|
|
|
|
|
| local len_p=0
|
| local len_s=0
|
|
|
| if ("`pselect'"!="") {
|
| numlist "`pselect'", integer range(>=`deriv') sort
|
| local plist=r(numlist)
|
| }
|
|
|
| if ("`sselect'"!="") {
|
| numlist "`sselect'", integer range(>=0) sort
|
| local slist=r(numlist)
|
| }
|
|
|
| local len_p: word count `plist'
|
| local len_s: word count `slist'
|
|
|
| if (`len_p'==1&`len_s'==0) {
|
| local slist `plist'
|
| local len_s=1
|
| }
|
| if (`len_p'==0&`len_s'==1) {
|
| local plist `slist'
|
| local len_p=1
|
| }
|
|
|
| if ("`binspos'"!="ES"&"`binspos'"!="QS") {
|
| if ("`bynbins'"!=""|"`pselect'"!=""|"`sselect'"!="") {
|
| di as error "bynbins(), pselect() or sselect() incorrectly specified."
|
| exit
|
| }
|
| }
|
|
|
| tokenize `bins'
|
| local binsp "`1'"
|
| local binss "`2'"
|
| if ("`binsp'"=="") local binsp=.
|
| if ("`binss'"=="") local binss=`binsp'
|
| if ("`bins'"!="") {
|
| if ("`bynbins'"!=""&"`bynbins'"!="T"&"`bynbins'"!="0") {
|
| di as error "bynbins() or bins() is incorrectly specified."
|
| exit
|
| }
|
| }
|
|
|
|
|
| if (("`bins'"!=""|"`bynbins'"=="0"|"`bynbins'"=="T"|"`bynbins'"=="")&("`binspos'"=="ES"|"`binspos'"=="QS")) local selection "J"
|
|
|
| if ("`selection'"=="J") {
|
| if (`len_p'>1|`len_s'>1) {
|
| di as error "Only one p and one s are allowed to select # of bins."
|
| exit
|
| }
|
| if ("`plist'"=="") local plist=`deriv'
|
| if ("`slist'"=="") local slist=`plist'
|
| if ("`bins'"=="") {
|
| local binsp `plist'
|
| local binss `slist'
|
| }
|
| local len_p=1
|
| local len_s=1
|
| if ("`pwc'"=="T"|"`pwc'"=="") local pwc `=`binsp'+1' `=`binss'+1'
|
| }
|
|
|
|
|
| if ("`selection'"!="J" & ("`pwc'"=="T"|"`pwc'"=="")) local pselectOK "T"
|
|
|
| if (("`pselectOK'"=="T") & ("`bynbins'"!=""&"`bynbins'"!="T") & (`len_p'>1|`len_s'>1)) {
|
| local selection "P"
|
| }
|
|
|
|
|
| if ((`len_p'<=1&`len_s'<=1) & "`selection'"!="J") {
|
| local selection "NA"
|
| if ("`pwc'"=="") {
|
| if ("`bins'"!="") local pwc `=`binsp'+1' `=`binss'+1'
|
| else {
|
| if (`len_p'==1&`len_s'==1) local pwc `=`plist'+1' `=`slist'+1'
|
| else local pwc `=`deriv'+1' `=`deriv'+1'
|
| }
|
| }
|
| }
|
|
|
|
|
| if ("`selection'"=="") {
|
| di as error "Degree, smoothness, or # of bins are not correctly specified."
|
| exit
|
| }
|
|
|
| if ("`selection'"=="NA") local binselectmethod "User-specified"
|
| else {
|
| if ("`binsmethod'"=="DPI") local binselectmethod "IMSE-optimal plug-in choice"
|
| if ("`binsmethod'"=="ROT") local binselectmethod "IMSE-optimal rule-of-thumb choice"
|
| if ("`selection'"=="J") local binselectmethod "`binselectmethod' (select # of bins)"
|
| if ("`selection'"=="P") local binselectmethod "`binselectmethod' (select degree and smoothness)"
|
| }
|
|
|
|
|
| tokenize `pwc'
|
| local tsha_p "`1'"
|
| local tsha_s "`2'"
|
| if ("`tsha_p'"==""|"`tsha_p'"=="T") local tsha_p=.
|
| if ("`tsha_s'"=="") local tsha_s `tsha_p'
|
|
|
|
|
|
|
| if ("`selection'"=="J") {
|
| if ("`tsha_p'"!=".") {
|
| if (`tsha_p'<=`binsp') {
|
| local tsha_p=`binsp'+1
|
| local tsha_s=`tsha_p'
|
| di as text "Warning: Degree for pwc() has been changed. It must be greater than the degree for bin selection."
|
| }
|
| }
|
| }
|
| if ("`selection'"=="NA") {
|
| di as text "Warning: Testing procedures are valid when nbins() is much larger than the IMSE-optimal choice."
|
| }
|
|
|
|
|
| if ("`masspoints'"=="") {
|
| local massadj "T"
|
| local localcheck "T"
|
| }
|
| else if ("`masspoints'"=="off") {
|
| local massadj "F"
|
| local localcheck "F"
|
| }
|
| else if ("`masspoints'"=="noadjust") {
|
| local massadj "F"
|
| local localcheck "T"
|
| }
|
| else if ("`masspoints'"=="nolocalcheck") {
|
| local massadj "T"
|
| local localcheck "F"
|
| }
|
| else if ("`masspoints'"=="veryfew") {
|
| di as error "veryfew() not allowed for testing."
|
| exit
|
| }
|
|
|
|
|
| if ("`dfcheck'"=="") local dfcheck 20 30
|
| tokenize `dfcheck'
|
| local dfcheck_n1 "`1'"
|
| local dfcheck_n2 "`2'"
|
|
|
|
|
| if (`"`at'"'!=`""'&`"`at'"'!=`"mean"'&`"`at'"'!=`"median"'&`"`at'"'!=`"0"') local atwout "user"
|
|
|
|
|
| if ("`lp'"=="") local lp "inf"
|
|
|
|
|
| if ("`usegtools'"=="off") local usegtools ""
|
| if ("`usegtools'"=="on") local usegtools usegtools
|
| if ("`usegtools'"!="") {
|
| capture which gtools
|
| if (_rc) {
|
| di as error "Gtools package not installed."
|
| exit
|
| }
|
| local localcheck "F"
|
| local sel_gtools "on"
|
| }
|
| else local sel_gtools "off"
|
|
|
|
|
| if ("`absorb'"!="") {
|
| capture which reghdfe
|
| if (_rc) {
|
| di as error "reghdfe not installed."
|
| exit
|
| }
|
| }
|
|
|
|
|
| if (`tsha_p'<`tsha_s'|`binsp'<`binss') {
|
| di as error "p cannot be smaller than s."
|
| exit
|
| }
|
| if ("`tsha_p'"!="."&"`binsp'"!=".") {
|
| if (`tsha_p'<=`binsp') {
|
| di as text in gr "Warning: p for testing <= p for bins() not suggested."
|
| }
|
| }
|
| if (`tsha_p'<`deriv') {
|
| di as error "p for test cannot be smaller than deriv."
|
| exit
|
| }
|
| if (`nsims'<2000|`simsgrid'<50) {
|
| di as text "Note: A larger number random draws/evaluation points is recommended to obtain the final results."
|
| }
|
|
|
|
|
|
|
| preserve
|
|
|
|
|
| tokenize `varlist'
|
| fvrevar `1', tsonly
|
| local y_var "`r(varlist)'"
|
| fvrevar `2', tsonly
|
| local x_var "`r(varlist)'"
|
|
|
| macro shift 2
|
| local w_var "`*'"
|
|
|
|
|
| if ("`atwout'"=="user") {
|
| append using `at'
|
| }
|
|
|
| fvrevar `w_var', tsonly
|
| local w_var "`r(varlist)'"
|
| local nwvar: word count `w_var'
|
|
|
|
|
| tempname wuser
|
| if ("`atwout'"=="user") {
|
| mata: st_matrix("`wuser'", st_data(`=_N', "`w_var'"))
|
| qui drop in `=_N'
|
| }
|
|
|
|
|
| local indexlist ""
|
| local i = 1
|
| foreach v in `w_var' {
|
| if strpos("`v'", ".") == 0 {
|
| local indexlist `indexlist' `i'
|
| }
|
| local ++i
|
| }
|
|
|
|
|
| if (`"`at'"'==""&`nwvar'>0) {
|
| local at "mean"
|
| }
|
|
|
| marksample touse
|
| markout `touse' `by', strok
|
| qui keep if `touse'
|
| local eN=_N
|
|
|
|
|
| if ("`usegtools'"==""&("`masspoints'"!="off"|"`binspos'"=="QS")) {
|
| if ("`:sortedby'"!="`x_var'") sort `x_var', stable
|
| }
|
|
|
|
|
|
|
| local byvarname `by'
|
| capture confirm numeric variable `by'
|
| if _rc {
|
| local bystring "T"
|
|
|
| tempvar by
|
| tempname bylabel
|
| qui egen `by'=group(`byvarname'), lname(`bylabel')
|
| }
|
|
|
| local bylabel `:value label `by''
|
|
|
| tempname byvalmatrix
|
| qui tab `by', nofreq matrow(`byvalmatrix')
|
|
|
|
|
| local bynum=r(r)
|
| if (`bynum'==1) {
|
| di as error "More than one group is required."
|
| exit
|
| }
|
| tempname xminmat xmaxmat Nmat
|
| matrix `xminmat'=J(`bynum', 1, .)
|
| matrix `xmaxmat'=`xminmat'
|
| matrix `Nmat'=`xminmat'
|
| local Ntotal=0
|
| forvalues i=1/`bynum' {
|
| local byv `=`byvalmatrix'[`i',1]'
|
| local byvals `byvals' `byv'
|
| if ("`wtype'"=="f") sum `x_var' if `by'==`byv' `wt', meanonly
|
| else sum `x_var' if `by'==`byv', meanonly
|
| mat `xminmat'[`i',1]=r(min)
|
| mat `xmaxmat'[`i',1]=r(max)
|
| mat `Nmat'[`i',1]=r(N)
|
| local Ntotal=`Ntotal'+r(N)
|
| }
|
| mata: st_local("Ntotal", strofreal(sum(st_matrix("`Nmat'"))))
|
|
|
|
|
| mata: st_local("max_xmin", strofreal(max(st_matrix("`xminmat'"))));
|
| st_local("min_xmax", strofreal(min(st_matrix("`xmaxmat'"))));
|
| st_local("xmin", strofreal(min(st_matrix("`xminmat'"))));
|
| st_local("xmax", strofreal(max(st_matrix("`xmaxmat'"))))
|
|
|
|
|
|
|
| tempname xvec yvec byvec cluvec binedges
|
| mata: `xvec'=st_data(., "`x_var'"); `yvec'=st_data(.,"`y_var'"); `byvec'=.; `cluvec'=.
|
|
|
|
|
|
|
| tempname Ndistlist Nclustlist mat_imse_var_rot mat_imse_bsq_rot mat_imse_var_dpi mat_imse_bsq_dpi
|
| mat `Ndistlist'=J(`bynum',1,.)
|
| mat `Nclustlist'=J(`bynum',1,.)
|
|
|
| mat `mat_imse_var_rot'=J(`bynum',1,.)
|
| mat `mat_imse_bsq_rot'=J(`bynum',1,.)
|
| mat `mat_imse_var_dpi'=J(`bynum',1,.)
|
| mat `mat_imse_bsq_dpi'=J(`bynum',1,.)
|
|
|
| if (`bynum'>1) mata: `byvec'=st_data(.,"`by'")
|
| if ("`clusterON'"=="T") mata: `cluvec'=st_data(.,"`clustervar'")
|
|
|
|
|
|
|
|
|
|
|
|
|
| tempname fullkmat
|
|
|
|
|
| if ("`binspos'"!="ES"&"`binspos'"!="QS") {
|
| capture numlist "`binspos'", ascending
|
| if (_rc==0) {
|
| local knotlistON "T"
|
| local knotlist `binspos'
|
| local nbins_all: word count `knotlist'
|
| local first: word 1 of `knotlist'
|
| local last: word `nbins_all' of `knotlist'
|
| if (`first'<=`max_xmin'|`last'>=`min_xmax') {
|
| di as error "Inner knots specified out of allowed range."
|
| exit
|
| }
|
| else {
|
| local nbins_all=`nbins_all'+1
|
| local pos "user"
|
|
|
| foreach el of local knotlist {
|
| mat `fullkmat'=(nullmat(`fullkmat') \ `el')
|
| }
|
| mat `fullkmat'=(`xmin' \ `fullkmat' \ `xmax')
|
| }
|
| }
|
| else {
|
| di as error "Numeric list incorrectly specified in binspos()."
|
| exit
|
| }
|
| }
|
|
|
|
|
| if ("`selection'"!="NA" & "`samebinsby'"!="") {
|
| local selectfullON "T"
|
| }
|
|
|
| if ("`selectfullON'"=="T") {
|
| local Ndist=.
|
| if ("`massadj'"=="T") {
|
| if ("`numdist'"!=""&"`numdist'"!=".") {
|
| local Ndist=`numdist'
|
| }
|
| else {
|
| if ("`usegtools'"=="") {
|
| mata: `binedges'=binsreg_uniq(`xvec', ., 1, "Ndist")
|
| mata: mata drop `binedges'
|
| }
|
| else {
|
| qui gunique `x_var'
|
| local Ndist=r(unique)
|
| }
|
| }
|
| local eN=min(`eN', `Ndist')
|
| }
|
|
|
| local Nclust=.
|
| if ("`clusterON'"=="T") {
|
| if ("`numclust'"!=""&"`numclust'"!=".") {
|
| local Nclust=`numclust'
|
| }
|
| else {
|
| if ("`usegtools'"=="") {
|
| mata: st_local("Nclust", strofreal(rows(uniqrows(`cluvec'))))
|
| }
|
| else {
|
| qui gunique `clustervar'
|
| local Nclust=r(unique)
|
| }
|
| }
|
| local eN=min(`eN', `Nclust')
|
| }
|
|
|
|
|
| if ("`binsp'"==".") local binspcheck=6
|
| else local binspcheck=`binsp'
|
| if ("`nbinsrot'"==""&(`eN'<=`dfcheck_n1'+`binspcheck'+1+`qrot')) {
|
|
|
| di as error "Too few observations for bin selection."
|
| exit
|
| }
|
| else {
|
| local randcut1k `randcut'
|
| if ("`randcut'"=="" & `Ntotal'>5000) {
|
| local randcut1k=max(5000/`Ntotal', 0.01)
|
| di as text in gr "Warning: To speed up computation, bin/degree selection uses a subsample of roughly max(5000, 0.01n) observations if n>5000. To use the full sample, set randcut(1)."
|
| }
|
| if ("`selection'"=="J") {
|
| qui binsregselect `y_var' `x_var' `w_var' `wt', deriv(`deriv') bins(`binsp' `binss') nbins()
|
| absorb(`absorb') reghdfeopt(`reghdfeopt')
|
| binsmethod(`binsmethod') binspos(`binspos') nbinsrot(`nbinsrot')
|
| `vce_select' masspoints(`masspoints') dfcheck(`dfcheck_n1' `dfcheck_n2')
|
| numdist(`Ndist') numclust(`Nclust') randcut(`randcut1k') usegtools(`sel_gtools')
|
| if (e(nbinsrot_regul)==.) {
|
| di as error "Bin selection fails."
|
| exit
|
| }
|
| if ("`binsmethod'"=="ROT") {
|
| local nbins_all=e(nbinsrot_regul)
|
| mat `mat_imse_var_rot'=J(`bynum',1,e(imse_var_rot))
|
| mat `mat_imse_bsq_rot'=J(`bynum',1,e(imse_bsq_rot))
|
| }
|
| else if ("`binsmethod'"=="DPI") {
|
| local nbins_all=e(nbinsdpi)
|
| mat `mat_imse_var_dpi'=J(`bynum',1,e(imse_var_dpi))
|
| mat `mat_imse_bsq_dpi'=J(`bynum',1,e(imse_bsq_dpi))
|
| if (`nbins_all'==.) {
|
| local nbins_all=e(nbinsrot_regul)
|
| mat `mat_imse_var_rot'=J(`bynum',1,e(imse_var_rot))
|
| mat `mat_imse_bsq_rot'=J(`bynum',1,e(imse_bsq_rot))
|
| di as text in gr "Warning: DPI selection fails. ROT choice used."
|
| }
|
| }
|
| }
|
| else if ("`selection'"=="P") {
|
| qui binsregselect `y_var' `x_var' `w_var' `wt', deriv(`deriv') nbins(`nbins_all')
|
| absorb(`absorb') reghdfeopt(`reghdfeopt')
|
| pselect(`plist') sselect(`slist')
|
| binsmethod(`binsmethod') binspos(`binspos') nbinsrot(`nbinsrot')
|
| `vce_select' masspoints(`masspoints') dfcheck(`dfcheck_n1' `dfcheck_n2')
|
| numdist(`Ndist') numclust(`Nclust') randcut(`randcut1k') usegtools(`sel_gtools')
|
| if (e(prot_regul)==.) {
|
| di as error "Bin selection fails."
|
| exit
|
| }
|
| if ("`binsmethod'"=="ROT") {
|
| local binsp=e(prot_regul)
|
| local binss=e(srot_regul)
|
| mat `mat_imse_var_rot'=J(`bynum',1,e(imse_var_rot))
|
| mat `mat_imse_bsq_rot'=J(`bynum',1,e(imse_bsq_rot))
|
| }
|
| else if ("`binsmethod'"=="DPI") {
|
| local binsp=e(pdpi)
|
| local binss=e(sdpi)
|
| mat `mat_imse_var_dpi'=J(`bynum',1,e(imse_var_dpi))
|
| mat `mat_imse_bsq_dpi'=J(`bynum',1,e(imse_bsq_dpi))
|
| if (`binsp'==.) {
|
| local binsp=e(prot_regul)
|
| local binss=e(srot_regul)
|
| mat `mat_imse_var_rot'=J(`bynum',1,e(imse_var_rot))
|
| mat `mat_imse_bsq_rot'=J(`bynum',1,e(imse_bsq_rot))
|
| di as text in gr "Warning: DPI selection fails. ROT choice used."
|
| }
|
| }
|
| local tsha_p=`binsp'+1
|
| local tsha_s=`binss'+1
|
| }
|
| }
|
| }
|
|
|
| if ("`selectfullON'"=="T"|("`selection'"=="NA"&"`samebinsby'"!="")) {
|
|
|
| local knotlistON "T"
|
| if ("`binspos'"=="ES") {
|
| local stepsize=(`xmax'-`xmin')/`nbins_all'
|
| forvalues i=1/`=`nbins_all'+1' {
|
| mat `fullkmat'=(nullmat(`fullkmat') \ `=`xmin'+`stepsize'*(`i'-1)')
|
| }
|
| }
|
| else if ("`binspos'"=="QS") {
|
| if (`nbins_all'==1) mat `fullkmat'=(`xmin' \ `xmax')
|
| else {
|
| binsreg_pctile `x_var' `wt', nq(`nbins_all') `usegtools'
|
| mat `fullkmat'=(`xmin' \ r(Q) \ `xmax')
|
| }
|
| }
|
| }
|
|
|
|
|
| if ("`pos'"=="user") {
|
| local binselectmethod "User-specified"
|
| local placement "User-specified"
|
| }
|
| else if ("`binspos'"=="ES") {
|
| local placement "Evenly-spaced"
|
| }
|
| else if ("`binspos'"=="QS") {
|
| local placement "Quantile-spaced"
|
| }
|
|
|
|
|
|
|
|
|
| if ("`simsseed'"!="") set seed `simsseed'
|
|
|
|
|
| tempname Xm uni_grid uni_grid_bin uni_basis num denom nummat tstat pmat xsub ysub byindex xcatsub
|
| mata: `tstat'=J(`=`bynum'*(`bynum'-1)/2',3,.); `pmat'=J(`=`bynum'*(`bynum'-1)/2',1,.)
|
|
|
| tempname Xm0 fit fit0 se vcov
|
| mata: `Xm0'=.; `fit'=.; `fit0'=0; `se'=.; `vcov'=.
|
|
|
| tempvar xcat bycond
|
| qui gen `xcat'=. in 1
|
| qui gen `bycond'=. in 1
|
|
|
|
|
| tempname nbinslist teststat pvalue pwc_plist pwc_slist
|
|
|
|
|
| mata: `uni_grid'=rangen(`max_xmin', `min_xmax', `simsgrid'+2);
|
| `uni_grid'=`uni_grid'[|2 \ `=`simsgrid'+1'|]
|
|
|
|
|
| tempname wval
|
| if (`nwvar'>0) {
|
| if (`"`at'"'==`"mean"'|`"`at'"'==`"median"') {
|
| matrix `wval'=J(1, `nwvar', 0)
|
| tempname wvaltemp mataobj
|
| mata: `mataobj'=.
|
| foreach wpos in `indexlist' {
|
| local wname: word `wpos' of `w_var'
|
| if ("`usegtools'"=="") {
|
| if ("`wtype'"!="") qui tabstat `wname' [aw`exp'], stat(`at') save
|
| else qui tabstat `wname', stat(`at') save
|
| mat `wvaltemp'=r(StatTotal)
|
| }
|
| else {
|
| qui gstats tabstat `wname' `wt', stat(`at') matasave("`mataobj'")
|
| mata: st_matrix("`wvaltemp'", `mataobj'.getOutputCol(1))
|
| }
|
| mat `wval'[1,`wpos']=`wvaltemp'[1,1]
|
| }
|
| mata: mata drop `mataobj'
|
| }
|
| else if (`"`at'"'==`"0"') {
|
| matrix `wval'=J(1,`nwvar',0)
|
| }
|
| else if ("`atwout'"=="user") {
|
| matrix `wval'=`wuser'
|
| }
|
| }
|
|
|
|
|
| tempname wvec wvec0
|
| mata: `wvec'=J(1,0,.); `wvec0'=J(1,0,.)
|
| if (`nwvar'>0) {
|
| mata: `wvec0'=st_matrix("`wval'")
|
| if (`deriv'==0&"`asyvar'"=="off") mata: `wvec'=(`wvec', `wvec0')
|
| else mata: `wvec'=(`wvec', J(1,`nwvar',0))
|
| }
|
| if ("`estmethod'"=="qreg"|"`estmethod'"=="reghdfe") {
|
| mata: `wvec0'=(`wvec0', 1)
|
| if (`deriv'==0) mata: `wvec'=(`wvec', 1)
|
| else mata: `wvec'=(`wvec', 0)
|
| }
|
|
|
|
|
| local byvalnamelist ""
|
| local counter=1
|
| local counter2=1
|
|
|
|
|
|
|
| foreach byval in `byvals' {
|
| local conds "if `by'==`byval'"
|
| qui replace `bycond'=(`by'==`byval')
|
|
|
| if ("`bylabel'"=="") local byvalname=`byval'
|
| else {
|
| local byvalname `: label `bylabel' `byval''
|
| }
|
| local byvalnamelist `" `byvalnamelist' `"`byvalname'"' "'
|
|
|
| mata: `byindex'=`byvec':==`byval'
|
| mata: `xsub'=select(`xvec',`byindex'); `ysub'=select(`yvec', `byindex')
|
|
|
|
|
|
|
|
|
| if ("`wtype'"=="f") sum `x_var' `conds' `wt', meanonly
|
| else sum `x_var' `conds', meanonly
|
|
|
| local xmin=r(min)
|
| local xmax=r(max)
|
| local N=r(N)
|
|
|
|
|
| if ("`wtype'"!="f") local eN=r(N)
|
| else {
|
| qui count `conds'
|
| local eN=r(N)
|
| }
|
|
|
| local Ndist=.
|
| if ("`massadj'"=="T") {
|
| if ("`usegtools'"=="") {
|
| mata: `binedges'=binsreg_uniq(`xsub', ., 1, "Ndist")
|
| mata: mata drop `binedges'
|
| }
|
| else {
|
| qui gunique `x_var' `conds'
|
| local Ndist=r(unique)
|
| }
|
| local eN=min(`eN', `Ndist')
|
| mat `Ndistlist'[`counter',1]=`Ndist'
|
| }
|
|
|
|
|
| local Nclust=.
|
| if ("`clusterON'"=="T") {
|
| if ("`usegtools'"=="") {
|
| mata: st_local("Nclust", strofreal(rows(uniqrows(select(`cluvec', `byindex')))))
|
| }
|
| else {
|
| qui gunique `clustervar' `conds'
|
| local Nclust=r(unique)
|
| }
|
| local eN=min(`eN', `Nclust')
|
| mat `Nclustlist'[`counter',1]=`Nclust'
|
| }
|
|
|
|
|
|
|
|
|
| if ("`pos'"!="user") local pos `binspos'
|
|
|
|
|
| local nbins ""
|
| if (`lenbynbins'>1) local nbins: word `counter' of `bynbins'
|
| if ("`nbins_all'"!="") local nbins=`nbins_all'
|
|
|
| if ("`selection'"!="NA"&"`knotlistON'"!="T") {
|
|
|
| if ("`binsp'"==".") local binspcheck=6
|
| else local binspcheck=`binsp'
|
| if ("`nbinsrot'"==""&(`eN'<=`dfcheck_n1'+`binspcheck'+1+`qrot')) {
|
| di as error "Too few observations for bin selection."
|
| exit
|
| }
|
| else {
|
| local randcut1k `randcut'
|
| if ("`randcut'"=="" & `N'>5000) {
|
| local randcut1k=max(5000/`N', 0.01)
|
| di as text in gr "Warning: To speed up computation, bin/degree selection uses a subsample of roughly max(5000, 0.01n) observations if n>5000. To use the full sample, set randcut(1)."
|
| }
|
| if ("`selection'"=="J") {
|
| qui binsregselect `y_var' `x_var' `w_var' `conds' `wt', deriv(`deriv')
|
| bins(`binsp' `binss') nbins()
|
| absorb(`absorb') reghdfeopt(`reghdfeopt')
|
| binsmethod(`binsmethod') binspos(`pos') nbinsrot(`nbinsrot')
|
| `vce_select' masspoints(`masspoints') dfcheck(`dfcheck_n1' `dfcheck_n2')
|
| numdist(`Ndist') numclust(`Nclust') randcut(`randcut1k') usegtools(`sel_gtools')
|
| if (e(nbinsrot_regul)==.) {
|
| di as error "Bin selection fails."
|
| exit
|
| }
|
| if ("`binsmethod'"=="ROT") {
|
| local nbins=e(nbinsrot_regul)
|
| mat `mat_imse_bsq_rot'[`counter',1]=e(imse_bsq_rot)
|
| mat `mat_imse_var_rot'[`counter',1]=e(imse_var_rot)
|
| }
|
| else if ("`binsmethod'"=="DPI") {
|
| local nbins=e(nbinsdpi)
|
| mat `mat_imse_bsq_dpi'[`counter',1]=e(imse_bsq_dpi)
|
| mat `mat_imse_var_dpi'[`counter',1]=e(imse_var_dpi)
|
| if (`nbins'==.) {
|
| local nbins=e(nbinsrot_regul)
|
| mat `mat_imse_bsq_rot'[`counter',1]=e(imse_bsq_rot)
|
| mat `mat_imse_var_rot'[`counter',1]=e(imse_var_rot)
|
| di as text in gr "Warning: DPI selection fails. ROT choice used."
|
| }
|
| }
|
| }
|
| else if ("`selection'"=="P") {
|
| qui binsregselect `y_var' `x_var' `w_var' `wt', deriv(`deriv') nbins(`nbins')
|
| absorb(`absorb') reghdfeopt(`reghdfeopt')
|
| pselect(`plist') sselect(`slist')
|
| binsmethod(`binsmethod') binspos(`binspos') nbinsrot(`nbinsrot')
|
| `vce_select' masspoints(`masspoints') dfcheck(`dfcheck_n1' `dfcheck_n2')
|
| numdist(`Ndist') numclust(`Nclust') randcut(`randcut1k') usegtools(`sel_gtools')
|
| if (e(prot_regul)==.) {
|
| di as error "Bin selection fails."
|
| exit
|
| }
|
| if ("`binsmethod'"=="ROT") {
|
| local binsp=e(prot_regul)
|
| local binss=e(srot_regul)
|
| mat `mat_imse_bsq_rot'[`counter',1]=e(imse_bsq_rot)
|
| mat `mat_imse_var_rot'[`counter',1]=e(imse_var_rot)
|
| }
|
| else if ("`binsmethod'"=="DPI") {
|
| local binsp=e(pdpi)
|
| local binss=e(sdpi)
|
| mat `mat_imse_bsq_dpi'[`counter',1]=e(imse_bsq_dpi)
|
| mat `mat_imse_var_dpi'[`counter',1]=e(imse_var_dpi)
|
| if (`binsp'==.) {
|
| local binsp=e(prot_regul)
|
| local binss=e(srot_regul)
|
| mat `mat_imse_bsq_rot'[`counter',1]=e(imse_bsq_rot)
|
| mat `mat_imse_var_rot'[`counter',1]=e(imse_var_rot)
|
| di as text in gr "Warning: DPI selection fails. ROT choice used."
|
| }
|
| }
|
| local tsha_p=`binsp'+1
|
| local tsha_s=`binss'+1
|
| }
|
| }
|
| }
|
|
|
|
|
|
|
| if ((`nbins'-1)*(`tsha_p'-`tsha_s'+1)+`tsha_p'+1+`dfcheck_n2'>=`eN') {
|
| di as text in gr "Warning: Too small effective sample size for testing shape."
|
| }
|
|
|
|
|
| tempname kmat
|
| if ("`knotlistON'"=="T") {
|
| mat `kmat'=`fullkmat'
|
| }
|
| else {
|
| if ("`pos'"=="ES") {
|
| local stepsize=(`xmax'-`xmin')/`nbins'
|
| forvalues i=1/`=`nbins'+1' {
|
| mat `kmat'=(nullmat(`kmat') \ `=`xmin'+`stepsize'*(`i'-1)')
|
| }
|
| }
|
| else {
|
| if (`nbins'==1) mat `kmat'=(`xmin' \ `xmax')
|
| else {
|
| binsreg_pctile `x_var' `conds' `wt', nq(`nbins') `usegtools'
|
| mat `kmat'=(`xmin' \ r(Q) \ `xmax')
|
| }
|
| }
|
| }
|
|
|
|
|
| mata: st_matrix("`kmat'", (`xmin' \ uniqrows(st_matrix("`kmat'")[|2 \ `=`nbins'+1'|])))
|
| if (`nbins'!=rowsof(`kmat')-1) {
|
| di as text in gr "Warning: Repeated knots. Some bins dropped."
|
| local nbins=rowsof(`kmat')-1
|
| }
|
| binsreg_irecode `x_var' `conds', knotmat(`kmat') bin(`xcat')
|
| `usegtools' nbins(`nbins') pos(`pos') knotliston(`knotlistON')
|
|
|
| mata: `xcatsub'=st_data(., "`xcat'")
|
| mata: `xcatsub'=select(`xcatsub', `byindex')
|
|
|
|
|
| mat `nbinslist'=(nullmat(`nbinslist') \ `nbins')
|
| mat `pwc_plist'=(nullmat(`pwc_plist') \ `tsha_p')
|
| mat `pwc_slist'=(nullmat(`pwc_slist') \ `tsha_s')
|
|
|
|
|
| if ("`localcheck'"=="T") {
|
| mata: st_local("Ncat", strofreal(rows(uniqrows(`xcatsub'))))
|
| if (`nbins'==`Ncat') {
|
| mata: `binedges'=binsreg_uniq(`xsub', `xcatsub', `nbins', "uniqmin")
|
| mata: mata drop `binedges'
|
| }
|
| else {
|
| local uniqmin=0
|
| di as text in gr "Warning: There are empty bins. Specify a smaller number in nbins()."
|
| }
|
|
|
| if (`uniqmin'<`tsha_p'+1) {
|
| di as text in gr "Warning: Some bins have too few distinct x-values for testing."
|
| }
|
| }
|
|
|
|
|
|
|
|
|
| local nseries=(`tsha_p'-`tsha_s'+1)*(`nbins'-1)+`tsha_p'+1
|
| local tsha_series ""
|
| forvalues i=1/`nseries' {
|
| tempvar sp`i'
|
| local tsha_series `tsha_series' `sp`i''
|
| qui gen `sp`i''=. in 1
|
| }
|
|
|
| tempname tsha_b tsha_V
|
| mata: binsreg_st_spdes(`xsub', "`tsha_series'", "`kmat'", `xcatsub', `tsha_p', 0, `tsha_s', "`bycond'")
|
| if ("`estmethod'"!="qreg"&"`estmethod'"!="reghdfe") {
|
| capture `estcmd' `y_var' `tsha_series' `w_var' `wt', nocon `vce' `estmethodopt'
|
| }
|
| else if ("`estmethod'"=="qreg") {
|
| if ("`boot'"=="on") capture bsqreg `y_var' `tsha_series' `w_var', quantile(`quantile') reps(`reps')
|
| else capture qreg `y_var' `tsha_series' `w_var' `wt', quantile(`quantile') `vce' `estmethodopt'
|
| }
|
| else {
|
| capture `estcmd' `y_var' `tsha_series' `w_var' `wt', absorb(`absorb') `reghdfeopt' `vce'
|
| }
|
|
|
|
|
| if (_rc==0) {
|
| matrix `tsha_b'=e(b)
|
| matrix `tsha_V'=e(V)
|
| if ("`estmethod'"!="qreg"&"`estmethod'"!="reghdfe") mata: binsreg_checkdrop("`tsha_b'", "`tsha_V'", `nseries')
|
| else mata: binsreg_checkdrop("`tsha_b'", "`tsha_V'", `nseries', "T")
|
| matrix `tsha_b'=`tsha_b''
|
| }
|
| else {
|
| error _rc
|
| exit _rc
|
| }
|
|
|
|
|
| mata: `uni_grid_bin'`counter'=binspwc_locate(`uni_grid', st_matrix("`kmat'"))
|
|
|
|
|
| mata: `uni_basis'=binsreg_spdes(`uni_grid', "`kmat'", `uni_grid_bin'`counter', `tsha_p', `deriv', `tsha_s')
|
| if (("`estmethod'"=="logit"|"`estmethod'"=="probit")&"`transform'"=="T") {
|
| if (`deriv'==0) {
|
| mata: `fit0'=(`uni_basis', J(rows(`uni_basis'),1,1)#`wvec0')*st_matrix("`tsha_b'")
|
| if ("`estmethod'"=="logit") {
|
| mata: `fit'=logistic(`fit0');
|
| `se'=logisticden(`fit0'):*
|
| binsreg_pred((`uni_basis', J(rows(`uni_basis'),1,1)#`wvec'),.,st_matrix("`tsha_V'"),"se")[,2]
|
| }
|
| else {
|
| mata: `fit'=normal(`fit0');
|
| `se'=normalden(`fit0'):*
|
| binsreg_pred((`uni_basis', J(rows(`uni_basis'),1,1)#`wvec'),.,st_matrix("`tsha_V'"),"se")[,2]
|
| }
|
| }
|
| if (`deriv'==1) {
|
| mata: `Xm0'=binsreg_spdes(`uni_grid', "`kmat'", `uni_grid_bin'`counter', `tsha_p', 0, `tsha_s');
|
| `Xm0'=(`Xm0', J(rows(`Xm0'),1,1)#`wvec0');
|
| `fit0'=`Xm0'*st_matrix("`tsha_b'");
|
| `Xm'=(`uni_basis', J(rows(`uni_basis'),1,1)#`wvec')
|
| if ("`estmethod'"=="logit") {
|
| mata: `fit'=binsreg_pred(`Xm',st_matrix("`tsha_b'"),.,"xb")[,1]
|
| if ("`asyvar'"=="off") {
|
| mata: `Xm'=logisticden(`fit0'):*(1:-2*logistic(`fit0')):*`fit':*`Xm0' +
|
| logisticden(`fit0'):*`Xm';
|
| `se'=sqrt(rowsum((`Xm'*st_matrix("`tsha_V'")):*`Xm'))
|
| }
|
| else {
|
| mata: `se'=logisticden(`fit0'):*(binsreg_pred(`Xm',.,st_matrix("`tsha_V'"),"se")[,2])
|
| }
|
| mata: `fit'=logisticden(`fit0'):*`fit'
|
| }
|
| else {
|
| mata: `fit'=binsreg_pred(`Xm',st_matrix("`tsha_b'"),.,"xb")[,1]
|
| if ("`asyvar'"=="off") {
|
| mata:`Xm'=(-`fit0'):*normalden(`fit0'):*`fit':*`Xm0' +
|
| normalden(`fit0'):*`Xm';
|
| `se'=sqrt(rowsum((`Xm'*st_matrix("`tsha_V'")):*`Xm'))
|
| }
|
| else {
|
| mata: `se'=normalden(`fit0'):*(binsreg_pred(`Xm',.,st_matrix("`tsha_V'"),"se")[,2])
|
| }
|
| mata: `fit'=normalden(`fit0'):*`fit'
|
| }
|
| }
|
| mata: `Xm'=(`fit', `se')
|
| }
|
| else {
|
| mata: `Xm'=(`uni_basis', J(rows(`uni_basis'),1,1)#`wvec');
|
| `Xm'=binsreg_pred(`Xm', st_matrix("`tsha_b'"), st_matrix("`tsha_V'"), "all")
|
| }
|
|
|
|
|
|
|
| mata: `num'`counter'=`Xm'[,1];
|
| `denom'`counter'=`Xm'[,2]
|
|
|
|
|
| if ("`estmethod'"=="qreg"|"`estmethod'"=="reghdfe") {
|
| if (`deriv'==0) mata: `uni_basis'=(`uni_basis', J(rows(`uni_basis'),1,1))
|
| else mata: `uni_basis'=(`uni_basis', J(rows(`uni_basis'),1,0))
|
| mata: `vcov'=st_matrix("`tsha_V'");
|
| `vcov'= (`vcov'[|1,1 \ `nseries', `nseries'|], `vcov'[|1,cols(`vcov') \ `nseries', cols(`vcov')|] \
|
| `vcov'[|cols(`vcov'), 1 \ cols(`vcov'), `nseries'|], `vcov'[cols(`vcov'), cols(`vcov')]);
|
| st_matrix("`vcov'", `vcov')
|
| }
|
|
|
| if ("`estmethod'"!="qreg"&"`estmethod'"!="reghdfe") {
|
| mata: `se'`counter'=binsreg_pred(`uni_basis', ., st_matrix("`tsha_V'")[|1,1 \ `nseries',`nseries'|], "se")[,2];
|
| `nummat'`counter'=binspwc_nummat(`uni_basis', "`tsha_V'", `nseries')
|
| }
|
| else {
|
| mata: `se'`counter'=binsreg_pred(`uni_basis', ., `vcov', "se")[,2];
|
| `nummat'`counter'=binspwc_nummat(`uni_basis', "`vcov'", `=`nseries'+1')
|
| }
|
|
|
|
|
|
|
| if (`counter'>1) {
|
| forval gr=1/`=`counter'-1' {
|
|
|
| if ("`testtype'"=="l") {
|
| mata: `tstat'[`counter2',.]=(max((`num'`counter'-`num'`gr'):/
|
| sqrt((`denom'`counter':^2)+(`denom'`gr':^2))), `counter', `gr')
|
| }
|
| else if ("`testtype'"=="r") {
|
| mata: `tstat'[`counter2',.]=(min((`num'`counter'-`num'`gr'):/
|
| sqrt((`denom'`counter':^2)+(`denom'`gr':^2))), `counter', `gr')
|
| }
|
| else {
|
| if ("`lp'"=="inf") {
|
| mata: `tstat'[`counter2',.]=(max(abs((`num'`counter'-`num'`gr'):/
|
| sqrt((`denom'`counter':^2)+(`denom'`gr':^2)))), `counter', `gr')
|
| }
|
| else {
|
| mata: `tstat'[`counter2',.]=(mean(((`num'`counter'-`num'`gr'):/
|
| sqrt((`denom'`counter':^2)+(`denom'`gr':^2))):^`lp')^(1/`lp'), `counter', `gr')
|
| }
|
| }
|
|
|
|
|
| mata: `pmat'[`counter2',1]=binspwc_pval(`nummat'`counter', `nummat'`gr', `se'`counter', `se'`gr',
|
| `tstat'[`counter2',1], `nsims', "`testtype'", "`lp'")
|
|
|
| local ++counter2
|
| }
|
| }
|
|
|
| drop `tsha_series'
|
|
|
| local ++counter
|
|
|
| }
|
|
|
| mata: st_matrix("`teststat'", `tstat'); st_matrix("`pvalue'", `pmat')
|
|
|
|
|
| mata: mata drop `Xm' `uni_grid' `uni_basis' `tstat' `pmat' `xsub' `ysub' `byindex' `xcatsub'
|
| `xvec' `yvec' `byvec' `cluvec' `Xm0' `fit' `fit0' `se' `vcov' `wvec' `wvec0'
|
|
|
| mata: mata drop `uni_grid_bin'* `num'* `denom'* `nummat'* `se'*
|
|
|
|
|
|
|
|
|
|
|
| di ""
|
| di in smcl in gr "Pairwise group comparison based on binscatter estimates"
|
| di in smcl in gr "Estimation method: `estmethod'"
|
| di in smcl in gr "Derivative: `deriv'"
|
| di in smcl in gr "Group variable: `byvarname'"
|
| di in smcl in gr "Bin/Degree selection method: `binselectmethod'"
|
| di in smcl in gr "Placement: `placement'"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| forval i=1/`=`counter2'-1' {
|
| local g1=`=`teststat'[`i',2]'
|
| local g2=`=`teststat'[`i',3]'
|
| local group1: word `g1' of `byvalnamelist'
|
| local group2: word `g2' of `byvalnamelist'
|
|
|
| di ""
|
| di in smcl in gr "Group `group1' vs. Group `group2'"
|
| di in smcl in gr "{hline 30}{c TT}{hline 10}{c TT}{hline 10}"
|
| di in smcl in gr "{lalign 1:Group `byvarname'=}" _col(30) " {c |}" _col(38) "`group1'" _col(42) "{c |}" _col(48) "`group2'"
|
| di in smcl in gr "{hline 30}{c +}{hline 10}{c +}{hline 10}"
|
| di in smcl in gr "{lalign 1:# of observations}" _col(30) " {c |} " _col(32) as result %7.0f `=`Nmat'[`g1',1]' _col(42) in gr "{c |}" _col(44) as result %7.0f `=`Nmat'[`g2',1]'
|
| di in smcl in gr "{lalign 1:# of distinct values}" _col(30) " {c |} " _col(32) as result %7.0f `=`Ndistlist'[`g1',1]' _col(42) in gr "{c |}" _col(44) as result %7.0f `=`Ndistlist'[`g2',1]'
|
| di in smcl in gr "{lalign 1:# of clusters}" _col(30) " {c |} " _col(32) as result %7.0f `=`Nclustlist'[`g1',1]' _col(42) in gr "{c |}" _col(44) as result %7.0f `=`Nclustlist'[`g2',1]'
|
| di in smcl in gr "{lalign 1:Degree of polynomial}" _col(30) " {c |} " _col(32) as result %7.0f `=`pwc_plist'[`g1',1]' _col(42) in gr "{c |}" _col(44) as result %7.0f `=`pwc_plist'[`g2',1]'
|
| di in smcl in gr "{lalign 1:# of smoothness constraints}" _col(30) " {c |} " _col(32) as result %7.0f `=`pwc_slist'[`g1',1]' _col(42) in gr "{c |}" _col(44) as result %7.0f `=`pwc_slist'[`g2',1]'
|
| di in smcl in gr "{lalign 1:# of bins}" _col(30) " {c |} " _col(32) as result %7.0f `=`nbinslist'[`g1',1]' _col(42) in gr "{c |}" _col(44) as result %7.0f `=`nbinslist'[`g2',1]'
|
| di in smcl in gr "{hline 30}{c BT}{hline 21}"
|
|
|
| di ""
|
| di in smcl in gr "diff = group `group1' - group `group2'"
|
| if ("`testtype'"=="l") {
|
| di in smcl in gr "{hline 19}{c TT}{hline 30}"
|
| di in smcl in gr "H0:" _col(20) in gr
|
| "{c |}" _col(22) "sup T" _col(40) "p value"
|
| di in smcl in gr "{hline 19}{c +}{hline 30}"
|
| local stat=`teststat'[`i',1]
|
| local pval=`pvalue'[`i',1]
|
| di in smcl in gr "diff<=0" _col(20) in gr "{c |}"
|
| _col(22) as result %7.3f `stat'
|
| _col(40) as result %7.3f `pval'
|
|
|
| di in smcl in gr "{hline 19}{c BT}{hline 30}"
|
| }
|
| else if ("`testtype'"=="r") {
|
| di in smcl in gr "{hline 19}{c TT}{hline 30}"
|
| di in smcl in gr "H0:" _col(20) in gr
|
| "{c |}" _col(22) "inf T" _col(40) "p value"
|
| di in smcl in gr "{hline 19}{c +}{hline 30}"
|
| local stat=`teststat'[`i',1]
|
| local pval=`pvalue'[`i',1]
|
| di in smcl in gr "diff>=0" _col(20) in gr "{c |}"
|
| _col(22) as result %7.3f `stat'
|
| _col(40) as result %7.3f `pval'
|
| di in smcl in gr "{hline 19}{c BT}{hline 30}"
|
| }
|
| else {
|
| di in smcl in gr "{hline 19}{c TT}{hline 30}"
|
| if ("`lp'"=="inf") {
|
| di in smcl in gr "H0:" _col(20) in gr
|
| "{c |}" _col(22) "sup |T|" _col(40) "p value"
|
| }
|
| else {
|
| di in smcl in gr "H0:" _col(20) in gr
|
| "{c |}" _col(22) "L`lp' of T" _col(40) "p value"
|
| }
|
| di in smcl in gr "{hline 19}{c +}{hline 30}"
|
| local stat=`teststat'[`i',1]
|
| local pval=`pvalue'[`i',1]
|
| di in smcl in gr "diff=0" _col(20) in gr "{c |}"
|
| _col(22) as result %7.3f `stat'
|
| _col(40) as result %7.3f `pval'
|
| di in smcl in gr "{hline 19}{c BT}{hline 30}"
|
| }
|
| di ""
|
| }
|
|
|
|
|
|
|
|
|
|
|
| ereturn clear
|
|
|
| ereturn scalar N=`Ntotal'
|
| ereturn scalar p=`binsp'
|
| ereturn scalar s=`binss'
|
|
|
|
|
| ereturn matrix pval=`pvalue'
|
| ereturn matrix stat=`teststat'
|
|
|
|
|
| ereturn matrix nbins_by=`nbinslist'
|
| ereturn matrix pwc_plist=`pwc_plist'
|
| ereturn matrix pwc_slist=`pwc_slist'
|
| ereturn matrix Nclust_by=`Nclustlist'
|
| ereturn matrix Ndist_by=`Ndistlist'
|
| ereturn matrix N_by=`Nmat'
|
|
|
| ereturn matrix imse_var_rot=`mat_imse_var_rot'
|
| ereturn matrix imse_bsq_rot=`mat_imse_bsq_rot'
|
| ereturn matrix imse_var_dpi=`mat_imse_var_dpi'
|
| ereturn matrix imse_bsq_dpi=`mat_imse_bsq_dpi'
|
|
|
|
|
|
|
| ereturn local byvalue `byvalnamelist'
|
| end
|
|
|
| mata:
|
|
|
| real matrix binspwc_nummat(real matrix X, string scalar covname, real scalar k)
|
| {
|
| real matrix cov, num, U, V, sv
|
|
|
| cov=st_matrix(covname)[|1,1\k,k|]
|
| if (rank(cov)==k) {
|
| num=X*cholesky(cov)
|
| }
|
| else {
|
| svd(cov, U=., sv=., V=.)
|
| pragma unused V
|
| num=X*U*diag(sv:^0.5)*U'
|
| }
|
|
|
| return(num)
|
| }
|
|
|
|
|
|
|
| real scalar binspwc_pval(real matrix nummatA, real matrix nummatB,
|
| real vector denomA, real vector denomB,
|
| real scalar stat, real scalar rep,
|
| string scalar type, string scalar metric)
|
| {
|
| real scalar kA, kB, i, lp, pval
|
| real vector t
|
|
|
| kA=cols(nummatA)
|
| kB=cols(nummatB)
|
| if (metric!="inf") {
|
| lp=strtoreal(metric)
|
| }
|
| pval=0
|
|
|
| for (i=1; i<=rep; i++) {
|
| t=(nummatA*rnormal(kA,1,0,1)-nummatB*rnormal(kB,1,0,1)):/sqrt(denomA:^2+denomB:^2)
|
| if (type=="l") {
|
| pval=pval+(max(t)>=stat)
|
| }
|
| else if (type=="r") {
|
| pval=pval+(min(t)<=stat)
|
| }
|
| else {
|
| if (metric=="inf") {
|
| pval=pval+(max(abs(t))>=stat)
|
| }
|
| else {
|
| pval=pval+(mean(abs(t):^lp)^(1/lp)>=stat)
|
| }
|
| }
|
| }
|
|
|
| return(pval/rep)
|
| }
|
|
|
|
|
|
|
| real vector binspwc_locate(real vector x, real vector kmat)
|
| {
|
| real vector bin, index
|
| real scalar n, nbin, i, lb, ub
|
|
|
| n=rows(x)
|
| nbin=rows(kmat)-1
|
| bin=J(n,1,.)
|
| for (i=1; i<=nbin; i++) {
|
| lb=kmat[i,1]
|
| ub=kmat[i+1,1]
|
| if (i<nbin) {
|
| index=selectindex((x:>=lb):&(x:<ub))
|
| bin[index]=J(rows(index),1,i)
|
| }
|
| else {
|
| index=selectindex((x:>=lb):&(x:<=ub))
|
| bin[index]=J(rows(index),1,i)
|
| }
|
| }
|
|
|
| return(bin)
|
| }
|
|
|
| end
|
|
|