*! Version 1.1 24 March 2015
*! Jean-Benoit Hardouin
************************************************************************************************************
* Stata program : gengutt
* Generation of Guttman's error per individuals
* Release 1.1 : March 24, 2015  /*no required -order- option*/
*
*
* Jean-benoit Hardouin, phD, Assistant Professor
* Team of Biostatistics, Pharmacoepidemiology and Subjective Measures in Health Sciences  (UPRES EA 4275 SPHERE)
* University of Nantes - Faculty of Pharmaceutical Sciences
* France
* jean-benoit.hardouin@anaqol.org
*
* Release 1 : March 20, 2015   /*initial version with only -order- option*/
*
* 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 gengutt , rclass
syntax varlist(min=2 numeric) [if] , Gen(string) [ Replace Order(string) PAIRWise] /*[,PAIRWise PAIR ppp pmm noADJust GENERror(string) REPlace GRaph MONOtonicity(string) NIRESTscore(string) NIPmatrix(string) HTML(string)]*/
preserve

tokenize `varlist'
local nbitems : word count `varlist'
local nbs=0
forvalues i=1/`nbitems' {
    qui su ``i''
	local modamax`i'=r(max)
	local nbs=`nbs'+`modamax`i''
	local testmodamax`i'=0
    local ordermax`i'=0
}
if "`order'"=="" {
   tempname mattmp
   matrix `mattmp'=J(3,`nbs',0)
   local col=1
   forvalues i=1/`nbitems' {
      forvalues s=1/`modamax`i'' {
	     qui count if ``i''>=`s'
		 matrix `mattmp'[3,`col']=r(N)
		 matrix `mattmp'[1,`col']=`i'
		 matrix `mattmp'[2,`col']=`s'
		 local ++col
	  }
   }
   local list
   forvalues j=1/`nbs' {
      local max=0
      local col=0
      local item=0
      forvalues i=1/`nbs' {
         if `mattmp'[3,`i']>`max' {
	        local max=`mattmp'[3,`i']
		    local col=`i'
		    local item=`mattmp'[1,`i']
		 }
	  }
	  local list `list' `item'
	  matrix `mattmp'[3,`col']=0 
   }
   local order `list'
}
local nbsteps: word count `order'
tempname matorder
matrix `matorder'=J(3,`nbsteps',0)
local defordre=1
forvalues i=1/`nbsteps' {
   local tmp: word `i' of `order'
   local ++testmodamax`tmp'
   matrix `matorder'[1,`i']=`tmp'
   local ordermax`tmp'=`ordermax`tmp''+1
   matrix `matorder'[2,`i']=`ordermax`tmp''	   
   local i`tmp'_m`ordermax`tmp''=`defordre'
   local s`defordre' i`tmp'_m`ordermax`tmp''
   tempname `s`defordre''
   qui gen ``s`defordre'''=``tmp''>=`ordermax`tmp''
   local ++defordre
}
local error=0
forvalues i=1/`nbitems' {
  if `modamax`i''>`testmodamax`i'' {
	  di in red "The item `i' presents more answer categories than defined in the -order- option "
	  local error=1
  }
}
if `error'!=0 {
  error 198
}   
if "`replace'"!="" {
  qui capture drop `gen'
}
qui gen `gen'=0
forvalues i=1/`nbitems' {
   if "`replace'"!="" {
	  qui capture drop `gen'_``i''
   }
   qui gen `gen'_``i''=0
}
forvalues i=1/`nbsteps' {
   forvalues j=`=`i'+1'/`nbsteps' {
	  qui replace `gen'=`gen'+1 if ``s`i'''<``s`j'''&``s`i'''!=.&``s`j'''!=.
	  local iti=`matorder'[1,`i']
	  local itj=`matorder'[1,`j']
	  qui replace `gen'_``iti''=`gen'_``iti''+1 if ``s`i'''<``s`j'''&``s`i'''!=.&``s`j'''!=.
	  qui replace `gen'_``itj''=`gen'_``itj''+1 if ``s`i'''<``s`j'''&``s`i'''!=.&``s`j'''!=.
   }
}
if "`replace'"!="" {
    capture drop `gen'_gp
}
egen `gen'_gp=concat(``s1''-``s`nbsteps''')

capture restore, not
end