|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if c(version) < 12 & c(version) >= 9 {
|
|
|
|
|
|
|
| capture mata: mata drop m_calckw()
|
| capture mata: mata drop m_omega()
|
| capture mata: mata drop ms_vcvorthog()
|
| capture mata: mata drop s_vkernel()
|
| capture mata: mata drop s_cdsy()
|
| mata: mata mlib index
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| program define ivreg2, eclass byable(onecall) sortpreserve
|
| local lversion 04.1.11
|
|
|
|
|
| local caller = _caller()
|
|
|
|
|
| version 8
|
|
|
|
|
| if replay() {
|
|
|
|
|
| syntax [, VERsion * ]
|
| if "`version'"~="" & "`options'"=="" {
|
|
|
| di in gr "`lversion'"
|
| ereturn clear
|
| ereturn local version `lversion'
|
| exit
|
| }
|
| else if "`version'"~="" & "`options'"~="" {
|
|
|
| di as err "invalid syntax - cannot combine version with other options"
|
| exit 198
|
| }
|
| else {
|
|
|
| local 0 `", `options'"'
|
| }
|
| }
|
|
|
|
|
| if replay() & _by() {
|
| di as err "invalid syntax - cannot use by with replay"
|
| exit 601
|
| }
|
|
|
|
|
| if _by() {
|
| local BY `"by `_byvars'`_byrc0':"'
|
| }
|
|
|
|
|
|
|
|
|
| if `caller' < 11 {
|
| local ver = round(`caller')
|
| local ivreg2cmd ivreg2`ver'
|
|
|
|
|
| if replay() {
|
| ereturn local cmd "`ivreg2cmd'"
|
| `ivreg2cmd' `0'
|
| ereturn local cmd "ivreg2"
|
|
|
| }
|
| else {
|
|
|
| `BY' `ivreg2cmd' `0'
|
| ereturn local cmd "ivreg2"
|
| ereturn local ivreg2cmd "`ivreg2cmd'"
|
| ereturn local version `lversion'
|
| ereturn local predict ivreg2_p
|
| }
|
| exit
|
| }
|
|
|
|
|
|
|
| if replay() {
|
| ivreg211 `0'
|
| }
|
|
|
| else {
|
|
|
| syntax [anything] [if] [in] [aw fw pw iw] [, * ]
|
|
|
| `BY' ivreg211 `anything' `if' `in' [`weight' `exp'], `options' caller(`caller')
|
|
|
| ereturn local cmd "ivreg2"
|
| ereturn local ivreg2cmd "ivreg2"
|
| ereturn local version `lversion'
|
| ereturn local predict ivreg2_p
|
| ereturn local cmdline ivreg2 `0'
|
| }
|
|
|
| end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if c(stata_version) < 11 {
|
| exit
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| program define ivreg211, eclass byable(recall) sortpreserve
|
| version 11.2
|
|
|
| local ivreg2cmd "ivreg211"
|
| local ivreg2name "ivreg2"
|
|
|
| if replay() {
|
| syntax [,
|
| FIRST FFIRST RF SFIRST
|
| dropfirst droprf dropsfirst
|
| Level(integer $S_level)
|
| NOHEader NOFOoter
|
| EForm(string) PLUS
|
| NOOMITTED vsquish noemptycells
|
| baselevels allbaselevels
|
| VERsion
|
| caller(real 0)
|
| ]
|
| if "`version'" != "" & "`first'`ffirst'`rf'`noheader'`nofooter'`dropfirst'`droprf'`eform'`plus'" != "" {
|
| di as err "option version not allowed"
|
| error 198
|
| }
|
| if "`version'" != "" {
|
| di in gr "`lversion'"
|
| ereturn clear
|
| ereturn local version `lversion'
|
| exit
|
| }
|
| if `"`e(cmd)'"' != "ivreg2" {
|
| error 301
|
| }
|
|
|
| local dispopt eform(`eform') `noomitted' `vsquish' `noemptycells' `baselevels' `allbaselevels'
|
|
|
|
|
| if "`e(firsteqs)'" != "" & "`dropfirst'" == "" {
|
| local savefirst "savefirst"
|
| }
|
| if "`e(rfeq)'" != "" & "`droprf'" == "" {
|
| local saverf "saverf"
|
| }
|
| if "`e(sfirsteq)'" != "" & "`dropsfirst'" == "" {
|
| local savesfirst "savesfirst"
|
| }
|
|
|
| DispCollinDups
|
| }
|
| else {
|
|
|
|
|
|
|
| syntax [anything(name=0)] [if] [in] [aw fw pw iw/] [,
|
| NOID NOCOLLIN
|
| FIRST FFIRST SAVEFIRST SAVEFPrefix(name)
|
| RF SAVERF SAVERFPrefix(name)
|
| SFIRST SAVESFIRST SAVESFPrefix(name)
|
| SMall NOConstant
|
| Robust CLuster(varlist) kiefer dkraay(integer 0)
|
| BW(string) kernel(string) center
|
| GMM GMM2s CUE
|
| LIML COVIV FULLER(real 0) Kclass(real 0)
|
| ORTHOG(string) ENDOGtest(string) REDundant(string)
|
| PARTIAL(string) FWL(string)
|
| Level(integer $S_level)
|
| NOHEader NOFOoter NOOUTput
|
| bvclean NOOMITTED omitted vsquish noemptycells
|
| baselevels allbaselevels
|
| title(string) subtitle(string)
|
| DEPname(string) EForm(string) PLUS
|
| Tvar(varname) Ivar(varname)
|
| B0(string) SMATRIX(string) WMATRIX(string)
|
| sw psd0 psda useqr
|
| dofminus(integer 0) sdofminus(integer 0)
|
| NOPARTIALSMALL
|
| fvall fvsep
|
| caller(real 0)
|
| ]
|
|
|
|
|
| checkversion_ranktest `caller'
|
| local ranktestcmd `r(ranktestcmd)'
|
|
|
|
|
| sreturn clear
|
| ivparse `0', ivreg2name(`ivreg2name')
|
| partial(`partial')
|
| fwl(`fwl')
|
| orthog(`orthog')
|
| endogtest(`endogtest')
|
| redundant(`redundant')
|
| depname(`depname')
|
| `robust'
|
| cluster(`cluster')
|
| bw(`bw')
|
| kernel(`kernel')
|
| dkraay(`dkraay')
|
| `center'
|
| `kiefer'
|
| `sw'
|
| `noconstant'
|
| tvar(`tvar')
|
| ivar(`ivar')
|
| `gmm2s'
|
| `gmm'
|
| `cue'
|
| `liml'
|
| fuller(`fuller')
|
| kclass(`kclass')
|
| b0(`b0')
|
| wmatrix(`wmatrix')
|
| `noid'
|
| `savefirst'
|
| savefprefix(`savefprefix')
|
| `saverf'
|
| saverfprefix(`saverfprefix')
|
| `savesfirst'
|
| savesfprefix(`savesfprefix')
|
| dofminus(`dofminus')
|
| `psd0'
|
| `psda'
|
| `nocollin'
|
| `useqr'
|
| `bvclean'
|
| eform(`eform')
|
| `noomitted'
|
| `vsquish'
|
| `noemptycells'
|
| `baselevels'
|
| `allbaselevels'
|
|
|
|
|
| local lhs `s(lhs)'
|
| local depname `s(depname)'
|
| local endo `s(endo)'
|
| local inexog `s(inexog)'
|
| local exexog `s(exexog)'
|
| local partial `s(partial)'
|
| local cons =s(cons)
|
| local partialcons =s(partialcons)
|
| local tvar `s(tvar)'
|
| local ivar `s(ivar)'
|
| local tdelta `s(tdelta)'
|
| local tsops =s(tsops)
|
| local fvops =s(fvops)
|
| local robust `s(robust)'
|
| local cluster `s(cluster)'
|
| local bw =`s(bw)'
|
| local bwopt `s(bwopt)'
|
| local kernel `s(kernel)'
|
| local center =`s(center)'
|
| local kclassopt `s(kclassopt)'
|
| local fulleropt `s(fulleropt)'
|
| local liml `s(liml)'
|
| local noid `s(noid)'
|
| local useqr =`s(useqr)'
|
| local savefirst `s(savefirst)'
|
| local savefprefix `s(savefprefix)'
|
| local saverf `s(saverf)'
|
| local saverfprefix `s(saverfprefix)'
|
| local savesfirst `s(savesfirst)'
|
| local savesfprefix `s(savesfprefix)'
|
| local psd `s(psd)'
|
| local dofmopt `s(dofmopt)'
|
| local bvclean =`s(bvclean)'
|
| local dispopt `s(dispopt)'
|
|
|
|
|
| if `tsops' | "`kernel'"~="" {
|
| cap tsset
|
| if _rc>0 {
|
| tsset `ivar' `tvar'
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| tempvar wvar
|
| if "`weight'" == "fweight" | "`weight'"=="aweight" {
|
| local wtexp `"[`weight'=`exp']"'
|
| qui gen double `wvar'=`exp'
|
| }
|
| if "`weight'" == "fweight" & "`kernel'" !="" {
|
| di in red "fweights not allowed (data are -tsset-)"
|
| exit 101
|
| }
|
| if "`weight'" == "fweight" & "`sw'" != "" {
|
| di in red "fweights currently not supported with -sw- option"
|
| exit 101
|
| }
|
| if "`weight'" == "iweight" {
|
| if "`robust'`cluster'`gmm2s'`kernel'" !="" {
|
| di in red "iweights not allowed with robust or gmm"
|
| exit 101
|
| }
|
| else {
|
| local wtexp `"[`weight'=`exp']"'
|
| qui gen double `wvar'=`exp'
|
| }
|
| }
|
| if "`weight'" == "pweight" {
|
| local wtexp `"[aweight=`exp']"'
|
| qui gen double `wvar'=`exp'
|
| local robust "robust"
|
| }
|
| if "`weight'" == "" {
|
|
|
| qui gen byte `wvar'=1
|
| }
|
|
|
|
|
|
|
|
|
| marksample touse
|
| if `tsops' {
|
| markout `touse' `lhs' `inexog' `exexog' `endo' `cluster' `tvar', strok
|
| }
|
| else {
|
| markout `touse' `lhs' `inexog' `exexog' `endo' `cluster', strok
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| sum `wvar' if `touse' `wtexp', meanonly
|
|
|
| if "`weight'" ~= "" {
|
| di in gr "(sum of wgt is " %14.4e `r(sum_w)' ")"
|
| }
|
| if "`weight'"=="" | "`weight'"=="fweight" | "`weight'"=="iweight" {
|
|
|
|
|
| local wf=1
|
| local N=r(sum_w)
|
| }
|
| else if "`weight'"=="aweight" | "`weight'"=="pweight" {
|
| local wf=r(N)/r(sum_w)
|
| local N=r(N)
|
| }
|
| else {
|
|
|
| di as err "ivreg2 error - misspecified weights"
|
| exit 198
|
| }
|
| if `N'==0 {
|
| di as err "no observations"
|
| exit 2000
|
| }
|
|
|
|
|
|
|
|
|
|
|
| tempvar tindex
|
| qui gen `tindex'=1 if `touse'
|
| qui replace `tindex'=sum(`tindex') if `touse'
|
|
|
| if `tsops' | "`kernel'"~="" {
|
|
|
| tsreport if `touse', panel
|
| if `r(N_gaps)' != 0 {
|
| di as text "Warning: time variable " as res "`tvar'" as text " has "
|
| as res "`r(N_gaps)'" as text " gap(s) in relevant range"
|
| }
|
|
|
| sum `tvar' if `touse', meanonly
|
| local T = r(max)-r(min) + 1
|
| local T1 = `T' - 1
|
| if (`bw' > (`T1'/`tdelta')) {
|
| di as err "invalid bandwidth in option bw() - cannot exceed timespan of data"
|
| exit 198
|
| }
|
| }
|
|
|
|
|
| if "`kiefer'" ~= "" {
|
| local bw =`T'
|
| }
|
|
|
|
|
|
|
| if "`noconstant'"=="" {
|
|
|
| tempvar ones
|
| qui gen byte `ones' = 1 if `touse'
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| CheckDupsCollin,
|
| lhs(`lhs')
|
| endo(`endo')
|
| inexog(`inexog')
|
| exexog(`exexog')
|
| partial(`partial')
|
| orthog(`orthog')
|
| endogtest(`endogtest')
|
| redundant(`redundant')
|
| touse(`touse')
|
| wvar(`wvar')
|
| wf(`wf')
|
| `noconstant'
|
| `nocollin'
|
| `fvall'
|
| `fvsep'
|
|
|
|
|
| foreach vl in lhs endo inexog exexog partial orthog endogtest redundant {
|
| local `vl' `s(`vl')'
|
| local `vl'0 `s(`vl'0)'
|
| }
|
| local dups `s(dups)'
|
| local collin `s(collin)'
|
| local ecollin `s(ecollin)'
|
|
|
|
|
| foreach vl in lhs endo inexog exexog partial orthog endogtest redundant {
|
| foreach var of local `vl'0 {
|
| _ms_parse_parts `var'
|
| if ~`r(omit)' {
|
| fvrevar `var' if `touse'
|
| local `vl'1 ``vl'1' `var'
|
| local fv_`vl'1 `fv_`vl'1' `r(varlist)'
|
| }
|
| }
|
| local `vl'1 : list retokenize `vl'1
|
| local fv_`vl'1 : list retokenize fv_`vl'1
|
| }
|
|
|
|
|
| local wrongvars_ct : word count `lhs'
|
| if `wrongvars_ct' > 1 {
|
| di as err "multiple dependent variables specified: `lhs'"
|
| error 198
|
| }
|
|
|
|
|
|
|
| local wrongvars : list orthog1 - inexog1
|
| local wrongvars : list wrongvars - exexog1
|
| local wrongvars_ct : word count `wrongvars'
|
| if `wrongvars_ct' {
|
| di as err "Error: `wrongvars' listed in orthog() but does not appear as exogenous."
|
| error 198
|
| }
|
|
|
| local wrongvars : list endogtest1 - endo1
|
| local wrongvars_ct : word count `wrongvars'
|
| if `wrongvars_ct' {
|
| di as err "Error: `wrongvars' listed in endog() but does not appear as endogenous."
|
| error 198
|
| }
|
|
|
| local wrongvars : list redundant1 - exexog1
|
| local wrongvars_ct : word count `wrongvars'
|
| if `wrongvars_ct' {
|
| di as err "Error: `wrongvars' listed in redundant() but does not appear as exogenous."
|
| error 198
|
| }
|
|
|
|
|
| local allnames `lhs' `endo' `inexog' `exexog'
|
| local allnames0 `lhs0' `endo0' `inexog0' `exexog0'
|
| local allnames1 `lhs1' `endo1' `inexog1' `exexog1'
|
| local fv_allnames1 `fv_lhs1' `fv_endo1' `fv_inexog1' `fv_exexog1'
|
|
|
|
|
|
|
|
|
|
|
| if "`partial1'" != "" | `partialcons'==1 {
|
| preserve
|
|
|
|
|
|
|
| local inexog0 : list inexog0 - partial0
|
| local inexog1 : list inexog1 - partial1
|
| local fv_inexog1 : list fv_inexog1 - fv_partial1
|
|
|
|
|
|
|
| if "`cluster'"~="" {
|
| local pvarcheck : list cluster in allnames1
|
| if `pvarcheck' {
|
| di in r "Error: cannot use cluster variable `cluster' as dependent variable, regressor or IV"
|
| di in r " in combination with -partial- option."
|
| error 198
|
| }
|
| }
|
| if "`tvar'"~="" {
|
| local pvarcheck : list tvar in allnames1
|
| if `pvarcheck' {
|
| di in r "Error: cannot use time variable `tvar' as dependent variable, regressor or IV"
|
| di in r " in combination with -partial- option."
|
| error 198
|
| }
|
| }
|
| if "`ivar'"~="" {
|
| local pvarcheck : list ivar in allnames1
|
| if `pvarcheck' {
|
| di in r "Error: cannot use panel variable `ivar' as dependent variable, regressor or IV"
|
| di in r " in combination with -partial- option."
|
| error 198
|
| }
|
| }
|
| if "`wtexp'"~="" {
|
| tokenize `exp', parse("*/()+-^&|~")
|
| local wvartokens `*'
|
| local nwvarnames : list allnames1 - wvartokens
|
| local wvarnames : list allnames1 - nwvarnames
|
| if "`wvarnames'"~="" {
|
| di in r "Error: cannot use weight variables as dependent variable, regressor or IV"
|
| di in r " in combination with -partial- option."
|
| error 198
|
| }
|
| }
|
|
|
|
|
| recast double `fv_lhs1' `fv_endo1' `fv_inexog1' `fv_exexog1' `fv_partial1'
|
| mata: s_partial ("`fv_lhs1'",
|
| "`fv_endo1'",
|
| "`fv_inexog1'",
|
| "`fv_exexog1'",
|
| "`fv_partial1'",
|
| "`touse'",
|
| "`weight'",
|
| "`wvar'",
|
| `wf',
|
| `N',
|
| `cons')
|
|
|
| local partial_ct : word count `partial1'
|
|
|
| capture drop `ones'
|
| local ones ""
|
| if "`noconstant'" == "" {
|
|
|
| local partial_ct = `partial_ct' + 1
|
| local noconstant "noconstant"
|
| local cons 0
|
| }
|
| }
|
| else {
|
|
|
| local partial_ct 0
|
| local partialcons 0
|
| }
|
|
|
| if "`nopartialsmall'"=="" {
|
| local sdofminus = `sdofminus'+`partial_ct'
|
| }
|
|
|
|
|
|
|
| local rhs0 `endo0' `inexog0'
|
| local rhs1 `endo1' `inexog1'
|
| local insts1 `exexog1' `inexog1'
|
| local fv_insts1 `fv_exexog1' `fv_inexog1'
|
| local fv_rhs1 `fv_endo1' `fv_inexog1'
|
| local rhs0_ct : word count `rhs0'
|
| local rhs1_ct : word count `fv_rhs1'
|
| local iv1_ct : word count `fv_insts1'
|
| local endo1_ct : word count `fv_endo1'
|
| local exex1_ct : word count `fv_exexog1'
|
| local endoexex1_c : word count `fv_endo1' `fv_exexog1'
|
| local inexog1_ct : word count `fv_inexog1'
|
|
|
|
|
| local rhs1_ct = `rhs1_ct' + `cons'
|
| local rhs0_ct = `rhs0_ct' + `cons'
|
| local iv1_ct = `iv1_ct' + `cons'
|
|
|
|
|
| local cnb0 `endo0' `inexog0'
|
| local cnb1 `endo1' `inexog1'
|
| local cnZ0 `exexog0' `inexog0'
|
| local cnZ1 `exexog1' `inexog1'
|
| if `cons' {
|
| local cnb0 "`cnb0' _cons"
|
| local cnb1 "`cnb1' _cons"
|
| local cnZ0 "`cnZ0' _cons"
|
| local cnZ1 "`cnZ1' _cons"
|
| }
|
|
|
|
|
|
|
| CheckMisc,
|
| rhs1_ct(`rhs1_ct')
|
| iv1_ct(`iv1_ct')
|
| bvector(`b0')
|
| smatrix(`smatrix')
|
| wmatrix(`wmatrix')
|
| cnb1(`cnb1')
|
| cnZ1(`cnZ1')
|
|
|
| if "`b0'"~="" {
|
| tempname b0
|
| mat `b0' = r(b0)
|
| }
|
| if "`smatrix'"~="" {
|
| tempname S0
|
| mat `S0' = r(S0)
|
| }
|
| if "`wmatrix'"~="" {
|
| tempname wmatrix
|
| mat `wmatrix' = r(W0)
|
| }
|
|
|
|
|
| tempname YY yy yyc
|
| tempname XX X1X1 X2X2 X1Z X1Z1 XZ Xy
|
| tempname ZZ Z1Z1 Z2Z2 Z1Z2 Z1X2 Zy ZY Z2y Z2Y
|
| tempname XXinv X2X2inv ZZinv XPZXinv
|
| tempname rankxx rankzz condxx condzz
|
|
|
|
|
| mata: s_crossprods ("`fv_lhs1'",
|
| "`fv_endo1'",
|
| "`fv_inexog1' `ones'",
|
| "`fv_exexog1'",
|
| "`touse'",
|
| "`weight'",
|
| "`wvar'",
|
| `wf',
|
| `N')
|
| mat `XX' =r(XX)
|
| mat `X1X1' =r(X1X1)
|
| mat `X1Z' =r(X1Z)
|
| mat `ZZ' =r(ZZ)
|
| mat `Z2Z2' =r(Z2Z2)
|
| mat `Z1Z2' =r(Z1Z2)
|
| mat `XZ' =r(XZ)
|
| mat `Xy' =r(Xy)
|
| mat `Zy' =r(Zy)
|
| mat `YY' =r(YY)
|
| scalar `yy' =r(yy)
|
| scalar `yyc' =r(yyc)
|
| mat `ZY' =r(ZY)
|
| mat `Z2y' =r(Z2y)
|
| mat `Z2Y' =r(Z2Y)
|
| mat `XXinv' =r(XXinv)
|
| mat `ZZinv' =r(ZZinv)
|
| mat `XPZXinv' =r(XPZXinv)
|
| scalar `condxx' =r(condxx)
|
| scalar `condzz' =r(condzz)
|
|
|
| scalar `rankzz' = rowsof(`ZZinv') - diag0cnt(`ZZinv')
|
| scalar `rankxx' = rowsof(`XXinv') - diag0cnt(`XXinv')
|
| local overid = `rankzz' - `rankxx'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| local N_clust=0
|
| local N_clust1=0
|
| local N_clust2=0
|
| if "`cluster'"!="" {
|
| local clopt "cluster(`cluster')"
|
| tokenize `cluster'
|
| local cluster1 "`1'"
|
| local cluster2 "`2'"
|
| if "`kernel'"~="" {
|
|
|
|
|
|
|
| if "`cluster2'"~="" {
|
|
|
| if "`cluster1'"=="`tvar'" & "`cluster2'"=="`ivar'" {
|
| local cluster1 "`2'"
|
| local cluster2 "`1'"
|
| }
|
| if "`cluster1'"~="`ivar'" | "`cluster2'"~="`tvar'" {
|
| di as err "Error: cluster kernel-robust requires clustering on tsset panel & time vars."
|
| di as err " tsset panel var=`ivar'; tsset time var=`tvar'; cluster vars=`cluster1',`cluster2'"
|
| exit 198
|
| }
|
| }
|
| else {
|
| if "`cluster1'"~="`tvar'" {
|
| di as err "Error: cluster kernel-robust requires clustering on tsset time variable."
|
| di as err " tsset time var=`tvar'; cluster var=`cluster1'"
|
| exit 198
|
| }
|
| }
|
| }
|
|
|
|
|
| tempvar clusterid1
|
| qui egen `clusterid1'=group(`cluster1') if `touse'
|
| sum `clusterid1' if `touse', meanonly
|
| if "`cluster2'"=="" {
|
| local N_clust=r(max)
|
| local N_clust1=`N_clust'
|
| if "`kernel'"=="" {
|
|
|
|
|
| sort `cluster1'
|
| }
|
| }
|
| else {
|
| local N_clust1=r(max)
|
| if "`kernel'"=="" {
|
| tempvar clusterid2 clusterid3
|
|
|
| qui egen `clusterid2'=group(`cluster2') if `touse'
|
| qui egen `clusterid3'=group(`cluster1' `cluster2') if `touse'
|
|
|
|
|
| sort `clusterid3' `cluster1'
|
| sum `clusterid2' if `touse', meanonly
|
| local N_clust2=r(max)
|
| }
|
| else {
|
|
|
| tempvar clusterid2
|
| qui egen `clusterid2'=group(`cluster2') if `touse'
|
| sum `clusterid2' if `touse', meanonly
|
| local N_clust2=r(max)
|
|
|
| local clusterid2 `cluster2'
|
| }
|
|
|
| local N_clust=min(`N_clust1',`N_clust2')
|
|
|
| }
|
| }
|
|
|
|
|
|
|
|
|
| tempname b W S V beta lambda j jp rss mss rmse sigmasq rankV rankS
|
| tempname arubin arubinp arubin_lin arubin_linp
|
| tempname r2 r2_a r2u r2c F Fp Fdf2 ivest
|
|
|
| tempvar resid
|
| qui gen double `resid'=.
|
|
|
|
|
|
|
|
|
|
|
| if "`liml'`kclassopt'"~="" {
|
|
|
| mata: s_liml( "`ZZ'",
|
| "`XX'",
|
| "`XZ'",
|
| "`Zy'",
|
| "`Z2Z2'",
|
| "`YY'",
|
| "`ZY'",
|
| "`Z2Y'",
|
| "`Xy'",
|
| "`ZZinv'",
|
| "`fv_lhs1'",
|
| "`fv_lhs1' `fv_endo1'",
|
| "`resid'",
|
| "`fv_endo1' `fv_inexog1' `ones'",
|
| "`fv_endo1'",
|
| "`fv_exexog1' `fv_inexog1' `ones'",
|
| "`fv_exexog1'",
|
| "`fv_inexog1' `ones'",
|
| `fuller',
|
| `kclass',
|
| "`coviv'",
|
| "`touse'",
|
| "`weight'",
|
| "`wvar'",
|
| `wf',
|
| `N',
|
| "`robust'",
|
| "`clusterid1'",
|
| "`clusterid2'",
|
| "`clusterid3'",
|
| `bw',
|
| "`kernel'",
|
| "`sw'",
|
| "`psd'",
|
| "`ivar'",
|
| "`tvar'",
|
| "`tindex'",
|
| `tdelta',
|
| `center',
|
| `dofminus',
|
| `useqr')
|
|
|
| mat `b'=r(beta)
|
| mat `S'=r(S)
|
| mat `V'=r(V)
|
| scalar `lambda'=r(lambda)
|
| local kclass=r(kclass)
|
| scalar `j'=r(j)
|
| scalar `rss'=r(rss)
|
| scalar `sigmasq'=r(sigmasq)
|
| scalar `rankV'=r(rankV)
|
| scalar `rankS'=r(rankS)
|
|
|
| scalar `arubin'=(`N'-`dofminus')*ln(`lambda')
|
| scalar `arubin_lin'=(`N'-`dofminus')*(`lambda'-1)
|
|
|
|
|
| if "`nocollin'"~="" & `kclass'<1e-8 {
|
| di as err "warning: k=1 in LIML estimation; results equivalent to OLS;"
|
| di as err " may be caused by collinearities"
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
| if "`liml'`kclassopt'`b0'"=="" {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| local effic1s = (
|
| "`gmm2s'`robust'`cluster'`kernel'"==""
|
| | ("`smatrix'"~="" & "`wmatrix'"=="")
|
| )
|
|
|
|
|
| mata: s_gmm1s( "`ZZ'",
|
| "`XX'",
|
| "`XZ'",
|
| "`Zy'",
|
| "`ZZinv'",
|
| "`fv_lhs1'",
|
| "`resid'",
|
| "`fv_endo1' `fv_inexog1' `ones'",
|
| "`fv_exexog1' `fv_inexog1' `ones'",
|
| "`touse'",
|
| "`weight'",
|
| "`wvar'",
|
| `wf',
|
| `N',
|
| "`wmatrix'",
|
| "`S0'",
|
| `dofminus',
|
| `effic1s',
|
| `overid',
|
| `useqr')
|
| mat `b'=r(beta)
|
| mat `W'=r(W)
|
|
|
|
|
| if `effic1s' {
|
| mat `V'=r(V)
|
| mat `S'=r(S)
|
| scalar `j'=r(j)
|
| scalar `rss'=r(rss)
|
| scalar `sigmasq'=r(sigmasq)
|
| scalar `rankV'=r(rankV)
|
| scalar `rankS'=r(rankS)
|
| }
|
| else {
|
|
|
|
|
|
|
| tempname QXZ_W_QZX
|
| mat `QXZ_W_QZX'=r(QXZ_W_QZX)
|
|
|
|
|
| if "`smatrix'"~="" {
|
| mat `S'=`S0'
|
| }
|
| else {
|
|
|
|
|
|
|
|
|
|
|
| if `bw' != 0 {
|
| if `bw' == -1 {
|
| tempvar abwtouse
|
| gen byte `abwtouse' = (`resid' < .)
|
| abw `resid' `exexog1' `inexog1' `abwtouse', |
| tindex(`tindex') nobs(`N') tobs(`T') noconstant kernel(`kernel')
|
| local bw `r(abw)'
|
| local bwopt "bw(`bw')"
|
| local bwchoice "`r(bwchoice)'"
|
| }
|
| }
|
|
|
|
|
| mata: s_omega( "`ZZ'",
|
| "`resid'",
|
| "`fv_exexog1' `fv_inexog1' `ones'",
|
| "`touse'",
|
| "`weight'",
|
| "`wvar'",
|
| `wf',
|
| `N',
|
| "`robust'",
|
| "`clusterid1'",
|
| "`clusterid2'",
|
| "`clusterid3'",
|
| `bw',
|
| "`kernel'",
|
| "`sw'",
|
| "`psd'",
|
| "`ivar'",
|
| "`tvar'",
|
| "`tindex'",
|
| `tdelta',
|
| `center',
|
| `dofminus')
|
| mat `S'=r(S)
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if "`gmm2s'"=="" & "`robust'`cluster'`kernel'"~="" {
|
| mata: s_iegmm( "`ZZ'",
|
| "`XX'",
|
| "`XZ'",
|
| "`Zy'",
|
| "`QXZ_W_QZX'",
|
| "`fv_lhs1'",
|
| "`resid'",
|
| "`fv_endo1' `fv_inexog1' `ones'",
|
| "`fv_exexog1' `fv_inexog1' `ones'",
|
| "`touse'",
|
| "`weight'",
|
| "`wvar'",
|
| `wf',
|
| `N',
|
| "`W'",
|
| "`S'",
|
| "`b'",
|
| `dofminus',
|
| `overid',
|
| `useqr')
|
| }
|
|
|
|
|
| if "`gmm2s'"~="" {
|
| mata: s_egmm( "`ZZ'",
|
| "`XX'",
|
| "`XZ'",
|
| "`Zy'",
|
| "`ZZinv'",
|
| "`fv_lhs1'",
|
| "`resid'",
|
| "`fv_endo1' `fv_inexog1' `ones'",
|
| "`fv_exexog1' `fv_inexog1' `ones'",
|
| "`touse'",
|
| "`weight'",
|
| "`wvar'",
|
| `wf',
|
| `N',
|
| "`S'",
|
| `dofminus',
|
| `overid',
|
| `useqr')
|
| mat `b'=r(beta)
|
| mat `W'=r(W)
|
| }
|
|
|
| mat `V'=r(V)
|
| scalar `j'=r(j)
|
| scalar `rss'=r(rss)
|
| scalar `sigmasq'=r(sigmasq)
|
| scalar `rankV'=r(rankV)
|
| scalar `rankS'=r(rankS)
|
| }
|
|
|
| }
|
|
|
|
|
|
|
|
|
| if "`cue'`b0'" != "" {
|
|
|
|
|
|
|
| mata: s_gmmcue( "`ZZ'",
|
| "`XZ'",
|
| "`fv_lhs1'",
|
| "`resid'",
|
| "`fv_endo1' `fv_inexog1' `ones'",
|
| "`fv_exexog1' `fv_inexog1' `ones'",
|
| "`touse'",
|
| "`weight'",
|
| "`wvar'",
|
| `wf',
|
| `N',
|
| "`robust'",
|
| "`clusterid1'",
|
| "`clusterid2'",
|
| "`clusterid3'",
|
| `bw',
|
| "`kernel'",
|
| "`sw'",
|
| "`psd'",
|
| "`ivar'",
|
| "`tvar'",
|
| "`tindex'",
|
| `tdelta',
|
| "`b'",
|
| "`b0'",
|
| `center',
|
| `dofminus',
|
| `useqr')
|
|
|
| mat `b'=r(beta)
|
| mat `S'=r(S)
|
| mat `W'=r(W)
|
| mat `V'=r(V)
|
| scalar `j'=r(j)
|
| scalar `rss'=r(rss)
|
| scalar `sigmasq'=r(sigmasq)
|
| scalar `rankV'=r(rankV)
|
| scalar `rankS'=r(rankS)
|
|
|
| }
|
|
|
|
|
|
|
|
|
|
|
| mat colnames `b' = `cnb1'
|
| mat colnames `V' = `cnb1'
|
| mat rownames `V' = `cnb1'
|
| mat colnames `S' = `cnZ1'
|
| mat rownames `S' = `cnZ1'
|
|
|
| capture mat colnames `W' = `cnZ1'
|
| capture mat rownames `W' = `cnZ1'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| scalar `rmse'=sqrt(`sigmasq')
|
| if "`noconstant'"=="" {
|
| scalar `mss'=`yyc' - `rss'
|
| }
|
| else {
|
| scalar `mss'=`yy' - `rss'
|
| }
|
|
|
| local Fdf1 = `rankxx' - `cons'
|
| local df_m = `rankxx' - `cons' + (`sdofminus'-`partialcons')
|
|
|
|
|
| if "`cluster'"=="" {
|
|
|
|
|
| local df_r = int(`N') - `rankxx' - `dofminus' - `sdofminus'
|
| }
|
| else {
|
|
|
| local df_r = `N_clust' - 1
|
| }
|
|
|
|
|
|
|
| local jdf = `rankzz' - `rankxx'
|
| if `jdf' == 0 & "`b0'"=="" {
|
| scalar `j' = 0
|
| }
|
| else {
|
| scalar `jp' = chiprob(`jdf',`j')
|
| }
|
| if "`liml'"~="" {
|
| scalar `arubinp' = chiprob(`jdf',`arubin')
|
| scalar `arubin_linp' = chiprob(`jdf',`arubin_lin')
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if "`small'" != "" {
|
| if "`cluster'"=="" {
|
| matrix `V'=`V'*(`N'-`dofminus')/(`N'-`rankxx'-`dofminus'-`sdofminus')
|
| }
|
| else {
|
| matrix `V'=`V'*(`N'-1)/(`N'-`rankxx'-`sdofminus')
|
|
|
| }
|
| scalar `sigmasq'=`rss'/(`N'-`rankxx'-`dofminus'-`sdofminus')
|
| scalar `rmse'=sqrt(`sigmasq')
|
| }
|
|
|
| scalar `r2u'=1-`rss'/`yy'
|
| scalar `r2c'=1-`rss'/`yyc'
|
| if "`noconstant'"=="" {
|
| scalar `r2'=`r2c'
|
| scalar `r2_a'=1-(1-`r2')*(`N'-1)/(`N'-`rankxx'-`dofminus'-`sdofminus')
|
| }
|
| else {
|
| scalar `r2'=`r2u'
|
| scalar `r2_a'=1-(1-`r2')*`N'/(`N'-`rankxx'-`dofminus'-`sdofminus')
|
| }
|
|
|
|
|
| local N=int(`N')
|
|
|
|
|
|
|
|
|
|
|
| tempname FB FV
|
| mat `FB'=`b'
|
| mat `FV'=`V'
|
| capture ereturn post `FB' `FV'
|
|
|
| local rc = _rc
|
| if `rc' != 506 {
|
|
|
| ivreg2_fvstrip `rhs1', dropomit
|
| capture test `r(varlist)'
|
| if "`small'" == "" {
|
| if "`cluster'"=="" {
|
| capture scalar `F' = r(chi2)/`Fdf1' * `df_r'/(`N'-`dofminus')
|
| }
|
| else {
|
|
|
| capture scalar `F' = r(chi2)/`Fdf1' *
|
| (`N_clust'-1)/`N_clust' *
|
| (`N'-`rankxx'-`sdofminus')/(`N'-1)
|
| }
|
| }
|
| else {
|
| capture scalar `F' = r(chi2)/`Fdf1'
|
| }
|
| capture scalar `Fp'=Ftail(`Fdf1',`df_r',`F')
|
| capture scalar `Fdf2'=`df_r'
|
| }
|
|
|
|
|
| if `j' == . | `rc'==506 {
|
| scalar `F' = .
|
| scalar `Fp' = .
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if "`sfirst'`savesfirst'`rf'`saverf'`first'`ffirst'`savefirst'" != "" & (`endo1_ct' > 0) {
|
|
|
|
|
| capture tsset
|
|
|
| local sdofmopt = "sdofminus(`sdofminus')"
|
|
|
|
|
|
|
|
|
| tempname fsres1
|
| qui gen double `fsres1'=.
|
| local fsresall `fsres1'
|
| foreach x of local fv_endo1 {
|
| tempname fsres
|
| qui gen double `fsres'=.
|
| local fsresall "`fsresall' `fsres'"
|
| }
|
|
|
|
|
| if "`cluster'"!="" {
|
| sort `clusterid3' `cluster1'
|
| }
|
| mata: s_ffirst( "`ZZ'",
|
| "`XX'",
|
| "`XZ'",
|
| "`ZY'",
|
| "`ZZinv'",
|
| "`XXinv'",
|
| "`XPZXinv'",
|
| "`Z2Z2'",
|
| "`Z1Z2'",
|
| "`Z2y'",
|
| "`fsres1'",
|
| "`fsresall'",
|
| "`fv_lhs1'",
|
| "`fv_endo1'",
|
| "`fv_inexog1' `ones'",
|
| "`fv_exexog1'",
|
| "`touse'",
|
| "`weight'",
|
| "`wvar'",
|
| `wf',
|
| `N',
|
| `N_clust',
|
| "`robust'",
|
| "`clusterid1'",
|
| "`clusterid2'",
|
| "`clusterid3'",
|
| `bw',
|
| "`kernel'",
|
| "`sw'",
|
| "`psd'",
|
| "`ivar'",
|
| "`tvar'",
|
| "`tindex'",
|
| `tdelta',
|
| `center',
|
| `dofminus',
|
| `sdofminus')
|
|
|
| tempname firstmat firstb firstv firsts
|
| mat `firstmat' = r(firstmat)
|
| mat rowname `firstmat' = rmse sheapr2 pr2 F df df_r pvalue
|
| SWF SWFdf1 SWFdf2 SWFp SWchi2 SWchi2p SWr2
|
| APF APFdf1 APFdf2 APFp APchi2 APchi2p APr2
|
| mat colname `firstmat' = `endo1'
|
| mat `firstb' = r(b)
|
| mat `firstv' = r(V)
|
| mat `firsts' = r(S)
|
| local archi2 =r(archi2)
|
| local archi2p =r(archi2p)
|
| local arf =r(arf)
|
| local arfp =r(arfp)
|
| local ardf =r(ardf)
|
| local ardf_r =r(ardf_r)
|
| local sstat =r(sstat)
|
| local sstatdf =r(sstatdf)
|
| local sstatp =r(sstatp)
|
| local rmse_rf =r(rmse_rf)
|
|
|
|
|
| capture tsset
|
|
|
| if "`sfirst'`savesfirst'" ~= "" {
|
| PostFirstRF if `touse',
|
| bmat(`firstb')
|
| vmat(`firstv')
|
| smat(`firsts')
|
| firstmat(`firstmat')
|
| lhs1(`lhs1')
|
| endo1(`endo1')
|
| znames0(`cnZ0')
|
| znames1(`cnZ1')
|
| bvclean(`bvclean')
|
| fvops(`fvops')
|
| partial_ct(`partial_ct')
|
| `robust'
|
| cluster(`cluster')
|
| cluster1(`cluster1')
|
| cluster2(`cluster2')
|
| nc(`N_clust')
|
| nc1(`N_clust1')
|
| nc2(`N_clust2')
|
| kernel(`kernel')
|
| bw(`bw')
|
| ivar(`ivar')
|
| tvar(`tvar')
|
| obs(`N')
|
| iv1_ct(`iv1_ct')
|
| cons(`cons')
|
| partialcons(`partialcons')
|
| dofminus(`dofminus')
|
| sdofminus(`sdofminus')
|
| local sfirsteq "`savesfprefix'sfirst_`lhs1'"
|
| local sfirsteq : subinstr local sfirsteq "." "_"
|
| capture est store `sfirsteq', title("System of first-stage/reduced form regressions")
|
| if _rc > 0 {
|
| di
|
| di in ye "Unable to store system of first-stage reduced form regressions."
|
| di
|
| }
|
| }
|
|
|
|
|
| if "`rf'`saverf'" ~= "" {
|
| PostFirstRF if `touse',
|
| rf
|
| rmse_rf(`rmse_rf')
|
| bmat(`firstb')
|
| vmat(`firstv')
|
| smat(`firsts')
|
| firstmat(`firstmat')
|
| lhs1(`lhs1')
|
| endo1(`endo1')
|
| znames0(`cnZ0')
|
| znames1(`cnZ1')
|
| bvclean(`bvclean')
|
| fvops(`fvops')
|
| partial_ct(`partial_ct')
|
| `robust'
|
| cluster(`cluster')
|
| cluster1(`cluster1')
|
| cluster2(`cluster2')
|
| nc(`N_clust')
|
| nc1(`N_clust1')
|
| nc2(`N_clust2')
|
| kernel(`kernel')
|
| bw(`bw')
|
| ivar(`ivar')
|
| tvar(`tvar')
|
| obs(`N')
|
| iv1_ct(`iv1_ct')
|
| cons(`cons')
|
| partialcons(`partialcons')
|
| dofminus(`dofminus')
|
| sdofminus(`sdofminus')
|
| local rfeq "`saverfprefix'`lhs1'"
|
| local rfeq : subinstr local rfeq "." "_"
|
| capture est store `rfeq', title("Reduced-form regression: `lhs'")
|
| if _rc > 0 {
|
| di
|
| di in ye "Unable to store reduced form regression of `lhs1'."
|
| di
|
| }
|
| }
|
|
|
|
|
| if "`first'`savefirst'" ~= "" {
|
| foreach vn in `endo1' {
|
|
|
| PostFirstRF if `touse',
|
| first(`vn')
|
| bmat(`firstb')
|
| vmat(`firstv')
|
| smat(`firsts')
|
| firstmat(`firstmat')
|
| lhs1(`lhs1')
|
| endo1(`endo1')
|
| znames0(`cnZ0')
|
| znames1(`cnZ1')
|
| bvclean(`bvclean')
|
| fvops(`fvops')
|
| partial_ct(`partial_ct')
|
| `robust'
|
| cluster(`cluster')
|
| cluster1(`cluster1')
|
| cluster2(`cluster2')
|
| nc(`N_clust')
|
| nc1(`N_clust1')
|
| nc2(`N_clust2')
|
| kernel(`kernel')
|
| bw(`bw')
|
| ivar(`ivar')
|
| tvar(`tvar')
|
| obs(`N')
|
| iv1_ct(`iv1_ct')
|
| cons(`cons')
|
| partialcons(`partialcons')
|
| dofminus(`dofminus')
|
| sdofminus(`sdofminus')
|
| local eqname "`savefprefix'`vn'"
|
| local eqname : subinstr local eqname "." "_"
|
| capture est store `eqname', title("First-stage regression: `vn'")
|
| if _rc == 0 {
|
| local firsteqs "`firsteqs' `eqname'"
|
| }
|
| else {
|
| di
|
| di in ye "Unable to store first-stage regression of `vn'."
|
| di
|
| }
|
| }
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| capture tsset
|
|
|
|
|
|
|
|
|
|
|
| if "`orthog'"!="" {
|
| tempname cj cstat cstatp
|
|
|
| scalar `cstat' = 0
|
|
|
|
|
| local cexexog1 : list fv_exexog1 - fv_orthog1
|
| local cinexog1 : list fv_inexog1 - fv_orthog1
|
| local cendo1 : list fv_inexog1 - cinexog1
|
| local cendo1 `fv_endo1' `cendo1'
|
| local clist_ct : word count `orthog1'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| capture _estimates hold `ivest', restore
|
| if _rc==1000 {
|
| di as err "ivreg2 internal error - no room to save temporary estimation results"
|
| di as err "Please drop one or more estimation results using -estimates drop-"
|
| exit 1000
|
| }
|
|
|
|
|
|
|
|
|
| tempname fv_S
|
| mat `fv_S'=`S'
|
| if `cons' {
|
| mat colnames `fv_S' = `fv_exexog1' `fv_inexog1' _cons
|
| mat rownames `fv_S' = `fv_exexog1' `fv_inexog1' _cons
|
| }
|
| else {
|
| mat colnames `fv_S' = `fv_exexog1' `fv_inexog1'
|
| mat rownames `fv_S' = `fv_exexog1' `fv_inexog1'
|
| }
|
| capture `ivreg2cmd' `fv_lhs1'
|
| `cinexog1'
|
| (`cendo1'=`cexexog1')
|
| if `touse'
|
| `wtexp',
|
| `noconstant'
|
| `options'
|
| `small'
|
| `robust'
|
| `gmm2s'
|
| `bwopt'
|
| `kernopt'
|
| `dofmopt'
|
| `sw'
|
| `psd'
|
| smatrix("`fv_S'")
|
| noid
|
| nocollin
|
| local rc = _rc
|
| if `rc' == 481 {
|
| scalar `cstat' = 0
|
| local cstatdf = 0
|
| }
|
| else {
|
| scalar `cj'=e(j)
|
| local cjdf=e(jdf)
|
| scalar `cstat' = `j' - `cj'
|
| local cstatdf = `jdf' - `cjdf'
|
| }
|
| _estimates unhold `ivest'
|
| scalar `cstatp'= chiprob(`cstatdf',`cstat')
|
|
|
|
|
| if `cstatdf' != `clist_ct' {
|
| scalar `cstat' = 0
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| if "`endogtest'"!="" {
|
| tempname estat estatp
|
|
|
| scalar `estat' = 0
|
|
|
| local eendo1 : list fv_endo1 - fv_endogtest1
|
| local einexog1 `fv_inexog1' `fv_endogtest1'
|
| local elist_ct : word count `endogtest1'
|
|
|
|
|
|
|
| capture {
|
| capture _estimates hold `ivest', restore
|
| if _rc==1000 {
|
| di as err "ivreg2 internal error - no room to save temporary estimation results"
|
| di as err "Please drop one or more estimation results using -estimates drop-"
|
| exit 1000
|
| }
|
| capture `ivreg2cmd' `fv_lhs1'
|
| `einexog1'
|
| (`eendo1'=`fv_exexog1')
|
| if `touse'
|
| `wtexp',
|
| `noconstant'
|
| `robust'
|
| `clopt'
|
| `gmm2s'
|
| `liml'
|
| `bwopt'
|
| `kernopt'
|
| `small'
|
| `dofmopt'
|
| `sw'
|
| `psd'
|
| `options'
|
| orthog(`fv_endogtest1')
|
| noid
|
| nocollin
|
| local rc = _rc
|
| if `rc' == 481 {
|
| scalar `estat' = 0
|
| local estatdf = 0
|
| }
|
| else {
|
| scalar `estat'=e(cstat)
|
| local estatdf=e(cstatdf)
|
| scalar `estatp'=e(cstatp)
|
| }
|
| _estimates unhold `ivest'
|
|
|
|
|
| if `estatdf' != `elist_ct' {
|
| scalar `estat' = 0
|
| }
|
| }
|
|
|
| }
|
|
|
|
|
|
|
|
|
| if `endo1_ct' > 0 & "`noid'"=="" {
|
|
|
|
|
| tempname idrkstat widrkstat iddf idp
|
| tempname ccf cdf rkf cceval cdeval cd
|
| tempname idstat widstat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| cap `ranktestcmd'
|
| (`fv_endo1')
|
| (`fv_exexog1')
|
| `wtexp'
|
| if `touse',
|
| partial(`fv_inexog1')
|
| full
|
| `noconstant'
|
| `robust'
|
| `clopt'
|
| `bwopt'
|
| `kernopt'
|
|
|
| local rkcmd `r(ranktestcmd)'
|
|
|
|
|
|
|
| local rkerror = _rc>0 | r(chi2)==.
|
| if ~`rkerror' {
|
| local rkerror = el(r(ccorr),1,1)==1
|
| }
|
| if `rkerror' {
|
| di as err "warning: -ranktest- error in calculating underidentification test statistics;"
|
| di as err " may be caused by collinearities"
|
| scalar `idstat' = .
|
| local iddf = .
|
| scalar `idp' = .
|
| scalar `cd' = .
|
| scalar `cdf' = .
|
| }
|
| else {
|
| if "`cluster'"=="" {
|
| scalar `idstat'=r(chi2)/r(N)*(`N'-`dofminus')
|
| }
|
| else {
|
|
|
| scalar `idstat'=r(chi2)
|
| }
|
| mat `cceval'=r(ccorr)
|
| mat `cdeval' = J(1,`endo1_ct',.)
|
| forval i=1/`endo1_ct' {
|
| mat `cceval'[1,`i'] = (`cceval'[1,`i'])^2
|
| mat `cdeval'[1,`i'] = `cceval'[1,`i'] / (1 - `cceval'[1,`i'])
|
| }
|
| local iddf = `iv1_ct' - (`rhs1_ct'-1)
|
| scalar `idp' = chiprob(`iddf',`idstat')
|
|
|
|
|
| scalar `cd'=`cdeval'[1,`endo1_ct']
|
| scalar `cdf'=`cd'*(`N'-`sdofminus'-`iv1_ct'-`dofminus')/`exex1_ct'
|
| }
|
|
|
|
|
|
|
|
|
| if "`robust'`cluster'`kernel'"=="" {
|
| scalar `widstat'=`cdf'
|
| }
|
| else {
|
|
|
| cap `ranktestcmd'
|
| (`fv_endo1')
|
| (`fv_exexog1')
|
| `wtexp'
|
| if `touse',
|
| partial(`fv_inexog1')
|
| full
|
| wald
|
| `noconstant'
|
| `robust'
|
| `clopt'
|
| `bwopt'
|
| `kernopt'
|
|
|
|
|
| local rkerror = _rc>0 | r(chi2)==.
|
| if ~`rkerror' {
|
| local rkerror = el(r(ccorr),1,1)==1
|
| }
|
| if `rkerror' {
|
| di as err "warning: -ranktest- error in calculating weak identification test statistics;"
|
| di as err " may be caused by collinearities"
|
| scalar `rkf' = .
|
| scalar `widstat' = .
|
| }
|
| else {
|
|
|
| if "`cluster'"=="" {
|
| scalar `rkf'=r(chi2)/r(N)*(`N'-`iv1_ct'-`sdofminus'-`dofminus')/`exex1_ct'
|
| }
|
| else {
|
| scalar `rkf' = r(chi2)/(`N'-1) *
|
| (`N'-`iv1_ct'-`sdofminus') *
|
| (`N_clust'-1)/`N_clust' /
|
| `exex1_ct'
|
| }
|
| scalar `widstat'=`rkf'
|
| }
|
| }
|
| }
|
|
|
|
|
| if `endo1_ct' > 0 & "`redundant'" ~= "" & "`noid'"=="" {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| local rexexog1 : list fv_exexog1 - fv_redundant1
|
| local redlist_ct : word count `redundant1'
|
|
|
| tempname rkmatrix
|
| qui `ranktestcmd'
|
| (`fv_endo1')
|
| (`fv_redundant1')
|
| `wtexp'
|
| if `touse',
|
| partial(`fv_inexog1' `rexexog1')
|
| null
|
| `noconstant'
|
| `robust'
|
| `clopt'
|
| `bwopt'
|
| `kernopt'
|
| mat `rkmatrix'=r(rkmatrix)
|
| tempname redstat redp
|
|
|
| if "`cluster'"=="" {
|
| scalar `redstat' = `rkmatrix'[1,1]/r(N)*(`N'-`dofminus')
|
| }
|
| else {
|
|
|
| scalar `redstat' = `rkmatrix'[1,1]
|
| }
|
| local reddf = `endo1_ct'*`redlist_ct'
|
| scalar `redp' = chiprob(`reddf',`redstat')
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if `N' <= `iv1_ct' {
|
| di in r "Error: number of observations must be greater than number of instruments"
|
| di in r " including constant."
|
| error 2001
|
| }
|
|
|
|
|
| if ("`gmm2s'`robust'`cluster'`kernel'" != "") & (`rankS' < `iv1_ct') {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| scalar `j' = .
|
| if "`orthog'"!="" {
|
| scalar `cstat' = .
|
| }
|
| if "`endogtest'"!="" {
|
| scalar `estat' = .
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if ~`bvclean' & (`rhs0_ct' > `rhs1_ct') {
|
| AddOmitted, bmat(`b') vmat(`V') cnb0(`cnb0') cnb1(`cnb1')
|
| mat `b' = r(b)
|
| mat `V' = r(V)
|
|
|
| if `fvops' & ~`partial_ct' {
|
| local bfv "buildfvinfo"
|
| }
|
| }
|
|
|
|
|
|
|
|
|
| if `partial_ct' {
|
| restore
|
| }
|
|
|
| if "`small'"!="" {
|
| local NminusK = `N'-`rankxx'-`sdofminus'
|
| capture ereturn post `b' `V', dep(`depname') obs(`N') esample(`touse') dof(`NminusK') `bfv'
|
| }
|
| else {
|
| capture ereturn post `b' `V', dep(`depname') obs(`N') esample(`touse') `bfv'
|
| }
|
|
|
| local rc = _rc
|
| if `rc' == 504 {
|
| di in red "Error: estimated variance-covariance matrix has missing values"
|
| exit 504
|
| }
|
| if `rc' == 506 {
|
| di in red "Error: estimated variance-covariance matrix not positive-definite"
|
| exit 506
|
| }
|
| if `rc' > 0 {
|
| di in red "Error: estimation failed - could not post estimation results"
|
| exit `rc'
|
| }
|
|
|
| local mok =1
|
| local mok = `mok' & ~`partial_ct'
|
| local mok = `mok' & ~(`fvops' & `bvclean')
|
| if `mok' & `endo1_ct' {
|
| ereturn local marginsnotok "Residuals SCores"
|
| ereturn local marginsok "XB default"
|
| }
|
| else if `mok' & ~`endo1_ct' {
|
| ereturn local marginsok "XB default"
|
| }
|
| else {
|
| ereturn local marginsnotok "Residuals SCores XB default"
|
| }
|
|
|
|
|
|
|
|
|
| ereturn local ecollin `ecollin'
|
| ereturn local collin `collin'
|
| ereturn local dups `dups'
|
| ereturn local partial1 `partial1'
|
| ereturn local partial `partial'
|
| ereturn local inexog1 `inexog1'
|
| ereturn local inexog0 `inexog0'
|
| ereturn local inexog `inexog'
|
| ereturn local exexog1 `exexog1'
|
| ereturn local exexog0 `exexog0'
|
| ereturn local exexog `exexog'
|
| ereturn local insts1 `exexog1' `inexog1'
|
| ereturn local insts0 `exexog0' `inexog0'
|
| ereturn local insts `exexog' `inexog'
|
| ereturn local instd1 `endo1'
|
| ereturn local instd0 `endo0'
|
| ereturn local instd `endo'
|
| ereturn local depvar1 `lhs1'
|
| ereturn local depvar0 `lhs0'
|
| ereturn local depvar `lhs'
|
|
|
| ereturn scalar inexog_ct =`inexog1_ct'
|
| ereturn scalar exexog_ct =`exex1_ct'
|
| ereturn scalar endog_ct =`endo1_ct'
|
| ereturn scalar partial_ct =`partial_ct'
|
|
|
| if "`smatrix'" == "" {
|
| ereturn matrix S `S'
|
| }
|
| else {
|
| ereturn matrix S `S0'
|
| }
|
|
|
|
|
| if "`wmatrix'"=="" & "`liml'`kclassopt'"=="" {
|
| ereturn matrix W `W'
|
| }
|
| else if "`liml'`kclassopt'"=="" {
|
| ereturn matrix W `wmatrix'
|
| }
|
|
|
| if "`kernel'"!="" {
|
| ereturn local kernel "`kernel'"
|
| ereturn scalar bw=`bw'
|
| ereturn local tvar "`tvar'"
|
| if "`ivar'" ~= "" {
|
| ereturn local ivar "`ivar'"
|
| }
|
| if "`bwchoice'" ~= "" {
|
| ereturn local bwchoice "`bwchoice'"
|
| }
|
| }
|
|
|
| if "`small'"!="" {
|
| ereturn scalar df_r=`df_r'
|
| ereturn local small "small"
|
| }
|
| if "`nopartialsmall'"=="" {
|
| ereturn local partialsmall "small"
|
| }
|
|
|
|
|
| if "`robust'" != "" {
|
| local vce "robust"
|
| }
|
| if "`cluster1'" != "" {
|
| if "`cluster2'"=="" {
|
| local vce "`vce' cluster"
|
| }
|
| else {
|
| local vce "`vce' two-way cluster"
|
| }
|
| }
|
| if "`kernel'" != "" {
|
| if "`robust'" != "" {
|
| local vce "`vce' hac"
|
| }
|
| else {
|
| local vce "`vce' ac"
|
| }
|
| local vce "`vce' `kernel' bw=`bw'"
|
| }
|
| if "`sw'" != "" {
|
| local vce "`vce' sw"
|
| }
|
| if "`psd'" != "" {
|
| local vce "`vce' `psd'"
|
| }
|
| local vce : list clean vce
|
| local vce = lower("`vce'")
|
| ereturn local vce `vce'
|
|
|
| if "`cluster'"!="" {
|
| ereturn scalar N_clust=`N_clust'
|
| ereturn local clustvar `cluster'
|
| }
|
| if "`cluster2'"!="" {
|
| ereturn scalar N_clust1=`N_clust1'
|
| ereturn scalar N_clust2=`N_clust2'
|
| ereturn local clustvar1 `cluster1'
|
| ereturn local clustvar2 `cluster2'
|
| }
|
|
|
| if "`robust'`cluster'" != "" {
|
| ereturn local vcetype "Robust"
|
| }
|
|
|
| ereturn scalar df_m=`df_m'
|
| ereturn scalar sdofminus=`sdofminus'
|
| ereturn scalar dofminus=`dofminus'
|
| ereturn scalar center=`center'
|
| ereturn scalar r2=`r2'
|
| ereturn scalar rmse=`rmse'
|
| ereturn scalar rss=`rss'
|
| ereturn scalar mss=`mss'
|
| ereturn scalar r2_a=`r2_a'
|
| ereturn scalar F=`F'
|
| ereturn scalar Fp=`Fp'
|
| ereturn scalar Fdf1=`Fdf1'
|
| ereturn scalar Fdf2=`Fdf2'
|
| ereturn scalar yy=`yy'
|
| ereturn scalar yyc=`yyc'
|
| ereturn scalar r2u=`r2u'
|
| ereturn scalar r2c=`r2c'
|
| ereturn scalar condzz=`condzz'
|
| ereturn scalar condxx=`condxx'
|
| ereturn scalar rankzz=`rankzz'
|
| ereturn scalar rankxx=`rankxx'
|
| ereturn scalar rankS=`rankS'
|
| ereturn scalar rankV=`rankV'
|
| ereturn scalar ll = -0.5 * (`N'*ln(2*_pi) + `N'*ln(`rss'/`N') + `N')
|
|
|
|
|
| ereturn scalar j=`j'
|
| ereturn scalar jdf=`jdf'
|
| if `j' != 0 & `j' != . {
|
| ereturn scalar jp=`jp'
|
| }
|
| if ("`robust'`cluster'"=="") {
|
| ereturn scalar sargan=`j'
|
| ereturn scalar sargandf=`jdf'
|
| if `j' != 0 & `j' != . {
|
| ereturn scalar sarganp=`jp'
|
| }
|
| }
|
| if "`liml'"!="" {
|
| ereturn scalar arubin=`arubin'
|
| ereturn scalar arubin_lin=`arubin_lin'
|
| if `j' != 0 & `j' != . {
|
| ereturn scalar arubinp=`arubinp'
|
| ereturn scalar arubin_linp=`arubin_linp'
|
| }
|
| ereturn scalar arubindf=`jdf'
|
| }
|
|
|
| if "`orthog'"!="" {
|
| ereturn scalar cstat=`cstat'
|
| if `cstat'!=0 & `cstat' != . {
|
| ereturn scalar cstatp=`cstatp'
|
| ereturn scalar cstatdf=`cstatdf'
|
| ereturn local clist `orthog1'
|
| }
|
| }
|
|
|
| if "`endogtest'"!="" {
|
| ereturn scalar estat=`estat'
|
| if `estat'!=0 & `estat' != . {
|
| ereturn scalar estatp=`estatp'
|
| ereturn scalar estatdf=`estatdf'
|
| ereturn local elist `endogtest1'
|
| }
|
| }
|
|
|
| if `endo1_ct' > 0 & "`noid'"=="" {
|
| ereturn scalar idstat=`idstat'
|
| ereturn scalar iddf=`iddf'
|
| ereturn scalar idp=`idp'
|
| ereturn scalar cd=`cd'
|
| ereturn scalar widstat=`widstat'
|
| ereturn scalar cdf=`cdf'
|
| capture ereturn matrix ccev=`cceval'
|
| capture ereturn matrix cdev `cdeval'
|
| capture ereturn scalar rkf=`rkf'
|
| }
|
|
|
| if "`redundant'"!="" & "`noid'"=="" {
|
| ereturn scalar redstat=`redstat'
|
| ereturn scalar redp=`redp'
|
| ereturn scalar reddf=`reddf'
|
| ereturn local redlist `redundant1'
|
| }
|
|
|
| if "`first'`ffirst'`savefirst'`sfirst'`savesfirst'" != "" & `endo1_ct'>0 {
|
|
|
| capture ereturn matrix first `firstmat'
|
| ereturn scalar arf=`arf'
|
| ereturn scalar arfp=`arfp'
|
| ereturn scalar archi2=`archi2'
|
| ereturn scalar archi2p=`archi2p'
|
| ereturn scalar ardf=`ardf'
|
| ereturn scalar ardf_r=`ardf_r'
|
| ereturn scalar sstat=`sstat'
|
| ereturn scalar sstatp=`sstatp'
|
| ereturn scalar sstatdf=`sstatdf'
|
| }
|
|
|
| ereturn local firsteqs `firsteqs'
|
| ereturn local rfeq `rfeq'
|
| ereturn local sfirsteq `sfirsteq'
|
|
|
| if "`liml'"!="" {
|
| ereturn local model "liml"
|
| ereturn scalar kclass=`kclass'
|
| ereturn scalar lambda=`lambda'
|
| if `fuller' > 0 & `fuller' < . {
|
| ereturn scalar fuller=`fuller'
|
| }
|
| }
|
| else if "`kclassopt'" != "" {
|
| ereturn local model "kclass"
|
| ereturn scalar kclass=`kclass'
|
| }
|
| else if "`gmm2s'`cue'`b0'`wmatrix'"=="" {
|
| if "`endo1'" == "" {
|
| ereturn local model "ols"
|
| }
|
| else {
|
| ereturn local model "iv"
|
| }
|
| }
|
| else if "`cue'`b0'"~="" {
|
| ereturn local model "cue"
|
| }
|
| else if "`gmm2s'"~="" {
|
| ereturn local model "gmm2s"
|
| }
|
| else if "`wmatrix'"~="" {
|
| ereturn local model "gmmw"
|
| }
|
| else {
|
|
|
| ereturn local model "unknown"
|
| }
|
|
|
| if "`weight'" != "" {
|
| ereturn local wexp "=`exp'"
|
| ereturn local wtype `weight'
|
| }
|
| ereturn local cmd `ivreg2cmd'
|
| ereturn local ranktestcmd `rkcmd'
|
| ereturn local version `lversion'
|
| ereturn scalar nocollin =("`nocollin'"~="")
|
| ereturn scalar partialcons =`partialcons'
|
| ereturn scalar cons =`cons'
|
|
|
| ereturn local predict "`ivreg2cmd'_p"
|
|
|
| if "`e(model)'"=="gmm2s" & "`wmatrix'"=="" {
|
| local title2 "2-Step GMM estimation"
|
| }
|
| else if "`e(model)'"=="gmm2s" & "`wmatrix'"~="" {
|
| local title2 "2-Step GMM estimation with user-supplied first-step weighting matrix"
|
| }
|
| else if "`e(model)'"=="gmmw" {
|
| local title2 "GMM estimation with user-supplied weighting matrix"
|
| }
|
| else if "`e(model)'"=="cue" & "`b0'"=="" {
|
| local title2 "CUE estimation"
|
| }
|
| else if "`e(model)'"=="cue" & "`b0'"~="" {
|
| local title2 "CUE evaluated at user-supplied parameter vector"
|
| }
|
| else if "`e(model)'"=="ols" {
|
| local title2 "OLS estimation"
|
| }
|
| else if "`e(model)'"=="iv" {
|
| local title2 "IV (2SLS) estimation"
|
| }
|
| else if "`e(model)'"=="liml" {
|
| local title2 "LIML estimation"
|
| }
|
| else if "`e(model)'"=="kclass" {
|
| local title2 "k-class estimation"
|
| }
|
| else {
|
|
|
| local title2 "unknown estimation"
|
| }
|
| if "`e(vcetype)'" == "Robust" {
|
| local hacsubtitle1 "heteroskedasticity"
|
| }
|
| if "`e(kernel)'"!="" & "`e(clustvar)'"=="" {
|
| local hacsubtitle3 "autocorrelation"
|
| }
|
| if "`kiefer'"!="" {
|
| local hacsubtitle3 "within-cluster autocorrelation (Kiefer)"
|
| }
|
| if "`e(clustvar)'"!="" {
|
| if "`e(clustvar2)'"=="" {
|
| local hacsubtitle3 "clustering on `e(clustvar)'"
|
| }
|
| else {
|
| local hacsubtitle3 "clustering on `e(clustvar1)' and `e(clustvar2)'"
|
| }
|
| if "`e(kernel)'" != "" {
|
| local hacsubtitle4 "and kernel-robust to common correlated disturbances (Driscoll-Kraay)"
|
| }
|
| }
|
| if "`hacsubtitle1'"~="" & "`hacsubtitle3'" ~= "" {
|
| local hacsubtitle2 " and "
|
| }
|
| if "`title'"=="" {
|
| ereturn local title "`title1'`title2'"
|
| }
|
| else {
|
| ereturn local title "`title'"
|
| }
|
| if "`subtitle'"~="" {
|
| ereturn local subtitle "`subtitle'"
|
| }
|
| local hacsubtitle "`hacsubtitle1'`hacsubtitle2'`hacsubtitle3'"
|
| if "`b0'"~="" {
|
| ereturn local hacsubtitleB "Estimates based on supplied parameter vector"
|
| }
|
| else if "`hacsubtitle'"~="" & "`gmm2s'`cue'"~="" {
|
| ereturn local hacsubtitleB "Estimates efficient for arbitrary `hacsubtitle'"
|
| }
|
| else if "`wmatrix'"~="" {
|
| ereturn local hacsubtitleB "Efficiency of estimates dependent on weighting matrix"
|
| }
|
| else {
|
| ereturn local hacsubtitleB "Estimates efficient for homoskedasticity only"
|
| }
|
| if "`hacsubtitle'"~="" {
|
| ereturn local hacsubtitleV "Statistics robust to `hacsubtitle'"
|
| }
|
| else {
|
| ereturn local hacsubtitleV "Statistics consistent for homoskedasticity only"
|
| }
|
| if "`hacsubtitle4'"~="" {
|
| ereturn local hacsubtitleV2 "`hacsubtitle4'"
|
| }
|
| if "`sw'"~="" {
|
| ereturn local hacsubtitleV "Stock-Watson heteroskedastic-robust statistics (BETA VERSION)"
|
| }
|
| }
|
|
|
|
|
|
|
|
|
| if "`nooutput'" == "" {
|
|
|
|
|
| if "`savesfirst'`saverf'`savefirst'" != "" {
|
| DispStored `"`savesfirst'"' `"`saverf'"' `"`savefirst'"'
|
| }
|
| if "`rf'" != "" {
|
| local eqname "`e(rfeq)'"
|
| tempname ivest
|
| _estimates hold `ivest', copy
|
| capture estimates restore `eqname'
|
| if _rc != 0 {
|
| di
|
| di in ye "Unable to display stored reduced form estimation."
|
| di
|
| }
|
| else {
|
| DispSFirst "rf" `"`plus'"' `"`level'"' `"`nofooter'"' `"`ivreg2name'"' "`dispopt'"
|
| }
|
| _estimates unhold `ivest'
|
| }
|
| if "`first'" != "" {
|
| DispFirst `"`ivreg2name'"'
|
| }
|
| if "`sfirst'"!="" {
|
| local eqname "`e(sfirsteq)'"
|
| tempname ivest
|
| _estimates hold `ivest', copy
|
| capture estimates restore `eqname'
|
| if _rc != 0 {
|
| di
|
| di in ye "Unable to display stored first-stage/reduced form estimations."
|
| di
|
| }
|
| else {
|
| DispSFirst "sfirst" `"`plus'"' `"`level'"' `"`nofooter'"' `"`ivreg2name'"' "`dispopt'"
|
| }
|
| _estimates unhold `ivest'
|
| }
|
| if "`first'`ffirst'`sfirst'" != "" {
|
| DispFFirst `"`ivreg2name'"'
|
| }
|
|
|
|
|
| if "`e(model)'"=="first" | "`e(model)'"=="rf" | "`e(model)'"=="sfirst" {
|
| DispSFirst "`e(model)'" `"`plus'"' `"`level'"' `"`nofooter'"' `"`ivreg2name'"' "`dispopt'"
|
| }
|
| else {
|
| DispMain `"`noheader'"' `"`plus'"' `"`level'"' `"`nofooter'"' `"`ivreg2name'"' "`dispopt'"
|
| }
|
| }
|
|
|
|
|
| if "`savefirst'" == "" {
|
| local firsteqs "`e(firsteqs)'"
|
| foreach eqname of local firsteqs {
|
| capture estimates drop `eqname'
|
| }
|
| ereturn local firsteqs
|
| }
|
|
|
| if "`saverf'" == "" {
|
| local eqname "`e(rfeq)'"
|
| capture estimates drop `eqname'
|
| ereturn local rfeq
|
| }
|
|
|
| if "`savesfirst'" == "" {
|
| local eqname "`e(sfirsteq)'"
|
| capture estimates drop `eqname'
|
| ereturn local sfirsteq
|
| }
|
|
|
| end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| program define DispSFirst, eclass
|
| args model plus level nofooter helpfile dispopt
|
| version 11.2
|
|
|
| di
|
| if "`model'"=="first" {
|
| di in gr "First-stage regression of `e(depvar)':"
|
| }
|
| else if "`model'"=="rf" {
|
| local strlen = length("`e(depvar)'")+25
|
| di in gr "Reduced-form regression: `e(depvar)'"
|
| di in smcl in gr "{hline `strlen'}"
|
| }
|
| else if "`model'"=="sfirst" {
|
| di in gr "System of first-stage/reduced-form regressions:"
|
| di in smcl in gr "{hline 47}"
|
| }
|
|
|
|
|
|
|
| if "`e(hacsubtitleV)'" ~= "" {
|
| di in gr _n "`e(hacsubtitleV)'"
|
| }
|
| if "`e(hacsubtitleV2)'" ~= "" {
|
| di in gr "`e(hacsubtitleV2)'"
|
| }
|
| di in gr "Number of obs = " _col(31) in ye %8.0f e(N)
|
| if "`e(kernel)'"!="" {
|
| di in gr " kernel=`e(kernel)'; bandwidth=" `e(bw)'
|
| if "`e(bwchoice)'"!="" {
|
| di in gr " `e(bwchoice)'"
|
| }
|
| di in gr " time variable (t): " in ye e(tvar)
|
| if "`e(ivar)'" != "" {
|
| di in gr " group variable (i): " in ye e(ivar)
|
| }
|
| }
|
| if "`e(clustvar)'"!="" {
|
| if "`e(clustvar2)'"=="" {
|
| local N_clust `e(N_clust)'
|
| local clustvar `e(clustvar)'
|
| }
|
| else {
|
| local N_clust `e(N_clust1)'
|
| local clustvar `e(clustvar1)'
|
| }
|
| di in gr "Number of clusters (`clustvar') = " _col(33) in ye %6.0f `N_clust'
|
| }
|
| if "`e(clustvar2)'"!="" {
|
| di in gr "Number of clusters (" "`e(clustvar2)'" ") = " _col(33) in ye %6.0f e(N_clust2)
|
| }
|
|
|
|
|
|
|
| local cluster `e(clustvar)'
|
| ereturn local clustvar
|
|
|
|
|
| ereturn display, `plus' level(`level') `dispopt'
|
| ereturn local clustvar `cluster'
|
|
|
| end
|
|
|
|
|
|
|
| program define DispMain, eclass
|
| args noheader plus level nofooter helpfile dispopt
|
| version 11.2
|
|
|
|
|
|
|
|
|
|
|
| capture local rprob ("`e(j)'"==".")
|
|
|
| if "`noheader'"=="" {
|
| if "`e(title)'" ~= "" {
|
| di in gr _n "`e(title)'"
|
| local tlen=length("`e(title)'")
|
| di in gr "{hline `tlen'}"
|
| }
|
| if "`e(subtitle)'" ~= "" {
|
| di in gr "`e(subtitle)'"
|
| }
|
| if "`e(model)'"=="liml" | "`e(model)'"=="kclass" {
|
| di in gr "k =" %7.5f `e(kclass)'
|
| }
|
| if "`e(model)'"=="liml" {
|
| di in gr "lambda =" %7.5f `e(lambda)'
|
| }
|
| if e(fuller) > 0 & e(fuller) < . {
|
| di in gr "Fuller parameter=" %-5.0f `e(fuller)'
|
| }
|
| if "`e(hacsubtitleB)'" ~= "" {
|
| di in gr _n "`e(hacsubtitleB)'" _c
|
| }
|
| if "`e(hacsubtitleV)'" ~= "" {
|
| di in gr _n "`e(hacsubtitleV)'"
|
| }
|
| if "`e(hacsubtitleV2)'" ~= "" {
|
| di in gr "`e(hacsubtitleV2)'"
|
| }
|
| if "`e(kernel)'"!="" {
|
| di in gr " kernel=`e(kernel)'; bandwidth=" `e(bw)'
|
| if "`e(bwchoice)'"!="" {
|
| di in gr " `e(bwchoice)'"
|
| }
|
| di in gr " time variable (t): " in ye e(tvar)
|
| if "`e(ivar)'" != "" {
|
| di in gr " group variable (i): " in ye e(ivar)
|
| }
|
| }
|
| di
|
| if "`e(clustvar)'"!="" {
|
| if "`e(clustvar2)'"=="" {
|
| local N_clust `e(N_clust)'
|
| local clustvar `e(clustvar)'
|
| }
|
| else {
|
| local N_clust `e(N_clust1)'
|
| local clustvar `e(clustvar1)'
|
| }
|
| di in gr "Number of clusters (`clustvar') = " _col(33) in ye %6.0f `N_clust' _continue
|
| }
|
| di in gr _col(55) "Number of obs = " in ye %8.0f e(N)
|
| if "`e(clustvar2)'"!="" {
|
| di in gr "Number of clusters (" "`e(clustvar2)'" ") = " _col(33) in ye %6.0f e(N_clust2) _continue
|
| }
|
| di in gr _c _col(55) "F(" %3.0f e(Fdf1) "," %6.0f e(Fdf2) ") = "
|
| if e(F) < 99999 {
|
| di in ye %8.2f e(F)
|
| }
|
| else {
|
| di in ye %8.2e e(F)
|
| }
|
| di in gr _col(55) "Prob > F = " in ye %8.4f e(Fp)
|
|
|
| di in gr "Total (centered) SS = " in ye %12.0g e(yyc) _continue
|
| di in gr _col(55) "Centered R2 = " in ye %8.4f e(r2c)
|
| di in gr "Total (uncentered) SS = " in ye %12.0g e(yy) _continue
|
| di in gr _col(55) "Uncentered R2 = " in ye %8.4f e(r2u)
|
| di in gr "Residual SS = " in ye %12.0g e(rss) _continue
|
| di in gr _col(55) "Root MSE = " in ye %8.4g e(rmse)
|
| di
|
| }
|
|
|
|
|
|
|
|
|
| local cluster `e(clustvar)'
|
| ereturn local clustvar
|
| ereturn display, `plus' level(`level') `dispopt'
|
| ereturn local clustvar `cluster'
|
|
|
|
|
|
|
| if ~("`nofooter'"~="" | (e(model)=="ols" & (e(sargan)==0 | e(j)==0))) {
|
|
|
|
|
| if "`e(instd)'"~="" & "`e(idstat)'"~="" {
|
| di in smcl _c "{help `helpfile'##idtest:Underidentification test}"
|
| if "`e(vcetype)'`e(kernel)'"=="" {
|
| di in gr _c " (Anderson canon. corr. LM statistic):"
|
| }
|
| else {
|
| di in gr _c " (Kleibergen-Paap rk LM statistic):"
|
| }
|
| di in ye _col(71) %8.3f e(idstat)
|
| di in gr _col(52) "Chi-sq(" in ye e(iddf) |
| in gr ") P-val = " in ye _col(73) %6.4f e(idp)
|
|
|
| if "`e(redlist)'"!="" {
|
| di in gr "-redundant- option:"
|
| di in smcl _c "{help `helpfile'##redtest:IV redundancy test}"
|
| di in gr _c " (LM test of redundancy of specified instruments):"
|
| di in ye _col(71) %8.3f e(redstat)
|
| di in gr _col(52) "Chi-sq(" in ye e(reddf) |
| in gr ") P-val = " in ye _col(73) %6.4f e(redp)
|
| di in gr "Instruments tested: " _c
|
| Disp `e(redlist)', _col(23)
|
| }
|
| di in smcl in gr "{hline 78}"
|
| }
|
|
|
| if "`e(instd)'"~="" & "`e(idstat)'"~="" {
|
| di in smcl _c "{help `helpfile'##widtest:Weak identification test}"
|
| di in gr " (Cragg-Donald Wald F statistic):" in ye _col(71) %8.3f e(cdf)
|
| if "`e(vcetype)'`e(kernel)'"~="" {
|
| di in gr " (Kleibergen-Paap rk Wald F statistic):" in ye _col(71) %8.3f e(widstat)
|
| }
|
| di in gr _c "Stock-Yogo weak ID test critical values:"
|
| Disp_cdsy, model(`e(model)') k2(`e(exexog_ct)') nendog(`e(endog_ct)') fuller("`e(fuller)'") col1(42) col2(73)
|
| if `r(cdmissing)' {
|
| di in gr _col(64) "<not available>"
|
| }
|
| else {
|
| di in gr "Source: Stock-Yogo (2005). Reproduced by permission."
|
| if "`e(vcetype)'`e(kernel)'"~="" {
|
| di in gr "NB: Critical values are for Cragg-Donald F statistic and i.i.d. errors."
|
| }
|
| }
|
| di in smcl in gr "{hline 78}"
|
| }
|
|
|
|
|
|
|
| if `rprob' == 0 {
|
|
|
| if "`e(vcetype)'" == "Robust" | "`e(model)'" == "gmmw" {
|
| if "`e(instd)'" != "" {
|
| di in smcl _c "{help `helpfile'##overidtests:Hansen J statistic}"
|
| di in gr _c " (overidentification test of all instruments):"
|
| }
|
| else {
|
| di in smcl _c "{help `helpfile'##overidtests:Hansen J statistic}"
|
| di in gr _c " (Lagrange multiplier test of excluded instruments):"
|
| }
|
| }
|
| else {
|
| if "`e(instd)'" != "" {
|
| di in smcl _c "{help `helpfile'##overidtests:Sargan statistic}"
|
| di in gr _c " (overidentification test of all instruments):"
|
| }
|
| else {
|
| di in smcl _c "{help `helpfile'##overidtests:Sargan statistic}"
|
| di in gr _c " (Lagrange multiplier test of excluded instruments):"
|
| }
|
| }
|
| di in ye _col(71) %8.3f e(j)
|
| if e(jdf) {
|
| di in gr _col(52) "Chi-sq(" in ye e(jdf) |
| in gr ") P-val = " in ye _col(73) %6.4f e(jp)
|
| }
|
| else {
|
| di in gr _col(50) "(equation exactly identified)"
|
| }
|
|
|
|
|
| if e(cstat) != . {
|
|
|
| if e(cstat) > 0 {
|
| di in gr "-orthog- option:"
|
| if "`e(vcetype)'" == "Robust" {
|
| di in gr _c "Hansen J statistic (eqn. excluding suspect orthog. conditions): "
|
| }
|
| else {
|
| di in gr _c "Sargan statistic (eqn. excluding suspect orthogonality conditions):"
|
| }
|
| di in ye _col(71) %8.3f e(j)-e(cstat)
|
| di in gr _col(52) "Chi-sq(" in ye e(jdf)-e(cstatdf) in gr ") P-val = " |
| in ye _col(73) %6.4f chiprob(e(jdf)-e(cstatdf),e(j)-e(cstat))
|
| di in smcl _c "{help `helpfile'##ctest:C statistic}"
|
| di in gr _c " (exogeneity/orthogonality of suspect instruments): "
|
| di in ye _col(71) %8.3f e(cstat)
|
| di in gr _col(52) "Chi-sq(" in ye e(cstatdf) in gr ") P-val = " |
| in ye _col(73) %6.4f e(cstatp)
|
| di in gr "Instruments tested: " _c
|
| Disp `e(clist)', _col(23)
|
| }
|
| if e(cstat) == 0 {
|
| di in gr _n "Collinearity/identification problems in eqn. excl. suspect orthog. conditions:"
|
| di in gr " C statistic not calculated for -orthog- option"
|
| }
|
| }
|
| }
|
| else {
|
|
|
| di in r "Warning: estimated covariance matrix of moment conditions not of full rank."
|
| if e(j)==. {
|
| di in r " overidentification statistic not reported, and standard errors and"
|
| }
|
| di in r " model tests should be interpreted with caution."
|
| di in r "Possible causes:"
|
| if e(nocollin) {
|
| di in r " collinearities in regressors or instruments (with -nocollin- option)"
|
| }
|
| if "`e(N_clust)'" != "" {
|
| di in r " number of clusters insufficient to calculate robust covariance matrix"
|
| }
|
| if "`e(kernel)'" != "" {
|
| di in r " covariance matrix of moment conditions not positive definite"
|
| di in r " covariance matrix uses too many lags"
|
| }
|
| di in r " singleton dummy variable (dummy with one 1 and N-1 0s or vice versa)"
|
| di in r in smcl _c "{help `helpfile'##partial:partial}"
|
| di in r " option may address problem."
|
| }
|
|
|
|
|
| if e(estat) != . {
|
|
|
| if e(estat) > 0 {
|
| di in gr "-endog- option:"
|
| di in smcl _c "{help `helpfile'##endogtest:Endogeneity test}"
|
| di in gr _c " of endogenous regressors: "
|
| di in ye _col(71) %8.3f e(estat)
|
| di in gr _col(52) "Chi-sq(" in ye e(estatdf) |
| in gr ") P-val = " in ye _col(73) %6.4f e(estatp)
|
| di in gr "Regressors tested: " _c
|
| Disp `e(elist)', _col(23)
|
| }
|
| if e(estat) == 0 {
|
| di in gr _n "Collinearity/identification problems in restricted equation:"
|
| di in gr " Endogeneity test statistic not calculated for -endog- option"
|
| }
|
| }
|
|
|
| di in smcl in gr "{hline 78}"
|
|
|
| if "`e(model)'" == "liml" & "`e(vcetype)'" ~= "Robust" & "`e(kernel)'" == "" {
|
| if "`e(instd)'" != "" {
|
| di in smcl _c "{help `helpfile'##liml:Anderson-Rubin statistic}"
|
| di in gr _c " (overidentification test of all instruments):"
|
| }
|
| else {
|
| di in smcl _c "{help `helpfile'##liml:Anderson-Rubin statistic}"
|
| di in gr _c " (LR test of excluded instruments):"
|
| }
|
| di in ye _col(72) %7.3f e(arubin)
|
| if e(arubindf) {
|
| di in gr _col(52) "Chi-sq(" in ye e(arubindf) |
| in gr ") P-val = " in ye _col(73) %6.4f e(arubinp)
|
| }
|
| else {
|
| di in gr _col(50) "(equation exactly identified)"
|
| }
|
| di in smcl in gr "{hline 78}"
|
| }
|
| }
|
|
|
|
|
| if "`nofooter'"=="" {
|
|
|
|
|
|
|
| if colsof(e(S)) > e(rankzz) {
|
| di in gr "Collinearities detected among instruments: " _c
|
| di in gr colsof(e(S))-e(rankzz) " instrument(s) dropped"
|
| }
|
|
|
| if "`e(collin)'`e(dups)'" != "" | e(partial_ct) {
|
|
|
| local one "1"
|
| }
|
| if e(endog_ct) {
|
| di in gr "Instrumented:" _c
|
| Disp `e(instd`one')', _col(23)
|
| }
|
| if e(inexog_ct) {
|
| di in gr "Included instruments:" _c
|
| Disp `e(inexog`one')', _col(23)
|
| }
|
| if e(exexog_ct) {
|
| di in gr "Excluded instruments:" _c
|
| Disp `e(exexog`one')', _col(23)
|
| }
|
| if e(partial_ct) {
|
| if e(partialcons) {
|
| local partial "`e(partial`one')' _cons"
|
| }
|
| else {
|
| local partial "`e(partial`one')'"
|
| }
|
| di in smcl _c "{help `helpfile'##partial:Partialled-out}"
|
| di in gr ":" _c
|
| Disp `partial', _col(23)
|
| if "`e(partialsmall)'"=="" {
|
| di in gr _col(23) "nb: total SS, model F and R2s are after partialling-out;"
|
| di in gr _col(23) " any {help `helpfile'##s_small:small-sample adjustments} do not include"
|
| di in gr _col(23) " partialled-out variables in regressor count K"
|
| }
|
| else {
|
| di in gr _col(23) "nb: total SS, model F and R2s are after partialling-out;"
|
| di in gr _col(23) " any {help `helpfile'##s_small:small-sample adjustments} include partialled-out"
|
| di in gr _col(23) " variables in regressor count K"
|
| }
|
| }
|
| if "`e(dups)'" != "" {
|
| di in gr "Duplicates:" _c
|
| Disp `e(dups)', _col(23)
|
| }
|
| if "`e(collin)'" != "" {
|
| di in gr "Dropped collinear:" _c
|
| Disp `e(collin)', _col(23)
|
| }
|
| if "`e(ecollin)'" != "" {
|
| di in gr "Reclassified as exog:" _c
|
| Disp `e(ecollin)', _col(23)
|
| }
|
| di in smcl in gr "{hline 78}"
|
| }
|
| end
|
|
|
|
|
|
|
|
|
|
|
| program define DispCollinDups
|
| version 11.2
|
| if "`e(dups)'" != "" {
|
| di in gr "Warning - duplicate variables detected"
|
| di in gr "Duplicates:" _c
|
| Disp `e(dups)', _col(16)
|
| }
|
| if "`e(collin)'" != "" {
|
| di in gr "Warning - collinearities detected"
|
| di in gr "Vars dropped:" _c
|
| Disp `e(collin)', _col(16)
|
| }
|
| end
|
|
|
|
|
|
|
| program define DispFirst
|
| version 11.2
|
| args helpfile
|
| tempname firstmat ivest sheapr2 pr2 F df df_r pvalue
|
| tempname SWF SWFdf1 SWFdf2 SWFp SWr2
|
|
|
| mat `firstmat'=e(first)
|
| if `firstmat'[1,1] == . {
|
| di
|
| di in ye "Unable to display first-stage estimates; macro e(first) is missing"
|
| exit
|
| }
|
| di in gr _newline "First-stage regressions"
|
| di in smcl in gr "{hline 23}"
|
| di
|
| local endo1 : colnames(`firstmat')
|
| local nrvars : word count `endo1'
|
| local firsteqs "`e(firsteqs)'"
|
| local nreqs : word count `firsteqs'
|
| if `nreqs' < `nrvars' {
|
| di in ye "Unable to display all first-stage regressions."
|
| di in ye "There may be insufficient room to store results using -estimates store-,"
|
| di in ye "or names of endogenous regressors may be too long to store the results."
|
| di in ye "Try dropping one or more estimation results using -estimates drop-,"
|
| di in ye "using the -savefprefix- option, or using shorter variable names."
|
| di
|
| }
|
| local robust "`e(vcetype)'"
|
| local cluster "`e(clustvar)'"
|
| local kernel "`e(kernel)'"
|
| foreach eqname of local firsteqs {
|
| _estimates hold `ivest'
|
| capture estimates restore `eqname'
|
| if _rc != 0 {
|
| di
|
| di in ye "Unable to list stored estimation `eqname'."
|
| di in ye "There may be insufficient room to store results using -estimates store-,"
|
| di in ye "or names of endogenous regressors may be too long to store the results."
|
| di in ye "Try dropping one or more estimation results using -estimates drop-,"
|
| di in ye "using the -savefprefix- option, or using shorter variable names."
|
| di
|
| }
|
| else {
|
| local vn "`e(depvar)'"
|
| estimates replay `eqname', noheader
|
| mat `sheapr2' =`firstmat'["sheapr2","`vn'"]
|
| mat `pr2' =`firstmat'["pr2","`vn'"]
|
| mat `F' =`firstmat'["F","`vn'"]
|
| mat `df' =`firstmat'["df","`vn'"]
|
| mat `df_r' =`firstmat'["df_r","`vn'"]
|
| mat `pvalue' =`firstmat'["pvalue","`vn'"]
|
| mat `SWF' =`firstmat'["SWF","`vn'"]
|
| mat `SWFdf1' =`firstmat'["SWFdf1","`vn'"]
|
| mat `SWFdf2' =`firstmat'["SWFdf2","`vn'"]
|
| mat `SWFp' =`firstmat'["SWFp","`vn'"]
|
| mat `SWr2' =`firstmat'["SWr2","`vn'"]
|
|
|
| di in gr "F test of excluded instruments:"
|
| di in gr " F(" %3.0f `df'[1,1] "," %6.0f `df_r'[1,1] ") = " in ye %8.2f `F'[1,1]
|
| di in gr " Prob > F = " in ye %8.4f `pvalue'[1,1]
|
|
|
| di in smcl "{help `helpfile'##swstats:Sanderson-Windmeijer multivariate F test of excluded instruments:}"
|
| di in gr " F(" %3.0f `SWFdf1'[1,1] "," %6.0f `SWFdf2'[1,1] ") = " in ye %8.2f `SWF'[1,1]
|
| di in gr " Prob > F = " in ye %8.4f `SWFp'[1,1]
|
|
|
| di
|
| }
|
| _estimates unhold `ivest'
|
| }
|
| end
|
|
|
|
|
|
|
| program define DispStored
|
| args savesfirst saverf savefirst
|
| version 11.2
|
|
|
| if "`savesfirst'" != "" {
|
| local eqlist "`e(sfirsteq)'"
|
| }
|
| if "`saverf'" != "" {
|
| local eqlist "`eqlist' `e(rfeq)'"
|
| }
|
| if "`savefirst'" != "" {
|
| local eqlist "`eqlist' `e(firsteqs)'"
|
| }
|
| local eqlist : list retokenize eqlist
|
|
|
| di in gr _newline "Stored estimation results"
|
| di in smcl in gr "{hline 25}" _c
|
| capture estimates dir `eqlist'
|
| if "`eqlist'" != "" & _rc == 0 {
|
|
|
| estimates dir `eqlist'
|
| }
|
| else if "`eqlist'" != "" & _rc != 0 {
|
| di
|
| di in ye "Unable to list stored estimations."
|
| di
|
| }
|
| end
|
|
|
|
|
|
|
| program define DispFFirst
|
| version 11.2
|
| args helpfile
|
| tempname firstmat
|
| tempname sheapr2 pr2 F df df_r pvalue
|
| tempname SWF SWFdf1 SWFdf2 SWFp SWchi2 SWchi2p SWr2
|
| mat `firstmat'=e(first)
|
| if `firstmat'[1,1] == . {
|
| di
|
| di in ye "Unable to display summary of first-stage estimates; macro e(first) is missing"
|
| exit
|
| }
|
| local endo : colnames(`firstmat')
|
| local nrvars : word count `endo'
|
| local robust "`e(vcetype)'"
|
| local cluster "`e(clustvar)'"
|
| local kernel "`e(kernel)'"
|
| local efirsteqs "`e(firsteqs)'"
|
|
|
| mat `df' =`firstmat'["df",1]
|
| mat `df_r' =`firstmat'["df_r",1]
|
| mat `SWFdf1' =`firstmat'["SWFdf1",1]
|
| mat `SWFdf2' =`firstmat'["SWFdf2",1]
|
|
|
| di
|
| di in gr _newline "Summary results for first-stage regressions"
|
| di in smcl in gr "{hline 43}"
|
| di
|
|
|
| di _c in smcl _col(44) "{help `helpfile'##swstats:(Underid)}"
|
| di in smcl _col(65) "{help `helpfile'##swstats:(Weak id)}"
|
|
|
| di _c in gr "Variable |"
|
| di _c in smcl _col(16) "{help `helpfile'##swstats:F}" in gr "("
|
| di _c in ye _col(17) %3.0f `df'[1,1] in gr "," in ye %6.0f `df_r'[1,1] in gr ") P-val"
|
| di _c in gr _col(37) "|"
|
| di _c in smcl _col(39) "{help `helpfile'##swstats:SW Chi-sq}" in gr "("
|
| di _c in ye %3.0f `SWFdf1'[1,1] in gr ") P-val"
|
| di _c in gr _col(60) "|"
|
| di _c in smcl _col(62) "{help `helpfile'##swstats:SW F}" in gr "("
|
| di in ye _col(67) %3.0f `SWFdf1'[1,1] in gr "," in ye %6.0f `SWFdf2'[1,1] in gr ")"
|
|
|
| local i = 1
|
| foreach vn of local endo {
|
|
|
| mat `sheapr2' =`firstmat'["sheapr2","`vn'"]
|
| mat `pr2' =`firstmat'["pr2","`vn'"]
|
| mat `F' =`firstmat'["F","`vn'"]
|
| mat `df' =`firstmat'["df","`vn'"]
|
| mat `df_r' =`firstmat'["df_r","`vn'"]
|
| mat `pvalue' =`firstmat'["pvalue","`vn'"]
|
| mat `SWF' =`firstmat'["SWF","`vn'"]
|
| mat `SWFdf1' =`firstmat'["SWFdf1","`vn'"]
|
| mat `SWFdf2' =`firstmat'["SWFdf2","`vn'"]
|
| mat `SWFp' =`firstmat'["SWFp","`vn'"]
|
| mat `SWchi2' =`firstmat'["SWchi2","`vn'"]
|
| mat `SWchi2p' =`firstmat'["SWchi2p","`vn'"]
|
| mat `SWr2' =`firstmat'["SWr2","`vn'"]
|
|
|
| local vnlen : length local vn
|
| if `vnlen' > 12 {
|
| local vn : piece 1 12 of "`vn'"
|
| }
|
| di _c in y %-12s "`vn'" _col(14) in gr "|" _col(18) in y %8.2f `F'[1,1]
|
| di _c _col(28) in y %8.4f `pvalue'[1,1]
|
| di _c _col(37) in g "|" _col(42) in y %8.2f `SWchi2'[1,1] _col(51) in y %8.4f `SWchi2p'[1,1]
|
| di _col(60) in g "|" _col(65) in y %8.2f `SWF'[1,1]
|
| local i = `i' + 1
|
| }
|
| di
|
|
|
| if "`robust'`cluster'" != "" {
|
| if "`cluster'" != "" {
|
| local rtype "cluster-robust"
|
| }
|
| else if "`kernel'" != "" {
|
| local rtype "heteroskedasticity and autocorrelation-robust"
|
| }
|
| else {
|
| local rtype "heteroskedasticity-robust"
|
| }
|
| }
|
| else if "`kernel'" != "" {
|
| local rtype "autocorrelation-robust"
|
| }
|
| if "`robust'`cluster'`kernel'" != "" {
|
| di in gr "NB: first-stage test statistics `rtype'"
|
| di
|
| }
|
|
|
| local k2 = `SWFdf1'[1,1]
|
| di in gr "Stock-Yogo weak ID F test critical values for single endogenous regressor:"
|
| Disp_cdsy, model(`e(model)') k2(`e(exexog_ct)') nendog(1) fuller("`e(fuller)'") col1(36) col2(67)
|
| if `r(cdmissing)' {
|
| di in gr _col(64) "<not available>"
|
| }
|
| else {
|
| di in gr "Source: Stock-Yogo (2005). Reproduced by permission."
|
| if "`e(model)'"=="iv" & "`e(vcetype)'`e(kernel)'"=="" {
|
| di in gr "NB: Critical values are for Sanderson-Windmeijer F statistic."
|
| }
|
| else {
|
| di in gr "NB: Critical values are for i.i.d. errors only."
|
| }
|
| di
|
| }
|
|
|
|
|
| if e(iddf)~=`SWFdf1'[1,1] {
|
| di in red "Warning: Error in calculating first-stage id statistics above;"
|
| di in red " dof of SW statistics is " `SWFdf1'[1,1] ", should be L-(K-1)=`e(iddf)'."
|
| }
|
|
|
| tempname iddf idstat idp widstat cdf rkf
|
| scalar `iddf'=e(iddf)
|
| scalar `idstat'=e(idstat)
|
| scalar `idp'=e(idp)
|
| scalar `widstat'=e(widstat)
|
| scalar `cdf'=e(cdf)
|
| capture scalar `rkf'=e(rkf)
|
| di in smcl "{help `helpfile'##idtest:Underidentification test}"
|
| di in gr "Ho: matrix of reduced form coefficients has rank=K1-1 (underidentified)"
|
| di in gr "Ha: matrix has rank=K1 (identified)"
|
| if "`robust'`kernel'"=="" {
|
| di in ye "Anderson canon. corr. LM statistic" _c
|
| }
|
| else {
|
| di in ye "Kleibergen-Paap rk LM statistic" _c
|
| }
|
| di in gr _col(42) "Chi-sq(" in ye `iddf' in gr ")=" %-7.2f in ye `idstat' |
| _col(61) in gr "P-val=" %6.4f in ye `idp'
|
|
|
| di
|
| di in smcl "{help `helpfile'##widtest:Weak identification test}"
|
| di in gr "Ho: equation is weakly identified"
|
| di in ye "Cragg-Donald Wald F statistic" _col(65) %8.2f `cdf'
|
| if "`robust'`kernel'"~="" {
|
| di in ye "Kleibergen-Paap Wald rk F statistic" _col(65) %8.2f `rkf'
|
| }
|
| di
|
|
|
| di in gr "Stock-Yogo weak ID test critical values for K1=`e(endog_ct)' and L1=`e(exexog_ct)':"
|
| Disp_cdsy, model(`e(model)') k2(`e(exexog_ct)') nendog(`e(endog_ct)') fuller("`e(fuller)'") col1(36) col2(67)
|
| if `r(cdmissing)' {
|
| di in gr _col(64) "<not available>"
|
| }
|
| else {
|
| di in gr "Source: Stock-Yogo (2005). Reproduced by permission."
|
| if "`e(vcetype)'`e(kernel)'"~="" {
|
| di in gr "NB: Critical values are for Cragg-Donald F statistic and i.i.d. errors."
|
| }
|
| }
|
| di
|
|
|
| tempname arf arfp archi2 archi2p ardf ardf_r
|
| tempname sstat sstatp sstatdf
|
| di in smcl "{help `helpfile'##wirobust:Weak-instrument-robust inference}"
|
| di in gr "Tests of joint significance of endogenous regressors B1 in main equation"
|
| di in gr "Ho: B1=0 and orthogonality conditions are valid"
|
|
|
| scalar `arf'=e(arf)
|
| scalar `arfp'=e(arfp)
|
| scalar `archi2'=e(archi2)
|
| scalar `archi2p'=e(archi2p)
|
| scalar `ardf'=e(ardf)
|
| scalar `ardf_r'=e(ardf_r)
|
| scalar `sstat'=e(sstat)
|
| scalar `sstatp'=e(sstatp)
|
| scalar `sstatdf'=e(sstatdf)
|
| di in ye _c "Anderson-Rubin Wald test"
|
| di in gr _col(36) "F(" in ye `ardf' in gr "," in ye `ardf_r' in gr ")=" |
| _col(49) in ye %7.2f `arf' _col(61) in gr "P-val=" in ye %6.4f `arfp'
|
| di in ye _c "Anderson-Rubin Wald test"
|
| di in gr _col(36) "Chi-sq(" in ye `ardf' in gr ")=" |
| _col(49) in ye %7.2f `archi2' _col(61) in gr "P-val=" in ye %6.4f `archi2p'
|
| di in ye _c "Stock-Wright LM S statistic"
|
| di in gr _col(36) "Chi-sq(" in ye `sstatdf' in gr ")=" |
| _col(49) in ye %7.2f `sstat' _col(61) in gr "P-val=" in ye %6.4f `sstatp'
|
| di
|
| if "`robust'`cluster'`kernel'" != "" {
|
| di in gr "NB: Underidentification, weak identification and weak-identification-robust"
|
| di in gr " test statistics `rtype'"
|
| di
|
| }
|
|
|
| if "`cluster'" != "" & "`e(clustvar2)'"=="" {
|
| di in gr "Number of clusters N_clust = " in ye %10.0f e(N_clust)
|
| }
|
| else if "`e(clustvar2)'" ~= "" {
|
| di in gr "Number of clusters (1) N_clust1 = " in ye %10.0f e(N_clust1)
|
| di in gr "Number of clusters (2) N_clust2 = " in ye %10.0f e(N_clust2)
|
| }
|
| di in gr "Number of observations N = " in ye %10.0f e(N)
|
| di in gr "Number of regressors K = " in ye %10.0f e(rankxx)
|
| di in gr "Number of endogenous regressors K1 = " in ye %10.0f e(endog_ct)
|
| di in gr "Number of instruments L = " in ye %10.0f e(rankzz)
|
| di in gr "Number of excluded instruments L1 = " in ye %10.0f e(ardf)
|
| if "`e(partial)'" != "" {
|
| di in gr "Number of partialled-out regressors/IVs = " in ye %10.0f e(partial_ct)
|
| di in gr "NB: K & L do not included partialled-out variables"
|
| }
|
|
|
| end
|
|
|
|
|
|
|
| program define PostFirstRF, eclass
|
| version 11.2
|
| syntax [if]
|
| [ ,
|
| first(string)
|
| rf
|
| rmse_rf(real 0)
|
| bmat(name)
|
| vmat(name)
|
| smat(name)
|
| firstmat(name)
|
| lhs1(string)
|
| endo1(string)
|
| znames0(string)
|
| znames1(string)
|
| bvclean(integer 0)
|
| fvops(integer 0)
|
| partial_ct(integer 0)
|
| robust
|
| cluster(string)
|
| cluster1(string)
|
| cluster2(string)
|
| nc(integer 0)
|
| nc1(integer 0)
|
| nc2(integer 0)
|
| kernel(string)
|
| bw(real 0)
|
| ivar(name)
|
| tvar(name)
|
| obs(integer 0)
|
| iv1_ct(integer 0)
|
| cons(integer 0)
|
| partialcons(integer 0)
|
| dofminus(integer 0)
|
| sdofminus(integer 0)
|
| ]
|
|
|
|
|
| local N = `obs'
|
| local N_clust = `nc'
|
| local N_clust1 = `nc1'
|
| local N_clust2 = `nc2'
|
| tempname b V S
|
| mat `b' = `bmat'
|
| mat `V' = `vmat'
|
| mat `S' = `smat'
|
|
|
| marksample touse
|
|
|
| mat colname `b' = `lhs1' `endo1'
|
| mat rowname `b' = `znames1'
|
| mat `b' = vec(`b')
|
| mat `b' = `b''
|
| mat colname `V' = `: colfullnames `b''
|
| mat rowname `V' = `: colfullnames `b''
|
| mat colname `S' = `: colfullnames `b''
|
| mat rowname `S' = `: colfullnames `b''
|
|
|
| if "`cluster'"=="" {
|
| matrix `V'=`V'*(`N'-`dofminus')/(`N'-`iv1_ct'-`dofminus'-`sdofminus')
|
| }
|
| else {
|
| matrix `V'=`V'*(`N'-1)/(`N'-`iv1_ct'-`sdofminus')
|
|
|
| }
|
|
|
|
|
|
|
| if "`rf'`first'"~="" {
|
| if "`rf'"~="" {
|
| local vnum = 0
|
| local model rf
|
| local depvar `lhs1'
|
| local rmse = `rmse_rf'
|
| }
|
| else {
|
| local vnum : list posof "`first'" in endo1
|
| local vnum = `vnum'
|
| local model first
|
| local depvar `first'
|
| local rmse = el(`firstmat', rownumb(`firstmat',"rmse"), colnumb(`firstmat',"`first'"))
|
| }
|
| local c0 = 1 + `vnum'*`iv1_ct'
|
| local c1 = (`vnum'+1)*`iv1_ct'
|
| mat `b' = `b'[1,`c0'..`c1']
|
| mat `V' = `V'[`c0'..`c1',`c0'..`c1']
|
| mat `S' = `S'[`c0'..`c1',`c0'..`c1']
|
| mat coleq `b' = ""
|
| mat coleq `V' = ""
|
| mat roweq `V' = ""
|
| mat coleq `S' = ""
|
| mat roweq `S' = ""
|
| }
|
| else {
|
| local model sfirst
|
| local eqlist `lhs1' `endo1'
|
| }
|
|
|
|
|
|
|
| if ~`bvclean' {
|
| AddOmitted, bmat(`b') vmat(`V') cnb0(`znames0') cnb1(`znames1') eqlist(`eqlist')
|
| mat `b' = r(b)
|
| mat `V' = r(V)
|
|
|
| if `fvops' & ~`partial_ct' {
|
| local bfv "buildfvinfo"
|
| }
|
| }
|
|
|
| local dof = `N' - `iv1_ct' - `dofminus' - `sdofminus'
|
| ereturn post `b' `V', obs(`obs') esample(`touse') dof(`dof') depname(`depvar') `bfv'
|
|
|
|
|
| if "`rf'`first'"~="" {
|
| ereturn scalar rmse = `rmse'
|
| ereturn scalar df_r = `dof'
|
| ereturn scalar df_m = `iv1_ct' - `cons' + `sdofminus' - `partialcons'
|
| }
|
| ereturn scalar k_eq = `: word count `endo1''
|
| ereturn local cmd ivreg2
|
| ereturn local model `model'
|
| ereturn matrix S `S'
|
|
|
| if "`kernel'"!="" {
|
| ereturn local kernel "`kernel'"
|
| ereturn scalar bw=`bw'
|
| ereturn local tvar "`tvar'"
|
| if "`ivar'" ~= "" {
|
| ereturn local ivar "`ivar'"
|
| }
|
| }
|
|
|
| if "`robust'" != "" {
|
| local vce "robust"
|
| }
|
| if "`cluster1'" != "" {
|
| if "`cluster2'"=="" {
|
| local vce "`vce' cluster"
|
| }
|
| else {
|
| local vce "`vce' two-way cluster"
|
| }
|
| }
|
| if "`kernel'" != "" {
|
| if "`robust'" != "" {
|
| local vce "`vce' hac"
|
| }
|
| else {
|
| local vce "`vce' ac"
|
| }
|
| local vce "`vce' `kernel' bw=`bw'"
|
| }
|
|
|
| local vce : list clean vce
|
| local vce = lower("`vce'")
|
| ereturn local vce `vce'
|
|
|
| if "`cluster'"!="" {
|
| ereturn scalar N_clust=`N_clust'
|
| ereturn local clustvar `cluster'
|
| }
|
| if "`cluster2'"!="" {
|
| ereturn scalar N_clust1=`N_clust1'
|
| ereturn scalar N_clust2=`N_clust2'
|
| ereturn local clustvar1 `cluster1'
|
| ereturn local clustvar2 `cluster2'
|
| }
|
|
|
| if "`robust'`cluster'" != "" {
|
| ereturn local vcetype "Robust"
|
| }
|
|
|
|
|
| if "`e(vcetype)'" == "Robust" {
|
| local hacsubtitle1 "heteroskedasticity"
|
| }
|
| if "`e(kernel)'"!="" & "`e(clustvar)'"=="" {
|
| local hacsubtitle3 "autocorrelation"
|
| }
|
| if "`kiefer'"!="" {
|
| local hacsubtitle3 "within-cluster autocorrelation (Kiefer)"
|
| }
|
| if "`e(clustvar)'"!="" {
|
| if "`e(clustvar2)'"=="" {
|
| local hacsubtitle3 "clustering on `e(clustvar)'"
|
| }
|
| else {
|
| local hacsubtitle3 "clustering on `e(clustvar1)' and `e(clustvar2)'"
|
| }
|
| if "`e(kernel)'" != "" {
|
| local hacsubtitle4 "and kernel-robust to common correlated disturbances (Driscoll-Kraay)"
|
| }
|
| }
|
| if "`hacsubtitle1'"~="" & "`hacsubtitle3'" ~= "" {
|
| local hacsubtitle2 " and "
|
| }
|
| local hacsubtitle "`hacsubtitle1'`hacsubtitle2'`hacsubtitle3'"
|
| if "`hacsubtitle'"~="" {
|
| ereturn local hacsubtitleV "Statistics robust to `hacsubtitle'"
|
| }
|
| else {
|
| ereturn local hacsubtitleV "Statistics consistent for homoskedasticity only"
|
| }
|
| if "`hacsubtitle4'"~="" {
|
| ereturn local hacsubtitleV2 "`hacsubtitle4'"
|
| }
|
| if "`sw'"~="" {
|
| ereturn local hacsubtitleV "Stock-Watson heteroskedastic-robust statistics (BETA VERSION)"
|
| }
|
|
|
| end
|
|
|
|
|
|
|
|
|
| program define IsStop, sclass
|
| |
|
|
| version 11.2
|
| if `"`0'"' == "[" {
|
| sret local stop 1
|
| exit
|
| }
|
| if `"`0'"' == "," {
|
| sret local stop 1
|
| exit
|
| }
|
| if `"`0'"' == "if" {
|
| sret local stop 1
|
| exit
|
| }
|
|
|
| if substr(`"`0'"',1,3) == "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
|
|
|
|
|
|
|
| program define Disp
|
| version 11.2
|
| syntax [anything] [, _col(integer 15) ]
|
| local maxlen = 80-`_col'
|
| local len = 0
|
| local first = 1
|
| foreach vn in `anything' {
|
|
|
| _ms_parse_parts `vn'
|
| if ~`r(omit)' {
|
| local vnlen : length local vn
|
| if `len'+`vnlen' > `maxlen' {
|
| di
|
| local first = 1
|
| local len = `vnlen'
|
| }
|
| else {
|
| local len = `len'+`vnlen'+1
|
| }
|
| if `first' {
|
| local first = 0
|
| di in gr _col(`_col') "`vn'" _c
|
| }
|
| else {
|
| di in gr " `vn'" _c
|
| }
|
| }
|
| }
|
|
|
| di
|
| end
|
|
|
|
|
|
|
| program define Disp_cdsy, rclass
|
| version 11.2
|
| syntax , col1(integer) col2(integer) model(string) k2(integer) nendog(integer) [ fuller(string) ]
|
| local cdmissing=1
|
| if "`model'"=="iv" | "`model'"=="gmm2s" | "`model'"=="gmmw" {
|
| cdsy, type(ivbias5) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') " 5% maximal IV relative bias" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(ivbias10) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "10% maximal IV relative bias" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(ivbias20) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "20% maximal IV relative bias" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(ivbias30) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "30% maximal IV relative bias" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(ivsize10) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "10% maximal IV size" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(ivsize15) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "15% maximal IV size" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(ivsize20) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "20% maximal IV size" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(ivsize25) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "25% maximal IV size" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| }
|
| else if ("`model'"=="liml" & "`fuller'"=="") | "`model'"=="cue" {
|
| cdsy, type(limlsize10) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "10% maximal LIML size" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(limlsize15) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "15% maximal LIML size" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(limlsize20) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "20% maximal LIML size" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(limlsize25) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "25% maximal LIML size" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| }
|
| else if ("`model'"=="liml" & "`fuller'"~="") {
|
| cdsy, type(fullrel5) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') " 5% maximal Fuller rel. bias" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(fullrel10) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "10% maximal Fuller rel. bias" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(fullrel20) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "20% maximal Fuller rel. bias" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(fullrel30) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "30% maximal Fuller rel. bias" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(fullmax5) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') " 5% Fuller maximum bias" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(fullmax10) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "10% Fuller maximum bias" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(fullmax20) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "20% Fuller maximum bias" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| cdsy, type(fullmax30) k2(`k2') nendog(`nendog')
|
| if "`r(cv)'"~="." {
|
| di in gr _col(`col1') "30% Fuller maximum bias" in ye _col(`col2') %6.2f r(cv)
|
| local cdmissing=0
|
| }
|
| di in gr "NB: Critical values based on Fuller parameter=1"
|
| }
|
| return scalar cdmissing =`cdmissing'
|
| end
|
|
|
| program define cdsy, rclass
|
| version 11.2
|
| syntax , type(string) k2(integer) nendog(integer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| tempname temp cv
|
|
|
|
|
| scalar `cv'=.
|
|
|
| if "`type'"=="ivbias5" {
|
| mata: s_cdsy("`temp'", 1)
|
| if `k2'<=100 & `nendog'<=3 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="ivbias10" {
|
| mata: s_cdsy("`temp'", 2)
|
| if `k2'<=100 & `nendog'<=3 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="ivbias20" {
|
| mata: s_cdsy("`temp'", 3)
|
| if `k2'<=100 & `nendog'<=3 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="ivbias30" {
|
| mata: s_cdsy("`temp'", 4)
|
| if `k2'<=100 & `nendog'<=3 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
|
|
| if "`type'"=="ivsize10" {
|
| mata: s_cdsy("`temp'", 5)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="ivsize15" {
|
| mata: s_cdsy("`temp'", 6)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="ivsize20" {
|
| mata: s_cdsy("`temp'", 7)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="ivsize25" {
|
| mata: s_cdsy("`temp'", 8)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="fullrel5" {
|
| mata: s_cdsy("`temp'", 9)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="fullrel10" {
|
| mata: s_cdsy("`temp'", 10)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="fullrel20" {
|
| mata: s_cdsy("`temp'", 11)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="fullrel30" {
|
| mata: s_cdsy("`temp'", 12)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="fullmax5" {
|
| mata: s_cdsy("`temp'", 13)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="fullmax10" {
|
| mata: s_cdsy("`temp'", 14)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="fullmax20" {
|
| mata: s_cdsy("`temp'", 15)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="fullmax30" {
|
| mata: s_cdsy("`temp'", 16)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="limlsize10" {
|
| mata: s_cdsy("`temp'", 17)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="limlsize15" {
|
| mata: s_cdsy("`temp'", 18)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="limlsize20" {
|
| mata: s_cdsy("`temp'", 19)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
|
|
| if "`type'"=="limlsize25" {
|
| mata: s_cdsy("`temp'", 20)
|
| if `k2'<=100 & `nendog'<=2 {
|
| scalar `cv'=`temp'[`k2',`nendog']
|
| }
|
| }
|
| return scalar cv=`cv'
|
| end
|
|
|
|
|
|
|
| program define ivparse, sclass
|
| version 11.2
|
| syntax [anything(name=0)]
|
| [ ,
|
| ivreg2name(name)
|
| partial(string)
|
| fwl(string)
|
| orthog(varlist fv ts)
|
| endogtest(varlist fv ts)
|
| redundant(varlist fv ts)
|
| depname(string)
|
| robust
|
| cluster(varlist fv ts)
|
| bw(string)
|
| kernel(string)
|
| dkraay(integer 0)
|
| sw
|
| kiefer
|
| center
|
| NOCONSTANT
|
| tvar(varname)
|
| ivar(varname)
|
| gmm2s
|
| gmm
|
| cue
|
| liml
|
| fuller(real 0)
|
| kclass(real 0)
|
| b0(string)
|
| wmatrix(string)
|
| NOID
|
| savefirst
|
| savefprefix(name)
|
| saverf
|
| saverfprefix(name)
|
| savesfirst
|
| savesfprefix(name)
|
| psd0
|
| psda
|
| dofminus(integer 0)
|
| NOCOLLIN
|
| useqr
|
| bvclean
|
| eform(string)
|
| NOOMITTED
|
| vsquish
|
| noemptycells
|
| baselevels
|
| allbaselevels
|
| ]
|
|
|
|
|
| local tsops = ("`s(tsops)'"=="true")
|
| local fvops = ("`s(fvops)'"=="true")
|
|
|
| local cons =("`noconstant'"=="")
|
|
|
| local n 0
|
| gettoken lhs 0 : 0, parse(" ,[") match(paren)
|
| IsStop `lhs'
|
| while `s(stop)'==0 {
|
| if "`paren'"=="(" {
|
| local ++n
|
| if `n'>1 {
|
| di as err `"syntax is "(all instrumented variables = instrument variables)""'
|
| exit 198
|
| }
|
| gettoken p lhs : lhs, parse(" =")
|
| while "`p'"!="=" {
|
| if "`p'"=="" {
|
| di as err `"syntax is "(all instrumented variables = instrument variables)""'
|
| di as er `"the equal sign "=" is required"'
|
| exit 198
|
| }
|
| local endo `endo' `p'
|
| gettoken p lhs : lhs, parse(" =")
|
| }
|
| local exexog `lhs'
|
| }
|
| else {
|
| local inexog `inexog' `lhs'
|
| }
|
| gettoken lhs 0 : 0, parse(" ,[") match(paren)
|
| IsStop `lhs'
|
| }
|
|
|
| gettoken lhs inexog : inexog
|
| local endo : list retokenize endo
|
| local inexog : list retokenize inexog
|
| local exexog : list retokenize exexog
|
|
|
| if "`depname'"=="" {
|
| local depname `lhs'
|
| }
|
|
|
|
|
| local partial `partial' `fwl'
|
|
|
|
|
|
|
| local partial : subinstr local partial "_cons" "", all count(local partialcons) word
|
| local partial : list retokenize partial
|
| if "`partial'"=="_all" {
|
| local partial `inexog'
|
| }
|
|
|
|
|
| if "`partial'"~="" {
|
| local partialcons = (`cons' | `partialcons')
|
| }
|
|
|
|
|
|
|
| sreturn clear
|
| local 0 `lhs' `inexog' `endo' `exexog' `partial'
|
| syntax varlist(fv ts)
|
| local tsops = ("`s(tsops)'"=="true") | `tsops'
|
| local fvops = ("`s(fvops)'"=="true") | `fvops'
|
|
|
|
|
| if "`tvar'" == "" {
|
| local tvar `_dta[_TStvar]'
|
| }
|
| if "`ivar'" == "" {
|
| local ivar `_dta[_TSpanel]'
|
| }
|
| if "`_dta[_TSdelta]'" == "" {
|
| local tdelta 1
|
| }
|
| else {
|
| local tdelta = `_dta[_TSdelta]'
|
| }
|
|
|
| sreturn local lhs `lhs'
|
| sreturn local depname `depname'
|
| sreturn local endo `endo'
|
| sreturn local inexog `inexog'
|
| sreturn local exexog `exexog'
|
| sreturn local partial `partial'
|
| sreturn local cons =`cons'
|
| sreturn local partialcons =`partialcons'
|
| sreturn local tsops =`tsops'
|
| sreturn local fvops =`fvops'
|
| sreturn local tvar `tvar'
|
| sreturn local ivar `ivar'
|
| sreturn local tdelta `tdelta'
|
| sreturn local noid `noid'
|
| sreturn local liml `liml'
|
|
|
|
|
| sreturn local useqr =("`useqr'" ~= "")
|
|
|
|
|
| if "`cluster'`sw'"~="" {
|
| local robust "robust"
|
| }
|
|
|
|
|
|
|
|
|
|
|
| if `dkraay' {
|
| if "`bw'" == "" {
|
| local bw `dkraay'
|
| }
|
| if "`cluster'" == "" {
|
| local cluster `tvar'
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| if "`bw'" == "" & "`kernel'" == "" {
|
| local bw 0
|
| }
|
| else {
|
|
|
|
|
| mata: s_vkernel("`kernel'", "`bw'", "`ivar'")
|
| local kernel `r(kernel)'
|
| local bw `r(bw)'
|
| local tsops = 1
|
| }
|
|
|
| if "`kiefer'" ~= "" & "`kernel'" == "" {
|
| local kernel "Truncated"
|
| }
|
|
|
|
|
| sreturn local bw `bw'
|
| sreturn local kernel `kernel'
|
| sreturn local robust `robust'
|
| sreturn local cluster `cluster'
|
| if `bw' {
|
| sreturn local bwopt "bw(`bw')"
|
| sreturn local kernopt "kernel(`kernel')"
|
| }
|
|
|
| sreturn local center =("`center'"=="center")
|
|
|
|
|
| if `fuller' != 0 {
|
| sreturn local liml "liml"
|
| sreturn local fulleropt "fuller(`fuller')"
|
| }
|
|
|
| if `kclass' != 0 {
|
| sreturn local kclassopt "kclass(`kclass')"
|
| }
|
|
|
|
|
| if "`b0'" ~= "" {
|
| sreturn local noid "noid"
|
| }
|
|
|
|
|
| if "`savefprefix'" != "" {
|
| local savefirst "savefirst"
|
| }
|
| else {
|
| local savefprefix "_`ivreg2name'_"
|
| }
|
| sreturn local savefirst `savefirst'
|
| sreturn local savefprefix `savefprefix'
|
| if "`saverfprefix'" != "" {
|
| local saverf "saverf"
|
| }
|
| else {
|
| local saverfprefix "_`ivreg2name'_"
|
| }
|
| sreturn local saverf `saverf'
|
| sreturn local saverfprefix `saverfprefix'
|
| if "`savesfprefix'" != "" {
|
| local savesfirst "savesfirst"
|
| }
|
| else {
|
| local savesfprefix "_`ivreg2name'_"
|
| }
|
| sreturn local savesfirst `savesfirst'
|
| sreturn local savesfprefix `savesfprefix'
|
|
|
|
|
| sreturn local psd "`psd0'`psda'"
|
|
|
|
|
| if `dofminus' {
|
| sreturn local dofmopt dofminus(`dofminus')
|
| }
|
|
|
|
|
| local dispopt eform(`eform') `vsquish' `noomitted' `noemptycells' `baselevels' `allbaselevels'
|
|
|
|
|
| local bvclean = wordcount("`bvclean'") | wordcount("`partial'") | `partialcons'
|
| sreturn local bvclean `bvclean'
|
| sreturn local dispopt `dispopt'
|
|
|
|
|
|
|
| if `partialcons' & ~`cons' {
|
| di in r "Error: _cons listed in partial() but equation specifies -noconstant-."
|
| exit 198
|
| }
|
| if `partialcons' > 1 {
|
|
|
| di in r "Error: _cons listed more than once in partial()."
|
| exit 198
|
| }
|
|
|
|
|
| if "`tvar'"!="`_dta[_TStvar]'" {
|
| di as err "invalid tvar() option - data already -tsset-"
|
| exit 5
|
| }
|
| if "`ivar'"!="`_dta[_TSpanel]'" {
|
| di as err "invalid ivar() option - data already -xtset-"
|
| exit 5
|
| }
|
|
|
|
|
| if `dkraay' {
|
| if "`ivar'" == "" | "`tvar'" == "" {
|
| di as err "invalid use of dkraay option - must use tsset panel data"
|
| exit 5
|
| }
|
| if "`dkraay'" ~= "`bw'" {
|
| di as err "cannot use dkraay(.) and bw(.) options together"
|
| exit 198
|
| }
|
| if "`cluster'" ~= "`tvar'" {
|
| di as err "invalid use of dkraay option - must cluster on `tvar' (or omit cluster option)"
|
| exit 198
|
| }
|
| }
|
|
|
|
|
| if "`kiefer'" ~= "" {
|
| if "`ivar'" == "" | "`tvar'" == "" {
|
| di as err "invalid use of kiefer option - must use tsset panel data"
|
| exit 5
|
| }
|
| if "`robust'" ~= "" {
|
| di as err "incompatible options: kiefer and robust"
|
| exit 198
|
| }
|
| if "`kernel'" ~= "" & "`kernel'" ~= "Truncated" {
|
| di as err "incompatible options: kiefer and kernel(`kernel')"
|
| exit 198
|
| }
|
| if (`bw'~=0) {
|
| di as err "incompatible options: kiefer and bw"
|
| exit 198
|
| }
|
| }
|
|
|
|
|
| if "`sw'" ~= "" & "`cluster'" ~= "" {
|
| di as err "Stock-Watson robust SEs not supported with -cluster- option"
|
| exit 198
|
| }
|
| if "`sw'" ~= "" & "`kernel'" ~= "" {
|
| di as err "Stock-Watson robust SEs not supported with -kernel- option"
|
| exit 198
|
| }
|
| if "`sw'" ~= "" & "`ivar'"=="" {
|
| di as err "Must -xtset- or -tsset- data or specify -ivar- with -sw- option"
|
| exit 198
|
| }
|
|
|
|
|
| if "`liml'`kclassopt'" != "" {
|
| if "`gmm2s'`cue'" != "" {
|
| di as err "GMM estimation not available with LIML or k-class estimators"
|
| exit 198
|
| }
|
| if `fuller' < 0 {
|
| di as err "invalid Fuller option"
|
| exit 198
|
| }
|
| if "`liml'" != "" & "`kclassopt'" != "" {
|
| di as err "cannot use liml and kclass options together"
|
| exit 198
|
| }
|
| if `kclass' < 0 {
|
| di as err "invalid k-class option"
|
| exit 198
|
| }
|
| }
|
|
|
| if "`gmm2s'" != "" & "`cue'" != "" {
|
| di as err "incompatible options: 2-step efficient gmm and cue gmm"
|
| exit 198
|
| }
|
|
|
| if "`gmm2s'`cue'" != "" & "`exexog'" == "" {
|
| di as err "option `gmm2s'`cue' invalid: no excluded instruments specified"
|
| exit 102
|
| }
|
|
|
|
|
| if "`gmm'" ~= "" {
|
| di as err "-gmm- is no longer a supported option; use -gmm2s- with the appropriate option"
|
| di as res " gmm = gmm2s robust"
|
| di as res " gmm robust = gmm2s robust"
|
| di as res " gmm bw() = gmm2s bw()"
|
| di as res " gmm robust bw() = gmm2s robust bw()"
|
| di as res " gmm cluster() = gmm2s cluster()"
|
| exit 198
|
| }
|
|
|
|
|
| if "`b0'" ~= "" & "`gmm2s'`cue'`liml'`wmatrix'" ~= "" {
|
| di as err "incompatible options: -b0- and `gmm2s' `cue' `liml' `wmatrix'"
|
| exit 198
|
| }
|
| if "`b0'" ~= "" & `kclass' ~= 0 {
|
| di as err "incompatible options: -b0- and kclass(`kclass')"
|
| exit 198
|
| }
|
|
|
| if "`psd0'"~="" & "`psda'"~="" {
|
| di as err "cannot use psd0 and psda options together"
|
| exit 198
|
| }
|
| end
|
|
|
|
|
|
|
| program define CheckDupsCollin, sclass
|
| version 11.2
|
| syntax
|
| [ ,
|
| lhs(string)
|
| endo(string)
|
| inexog(string)
|
| exexog(string)
|
| partial(string)
|
| orthog(string)
|
| endogtest(string)
|
| redundant(string)
|
| touse(string)
|
| wvar(string)
|
| wf(real 0)
|
| NOCONSTANT
|
| NOCOLLIN
|
| fvall
|
| fvsep
|
| ]
|
|
|
| if "`fvall'`fvsep'"=="" {
|
| local rhs `endo' `inexog'
|
| foreach vl in lhs rhs exexog {
|
| fvexpand ``vl'' if `touse'
|
| local `vl' `r(varlist)'
|
| }
|
| local allvars `rhs' `exexog'
|
| }
|
| else if "`fvall'"~="" {
|
| fvexpand `lhs' if `touse'
|
| local lhs `r(varlist)'
|
| fvexpand `endo' `inexog' `exexog' if `touse'
|
| local allvars `r(varlist)'
|
| }
|
| else if "`fvsep'"~="" {
|
| foreach vl in lhs endo inexog exexog {
|
| fvexpand ``vl'' if `touse'
|
| local `vl' `r(varlist)'
|
| }
|
| local allvars `endo' `inexog' `exexog'
|
| }
|
| else {
|
| di as err "internal ivreg2 err: CheckDupsCollin"
|
| exit 198
|
| }
|
|
|
|
|
|
|
| ivreg2_fvstrip `allvars'
|
| local sallvars `r(varlist)'
|
|
|
|
|
|
|
|
|
| foreach vl in endo inexog exexog partial orthog endogtest redundant {
|
| fvexpand ``vl'' if `touse'
|
| ivreg2_fvstrip `r(varlist)'
|
| local stripped `r(varlist)'
|
| matchnames "`stripped'" "`sallvars'" "`allvars'"
|
| local `vl' `r(names)'
|
| }
|
|
|
|
|
|
|
|
|
|
|
| local lhs0 `lhs'
|
| local dupsen1 : list dups endo
|
| local dupsin1 : list dups inexog
|
| local dupsex1 : list dups exexog
|
| foreach vl in endo inexog exexog partial orthog endogtest redundant {
|
| local `vl'0 : list uniq `vl'
|
| }
|
|
|
| local dupsen2 : list endo0 & inexog0
|
| local endo0 : list endo0 - inexog0
|
|
|
| local dupsex2 : list exexog0 & inexog0
|
| local exexog0 : list exexog0 - inexog0
|
|
|
| local dupsex3 : list exexog0 & endo0
|
| local exexog0 : list exexog0 - endo0
|
| local dups "`dupsen1' `dupsex1' `dupsin1' `dupsen2' `dupsex2' `dupsex3'"
|
| local dups : list uniq dups
|
|
|
|
|
|
|
|
|
|
|
| local endo0_ct : word count `endo0'
|
| local inexog0_ct : word count `inexog0'
|
| local rhs0_ct : word count `inexog0' `exexog0'
|
| local exexog0_ct : word count `exexog'
|
|
|
| if "`nocollin'" == "" {
|
|
|
|
|
| tempvar normwt
|
| qui gen double `normwt' = `wf' * `wvar' if `touse'
|
|
|
|
|
| if `endo0_ct'==0 {
|
|
|
|
|
|
|
| qui ivreg2_rmcollright2 `inexog0' `exexog0' if `touse',
|
| normwt(`normwt') `noconstant' noexpand newonly
|
|
|
|
|
| if `r(k_omitted)' {
|
| local collin `collin' `r(omitted)'
|
| local inexog0 ""
|
| local exexog0 ""
|
| local nvarlist `r(varlist)'
|
| local i 1
|
| while `i' <= `rhs0_ct' {
|
| local nvar : word `i' of `nvarlist'
|
| if `i' <= `inexog0_ct' {
|
| local inexog0 `inexog0' `nvar'
|
| }
|
| else {
|
| local exexog0 `exexog0' `nvar'
|
| }
|
| local ++i
|
| }
|
| local inexog0 : list retokenize inexog0
|
| local exexog0 : list retokenize exexog0
|
| }
|
| }
|
|
|
| else {
|
|
|
|
|
| qui ivreg2_rmcollright2 `endo0' if `touse',
|
| normwt(`normwt') `noconstant' noexpand newonly
|
|
|
|
|
| if `r(k_omitted)' {
|
| local collin `collin' `r(omitted)'
|
| local endo0 `r(varlist)'
|
| }
|
|
|
|
|
|
|
|
|
| qui ivreg2_rmcollright2 `inexog0' `exexog0' `endo0' if `touse',
|
| normwt(`normwt') `noconstant' noexpand newonly
|
| if `r(k_omitted)' {
|
|
|
|
|
|
|
| local ecollin `r(omitted)'
|
| local ecollin : list ecollin - inexog0
|
| local ecollin : list ecollin - exexog0
|
| if wordcount("`ecollin'") {
|
|
|
| local endo0 : list endo0 - ecollin
|
| local inexog0 `ecollin' `inexog0'
|
| local inexog0 : list retokenize inexog0
|
| local endo0_ct : word count `endo0'
|
| local inexog0_ct : word count `inexog0'
|
| local rhs0_ct : word count `inexog0' `exexog0'
|
|
|
|
|
| qui ivreg2_rmcollright2 `inexog0' `exexog0' `endo0' if `touse',
|
| normwt(`normwt') `noconstant' noexpand newonly
|
| }
|
|
|
| local collin `collin' `r(omitted)'
|
| local inexog0 ""
|
| local exexog0 ""
|
| local nvarlist `r(varlist)'
|
| local i 1
|
| while `i' <= `rhs0_ct' {
|
| local nvar : word `i' of `nvarlist'
|
| if `i' <= `inexog0_ct' {
|
| local inexog0 `inexog0' `nvar'
|
| }
|
| else {
|
| local exexog0 `exexog0' `nvar'
|
| }
|
| local ++i
|
| }
|
| local inexog0 : list retokenize inexog0
|
| local exexog0 : list retokenize exexog0
|
| }
|
| }
|
|
|
|
|
| if "`dups'" != "" {
|
| di in gr "Warning - duplicate variables detected"
|
| di in gr "Duplicates:" _c
|
| Disp `dups', _col(21)
|
| }
|
| if "`ecollin'" != "" {
|
| di in gr "Warning - endogenous variable(s) collinear with instruments"
|
| di in gr "Vars now exogenous:" _c
|
| Disp `ecollin', _col(21)
|
| }
|
| if "`collin'" != "" {
|
| di in gr "Warning - collinearities detected"
|
| di in gr "Vars dropped:" _c
|
| Disp `collin', _col(21)
|
| }
|
| }
|
|
|
|
|
| if wordcount("`partial0'") {
|
| ivreg2_fvstrip `inexog0' if `touse'
|
| local sinexog0 `r(varlist)'
|
| ivreg2_fvstrip `partial0' if `touse'
|
| local spartial0 `r(varlist)'
|
| matchnames "`spartial0'" "`sinexog0'" "`inexog0'"
|
| local partial0 `r(names)'
|
| local partialcheck : list partial0 - inexog0
|
| if ("`partialcheck'"~="") {
|
| di in r "Error: `partialcheck' listed in partial() but not in list of regressors."
|
| error 198
|
| }
|
| }
|
|
|
|
|
| foreach vl in lhs endo inexog exexog partial orthog endogtest redundant {
|
| sreturn local `vl' ``vl''
|
| sreturn local `vl'0 ``vl'0'
|
| }
|
| sreturn local dups `dups'
|
| sreturn local collin `collin'
|
| sreturn local ecollin `ecollin'
|
|
|
| end
|
|
|
|
|
|
|
| program define CheckMisc, rclass
|
| version 11.2
|
| syntax
|
| [ ,
|
| rhs1_ct(integer 0)
|
| iv1_ct(integer 0)
|
| bvector(name)
|
| smatrix(name)
|
| wmatrix(name)
|
| cnb1(string)
|
| cnZ1(string)
|
| ]
|
|
|
|
|
| if `rhs1_ct' == 0 {
|
| di as err "error: no regressors specified"
|
| exit 102
|
| }
|
| if `rhs1_ct' > `iv1_ct' {
|
| di as err "equation not identified; must have at least as many instruments"
|
| di as err "not in the regression as there are instrumented variables"
|
| exit 481
|
| }
|
|
|
|
|
| if "`bvector'" != "" {
|
| tempname b0
|
|
|
| cap matsort `bvector' "`cnb1'"
|
| matrix `b0'=r(sorted)
|
| local scols = colsof(`b0')
|
| local bcols : word count `cnb1'
|
| if _rc ~= 0 | (`scols'~=`bcols') {
|
| di as err "-b0- option error: supplied b0 columns do not match regressor list"
|
| exit 198
|
| }
|
| return mat b0 = `b0'
|
| }
|
|
|
|
|
| if "`smatrix'" != "" {
|
| tempname S0
|
|
|
| cap mat S0 = `smatrix'
|
| if _rc ~= 0 {
|
| di as err "invalid matrix `smatrix' in smatrix option"
|
| exit _rc
|
| }
|
|
|
| cap matsort `smatrix' "`cnZ1'"
|
| matrix `S0'=r(sorted)
|
| local srows = rowsof(`S0')
|
| local scols = colsof(`S0')
|
| local zcols : word count `cnZ1'
|
| if _rc ~= 0 | (`srows'~=`zcols') | (`scols'~=`zcols') {
|
| di as err "-smatrix- option error: supplied matrix columns/rows do not match IV list"
|
| exit 198
|
| }
|
| if issymmetric(`S0')==0 {
|
| di as err "-smatrix- option error: supplied matrix is not symmetric"
|
| exit 198
|
| }
|
| return mat S0 = `S0'
|
| }
|
|
|
|
|
| if "`wmatrix'" != "" {
|
| tempname W0
|
|
|
| cap mat W0 = `wmatrix'
|
| if _rc ~= 0 {
|
| di as err "invalid matrix `wmatrix' in wmatrix option"
|
| exit _rc
|
| }
|
|
|
| cap matsort `wmatrix' "`cnZ1'"
|
| matrix `W0'=r(sorted)
|
| local srows = rowsof(`W0')
|
| local scols = colsof(`W0')
|
| local zcols : word count `cnZ1'
|
| if _rc ~= 0 | (`srows'~=`zcols') | (`scols'~=`zcols') {
|
| di as err "-wmatrix- option error: supplied matrix columns/rows do not match IV list"
|
| exit 198
|
| }
|
| if issymmetric(`W0')==0 {
|
| di as err "-smatrix- option error: supplied matrix is not symmetric"
|
| exit 198
|
| }
|
| return mat W0 = `W0'
|
| }
|
| end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| program define ivreg2_fvstrip, rclass
|
| version 11.2
|
| syntax [anything] [if] , [ dropomit expand onebyone NOIsily ]
|
| if "`expand'"~="" {
|
| if "`onebyone'"=="" {
|
| fvexpand `anything' `if'
|
| local anything `r(varlist)'
|
| }
|
| else {
|
| foreach vn of local anything {
|
| fvexpand `vn' `if'
|
| local newlist `newlist' `r(varlist)'
|
| }
|
| local anything : list clean newlist
|
| }
|
| }
|
| foreach vn of local anything {
|
| if "`dropomit'"~="" {
|
| _ms_parse_parts `vn'
|
| if ~`r(omit)' {
|
| local unstripped `unstripped' `vn'
|
| }
|
| }
|
| else {
|
| local unstripped `unstripped' `vn'
|
| }
|
| }
|
|
|
| foreach vn of local unstripped {
|
| local svn ""
|
| _ms_parse_parts `vn'
|
| if "`r(type)'"=="variable" & "`r(op)'"=="" {
|
| local svn `vn'
|
| }
|
| else if "`r(type)'"=="variable" & "`r(op)'"=="o" {
|
| local svn `r(name)'
|
| }
|
| else if "`r(type)'"=="variable" {
|
| local op `r(op)'
|
| local op : subinstr local op "o" "", all
|
| local svn `op'.`r(name)'
|
| }
|
| else if "`r(type)'"=="factor" {
|
| local op `r(op)'
|
| local op : subinstr local op "b" "", all
|
| local op : subinstr local op "n" "", all
|
| local op : subinstr local op "o" "", all
|
| local svn `op'.`r(name)'
|
| }
|
| else if"`r(type)'"=="interaction" {
|
| forvalues i=1/`r(k_names)' {
|
| local op `r(op`i')'
|
| local op : subinstr local op "b" "", all
|
| local op : subinstr local op "n" "", all
|
| local op : subinstr local op "o" "", all
|
| local opv `op'.`r(name`i')'
|
| if `i'==1 {
|
| local svn `opv'
|
| }
|
| else {
|
| local svn `svn'#`opv'
|
| }
|
| }
|
| }
|
| else if "`r(type)'"=="product" {
|
| di as err "ivreg2_fvstrip error - type=product for `vn'"
|
| exit 198
|
| }
|
| else if "`r(type)'"=="error" {
|
| di as err "ivreg2_fvstrip error - type=error for `vn'"
|
| exit 198
|
| }
|
| else {
|
| di as err "ivreg2_fvstrip error - unknown type for `vn'"
|
| exit 198
|
| }
|
| local stripped `stripped' `svn'
|
| }
|
| local stripped : list retokenize stripped
|
|
|
| if "`noisily'"~="" {
|
| di as result "`stripped'"
|
| }
|
|
|
| return local varlist `stripped'
|
| end
|
|
|
|
|
|
|
| program define AddOmitted, rclass
|
| version 11.2
|
| syntax
|
| [ ,
|
| bmat(name)
|
| vmat(name)
|
| cnb0(string)
|
| cnb1(string)
|
| eqlist(string)
|
| ]
|
|
|
| tempname newb newV
|
| local eq_ct =max(1,wordcount("`eqlist'"))
|
| local rhs0_ct : word count `cnb0'
|
| local rhs1_ct : word count `cnb1'
|
|
|
| foreach vn in `cnb1' {
|
| local cnum : list posof "`vn'" in cnb0
|
| local cnumlist "`cnumlist' `cnum'"
|
| }
|
|
|
|
|
|
|
|
|
| mata: s_AddOmitted(
|
| "`bmat'",
|
| "`vmat'",
|
| "`cnumlist'",
|
| `eq_ct',
|
| `rhs0_ct',
|
| `rhs1_ct')
|
| mat `newb' = r(b)
|
| mat `newV' = r(V)
|
|
|
| if `eq_ct'==1 {
|
| local allnames `cnb0'
|
| }
|
| else {
|
| foreach eqname in `eqlist' {
|
| foreach vname in `cnb0' {
|
| local allnames "`allnames' `eqname':`vname'"
|
| }
|
| }
|
| }
|
| mat colnames `newb' = `allnames'
|
| mat rownames `newb' = y1
|
| mat colnames `newV' = `allnames'
|
| mat rownames `newV' = `allnames'
|
|
|
| return matrix b =`newb'
|
| return matrix V =`newV'
|
| end
|
|
|
|
|
|
|
| program define matsort, rclass
|
| version 11.2
|
| args bvmat names
|
| tempname m1 m2
|
| foreach vn in `names' {
|
| mat `m1'=nullmat(`m1'), `bvmat'[1...,"`vn'"]
|
| }
|
| if rowsof(`m1')>1 {
|
| foreach vn in `names' {
|
| mat `m2'=nullmat(`m2') \ `m1'["`vn'",1...]
|
| }
|
| return matrix sorted =`m2'
|
| }
|
| else {
|
| return matrix sorted =`m1'
|
| }
|
| end
|
|
|
|
|
| program define matchnames, rclass
|
| version 11.2
|
| args varnames namelist1 namelist2
|
|
|
| local k1 : word count `namelist1'
|
| local k2 : word count `namelist2'
|
|
|
| if `k1' ~= `k2' {
|
| di as err "namelist error"
|
| exit 198
|
| }
|
| foreach vn in `varnames' {
|
| local i : list posof `"`vn'"' in namelist1
|
| if `i' > 0 {
|
| local newname : word `i' of `namelist2'
|
| }
|
| else {
|
|
|
| local newname "`vn'"
|
| }
|
| local names "`names' `newname'"
|
| }
|
| local names : list clean names
|
| return local names "`names'"
|
| end
|
|
|
|
|
| program define checkversion_ranktest, rclass
|
| version 11.2
|
| args caller
|
|
|
|
|
| capture ranktest, version
|
| if _rc != 0 {
|
| di as err "Error: must have ranktest version 01.3.02 or greater installed"
|
| di as err "To install, from within Stata type " _c
|
| di in smcl "{stata ssc install ranktest :ssc install ranktest}"
|
| exit 601
|
| }
|
| local vernum "`r(version)'"
|
| if ("`vernum'" < "01.3.02") | ("`vernum'" > "09.9.99") {
|
| di as err "Error: must have ranktest version 01.3.02 or greater installed"
|
| di as err "Currently installed version is `vernum'"
|
| di as err "To update, from within Stata type " _c
|
| di in smcl "{stata ssc install ranktest, replace :ssc install ranktest, replace}"
|
| exit 601
|
| }
|
|
|
|
|
|
|
| if `caller' >= 16 {
|
| return local ranktestcmd version `caller': ranktest
|
| }
|
| else {
|
| return local ranktestcmd version 11.2: ranktest
|
| }
|
| end
|
|
|
|
|
|
|
| program define ivreg2_rmcollright2, rclass
|
| version 11.2
|
| syntax [ anything ]
|
| [if] [in]
|
| [,
|
| NORMWT(varname)
|
| NOCONStant
|
| NOEXPAND
|
| newonly
|
| lindep
|
| ]
|
|
|
|
|
| if "`anything'"=="" {
|
| return scalar k_omitted =0
|
| exit
|
| }
|
|
|
| marksample touse
|
| markout `touse' `anything'
|
|
|
| local cons = ("`noconstant'"=="")
|
| local expand = ("`noexpand'"=="")
|
| local newonly = ("`newonly'"~="")
|
| local forcedrop = ("`forcedrop'"~="")
|
| local lindep = ("`lindep'"~="")
|
| local 0 `anything'
|
| sreturn clear
|
| syntax varlist(ts fv)
|
| local tsops = ("`s(tsops)'"=="true")
|
| local fvops = ("`s(fvops)'"=="true")
|
|
|
| if `tsops' | `fvops' {
|
| if `expand' {
|
| fvexpand `anything' if `touse'
|
| local anything `r(varlist)'
|
| fvrevar `anything' if `touse'
|
| local fv_anything `r(varlist)'
|
| }
|
| else {
|
|
|
|
|
| foreach var in `anything' {
|
| fvrevar `var' if `touse'
|
| local fv_anything `fv_anything' `r(varlist)'
|
| }
|
| }
|
| }
|
| else {
|
| local fv_anything `anything'
|
| }
|
|
|
| tempname wname
|
| if "`normwt'"=="" {
|
| qui gen byte `wname'=1 if `touse'
|
| }
|
| else {
|
| qui gen double `wname' = `normwt' if `touse'
|
| }
|
|
|
| mata: s_rmcoll2("`fv_anything'", "`anything'", "`wname'", "`touse'", `cons', `lindep')
|
|
|
| foreach var in `r(omitted)' {
|
| di as text "note: `var' omitted because of collinearity"
|
| }
|
|
|
| local omitted "`r(omitted)'"
|
| local k_omitted =r(k_omitted)
|
| if `lindep' {
|
| tempname lindepmat
|
| mat `lindepmat' = r(lindep)
|
| mat rownames `lindepmat' = `anything'
|
| mat colnames `lindepmat' = `anything'
|
| }
|
|
|
|
|
| if `k_omitted' {
|
| foreach var in `omitted' {
|
| _ms_parse_parts `var'
|
| if r(omit) {
|
| local alreadyomitted `alreadyomitted' `var'
|
| }
|
| else {
|
| ivreg2_rmc2_ms_put_omit `var'
|
| local ovar `s(ospec)'
|
| local anything : subinstr local anything "`var'" "`ovar'", word
|
| }
|
| }
|
| if `newonly' {
|
| local omitted : list omitted - alreadyomitted
|
| local k_omitted : word count `omitted'
|
| }
|
| }
|
|
|
|
|
| return scalar k_omitted =`k_omitted'
|
| return local omitted `omitted'
|
| return local varlist `anything'
|
| if `lindep' {
|
| return mat lindep `lindepmat'
|
| }
|
|
|
| end
|
|
|
|
|
|
|
|
|
| program ivreg2_rmc2_ms_put_omit, sclass
|
| version 11.2
|
| args vn
|
| _ms_parse_parts `vn'
|
| if r(type) =="variable" {
|
| local name `r(name)'
|
| local ovar o.`name'
|
| }
|
| if r(type) == "factor" {
|
| if !r(base) {
|
| local name `r(name)'
|
| if "`r(ts_op)'" != "" {
|
| local name `r(ts_op)'.`name'
|
| }
|
| local ovar `r(level)'o.`name'
|
| }
|
| else {
|
| local ovar `vn'
|
| }
|
| }
|
| else if r(type) == "interaction" {
|
| local k = r(k_names)
|
|
|
| forval i = 1/`k' {
|
| local name = r(name`i')
|
| if "`r(ts_op`i')'" != "" {
|
| local name `r(ts_op`i')'.`name'
|
| }
|
| if "`r(level`i')'" != "" {
|
| if r(base`i') {
|
| local name `r(level`i')'b.`name'
|
| }
|
| else {
|
| local name `r(level`i')'o.`name'
|
| }
|
| }
|
| else {
|
| local name o.`name'
|
| }
|
| local spec `spec'`sharp'`name'
|
| local sharp "#"
|
| }
|
| local ovar `spec'
|
|
|
| }
|
| _msparse `ovar'
|
| sreturn local ospec `r(stripe)'
|
| end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| prog def abw, rclass
|
| version 11.2
|
| syntax varlist(ts), [ tindex(varname) nobs(integer 0) tobs(integer 0) NOConstant Kernel(string)]
|
|
|
| if "`kernel'" == "" {
|
| local kernel = "Bartlett"
|
| }
|
|
|
| if !inlist("`kernel'", "Bartlett", "Parzen", "Quadratic Spectral") {
|
| di as err "Error: kernel `kernel' not compatible with bw(auto)"
|
| return scalar abw = 1
|
| return local bwchoice "Kernel `kernel' not compatible with bw(auto); bw=1 (default)"
|
| exit
|
| }
|
| else {
|
|
|
| local cons 1
|
| if "`noconstant'" != "" {
|
| local cons 0
|
| }
|
|
|
| tsrevar `varlist'
|
| local varlist1 `r(varlist)'
|
| mata: s_abw("`varlist1'", "`tindex'", `nobs', `tobs', `cons', "`kernel'")
|
| return scalar abw = `abw'
|
| return local bwchoice "Automatic bw selection according to Newey-West (1994)"
|
| }
|
| end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| capture version 11.2
|
|
|
| mata:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| void s_abw (string scalar Zulist,
|
| string scalar tindexname,
|
| real scalar nobs,
|
| real scalar tobs,
|
| real scalar cons,
|
| string scalar kernel
|
| )
|
| {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| string rowvector Zunames, tov
|
| string scalar v, v2
|
| real matrix uZ
|
| real rowvector h
|
| real scalar lenzu, abw
|
|
|
|
|
| Zunames = tokens(Zulist)
|
| lenzu=cols(Zunames)-1
|
| v = Zunames[|1\lenzu|]
|
| v2 = Zunames[lenzu+1]
|
| st_view(uZ,.,v,v2)
|
| tnow=st_data(., tindexname)
|
|
|
|
|
|
|
| if (cons) {
|
| nrows1=cols(uZ)-2
|
| nrows2=1
|
| }
|
| else {
|
| nrows1=cols(uZ)-1
|
| nrows2=0
|
| }
|
|
|
|
|
| h = J(nrows1,1,1) \ J(nrows2,1,0)
|
|
|
|
|
|
|
|
|
|
|
|
|
| expo = 2/9
|
| q = 1
|
|
|
| cgamma = 1.1447
|
| if(kernel == "Parzen") {
|
| expo = 4/25
|
| q = 2
|
| cgamma = 2.6614
|
| }
|
|
|
| if(kernel == "Quadratic Spectral") {
|
| expo = 2/25
|
| q = 2
|
| cgamma = 1.3221
|
| }
|
|
|
|
|
|
|
|
|
| mstar = trunc(20 *(tobs/100)^expo)
|
|
|
|
|
| u = uZ[.,1]
|
| Z = uZ[|1,2 \.,.|]
|
|
|
|
|
| f = (u :* Z) * h
|
|
|
|
|
| sigmahat = J(mstar+1,1,0)
|
| for(j=0;j<=mstar;j++) {
|
| lsj = "L"+strofreal(j)
|
| tlag=st_data(., lsj+"."+tindexname)
|
| tmatrix = tnow, tlag
|
| svar=(tnow:<.):*(tlag:<.)
|
| tmatrix=select(tmatrix,svar)
|
|
|
| sigmahat[j+1] = quadcross(f[tmatrix[.,1],.], f[tmatrix[.,2],.]) / nobs
|
| }
|
|
|
|
|
| shatq = 0
|
| shat0 = sigmahat[1]
|
| for(j=1;j<=mstar;j++) {
|
| shatq = shatq + 2 * sigmahat[j+1] * j^q
|
| shat0 = shat0 + 2 * sigmahat[j+1]
|
| }
|
|
|
|
|
| expon = 1/(2*q+1)
|
| gammahat = cgamma*( (shatq/shat0)^2 )^expon
|
|
|
| m = gammahat * tobs^expon
|
|
|
|
|
| if(kernel == "Bartlett" | kernel == "Parzen") {
|
| optlag = min((trunc(m),mstar))
|
| }
|
| else if(kernel == "Quadratic Spectral") {
|
| optlag = min((m,mstar))
|
| }
|
|
|
|
|
|
|
| abw = optlag + 1
|
| st_local("abw",strofreal(abw))
|
| }
|
|
|
|
|
|
|
|
|
| void s_rmcoll2( string scalar fv_vnames,
|
| string scalar vnames,
|
| string scalar wname,
|
| string scalar touse,
|
| scalar cons,
|
| scalar lindep)
|
| {
|
| st_view(X=., ., tokens(fv_vnames), touse)
|
| st_view(w=., ., tokens(wname), touse)
|
| st_view(mtouse=., ., tokens(touse), touse)
|
|
|
| if (cons) {
|
| Xmean=mean(X,w)
|
| XX=quadcrossdev(X,Xmean, w, X,Xmean)
|
| }
|
| else {
|
| XX=quadcross(X, w, X)
|
| }
|
|
|
| XXinv=invsym(XX, range(1,cols(X),1))
|
|
|
| st_numscalar("r(k_omitted)", diag0cnt(XXinv))
|
| if (lindep) {
|
| st_matrix("r(lindep)", XX*XXinv)
|
| }
|
| smat = (diagonal(XXinv) :== 0)'
|
| vl=tokens(vnames)
|
| vl_drop = select(vl, smat)
|
| vl_keep = select(vl, (1 :- smat))
|
|
|
| if (cols(vl_keep)>0) {
|
| st_global("r(varlist)", invtokens(vl_keep))
|
| }
|
| if (cols(vl_drop)>0) {
|
| st_global("r(omitted)", invtokens(vl_drop))
|
| }
|
| }
|
|
|
|
|
|
|
|
|
| void s_AddOmitted( string scalar bname,
|
| string scalar vname,
|
| string scalar cnumlist,
|
| scalar eq_ct,
|
| scalar rhs0_ct,
|
| scalar rhs1_ct)
|
|
|
| {
|
| b = st_matrix(bname)
|
| V = st_matrix(vname)
|
| cn = strtoreal(tokens(cnumlist))
|
|
|
|
|
|
|
| col_ct = eq_ct * rhs0_ct
|
|
|
| newb = J(1,col_ct,0)
|
| newV = J(col_ct,col_ct,0)
|
|
|
|
|
|
|
|
|
| cn = (J(1,eq_ct,1) # cn) + ((range(0,eq_ct-1,1)' # J(1,rhs1_ct,1) ) * rhs0_ct)
|
|
|
|
|
| newb[1, cn] = b
|
| newV[cn, cn] = V
|
|
|
| st_matrix("r(b)", newb)
|
| st_matrix("r(V)", newV)
|
|
|
| }
|
|
|
|
|
|
|
|
|
| void s_partial( string scalar yname,
|
| string scalar X1names,
|
| string scalar X2names,
|
| string scalar Z1names,
|
| string scalar Pnames,
|
| string scalar touse,
|
| string scalar weight,
|
| string scalar wvarname,
|
| scalar wf,
|
| scalar N,
|
| scalar cons)
|
|
|
| {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| ytoken=tokens(yname)
|
| X1tokens=tokens(X1names)
|
| X2tokens=tokens(X2names)
|
| Z1tokens=tokens(Z1names)
|
| Ptokens=tokens(Pnames)
|
| Ytokens = (ytoken, X1tokens, X2tokens, Z1tokens)
|
|
|
| st_view(wvar, ., st_tsrevar(wvarname), touse)
|
| st_view(Y, ., Ytokens, touse)
|
| st_view(P, ., Ptokens, touse)
|
| L = cols(P)
|
|
|
| if (cons & L>0) {
|
| Ymeans = mean(Y,wf*wvar)
|
| Pmeans = mean(P,wf*wvar)
|
| PY = quadcrossdev(P, Pmeans, wf*wvar, Y, Ymeans)
|
| PP = quadcrossdev(P, Pmeans, wf*wvar, P, Pmeans)
|
| }
|
| else if (!cons & L>0) {
|
| PY = quadcross(P, wf*wvar, Y)
|
| PP = quadcross(P, wf*wvar, P)
|
| }
|
| else {
|
| Ymeans = mean(Y,wf*wvar)
|
| }
|
|
|
|
|
|
|
| if (L>0) {
|
| b = cholqrsolve(PP, PY)
|
| }
|
|
|
| if (cons & L>0) {
|
| Y[.,.] = (Y :- Ymeans) - (P :- Pmeans)*b
|
| }
|
| else if (!cons & L>0) {
|
| Y[.,.] = Y - P*b
|
| }
|
| else {
|
| Y[.,.] = (Y :- Ymeans)
|
| }
|
|
|
| }
|
|
|
|
|
|
|
|
|
|
|
| void s_crossprods( string scalar yname,
|
| string scalar X1names,
|
| string scalar X2names,
|
| string scalar Z1names,
|
| string scalar touse,
|
| string scalar weight,
|
| string scalar wvarname,
|
| scalar wf,
|
| scalar N)
|
|
|
| {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| ytoken=tokens(yname)
|
| X1tokens=tokens(X1names)
|
| X2tokens=tokens(X2names)
|
| Z1tokens=tokens(Z1names)
|
|
|
| Xtokens = (X1tokens, X2tokens)
|
| Ztokens = (Z1tokens, X2tokens)
|
|
|
| K1=cols(X1tokens)
|
| K2=cols(X2tokens)
|
| K=K1+K2
|
| L1=cols(Z1tokens)
|
| L2=cols(X2tokens)
|
| L=L1+L2
|
|
|
| st_view(wvar, ., st_tsrevar(wvarname), touse)
|
| st_view(A, ., st_tsrevar((ytoken, Xtokens, Z1tokens)), touse)
|
|
|
| AA = quadcross(A, wf*wvar, A)
|
|
|
| if (K>0) {
|
| XX = AA[(2::K+1),(2..K+1)]
|
| Xy = AA[(2::K+1),1]
|
| }
|
| if (K1>0) {
|
| X1X1 = AA[(2::K1+1),(2..K1+1)]
|
| }
|
|
|
| if (L1 > 0) {
|
| Z1Z1 = AA[(K+2::rows(AA)),(K+2..rows(AA))]
|
| }
|
|
|
| if (L2 > 0) {
|
| Z2Z2 = AA[(K1+2::K+1), (K1+2::K+1)]
|
| Z2y = AA[(K1+2::K+1), 1]
|
| }
|
|
|
| if ((L1>0) & (L2>0)) {
|
| Z2Z1 = AA[(K1+2::K+1), (K+2::rows(AA))]
|
| ZZ2 = Z2Z1, Z2Z2
|
| ZZ1 = Z1Z1, Z2Z1'
|
| ZZ = ZZ1 \ ZZ2
|
| }
|
| else if (L1>0) {
|
| ZZ = Z1Z1
|
| }
|
| else {
|
|
|
| ZZ = Z2Z2
|
| ZZ2 = Z2Z2
|
| }
|
|
|
| if ((K1>0) & (L1>0)) {
|
| X1Z1 = AA[(2::K1+1), (K+2::rows(AA))]
|
| }
|
|
|
| if ((K1>0) & (L2>0)) {
|
| X1Z2 = AA[(2::K1+1), (K1+2::K+1)]
|
| if (L1>0) {
|
| X1Z = X1Z1, X1Z2
|
| XZ = X1Z \ ZZ2
|
| }
|
| else {
|
| XZ = X1Z2 \ ZZ2
|
| X1Z = X1Z2
|
| }
|
| }
|
| else if (K1>0) {
|
| XZ = X1Z1
|
| X1Z= X1Z1
|
| }
|
| else if (L1>0) {
|
| XZ = AA[(2::K+1),(K+2..rows(AA))], AA[(2::K+1),(2..K+1)]
|
| }
|
| else {
|
| XZ = ZZ
|
| }
|
|
|
| if ((L1>0) & (L2>0)) {
|
| Zy = AA[(K+2::rows(AA)), 1] \ AA[(K1+2::K+1), 1]
|
| ZY = AA[(K+2::rows(AA)), (1..K1+1)] \ AA[(K1+2::K+1), (1..K1+1)]
|
| Z2Y = AA[(K1+2::K+1), (1..K1+1)]
|
| }
|
| else if (L1>0) {
|
| Zy = AA[(K+2::rows(AA)), 1]
|
| ZY = AA[(K+2::rows(AA)), (1..K1+1)]
|
| }
|
| else if (L2>0) {
|
| Zy = AA[(K1+2::K+1), 1]
|
| ZY = AA[(K1+2::K+1), (1..K1+1)]
|
| Z2Y = ZY
|
| }
|
|
|
|
|
| YY = AA[(1::K1+1), (1..K1+1)]
|
| yy = AA[1,1]
|
| st_subview(y, A, ., 1)
|
| ym = sum(wf*wvar:*y)/N
|
| yyc = quadcrossdev(y, ym, wf*wvar, y, ym)
|
|
|
| XXinv = invsym(XX)
|
| if (Xtokens==Ztokens) {
|
| ZZinv = XXinv
|
| XPZXinv = XXinv
|
| }
|
| else {
|
| ZZinv = invsym(ZZ)
|
| XPZX = makesymmetric(XZ*ZZinv*XZ')
|
| XPZXinv=invsym(XPZX)
|
| }
|
|
|
|
|
| condxx=cond(XX)
|
| condzz=cond(ZZ)
|
|
|
| st_matrix("r(XX)", XX)
|
| st_matrix("r(X1X1)", X1X1)
|
| st_matrix("r(X1Z)", X1Z)
|
| st_matrix("r(ZZ)", ZZ)
|
| st_matrix("r(Z2Z2)", Z2Z2)
|
| st_matrix("r(Z1Z2)", Z2Z1')
|
| st_matrix("r(Z2y)",Z2y)
|
| st_matrix("r(XZ)", XZ)
|
| st_matrix("r(Xy)", Xy)
|
| st_matrix("r(Zy)", Zy)
|
| st_numscalar("r(yy)", yy)
|
| st_numscalar("r(yyc)", yyc)
|
| st_matrix("r(YY)", YY)
|
| st_matrix("r(ZY)", ZY)
|
| st_matrix("r(Z2Y)", Z2Y)
|
| st_matrix("r(XXinv)", XXinv)
|
| st_matrix("r(ZZinv)", ZZinv)
|
| st_matrix("r(XPZXinv)", XPZXinv)
|
| st_numscalar("r(condxx)",condxx)
|
| st_numscalar("r(condzz)",condzz)
|
|
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| void s_gmm1s( string scalar ZZmatrix,
|
| string scalar XXmatrix,
|
| string scalar XZmatrix,
|
| string scalar Zymatrix,
|
| string scalar ZZinvmatrix,
|
| string scalar yname,
|
| string scalar ename,
|
| string scalar Xnames,
|
| string scalar Znames,
|
| string scalar touse,
|
| string scalar weight,
|
| string scalar wvarname,
|
| scalar wf,
|
| scalar N,
|
| string scalar Wmatrix,
|
| string scalar Smatrix,
|
| scalar dofminus,
|
| scalar efficient,
|
| scalar overid,
|
| scalar useqr)
|
| {
|
|
|
| Ztokens=tokens(Znames)
|
| Xtokens=tokens(Xnames)
|
|
|
| st_view(Z, ., st_tsrevar(Ztokens), touse)
|
| st_view(X, ., st_tsrevar(Xtokens), touse)
|
| st_view(y, ., st_tsrevar(yname), touse)
|
| st_view(e, ., ename, touse)
|
| st_view(wvar, ., st_tsrevar(wvarname), touse)
|
|
|
|
|
|
|
|
|
|
|
| ZZ = st_matrix(ZZmatrix)
|
| XX = st_matrix(XXmatrix)
|
| XZ = st_matrix(XZmatrix)
|
| Zy = st_matrix(Zymatrix)
|
| ZZinv = st_matrix(ZZinvmatrix)
|
|
|
| QZZ = ZZ / N
|
| QXX = XX / N
|
| QXZ = XZ / N
|
| QZy = Zy / N
|
| QZZinv = ZZinv*N
|
|
|
| useqr = (diag0cnt(QZZinv)>0) | useqr
|
|
|
|
|
| if (Wmatrix~="") {
|
| W = st_matrix(Wmatrix)
|
| useqr = (diag0cnt(W)>0) | useqr
|
| }
|
|
|
| if (Smatrix~="") {
|
| omega=st_matrix(Smatrix)
|
| useqr = (diag0cnt(omega)>0) | useqr
|
| }
|
|
|
| if (efficient) {
|
| if ((Xtokens==Ztokens) & (Smatrix=="")) {
|
|
|
| beta = cholqrsolve(QZZ, QZy, useqr)
|
| beta = beta'
|
| e[.,.] = y - X * beta'
|
| ee = quadcross(e, wf*wvar, e)
|
| sigmasq=ee/(N-dofminus)
|
| omega = sigmasq * QZZ
|
| W = 1/sigmasq * QZZinv
|
| V = 1/N * sigmasq * QZZinv
|
| rankS = rows(omega) - diag0cnt(QZZinv)
|
| rankV = rows(V) - diag0cnt(V)
|
| }
|
| else if (Smatrix=="") {
|
| aux1 = cholqrsolve(QZZ, QXZ', useqr)
|
| aux2 = cholqrsolve(QZZ, QZy, useqr)
|
| aux3 = makesymmetric(QXZ * aux1)
|
| beta = cholqrsolve(aux3, QXZ * aux2, useqr)
|
| beta = beta'
|
| e[.,.] = y - X * beta'
|
| ee = quadcross(e, wf*wvar, e)
|
| sigmasq = ee/(N-dofminus)
|
| omega = sigmasq * QZZ
|
| W = 1/sigmasq * QZZinv
|
| V = 1/N * sigmasq * invsym(aux3)
|
| rankS = rows(omega) - diag0cnt(QZZinv)
|
| rankV = rows(V) - diag0cnt(V)
|
| }
|
| else {
|
| aux1 = cholqrsolve(omega, QXZ', useqr)
|
| aux2 = cholqrsolve(omega, QZy, useqr)
|
| aux3 = makesymmetric(QXZ * aux1)
|
| beta = cholqrsolve(aux3, QXZ * aux2, useqr)
|
| beta = beta'
|
| e[.,.] = y - X * beta'
|
| ee = quadcross(e, wf*wvar, e)
|
| sigmasq=ee/(N-dofminus)
|
| W = invsym(omega)
|
| V = 1/N * invsym(aux3)
|
| rankS = rows(omega) - diag0cnt(W)
|
| rankV = rows(V) - diag0cnt(V)
|
| }
|
| if (overid) {
|
| Ze = quadcross(Z, wf*wvar, e)
|
| gbar = Ze / N
|
| aux4 = cholqrsolve(omega, gbar, useqr)
|
| j = N * gbar' * aux4
|
| }
|
| else {
|
| j=0
|
| }
|
| st_matrix("r(beta)", beta)
|
| st_matrix("r(V)", V)
|
| st_matrix("r(S)", omega)
|
| st_matrix("r(W)", W)
|
| st_numscalar("r(rss)", ee)
|
| st_numscalar("r(j)", j)
|
| st_numscalar("r(sigmasq)", sigmasq)
|
| st_numscalar("r(rankS)", rankS)
|
| st_numscalar("r(rankV)", rankV)
|
| }
|
| else {
|
| if ((Xtokens==Ztokens) & (Wmatrix=="")) {
|
| beta = cholqrsolve(QZZ, QZy, useqr)
|
| beta = beta'
|
| e[.,.] = y - X * beta'
|
| ee = quadcross(e, wf*wvar, e)
|
| sigmasq=ee/(N-dofminus)
|
| W = 1/sigmasq * QZZinv
|
| QXZ_W_QZX = 1/sigmasq * QZZ
|
| }
|
| else if (Wmatrix=="") {
|
| aux1 = cholqrsolve(QZZ, QXZ', useqr)
|
| aux2 = cholqrsolve(QZZ, QZy, useqr)
|
| aux3 = makesymmetric(QXZ * aux1)
|
| beta = cholqrsolve(aux3, QXZ * aux2, useqr)
|
| beta = beta'
|
| e[.,.] = y - X * beta'
|
| ee = quadcross(e, wf*wvar, e)
|
| sigmasq=ee/(N-dofminus)
|
| W = 1/sigmasq * QZZinv
|
| QXZ_W_QZX = 1/sigmasq * aux3
|
| }
|
| else {
|
| QXZ_W_QZX = QXZ * W * QXZ'
|
| _makesymmetric(QXZ_W_QZX)
|
| beta = cholqrsolve(QXZ_W_QZX, QXZ * W * QZy, useqr)
|
| beta = beta'
|
| e[.,.] = y - X * beta'
|
| }
|
| st_matrix("r(QXZ_W_QZX)", QXZ_W_QZX)
|
| st_matrix("r(beta)", beta)
|
| st_matrix("r(W)",W)
|
| }
|
|
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| void s_egmm( string scalar ZZmatrix,
|
| string scalar XXmatrix,
|
| string scalar XZmatrix,
|
| string scalar Zymatrix,
|
| string scalar ZZinvmatrix,
|
| string scalar yname,
|
| string scalar ename,
|
| string scalar Xnames,
|
| string scalar Znames,
|
| string scalar touse,
|
| string scalar weight,
|
| string scalar wvarname,
|
| scalar wf,
|
| scalar N,
|
| string scalar Smatrix,
|
| scalar dofminus,
|
| scalar overid,
|
| scalar useqr)
|
| {
|
|
|
| Ztokens=tokens(Znames)
|
| Xtokens=tokens(Xnames)
|
|
|
| st_view(Z, ., st_tsrevar(Ztokens), touse)
|
| st_view(X, ., st_tsrevar(Xtokens), touse)
|
| st_view(y, ., st_tsrevar(yname), touse)
|
| st_view(e, ., ename, touse)
|
| st_view(wvar, ., st_tsrevar(wvarname), touse)
|
|
|
|
|
|
|
|
|
|
|
| ZZ = st_matrix(ZZmatrix)
|
| XX = st_matrix(XXmatrix)
|
| XZ = st_matrix(XZmatrix)
|
| Zy = st_matrix(Zymatrix)
|
| ZZinv = st_matrix(ZZinvmatrix)
|
|
|
| QZZ = ZZ / N
|
| QXX = XX / N
|
| QXZ = XZ / N
|
| QZy = Zy / N
|
| QZZinv = ZZinv*N
|
|
|
|
|
| omega=st_matrix(Smatrix)
|
| W = invsym(omega)
|
| rankS = rows(omega) - diag0cnt(W)
|
|
|
| if (rankS<rows(omega)) {
|
| errprintf("\nError: estimated covariance matrix of moment conditions not of full rank,")
|
| errprintf("\n and optimal GMM weighting matrix not unique.")
|
| errprintf("\nPossible causes:")
|
| errprintf("\n singleton dummy variable (dummy with one 1 and N-1 0s or vice versa)")
|
| errprintf("\n {help ivreg2##partial:partial} option may address problem.\n")
|
| exit(506)
|
| }
|
|
|
| aux1 = cholqrsolve(omega, QXZ', useqr)
|
| aux2 = cholqrsolve(omega, QZy, useqr)
|
| aux3 = makesymmetric(QXZ * aux1)
|
| beta = cholqrsolve(aux3, QXZ * aux2, useqr)
|
| beta = beta'
|
|
|
|
|
|
|
|
|
| V = 1/N * invsym(aux3)
|
| rankV = rows(V) - diag0cnt(V)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| e[.,.] = y - X * beta'
|
| ee = quadcross(e, wf*wvar, e)
|
| sigmasq=ee/(N-dofminus)
|
|
|
|
|
| if (cols(Z) > cols(X)) {
|
| Ze = quadcross(Z, wf*wvar, e)
|
| gbar = Ze / N
|
| aux4 = cholqrsolve(omega, gbar, useqr)
|
| j = N * gbar' * aux4
|
| }
|
| else {
|
| j=0
|
| }
|
|
|
| st_matrix("r(beta)", beta)
|
| st_matrix("r(V)", V)
|
| st_matrix("r(W)", W)
|
| st_numscalar("r(rss)", ee)
|
| st_numscalar("r(j)", j)
|
| st_numscalar("r(sigmasq)", sigmasq)
|
| st_numscalar("r(rankV)",rankV)
|
| st_numscalar("r(rankS)",rankS)
|
|
|
| }
|
|
|
|
|
|
|
| void s_iegmm( string scalar ZZmatrix,
|
| string scalar XXmatrix,
|
| string scalar XZmatrix,
|
| string scalar Zymatrix,
|
| string scalar QXZ_W_QZXmatrix,
|
| string scalar yname,
|
| string scalar ename,
|
| string scalar Xnames,
|
| string scalar Znames,
|
| string scalar touse,
|
| string scalar weight,
|
| string scalar wvarname,
|
| scalar wf,
|
| scalar N,
|
| string scalar Wmatrix,
|
| string scalar Smatrix,
|
| string scalar bname,
|
| scalar dofminus,
|
| scalar overid,
|
| scalar useqr)
|
| {
|
|
|
| Ztokens=tokens(Znames)
|
| Xtokens=tokens(Xnames)
|
|
|
| st_view(Z, ., st_tsrevar(Ztokens), touse)
|
| st_view(X, ., st_tsrevar(Xtokens), touse)
|
| st_view(y, ., st_tsrevar(yname), touse)
|
| st_view(e, ., ename, touse)
|
| st_view(wvar, ., st_tsrevar(wvarname), touse)
|
|
|
|
|
|
|
|
|
|
|
| QZZ = st_matrix(ZZmatrix) / N
|
| QXX = st_matrix(XXmatrix) / N
|
| QXZ = st_matrix(XZmatrix) / N
|
| QZy = st_matrix(Zymatrix) / N
|
| QXZ_W_QZX = st_matrix(QXZ_W_QZXmatrix)
|
|
|
| useqr = (diag0cnt(QXZ_W_QZX)>0) | useqr
|
|
|
|
|
| beta = st_matrix(bname)
|
|
|
|
|
| W = st_matrix(Wmatrix)
|
|
|
|
|
| omega=st_matrix(Smatrix)
|
|
|
|
|
| ee = quadcross(e, wf*wvar, e)
|
| sigmasq=ee/(N-dofminus)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| aux5 = cholqrsolve(QXZ_W_QZX, QXZ * W, useqr)
|
| V = 1/N * aux5 * omega * aux5'
|
| _makesymmetric(V)
|
|
|
|
|
|
|
|
|
|
|
| rankV = rows(V) - diag0cnt(invsym(V))
|
| rankS = rows(omega) - diag0cnt(invsym(omega))
|
|
|
|
|
| if (overid) {
|
|
|
|
|
|
|
|
|
|
|
| aux1 = cholqrsolve(omega, QXZ', useqr)
|
| aux2 = cholqrsolve(omega, QZy, useqr)
|
| aux3s = makesymmetric(QXZ * aux1)
|
| beta2s = cholqrsolve(aux3s, QXZ * aux2, useqr)
|
| beta2s = beta2s'
|
| e2s = y - X * beta2s'
|
| Ze2s = quadcross(Z, wf*wvar, e2s)
|
| gbar = Ze2s / N
|
| aux4 = cholqrsolve(omega, gbar, useqr)
|
| j = N * gbar' * aux4
|
| }
|
| else {
|
| j=0
|
| }
|
|
|
| st_matrix("r(V)", V)
|
| st_numscalar("r(j)", j)
|
| st_numscalar("r(rss)", ee)
|
| st_numscalar("r(sigmasq)", sigmasq)
|
| st_numscalar("r(rankV)",rankV)
|
| st_numscalar("r(rankS)",rankS)
|
|
|
| }
|
|
|
|
|
|
|
| void s_liml( string scalar ZZmatrix,
|
| string scalar XXmatrix,
|
| string scalar XZmatrix,
|
| string scalar Zymatrix,
|
| string scalar Z2Z2matrix,
|
| string scalar YYmatrix,
|
| string scalar ZYmatrix,
|
| string scalar Z2Ymatrix,
|
| string scalar Xymatrix,
|
| string scalar ZZinvmatrix,
|
| string scalar yname,
|
| string scalar Ynames,
|
| string scalar ename,
|
| string scalar Xnames,
|
| string scalar X1names,
|
| string scalar Znames,
|
| string scalar Z1names,
|
| string scalar Z2names,
|
| scalar fuller,
|
| scalar kclass,
|
| string scalar coviv,
|
| string scalar touse,
|
| string scalar weight,
|
| string scalar wvarname,
|
| scalar wf,
|
| scalar N,
|
| string scalar robust,
|
| string scalar clustvarname,
|
| string scalar clustvarname2,
|
| string scalar clustvarname3,
|
| scalar bw,
|
| string scalar kernel,
|
| string scalar sw,
|
| string scalar psd,
|
| string scalar ivarname,
|
| string scalar tvarname,
|
| string scalar tindexname,
|
| scalar tdelta,
|
| scalar center,
|
| scalar dofminus,
|
| scalar useqr)
|
|
|
| {
|
| struct ms_vcvorthog scalar vcvo
|
|
|
| vcvo.ename = ename
|
| vcvo.Znames = Znames
|
| vcvo.touse = touse
|
| vcvo.weight = weight
|
| vcvo.wvarname = wvarname
|
| vcvo.robust = robust
|
| vcvo.clustvarname = clustvarname
|
| vcvo.clustvarname2 = clustvarname2
|
| vcvo.clustvarname3 = clustvarname3
|
| vcvo.kernel = kernel
|
| vcvo.sw = sw
|
| vcvo.psd = psd
|
| vcvo.ivarname = ivarname
|
| vcvo.tvarname = tvarname
|
| vcvo.tindexname = tindexname
|
| vcvo.wf = wf
|
| vcvo.N = N
|
| vcvo.bw = bw
|
| vcvo.tdelta = tdelta
|
| vcvo.center = center
|
| vcvo.dofminus = dofminus
|
| vcvo.ZZ = st_matrix(ZZmatrix)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Ytokens=tokens(Ynames)
|
| Ztokens=tokens(Znames)
|
| Z1tokens=tokens(Z1names)
|
| Z2tokens=tokens(Z2names)
|
| Xtokens=tokens(Xnames)
|
| X1tokens=tokens(X1names)
|
|
|
| st_view(Z, ., st_tsrevar(Ztokens), touse)
|
| st_view(X, ., st_tsrevar(Xtokens), touse)
|
| st_view(y, ., st_tsrevar(yname), touse)
|
| st_view(e, ., ename, touse)
|
| st_view(wvar, ., st_tsrevar(wvarname), touse)
|
|
|
| vcvo.e = &e
|
| vcvo.Z = &Z
|
| vcvo.wvar = &wvar
|
|
|
|
|
|
|
|
|
|
|
| QZZ = st_matrix(ZZmatrix) / N
|
| QXX = st_matrix(XXmatrix) / N
|
| QXZ = st_matrix(XZmatrix) / N
|
| QZy = st_matrix(Zymatrix) / N
|
| QZ2Z2 = st_matrix(Z2Z2matrix) / N
|
| QYY = st_matrix(YYmatrix) / N
|
| QZY = st_matrix(ZYmatrix) / N
|
| QZ2Y = st_matrix(Z2Ymatrix) / N
|
| QXy = st_matrix(Xymatrix) / N
|
| QZZinv = st_matrix(ZZinvmatrix)*N
|
|
|
| useqr = (diag0cnt(QZZ)>0) | useqr
|
|
|
|
|
| if (kclass == 0) {
|
| aux1 = cholqrsolve(QZZ, QZY, useqr)
|
| QWW = QYY - QZY'*aux1
|
| _makesymmetric(QWW)
|
| if (cols(Z2tokens) > 0) {
|
| aux2 = cholqrsolve(QZ2Z2, QZ2Y, useqr)
|
| QWW1 = QYY - QZ2Y'*aux2
|
| _makesymmetric(QWW1)
|
| }
|
| else {
|
|
|
| QWW1 = QYY
|
| }
|
| M=matpowersym(QWW, -0.5)
|
| Eval=symeigenvalues(M*QWW1*M)
|
| lambda=rowmin(Eval)
|
| }
|
|
|
|
|
| if (cols(Z)==cols(X)) {
|
| lambda=1
|
| }
|
|
|
| if (fuller > (N-cols(Z))) {
|
| printf("\n{error:Error: invalid choice of Fuller LIML parameter.}\n")
|
| exit(error(3351))
|
| }
|
| else if (fuller > 0) {
|
| k = lambda - fuller/(N-cols(Z))
|
| }
|
| else if (kclass > 0) {
|
| k = kclass
|
| }
|
| else {
|
| k = lambda
|
| }
|
|
|
| aux3 = cholqrsolve(QZZ, QXZ', useqr)
|
| QXhXh=(1-k)*QXX + k*QXZ*aux3
|
| _makesymmetric(QXhXh)
|
| aux4 = cholqrsolve(QZZ, QZy, useqr)
|
| aux5 = cholqrsolve(QXhXh, QXZ, useqr)
|
| aux6 = cholqrsolve(QXhXh, QXy, useqr)
|
| beta = aux6*(1-k) + k*aux5*aux4
|
| beta = beta'
|
|
|
| e[.,.] = y - X * beta'
|
| ee = quadcross(e, wf*wvar, e)
|
| sigmasq = ee /(N-dofminus)
|
|
|
| omega = m_omega(vcvo)
|
|
|
| QXhXhinv=invsym(QXhXh)
|
|
|
| if ((robust=="") & (clustvarname=="") & (kernel=="")) {
|
|
|
| if (coviv=="") {
|
|
|
| V = 1/N * sigmasq * QXhXhinv
|
| rankV = rows(V) - diag0cnt(V)
|
| }
|
| else {
|
| aux7 = makesymmetric(QXZ * aux3)
|
| V = 1/N * sigmasq * invsym(aux7)
|
| rankV = rows(V) - diag0cnt(V)
|
| }
|
| rankS = rows(omega) - diag0cnt(invsym(omega))
|
| if (cols(Z)>cols(X)) {
|
| Ze = quadcross(Z, wf*wvar, e)
|
| gbar = Ze / N
|
| aux8 = cholqrsolve(omega, gbar, useqr)
|
| j = N * gbar' * aux8
|
| }
|
| else {
|
| j=0
|
| }
|
| }
|
| else {
|
|
|
| if (coviv=="") {
|
| aux9 = cholqrsolve(QZZ, aux5', useqr)
|
| V = 1/N * aux9' * omega * aux9
|
| _makesymmetric(V)
|
| rankV = rows(V) - diag0cnt(invsym(V))
|
| rankS = rows(omega) - diag0cnt(invsym(omega))
|
| }
|
| else {
|
| aux10 = QXZ * aux3
|
| _makesymmetric(aux10)
|
| aux11 = cholqrsolve(aux10, aux3', useqr)
|
| V = 1/N * aux11 * omega * aux11'
|
| _makesymmetric(V)
|
| rankV = rows(V) - diag0cnt(invsym(V))
|
| rankS = rows(omega) - diag0cnt(invsym(omega))
|
| }
|
| if (cols(Z)>cols(X)) {
|
| aux12 = cholqrsolve(omega, QXZ', useqr)
|
| aux13 = cholqrsolve(omega, QZy, useqr)
|
| aux14 = makesymmetric(QXZ * aux12)
|
| beta2s = cholqrsolve(aux14, QXZ * aux13, useqr)
|
| beta2s = beta2s'
|
| e2s = y - X * beta2s'
|
| Ze2s = quadcross(Z, wf*wvar, e2s)
|
| gbar = Ze2s / N
|
| aux15 = cholqrsolve(omega, gbar, useqr)
|
| j = N * gbar' * aux15
|
| }
|
| else {
|
| j=0
|
| }
|
| }
|
| _makesymmetric(V)
|
|
|
| st_matrix("r(beta)", beta)
|
| st_matrix("r(S)", omega)
|
| st_matrix("r(V)", V)
|
| st_numscalar("r(lambda)", lambda)
|
| st_numscalar("r(kclass)", k)
|
| st_numscalar("r(j)", j)
|
| st_numscalar("r(rss)", ee)
|
| st_numscalar("r(sigmasq)", sigmasq)
|
| st_numscalar("r(rankV)",rankV)
|
| st_numscalar("r(rankS)",rankS)
|
|
|
| }
|
|
|
|
|
|
|
|
|
| void s_gmmcue( string scalar ZZmatrix,
|
| string scalar XZmatrix,
|
| string scalar yname,
|
| string scalar ename,
|
| string scalar Xnames,
|
| string scalar Znames,
|
| string scalar touse,
|
| string scalar weight,
|
| string scalar wvarname,
|
| scalar wf,
|
| scalar N,
|
| string scalar robust,
|
| string scalar clustvarname,
|
| string scalar clustvarname2,
|
| string scalar clustvarname3,
|
| scalar bw,
|
| string scalar kernel,
|
| string scalar sw,
|
| string scalar psd,
|
| string scalar ivarname,
|
| string scalar tvarname,
|
| string scalar tindexname,
|
| scalar tdelta,
|
| string scalar bname,
|
| string scalar b0name,
|
| scalar center,
|
| scalar dofminus,
|
| scalar useqr)
|
|
|
| {
|
|
|
| struct ms_vcvorthog scalar vcvo
|
|
|
| vcvo.ename = ename
|
| vcvo.Znames = Znames
|
| vcvo.touse = touse
|
| vcvo.weight = weight
|
| vcvo.wvarname = wvarname
|
| vcvo.robust = robust
|
| vcvo.clustvarname = clustvarname
|
| vcvo.clustvarname2 = clustvarname2
|
| vcvo.clustvarname3 = clustvarname3
|
| vcvo.kernel = kernel
|
| vcvo.sw = sw
|
| vcvo.psd = psd
|
| vcvo.ivarname = ivarname
|
| vcvo.tvarname = tvarname
|
| vcvo.tindexname = tindexname
|
| vcvo.wf = wf
|
| vcvo.N = N
|
| vcvo.bw = bw
|
| vcvo.tdelta = tdelta
|
| vcvo.center = center
|
| vcvo.dofminus = dofminus
|
| vcvo.ZZ = st_matrix(ZZmatrix)
|
|
|
| Ztokens=tokens(Znames)
|
| Xtokens=tokens(Xnames)
|
|
|
| st_view(Z, ., st_tsrevar(Ztokens), touse)
|
| st_view(X, ., st_tsrevar(Xtokens), touse)
|
| st_view(y, ., st_tsrevar(yname), touse)
|
| st_view(e, ., ename, touse)
|
| st_view(wvar, ., st_tsrevar(wvarname), touse)
|
|
|
|
|
| vcvo.e = &e
|
| vcvo.Z = &Z
|
| vcvo.wvar = &wvar
|
| py = &y
|
| pX = &X
|
|
|
| if (b0name=="") {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| beta_init = st_matrix(bname)
|
|
|
|
|
|
|
|
|
|
|
|
|
| S = optimize_init()
|
|
|
| optimize_init_evaluator(S, &m_cuecrit())
|
| optimize_init_which(S, "min")
|
| optimize_init_evaluatortype(S, "d0")
|
| optimize_init_params(S, beta_init)
|
|
|
| optimize_init_argument(S, 1, py)
|
| optimize_init_argument(S, 2, pX)
|
| optimize_init_argument(S, 3, vcvo)
|
| optimize_init_argument(S, 4, useqr)
|
|
|
| beta = optimize(S)
|
|
|
|
|
| j = optimize_result_value(S)
|
|
|
|
|
| e[.,.] = y - X * beta'
|
| omega = m_omega(vcvo)
|
| }
|
| else {
|
|
|
| beta = st_matrix(b0name)
|
| e[.,.] = y - X * beta'
|
| omega = m_omega(vcvo)
|
|
|
| gbar = 1/N * quadcross(Z, wf*wvar, e)
|
| j = N * gbar' * cholsolve(omega, gbar, useqr)
|
|
|
| }
|
|
|
|
|
| QXZ = st_matrix(XZmatrix)/N
|
|
|
| ee = quadcross(e, wf*wvar, e)
|
| sigmasq=ee/(N-dofminus)
|
|
|
|
|
|
|
|
|
|
|
| aux1 = cholsolve(omega, QXZ')
|
| if (aux1[1,1]==.) {
|
| errprintf("\nError: estimated covariance matrix of moment conditions not of full rank,")
|
| errprintf("\n and optimal GMM weighting matrix not unique.")
|
| errprintf("\nPossible causes:")
|
| errprintf("\n collinearities in instruments (if -nocollin- option was used)")
|
| errprintf("\n singleton dummy variable (dummy with one 1 and N-1 0s or vice versa)")
|
| errprintf("\n {help ivreg2##partial:partial} option may address problem.\n")
|
| exit(506)
|
| }
|
| aux3 = makesymmetric(QXZ * aux1)
|
| V = 1/N * invsym(aux3)
|
| if (diag0cnt(V)) {
|
|
|
| errprintf("\nError: estimated variance matrix of b not of full rank, and CUE estimates")
|
| errprintf("\n unreliable; may be caused by collinearities\n")
|
| exit(506)
|
| }
|
| W = invsym(omega)
|
|
|
| st_matrix("r(beta)", beta)
|
| st_matrix("r(S)", omega)
|
| st_matrix("r(W)", W)
|
| st_matrix("r(V)", V)
|
| st_numscalar("r(j)", j)
|
| st_numscalar("r(rss)", ee)
|
| st_numscalar("r(sigmasq)", sigmasq)
|
|
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| void m_cuecrit(todo, beta, pointer py, pointer pX, struct ms_vcvorthog scalar vcvo, useqr, j, g, H)
|
| {
|
|
|
|
|
| omega = m_omega(vcvo)
|
|
|
|
|
| gbar = 1/vcvo.N * quadcross(*vcvo.Z, vcvo.wf*(*vcvo.wvar), *vcvo.e)
|
| aux1 = cholqrsolve(omega, gbar, useqr)
|
| j = vcvo.N * gbar' * aux1
|
|
|
|
|
|
|
|
|
|
|
| }
|
|
|
|
|
|
|
|
|
| void s_ffirst( string scalar ZZmatrix,
|
| string scalar XXmatrix,
|
| string scalar XZmatrix,
|
| string scalar ZYmatrix,
|
| string scalar ZZinvmatrix,
|
| string scalar XXinvmatrix,
|
| string scalar XPZXinvmatrix,
|
| string scalar X2X2matrix,
|
| string scalar Z1X2matrix,
|
| string scalar X2ymatrix,
|
| string scalar ename,
|
| string scalar ematnames,
|
| string scalar yname,
|
| string scalar X1names,
|
| string scalar X2names,
|
| string scalar Z1names,
|
| string scalar touse,
|
| string scalar weight,
|
| string scalar wvarname,
|
| scalar wf,
|
| scalar N,
|
| scalar N_clust,
|
| string scalar robust,
|
| string scalar clustvarname,
|
| string scalar clustvarname2,
|
| string scalar clustvarname3,
|
| scalar bw,
|
| string scalar kernel,
|
| string scalar sw,
|
| string scalar psd,
|
| string scalar ivarname,
|
| string scalar tvarname,
|
| string scalar tindexname,
|
| scalar tdelta,
|
| scalar center,
|
| scalar dofminus,
|
| scalar sdofminus)
|
|
|
| {
|
|
|
| struct ms_vcvorthog scalar vcvo
|
|
|
| vcvo.Znames = Znames
|
| vcvo.touse = touse
|
| vcvo.weight = weight
|
| vcvo.wvarname = wvarname
|
| vcvo.robust = robust
|
| vcvo.clustvarname = clustvarname
|
| vcvo.clustvarname2 = clustvarname2
|
| vcvo.clustvarname3 = clustvarname3
|
| vcvo.kernel = kernel
|
| vcvo.sw = sw
|
| vcvo.psd = psd
|
| vcvo.ivarname = ivarname
|
| vcvo.tvarname = tvarname
|
| vcvo.tindexname = tindexname
|
| vcvo.wf = wf
|
| vcvo.N = N
|
| vcvo.bw = bw
|
| vcvo.tdelta = tdelta
|
| vcvo.center = center
|
| vcvo.dofminus = dofminus
|
| vcvo.ZZ = st_matrix(ZZmatrix)
|
|
|
|
|
|
|
|
|
|
|
|
|
| Xnames = invtokens( (X1names, X2names), " ")
|
| Znames = invtokens( (Z1names, X2names), " ")
|
|
|
| st_view(y, ., st_tsrevar(tokens(yname)), touse)
|
| st_view(X1, ., st_tsrevar(tokens(X1names)), touse)
|
| st_view(Z1, ., st_tsrevar(tokens(Z1names)), touse)
|
| st_view(X, ., st_tsrevar(tokens(Xnames)), touse)
|
| st_view(Z, ., st_tsrevar(tokens(Znames)), touse)
|
| st_view(e, ., ename, touse)
|
| st_view(emat, ., tokens(ematnames), touse)
|
| st_view(wvar, ., st_tsrevar(wvarname), touse)
|
|
|
| vcvo.wvar = &wvar
|
| vcvo.Z = &Z
|
| vcvo.Znames = Znames
|
| vcvo.ZZ = st_matrix(ZZmatrix)
|
|
|
| if ("X2names"~="") {
|
| st_view(X2, ., st_tsrevar(tokens(X2names)), touse)
|
| }
|
|
|
| K1=cols(X1)
|
| K2=cols(X2)
|
| K=K1+K2
|
| L1=cols(Z1)
|
| L2=cols(X2)
|
| L=L1+L2
|
| df = L1
|
| df_r = N-L
|
|
|
| ZZinv = st_matrix(ZZinvmatrix)
|
| XXinv = st_matrix(XXinvmatrix)
|
| XPZXinv = st_matrix(XPZXinvmatrix)
|
| QZZ = st_matrix(ZZmatrix) / N
|
| QXX = st_matrix(XXmatrix) / N
|
| QZX = st_matrix(XZmatrix)' / N
|
| QZY = st_matrix(ZYmatrix) / N
|
| QZZinv = ZZinv*N
|
| QXXinv = XXinv*N
|
| QX2X2 = st_matrix(X2X2matrix) / N
|
| QZ1X2 = st_matrix(Z1X2matrix) / N
|
| QX2y = st_matrix(X2ymatrix) / N
|
|
|
| sheaall = (diagonal(XXinv) :/ diagonal(XPZXinv))
|
| sheaall = (sheaall[(1::K1), 1 ])'
|
|
|
|
|
| bz = cholsolve(QZZ, QZY)
|
| Yhat = Z*bz
|
| Xhat = Yhat[.,(2..(K1+1))], X2
|
|
|
| eall = (y, X1) - Yhat
|
| ee = quadcross(eall, wf*wvar, eall)
|
|
|
| sigmasqall = ee / (N-dofminus)
|
|
|
| rmseall = sqrt( ee / (N-L-dofminus-sdofminus) )
|
|
|
| V = sigmasqall # ZZinv
|
|
|
| R = I(L1) , J(L1, L2, 0)
|
|
|
| QXhXh = quadcross(Xhat, wf*wvar, Xhat) / N
|
| QXhX1 = quadcross(Xhat, wf*wvar, X1 ) / N
|
|
|
|
|
|
|
| vcvo.ename = ematnames
|
| vcvo.e = &emat
|
| emat[.,.] = eall
|
| omegar = m_omega(vcvo)
|
| Vr = makesymmetric(I(K1+1)#QZZinv * omegar * I(K1+1)#QZZinv) / N
|
|
|
|
|
| Rb = bz[ (1::L1), 1 ]
|
| RVR = Vr[| 1,1 \ L1, L1 |]
|
| ARWald = Rb' * cholsolve(RVR, Rb)
|
| ARF = ARWald
|
| ARdf = L1
|
| if (clustvarname=="") {
|
| ARdf2 = (N-dofminus-L-sdofminus)
|
| ARF = ARWald / (N-dofminus) * ARdf2 / ARdf
|
| }
|
| else {
|
| ARdf2 = N_clust - 1
|
| ARF = ARWald / (N-1) * (N-L-sdofminus) /(N_clust) * ARdf2 / ARdf
|
| }
|
| ARFp = Ftail(ARdf, ARdf2, ARF)
|
| ARchi2 = ARWald
|
| ARchi2p = chi2tail(ARdf, ARchi2)
|
|
|
|
|
|
|
|
|
| if (K2>0) {
|
| by = cholsolve(QX2X2, QX2y)
|
| e[.,.] = y-X2*by
|
| }
|
| else {
|
| e[.,.] = y
|
| }
|
|
|
| vcvo.e = &e
|
| vcvo.ename = ename
|
|
|
| omega = m_omega(vcvo)
|
| gbar = 1/N * quadcross(Z, wf*wvar, e)
|
| sstat = N * gbar' * cholsolve(omega, gbar)
|
| sstatdf = L1
|
| sstatp = chi2tail(sstatdf, sstat)
|
|
|
|
|
|
|
| firstmat=J(21,0,0)
|
|
|
| vcvo.e = &e
|
| vcvo.ename = ename
|
|
|
| for (i=1; i<=K1; i++) {
|
|
|
|
|
| rmse = rmseall[i+1,i+1]
|
|
|
| shea = sheaall[1,i]
|
|
|
|
|
| b=bz[., (i+1)]
|
|
|
|
|
| Rb = b[ (1::L1), . ]
|
| RVR = V[| 1+i*L,1+i*L \ i*L+L1, i*L+L1 |]
|
| Wald = Rb' * cholsolve(RVR, Rb)
|
|
|
|
|
| pr2 = (Wald/(N-dofminus)) / (1 + (Wald/(N-dofminus)))
|
|
|
|
|
| if ((robust~="") | (clustvarname~="") | (kernel~="") | (sw~="")) {
|
| RVR = Vr[| 1+i*L,1+i*L \ i*L+L1, i*L+L1 |]
|
| Wald = Rb' * cholsolve(RVR, Rb)
|
| }
|
|
|
| df = L1
|
| if (clustvarname=="") {
|
| df_r = (N-dofminus-L-sdofminus)
|
| F = Wald / (N-dofminus) * df_r / df
|
| }
|
| else {
|
| df_r = N_clust - 1
|
| F = Wald / (N-1) * (N-L-sdofminus) * (N_clust - 1) / N_clust / df
|
| }
|
| pvalue = Ftail(df, df_r, F)
|
|
|
|
|
| if (K1==1) {
|
| Fdf1 = df
|
| Fdf2 = df_r
|
| SWF = F
|
| SWFp = pvalue
|
| SWchi2 = Wald
|
| SWchi2p = chi2tail(Fdf1, SWchi2)
|
| SWr2 = pr2
|
| APF = SWF
|
| APFp = SWFp
|
| APchi2 = SWchi2
|
| APchi2p = SWchi2p
|
| APr2 = SWr2
|
| }
|
| else {
|
|
|
|
|
|
|
| selmat=J(1,K,1)
|
| selmat[1,i]=0
|
|
|
|
|
|
|
|
|
|
|
| QXhXhi = select(select(QXhXh,selmat)', selmat)
|
| QXhX1i = select(QXhX1[.,i], selmat')
|
|
|
| b1=cholsolve(QXhXhi, QXhX1i)
|
| QXhXhinv = invsym(QXhXhi)
|
| b1=QXhXhinv*QXhX1i
|
| e1 = X1[.,i] - select(Xhat,selmat)*b1
|
|
|
| QZe1 = quadcross(Z, wf*wvar, e1 ) / N
|
| b2=cholsolve(QZZ, QZe1)
|
| APe2 = e1 - Z*b2
|
| ee = quadcross(APe2, wf*wvar, APe2)
|
| sigmasq2 = ee / (N-dofminus)
|
|
|
| Vi = sigmasq2 * QZZinv / N
|
| APRb=b2[ (1::L1), .]
|
| APRVR = Vi[ (1::L1), (1..L1) ]
|
| APWald = APRb' * cholsolve(APRVR, APRb)
|
|
|
|
|
| APr2 = (APWald/(N-dofminus)) / (1 + (APWald/(N-dofminus)))
|
|
|
|
|
|
|
| e1 = X1[.,i] - select(X,selmat)*b1
|
|
|
| QZe1 = quadcross(Z, wf*wvar, e1 ) / N
|
| b2=cholsolve(QZZ, QZe1)
|
| SWe2 = e1 - Z*b2
|
| ee = quadcross(SWe2, wf*wvar, SWe2)
|
| sigmasq2 = ee / (N-dofminus)
|
| Vi = sigmasq2 * QZZinv / N
|
| SWRb=b2[ (1::L1), .]
|
| SWRVR = Vi[ (1::L1), (1..L1) ]
|
| SWWald = SWRb' * cholsolve(SWRVR, SWRb)
|
|
|
|
|
| SWr2 = (SWWald/(N-dofminus)) / (1 + (SWWald/(N-dofminus)))
|
|
|
|
|
| if ((robust~="") | (clustvarname~="") | (kernel~="") | (sw~="")) {
|
| e[.,1]=APe2
|
| omega=m_omega(vcvo)
|
| Vi = makesymmetric(QZZinv * omega * QZZinv) / N
|
| APRVR = Vi[ (1::L1), (1..L1) ]
|
| APWald = APRb' * cholsolve(APRVR, APRb)
|
| e[.,1]=SWe2
|
| omega=m_omega(vcvo)
|
| Vi = makesymmetric(QZZinv * omega * QZZinv) / N
|
| SWRVR = Vi[ (1::L1), (1..L1) ]
|
| SWWald = SWRb' * cholsolve(SWRVR, SWRb)
|
| }
|
|
|
|
|
| Fdf1 = (L1-K1+1)
|
| if (clustvarname=="") {
|
| Fdf2 = (N-dofminus-L-sdofminus)
|
| APF = APWald / (N-dofminus) * Fdf2 / Fdf1
|
| SWF = SWWald / (N-dofminus) * Fdf2 / Fdf1
|
| }
|
| else {
|
| Fdf2 = N_clust - 1
|
| APF = APWald / (N-1) * (N-L-sdofminus) * (N_clust - 1) / N_clust / Fdf1
|
| SWF = SWWald / (N-1) * (N-L-sdofminus) * (N_clust - 1) / N_clust / Fdf1
|
| }
|
| APFp = Ftail(Fdf1, Fdf2, APF)
|
| APchi2 = APWald
|
| APchi2p = chi2tail(Fdf1, APchi2)
|
| SWFp = Ftail(Fdf1, Fdf2, SWF)
|
| SWchi2 = SWWald
|
| SWchi2p = chi2tail(Fdf1, SWchi2)
|
| }
|
|
|
|
|
| firstmat = firstmat ,
|
| (rmse \ shea \ pr2 \ F \ df \ df_r \ pvalue
|
| \ SWF \ Fdf1 \ Fdf2 \ SWFp \ SWchi2 \ SWchi2p \ SWr2
|
| \ APF \ Fdf1 \ Fdf2 \ APFp \ APchi2 \ APchi2p \ APr2)
|
| }
|
|
|
| st_numscalar("r(rmse_rf)", rmseall[1,1])
|
| st_matrix("r(firstmat)", firstmat)
|
| st_matrix("r(b)", bz)
|
| st_matrix("r(V)", Vr)
|
| st_matrix("r(S)", omegar)
|
| st_numscalar("r(archi2)", ARchi2)
|
| st_numscalar("r(archi2p)", ARchi2p)
|
| st_numscalar("r(arf)", ARF)
|
| st_numscalar("r(arfp)", ARFp)
|
| st_numscalar("r(ardf)", ARdf)
|
| st_numscalar("r(ardf_r)", ARdf2)
|
| st_numscalar("r(sstat)",sstat)
|
| st_numscalar("r(sstatp)",sstatp)
|
| st_numscalar("r(sstatdf)",sstatdf)
|
|
|
| }
|
|
|
|
|
|
|
| void s_omega(
|
| string scalar ZZmatrix,
|
| string scalar ename,
|
| string scalar Znames,
|
| string scalar touse,
|
| string scalar weight,
|
| string scalar wvarname,
|
| scalar wf,
|
| scalar N,
|
| string scalar robust,
|
| string scalar clustvarname,
|
| string scalar clustvarname2,
|
| string scalar clustvarname3,
|
| scalar bw,
|
| string scalar kernel,
|
| string scalar sw,
|
| string scalar psd,
|
| string scalar ivarname,
|
| string scalar tvarname,
|
| string scalar tindexname,
|
| scalar tdelta,
|
| scalar center,
|
| scalar dofminus)
|
| {
|
|
|
| struct ms_vcvorthog scalar vcvo
|
|
|
| vcvo.ename = ename
|
| vcvo.Znames = Znames
|
| vcvo.touse = touse
|
| vcvo.weight = weight
|
| vcvo.wvarname = wvarname
|
| vcvo.robust = robust
|
| vcvo.clustvarname = clustvarname
|
| vcvo.clustvarname2 = clustvarname2
|
| vcvo.clustvarname3 = clustvarname3
|
| vcvo.kernel = kernel
|
| vcvo.sw = sw
|
| vcvo.psd = psd
|
| vcvo.ivarname = ivarname
|
| vcvo.tvarname = tvarname
|
| vcvo.tindexname = tindexname
|
| vcvo.wf = wf
|
| vcvo.N = N
|
| vcvo.bw = bw
|
| vcvo.tdelta = tdelta
|
| vcvo.center = center
|
| vcvo.dofminus = dofminus
|
| vcvo.ZZ = st_matrix(ZZmatrix)
|
|
|
| st_view(Z, ., st_tsrevar(tokens(Znames)), touse)
|
| st_view(wvar, ., st_tsrevar(wvarname), touse)
|
| st_view(e, ., vcvo.ename, touse)
|
|
|
| vcvo.e = &e
|
| vcvo.Z = &Z
|
| vcvo.wvar = &wvar
|
|
|
| ZZ = st_matrix(ZZmatrix)
|
|
|
| S=m_omega(vcvo)
|
|
|
| st_matrix("r(S)", S)
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| function cholqrsolve ( numeric matrix A,
|
| numeric matrix B,
|
| | real scalar useqr)
|
| {
|
| if (args()==2) useqr = 0
|
|
|
| real matrix C
|
|
|
| if (!useqr) {
|
| C = cholsolve(A, B)
|
| if (C[1,1]==.) {
|
| C = qrsolve(A, B)
|
| }
|
| }
|
| else {
|
| C = qrsolve(A, B)
|
| }
|
|
|
| return(C)
|
|
|
| }
|
|
|
| end
|
|
|
| exit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|