*
* @RegCorrs( options )  resids
*
* RegCorrs performs an autocorrelation analysis on a series of
* residuals, similar to those typically done after estimating an ARIMA
* model.
*
* Parameters:
*   resids      : (input) series of residuals. If omitted, uses the %RESIDS series
*
* Options:
*   NUMBER=number of autocorrelations to compute [depends upon number of data points]
*   [GRAPH]/NOGRAPH Graph the correlations
*   REPORT/[NOREPORT]
*   SPAN=width of test intervals [1]
*   METHOD=[YULE]/BURG
*   TITLE=Title of graph window and report window
*   HEADER=header string for graph
*   FOOTER=footer string for graph
*   PRINT/[NOPRINT] Include printed output from the correlations
*   QSTATS/[NOQSTATS] Computes and displays Ljung-Box Q Statistics
*   DFC=degrees of freedom correction for Q statistics
*   [CRITERIA]/NOCRITERIA Computes and displays the information criteria
*
* Variables defined:
*   %AIC  Akaike Information Criterion
*   %SBC  Schwarz Bayesian Criterion
*
* Revision Schedule:
*   02/2003 Written by Estima
*   12/2003 Updated to use REPORT
*   06/2005 Revised to use %LOGL in generated IC's
*   06/2005 Change default method to YULE
*   05/2007 Add FOOTER option
*   06/2010 Add SPAN option
*   09/2010 Add TITLE option
*
procedure RegCorrs resids
type series resids
*
option switch  graph     1
option switch  qstats	 0
option switch  criteria  1
option integer number
option integer span      1
option choice  method    1   yule burg
option string  title
option string  header
option string  footer
option switch  report    0
option integer dfc       0
option switch  print     0
*
local string 	qstring cstring
local integer 	ncorr i
local series 	corrs pcorrs upper lower
local real 		qstat
local int 		reshandle
local string	ltitle
local report    creport
*
declare real %aic %sbc
*
* If user didn't supply a residuals series, use %resids. So we don't
* have to make an unnecessary copy, this uses an integer tag to
* reference the series.
*
if .not.%defined(resids)
   compute reshandle=%resids
else
   compute reshandle=resids
*
* Compute the AIC and Schwarz criteria using the log likelihood
*
compute %aic = -2.0*%logl/%nobs+2.0*%nreg/%nobs
compute %sbc = -2.0*%logl/%nobs+log(%nobs)*%nreg/%nobs
*
if .not.%defined(title)
   compute ltitle="Residual Analysis"
else
   compute ltitle=title

clear corrs
corr(number=number,qstats,print=print,results=corrs,partial=pcorrs,$
  method=method,dfc=dfc) reshandle
disp(store=qstring) "Q=" *.## %cdstat "P-value" #.##### %signif
disp(store=cstring) "AIC=" *.### %aic "SBC=" *.### %sbc
inquire(series=corrs) * ncorr

if graph {
   set upper 1 ncorr =  2.0/sqrt(%nobs)
   set lower 1 ncorr = -2.0/sqrt(%nobs)
   *
   * We use SPGRAPH as a wrapper because we might be using GRTEXT to add
   * the final Q and the criteria to the graph.
   *
   spgraph(window=ltitle)
   graph(max=1.0,min=-1.0,number=1,style=bargraph,$
      overlay=line,header=header,footer=footer,ovcount=2,ovsame) 3
   # corrs 2 ncorr
   # upper 2 ncorr 1
   # lower 2 ncorr 1
   if qstats
      grtext(entry=2,y=-.9,align=left,size=16) qstring
   if criteria
      grtext(entry=2,y=-.8,align=left,size=16) cstring
   spgraph(done)
}
*
* Do the report of the correlations, partial correlations, running LB Q
* statistics and their significance levels.
*
if report {
   report(use=creport,action=define,title=ltitle,hlabels=||"Lag","Corr","Partial","LB Q","Q Signif"||)
   compute qstat=0.0
   do i=2,ncorr
      compute qstat=qstat+(%nobs*(%nobs+2.0)/(%nobs-i+1))*corrs(i)^2
      if %clock(i-1,span)<>span
         next
      report(use=creport,row=new,atcol=1) i-1 corrs(i) pcorrs(i) qstat
      if i-1-dfc>0
         report(use=creport,row=current,atcol=5) %chisqr(qstat,i-1-dfc)
   end do i
   report(use=creport,action=format,atcol=2,tocol=3,width=6)
   report(use=creport,action=format,atcol=4,width=9)
   report(use=creport,action=format,atcol=5,width=6)
   report(use=creport,action=show)
}
end RegCorrs

