capture program drop rel
program rel,rclass
syntax varlist, PARTition(numlist integer >0) [SCOrename(string) Alpha(real 0.7) Delta(real 0.9) h(real 0.3) HJmin(real 0.3)]

local C = 0
foreach z in `partition' {
	local C = `C' + `z'
}
	
local nbvars : word count `varlist'

if `C' != `nbvars' {
	di in red "The sum of the numbers in the partition option is different from the number of variables precised in varlist"
	exit
}

local P:word count `partition'
if "`scorename'" !="" {
	local S:word count `scorename'
	if `P'!=`S' {
		di in red "The number of score names given is different from the number of dimensions in the partition option" 
		exit
	}
}
di as result "{hline}"
di "{bf:Reliability}" 
di as result "{hline}"
di

local y = 1
local nbitems = 0
matrix aa = J(`P',4,.)

foreach z in `partition' {
	local nbitems = `nbitems' + `z'
}

local i = 1 
foreach x in `varlist' {
	local var`i' = "`x'"
	local `++i'
}

matrix d = J(`nbitems',2,.)

local i = 1
foreach x in `partition' {
	
	if `i' == 1 local s = `x'
	else local s = `s' +`x'
	
	local liste = ""
	forvalues w = `y'/`s' {
		local liste `liste' `var`w'' 
	}
	
	*qui count if !=.
	
	capture qui alpha `liste', asi item std
	local al`i' = r(alpha)
	local n`i' = e(N)
	
	capture qui loevh `liste', pairwise 
	local h`i' = r(loevH)
	mat a = r(Obs)
	*local n`i' = a[1,1]
	matrix c = r(loevHj)	
	matrix ct = c'
		
	
	local lister = ""
	forvalues w = `y'/`s' {
		tempvar z
		qui gen `z' = round(`var`w'')
		local lister `lister' `z' 
	}
	
	capture qui delta `lister'
	local delt`i' = r(delta)
	
		
	// on remplit d avec les valeurs de ct
	local k = 0
	forvalues j = `y'/`s' {
		local k = `k'+1
		matrix d[`j',1] = ct[`k',1]
		matrix d[`j',2] = `i'
	}
	
	matrix aa [`i',1] = `al`i''
	matrix aa [`i',2] = `delt`i''
	matrix aa [`i',3] = `h`i''
	
	local `i++' 
	local y = `s'+1
}

matrix rownames d = `varlist'
local i = 1
local y = 1

foreach x in `partition' {
	if `i' == 1 local s = `x'
	else local s = `s' +`x'
	
	matrix C = d[`y'..`s',1.]
	local min`i' = C[1,1]
	local n : rownames C
	tokenize `n'
	local t`i' = "`1'"
	forvalues j = 1/`x' {
		local t = "``j''"
		if C[`j',1] <= `min`i'' {
			local min`i' = C[`j',1] 
			local t`i' = "``j''"
			local itmin`i' = "``j''"
		}
	}
	*di "`t`i''"
	matrix aa [`i',4] = `min`i''
	local `i++' 
	local y = `s'+1
}

matrix colnames aa = "alpha" "delta" "H" "Hj_min"

if "`scorename'"=="" {
	local i = 1
	local y = 1
	local name
	local nname
	forvalues i = 1/`P' {
		local name "Dim`i'"
		local nname `nname' `name'
	}
local scorename = "`nname'"
}	

local maxlen = 0
foreach sco in `scorename' {
	local w = length("`sco'")
	if `w' > `maxlen' local maxlen = `w'
}

local i = 1
local j = 1
local y = 1
local col = `maxlen'+8
di _col(`col') "{bf:n}" _c
local col = `col'+5
di _col(`col') "{bf:alpha}" _c
local col = `col'+8
di _col(`col') "{bf:delta}" _c
local col = `col'+11
di _col(`col') "{bf:H}" _c
local col = `col'+5
di _col(`col') "{bf:Hj_min}"
*di "{hline 41}"


foreach s in `scorename' {
	di in blue "{bf:`s'}" _c
	local col = `maxlen'+3
	local n : di %6.0f `n`i''
	di in blue _col(`col') "{text:`n'}" _c
	local col = `col'+10
	
	local a : di %5.2f `al`i''
	if `a' < `alpha' {
		di _col(`col') "{error:`a'} " _c
	}
	else di _col(`col') "{text:`a'}" _c
	
	local col = `col'+8
	local d : di %5.2f `delt`i''
	if `d' < `delta' {
		di _col(`col') "{error:`d'} " _c
	}
	else di _col(`col') "{text:`d'}" _c
	
	local col = `col'+8
	local h : di %4.2f `h`i''
	if `h' < `h' {
		di _col(`col') "{error:`h'} " _c
	}
	else di _col(`col') "{text:`h'}" _c
	
	local col = `col'+8
	local m : di %6.2f `min`i''
	if `m' < `hjmin' {
		di _col(`col') "{error:`m'} " _c
		di "{text:(item `itmin`i'')}" _c
	}
	else di _col(`col') "{text:`m'}" _c
	
	di
	local `++i'
}

end

*rel ioc1-ioc37, partition(4 4 7 3 3 4 7 5) scorename(HA PSE W BCC AC AE LI MOC)  a(0.7) d(0.9) h(0.3) hjmin(0.3)
*rel x1-x40, partition(5 5 5 5 5 5 5 5) scorename(HA PSE W BCC AC AE LI MOC)  a(0.7) d(0.9) h(0.3) hjmin(0.3)
