*! version 0.5.2 2009-05-25 jsl
*   - allow print only and nomakevars

//  generate gcprvalue predictions to plot

capture program drop gcprgen
program define gcprgen, rclass
    version 9
*(numeric min=1 max=1)
    syntax [varlist] [if] [in] ///
        , [ ///
        From(real -999) To(real 999) ///
        Ncases(integer 0) gap(real 1.0) ///
        NOINTeract ///
        Generate(string) ///
        x(string) ///
        Matrix(string) ///
        NOIsily /// Show output of gcprvalue
        PRINT /// print matrix
        NOMAKEvars ///
        ]

local makevars "makevars"
if "`nomakevars'"=="nomakevars" {
    local makevars ""
}
local printonly = 0
if "`print'"=="print" & "`matrix'"!="" ///
    & `from'==-999 & `to'==999 {
    local printonly = 1
}

if `printonly'==0 {
//  if no matrix, create one with name of generate varaible
    if "`matrix'"=="" {
        local matrix "`generate'"
    }

//  show output from gcprvalue?

    local quietly "quietly "
    if "`noisily'"=="noisily" {
        local quietly ""
        di _new "Results from gcprvalue called by gcprgen" _new
    }

//  create values to compute for plots

    * check from and to
    if `from'>=`to' {
        di in r "from() must be < to()"
        exit
     }
    * turn gap into increments
    if `gap'<=0 {
        di in r "gap must be positive or 0."
        exit
    }
    if `gap'==1 & `ncases'!= 0 { // use ncases
    }
    else { // use gap
        local range = `to' - `from'
        if `gap'!=0 {
            local ngaps = `range'/`gap'
            if  int(`ngaps')==`ngaps' {
                local ncases = `ngaps' + 1
            }
            else {
                di in r "gap does not divide evenly into from to interval."
                exit 198
            }
        }
    }
    * verify valid number of plot points
    if `ncases' < 3 {
        di in r "ncases() must be greater than 3"
        exit 198
    }
    if `ncases' > _N {
        set obs `ncases'
    }
    local inc = (`to'-`from')/(`ncases'-1)

//  call GCPRVALUE

    local chngvar "`varlist'"
    foreach v of numlist `from'(`inc')`to' {
        local xstring "`x' `chngvar'=`v'"
        local lbl "At_`v'"
        `quietly' gcprvalue, x(`xstring') xis(`v') ///
            matrix(`matrix') label(`lbl') `nointeract'
    }
}

//  print results

    gc_results, matrix(`matrix') `makevars' `print'

end
exit

// TO DO: Perhaps passthrough these options to prvalue?

    Rest(passthru) ///
    Level(passthru)
    Brief noBAse
    MARginal CI
    noLAbel noBAse Brief ///
    YStar ept DELta BOOTstrap REPs(passthru) SIze(passthru) ///
    DOts match NORMal PERCENTile BIAScorrected ///
    CONditional

//  VERSION HISTORY

* gcprgen version 2.0.4 23Jun2005 : _pexstring bug fix (2)
* version 0.0.1 2008-06-19 jsl
* version 0.1.3 2008-06-25 add nointeract
* version 0.1.3 2008-06-25 add nointeract
* version 0.1.2 2008-06-25 let gen define the matrix name
* - If matrix not defined used gen(); else use matrix()
* version 0.1.1 2008-06-24 revise global names
* version 0.1.0 2008-06-19 jsl
* version 0.5.0 2009-05-22 jsl
* version 0.5.1 2009-05-25 jsl