program define senspescore, rclass
version 8
syntax varlist(min=1 max=1 numeric)  [, Group(varname) Threshold(string) INVerse]

tempfile senspescorefile
qui save `senspescorefile', replace

qui sort `group'
tempname p0 p1 n0 n1 q0
qui count
local nind=r(N)
qui gen `n0'=.
qui gen `n1'=.
qui gen `p0'=.
qui gen `p1'=.

local l0=0
local l1=1

if "`inverse'"=="" {
   local m0="<"
   local m1=">"
}
else {
   local m0=">"
   local m1="<"
}
qui count if `group'==`l0'
local group0=r(N)
qui count if `group'==`l1'
local group1=r(N)


forvalues i=1/`nind' {
   qui count if `varlist'`m0'`varlist'[`i']&`group'==`l0'
   qui replace `n0'=`r(N)' in `i'
   qui count if `varlist'`m1'`varlist'[`i']&`group'==`l1'
   qui replace `n1'=`r(N)' in `i'
   qui replace `p1'=`n`l1''/`group`l1'' in `i'
   qui replace `p0'=`n`l0''/`group`l0'' in `i'
}
tempname diff min
if "`threshold'"!="" {
   qui gen `diff'=abs(`varlist'-`threshold')
   qui sort `diff'
   qui gen `min'=1 in 1
}
sort `varlist'

label variable `n0' "N bien classées groupe `l0'"
label variable `n1' "N bien classées groupe `l1'"
label variable `p0' "Specificity"
label variable `p1' "Sensibility"
twoway line `n0' `n1' `varlist',name(n,replace) xline(`threshold')
twoway line `p0' `p1' `varlist',name(p,replace) xline(`threshold')
qui gen `q0'=1-`p0'
label variable `q0' "1-Specificity"
local scatter
if "`threshold'"!="" {
   local scatter "(scatter `p1' `q0' if `min'==1)"
}
twoway (line `p1' `q0') (line `q0' `q0') `scatter'  ,name(roc,replace) legend(off)




use `senspescorefile', clear
end