/*
Regarder si les modèles convergent dans la partie 1
Prévoir le cas du cross loading (=> numéro des contraintes, etc)
*/

capture program drop  rsoortBP
program define rsoortBP,rclass
syntax varlist(min=2 numeric) [if] [in], PARTition(numlist) [lt(string) METHod(string) UNIFormrecalibration(varlist) NONUNIFormrecalibration(varlist) REPrioritization(varlist) noSearch CFARmsea(real -999) CFACfi(real -999) CFAOR iterate(integer 100) onlycovl Test Hierarchical sb adjust Group(varlist) trace *]
version 15

preserve

//rename *, lower

local P:word count `partition'
local S:word count `lt'

if "`lt'" != "" {
	if `P'!=`S' {
		di in red "The number of arguments in lt() must be equal to the number of arguments in partition()"
		exit 119
	}
	else {
		tokenize `lt'
		forvalues i = 1/`S' {
			local ltname`i' = "``i''"
		}
	}
}
else {
	forvalues i = 1/`P' {
		local ltname`i' = "LT`i'"
	}
}




order `varlist'

if "`trace'" != "" {
	local qui ""
}
else {
	local qui "qui"
}

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

tokenize `varlist'

local nbitems:word count `varlist'

*di "`varlist'"
local varlist
forvalues i=1/`nbitems' {
	//di "``i''"
	capture rename ``i'', lower
	local low = lower("``i''")
	local var`i' = `low'
	local varlist `varlist' `low'
}
tokenize `varlist'
*di "`varlist'"

/*
local i = 1
foreach x in `varlist' {
	local var`i' = lower("`x'")
	di "ok"
	rename `x' `var`i''
	local ++i
}
*/
local nbitems:word count `varlist'
local mod=mod(`nbitems',2)
if `mod'!=0 {
  di in red "You must enter an even number of items : the first half of the items represents the items in time 1 and the second half the items in time 2"
  error
}
local nbitems=`nbitems'/2

local nbdims:word count `partition'


local C = 0
foreach z in `partition' {
	local C = `C' + `z'
}


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



if `cfarmsea' == -999{
	local cfarmsea
}
if `cfacfi' == -999{
	local cfacfi
}

if "`method'"=="" {
   local method "ml"
}

local i = 1
local j = 0
foreach p in `partition' {
	local part`i' = `p'+`j'
	local ++i
	local j = `j'+`p'

}

if "`sb'" != "" {
	local sb = "vce(sbentler)"
}


di as result _col(20) "{hline 35}"
di as text _col(20) "Time 1" _col(40) "Time 2"
di as text _col(20) "{hline 35}"
local j = 1
forvalues i=1/`nbitems' {
	*set trace on
	if (!missing("`part`j''")) {
		if (`i' ==1|`i'==`=`part`j''+1') {
			if `i'!=1 local ++j
			di _col(20) "`ltname`j''" _col(40) "`ltname`j''"
		}
	}
	*set trace off
	di _col(20) "``i''" _col(40) "``=`i'+`nbitems'''"
}
di as result _col(20) "{hline 35}"


/**************************************************************************************************************
DIF time 1 (if "group" is option specified)
***************************************************************************************************************/


		/**************************************************************************************************************
		DIF : Model A
		***************************************************************************************************************/



if "`group'" !="" {




tokenize `varlist'

local ii = 1
local s
local stop = 0
local i = 1


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

local i = 1
foreach x in `partition' {
	local part`i' = `x'
}

//tokenize `varlist'
forvalues i=1/`nbitems' {
   local cov12b `cov12b' e.``i''*e.``=`i'+`nbitems'''
}

//di "`varlist'"

local k = 1
local i = 1
local j = 0
local pp = 0
foreach p in `partition' {
	local pp = `pp'+`p'
	while (`j'<`pp') {
		local bloca`k' `bloca`k'' `var`i''
		local blocb`k' `blocb`k'' `var`=`i'+`nbitems'''
		//di "`var`i''"
		local i = `i'+1
		local j = `j'+1
	}
	//di in red `j'
	//order `var`j''-`var`pp''
	//order `var`=`j'+`nbitems'''-`var`=`pp'+`nbitems'''


	local ++k
}

di  "`bloca1'"
di  "`bloca2'"
di  "`blocb1'"
di  "`blocb2'"



local i = 1
local j = 1
local pp = 0
foreach p in `partition' {
	local pp = `pp'+`p'
	local s `s' (L`i'_1 -> `bloca`i'')
	local cl `cl' means(L`i'_1@0)  var(L`i'_1@1)
	forvalues k = 1/`=`nbdims'-1' {
		if (`=`i'+`k''<=`nbdims') {
			local covl `covl' L`i'_1*L`=`i'+`k''_1
		}
	}

	local j = `pp'+1
	local i = `i'+1
}



local z "`s', `cl' iterate(`iterate') method(`method') `sb' group(`group') ginvariant(covex)"

di in red "DIF : model A (without search for modification indices)"

di as text "sem `z' "

sem `z'

est store modelAwo

if "`cfarmsea'"!="" {
	local stoprmsea=0
	while `stoprmsea'==0 {
		qui estat gof
		di in red "rmsea="round(`r(rmsea)',0.001)
		if r(rmsea)<`cfarmsea' {
			local stoprmsea = 1
		}
		else {
			estat mindices, showpclass(merrvar)
			tempvar rname chi df p ccovnum
			mat m = r(mindices)
			matrix m2 = m[.,1..3]
			svmat2 m2 , rnames(`rname') names(`chi' `df' `p') full
			gsort -`chi'
			local nrows = rowsof(m2)
			gen `ccovnum' = _n
			list `ccovnum' `rname' `chi' in 1/`nrows'

			local num = `ccovnum'[1]
			constraint get `num'
			local c`num' = r(contents)
			di "`c`num''"
			constraint drop `num'

			*if (même dimension) {
				local coverror = `rnames'[1]
				di "`coverror'"
			*}
			constraint 1001 [/]cov(L1_1,L2_1)#0bn.grp_v1 - [/]cov(L1_1,L2_1)#1.grp_v1 = 0

		}
	}






	stop
}










	/**************************************************************************************************************
		DIF : Model B
    ***************************************************************************************************************/

levelsof `group', local(levs)
tokenize `levs'
local length = wordcount("`levs'")
forvalues l = 1/`length' {
	local level`l' = ``l''
}
local l

local i = 1
local j = 1
local pp = 0
local s
local cl
foreach p in `partition' {
	local l = 1
	local pp = `pp'+`p'
	local s `s' (L`i'_1 -> `bloca`i'')
	local cl `cl' means(`level1':L`i'_1@0) var(`level1':L`i'_1@1)
	forvalues k = 1/`=`nbdims'-1' {
		if (`=`i'+`k''<=`nbdims') {
			local covl `covl' L`i'_1*L`=`i'+`k''_1
		}
	}

	local j = `pp'+1
	local i = `i'+1
}



/* définition des contraintes */

constraint drop _all
tokenize `varlist'

local c = 1
local i = 1
local j = 1
local pp = 0
foreach p in `partition' {
	local pp = `pp'+`p'
	while `j'<= `pp' {
		constraint `c' [``j'']`level1'bn.`group'#c.L`i'_1 - [``j'']`level2'.`group'#c.L`i'_1 = 0
		local ++j
		local ++c
	}
	local ++i
}

local i = 1
foreach v of varlist `varlist'  {
	if `i'<=`nbitems' {
		constraint `c' [``i'']`level1'bn.`group' - [``i'']`level2'bn.`group' = 0
		local ++c
		local ++i
	}
}

local i = 1
foreach v of varlist `varlist'  {
	if `i'<=`nbitems' {
		constraint `c' [/]var(e.``i'')#`level1'bn.`group' - [/]var(e.``i'')#`level2'bn.`group' = 0
		local ++c
		local ++i
	}
}



constraint list





//local z "`s', `cl' iterate(`iterate') method(`method') `sb' group(`group') ginvariant(mcoef mcons merrvar) noanchor forcenoanchor"
//local z "`s', `cl' iterate(`iterate') method(`method') `sb' group(`group') ginvariant(covex) constraints(1/`=`nbitems'*3')"
//local z "`s', `cl' iterate(`iterate') method(`method') `sb' group(`group') ginvariant(mcoef mcons merrvar ) constraints(`=`nbitems'+1'/`=`nbitems'*3') noanchor forcenoanchor"
local z "`s', `cl' iterate(`iterate') method(`method') `sb' group(`group') ginvariant(mcoef) noanchor forcenoanchor"

di in red "DIF : model B"

di as text "sem `z' constraints(`=`nbitems'+1'/`=`nbitems'*3') "

sem `z' constraints(1/`=`nbitems'*3')

est store modelB
est store modelBref

lrtest modelA modelB


if  r(p) < 0.05 {
local stop = 0

while (`stop'!=1) {

	estat ginvariant, showpclass(mcoef mcons merrvar)
	qui ret li
	matrix m = r(test)
	matrix m2 = m[.,4..6]
	tempvar number rname chi df p
	svmat2 m2, rnames(`rname') names(`chi' `df' `p') full
	gen `number' = .
	local c = 1
	forvalues i = 1(2)`=`nbitems'*2' {
		qui replace `number' = `c' in `i'
		local ++c
	}

	local c = `=`nbitems'+1'
	forvalues i = 2(2)`=`nbitems'*2' {
		qui replace `number' = `c' in `i'
		local ++c
	}

	local c = `=`nbitems'*2+1'
	forvalues i = `=`nbitems'*2+1'/`=`nbitems'*3' {
		qui replace `number' = `c' in `i'
		local ++c
	}

	qui sort `p'
	list `number' `rname' `chi' `df' `p' in 1/`=`nbitems'*3'
	//list `number' `p'
	local num = `number'[1]
	//di `num'
	constraint get `num'
	local c`num' = r(contents)
	di "`c`num''"
	constraint drop `num'
	constraint list _all

	di in red "Modèle après avoir retiré `c`num''"

	sem `z' constraints(1/`=`nbitems'*3')

	est store modelBtest
	lrtest modelBref modelBtest

	if  r(p) >= 0.05 {
		local stop = 1
	}
	else {
		est store modelBref
		di in red "donc on retire `c`num''"
	}

	*estat ginvariant, showpclass(mcoef mcons merrvar)

}

} // fin if LRT < 0.05


/*************************************************************************************************************
Model 1
**************************************************************************************************************/


stop
}  // fin if(group)




/*************
Response-Shift
*************/


/**************************************************************************************************************
Model 1
***************************************************************************************************************/
tokenize `varlist'

local ii = 1
local s
local stop = 0
local i = 1


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

local i = 1
foreach x in `partition' {
	local part`i' = `x'
}

forvalues i=1/`nbitems' {
   local cov12b `cov12b' e.``i''*e.``=`i'+`nbitems'''
}

local k = 1
local i = 1
local j = 0
local pp = 0
local cl
foreach p in `partition' {
	local pp = `pp'+`p'
	while (`j'<`pp') {
		local bloca`k' `bloca`k'' `var`i''
		local blocb`k' `blocb`k'' `var`=`i'+`nbitems'''
		//di "`var`i''"
		local i = `i'+1
		local j = `j'+1
	}
	//di in red `j'
	//order `var`j''-`var`pp''
	//order `var`=`j'+`nbitems'''-`var`=`pp'+`nbitems'''


	local ++k
}


local i = 1
local j = 1
local pp = 0
local s
foreach p in `partition' {
	local pp = `pp'+`p'
	local s `s' (L`i'_1 -> `bloca`i'') (L`i'_2 -> `blocb`i'')
	local cl `cl' means(L`i'_1@0) means(L`i'_2@0) var(L`i'_1@1) var(L`i'_2@1)
	local covl `covl' L`i'_1*L`i'_2
	forvalues k = 1/`=`nbdims'-1' {
		if (`=`i'+`k''<=`nbdims') {
			local covl `covl' L`i'_1*L`=`i'+`k''_1   L`i'_2*L`=`i'+`k''_2 // uniquement même trait latent ou même temps
			//local covl `covl' L`i'_1*L`=`i'+`k''_1     L`i'_2*L`=`i'+`k''_2    L`i'_1*L`=`i'+`k''_2
		}
	}

	local j = `pp'+1
	local i = `i'+1
}


if "`onlycovl'" != "" {
	local z "`s', covstruct(_lexogenous, diagonal) `cl' cov(`cov12b' `covl') iterate(`iterate') method(`method') `sb'"
}
else {
	local z "`s', `cl' cov(`cov12b') iterate(`iterate') method(`method') `sb'"
}


//local z1 "`s', covstruct(_lexogenous, diagonal) cov(`cov12b' `covl') iterate(`iterate') method(`method') `sb'"

`qui' di "sem `z'"

di as result "Step 1: Measurement model"
di as text "Model 1 estimation..."

qui sem `z'

//ll



qui est store model1


//local s "sem (T1->`1'-``nbitems'')(T2->``=`nbitems'+1''-``=`nbitems'*2''), var(T1@1) var(T2@1) means(T1@0) means(T2@0) method(`method') cov(`cov12b' `covs')"
//sem (T1->`1'-``nbitems'')(T2->``=`nbitems'+1''-``=`nbitems'*2''), var(T1@1) var(T2@1) means(T1@0) means(T2@0) method(`method') cov(`cov12b' `covs') iterate(`iterate') nolog

//estat gof, stat(all)


if ("`cfarmsea'"!="" | "`cfacfi'"!="") {

while (`ii' == 1 | "`e'" != "") & `stop'!=1 {
	if `ii' == 1 & ("`cfarmsea'"!="" | "`cfacfi'"!="") {
		di
		di _n "{bf:step 1:} {text:(model 1 without auto-search of modification indices)}"
	}

	if "`covs'" != "" & ("`cfarmsea'"!="" | "`cfacfi'"!=""){
		di _n "{bf:step `ii':} {text:`covsi'}"
	}
	local covsi

	/* Model 1 without covariances between errors */

	qui sem `z' cov(`covs')
	est store model1
	//qui sem (T1->`1'-``nbitems'')(T2->``=`nbitems'+1''-``=`nbitems'*2''), var(T1@1) var(T2@1) means(T1@0) means(T2@0) method(`method') cov(`cov12b' `covs') iterate(`iterate') nolog
	qui estat gof, stat(all)

	/* Looking for modification indices */

	if ("`cfarmsea'"!="" | "`cfacfi'"!="") {
		local stoprmsea = 0
		local stopcfi = 0
		if "`cfarmsea'"!="" {
			qui estat gof
			di "rmsea =" round(r(rmsea),0.001)
			if r(rmsea)<=`cfarmsea' {
				local stoprmsea = 1
			}
			//else local stoprmsea = 0
		}
		else local stoprmsea = 1
		if "`cfacfi'"!="" {
			qui estat gof
			di "cfi =" round(r(cfi),0.001)
			if r(cfi)>=`cfacfi' {
				local stopcfi = 1
			}
			//else local stopcfi = 0
		}
		else local stopcfi = 1



		/* OR */
		if "`cfaor'"!="" {
			if `stoprmsea'==1 | `stopcfi'==1 {
				local stop = 1
			}
		}
		/* AND */
		if "`cfaor'"=="" {
			if `stoprmsea'==1 & `stopcfi'==1 {
				local stop = 1
			}
		}
	}


	if ("`cfarmsea'"!="" | "`cfacfi'"!="") & `stop'!=1 {
		qui estat mindices, showpclass(mcons merrvar)
		tempname mm nomcol
		mat `mm' = r(mindices)
		//mat li `mm'

		svmat2 `mm' , r(`nomcol') full
		gsort - `mm'1

		local nrows = rowsof(`mm')

		local y = 1
		local i = 0
		//local partition `nbitems' `nbitems'
		foreach x of numlist `partition' {
			local ++i
			if `i' == 1 local s = `x'
			else local s = `s' +`x'

			forvalues w = `y'/`s' {
				local class_`var`w'' = `i'
				local class_`var`=`w'+`nbitems''' = `i'+`nbitems'
				*di "`var`w''"
				*di "`class_`var`w'''"
				*di "`var`=`w'+`nbitems'''"
				*di "`class_`var`=`w'+`nbitems''''"
			}
			local y = `s'+1
		}

		local f1b
		local f2b
		local f
		local f1
		local f2
		forvalues i = 1/`nrows' {
			local tmp3=`nomcol'[`i']
			lstrfun e, subinstr("`tmp3'","cov(","",.)
			lstrfun e, subinstr("`e'","/","",.)
			lstrfun e, subinstr("`e'",")","",.)
			lstrfun e, subinstr("`e'",":_cons","",.)
			lstrfun e, subinstr("`e'",",","*",.)

			lstrfun d, subinstr("`e'",","," ",.)
			lstrfun d, subinstr("`d'","e.","",.)
			lstrfun d, subinstr("`d'","*"," ",.)

			qui replace `nomcol' = "`d'" in `i'

			local d = `nomcol'[`i']

			//tokenize "`d'"
			local v1 = substr("`d'",1,strpos("`d'", " "))
            local v2 = substr("`d'",strpos("`d'", " ")+1,.)




			if ("`class_`v1''" != "`class_`v2''") /*| (`class_`v1'' > `nbitems') */ { /*** enlever le | si on cherche aussi au temps2 ***/
				qui replace `nomcol' = "" in `i'
			}

			local tmp3=`nomcol'[`i']


			if "`tmp3'" != "" {
				//di in red "`tmp3'"
				local e1 = substr("`tmp3'",1,strpos("`tmp3'", " ")-1)
				local e2 = substr("`tmp3'",strpos("`tmp3'", " ")+1,.)
				local ni = `nbitems'*2
				forvalues jj = 1/`ni' {
					if `jj' <= `nbitems' {

						if "`e1'" == "``jj''" {
							local f1 = "``jj''"
							local f1b = "``=`jj'+`nbitems'''"
						}
						if "`e2'" == "``jj''" {
							local f2 = "``jj''"
							local f2b = "``=`jj'+`nbitems'''"
						}
					}
					else if `jj' > `nbitems' { /*** remettre si on cherche aussi au temps2 ***/
						if "`e1'" == "``jj''" {
							local f1 = "``jj''"
							local f1b = "``=`jj'-`nbitems'''"
							//di in red "`f1b'"

						}
						if "`e2'" == "``jj''" {
							local f2 = "``jj''"
							local f2b = "``=`jj'-`nbitems'''"
						}
					}

				}
				if "`f1b'"!="" & "`f2b'"!="" {
					local f = "e.`f1b'*e.`f2b'"
					//di in red "`f' en plus de `e'"
				}


				continue, break
			}
			else {
				local e
				local f
				local f1b
				local f2b
			}
		}

		local covsi "`e' (`f') "
		local covs `covs' `e' `f'
		local f
		drop `mm'1-`nomcol'

	}

	//if e(converged) == 0 di in red "Warning : model did not converge after 50 iterations"
	local ++ii

}

}



/* Model 1 final */


//di "{bf:Model 1 (step 1):}"
//local s "sem (T1->`1'-``nbitems'')(T2->``=`nbitems'+1''-``=`nbitems'*2''), var(T1@1) var(T2@1) means(T1@0) means(T2@0) method(`method') cov(`cov12b' `covs') "
//di as text "`s'"
//di "sem `z' cov(`covs')"
local sem1 `z' cov(`covs')
//sem

//est replay model1

qui estat gof, stats(all)
local tli1=r(tli)
local cfi1=r(cfi)
local srmr1=r(srmr)
local rmsea1=r(rmsea)
local ubrmsea1=r(ub90_rmsea)
local lbrmsea1=r(lb90_rmsea)
local chi21=r(chi2_ms)
local df1=r(df_ms)
local dfc1=6*`nbitems'+1
local p1=r(p_ms)
local bic1=r(bic)

if "`sb'" != "" {
	local cmodel1 = e(sbc_ms)
	//local chi21=r(chi2_ms)
}

//di in red `chi21'

*di as text "End of step 1"


/**************************************************************************************************************
Model 2
***************************************************************************************************************/

di
if "`test'" != "" {
	di as result "Step 2: Overall test of response shift"
}
else {
	di as result "Step 2: Estimation of ""no response shift"" model (no overall test is performed because the ""test"" option was not specified)"
}
di as text "Model 2 estimation..."

local i = 1
local j = 1
local pp = 0
local s
local k = 1
local var
local cl
local covl
foreach p in `partition' {
	local pp = `pp'+`p'
	while `j'<= `pp' {
		local s `s' (L`i'_1@load`k' _cons@int`k' -> `var`j'') (L`i'_2@load`k' _cons@int`k' -> `var`=`j'+`nbitems''')
		local var `var' var(e.`var`j''@var`k') var(e.`var`=`j'+`nbitems'''@var`k')
		local ++j
		local ++k
	}
	local cl `cl' means(L`i'_1@0) var(L`i'_1@1) var(L`i'_2) means(L`i'_2)
	local covl `covl' L`i'_1*L`i'_2
	forvalues g = 1/`=`nbdims'-1' {
		if (`=`i'+`g''<=`nbdims') {
			//di in red `g'
			local covl `covl' L`i'_1*L`=`i'+`g''_1 L`i'_2*L`=`i'+`g''_2
		}
	}

	local ++i
}


local z "`s', `var' covstruct(_lexogenous, diagonal) `cl' cov(`cov12b' `covl' `covs') iterate(`iterate') method(`method') `sb'"

//di "sem `z'"
//di "model 2"

qui sem `z'
local m2 = e(cmdline)
//qui `m2' stand
est store model2
est store model2init


/*
tempname b V
matrix `b'=r(table)
matrix `V'=e(V)
//mat li `V'

local i = 1
foreach p in `partition' {
	local truechange_L`i'=`b'[1,`=`nbitems'*4+`i'']
	local Vtruechange_L`i'=`V'[`=`nbitems'*4+`i'',`=`nbitems'*4+`i'']
	//di `Vtruechange_L`i''
	local ++i
}


//estat gof
qui estat gof, stat(all)
local iter2=e(ic)
local tli2=r(tli)
local cfi2=r(cfi)
local srmr2=r(srmr)
local rmsea2=r(rmsea)
local ubrmsea2=r(ub90_rmsea)
local lbrmsea2=r(lb90_rmsea)
local pclose2=r(pclose)
local chi22=r(chi2_ms)
local df2=r(df_ms)
local dfc2=3*`nbitems'+3
local p2=r(p_ms)
local bic2=r(bic)
local convergence2=e(converged)


if "`sb'" != "" {
	local cmodel2 = e(sbc_ms)
	//local chi22=r(chi2_ms)
}


ereturn matrix b2=`b'
ereturn scalar iter2=`iter2'
ereturn scalar tli2=`tli2'
ereturn scalar cfi2=`cfi2'
ereturn scalar srmr2=`srmr2'
ereturn scalar rmsea2=`rmsea2'
ereturn scalar ubrmsea2=`ubrmsea2'
ereturn scalar lbrmsea2=`lbrmsea2'
ereturn scalar pclose2=`pclose2'
ereturn scalar chi22=`chi22'
ereturn scalar df2=`df2'
ereturn scalar dfc2=`dfc2'
ereturn scalar p2=`p2'
ereturn scalar bic2=`bic2'
ereturn scalar convergence2=`convergence2'

local i = 1
foreach p in `partition' {
	ereturn scalar truechange_L`i'=`truechange_L`i''
	ereturn scalar Vtruechange_L`i'=`Vtruechange_L`i''
	ereturn scalar setruechange_L`i'=`=sqrt(`Vtruechange_L`i'')'
	ereturn scalar ztruechange_L`i'=`=`truechange_L`i''/sqrt(`Vtruechange_L`i'')'
	ereturn scalar ptruechange_L`i'=`=2-2*normal(abs(`truechange_L`i'')/sqrt(`Vtruechange_L`i''))'
	local ++i
}
*/






/**************************************************************************************************************
Autre syntaxe
***************************************************************************************************************/

constraint drop _all
tokenize `varlist'

local all
local i = 1
local j = 1

//local moins = 0
foreach v of varlist `varlist'  {
	if `i'<=`nbitems' {
		local current [/]var(e.``i'') - [/]var(e.``=`i'+`nbitems''') = 0
		if !strpos("`all'","`current'") {
			constraint `j' [/]var(e.``i'') - [/]var(e.``=`i'+`nbitems''') = 0
			//local moins = 0
			local ++j
		}
		/*else {
			local moins = 1
		}*/
		local all `all' [/]var(e.``i'') - [/]var(e.``=`i'+`nbitems''') = 0
		local ++i
	}
}



local all
local i = 1
foreach v of varlist `varlist'  {
	if `i'<=`nbitems' {
		local current [/]var(e.``i'') - [/]var(e.``=`i'+`nbitems''') = 0
		if !strpos("`all'","`current'") {
			constraint `j' [``i'']_cons - [``=`i'+`nbitems''']_cons = 0
			local ++j
		}
		local all `all' [/]var(e.``i'') - [/]var(e.``=`i'+`nbitems''') = 0
		local ++i
	}
}


local c = `j'
local i = 1
local j = 1
local pp = 0
foreach p in `partition' {
	local pp = `pp'+`p'
	while `j'<= `pp' {
		constraint `c' [``j'']L`i'_1 - [``=`j'+`nbitems''']L`i'_2 = 0
		//local s `s' (L`i'_1@load`k' _cons@int`k' -> `var`j'') (L`i'_2@load`k' _cons@int`k' -> `var`=`j'+`nbitems''')
		local ++j
		local ++c
		//local ++k
	}
	local ++i
}

//constraint list



//di "Model 2 (step 2)" // "Model 2 (autre syntaxe)"
//di "`sem1'"
`qui' di "sem `sem1' `cl' constraints(1/`=`nbitems'*3')"
//local sem2 "`s', covstruct(_lexogenous, diagonal) cov(`cov12b' `covl') iterate(`iterate') method(`method') constraints(1/`=`nbitems'*3')"
//di "`sem2'"

set matsize 11000
qui sem `sem1' `cl' constraints(1/`=`nbitems'*3')
est store model2
est store model2init

/*
local m2 = e(cmdline)
qui `m2' stand
est store model2init
*/








if "`sb'" != "" {
	local cmodel2 = e(sbc_ms)
	//local chi22=r(chi2_ms)
}




tempname b V
matrix `b'=r(table)
matrix `V'=e(V)
//mat li `V'

local i = 1
foreach p in `partition' {
	local truechange_L`i'=`b'[1,`=`nbitems'*4+`i'']
	local Vtruechange_L`i'=`V'[`=`nbitems'*4+`i'',`=`nbitems'*4+`i'']
	local setruechange_L`i'=`=sqrt(`Vtruechange_L`i'')'
	local ztruechange_L`i'=`=`truechange_L`i''/sqrt(`Vtruechange_L`i'')'
	local ptruechange_L`i'=`=2-2*normal(abs(`truechange_L`i'')/sqrt(`Vtruechange_L`i''))'
	local ++i
}

//estat gof
qui estat gof, stat(all)
local iter2=e(ic)
local tli2=r(tli)
local cfi2=r(cfi)
local srmr2=r(srmr)
local rmsea2=r(rmsea)
local ubrmsea2=r(ub90_rmsea)
local lbrmsea2=r(lb90_rmsea)
local pclose2=r(pclose)
local chi22=r(chi2_ms)
local df2=r(df_ms)
local dfc2=3*`nbitems'+3
local p2=r(p_ms)
local bic2=r(bic)
local convergence2=e(converged)

/*
ereturn matrix b2=`b'
ereturn scalar iter2=`iter2'
ereturn scalar tli2=`tli2'
ereturn scalar cfi2=`cfi2'
ereturn scalar srmr2=`srmr2'
ereturn scalar rmsea2=`rmsea2'
ereturn scalar ubrmsea2=`ubrmsea2'
ereturn scalar lbrmsea2=`lbrmsea2'
ereturn scalar pclose2=`pclose2'
ereturn scalar chi22=`chi22'
ereturn scalar df2=`df2'
ereturn scalar dfc2=`dfc2'
ereturn scalar p2=`p2'
ereturn scalar bic2=`bic2'
ereturn scalar convergence2=`convergence2'
*/

local i = 1
foreach p in `partition' {
	return scalar truechange_L`i'_2=`truechange_L`i''
	return scalar Vtruechange_L`i'_2=`Vtruechange_L`i''
	return scalar setruechange_L`i'_2=`=sqrt(`Vtruechange_L`i'')'
	return scalar ztruechange_L`i'_2=`=`truechange_L`i''/sqrt(`Vtruechange_L`i'')'
	return scalar ptruechange_L`i'_2=`=2-2*normal(abs(`truechange_L`i'')/sqrt(`Vtruechange_L`i''))'
	local ++i
}

/*
local i = 1
foreach p in `partition' {
	di as text "Observed change (i.e. without accounting for response shift) for `ltname`i'' is " %4.3f `truechange_L`i'' " (p = " %4.3f `ptruechange_L`i'' ")"
	local ++i
}
*/

if "`test'"!="" {
	if "`sb'"=="" {
		local chi221=`chi22'-`chi21'
		local df21=`df2'-`df1'
		local p21=1-chi2(`df21',`chi221')
		local cmodel1= 1
		local cmodel2 = 1
	}
	else {
		local cd=(`df2'*`cmodel2'-`df1'*`cmodel1')/(`df2'-`df1')
		local df21=`df2'-`df1'
		local chi221=(`chi22'-`chi21')/`cd'
		local p21 = 1-chi2(`df21',`chi221')
		//local adjust = " (sb)"
	}
	//di in red `chi21'
	//di in red `chi22'

	if (`p21'>0.05) {
		local testno=" no"
	}
	else {
		//local p21 = round(`p21',0.001)
		//if `p21'<0.001 {
			//local p21 = "< 0.001"
		//}
		local testno = ""
	}

	di "{hline 51}"
	di in gr                                /*_col(39) "Test of global Response-Shift "               _col(79) "Comparison with model 1"*/
	di in gr "Models" _col(18)  "Chi-square" _col(32) "df" _col(36) "p-value" _col(48) "BIC"  /*_col(39) "Chi-square" _col(54) "df" _col(59) "p-value" *_col(79) "Chi-square" _col(94) "df" _col(99) "p-value"*/
	di "{hline 51}"
	di in gr "Model 1" _col(20) %8.2f `=`chi21'/`cmodel1'' _col(30) %4.0f `df1' _col(37) %6.4f `p1' _col(45) %7.2f `bic1'
	di in gr "Model 2" _col(20) %8.2f `=`chi22'/`cmodel2'' _col(30) %4.0f `df2' _col(37) %6.4f `p2' _col(45) %7.2f `bic2' /*_col(40) %8.2f `=abs(`chi22'-`chi21')' _col(50) %6.0f `df21' %6.4f _col(60) `=1-chi2(`df21',abs(`chi22'-`chi21'))' *_col(82) %8.2f `=`=abs(`chi22'-`chi21')'' _col(90) %6.0f `df21' _col(100) %6.4f `=1-chi2(`df21',abs(`chi22'-`chi21'))'*/
	di "{hline 51}"

	di "chi2 = " %4.2f `chi221' " (`df21')"
	di "p-value of global test = " %4.3f `p21'
	//di "=> `no' Step 3"
}
else local testno = ""

if "`testno'"=="" {

}
else {
	di as text "We skip step 3 because the overall test for response shift is non significant"
}

*di as text "End of step 2"



/*** Etape 3 ***/

forvalues i = 1/`=`nbitems'*3' {
	constraint get `i'
	local c`i' = r(contents)
	//local csave`i' = r(contents)
}

matrix numcont = J(1,`=`nbitems'*3',.)
forvalues i = 1/`=`nbitems'*3'{
	if `i'<=`nbitems' {
		local noms "`noms' NUR_``i''"
	}
	else if `i' > `nbitems' & `i'<=`=`nbitems'*2' {
		local noms "`noms' UR_``=`i'-`nbitems'''"
	}
	else if `i' > `=`nbitems'*2' {
		local noms "`noms' REP_``=`i'-2*`nbitems'''"
	}

}
matrix colnames numcont = `noms'

if "`hierarchical'" != "" & "`testno'" !=" no"{


/**************************************************************************************************************
Model 3 / Non Uniform recalibration
***************************************************************************************************************/

di
di as result "Step 3 --Non Uniform recalibration--"

timer clear 20
timer on 20
local nbsign = 0
di as text "Iteration 1 running"
local pbconv = 0
local o = 1
local k = 1
local j = 1
local stop = 0
//local nonunif
while (`j'<=`nbitems' & `stop'!=1) {
	local max = -1
	local c = 0
	local stop = 0
	timer clear 10
	timer on 10
	forvalues i = 1/`nbitems' {
		constraint get `i'
		if r(defined)== 1 {
			constraint drop `i'
			qui sem `sem1' `cl' constraints(1/`=`nbitems'*3') forcenoanchor
			//constraint list
			constraint `i' `c`i''
			//constraint list
			//di "Après avoir enlevé `c`i'' : "
			if "`adjust'" != "" {
				local nbtest = `nbitems'-`j'+1
			}
			else local nbtest = 1
			//di "nbtest = `nbtest'"
			qui eret li
			if e(converged) == 1 {
				est store model3
				if "`sb'" == "" {
					`qui' lrtest model2 model3
					if r(p) < `=0.05/`nbtest'' {
						local nbsign = `nbsign'+1
					}
					if r(chi2) > `max' & r(p) < `=0.05/`nbtest'' {
						local max = r(chi2)
						local c = `i'
					}
				}
				else {
					qui estat gof, stats(all)
					local df3=r(df_ms)
					local cmodel3 = e(sbc_ms)
					local chi23=r(chi2sb_ms)
					local cd=(`df2'*`cmodel2'-`df3'*`cmodel3')/(`df2'-`df3')
					local df23=`df2'-`df3'
					local chi223=(`chi22'-`chi23')/`cd'
					local p23 = 1-chi2(`df23',`chi223')
					//di "lr(sb) = `chi223'"
					//di "p = `p23'"
					if `p23' < `=0.05/`nbtest'' {
						local nbsign = `nbsign'+1
					}
					if `chi223' > `max' & `p23' < `=0.05/`nbtest'' {
						local max = `chi223'
						local c = `i'
					}
				}
			}
			else {
				local ++pbconv
				//`qui' di "model did not converge (`pbconv' in total)"
			}

		}
	}
	//local mmax = `max'
	//di `mmax'
	//local cc`j' = `c'
	//di `cc`j''

	if `c' == 0 {
		//matrix numcont[1,`c']=0
		local stop = 1
		//di "stop"
	}

	if `c'!=0 {
		constraint get `c'
		matrix numcont[1,`c']=1
		//di "donc on retire"
		//di r(contents)
		local reprio`k' = r(contents)
		local ordre_reprio_`k' = `o'
		local ++o
		constraint drop `c'
		local c`c'
		local ++k
	}
	qui sem `sem1' `cl' constraints(1/`=`nbitems'*3')
	qui est store model2
	if "`sb'" != "" {
		qui estat gof, stats(all)
		local df2=r(df_ms)
		local cmodel2 = e(sbc_ms)
		local chi22=r(chi2_ms)
	}
	timer off 10
	qui timer list 10
	local ++j
	if `j'==2 {
		if (`nbsign'==0) local nbsign=1
		di as text "Iteration `j' running" " (Based on the results of the first iteration, step 3 --non-uniform recalibration-- should last approximately " `=round(`r(t10)'*`nbsign')' " seconds)"
	}
	else {
		di as text "Iteration `j' running"
	}
}

di as text "Constraints removed (non-uniform recalibration): "
forvalues i = 1/`nbitems' {
	if "`nonunif`i''"!="" di "`nonunif`i''"
}



/**************************************************************************************************************
Model 3 / Uniform recalibration
***************************************************************************************************************/



di
di as result "Step 3 --Uniform recalibration--"

timer clear 20
timer on 20
local nbsign = 0
di as text "Iteration 1 running..."
local pbconv = 0
local o = 1
local k = 1
local j = 1
local stop = 0
//local nonunif
while (`j'<=`nbitems' & `stop'!=1) {
	local max = -1
	local c = 0
	local stop = 0
	timer clear 10
	timer on 10
	forvalues i = `=`nbitems'+1'/`=`nbitems'*2' {
		constraint get `i'
		if r(defined)== 1 {
			constraint drop `i'
			qui sem `sem1' `cl' constraints(1/`=`nbitems'*3') forcenoanchor
			//constraint list
			constraint `i' `c`i''
			//constraint list
			//di "Après avoir enlevé `c`i'' : "
			if "`adjust'" != "" {
				local nbtest = `nbitems'-`j'+1
			}
			else local nbtest = 1
			//di "nbtest = `nbtest'"
			qui eret li
			if e(converged) == 1 {
				est store model3
				if "`sb'" == "" {
					`qui' lrtest model2 model3
					if r(p) < `=0.05/`nbtest'' {
						local nbsign = `nbsign'+1
					}
					if r(chi2) > `max' & r(p) < `=0.05/`nbtest'' {
						local max = r(chi2)
						local c = `i'
					}
				}
				else {
					qui estat gof, stats(all)
					local df3=r(df_ms)
					local cmodel3 = e(sbc_ms)
					local chi23=r(chi2sb_ms)
					local cd=(`df2'*`cmodel2'-`df3'*`cmodel3')/(`df2'-`df3')
					local df23=`df2'-`df3'
					local chi223=(`chi22'-`chi23')/`cd'
					local p23 = 1-chi2(`df23',`chi223')
					//di "lr(sb) = `chi223'"
					//di "p = `p23'"
					if `p23' < `=0.05/`nbtest'' {
						local nbsign = `nbsign'+1
					}
					if `chi223' > `max' & `p23' < `=0.05/`nbtest'' {
						local max = `chi223'
						local c = `i'
					}
				}
			}
			else {
				local ++pbconv
				//`qui' di "model did not converge (`pbconv' in total)"
			}

		}
	}
	//local mmax = `max'
	//di `mmax'
	//local cc`j' = `c'
	//di `cc`j''

	if `c' == 0 {
		//matrix numcont[1,`c']=0
		local stop = 1
		//di "stop"
	}

	if `c'!=0 {
		constraint get `c'
		matrix numcont[1,`c']=1
		//di "donc on retire"
		//di r(contents)
		local reprio`k' = r(contents)
		local ordre_reprio_`k' = `o'
		local ++o
		constraint drop `c'
		local c`c'
		local ++k
	}
	qui sem `sem1' `cl' constraints(1/`=`nbitems'*3')
	qui est store model2
	if "`sb'" != "" {
		qui estat gof, stats(all)
		local df2=r(df_ms)
		local cmodel2 = e(sbc_ms)
		local chi22=r(chi2_ms)
	}
	timer off 10
	qui timer list 10
	local ++j
	if `j'==2 {
		if (`nbsign'==0) local nbsign=1
		di as text "Iteration `j' running..." " (Based on the results of the first iteration, step 3 --uniform recalibration-- should last approximately " `=round(`r(t10)'*`nbsign')' " seconds)"
	}
	else {
		di as text "Iteration `j' running..."
	}
}
di as text "Constraints removed (uniform recalibration): "
forvalues i = 1/`=`nbitems'*3' {
	if "`unif`i''"!="" di "`unif`i''"
}


/**************************************************************************************************************
Model 3 / Reprioritization
***************************************************************************************************************/

di
di as result "Step 3 --Reprioritization--"

timer clear 20
timer on 20
local nbsign = 0
di as text "Iteration 1 running..."
local pbconv = 0
local o = 1
local k = 1
local j = 1
local stop = 0
//local nonunif
while (`j'<=`nbitems' & `stop'!=1) {
	local max = -1
	local c = 0
	local stop = 0
	timer clear 10
	timer on 10
	forvalues i = `=`nbitems'*2+1'/`=`nbitems'*3' {
		constraint get `i'
		if r(defined)== 1 {
			constraint drop `i'
			qui sem `sem1' `cl' constraints(1/`=`nbitems'*3') forcenoanchor
			//constraint list
			constraint `i' `c`i''
			//constraint list
			//di "Après avoir enlevé `c`i'' : "
			if "`adjust'" != "" {
				local nbtest = `nbitems'-`j'+1
			}
			else local nbtest = 1
			//di "nbtest = `nbtest'"
			qui eret li
			if e(converged) == 1 {
				est store model3
				if "`sb'" == "" {
					`qui' lrtest model2 model3
					if r(p) < `=0.05/`nbtest'' {
						local nbsign = `nbsign'+1
					}
					if r(chi2) > `max' & r(p) < `=0.05/`nbtest'' {
						local max = r(chi2)
						local c = `i'
					}
				}
				else {
					qui estat gof, stats(all)
					local df3=r(df_ms)
					local cmodel3 = e(sbc_ms)
					local chi23=r(chi2sb_ms)
					local cd=(`df2'*`cmodel2'-`df3'*`cmodel3')/(`df2'-`df3')
					local df23=`df2'-`df3'
					local chi223=(`chi22'-`chi23')/`cd'
					local p23 = 1-chi2(`df23',`chi223')
					//di "lr(sb) = `chi223'"
					//di "p = `p23'"
					if `p23' < `=0.05/`nbtest'' {
						local nbsign = `nbsign'+1
					}
					if `chi223' > `max' & `p23' < `=0.05/`nbtest'' {
						local max = `chi223'
						local c = `i'
					}
				}
			}
			else {
				local ++pbconv
				//`qui' di "model did not converge (`pbconv' in total)"
			}

		}
	}
	//local mmax = `max'
	//di `mmax'
	//local cc`j' = `c'
	//di `cc`j''

	if `c' == 0 {
		//matrix numcont[1,`c']=0
		local stop = 1
		//di "stop"
	}

	if `c'!=0 {
		constraint get `c'
		matrix numcont[1,`c']=1
		//di "donc on retire"
		//di r(contents)
		local reprio`k' = r(contents)
		local ordre_reprio_`k' = `o'
		local ++o
		constraint drop `c'
		local c`c'
		local ++k
	}
	qui sem `sem1' `cl' constraints(1/`=`nbitems'*3')
	qui est store model2
	if "`sb'" != "" {
		qui estat gof, stats(all)
		local df2=r(df_ms)
		local cmodel2 = e(sbc_ms)
		local chi22=r(chi2_ms)
	}
	timer off 10
	qui timer list 10
	local ++j
	if `j'==2 {
		if (`nbsign'==0) local nbsign=1
		di as text "Iteration `j' running..." " (Based on the results of the first iteration, step 3 --reprioritization-- should last approximately " `=round(`r(t10)'*`nbsign')' " seconds)"
	}
	else {
		di as text "Iteration `j' running..."
	}
}

di as text "Constraints removed (reprioritization) : "
forvalues i = 1/`nbitems' {
	if "`reprio`i''"!="" di "`reprio`i''"
}




di as result "Summary of step 3 "
/*
di "`pbconv' model(s) did not converge"
di
di as text "The following constraints were removed:"
forvalues i = 1/`=`nbitems'*3' {
	if "`nonunif`i''"!="" di "`nonunif`i'' (order = `ordre_nonunif_`i'')"
}
forvalues i = 1/`=`nbitems'*3' {
	if "`unif`i''"!="" di "`unif`i'' (order = `ordre_unif_`i'')"
}
forvalues i = 1/`=`nbitems'*3' {
	if "`reprio`i''"!="" di "`reprio`i'' (order = `ordre_reprio_`i'')"
}
//constraint list
*/
}








else if "`hierarchical'" == "" & "`testno'" !=" no"{

di
di as result "Step 3: Detection of response shift"

timer clear 20
timer on 20
local nbsign = 0
di as text "Iteration 1 running " _c
local pbconv = 0
local o = 1
local k = 1
local j = 1
local stop = 0
local nonunif
while (`stop'!=1) {
	local max = -1
	local c = 0
	local stop = 0
	timer clear 10
	timer on 10
	forvalues i = 1/`=`nbitems'*3' {
		constraint get `i'
		if r(defined)== 1 {
			//di as text "`j'_`i'"
			constraint drop `i'
			qui sem `sem1' `cl' constraints(1/`=`nbitems'*3') forcenoanchor
			//constraint list
			constraint `i' `c`i''
			//constraint list
			`qui' di as text "After removing `c`i'' : "
			qui eret li
			if e(converged) == 1 {
				est store model3
				if "`sb'" == "" {
					qui lrtest model2 model3
					if "`adjust'" != "" {
						local nbtest = `nbitems'*3-`j'+1
					}
					else local nbtest = 1
					if r(p) < `=0.05/`nbtest'' {
						local nbsign = `nbsign'+1
					}
					`qui' di "nbtest = `nbtest'"
					if r(chi2) > `max' & r(p) < `=0.05/`nbtest'' {
						local max = r(chi2)
						local c = `i'
					}
				}
				else {
					qui estat gof, stats(all)
					local df3=r(df_ms)
					local cmodel3 = e(sbc_ms)
					local chi23=r(chi2_ms)
					//di in red "chi22 = `chi22'"
					//di in red "chi23 = `chi23'"
					//di in red `cmodel2'
					//di in red `cmodel3'
					local cd=(`df2'*`cmodel2'-`df3'*`cmodel3')/(`df2'-`df3')
					local df23=`df2'-`df3'
					local chi223=(`chi22'-`chi23')/`cd'
					local p23 = 1-chi2(`df23',`chi223')
					//di "df2 = `df2'"
					//di "df3 = `df3'"
					di as text "cd = `cd'"
					//di "chi223 = `chi223'"
					//di "df23 = `df23'"
					di as text "lr(sb) = `chi223'"
					di as text "p = `p23'"
					if `chi223' > `max' & `p23' < 0.05 {
						local max = `chi223'
						local c = `i'
					}
				}
			}
			else {
				local ++pbconv
				`qui' di "model did not converge (`pbconv' in total)"
			}

		}
		di %4.0f `=`i'/(`nbitems'*3)*100' "% " _c
	}

	if `c' == 0 {
		//matrix numcont[1,`c']=0
		local stop = 1
		//di "stop"
	}
	if `c'!=0 {
		//di in red `c'
		matrix numcont[1,`c']=1
		constraint get `c'
		`qui' di "The following constraint was removed: "
		`qui' di r(contents)
		if `c'<=`nbitems' {
			local nonunif`k' = r(contents)
			local ordre_nonunif_`k' = `o'
			local ++o
		}
		else if `c' > `nbitems' & `c'<=`=`nbitems'*2' {
			local unif`k' = r(contents)
			local ordre_unif_`k' = `o'
			local ++o
		}
		else if `c' > `=`nbitems'*2' {
			local reprio`k' = r(contents)
			local ordre_reprio_`k' = `o'
			local ++o
		}
		constraint drop `c'
		local c`c'
		local ++k
	}
	qui sem `sem1' `cl' constraints(1/`=`nbitems'*3') forcenoanchor
	qui est store model2
	if "`sb'" != "" {
		qui estat gof, stats(all)
		local df2=r(df_ms)
		local cmodel2 = e(sbc_ms)
		local chi22=r(chi2_ms)
	}

	timer off 10
	qui timer list 10
	local ++j
	if `j'==2 {
		di as text _n "(Based on the results of the first iteration, Step 3 should last approximately " `=round(`r(t10)'*`nbsign')' " seconds)"
		di as text "Iteration 2 running " _c
	}
	else {
		if `c'!=0 {
			di as text _n "Iteration `j' running " _c
		}
	}
}
timer off 20
qui timer list 20
di _n "(Step 3 took "`=round(`r(t20)')' " seconds to complete)"
di
est store model3


/*
di as text "Contraintes libérées (recalibration non-uniforme) : "
forvalues i = 1/`nbitems' {
	if "`nonunif`i''"!="" di "`nonunif`i''"
}*/


di as result "Summary of step 3 "
/*
di
di as text "`pbconv' model(s) did not converge"
di
di as text "The following constraints were removed:"
forvalues i = 1/`=`nbitems'*3' {
	if "`nonunif`i''"!="" di "`nonunif`i'' (order = `ordre_nonunif_`i'')"
}
forvalues i = 1/`=`nbitems'*3' {
	if "`unif`i''"!="" di "`unif`i'' (order = `ordre_unif_`i'')"
}
forvalues i = 1/`=`nbitems'*3' {
	if "`reprio`i''"!="" di "`reprio`i'' (order = `ordre_reprio_`i'')"
}
//constraint list
*/
}



est store model4
local m4 = e(cmdline)

local i = 1
foreach p in `partition' {
	local cl `cl' means(L`i'_1@0) var(L`i'_1@1) var(L`i'_2@1) means(L`i'_2)
	local ++i
}




mat mt = numcont'

local maxlength = 0
foreach v of varlist `varlist' {
	if length("`v'")>`maxlength' {
		local maxlength = length("`v'")
	}
}

local et = "&|"
forvalues i=1/`=`nbitems'*3' {
	local et "`et'" "&"
}
matrix colnames mt = "1 if RS detected"

if "`testno'" == "" {
	di as text "Notation: "
	di as text "RS = response shift"
	di as text "NUR = non-uniform recalibration"
	di as text "UR = uniform recalibration"
	di as text "REP = reprioritization"
	local esp
	forvalues i = 1/`=`nbitems'*3+2' {
		local esp = "`esp'&"
	}
	matlist mt, cspec(& %~`=`maxlength'+4's & %18.0f & ) rspec("`esp'")
	mat li mt
	matrix rsitems = mt
	return matrix rsitems = mt
	di
	*di as text "End of step 3"
}


/***********************
        Step 4
************************/
di as result "Step 4: Assessment of true change"
qui est rep model4
qui est rest model4

tempname b V
matrix `b'=e(b)
matrix `V'=e(V)
//mat li `V'

local i = 1
foreach p in `partition' {
	local truechange_L`i'_4=`b'[1,`=`nbitems'*4+`i'']
	local Vtruechange_L`i'_4=`V'[`=`nbitems'*4+`i'',`=`nbitems'*4+`i'']
	local setruechange_L`i'_4=`=sqrt(`Vtruechange_L`i'_4')'
	local ztruechange_L`i'_4=`=`truechange_L`i'_4'/sqrt(`Vtruechange_L`i'_4')'
	local ptruechange_L`i'_4=`=2-2*normal(abs(`truechange_L`i'_4')/sqrt(`Vtruechange_L`i'_4'))'
	local ++i
}

local i = 1
foreach p in `partition' {
	return scalar truechange_L`i'_4 = `truechange_L`i'_4'
	return scalar Vtruechange_L`i'_4=`Vtruechange_L`i'_4'
	return scalar setruechange_L`i'_4=`setruechange_L`i'_4'
	return scalar ztruechange_L`i'_4=`ztruechange_L`i'_4'
	return scalar ptruechange_L`i'_4=`ptruechange_L`i'_4'
	local ++i
}





local i = 1
foreach p in `partition' {
	//di as text "True change (i.e. when accounting for response shift) for `ltname`i'' is " %4.3f `truechange_L`i'_4' " (p = " %4.3f `ptruechange_L`i'_4' ")"
	//di as text "Without accounting for response shift, the estimated change for `ltname`i'' would be " %4.3f `truechange_L`i'' " (p = " %4.3f `ptruechange_L`i'' ")"
	//di
	local ++i
}


/* LR test for LT means */
qui est rest model4
local m4 = e(cmdline)


forvalues i = 1/`P' {
	local cm `cm' means(L`i'_2@0)
}
qui `m4' `cm'
qui est store model4_0

if `P' > 1 {
	*di as text "Test if all LT means = 0 at time 2"
	qui lrtest model4 model4_0
	qui ret li
	if r(p) > 0.05 {
		di as text "LT means are not significantly different from 0 (p=" %4.3f r(p) ")"
	}
	else {
		di as text "LT means are significantly different from 0 (p=" %4.3f r(p) ")"
	}
}

/* Decomposition of observed change */

di
di as result "Decomposition of observed change"
di
/*
di as text "Notation:"
di as text "observed change = contribution of response shift + true change"
di as text "contribution of reponse shift = contribution of recalibration + contribution of reprioritization"
di
*/

qui est rest model4
qui estat framework, fitted

mat mu = r(mu)

forvalues i = 1/`P' {
	mat muLT`i' = mu[1,`nbitems'*2+`i'*2]
}

mat gamma = r(Gamma)

mat gamma1 = gamma[1...,1..1]
mat gamma2 = gamma[1...,2..2]
forvalues i=1(2)`=`P'*2' {
	mat g1 = gamma[1...,`i'..`i']
	mat gamma1 = gamma1,g1
	mat g2 = gamma[1...,`i'+1..`i'+1]
	mat gamma2 = gamma2,g2
}
mat gamma1 = gamma1[1...,2...]
mat gamma2 = gamma2[1...,2...]

mat alpha = r(alpha)

local v = 1
local c = 1
local i = 1
local j = 1
local pp = 0
foreach p in `partition' {
	local pp = `pp'+`p'
	while `j'<= `pp' {
		local tc = 0
		local reprio = 0
		mat mui = mu

		local mu1 = mui[1,`j']
		local mu2 = mui[1,`j'+`p']
		local mu2mu1 = `mu2'-`mu1'
		local oc`v' = `mu2mu1'

		local recalibration = alpha[1,`j'+`p']-alpha[1,`j']

		mat tc = gamma1[`j',1...]*muLT`i'[1...,1...]
		forvalues k = 1/`P' {
			local t = el(tc,1,`k')
			local tc = `tc'+`t'
		}
		local tc`v' = `tc'

		mat g1 = gamma1[`j',1...]
		mat g2 = gamma2[`j'+`p',1...]
		mat m = muLT`i'[1...,1...]

		mat reprio = (gamma2[`j'+`p',1...]-gamma1[`j',1...])*muLT`i'[1...,1...]
		*mat li reprio
		forvalues k = 1/`P' {
			local r = el(reprio,1,`k')
			local reprio = `reprio'+`r'
		}


		local rs = `recalibration'+`reprio'
		local rs`v' = `rs'

		//di as text "Observed change for `var`v'' = " %10.3g `mu2mu1' " = " %10.3g `rs'  " + " %10.3g `tc'
		if (`rs'!=0) {
			//di as text = "Contribution of response shift = " %10.3g `rs' " = "  %10.3g `recalibration' " + " %10.3g `reprio'
		}
		else {
			//di as text "(no repsonse shift for `var`v'')"
		}
		local ++j
		local ++v
		//di
	}
	local j = `j'+`p'
	local pp = `pp'+`p'
	local ++i
}





/* Table */

local v = 1
forvalues i=1/`=`nbitems'*3' {
	if rsitems[`i',1]==1 {
		if `i' > `nbitems' & `i'<=`=`nbitems'*2' {
			local v = `i'-`nbitems'
		}
		if `i' > `=`nbitems'*2' & `i'<=. {
			local v = `i'-`nbitems'-`nbitems'
		}

		if `i'<=`nbitems' {
			local rsd`v' "`rsd`v''" "NUR "
		}
		if `i'>`nbitems' & `i'<=`=`nbitems'*2' {
			local rsd`v' "`rsd`v''" "UR "
		}
		if `i'>`=`nbitems'*2' {
			local rsd`v' "`rsd`v''" "REP "
		}
	}
}


di _col(0) "{hline 128}"
di as result _col(0) "Variable" _col(25) "Response shift" _col(48) "Observed change" _col(72) "Response shift contribution" _col(105) "True change contribution"
di as text _col(0) "{hline 128}"

local i = 1
foreach v of varlist `varlist' {
	if `i' <= `nbitems' {
		di as text _col(0) "`v'" _c
		di _col(25) %10.2g "`rsd`i''" _c
		di _col(48) %10.2g `oc`i'' _c
		di _col(72) %10.2g `rs`i'' _c
		di _col(105)%10.2g `tc`i''
	}
	local ++i
}

di as result _col(0) "{hline 128}"





/* Effect-sizes */

di
di as result "Effect-sizes of observed change, response shift, and true change"
di
/*
di as text "Notation:"
di as text "observed change = contribution of response shift + true change"
di as text "contribution of reponse shift = contribution of recalibration + contribution of reprioritization"
*/

qui est rest model4
qui estat framework, fitted

mat mu = r(mu)

forvalues i = 1/`P' {
	mat muLT`i' = mu[1,`nbitems'*2+`i'*2]
}

mat gamma = r(Gamma)

mat gamma1 = gamma[1...,1..1]
mat gamma2 = gamma[1...,2..2]
forvalues i=1(2)`=`P'*2' {
	mat g1 = gamma[1...,`i'..`i']
	mat gamma1 = gamma1,g1
	mat g2 = gamma[1...,`i'+1..`i'+1]
	mat gamma2 = gamma2,g2
}
mat gamma1 = gamma1[1...,2...]
mat gamma2 = gamma2[1...,2...]

mat alpha = r(alpha)


mat sigma = r(Sigma)



local v = 1
local c = 1
local i = 1
local j = 1
local pp = 0
foreach p in `partition' {
	local pp = `pp'+`p'
	while `j'<= `pp' {
		local tc = 0
		local reprio = 0
		mat mui = mu

		local mu1 = mui[1,`j']
		local mu2 = mui[1,`j'+`p']
		local mu2mu1 = `mu2'-`mu1'


		/* standard deviations */
		local sd1 = el(sigma,`j',`j')
		local sd2 = el(sigma,`j'+`p',`j'+`p')
		local covar = el(sigma,`j',`j'+`p')
		local sd = sqrt(`sd1'+`sd2'-2*`covar')

		*di "sd = " `sd'

		local mu2mu1 = `mu2mu1'/`sd'
		local oc`v' = `mu2mu1'


		local recalibration = alpha[1,`j'+`p']-alpha[1,`j']
		local recalibration = `recalibration'/`sd'

		mat tc = gamma1[`j',1...]*muLT`i'[1...,1...]
		forvalues k = 1/`P' {
			local t = el(tc,1,`k')
			local tc = `tc'+`t'
		}


		local tc = `tc'/`sd'
		local tc`v' = `tc'
		*di "tc = " `tc'


		mat g1 = gamma1[`j',1...]
		mat g2 = gamma2[`j'+`p',1...]
		mat m = muLT`i'[1...,1...]

		mat reprio = (gamma2[`j'+`p',1...]-gamma1[`j',1...])*muLT`i'[1...,1...]
		*mat li reprio
		forvalues k = 1/`P' {
			local r = el(reprio,1,`k')
			local reprio = `reprio'+`r'
		}
		local reprio = `reprio'/`sd'

		local rs = `recalibration'+`reprio'
		local rs`v' = `rs'

		//di as text "Observed change for `var`v'' = " %4.3f `mu2mu1' " = " %4.3f `rs'  " + " %4.3f `tc'
		if (`rs'!=0) {
			//di as text = "Contribution of response shift = " %4.3f `rs' " = "  %4.3f `recalibration' " + " %4.3f `reprio'
		}
		else {
			//di as text "(no repsonse shift for `var`v'')"
		}
		local ++j
		local ++v
		//di
	}
	local j = `j'+`p'
	local pp = `pp'+`p'
	local ++i
}


/* Table of effect-sizes */

forvalues i=1/`nbitems' {
	local rsd`i'
}

local v = 1

forvalues i=1/`=`nbitems'*3' {
	if rsitems[`i',1]==1 {
		if `i' > `nbitems' & `i'<=`=`nbitems'*2' {
			local v = `i'-`nbitems'
		}
		if `i' > `=`nbitems'*2' & `i'<=. {
			local v = `i'-`nbitems'-`nbitems'
		}

		if `i'<=`nbitems' {
			local rsd`v' "`rsd`v''" "NUR "
		}
		if `i'>`nbitems' & `i'<=`=`nbitems'*2' {
			local rsd`v' "`rsd`v''" "UR "
		}
		if `i'>`=`nbitems'*2' {
			local rsd`v' "`rsd`v''" "REP "
		}
	}
}


di _col(0) "{hline 128}"
di as result _col(0) "Variable" _col(25) "Response shift" _col(48) "Observed change" _col(72) "Response shift contribution" _col(105) "True change contribution"
di as text _col(0) "{hline 128}"

local i = 1
foreach v of varlist `varlist' {
	if `i' <= `nbitems' {
		di as text _col(0) "`v'" _c
		di _col(25) %6.3f "`rsd`i''" _c
		di _col(48) %6.3f `oc`i'' _c
		di _col(72) %6.3f `rs`i'' _c
		di _col(105)%6.3f `tc`i''
	}
	local ++i
}

di as result _col(0) "{hline 128}"

/* radar plots */


tempvar ocs tcs variable
qui gen `ocs' = .
qui gen `tcs' = .
qui gen `variable' = ""
forvalues i = 1/`nbitems' {
	qui replace `variable' = "`var`i''" in `i'
	qui replace `ocs' = `oc`i'' in `i'
	qui replace `tcs' = `tc`i'' in `i'
}

//list `variable' `ocs' `tcs' in 1/`nbitems'
qui replace `variable' = "a" if `variable'==""

qui radar `variable' `ocs' `tcs' in 1/`nbitems', aspect(1) legend(lab(1 "Observed change") lab(2 "True change")) r(-1 -0.8 -0.5 0 0.5 0.8 1) note("") title(Effect-sizes of observed change and true change)




/* return */

matrix effectsizes = J(`nbitems',3,.)
forvalues i = 1/`nbitems'{
	mat effectsizes[`i',1] = `oc`i''
	mat effectsizes[`i',2] = `rs`i''
	mat effectsizes[`i',3] = `tc`i''
	local names `names' `var`i''
}
mat rownames effectsizes = `names'

return matrix effectsizes = effectsizes




//di "modèle 2 avec variances à 1"
qui `m2' `cl'
//di "modèle 4 avec variances à 1"
qui `m4' `cl'

est drop model2
//est dir


restore
end