capture program drop repet
program repet,rclass
syntax varlist [in], t2(varlist) PARTition(numlist integer >0) calcmethod(string) [SCOrename(string) KAPpa ICKAPpa(integer 0)]
preserve

local nbvars : word count `varlist'

if "`in'"!="" {
   qui keep `in'
}

if `ickappa' <= 0 {
	local ickappa = ""
}

/*
if "`ickappa'" != "" & "`kappa'" == "" {
	di "{it:The ickappa option is ignored}"
}
*/

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
}

/*
qui tab `id', nofreq
local u = r(r)
if `u' != _N {
	di in red "The variable `ident' has not unique values"
	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
	}
}
else {
	local name
	local nname
	forvalues i = 1/`P' {
		local name "Dim`i'"
		local nname `nname' `name'
	}
local scorename = "`nname'"
}

local a:word count `varlist'
local b:word count `t2'
if `a' != `b' di in red "The number of items is not the same in t1 and t2"

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

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

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

local decit = `maxit' + 4
local colit = `decit'



di as result "{hline}"
di as result "{bf:Reproducibility}"
di as result "{hline}"
di

foreach sco in `scorename' {
		*local t = "`sco'bis"
		tempname s
		local scorename2 `scorename2' `s'
}
	
qui calcscore `t2', scorename(`scorename2') partition(`partition') calcmethod(`calcmethod')

local i = 1
foreach var in `varlist' {
	tokenize `t2'
	qui kap `var' ``i''
	local k`i' = r(kappa)
	if "`ickappa'" != "" {
		qui kapci `var' ``i'', reps(`ickappa')
		local lbk`i' = r(lb_bc)
		local ubk`i' = r(ub_bc)
	}
	local `++i'
}

local i = 1
foreach s in `scorename' {
	tokenize `scorename2'
	tempname score id temps
	qui gen `id' = _n
	qui gen `score'_1 = `s'
	qui gen `score'_2 = ``i'' if ``i''!=.
	qui reshape long `score'_, i(`id') j(`temps')
	qui icc `score'_ `id'
	local n`i' = r(N_target)
	local icc`i' = r(icc_i)
	local lb`i' = r(icc_i_lb)
	local ub`i' = r(icc_i_ub)
	*qui drop `score' `temps' `id'
	qui sort `id'
	qui duplicates drop `id', force
	local `++i'
}

tokenize `sc'
local max = length("dimension")

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

local dec = `max' + 5

local i = 1
local j = 1
local y = 1
di "{bf:Dimension}" _c
di _col(`=`dec'+2') "{bf:n}" _c
local col = `dec'+6
di _col(`col') "{bf:Item}" _c
local col = `col'+`decit'

if "`kappa'" != "" {
	di _col(`col') "{bf:Kappa}" _c
	local col = `col'+10
	if "`ickappa'" != "" {
		di _col(`col') "{bf:95% CI for Kappa}" _c
		local col = `col'+20	
	}
	
}

di _col(`=`col'+2') "{bf:ICC}" _c
local col = `col'+9
di _col(`col') "{bf:95% CI for ICC}"

local zz = 0
foreach var in `varlist' {
	qui levelsof `var', local(levels)
	local z : word count `levels'
	if `z' > 2 local zz = 1
}

if "`kappa'" != "" & "`ickappa'" != "" & `zz' == 1 {
	local col = `dec'+`decit'+16
	di _col(`col') "{bf:(bootstrapped)}"
}


if "`ickappa'" != "" local h = `dec'+6+`decit'+10+8+21+12+1
else if "`kappa'" != "" local h = `dec'+6+`decit'+10+8+21+12-20
else local h = `dec'+6+`decit'+10+8+21+12-30
di "{hline `h'}"

local i = 1
foreach p in `partition' {
	tokenize `sc'
	di "{bf:``i''}" _c
	di _col(`dec') "{text:`n`i''}" _c
	if `j' == 1 local s = `p'
	else local s = `s' +`p'
	local col = `dec'+6
	di _col(`col') "{text:`var`y''}" _c 
	
	if "`kappa'" != "" {
		local k : di %5.2f `k`y''
		local col = `col'+`decit'
		di _col(`col') "{text:`k'}" _c
		if "`ickappa'" != "" {
			local lbk : di %5.2f `lbk`i''
			local ubk : di %5.2f `ubk`i''
			local col = `col'+11
			di _col(`col') "{text:[`lbk' ; `ubk']}" _c
			local col = `decit'+50
		}
		
	else local col = `decit'+30	
	}
	else local col = `decit'+20
		
	*local col = `decit'+30
	local icc : di %5.2f `icc`i''
	di _col(`col') "{text:`icc'}" _c
	local lb : di %5.2f `lb`i''
	local ub : di %5.2f `ub`i''
	local col = `col'+8
	di _col(`col')"{text:[`lb' ; `ub']}"
	local w = `y'+1
	
	forvalues z = `w'/`s' {
		local col = `dec'+6
		di _col(`col') "{text:`var`z''}" _c
		if "`kappa'" != "" {
			local k : di %5.2f `k`z''
			local col = `col'+`decit'
			di _col(`col') "{text:`k'}" _c
			if "`ickappa'" != "" {
				local lbk : di %5.2f `lbk`z''
				local ubk : di %5.2f `ubk`z''
				local col = `col'+11
				di _col(`col')"{text:[`lbk' ; `ubk']}"
			}
			else di
			
		}
		else di
	}
	local `i++' 
	local `j++' 
	local y = `s'+1
	di
}
end

*repet ioc1-ioc37, t2(ptgi1-peur16) partition(4 4 7 3 3 4 7 5) scorename(HA PSE W BCC AC AE LI MOC) kappa ickappa(20)
*repet sf36_3q_intenses sf36_3q_moderees sf36_3q_soulever sf36_3q_etages sf36_3q_etage sf36_3q_pencher sf36_3q_15km sf36_3q_500m sf36_3q_100m sf36_3q_douche sf36_4q_limite_temps_travail sf36_4q_moins_choses sf36_4q_type_travail sf36_4q_effort , t2(sf36_3q_intenses_v5 sf36_3q_moderees_v5 sf36_3q_soulever_v5 sf36_3q_etages_v5 sf36_3q_etage_v5 sf36_3q_pencher_v5 sf36_3q_15km_v5 sf36_3q_500m_v5 sf36_3q_100m_v5 sf36_3q_douche_v5 sf36_4q_limite_temps_travail_v5 sf36_4q_moins_choses_v5 sf36_4q_type_travail_v5 sf36_4q_effort_v5 ) partition(10 4)
