capture program drop kgv
program kgv,rclass
syntax varlist, categ(varlist) [KGVBoxplots KGVGroupboxplots]
	
local i = 1
local j = 1	
local k = 0
local max = 0
local a : word count `categ'
local nb:word count `varlist'

di as result "{hline}"
di "{bf:Known-groups validity}"
di as result "{hline}"
di
	
foreach sco in `varlist' {
	foreach cat in `categ' {
		local nblev = 0
		local maxlen`j' = 0
		qui anova `sco' `cat'
		local p`i'_`j' =  Ftail(e(df_m), e(df_r), e(F))
		qui levelsof `cat', local(levels) 
		local lbe : value label `cat'
			
		foreach l of local levels {
			qui count if `sco' !=. & `cat' == `l'
			local `++k'
			local eff`i'_`j'_`k' = r(N)
			
			if "`lbe'" != "" {
				local ll`j'_`k' : label `lbe' `l'
				local len = length("`ll`j'_`k''")
				if `len' > 10 {
					local c = substr("`ll`j'_`k''",1,9)
					local d = substr("`ll`j'_`k''",-1,1)
					local ll`j'_`k' "`c'" "~" "`d'" 
				}
				local w = length("`ll`j'_`k''")
				if `w' > `maxlen`j'' local maxlen`j' = `w'
			}
			else {
				local ll`j'_`k' = `l'
				local len = length("`ll`j'_`k''")
				if `len' > 10 {
					local c = substr("`ll`j'_`k''",1,9)
					local d = substr("`ll`j'_`k''",-1,1)
					local ll`j'_`k' "`c'" "~" "`d'" 
				}
				local w = length("`ll`j'_`k''")
				if `w' > `maxlen`j'' local maxlen`j' = `w'
			}
			qui su `sco' if `cat' == `l'
			local m`i'_`j'_`k' = r(mean)
			local s`i'_`j'_`k' = r(sd)
			local nblev = `nblev' + 1
		}
		if `nblev' > `max' local max = `nblev'
		local `++j'
		local k = 0
	}
	local `++i'
	local j = 1
}


/* coupure noms des scores */
/*
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 s`i' "`c'" "~" "`d'" 
	}
	else local s`i' = "`s'"
	local sc `sc' `s`i''
	local `++i'
}
*/
local i = 1
foreach s in `varlist' {
	local s`i' = abbrev("`s'",7)
	local sc `sc' `s`i''
	local `++i'
}

/*
local max = 0
foreach s in `scorename' {
	local len = length("`s'")
	if `len' > `max' local max = `len'
}
*/

local maxs = 0
forvalues j=1/`nb' {
	local len`j' = length("`s`j''")
	if `len`j'' > `maxs' local maxs = `len`j''
}

local i = 1
local k = 0
local j = 2

foreach cat in `categ'{
	local `++k'
	tokenize `categ'
	local c`k' = "``i'' ``j''"
	local i = `i' + 2 
	local j = `j' + 2 
}


local d = 1
local f = 2
forvalues h = 1/`a' {
	if `f' > `a' local f = `f'-1
	local j = 1
	local col =  `maxs'+6
	foreach cat in `c`h'' {
		di _col(`col') "{bf:`cat'}" _c
		local col = `col' + `maxlen`j'' + 5 + 40
		local `++j'
	}
	di

	local j = `d'
	local col = `maxs'+6
	foreach cat in `c`h'' {
		di _col(`=`col'+`maxlen`j''+5') "{bf:      mean    }" _c
		di "{bf:standard    }" _c
		di "{bf:p-value}" _c
		    
		local col = `col' + `maxlen`j'' + 5 + 40
		local `++j'
	}
	di
	
	local j = `d'
	local col = `maxs'+6

	foreach cat in `c`h'' {
		di _col(`=`col'+`maxlen`j''+6') "{bf:             error}"_c 
		local col = `col' + `maxlen`j'' + 5 + 40
		local `++j'
	}
	
	
	di
	di
	local i = 1
	local col = `maxs'+6

	forvalues g = 1/`nb' {
		
		di "{bf:`s`g''}" _c
		forvalues k = 1/`max' {
			forvalues j = `d'/`f' {
			
				di _col(`col') "{bf:`ll`j'_`k''} " _c
				if "`eff`i'_`j'_`k''" != "" di as text "(n=`eff`i'_`j'_`k'')" _c
				local m : di %6.2f `m`i'_`j'_`k''
				di _col(`=`col'+`maxlen`j''+10') "{text:`m'}  " _c
					
				local s : di %8.2f `s`i'_`j'_`k''
				di "{text: `s'}  " _c
					
				if `k' == 1 {
					local p : di %8.3f `p`i'_`j''
					di _col(`=`col'+31') "{text:`p'}  " _c
				}
				local col = `col' + `maxlen`j'' + 5 + 40
			}
			di
			local col = `maxs'+6
		
		}
		di 
		
	local `++i'

	}
	local d = `d'+2
	local f = `f'+2
	if `d' > `a' continue, break
	di
}

if "`kgvboxplots'" != "" {
	*qui set autotabgraphs on
	if "`kgvgroupboxplots'" != "" {
		foreach c in `categ' {
			foreach s in `varlist' {
				graph box `s', over(`c') name("`s'_`c'",replace) b1title("`c'") nodraw
				local g `g' `s'_`c' 
			}
		}
		gr combine `g', name(Known_groups_validity,replace)
	}
	else {
		foreach c in `categ' {
			foreach s in `varlist' {
				graph box `s', over(`c') name("`s'_`c'",replace) b1title("`c'")
				local g `g' `s'_`c' 
			}
		}
	}
}
end
*kgv HA-MOC, categ(radio chim) //kgvboxplots kgvgroupboxplots // radio_01 etud_01 actu_01)
