capture program drop convdiv
program convdiv
syntax varlist, PARTition(numlist integer >0) [SCOrename(string) TCONVdiv(real 0.4) convdivboxplots]
preserve

qui set autotabgraphs on

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 119
}

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 119
	}
}

qui detect `varlist', partition(`partition')

matrix A = r(Corrrestscores)
matrix B = r(Corrscores)

local i = 1
local y = 1

foreach x in `partition' {
	if `i' == 1 local s = `x'
	else local s = `s' +`x'

	forvalues z = `y'/`s' {
		matrix B[`z',`i'] = A[`z',`i']
	}
	local `i++'
	local y = `s'+1
}

if "`scorename'"!="" {
	matrix colnames B = `scorename'
}
else {
	local name
	local nname
	forvalues i = 1/`P' {
		local name "Dim`i'"
		local nname `nname' `name'
	}
local scorename = "`nname'"
matrix colnames B = `scorename'
}

/* coupure noms des scores */
/*
local i = 1
foreach s in `scorename' {
	local len = length("`s'")
	if `len' > 5 {
		local c = substr("`s'",1,4)
		local d = substr("`s'",-1,1)
		local s`i' "`c'" "~" "`d'"
	}
	else local s`i' = "`s'"
	local sc `sc' `s`i''
	local `++i'
}
*/

/* coupure noms des items */
/*
local i = 1
foreach s in `varlist' {
	local len = length("`s'")
	if `len' > 10 {
		local c = substr("`s'",1,9)
		local d = substr("`s'",-1,1)
		local var`i' "`c'" "~" "`d'"
	}
	else local var`i' = "`s'"
	local `++i'
}
*/

local i = 1
foreach v in `varlist' {
	local var`i' = abbrev("`v'",10)
	local `++i'
}

local i = 1
foreach s in `scorename' {
	local s`i' = abbrev("`s'",7)
	local sc `sc' `s`i''
	local `++i'
}

di as result "{hline}"
di "{bf:Correlation matrix}"
di "{hline}"
di

/*
local i = 1
foreach x in `varlist' {
	local var`i' = "`x'"
	local `++i'
}
*/
 /*
tokenize `sc'
local max = 3

forvalues j=1/`P' {
	local len`j' = length("`s`j''")
	if `len`j'' > `max' local max = `len`j''
}


local maxit = 1

forvalues i=1/`nbvars' {
	local len = length("`var`i''")
	if `len' > `maxit' local maxit = `len'
}
*/
local dec = 10
local col = `dec'

local decit = 14
local colit = `decit'


local col1 = `decit'
forvalues i=1/`P' {
	di _col(`col1') "{bf:`s`i''}" _c
	local col1 = `col1' + `dec'
}

di
local h = (`P'-1)*`dec'+`decit'+4
di "{hline `h'}"

local i = 1
local j = 1
local y = 1

foreach p in `partition' {
	if `j' == 1 local s = `p'
	else local s = `s' +`p'

	forvalues z = `y'/`s' {
		di as text "{bf:`var`z''}" _c
		local col = `decit'-1

		local dd = `z' // [counting cptdiv (one per item)]

		forvalues k = 1/`P' {

			local t = B[`z',`k']
			local t : di %6.3f `t'
			if `k' == `i' {
				if `t' < `tconvdiv' {
					di in red _col(`col') "{bf:`t'}" _c
					local cptconv = `cptconv'+1
					local col = `col' + `dec'
				}
				else {
					di _col(`col') "{bf:`t'}" _c
					local col = `col' + `dec'
				}
			}
			else {

				if B[`z',`k'] > B[`z',`i'] {
					di in red _col(`col') "`t'" _c
					if `dd' == `z' local cptdiv = `cptdiv'+1 // [one per item]
					local dd = 0
					local col = `col' + `dec'
				}
				else {
					di as text _col(`col') "{text:`t'}"_c
					local col = `col' + `dec'
				}
			}
		}
	di
	}
	di "{dup `h':-}"
	local `i++'
	local `j++'
	local y = `s'+1
}

local y = 1
local h = 1
local np : word count `partition'

foreach p in `partition' {
	if `h' == 1 local s = `p'
	else local s = `s' +`p'

	forvalues j = 1/`np' {
		mat C_`h'_`j' = B[`y'..`s',`j']

		tempvar tp_`h'_`j'
        mat colnames C_`h'_`j' = `tp_`h'_`j''

		svmat C_`h'_`j', names(col)
		*rename C_`h'_`j'
		*mat li C_`h'_`j'
	}

	local `++h'
	local y = `s'+1
}

if "`convdivboxplots'" != "" {
	forvalues h = 1/`np' {
		tokenize `scorename'
		local call = ""
		local callbox = ""
		local callleg = ""

		forvalues j = 1/`np' {
			*rename C_`h'_`j' _``j''
			*di "`tp_`h'_`j''"
			local call `call' /*_``j''*/ `tp_`h'_`j''
			*if `h' == `j' local color = "blue"
			*else local color = ""
			local callbox `callbox' box(`j',fcolor(`color') lcolor(`color')) marker(`j', mcolor(`color'))

			local lab = "``j''"
			local lab = `"`lab'"'
			local callleg `callleg' `j' "`lab'"
			*di `"`callleg'"'
			*di "`call'"

		}
		graph box `call', name("Conv_div_``h''",replace) `callbox' legend(order(`"`callleg'"') stack rows(1) size(small)) title(Correlations between items of ``h'' and dimensions) yline(`tconvdiv', lpattern(dot) lcolor(black))
		qui set autotabgraphs on
		*drop `call'
	}
}

/*
foreach var of varlist ioc1 ioc2 ioc3 {
    loc varlab `""`:var l `var''""'
    loc varlabs `"`varlabs'`varlab'"'
	di "`varlab'"
}
*/

/*
svmat B

local y = 1
local h = 1
qui gen d = 0
foreach p in `partition' {
	if `h' == 1 local s = `p'
	else local s = `s' +`p'

	replace d = `h' in `y'/`s'

	local `++h'
	local y = `s'+1
}

twoway (scatter B1 d)
*/
local t : di %5.3f `tconvdiv'
local p1 = (`nbvars'-`cptconv')/`nbvars'*100
local p1 : di %4.1f `p1'
local p2 = (`nbvars'-`cptdiv')/`nbvars'*100
local p2 : di %4.1f `p2'

di
di as result "Convergent validity:" _c
di as text " `=`nbvars'-`cptconv''/`nbvars' items (`p1'%) have a correlation coefficient with the score of "
di _col(22) "their own dimension greater than `t'"
di
di as result "Divergent validity:" _c
di as text "  `=`nbvars'-`cptdiv''/`nbvars' items (`p2'%) have a correlation coefficient with the score"
di _col(22) "of their own dimension greater than those computed with other scores."


end

*convdiv ioc1-ioc37, partition(4 4 7 3 3 4 7 5) scorename(Hddfdfdffda PSE W BCC Afdfdfererdfc AE LI MOC) tconvdiv(0.4) // convdivboxplots
*convdiv x1-x40, partition(5 5 5 5 5 5 5 5) scorename(Hdfda PSE W BCC Afdfdfererdfc AE LI MOC) tconvdiv(0.4) // convdivboxplots