*! gc_results 0.5.2 2009-05-25 scott long
*   - no long stand alone

//  accumulate predictions from gcprvalue to a matrix

capture program drop gc_results
program define gc_results, rclass
version 9

//  DECODE SYNTAX

    syntax , ///
        Matrix(string)      /// matrix to hold accumulated results
        [                   ///
        reset               /// start with a new matrix if one already exists
        ///
        print               /// print matrix
        printfromgcprvalue  /// print request coming from gcprvalue
        title(string)       /// title to label matrix
        /// NOSig suppress printing of Is_sig column
        NOVal               /// suppress printing of values column
        /// noz suppress printing of z-value
        Bounds              /// print ub and lb for save / cur predictions
        digits(integer 3)   /// decimal digits in output
        ///
        xis(string)         /// value of changing variable
        LABel(string)       /// row name for current information
        skip                /// add blank row to matrix
        ///
        MAKEVars            /// make variables from matrix.
        debug               /// print results for debugging
        ]

    tempname matbounds matprt

//  RESET OR PRINT

    local quit = 0
    * if print, get rid of columns as specified
    if "`print'"=="print" {

        * store original matrix
        mat `matbounds' = `matrix'[.,9..12]
        * 0.5.1
        mat `matprt' = `matrix'[.,1..8]
* 0.5.1
* if no sig, get rid of column 7
* if "`nosig'"=="nosig" {
*   mat `matprt' = `matprt'[.,1..6]
* }
        * noval, get rid of column 1
        if "`noval'"=="noval" {
            mat `matprt' = `matprt'[.,2...]
        }
        * add bounds if needed
        if "`bounds'"=="bounds" {
            mat `matprt' = `matprt', `matbounds'
        }
        di _new "Results from matrix `matrix': `title'"
        mat list `matprt', format(%8.`digits'f) title("`title'") noheader
        local quit = 1
    }

    * delete and recreate matrix if it exists
    if "`reset'"=="reset" {
        capture matrix drop `matrix'
        di _new "The matrix was `matrix' initialized."
        local quit = 1
    }

    if "`makevars'"=="makevars" {
        local quit = 0
    }

    if `quit'==1 {
        exit
    }

//  only logit now

    if "`e(cmd)'"!="logit" {
        di in red "current estimation command is not yet supported"
        exit
    }

//  MAKE VARIABLES FROM MATRIX

    if "`makevars'"=="makevars" {

        if "`matrix'"=="" {
            di in red "matrix() must be specified."
            exit
        }
        svmat `matrix', names(matcol)
        exit

    } // makevars

//  IF NO GROUP NAMES, CREATE DEFAULT NAMES

    local gsave "$gr0label"
    if "`gsave'"=="" { // if no label use name
        local gsave "$gr0var"
    }
    local gcur "$gr1label"
    if "`gcur'"=="" {
        local gcur "$gr1var"
    }

//  IF NO xis, CODE IT AS MISSING

    if "`xis'"=="" {
        local xis "."
    }

//  GRAB PRVALUE VALUES CREATED BY GCPRVALUE

    local p1cur = pepred[2,2]   // current prediction
    local p1sav = pepred[4,2]   // saved prediction
    local p1dif = pepred[6,2]   // difference
    local p1dup = peupper[6,2]  // upper bound of dif
    local p1dlo = pelower[6,2]  // lower bound of dif
    * get uppper and lower bounds
    local p1savub = _GCublb[1,1]
    local p1savlb = _GCublb[1,2]
    local p1curub = _GCublb[2,1]
    local p1curlb = _GCublb[2,2]
    * is difference significant?
    local uplo = `p1dup'*`p1dlo' // if positive, significant difference
    if `uplo'>0 {
        local difsig = 1
    }
    else {
        local difsig = 0
    }
    local zdif = _GCublb[3,3]

//  PUT RESULTS IN TEMP MATRIX
    tempname outmat
* 0.5.1
    mat `outmat' = `xis', `p1cur', `p1sav', `p1dif', `p1dlo', `p1dup', `difsig' ///
        , `zdif', `p1savlb', `p1savub', `p1curlb', `p1curub'
    if "`skip'"=="skip" {
        mat `outmat' = J(1,12,.)
    }
    if "`label'"=="" {
        local label "_"
    }
    * If row name invalid, dummy it out
    capture mat rownames `outmat' = "`label'"
    if _rc!=0 {
        mat rownames `outmat' = "PredVal"
    }
    if "`debug'"=="debug" {
        di "#1 debug of -gc_results-: list of matrix"
        mat list `outmat' noheader
    }

//  MOVE VALUES INTO MATRIX

    * does matrix exist? if so add results to end
    capture confirm matrix `matrix'
    if !_rc { // matrix found
        matrix `matrix' = `matrix' \ `outmat'
    }
    * if it doesn't, set up new matrix
    else {
        mat `matrix' = `outmat'
    }
    * label columns
    local lvl = r(level)
    if `lvl'==. {
        local sig "Is_sig"
    }
    else {
        local sig "Sig_`lvl'"
    }
    * UB and LB of save and dif.
    local slb = "`gsave'LB"
    local sub = "`gsave'UB"
    local clb = "`gcur'LB"
    local cub = "`gcur'UB"
* 0.5.1           1      2      3      4    5     6      7    8X     8-9   9-10  10-11 11-12
    local cnames "Value `gcur' `gsave' Diff Lower Upper `sig' zvalue `slb' `sub' `clb' `cub'"
    mat colnames `matrix' = `cnames'
    * row names are obtained from outmat matrix and accumulated

//  PRINT RESULTS

    if "`printfromgcprvalue'"=="printfromgcprvalue" {

        * store original matrix
* 0.5.1
        mat `matbounds' = `matrix'[.,9..12]
        mat `matprt' = `matrix'[.,1..8]
* if no sig, get rid of column 7
* if "`nosig'"=="nosig" {
*    mat `matprt' = `matprt'[.,1..6]
* }
        * noval, get rid of column 1
        if "`noval'"=="noval" {
            mat `matprt' = `matprt'[.,2...]
        }
        * add bounds if needed
        if "`bounds'"=="bounds" {
            mat `matprt' = `matprt', `matbounds'
        }
        di _new "Results from matrix `matrix': `title'"
        mat list `matprt', format(%8.`digits'f) title("`title'") noheader

    } // print

end
exit

* gdaccum2 0.2.3 2008-01-27*! gcresults 0.1.2 2008-07-11
*   - fix row labels if value is decimal
* gcresults 0.1.1 2008-06-24
*   - revise global names
* gcresults 0.5.0 2009-05-22
* gcresults 0.5.1 2009-05-25 scott long
*   - a z value to matrix