*! prior version 1.6.6 13Apr2005 plot values not labels
*  version 1.6.6 13Apr2005 plot values not labels
*  version 1.6.5 30Mar2005 plot values not labels
*  version 1.6.4 19Nov2003 - for stata 8, change where box opens
*  version 1.6.3 11Mar2001

window control clear

capture program drop qmlogview
capture program drop _mlgetv
capture program drop _mlinit
capture program drop _mlplot
capture program drop _mlnxt6
capture program drop _mlprnt
capture program drop _mlhlp

program define qmlogview
    if "`e(cmd)'"!="mlogit" {
        di in r "mlogview must be run after mlogit"
        exit
    }
    version 6.0

*=> Initialize
    global mlopts "`*'"
    _mlinit
    global ml_lbl ""

*=> List variables to plot
    local r1 = 2
    local c1 = 5
    local d1 = 12
    local c2 = `c1' + 55
    local c3 = `c2' + 20
    local c4 = `c3' + 28
    local c5 = `c4' + 24
    global ml_wi "Select Variables      Select Amount of Change"
    window control static   ml_wi        `c1'  `r1'  146   9
    local r1 = `r1' + `d1' - 1
* var#1
    local n1 = 1
    window control scombo   ml_rhsnm     `c1'  `r1'  50  50  ml_v`n1'
    window control radbegin "+1"         `c2'  `r1'  20   9  ml_v`n1'r
    window control radio    "+SD"        `c3'  `r1'  24   9  ml_v`n1'r
    window control radio    "0/1"        `c4'  `r1'  24   9  ml_v`n1'r
    window control radend   "Don't Plot" `c5'  `r1'  40   9  ml_v`n1'r
    local r1 = `r1' + `d1'
* var#2
    local n1 = 2
    window control scombo   ml_rhsnm     `c1'  `r1'  50  50  ml_v`n1'
    window control radbegin "+1"         `c2'  `r1'  20   9  ml_v`n1'r
    window control radio    "+SD"        `c3'  `r1'  24   9  ml_v`n1'r
    window control radio    "0/1"        `c4'  `r1'  24   9  ml_v`n1'r
    window control radend   "Don't Plot" `c5'  `r1'  40   9  ml_v`n1'r
    local r1 = `r1' + `d1'
* var#3
    local n1 = 3
    window control scombo   ml_rhsnm     `c1'  `r1'  50  50  ml_v`n1'
    window control radbegin "+1"         `c2'  `r1'  20   9  ml_v`n1'r
    window control radio    "+SD"        `c3'  `r1'  24   9  ml_v`n1'r
    window control radio    "0/1"        `c4'  `r1'  24   9  ml_v`n1'r
    window control radend   "Don't Plot" `c5'  `r1'  40   9  ml_v`n1'r
    local r1 = `r1' + `d1'
* var#4
    local n1 = 4
    window control scombo   ml_rhsnm     `c1'  `r1'  50  50  ml_v`n1'
    window control radbegin "+1"         `c2'  `r1'  20   9  ml_v`n1'r
    window control radio    "+SD"        `c3'  `r1'  24   9  ml_v`n1'r
    window control radio    "0/1"        `c4'  `r1'  24   9  ml_v`n1'r
    window control radend   "Don't Plot" `c5'  `r1'  40   9  ml_v`n1'r
    local r1 = `r1' + `d1'
* var#5
    local n1 = 5
    window control scombo   ml_rhsnm     `c1'  `r1'  50  50  ml_v`n1'
    window control radbegin "+1"         `c2'  `r1'  20   9  ml_v`n1'r
    window control radio    "+SD"        `c3'  `r1'  24   9  ml_v`n1'r
    window control radio    "0/1"        `c4'  `r1'  24   9  ml_v`n1'r
    window control radend   "Don't Plot" `c5'  `r1'  40   9  ml_v`n1'r
* var#6
    local r1 = `r1' + `d1'
    local n1 = 6
    window control scombo   ml_rhsnm     `c1'  `r1'  50  50  ml_v`n1'
    window control radbegin "+1"         `c2'  `r1'  20   9  ml_v`n1'r
    window control radio    "+SD"        `c3'  `r1'  24   9  ml_v`n1'r
    window control radio    "0/1"        `c4'  `r1'  24   9  ml_v`n1'r
    window control radend   "Don't Plot" `c5'  `r1'  40   9  ml_v`n1'r

*=> Define buttons that execute the plot program
    local r1 = `r1' + `d1'
    window control button "DC Plot"         2 `r1' 40 13 ml_dc
    global ml_dc   "_mlplot 1"
    window control button "OR Plot"        46 `r1' 40 13 ml_or
    global ml_or   "_mlplot 2"
    window control button "OR+DC Plot"     90 `r1' 40 13 ml_od
    global ml_od "_mlplot 3"
    window control button "Next 6"   134 `r1' 40 13 ml_nxt5
    global ml_nxt5 "_mlnxt6"

*=> Add a note to graph
    local r1 = `r1' + `d1' + 7
    local r2 = `r1'
    global ml_wlbl "Note"
    window control static ml_wlbl  5 `r1' 18   9
    window control edit           25 `r2' 148  8 ml_lbl

*=> Plot options
    local r1 = `r1' + `d1' + 3
    global ml_opt "Plot Options"
*   window control static ml_opt 2  `r1'  173  40 blackframe
* version 1.6.5
  window control static ml_opt 2  `r1'  173 50 blackframe
    local r1 = `r1' - 3
    window control static ml_opt 5  `r1'  40   8
    local r1 = `r1' + `d1' - 3
    local r2 = `r1'
    * tics
    global ml_wtic "Number of tics"
    window control static ml_wtic   5 `r1' 47  8
    window control edit            50 `r2' 25  8 ml_tic
    * range of plot
    global ml_wmin "Plot from"
    window control static ml_wmin    90  `r1'  28   8
    window control edit              120 `r1'  17   8  ml_xmin
    global ml_wmax "to"
    window control static ml_wmax    140  `r1' 6   8
    window control edit              149  `r1' 17   8  ml_xmax
    * connect if p>
    local r1 = `r1' + `d1'
    local r2 = `r1'
    global ml_wpgt "Connect if p>="
    window control static ml_wpgt   5 `r1' 42  8
    window control edit            50 `r2' 25  8 ml_pval
    * set base category
    global ml_wb "Base category"
    window control static ml_wb    90 `r1' 45  8
    window control edit           137 `r2' 28  8 ml_bcat
    * pack odds and use variable labels
    local r1 = `r1' + `d1' - 2
    window control check "Pack odds ratio plot"  5  `r1'   80 10 ml_pack
    window control check "Use variable labels"   90  `r1'  75 10 ml_vlbl
    * pack odds and use variable labels
    local r1 = `r1' + `d1' - 2
    window control check "Use category values for plot symbols"  5  `r1' 180 10 ml_valu

*=> Buttons for odds and ends
    local r1 = `r1' + 15
    window control button "Exit"      2  `r1' 40 13 ml_ex
    global ml_ex "exit 3000"
    window control button "Help"      46 `r1' 40 13 ml_help
    global ml_help "_mlhlp"
    window control button "Print"     90 `r1' 83 13 ml_print
    global ml_print "_mlprnt"

*   window dialog "Multinomial Logit Plots" 10 10 180 192
*   window dialog "Multinomial Logit Plots" 230 80 180 192
* version 1.6.5
  window dialog "Multinomial Logit Plots" 230 80 180 202

end

program define _mlplot
    if "`1'" ~= "" {
        global ml_ordc = `1'
    }

*=> construct list of vars from the scombo boxes 1 through 6
    local i = 1
    local varlst ""
    local stdlst ""
    while `i' < 7 {
        * type of plot: unstd, std, or 0 to 1
        local tmp "ml_v`i'r"
        * name of variable
        local tmpnm "ml_v`i'"
        if $`tmp'==1 {
            local varlst "`varlst' $`tmpnm'"
            local stdlst "`stdlst'u"
        }
        if $`tmp'==2 {
            local varlst "`varlst' $`tmpnm'"
            local stdlst "`stdlst's"
        }
        if $`tmp'==3 {
            local varlst "`varlst' $`tmpnm'"
            local stdlst "`stdlst'0"
        }
        * if type is not 1, 2, or 3, it is not plotted
        local i = `i' + 1
    }

*=> build options to pass to mlogplot
    local opts "std(`stdlst')"
    if "$ml_bcat"~="" {
        local opts "`opts' b($ml_bcat)"
    }
    local opts "`opts' p($ml_pval)"
    if "$ml_xmin"~="min" {
        local opts "`opts' min($ml_xmin)"
    }
    if "$ml_xmax"~="max" {
        local opts "`opts' max($ml_xmax)"
    }
    if "$ml_lbl" ~= "" {
        local opts "`opts' note($ml_lbl)"
    }
    if $ml_ordc>=2 {
        local opts "`opts' or"
    }
    if $ml_ordc~=2 {
        local opts "`opts' dc"
    }
    if $ml_pack==1 {
        local opts "`opts' packed"
    }
    if $ml_vlbl==1 {
        local opts "`opts' labels"
    }
* 1.6.5
    if $ml_valu==1 {
        local opts "`opts' values"
    }

    local opts "`opts' ntics($ml_tic)"
    local opts "`opts' $mlopts"
    di in white ". mlogplot`varlst', `opts'"
    mlogplot `varlst',`opts'
*    if $PE_mlerr==1 {
*        exit 3000
*    }
end

program define _mlinit

*=> set radio buttons
    local i = 1
    while `i' < 6 {
        global ml_v`i'r = 1
        local i = `i' + 1
    }

*=> defaults for check box
    global ml_rng = 1       /* use observed range */
    global ml_ordc = 1      /* DC plot */
    global ml_pack = 0      /* don't pack plot */
    global ml_vlbl = 0      /* don't plot value labels */
* 1.6.5
global ml_valu = 0      /* don't plot value labels */
    global ml_pval = .1     /* connect if p> */
    global ml_tic = 9
    global ml_lbl ""
    global ml_xmin "min"    /* plot from min to max */
    global ml_xmax "max"

 *=> get b from logit
    version 5.0
    mat ml_b = get(_b)
    version 6.0
    global ml_nvars = colsof(ml_b) - 1

*=> get names of variables
    global ml_rhsnm : colnames(ml_b)
    global ml_nvar : word count $ml_rhsnm
    global ml_nvar = $ml_nvar - 1
    global ml_lastv = 1
    global ml_lastv = 0
    _mlgetv
end

* get the names of variables to fill in the scombo boxes
program define _mlgetv
    * get number of last variable in box; 0 if none plotted before.
    * if this is called from Next 6, this will not be 0.
    local k = $ml_lastv
    local i = 1
    * loop through up to 6 new variables
    while `i' < 7 {
        local k = `k' + 1
        * if exceed max number of vars, do fill remaining scombo boxes
        if `k' > $ml_nvar {
            * do not plot
            global ml_v`i'r = 4
            * no name
            global ml_v`i' ""
        }
        * else, get next in list of variables
        else {
            global ml_v`i' : word `k' of $ml_rhsnm
            local tmp "ml_v`i'"
            _pedum $`tmp'
            global ml_v`i'r = 1
            if r(dummy) == 1 { global ml_v`i'r = 3 }
        }
        local i = `i' + 1
    }
    * ok that this can be larger than n vars in model
    global ml_lastv = $ml_lastv + 6
end

program define _mlnxt6
    * if last var from _mlgetv > n vars in model, reset
    if $ml_lastv > $ml_nvar {
        global ml_lastv = 0
    }
    _mlgetv
    _mlplot
end

program define _mlprnt
    gphprint,nologo
end

program define _mlhlp
    help mlogview
end