*! gdaccum2 0.2.3 2008-01-27
*   < minor cleanup

//  accumulate predictions to a matrix.

capture program drop gdaccum2
program define gdaccum2, rclass
version 9

//  DECODE SYNTAX

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

    tempname matbounds matprt

//  RESET OR PRINT

    local quit = 0
    if "`printonly'"=="printonly" {

        * store original matrix
        mat `matbounds' = `matrix'[.,8..11]
        mat `matprt' = `matrix'[.,1..7]
        * 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'
        }
        mat list `matprt', format(%8.`digits'f) title("`title'")
        local quit = 1
    }

    * delete matrix if it exists
    if "`reset'"=="reset" {
        capture matrix drop `matrix'
        di _new "The matrix was `matrix' initialized."
        local quit = 1
    }
    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 GSAVE OR GCUR, CREATE DEFAULT NAMES

    if "`g0label'"=="" {
            local gsave "$g0label"
    }
    else {
        local gsave "$g0var"
    }
    if "`g1label'"=="" {
        local gcur "$g1label"
    }
    else {
        local gcur "$g1var"
    }

//  IF NO CVAL GIVEN, CODE IT AS MISSING

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

//  GRAB PRVALUE VALUES

    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

* gdaccum2 0.1.1 2007-03-13 < Get UB and LB of save and dif.
    local p1savub = _GDublb[1,1]
    local p1savlb = _GDublb[1,2]
    local p1curub = _GDublb[2,1]
    local p1curlb = _GDublb[2,2]

    local uplo = `p1dup'*`p1dlo' // if positive, significant difference
    if `uplo'>0 {
        local difsig = 1
    }
    else {
        local difsig = 0
    }

//  PUT RESULTS IN TEMP MATRIX

    tempname outmat
    * gdaccum2 0.1.1 2007-03-13 < Get UB and LB of save and dif.
    mat `outmat' = `cval', `p1cur', `p1sav', `p1dif', `p1dlo', `p1dup', `difsig' ///
        , `p1savlb', `p1savub', `p1curlb', `p1curub'

    * 0.1.4
    if "`skip'"=="skip" {
        mat `outmat' = J(1,11,.)
    }

    if "`rownm'"=="" {
        local rownm "_"
    }
    mat rownames `outmat' = "`rownm'"

    if "`debug'"=="debug" {
        di "debug of -gdaccum-: list of outmat"
        mat list `outmat'
    }

//  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 { // matrix not found
        mat `matrix' = `outmat'
    }

    * label columns
    local lvl = r(level)
    if `lvl'==. {
        local sig "Is_sig"
    }
    else {
        local sig "Sig_`lvl'"
    }

    * gdaccum2 0.1.1 2007-03-13 < Get UB and LB of save and dif.
    local slb = "`gsave'LB"
    local sub = "`gsave'UB"
    local clb = "`gcur'LB"
    local cub = "`gcur'UB"
    local cnames "Value `gcur' `gsave' Diff Lower Upper `sig' `slb' `sub' `clb' `cub'"
    *   local cnames "Value `gcur' `gsave' Diff Lower Upper `sig'"
    mat colnames `matrix' = `cnames'
    * row names are obtained from outmat matrix and accumulated

//  PRINT RESULTS

    if "`print'"=="print" {

        * store original matrix
        mat `matbounds' = `matrix'[.,8..11]
        mat `matprt' = `matrix'[.,1..7]
        * 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'
        }
        mat list `matprt', format(%8.`digits'f) title("`title'")

    } // print

end
exit

//  History

* gdaccum1 0.1.0 2007-03-11
* gdaccum1 0.1.1 2007-03-13 < Get UB and LB of save and dif.
* gdaccum1 0.1.2 2007-03-14 < Rename and code cleanup.
* gdaccum1 0.1.3 2007-03-15 < Allow reset without logit first.
* gdaccum1 0.1.4 2007-03-15 < Misc minor; blank.
* gdaccum2 0.2.0 2007-03-22 <
* gdaccum2 0.2.1 2007-03-29 < noval nosig
* gdaccum2 0.2.2 2008-01-25 < warning on dropped matrix