*
*  PRINCOMP is a simple procedure for extracting principal components
*  from a set of series.
*
*  PRINCOMP  start  end
*  #   list of variables (regression format)
*
*  Options:
*    NCOMPS = number of components [number of variables]
*    PCOMP  = VECT[SERIES] of the extracted principal components
*
*  On output the principal components are put into a
*  a vector of series called PCOMP
*
*  Revision Schedule
*
*  05/01 Modified to use VECTOR of SERIES for the output,
*        rather than a block of numbered series.
*  05/05 Changed syntax for output series, added option of
*        table output.
*
procedure princomp start end pcomp
type integer start end
*
option integer ncomps  0
option vec[series] *pcomp
option switch  print  0
*
local index vars
local rect xmatrix eigenvec
local symm cmom
local vect eigenval princoef componnt
local int  n
local int  icomp nvars
local index slist
local int  startl endl
local int  nobsmake
*
enter(varying) slist
inquire(reglist) startl<<start endl<<end
# slist
*
make xmatrix startl endl nobsmake nvars
# slist
*
matrix cmom=tr(xmatrix)*xmatrix
eigen cmom eigenval eigenvec
if .not.ncomps
   compute n = nvars
else
   compute n = ncomps
if %defined(pcomp)
   dim pcomp(n)
if print
   report(action=define)
do icomp=1,n
   overlay eigenvec(1,icomp) with princoef(nvars)
   if print
      report(col=new,atrow=1,fillby=cols) eigenval(icomp) princoef
   matrix componnt=xmatrix*princoef
   if %defined(pcomp)
      set pcomp(icomp) start end = componnt(t-start+1)
end do icomp
if print
  report(action=show)
end



