*! Version 3 11July2018
*! Jean-Benoit Hardouin
************************************************************************************************************
* nopalera : NOPALERA algorithm
*
* Historic:
* Version 1 (2015-07-20): Jean-Benoit Hardouin /*ICC; rsbynpirt module*/
* Version 1.1 (2015-10-24): Jean-Benoit Hardouin /*NOPALERA module, ISOQOL 2015*/
* Version 2 (2015-10-24): Jean-Benoit Hardouin /*NOPALERA module, ISOQOL 2015*/
* Version 2.1 (2017-12-13): Jean-Benoit Hardouin /*Improvement*/
* Version 3 (2018-07-11): Jean-Benoit Hardouin /*Improvement for paper*/
*
* Jean-benoit Hardouin, phD, Assistant Professor
* INSERM UMR 1246-SPHERE
* MethodS in Patients-centered outcomes and HEalth ResEarches
* University of Nantes - Faculty of Pharmaceutical Sciences
* France
* jean-benoit.hardouin@anaqol.org
*
* News about this program: http://www.anaqol.org
*
* Copyright 2015,2017,2018 Jean-Benoit Hardouin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
************************************************************************************************************

program define nopalera, rclass
version 8.0
syntax varlist(numeric min=4) [, nobsauc nobsaabc noGraph  corr(numlist) NBAUCbs(int 10) NBAABCbs(int 20)  noRESTscore GRAPHBYGroup ] /*corr est la correction à appliquer pour corriger le RS unif*/

set more off
tempfile file1 file2 file3
qui save `file1', replace
preserve
local nbitems : word count `varlist'
qui count
local nind=r(N)
tokenize `varlist'
if mod(`nbitems',2)!=0 {
   di in red "You must indicate an even number of items"
   exit
}
else {
   local nbitems=`nbitems'/2
}
if "`corr'"=="" {
  forvalues i=1/`nbitems'{
     local corr "`corr' 0"
  }
}
local listofitems1
local listofitems2
forvalues i=1/`nbitems' {
   local listofitems1 `listofitems1' ``i''
   local listofitems2 `listofitems2' ``=`i'+`nbitems'''
}

di "NOPALERA: an algorithm to detect Response-shift at items level using non parametric IRT"
di "Number of individuals: `nind'"
di "Number of items: `nbitems'"
di "Number of Boostrap replications for AUC: `nbaucbs'"
di "Number of Boostrap replications for AABC: `nbaabcbs'"

/*******************************************************************************
Une boucle pour calculer le score, définir le score maximum par item et global
*******************************************************************************/

tempvar varscore1 varscore2
qui gen `varscore1'=0
qui gen `varscore2'=0
label variable `varscore1' "Total score time 1"
label variable `varscore2' "Total score time 2"
local scoremax=0
local flag=0


local modamax=0

forvalues i=1/`nbitems' {
   local corr`i': word `i' of `corr'
   qui replace ``=`i'+`nbitems'''=``=`i'+`nbitems'''+`corr`i''
   qui replace `varscore1'=`varscore1'+``i''
   qui replace `varscore2'=`varscore2'+``=`i'+`nbitems'''
   local list1 `list1' ``i''
   local list2 `list2' ``=`i'+`nbitems'''

   qui su ``i''
   local modamax`i'=r(max)
   qui su ``=`i'+`nbitems'''
   local modamax`i'=r(max)
   if r(min)!=0 {
      local flag=1
   }
   local scoremax=`scoremax'+`modamax`i''
      if `modamax`i''!=1 {
      local flagbin=0
   }
   if `modamax`i''>`modamax' {
      local modamax=`modamax`i''
   }
}

/*******************************************************************************
On va calculer maintenant les rest-score de chaque item à chaque temps
*******************************************************************************/

forvalues i=1/`nbitems'{
    tempvar rscore1``i'' rscore2``i'' vargroup1``i'' vargroup2``i'' vargroup2b``i''
	if "`restscore'"=="" {
	   qui gen `rscore1``i'''=`varscore1'-``i''
	   qui gen `rscore2``i'''=`varscore2'-``=`i'+`nbitems'''
    }
	else {
	   qui gen `rscore1``i'''=`varscore1'
	   qui gen `rscore2``i'''=`varscore2'
    }
	qui gengroup `rscore1``i''' , new(`vargroup1``i''') det
	local seuil1=r(list)
    *di "rscore1 et group1 item ``i''"
	*tab `rscore1``i''' `vargroup1``i'''
    local seuil1 "`seuil1' max"
    local nbseuil1: word count  `seuil1'
    qui gen `vargroup2b``i'''=`rscore2``i'''
	local t
    forvalues j=1/`nbseuil1' {
		if `j'==1 {
		    local seuilmin=0
		}
		else {
            local seuilmin : word `=`j'-1' of `seuil1'
			local seuilmin=`seuilmin'+1
			local seuilmin=`seuilmax'+1
        }

		local seuilmax : word `j' of `seuil1'
        if "`seuilmax'"!="max" {
            if `seuilmax'<`seuilmin' {
	    	    local seuilmax `seuilmin'
			}
		}
		local t `t' (`seuilmin'/`seuilmax'=`j')
    }
	*di "recode vargroup2b``i'' `t'"
	qui recode `vargroup2b``i''' `t'
    *di "rscore2 et group2 item ``i'' avant regroupage"
	*tab `rscore2``i''' `vargroup2b``i'''
    qui gengroup `vargroup2b``i''',new(`vargroup2``i''') det replace
    local seuil2=r(list)
    *di "rscore2 et group2 item ``i'' après regroupage"
	*tab `rscore2``i''' `vargroup2``i'''
    local seuil2 "`seuil2' max"
    *di "``i'' seuil2 : `seuil2'"
	local nbseuil2: word count  `seuil2'
	local t
    forvalues j=1/`nbseuil2' {
		if `j'==1 {
		    local seuilmin=1
		}
		else {
            local seuilmin : word `=`j'-1' of `seuil2'
			local seuilmin=`seuilmin'+1
		    local seuilmin=`seuilmax'+1
        }
		local seuilmax : word `j' of `seuil1'
        if "`seuilmax'"!="max" {
            if `seuilmax'<`seuilmin' {
	    	    local seuilmax `seuilmin'
			}
		}
        local t `t' (`seuilmin'/`seuilmax'=`j')
    }
	*di "recode vargroup1``i'' `t'"
	qui recode `vargroup1``i''' `t'
    *di "rscore1 et group1 item ``i'' après regroupage"
	*tab `rscore1``i''' `vargroup1``i'''

}
*tab `vargroup1itemA1' `vargroup2itemA1'
*exit
qui save `file3', replace

*exit
/*******************************************************************************
Quelques tests de conformité
*******************************************************************************/

qui su `varscore1'
local maxscore=r(max)
qui su `varscore2'
if `r(max)'>`maxscore' {
   local maxscore=r(max)
}
forvalues i=1/`nbitems' {
    qui su `vargroup1``i'''
    local maxgroup``i''=r(max)
}


/* sauvegarde des données */
tempfile rsbynpirtfile rsbynpirtfile1 rsbynpirtfile2
tempvar score
qui save `rsbynpirtfile', replace

/*******************************************************************************
Représentation graphique des tracelines
*******************************************************************************/


forvalues i=1/`nbitems' {
    tempfile file4
	tempname mat``i''
	matrix `mat``i'''=J(`=`maxgroup``i'''+2',9,0)
	forvalues j=1/`maxgroup``i''' {
	     local k=`j'+1
	     matrix `mat``i'''[`k',1]=`j'
		 *tab `vargroup1``i'''
		 qui su ``i'' if `vargroup1``i'''==`j'
	     matrix `mat``i'''[`k',2]=`r(mean)'
	     matrix `mat``i'''[`k',4]=`r(N)'
		 local temp=`i'+`nbitems'
		 *tab `vargroup2``i''' `vargroup1``i'''
		 tab ``temp'' if `vargroup2``i'''==`j',m
		 qui su ``temp'' if `vargroup2``i'''==`j'
	     matrix `mat``i'''[`k',3]=`r(mean)'
	     matrix `mat``i'''[`k',5]=`r(N)'
		 qui su `rscore1``i''' if `vargroup1``i'''==`j'
	     matrix `mat``i'''[`k',6]=`r(mean)'
		 qui su `rscore2``i''' if `vargroup2``i'''==`j'
	     matrix `mat``i'''[`k',7]=`r(mean)'
	}
	local j=`maxgroup``i'''+2
	matrix `mat``i'''[`j',1]=`maxgroup``i'''+1 /*numero du group*/
	matrix `mat``i'''[`j',2]=`modamax`i'' /*mean item 1*/
	matrix `mat``i'''[`j',3]=`modamax`i'' /*mean item 2*/
	matrix `mat``i'''[`j',4]=0 /*frequency 1*/
	matrix `mat``i'''[`j',5]=0 /*frequency 2*/
	if "`restscore'"=="" {
	   local maxi=`scoremax'-`modamax`i''
	}
	else {
	   local maxi=`scoremax'
	}
    matrix `mat``i'''[`j',6]=`maxi' /*mean rest-score 1*/
    matrix `mat``i'''[`j',7]=`maxi' /*mean rest-score 2*/
	label values `vargroup1``i''' lblvarscore``i''
	qui sort `vargroup1``i''' `vargroup2``i'''
	qui save `file4',replace
    forvalues j=1/`maxgroup``i''' {
        qui count if `vargroup1``i'''==`j'
	    local n1=r(N)
	    qui su `rscore1``i''' if `vargroup1``i'''==`j'
	    local min1=r(min)
	    local max1=r(max)
        qui count if `vargroup2``i'''==`j'
	    local n2=r(N)
	    qui su `rscore2``i''' if `vargroup2``i'''==`j'
	    local min2=r(min)
	    local max2=r(max)
	    local min=min(`min1',`min2')
	    local max=max(`max1',`max2')
	    label define lblvarscore``i'' `j' `""Scores `min'/`max'" "(n1=`n1';n2=`n2')""', add
    }

	label define lblvarscore``i'' 0 "Min", add
    label define lblvarscore``i'' `=`maxgroup``i'''+1' "Max", add
    qui drop _all
	qui svmat `mat``i'''
	qui rename `mat``i'''1 group
	qui rename `mat``i'''2 e1
	qui rename `mat``i'''3 e2
	qui rename `mat``i'''6 rs1
	qui rename `mat``i'''7 rs2
    qui sort group
	label values group lblvarscore``i''
	label variable group "Groups of scores"
	if "`graph'"=="" {
	    if "`graphbygroup'"!="" {
	        graph twoway (line e1 group ) (line e2 group ), title("Traceline of item ``i''") xtitle("") ytitle("Expected value of item ``i''", size(small)) ylabel(0(1)`modamax') xlabel(0(1)`=`maxgroup``i'''+1',valuelabel labsize(small) /*alternate*/) name("``i''", replace) legend(off)
	    }
		else {
		    if "`restscore'"=="" {
			     local xtitle="Rest-score"
				 qui su rs1
				 local max=r(max)
				 qui su rs2
				 if `r(max)'>`max' {
				     local max=r(max)
				 }

		    }
			else {
			     local xtitle="Score"
				 local max `maxscore'
		    }
		    graph twoway (line e1 rs1 ) (line e2 rs2 ), title("Traceline of item ``i''") xtitle("`xtitle'") ytitle("Expected value of item ``i''", size(small)) ylabel(0(1)`modamax') xlabel(0(1)`max',valuelabel labsize(small) /*alternate*/) name("auc``i''", replace) legend(off)
		}
	}
	qui use `file4', replace
}

/*******************************************************************************
Calcul des AUC
*******************************************************************************/

local tests
forvalues i=1/`nbitems' {
    local auc1`i'=0
    local auc2`i'=0
    forvalues g=1/`=`maxgroup``i'''+1' {
        local k=`g'+1
	    local auc=(`mat``i'''[`g',2]+`mat``i'''[`k',2])/2*(`mat``i'''[`k',6]-`mat``i'''[`g',6])
		local auc1`i'=`auc1`i''+`auc'
	    matrix `mat``i'''[`k',8]= `auc'
	    local auc=(`mat``i'''[`g',3]+`mat``i'''[`k',3])/2*(`mat``i'''[`k',7]-`mat``i'''[`g',7])
		local auc2`i'=`auc2`i''+`auc'
	    matrix `mat``i'''[`k',9]= `auc'
	}
    local AUC``i''=`auc1`i''
    local AUC``=`i'+`nbitems'''=`auc2`i''
    local diffAUC``i''=`=`AUC``i'''-`AUC``=`i'+`nbitems'''''
    return scalar AUC``i''=`auc1`i''
    return scalar AUC``=`i'+`nbitems'''=`auc2`i''
    local tests `tests' b``i''=(r(AUC``=`i'+`nbitems''')-r(AUC``i''))
}

/*******************************************************************************
Calcul des standard errors des AUC par bootstrap
*******************************************************************************/

*local abc=0
if "`bsauc'"=="" {
    *di "on lance le bootstrap sur les AUC"
	*set trace on
	tempfile file2
    qui save `file2', replace
    qui use `file1', clear
    capture qui bootstrap `tests' , rep(`nbaucbs') nol noh nodots : nopalera `varlist', nograph  `restscore' nobsaabc nobsauc
	local n=1
	while (_rc!=0) {
		di "`n'. The boostrap to obtain IC of the AUC has not been correctly performed, it is run again"
		local ++n
		capture qui bootstrap `tests' , rep(`nbaucbs') nol noh nodots : nopalera `varlist', nograph  `restscore' nobsaabc nobsauc
	}
    tempname mbootstrap
	matrix `mbootstrap'=r(table)
    qui use `file2', clear

	/*Création de la matrice p qui va permettre de décaler les ICC*/
	tempname p
	matrix `p'=J(`=(`scoremax'+1)*(`nbitems'+1)',`=`nbitems'*3+1',.)
	forvalues s=0/`scoremax' {
	   matrix `p'[`=`s'+1',1]=`s'
	   forvalues i=1/`nbitems' {
		  matrix `p'[`=`s'+1',`=3*`i'-1']=`s'
	   }
	}
	forvalues i=1/`nbitems' {
	   forvalues s=0/`scoremax' {
		  qui su ``i'' if `varscore1'==`s'
		  matrix `p'[`=`s'+1',`=3*`i'']=r(mean)
	   }
	   forvalues s=0/`scoremax' {
		  qui su ``=`i'+`nbitems''' if `varscore2'==`s'
		  if `mbootstrap'[4,`i']>=0.05 {
			 matrix `p'[`=`s'+1',`=3*`i'+1']=r(mean)
			 local prec=1
			 local prec=0.01 /*pas d'arrondi*/
		  }
		  else {
		     if `nind'/`scoremax'>20 {
			    local prec=0.5
			    local prec=1
			    local prec=0.01 /*pas d'arrondi*/
			 }
			 else {
			    local prec=1
			    local prec=0.01 /*pas d'arrondi*/
			 }
			 matrix `p'[`=`i'*(`scoremax'+1)+`s'+1',`=3*`i'-1']=round(`s'+`mbootstrap'[1,`i']/(`modamax`i''),`prec')
			 matrix `p'[`=`i'*(`scoremax'+1)+`s'+1',`=3*`i'+1']=r(mean)
		  }
	   }
	}
	di
	di "Research of uniform recalibration at item-level"
	di
	di "{hline 105}"
	di                                                               _col(45) "Bootstrap"                               _col(78) "  Normal-based"
	di "Items" _col(18) "AUC t1" _col(28) "AUC t2" _col(38) "VarAUC" _col(45) "Std. Err." _col(63) "z" _col(70) "P>|z|" _col(78) "[95% Conf. Int.]" _col(96) "Correction"
	di "{hline 105}"
    forvalues i=1/`nbitems' {
	   local corr``i''=round(`mbootstrap'[1,`i']/(`modamax`i''),`prec')
	   di "``i''/``=`i'+`nbitems'''" _col(19) %5.2f `AUC``i''' _col(29) %5.2f `AUC``=`i'+`nbitems'''' _col(39) %5.2f `diffAUC``i''' _col(49) %5.2f `mbootstrap'[2,`i'] _col(59) %5.2f `mbootstrap'[3,`i'] _col(69) %5.4f `mbootstrap'[4,`i'] _col(79) %5.2f `mbootstrap'[5,`i'] _col(89) %5.2f `mbootstrap'[6,`i'] _col(100) %6.2f `corr``i'''
	}
	di "{hline 105}"
	local abc=1
}
else {
    *di "pas de bootstrap AUC"
	local abc=1
}

*di "abc:`abc' bsAUC : `bsauc'"

/*******************************************************************************
Calcul des AABC et de leurs intervalles de confiance par boostrap
*******************************************************************************/
*set trace on
if `abc'==1 {
   *di "on lance les AABC"
   local testsb
   forvalues i=1/`nbitems' {
       if "`bsauc'"!="" { /*si pas de bootstrap pour AUC*/
	      *di "m1"
		  local sigAUC``i''=0
          local rdiffAUC``i''=0
	   }
	   else if `mbootstrap'[4,`i']<0.05 { /* si bootstrap et significatif*/
	      *di "m2"
	      local sigAUC``i''=1
		  local rdiffAUC``i''=-`corr``i'''
		  di "The item ``i'' traceline is corrected of `corr``i'''"
       }
       else { /* si bootstrap et NS*/
	      *di "m3"
	      local sigAUC``i''=0
          local rdiffAUC``i''=0
       }
	   tempname mat2``i''
	   local nbl=`=`maxgroup``i'''*4+7'
       matrix `mat2``i'''=J(`nbl',4,.)
	   matrix `mat2``i'''[1,2]=0
	   matrix `mat2``i'''[1,3]=0
	   matrix `mat2``i'''[`nbl',2]=`modamax`i''
	   matrix `mat2``i'''[`nbl',3]=`modamax`i''
	   local col1=1
	   local col2=1
	   local line=1
       while (`line'<=`=`maxgroup``i'''*4+7') {
	      local t1=`mat``i'''[`col1',6]
		  local t2=`mat``i'''[`col2',7]-`rdiffAUC``i'''
          local m2=`mat``i'''[`col1',2]
		  local m3=`mat``i'''[`col2',3]
		  if (`t1'<`t2'|(`t1'==`t2'&`m2'<`m3')) {
		      *di "m2 car `m2'<`m3'"
		      matrix `mat2``i'''[`line',1]=`t1'
		      matrix `mat2``i'''[`line',2]=`mat``i'''[`col1',2]
			  local ++col1
          }
		  else {
		      *di "m3 car `m2'>`m3'"
		      matrix `mat2``i'''[`line',1]=`t2'
		      matrix `mat2``i'''[`line',3]=`mat``i'''[`col2',3]
			  local ++col2
		  }
	      local line=`line'+2
	   }
	   *matrix list `mat2``i'''
	   local j=1
	   while (`j'<=`nbl') {
	       foreach c in 2 3 {
	          if `mat2``i'''[`j',`c']==. {
                   local prec=`j'-2
				   local m=`mat2``i'''[`prec',`c']
                   while (`m'==.&`prec'>=0) {
				       local prec=`prec'-2
               		   local m=`mat2``i'''[`prec',`c']
					   *di "prec:`prec' m: `m'"
				   }
                   local suiv=`j'+2
				   local m=`mat2``i'''[`suiv',`c']
                   while (`m'==.&`suiv'<=`nbl') {
				       local suiv=`suiv'+2
					   local m=`mat2``i'''[`suiv',`c']
					   *di "suiv:`suiv' m: `m'"
				   }
				   local yprec=`mat2``i'''[`prec',`c']
				   local ysuiv=`mat2``i'''[`suiv',`c']
				   local xprec=`mat2``i'''[`prec',1]
				   local xsuiv=`mat2``i'''[`suiv',1]
				   local x=`mat2``i'''[`j',1]
				   if (`xsuiv'!=`xprec') {
				       *di "matrix `mat2``i'''[`j',`c']=`yprec'+(`x'-`xprec')/(`xsuiv'-`xprec')*(`ysuiv'-`yprec')"
				       matrix `mat2``i'''[`j',`c']=`yprec'+(`x'-`xprec')/(`xsuiv'-`xprec')*(`ysuiv'-`yprec')
				   }
				   else {
				       matrix `mat2``i'''[`j',`c']=`mat2``i'''[`prec',`c']
				   }
		      }
		   }
		   local j=`j'+2
	   }
	   local j=2
	   *matrix list `mat2``i'''
	   while (`j'<`nbl') {
	       local prec=`j'-1
	       local suiv=`j'+1
		   local sign=(`mat2``i'''[`prec',2]-`mat2``i'''[`prec',3])*(`mat2``i'''[`suiv',2]-`mat2``i'''[`suiv',3])
		   *di "item ``i'', ligne `j', sign `sign'"
		   if (`sign'<0&`sign'!=.) {
		      local p1=(`mat2``i'''[`suiv',2]-`mat2``i'''[`prec',2])/(`mat2``i'''[`suiv',1]-`mat2``i'''[`prec',1])
		      local p2=(`mat2``i'''[`suiv',3]-`mat2``i'''[`prec',3])/(`mat2``i'''[`suiv',1]-`mat2``i'''[`prec',1])
			  if (`p1'==`p2') {
			      local cint=`mat2``i'''[`prec',1]
			  }
			  else  {
			      local cint=`mat2``i'''[`prec',1]+(`mat2``i'''[`prec',3]-`mat2``i'''[`prec',2])/(`p1'-`p2')
			  }
			  matrix `mat2``i'''[`j',1]=`cint'
              foreach c in 2 3 {
			     local yprec=`mat2``i'''[`prec',`c']
			     local ysuiv=`mat2``i'''[`suiv',`c']
			     local xprec=`mat2``i'''[`prec',1]
			     local xsuiv=`mat2``i'''[`suiv',1]
			     local x=`cint'
				 if (`xsuiv'==`xprec') {
				     matrix `mat2``i'''[`j',`c']=`mat2``i'''[`prec',`c']
				 }
				 else {
		             *di "matrix `mat2``i'''[`j',`c']=`yprec'+(`x'-`xprec')/(`xsuiv'-`xprec')*(`ysuiv'-`yprec')"
		             matrix `mat2``i'''[`j',`c']=`yprec'+(`x'-`xprec')/(`xsuiv'-`xprec')*(`ysuiv'-`yprec')
				 }
			  }
		   }
		   else {
		      local prec=`j'-1
			  forvalues c=1/3 {
			      matrix `mat2``i'''[`j',`c']=`mat2``i'''[`prec',`c']
			  }
		   }
		   local j=`j'+2
	   }
	   *matrix list `mat2``i'''
	   local aabc``i''=0
	   *set trace on
	   forvalues j=2/`nbl' {
	      local prec=`j'-1
		  local x=`mat2``i'''[`j',1]
		  local xprec=`mat2``i'''[`prec',1]
		  local y2=`mat2``i'''[`j',2]
		  local y3=`mat2``i'''[`j',3]
		  local yprec2=`mat2``i'''[`prec',2]
		  local yprec3=`mat2``i'''[`prec',3]
	      local aabc=(abs(`y2'-`y3')+abs(`yprec2'-`yprec3'))/2*abs(`x'-`xprec')
	      matrix `mat2``i'''[`j',4]=`aabc'
		  local aabc``i''=`aabc``i'''+`aabc'
	   }
       return scalar AABC``i''=`aabc``i'''
       *set trace off

	   local testsb `testsb' c``i''=r(AABC``i'')
	   *matrix list `mat2``i'''
       *di "return scalar AABC``i''=`aabc``i'''"
	   *matrix list `mat2``i'''
    }
	*set trace on
	if "`bsaabc'"=="" {
	    *di "on lance le bootstrap AABC"
		tempfile file3
		qui save `file3', replace
		qui use `file1', clear
		*di "bootstrap `testsb' , rep(`nbbootstrap') nol noh nodots saving(C:\Users\jean-\Documents\tmp\essai.dta, replace) : nopalera `varlist', fin nograph `restscore' nobsauc nobsaabc "
		*set trace on
		*set tracedepth 2
		tempfile filebsaabc
		*di "hello c'est l'heure du BS"
		*qui bootstrap `testsb' , rep(`nbaabcbs') nol noh nodots saving("`filebsaabc'", replace) trace : nopalera `varlist', nograph `restscore' nobsauc nobsaabc
		capture qui bootstrap `testsb' , rep(`nbaabcbs') saving("`filebsaabc'", replace)  : nopalera `varlist', nograph `restscore' nobsauc nobsaabc
		local n=1
		while (_rc!=0) {
		    di "`n'. The boostrap to obtain IC of the AABC has not been correctly performed, it is run again"
      		local ++n
			capture qui bootstrap `testsb' , rep(`nbaabcbs') saving("`filebsaabc'", replace)  : nopalera `varlist', nograph `restscore' nobsauc nobsaabc
		}
		*return list
		tempname mbootstrapb
		matrix `mbootstrapb'=r(table)
		*di "MATRIX mbootstrapb  `mbootstrapb'"
		*matrix list `mbootstrapb'
        qui use "`filebsaabc'", replace
		*su
		local thres=`nbaabcbs'-ceil(`nbaabcbs'/20)
		forvalues i=1/`nbitems' {
		   sort c``i''
		   qui su c``i'' in `thres'
		   local upper``i''=r(mean)
		   *di "Upper ``i'' : `upper``i'''"
		}
		di
		di "Research of non-uniform recalibration at item-level"
		di
		di "{hline 103}"
		di                                _col(36) "Bootstrap"       _col(56) "Uniform"      _col(72) "Non-uniform"
		di "Items" _col(23) "AABC" _col(29) "[95% Conf. Int.]" _col(50) "Recalibration"    _col(70) "Recalibration"  _col(88) "Reprioritization"
		di "{hline 103}"
		forvalues i=1/`nbitems' {
		   local xur=""
		   local xnur=""
		   local xrep=""
		   if `mbootstrap'[4,`i']<=0.05 {
		       local xur="X"
		   }
		   if (`aabc``i'''>`upper``i'''&"`xur'"=="X") {
		       local xnur="X"
		   }
		   if (`aabc``i'''>`upper``i'''&"`xur'"=="") {
		       local xrep="X"
		   }

		   di "``i''/``=`i'+`nbitems'''" _col(23) %5.2f `aabc``i''' _col(36) "[" %1.0f  0 "-" %5.2f `upper``i''' "]" _col(62) "`xur'" _col(82) "`xnur'" _col(102) "`xrep'"
		}
		di "{hline 103}"
		local abc=1
	}
	if "`graph'"=="" {
	    forvalues i=1/`nbitems' {
			qui drop _all
			qui svmat `mat2``i'''
			qui rename `mat2``i'''1 score
			qui rename `mat2``i'''2 t1
			qui rename `mat2``i'''3 at2
			qui sort score
			if "`restscore'"=="" {
					 local xtitle="Rest-score"
					 qui su score
					 local max=r(max)
			}
			else {
					 local xtitle="Score"
					 local max `maxscore'
			}
			list *
			graph twoway (line t1 score ) (line at2 score ) if score>0&score<=`maxscore', title("Traceline of item ``i''") xtitle("`xtitle'") ytitle("Expected value of item ``i''", size(small)) ylabel(0(1)`modamax') xlabel(0(1)`max',valuelabel labsize(small) /*alternate*/) name("aabc``i''", replace) legend(off)
		}
	}
}
qui restore , preserve
end