*! 1.0.0  17May97  Jeroen Weesie/ICS

* clogit with hausman tests for independence of irrelevant alternatives
program define iia
        version 5.0

        local varlist "ex req"
        local if "opt"
        local in "opt"
        local weight "aweight fweight"
        local options "Group(str) Code(str) *"
        parse "`*'"

        local copt "`options'"

      * scratch

        tempname b1 b2 V1 V2 est
        tempvar touse ic

      * selected sample

        mark `touse' `if' `in' [`weight'`exp']
        markout `touse' `varlist' `group'

      * verify -group- and -code-

        confirm exist var `group'
        confirm exist var `code'
        sort `group' `code'
        quietly by `group' `code' : assert _N==1
        quietly by `group' : gen byte `ic' = _n
        summ `ic', meanonly
        local mic = _result(6) /* number of categories */

      * labels for categories of -code-
      * beware: we depend here heavily on the sort order  -`group' `code'-

        local vt : type `code'
        if substr("`vt'",1,3) == "str" {    /* -code- is string variable */
           local i 1
           while `i' <= `mic' {
               local catlab = "`catlab' `c'" + `code'[`i']
               local i = `i'+1
           }
        }
        else {                          /* value labeled numeric variable */
            local vl : value label `code'
            if "`vl'" != "" {
                local i 1
                while `i' <= `mic' {
                    local c = `code'[`i']
                    local l : label `vl' `c'
                    local catlab "`catlab' `l'"
                    local i = `i'+1
                }
            }
            else {                      /* unlabeled numeric variable */
                local i 1
                while `i' <= `mic' {
                    local l =  `code'[`i']
                    local catlab = "`catlab' `l'"
                    local i = `i'+1
                }
            }
        }

      * full model

        parse "`varlist'", p(" ")
        local yvar "`1'"
        mac shift
        local xvar "`*'"

        _rmcoll `xvar'
        local xvar "$S_1"

        clogit `yvar' `xvar' if `touse' [`weight'`exp'], group(`group') `copt'

        mat `b1' = get(_b)
        mat `V1' = get(VCE)
        estimate hold `est'

        di _n in gr "Hausman's test for assumption of " _quote /*
                */ "Independence of Irrelevant Alternatives" _quote
        di _n in gr "Category   #obs    Hausman    df      p"
        di    in gr _dup(41) "-"

      * loop over the categories of -code-
        quietly {
            local i 1
            while `i' <= `mic' {
                clogit `yvar' `xvar' if `touse' & `ic'!=`i' [`weight'`exp'], group(`group') `copt'
                mat `b2' = get(_b)
                mat `V2' = get(VCE)

                * Hausman test
                hausman `b1' `V1' `b2' `V2', nodisplay zero
                local h   "$S_1"
                local df  "$S_2"

                count if `ic'!=`i' & `yvar'>0
                local nobs = _result(1)

                local clab : word `i' of `catlab'
                noi di in gr "`clab'" in ye _col(11) %5.0f `nobs' /*
                        */ _col(19) %8.2f `h' _col(30) %3.0f `df'    /*
                        */ _col(35) %7.4f chiprob(`df',`h')
                local i = `i'+1
            }
            noi di in gr _dup(41) "-"

            estimate unhold `est'
        }
end