*! version 1.4 3Apr2003 *! mark_holmes@unc.edu *! Klein I benchmarked (http://www.stanford.edu/~clint/bench/klein.tsp) 3Apr2002 program define liml, eclass byable(recall) version 6.0 if replay() { if `"`e(cmd)'"' != "liml" { error 301 } else { if _by() { error 190 } regress `0' } } else { local n 0 gettoken lhs 0 : 0, parse(" ,[") match(paren) IsStop `lhs' if `s(stop)' { error 198 } while `s(stop)'==0 { if "`paren'"=="(" { local n = `n' + 1 if `n'>1 { capture noi error 198 di in red `"syntax is "(all instrumented variables = instrument variables)""' exit 198 } gettoken p lhs : lhs, parse(" =") while "`p'"!="=" { if "`p'"=="" { capture noi error 198 di in red `"syntax is "(all instrumented variables = instrument variables)""' di in red `"the equal sign "=" is required"' exit 198 } local end`n' `end`n'' `p' gettoken p lhs : lhs, parse(" =") } tsunab end`n' : `end`n'' tsunab exog`n' : `lhs' } else { local exog `exog' `lhs' } gettoken lhs 0 : 0, parse(" ,[") match(paren) IsStop `lhs' } local 0 `"`lhs' `0'"' tsunab exog : `exog' tokenize `exog' local lhs "`1'" local 1 " " local exog `*' syntax [if] [in] marksample touse markout `touse' `lhs' `exog' `exog1' `end1' Subtract newexog : "`exog1'" "`exog'" /* now check for perfect collinearity in instrument list */ _rmcoll `newexog' local newexog "`r(varlist)'" local endo_ct : word count `end1' local ex_ct : word count `newexog' if `endo_ct' > `ex_ct' { di in red "equation not identified; must have at " /* */ "least as many instruments not in" di in red "the regression as there are " /* */ "instrumented variables" exit 481 } *** END CLIP FROM IVREG *** BEGIN LIML CODE di " " di in gr "Instruments : " in ye _col(20) "`newexog'" di in gr "Exogenous: " in ye _col(20) "`exog'" di in gr "Endogenous: " in ye _col(20) "`end1'" di in gr "Dep Var:" in ye _col(20) "`lhs'" tempname gamma W0 W1 eX ev D w0sm w1sm W0big W1big V lambda b tempvar ygam newlhs local i =1 foreach var in `lhs' `end1' { tempname e0`i' e1`i' qui reg `var' `exog' if `touse' qui predict `e0`i'' , residual qui reg `var' `exog' `newexog' if `touse' qui predict `e1`i'' , residual local e0s "`e0s' `e0`i''" local e1s "`e1s' `e1`i''" local i = `i'+1 } qui mat accum `W0' = `e0s' if `touse', nocon qui mat accum `W1' = `e1s' if `touse', nocon mat `D' = cholesky(inv(`W1'))'*`W0'*cholesky(inv(`W1')) mat symeig `eX' `ev' = `D' local n = colsof(`ev') scalar `lambda' = `ev'[1,`n'] mat `w0sm' = `W0'[1,2..`n'] mat `w1sm' = `W1'[1,2..`n'] mat `w0sm' = `w0sm'' mat `w1sm' = `w1sm'' mat `W0big' = `W0'[2..`n', 2..`n'] mat `W1big' = `W1'[2..`n', 2..`n'] mat `gamma' = syminv(`W0big' - scalar(`lambda')*`W1big')*(`w0sm' - scalar(`lambda')*`w1sm') mat `gamma' = `gamma'' mat colname `gamma' = `end1' mat score `ygam' = `gamma' qui gen `newlhs' = `lhs' - `ygam' qui reg `newlhs' `exog' if `touse' tempvar p e e2 qui predict `p' if e(sample) qui predict `e' if e(sample), residual qui gen `e2' = `e'^2 if `touse' qui count if e(sample) tempname n sigma2 scalar `n' = r(N) mat `b' = `gamma', e(b) qui sum `e2' if `touse' scalar `sigma2' = r(sum) ** generate the v-cov matrix local Ninst : word count `newexog' local Nendog: word count `end1' local Nexog: word count `exog' local k_over = `Ninst' - `Nendog' local k = `Nendog' + `Nexog' +1 ** bring in the kappa tempvar one gen `one'=1 local pks " " local i = 1 foreach var in `end1' `exog' `one' { tempname pk`i' qui reg `var' `exog' `newexog' if `touse' qui predict `pk`i'' if `touse' qui replace `pk`i'' = `var' - `pk`i'' qui replace `pk`i'' = `pk`i'' *scalar(`lambda') local pks "`pks' `pk`i''" local i = `i'+1 } scalar `sigma2' = scalar(`sigma2' ) /( scalar(`n') - `k') qui mat accum `V' = `end1' `exog' `one' `pks' if `touse', nocon local kp1 = `k'+1 mat `V' = `V'[1..`k',1..`k'] - `V'[1..`k',`kp1'...] mat `V' = scalar(`sigma2') * inv(`V') mat `V' = .5 * (`V' + `V'') local coln: colnames `b' mat rowname `V' = `end1' `exog' _cons mat colname `V' = `coln' estimates post `b' `V' di " " di in gr "Limited Information Maximum Likelihood" estimates display di " " if `k_over'>0 { di in gr "Overidentification Test" di in gr "Kappa " _col(30) in ye scalar(`lambda') di in gr "N * log(Kappa) " _col(30) in ye log(scalar(`lambda'))* `n' di in gr "(p-value) " _col(30) in ye chi2tail(`k_over' , log(scalar(`lambda'))* `n') di " " di in gr "Alternative Statistics" di in gr "Basmann F("in ye `k_over' in gr "," in ye `n' - `k' in gr")" _col(30) in ye (`n' - `k') * (scalar(`lambda') -1)/ (`k_over') di in gr "(p-value) "in ye _col(30) Ftail(`k_over' , `n' - `k' ,(`n' - `k') * (scalar(`lambda') -1)/ (`k_over')) } end program define IsStop, sclass /* sic, must do tests one-at-a-time, * 0, may be very large */ if `"`0'"' == "[" { sret local stop 1 exit } if `"`0'"' == "," { sret local stop 1 exit } if `"`0'"' == "if" { sret local stop 1 exit } if `"`0'"' == "in" { sret local stop 1 exit } if `"`0'"' == "" { sret local stop 1 exit } else sret local stop 0 end program define Disp local first "" local piece : piece 1 64 of `"`0'"' local i 1 while "`piece'" != "" { di in gr "`first'`piece'" local first " " local i = `i' + 1 local piece : piece `i' 64 of `"`0'"' } if `i'==1 { di } end /* Remove all tokens in dirt from full */ * Returns "cleaned" full list in cleaned */ program define Subtract /* : */ args cleaned /* macro name to hold cleaned list */ colon /* ":" */ full /* list to be cleaned */ dirt /* tokens to be cleaned from full */ tokenize `dirt' local i 1 while "``i''" != "" { local full : subinstr local full "``i''" "", word all local i = `i' + 1 } tokenize `full' /* cleans up extra spaces */ c_local `cleaned' `*' end