*! gdprdif2   0.2.3 2008-01-27
*   > minor cleanup

* compute a pair of prvalues using globals for x()

capture program drop gdprdif2
program define gdprdif2, rclass
    version 9

//  only logit now

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

//  CHECK IF XIS HAS RUN

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

//  DECODE SYNTAX

    syntax ///
        [ , ///
        cis(string)     /// variable that is control variale; cis(k5=2)
        GRANDmean       /// use grand means fromcombined means
        GROUPmean       /// use within group means
        NOINTERACT      /// model without interactions
        CONSTANT        /// model that has constants * 2008-01-26
        QUIetly         /// suppress output of prvalue
        Print           /// Print results from gdaccum
        rownm(string)   /// Name to label matrix row for current set of results.
        rowname(string)   /// Name to label matrix row for current set of results.
        MATrix(string)  /// Name of matrix to hold accumulated results.
        cval(string)    /// Value of the control variable.
        Bounds          /// print ub and lb for save / cur predictions
        reset           /// reset gdaccum matrix
        title(string)   /// title to label matrix
        debug           /// print results for debug
        skip            /// skip a row in the matrix after recording this information
        NOSig           /// suppress printing of Is_sig column
        NOVal           /// suppress printing of values column
        ]

    local qui "qui"
    if "`debug'"=="debug" {
        local qui ""
    }

    *2008-01-26
    if "`constant'"=="constant" {
        local nointeract "nointeract"
    }

    * allow two spellings of option...
    if "`rownm'"=="" & "`rowname'"!="" {
        local rownm "`rowname'"
    }

    * praccum options
    if "`matrix'"!="" {
        local matrix "matrix(`matrix') "
    }
    if "`title'"!="" {
        local title "title(`title') "
    }
    if "`rownm'"!="" {
        local rownm "rownm(`rownm') "
    }
    if "`cval'"!="" {
        local cval "cval(`cval') "
    }

    * nointeract is the default
    if "`nointeract'"=="" {
        local interact "interact"
    }
    * grandmean is the default
    if "`groupmean'"=="" {
        local grandmean "grandmean"
    }

//  DECIDE WHICH X() VALUES TO USE

    * grand means
    if "`grandmean'"=="grandmean" {

        if "`interact'"=="interact" {
            * use means for interaction variables
            local xis0 "$xisg0"
            local xis1 "$xisg1"
        }
        else {
            * use same means for both
            local xis0 "$xis"
            local xis1 "$xis"
        }
    }

    * group means
    else if "`groupmean'"=="groupmean" {

        if "`interact'"=="interact" {
            * by group means for interaction variables
            local xis0 "$xisg0mn0"
            local xis1 "$xisg1mn1"
        }
        else {
            * by group means not for interaction variables
            local xis0 "$xismn0"
            local xis1 "$xismn1"
        }
    }

//  CREATE STRINGS TO SET VALUES OF GROUP DUMMY

        local gisg0 "$gisg0"
        local gisg1 "$gisg1"

//  DECODE CIS - needed for 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

/*
        if "`debug'"=="debug" {
            di "ncisvars: `ncisvars'"
            forvalues i = 1/`ncisvars' {
                di "cisvar: `cisvar`i''"
                di "cisval: `cisval`i''"
            }
        }
*/

//  CIS() DEPENDING ON WHETHER INTERACT OR NOT

    * version 0.1.1
    if "`interact'"=="interact" {

        * if a control variable is set create cis for both groups
        if "`cis'"!="" {
            local cis0 ""
            local cis1 ""
            forvalues i = 1/`ncisvars' { // loop through cis vars
                local cis0 "`cis0' $g0var"
                local cis0 "`cis0'`cisvar`i''=`cisval`i''"
                local cis1 "`cis1' $g1var"
                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 gisg0 "$gvar"
        local gisg0 "`gisg0'=0"
        local gisg1 "$gvar"
        local gisg1 "`gisg1'=1"

    } // if nointeract

//  LABLELS FOR PRVALUE

    local glab0 "$g0var"
    local glab1 "$g1var"

//  CHECK RESULTS

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

        di _new "Variable names:"
        di _new "    \$gvar:        $gvar"
        di      "    \$g0var:       $g0var"
        di      "    \$g1var:       $g1var"
        di _new "RHS variable lists:"
        di _new "    \$rhsg0:       $rhsg0"
        di      "    \$rhsg1:       $rhsg1"
        di      "    \$rhsnoint:    $rhsnoint"
        di _new "RHS group variables set to 1"
        di _new "    \$gis:         $gis"
        di      "    \$gisg0:       $gisg0"
        di      "    \$gisg1:       $gisg1"
        di _new "RHS variables set to grand means:"
        di _new "    \$xis:        $xis"
        di      "    \$xisg0:      $xisg0"
        di      "    \$xisg1:      $xisg1"
        di _new "RHS variables set to group means:"
        di _new "    \$xismn0:     $xismn0"
        di      "    \$xismn1:     $xismn1"
        di      "    \$xisg0mn0:   $xisg0mn0"
        di      "    \$xisg1mn1:   $xisg1mn1"

        di "cis0:   `cis0'"
        di "glab0:  `glab0'"
        di "cis1:   `cis1'"
        di "glab1: `glab1'"

        if "`quietly'"!="quietly" {
            di _new "==> gdprdif output:"
        }
    } // if debug

//  CALL PRVALUE

    * gdprdif2 0.1.3 2007-03-13 > UB and LB for pr().
    mat _GDublb = J(2,2,.)
    `qui'      prvalue , x(`gisg0' `xis0' `cis0') rest(zero) save lab(`glab0')

    if "`debug'"=="debug" {
        di _new "prvalue at x(gisg0 xis0 cis0) rest(zero)"
        di _new "prvalue , x(`gisg0' `xis0' `cis0') rest(zero) save lab(`glab0')"
    }

    * gdprdif2 0.1.3 2007-03-13 > UB and LB for pr().
    mat _GDublb[1,1] = r(p1_hi)
    mat _GDublb[1,2] = r(p1_lo)
    `quietly' prvalue , x(`gisg1' `xis1' `cis1') rest(zero) diff lab(`glab1')
    if "`debug'"=="debug" {
        di _new "prvalue at x(gisg1 xis1 cis1) rest(zero)"
        di _new "prvalue , x(`gisg1' `xis1' `cis1') rest(zero) diff lab(`glab1')"
    }
    * gdprdif2 0.1.3 2007-03-13 > UB and LB for pr().
    mat _GDublb[2,1] = r(p1_hi)
    mat _GDublb[2,2] = r(p1_lo)
    * gdprdif2 0.1.2 2007-03-13 > Tie-in to gdaccum.
    if "`matrix'"!="" {
        * gdprdif2 0.1.5
        if "`skip'"=="skip" { // add blank line first
            gdaccum2, skip `matrix'
        }
        *di "GD: `print' `matrix' `rownm' `cval' `reset' `title'"
        gdaccum2, `print' `matrix' `rownm' `cval' `reset' `title' ///
            `bounds' `noval' `nosig'
    }

end
exit

//  History

* gdprvalue1 0.1.0 2007-03-11
* gdprvalue1 0.1.1 2007-03-12 > Add multiple cis() values.
* gdprvalue1 0.1.2 2007-03-13 > Tie-in to gdaccum.
* gdprvalue1 0.1.3 2007-03-13 > UB and LB for pr().
* gdprvalue1 0.1.4 2007-03-14 > General rename and code cleanup.
* gdprvalue1 0.1.5 2007-03-15 > Minor changes.
* gdprdif2   0.2.0 2007-03-22 >
* gdprdif2   0.2.1 2007-03-29 > noval nosig
* gdprdif2   0.2.2 2008-01-26 > enhance debug