*! version 1.1.1  01dec2006
* roc
program define clfit
	version 6.0
	if "`e(cmd)'" != "clogit" {
		noi di in red "results of clogit not found"
		exit 301
	}
	tempvar mvar touse
 	qui gen `touse' = 0
	qui replace `touse' = 1 if e(sample)
	local group "`e(group)'"
	local dep "`e(depvar)'"
	qui egen `mvar' = sum(`dep') if `touse', by(`group')
	cap asser `mvar' <= 1 if e(sample)
	if _rc != 0 {
		di in red "clfit is for 1-M matched data only"
		exit 459
	}
	syntax [, DBeta(string) DX2(string) HAT(string) GDBeta(string) /*
	*/ GDX2(string) Graph T2title(string) Symbol(string) XLAbel(string) /*
	*/ XLIne(string) *]

					/* weight, only fw allowed */
	local wtype `"`e(wtype)'"'
	local wexp `"`e(wexp)'"'
	if "`wtype'" == "iweight" {
		di in red "iweight not allowed"
		exit 101
	}
	if "`wexp'" != "" {
		local weight : subinstr local wexp "=" ""
	}

					/* new variables */
	if `"`dx2'"' == "" {
		local dx2 "_dx2"
	}
	confirm new var `dx2'
	if `"`dbeta'"' == "" {
		local dbeta "_dbeta"
	}
	confirm new var `dbeta'
	if `"`hat'"' == "" {
		local hat "_hat"
	}
	confirm new var `hat'
	if `"`gdbeta'"' == "" {
		local gdbeta "_gdbeta"
	}
	confirm new var `gdbeta'
	if `"`gdx2'"' == "" {
		local gdx2 "_gdx2"
	}
	confirm new var `gdx2'
					/* get ind varlist */
	tempname B
	mat `B' = e(b)
	local ind : colnames `B'

					/* calculation */
	tempvar xb z pr
	qui predict double `xb', xb
	qui predict double `pr'
	qui gen double `z' = `xb' + (`dep' - `pr')/`pr'
	tempvar wght
	if "`weight'" != "" {
		qui gen double `wght' = `weight'*`pr'
	}
	else qui gen double `wght' = `pr'

					/* centralize */
	local group `e(group)'
	tokenize `ind'
	local indc
	while "`1'" != "" {
		tempvar xm xc
		sort `group'
		qui by `group': gen `xm' = cond(_n==_N, sum(`wght'*`1'),.)
		qui by `group': replace `xm'=`xm'[_N] if `xm' == .
		qui by `group': gen `xc'= `1' - `xm'
		local indc `indc' `xc'
		mac shift
	}
	tempname hold0
	estimate hold `hold0'
	cap noi {
		qui regress `z' `indc' [weight=`wght'] if `touse', noc
	 	qui predict double `hat', h
	}
	estimate unhold `hold0'
	if _rc != 0 {
		exit _rc
	}
	qui sum `wght'
	qui replace `hat' = `hat'*`wght'/r(mean)
	qui gen double `dx2' = ((`dep' - `pr')/sqrt(`pr'*(1-`hat')))^2 /*
	*/ if e(sample)
	gen double `dbeta' = `dx2'*(`hat'/(1-`hat')) if e(sample)
	qui egen double `gdx2' = sum(`dx2') if `touse', by(`group')
	qui egen double `gdbeta' = sum(`dbeta') if `touse', by(`group')
	label var `dx2' "Lack of fit diagnostic"
	label var `dbeta' "Delta-Beta influence statistics"
	label var `hat' "Leverage"
	label var `gdx2' "Lack of fit diagnostic for each group"
	label var `gdbeta' "Delta-Beta influence statstics for each group"

					/* graph */
	if "`graph'" != "" {
		if `"`symbol'"' == "" { local symbol "o" }
		if `"`xlabel'"' == "" { local xlabel `"0,.25,.5,.75,1"' }
                if `"`xline'"'  == `""' { local xline  `".25,.5,.75"' }

		graph `dbeta' `pr' if e(sample), s(`symbol') border /*
		*/	t2(`"`t2title'"')  xlabel(`xlabel') /*
		*/	 xline(`xline') `options'
		noi di in gr _n `"Clogit model for `dep'"' _n(2) /*
		*/	"number of observations= " in yel %8.0f e(N)
	}
end