*! Version 1 20July2015
************************************************************************************************************
* rsbynpirt: Traces of items
* Version 1: July 20, 2015 /*ICC*/
*
* Historic:
* Version 1 (2015-07-20): Jean-Benoit Hardouin
*
* Jean-benoit Hardouin, phD, Assistant Professor
* Team of Biostatistics, Clinical Research and Subjective Measures in Health Sciences
* University of Nantes - Faculty of Pharmaceutical Sciences
* France
* jean-benoit.hardouin@anaqol.org
*
* News about this program :http://www.anaqol.org
*
* Copyright 2015 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 rsbynpirt
version 8.0
syntax varlist(numeric min=4) [,  noGraph]

preserve
local nbitems : word count `varlist'
tokenize `varlist'
if mod(`nbitems',2)!=0 {
   di in red "You must indicate an even number of items"
   exit
}
else {
   local nbitems=`nbitems'/2
}
local listofitems1
local listofitems2
forvalues i=1/`nbitems' {
   local listofitems1 `listofitems1' ``i''
   local listofitems2 `listofitems2' ``=`i'+`nbitems'''
}

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' {
   qui replace `varscore1'=`varscore1'+``i''
   qui replace `varscore2'=`varscore2'+``=`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''
   }
}


if `flag'==1 {
   di as error "The lower modality of the item must be 0"
   exit
}
if "`flagbin'"!=""&"`logistic'"!="" {
   di as error "The logistic option is not possible with polytomous items"
   exit
}

qui su `varscore1'
local maxscore=r(max)
qui su `varscore1'
if `r(max)'>`maxscore' {
   local maxscore=r(max)
}
tempfile rsbynpirtfile rsbynpirtfile1 rsbynpirtfile2
tempvar score
qui save `rsbynpirtfile', replace
qui traces `listofitems1', nograph icc saveicc
qui drop _all
*matrix list r(matscore)
tempname mat1
qui matrix `mat1'=r(matscore)
qui svmat `mat1', names(t1item)
forvalues i=1/`nbitems' {
   local j: word `i' of `listofitems1'
   qui rename t1item`i' `j'
}
qui rename t1item`=`nbitems'+1' `score'
qui contract `score' `listofitems1'
qui sort `score'
qui save `rsbynpirtfile1', replace

qui use `rsbynpirtfile', clear
qui traces `listofitems2', nograph icc saveicc
qui drop _all
tempname mat2
qui matrix `mat2'=r(matscore)
qui svmat `mat2', names(t2item)
forvalues i=1/`nbitems' {
   local j: word `i' of `listofitems2'
   qui rename t2item`i' `j'
}
qui rename t2item`=`nbitems'+1' `score'
qui contract `score' `listofitems2'
qui sort `score'
qui merge 1:1 `score' using `rsbynpirtfile1'

if "`graph'"=="" {
   forvalues i=1/`nbitems' {
      twoway (line ``i'' `score') (line ``=`i'+`nbitems''' `score'), name(``i'',replace)
   }
}
qui drop if `score'==0|`score'==`scoremax'

di
di "Items" _col(18) "AUC t1" _col(28) "AUC t2" _col(38) "Var AUC" _col(46) "Diff ICC" _col(57) "SD Diff"
di "{hline 63}"
forvalues i=1/`nbitems' {
   qui su ``i'' 
   local AUC``i''=r(sum)
   local AUC``i''=(`AUC``i'''+`modamax`i''/2)/(`scoremax'*`modamax`i'')*100
   qui su ``=`i'+`nbitems''' 
   local AUC`=`i'+`nbitems''=r(sum)
   local AUC`=`i'+`nbitems''=(`AUC`=`i'+`nbitems'''+`modamax`i''/2)/(`scoremax'*`modamax`i'')*100
   tempname diff``i''
   gen `diff``i'''=abs(``i''-``=`i'+`nbitems''')
   qui su `diff``i'''
   local d``i''=`r(sum)'/(`scoremax'*`modamax`i'')*100
   local var``i''=`r(sd)'*100
   di "``i''/``=`i'+`nbitems'''" _col(20) %4.1f `AUC``i''' _col(30) %4.1f `AUC`=`i'+`nbitems''' _col(40) %5.1f `=`AUC``i'''-`AUC`=`i'+`nbitems'''' _col(50) %4.1f `d``i''' _col(60) %4.1f `var``i'''
}
qui restore , preserve
end


