*! gcprvalue 0.5.3 2009-05-25 scott long print only
*    - uses gc_results.ado

//  compute groups with a pair of prvalues; save results to matrix

capture program drop gcprvalue
program define gcprvalue, rclass
    version 9

//  only logit now

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

//  CHECK IF gcspecify HAS RUN

    gc_checkxis
    local error = r(error)
    if `error'==1 {
        exit
    }

//  DECODE SYNTAX

    syntax ///
        [ , ///
        /// levels of variables
        GRANDmean       /// use grand means from combined means
        GROUPmean       /// use within-group means
        CROSSmean       /// use across-group (other groups) means
        x(string)       /// x() as in prvalue
        xis(passthru)   /// value being varied in rows of matrix
        NOINTeract      /// model without interactions; default is interact
        /// matrix holding results
        reset           /// reset matrix
        MATrix(passthru)  /// matrix holding results
        Print           /// print matrix of results
        LABel(passthru) /// labael for current predictions
        Bounds          /// print ub and lb for save / cur predictions
        Title(passthru) /// title for matrix
        skip            /// skip row in matrix after recording information
        /// NOSig suppress printing of Is_sig column
        NOVal           /// suppress printing of values column
        /// NOZ suppress printing of zvalues column
        ///
        QUIetly         /// suppress output of prvalue
        VERBOSE         /// show the globals used by prvalue
        debug           /// print results for debug
        ]

    * 0.5.3
    local printonly = 0
    if "`grandmean'"=="" & "`groupmean'"=="" & "`crossmean'"=="" ///
        & "`x'"=="" & "`xis'"=="" & "`matrix'"!="" & "`print'"=="print" {
        local printonly = 1
    }

    local cis "`x'" // use cis not x in code that follows
    local qui "qui"
    if "`debug'"=="debug" {
        di "1 cis:>`cis'<"
        local qui ""
    }

//  HOW CAN IT TELL WHICH WAS RUN??

    * nointeract is the default; user must know which was used!
    if "`nointeract'"=="" {
        local interact "interact"
    }
    if "`debug'"=="debug" {
        di "interact:   `interact'"
        di "nointeract: `nointeract'"
    }
    * determiine if model has only group variable in it
    _perhs
    local tmp = r(rhsnms)
    if "`tmp'"=="$grpvar" {
        local interact ""
        local nointeract "nointeract"
    }

//  DECIDE WHICH X() VALUES TO USE

    * if nothing specified, use grand means
    if "`groupmean'"=="" & "`crossmean'"=="" {
        local grandmean "grandmean"
    }
    * grand means - both groups use combined means
    if "`grandmean'"=="grandmean" {
        if "`interact'"=="interact" {
            local xis0 "$xgr0mng"
            local xis1 "$xgr1mng"
        }
        else {
            local xis0 "$xall"
            local xis1 "$xall"
        }
    }
    * group means: use own group means
    else if "`groupmean'"=="groupmean" {
        if "`interact'"=="interact" {
            local xis0 "$xgr0mn0"
            local xis1 "$xgr1mn1"
        }
        else { // noninteract
            local xis0 "$xallmn0"
            local xis1 "$xallmn1"
        }
    }
    * cross means: use other groups means
      else if "`crossmean'"=="crossmean" {
        if "`interact'"=="interact" {
            local xis0 "$xgr0mn1"
            local xis1 "$xgr1mn0"
        }
        else { // noninteract
            local xis0 "$xallmn1"
            local xis1 "$xallmn0"
        }
    }

//  CREATE STRINGS TO SET VALUES OF GROUP DUMMY

        local gisgr0 "$gr0"
        local gisgr1 "$gr1"
        if "`debug'"=="debug" {
            di "gisgr0: `gisgr0'"
            di "gisgr1: `gisgr1'"
        }

//  DECODE CIS - needed when more than one cis condition

        tokenize `cis', parse(" =")
        local allnums = "yes"
        local ncisvars = 0 // number of cis vars

        while "`1'" != "" { // loop over variables

            while "`2'"=="=" | "`2'"=="==" { // loop over values
                local temp1 "`1'"
                macro shift
                local 1 "`temp1'"
            }
            if "`2'"=="" {
                di _newline in red "Odd number of arguments in x()"
                error 198
            }
            local ncisvars = `ncisvars' + 1
            local cisvar`ncisvars' "`1'"
* DO: make sure variable is rhs variable
            local cisval`ncisvars' "`2'"
            confirm number `2'
            macro shift 2

        } // loop over variables in cis

//  CIS() DEPENDING ON WHETHER INTERACT OR NOT

    if "`interact'"=="interact" {

        * if a control variable set, create cis for both groups
        if "`cis'"!="" {
            local cis0 ""
            local cis1 ""
            forvalues i = 1/`ncisvars' { // loop through cis vars
                local cis0 "`cis0' $gr0var"
                local cis0 "`cis0'`cisvar`i''=`cisval`i'' "
                local cis1 "`cis1' $gr1var"
                local cis1 "`cis1'`cisvar`i''=`cisval`i'' "
            }

        } // if cis!=""

    } // if interact

    else if "`nointeract'"=="nointeract" {

        * with nointeract, control variable is same for both groups
        if "`cis'"!="" {
            local cis0 ""
            local cis1 ""
            forvalues i = 1/`ncisvars' { // loop through cis vars
                local cis0 "`cis0'`cisvar`i''=`cisval`i'' "
                local cis1 "`cis1'`cisvar`i''=`cisval`i'' "
            }
        }

        * group dummy
        local gisgr0 "$grpvar"
        local gisgr0 "`gisgr0'=0"
        local gisgr1 "$grpvar"
        local gisgr1 "`gisgr1'=1"

    } // if nointeract

//  LABLELS FOR PRVALUE

    local glab0 "$gr0label"
    local glab1 "$gr1label"

//  CHECK RESULTS

    if "`debug'"=="debug" {

        di _new "Variable names:"
        di _new "    \$grpvar:      $grpvar"
        di      "    \$gr0var:      $gr0var"
        di      "    \$gr1var:      $gr1var"
        di _new "RHS variable lists:"
        di _new "    \$rhsgr0:      $rhsgr0"
        di      "    \$rhsgr1:      $rhsgr1"
        di      "    \$rhsnoint:    $rhsnoint"
        di _new "RHS group variables set to 1"
        di _new "    \$grp:         $grp"
        di      "    \$gr0:         $gr0"
        di      "    \$gr1:         $gr1"
        di _new "RHS variables set to grand means:"
        di _new "    \$xall:        $xall"
        di      "    \$xgr0mng:     $xgr0mng"
        di      "    \$xgr1mng:     $xgr1mng"
        di _new "RHS variables set to group 0 means:"
        di _new "    \$xallmn0:     $xallmn0"
        di      "    \$xgr0mn0:     $xgr0mn0"
        di      "    \$xgr1mn0:     $xgr1mn0"

        di _new "RHS variables set to group 1 means:"
        di _new "    \$xallmn1:     $xallmn1"
        di      "    \$xgr0mn1:     $xgr0mn1"
        di      "    \$xgr1mn1:     $xgr1mn1"

        di _new "CIS stuff"
        di _new "    cis0:          `cis0'"
        di      "    glab0:         `glab0'"
        di      "    cis1:          `cis1'"
        di      "    glab1:         `glab1'"

        if "`quietly'"!="quietly" {
            di _new "==> gcprvalue output:"
        }

    } // if debug

* 0.5.3
if `printonly'==0 {

//  CALL PRVALUE

* see insert endowments below
* see insert returns below

    mat _GCublb = J(3,3,.) // global matrix to hold UB and LB and z
*2009-05-24
mat rownames _GCublb = Start End Diff
local cub = 1
local clb = 2
local cz = 3
    qui prvalue , x(`gisgr0' `xis0' `cis0') rest(zero) save lab(`glab0')
*di ". prvalue , x(`gisgr0' `xis0' `cis0') rest(zero) save lab(`glab0')"
    mat _GCublb[1,`cub'] = r(p1_hi)
    mat _GCublb[1,`clb'] = r(p1_lo)
    `quietly' prvalue , x(`gisgr1' `xis1' `cis1') rest(zero) diff lab(`glab1')
*di ". prvalue , x(`gisgr1' `xis1' `cis1') rest(zero) diff lab(`glab1')"
    mat _GCublb[2,`cub'] = r(p1_hi)
    mat _GCublb[2,`clb'] = r(p1_lo)
    tempname rpred
    mat `rpred' = r(pred)
    mat _GCublb[3,`cub'] = `rpred'[2,3] // ub of diff
    mat _GCublb[3,`clb'] = `rpred'[2,2] // lb of diff
    mat _GCublb[3,`cz'] = `rpred'[2,6] // z of dif
/*
r(pred)[2,6]
        DProb          LB          UB    Category          SE           z
0  -.09155078  -.13523032  -.04787121           0    .0222859  -4.1080143
1   .09155077   .04787121   .13523032           1    .0222859   4.1080136
*/

    if "verbose"=="`verbose'" {
        di ". prvalue , x(Lgisgr0 Lxis0 Lcis0) rest(zero) save lab(Lglab0)"
        di ". prvalue , x(Lgisgr1 Lxis1 Lcis1) rest(zero) diff lab(Lglab1)"
    }
} // end of if printonly==0

//  ADD TO MATRIX

    if "`matrix'"!="" {
        if "`skip'"=="skip" { // add blank line first
            gc_results, skip `matrix'
        }
        * rename command before sending to gc_results
        if "`print'"=="print" {
            local print "printfromgcprvalue"
        }
        if "debug"=="`debug'" {
            di ". gc_results, `print' `matrix' `label' `xis' `reset' `title'"
            di ">  `bounds' `noval' "
        }

        gc_results, `print' `matrix' `label' `xis' `reset' `title' ///
            `bounds' `noval'
    }

end
exit

* gdprdif3   0.3.0 2008-04-10 > Adds XGROUPmean, endowmens and returns options.
* gcpredict 0.0.1 2008-06-18
* gcpredict 0.0.2 2008-06-18 - if only $grpvar rhs figure out what to do
* gcpredict 0.1.1 2008-06-24 revise global names
* gcpredict 0.1.0 2008-06-18 - if only $grpvar rhs figure out what to do
* gcprvalue 0.5.1 2009-05-24 jsl
*   - return z of difference
* gcprvalue 0.5.0 2009-05-22 jsl - pass 2
* gcprvalue 0.5.0 2009-05-22 jsl - pass 1
* gcprvalue 0.5.2 2009-05-24 jsl
* gcprvalue 0.5.2 2009-05-25 scott long

// insert endowments 2009-05-23
        ENDOWments      ///
if "`endowments'"=="endowments" {
    mat _GCublb = J(2,2,.)
    qui      prvalue , x(`gisgr1' $xgr1mn1 `cis1') rest(zero) save lab(`glab1')
    mat _GCublb[1,1] = r(p1_hi)
    mat _GCublb[1,2] = r(p1_lo)
    `quietly' prvalue , x(`gisgr1' $xgr1mn0 `cis1') rest(zero) diff lab(`glab1')
    mat _GCublb[2,1] = r(p1_hi)
    mat _GCublb[2,2] = r(p1_lo)
    if "verbose"=="`verbose'" {
        di "ENDOWMENTS"
        di ". prvalue , x(Lgisgr1 Gxgr1mn1 Lcis1) rest(zero) save lab(Lglab1)"
        di ". prvalue , x(Lgisgr1 Gxgr1mn0 Lcis1) rest(zero) diff lab(Lglab1)"
    }
}

//  insert returns 2009-05-23
        RETURNs         ///
else if "`returns'"=="returns" {
    mat _GCublb = J(2,2,.)
    qui      prvalue , x(`gisgr1' $xgr1mn0 `cis1') rest(zero) save lab(`glab1')
    mat _GCublb[1,1] = r(p1_hi)
    mat _GCublb[1,2] = r(p1_lo)
    `quietly' prvalue , x(`gisgr0' $xgr0mn0 `cis0') rest(zero) diff lab(`glab0')
    mat _GCublb[2,1] = r(p1_hi)
    mat _GCublb[2,2] = r(p1_lo)
    if "verbose"=="`verbose'" {
        di "RETURNS"
        di ". prvalue , x(Lgisgr1 Gxgr1mn0 Lcis1) rest(zero) save lab(Lglab1)"
        di ". prvalue , x(Lgisgr0 Gxgr0mn0 Lcis0) rest(zero) diff lab(Lglab0)"
    }
}