*
* LIML does limited information maximum likelihood estimation of
* a linear regression equation. Before using this, you should set
* the instrument set using INSTRUMENTS.
*
* Syntax:
*
* @LIML  depvar  start  end
* # list of explanatory variables
*
* In addition to the regression output, this produces the LR specification
* test based upon T(lambda-1) where lambda is the eigenvalue used in
* the estimation process.
*
* Revision Schedule:
*
* 09/03 Written by Tom Doan, Estima
* 11/03 Revised to work with version 6
*
procedure LIML depvar start end resids
type series depvar *resids
type integer start end
*
local equation limeq
local integer ninstr i j
local vect[integer] inclexog exclexog
local rect[integer] itable etable ctable dtable
local rect ww
local symm w0 w1
local vect eigval
local real k
*
equation limeq depvar
compute itable  =%insttable()
compute ninstr  =%cols(itable)
compute etable  =%eqntable(limeq)
compute dtable  =||depvar|0||
compute ctable  =%tablemerge(%tablemerge(itable,dtable),etable)
compute inclexog=%tablefindall(itable,etable)
compute exclexog=%tablefindmiss(itable,etable)
cmom start end
# %rlfromtable(ctable)
compute ww=%sweeplist(%cmom,inclexog)
compute w0=%xsubmat(ww,ninstr+1,%ncmom,ninstr+1,%ncmom)
compute ww=%sweeplist(ww,exclexog)
compute w1=%xsubmat(ww,ninstr+1,%ncmom,ninstr+1,%ncmom)
eigen inv(w1)*w0 eigval
compute k=%minvalue(eigval)
ewise %cmom(i,j)=%cmom(i,j)-%if(i>ninstr.and.j>ninstr,k*w1(i-ninstr,j-ninstr),0.0)
linreg(cmom,equation=limeq,title='LIML')
*
* LR test
*
cdf(title='LIML Specification Test') chisqr %nobs*(k-1) ninstr-%nreg
end




