* @lagspec(lagmax)
* # series
* 
* lagspec : Computes AIC, BIC and likelihood ratio test for a VAR from
*           LAGMAX to 1 lags.  
* 
* The first part of this procedure is based on the ESTIMA code for
* computing the AIC and BIC for a set of lags.  It is modified to save
* the log determinants and then use these to compute the various
* likelihood ratio statistics
*
* Syntax:
* 
* @LAGSPEC(LAGMAX) 
* # SERIES
* 
* LAGMAX = highest model order for AIC, BIC, and LR test computation.
* SERIES = series to be used in VAR
* 
* Notice that the user is responsible for setting the sample size.  It
* is best that you set the sample so that all computations with use
* the same sample size.  This requires setting the start of the sample
* as START + LAGMAX with a SMPL instruction.
* 
* 
* Patrick T. Brandt
* Department of Political Science
* University of North Texas
* http://www.psci.unt.edu/~brandt
* brandt@unt.edu
* 
* Revision history:
*	Original version: November 2001
*


PROCEDURE LAGSPEC 

* DECLARE AND DIMENSION THE VARIABLES
OPTION INTEGER LAGMAX 
LOCAL INDEX LIST
LOCAL RECT LDETS
LOCAL INTEGER M
DIMENSION LDETS(LAGMAX,2)
ENTER(VARYING,ENTRIES=M) LIST

* USE A LOOP TO COMPUTE THE LOG DETERMINANTS, AIC AND BIC.  THIS IS A
* MODIFIED VERSION OF THE CODE IN THE RATS MANUAL.

DO P=1,LAGMAX

   * RUN MODEL FOR ORDER P
   SYSTEM
   VARIABLES LIST
   LAGS 1 TO P
   DET CONSTANT
   END(SYSTEM)
   ESTIMATE(NOPRINT)

   * SAVE THE LOG DETERMINANT AND LAG
   * NOTICE THESE ARE PUT IN BACKWARDS SO WE CAN TEST DOWN BY LOOPING
   * FORWARD LATER...

   COMPUTE LDETS(LAGMAX-P+1,1) = P
   COMPUTE LDETS(LAGMAX-P+1,2) = %LOGDET

   * COMPUTE THE AIC AND BIC AND PRINT THE RESULTS	

   COMPUTE BIC= %NOBS*%LOGDET+M*(M*P+1)*LOG(%NOBS)
   COMPUTE AIC= %NOBS*%LOGDET+M*(M*P+1)*2
   IF P==1
   { 
    DISPLAY 'AKAIKE AND BAYESIAN INFORMATION CRITERION FOR VAR(P)'
    DISPLAY @4 'LAGS'  @20 'AIC'  @35  'BIC' 
   }
   DISPLAY @5 #### P  @20 ####.#### AIC  @35 BIC

END DO P

   * NOW GO BACK AND LOOP OVER THE MATRIX OF LOG-DETERMINANTS TO DO
   * THE LR TESTS WITH SIMS CORRECTION

   * START BY SETTING P=1 AGAIN SO WE CAN REITERATE OVER THE VECTOR OF
   * SAVED LOG DETERMINANT VALUES

COMPUTE P=1

DO P=1,LAGMAX-1
   COMPUTE MCORR = M*LDETS(P,1) + 1
   COMPUTE CHI = (%NOBS-MCORR)*(LDETS(P+1,2) - LDETS(P,2))
   COMPUTE DF = (M*M)
   CDF(NOPRINT) CHISQR CHI DF
   COMPUTE CHIP = %SIGNIF
   IF P==1
   {
    DISPLAY ' '
    DISPLAY 'LIKELIHOOD RATIO TESTS FOR VAR(P) VERSUS VAR(P-1)'
    DISPLAY @4 'UNREST.' @13 'V.' @17 'REST.' @26 'CHI^2' @34 'P-VALUE' 
   }
   DISPLAY @5 ### LAGMAX-P+1 @18 ### LAGMAX-P @24 ####.#### CHI @34 #.#### CHIP
END DO P

END LAGSPEC