*! version 1.0.0  18Oct2016
program mypoisson6_estat, rclass
    version 14.2

    if "`e(cmd)'" != "mypoisson6" {
        error 301
    }

    gettoken subcmd rest : 0, parse(" ,")
    if "`subcmd'"=="mylinktest" {
        tempname eresults
        tempvar  xb xb2

        local depvar = e(depvar)
        predict double `xb' , xb
        generate double `xb2' = `xb'^2
        nobreak {
            _estimates hold `eresults'
            mylinktestwork `depvar' `xb' `xb2'
	    local chi2 = e(lt_chi2)
	    local df   = e(lt_df)
	    local p    = e(lt_p)
            _estimates unhold `eresults'
        }
	return clear
	return scalar chi2 = `chi2'
	return scalar df   = `df'
	return scalar p    = `p'
    }
    else {
        estat_default `0'
        return add
    }
end

program mylinktestwork, eclass

    syntax varlist(min=3 max=3)
    tempvar  b V

    quietly mypoisson6 `varlist'
    matrix `b' = e(b)
    matrix colnames `b' =  _hat _hatsq _cons
    ereturn repost  b = `b' , rename
    ereturn display
    quietly test _hatsq
    local chi2 = r(chi2)
    local df   = r(df)
    local p    = r(p)
    ereturn scalar lt_chi2 = `chi2'
    ereturn scalar lt_df   = `df'
    ereturn scalar lt_p    = `p'
end