program define testclv
syntax [,corr(real 0) nosim]

clear
qui set matsize 800
qui set memory 128m
set more off

if `corr'==0 {
   local rho 0.0
}
else if `corr'==1 {
   local rho 1.0
}
else {
   local rho 0`corr'
}

if "`sim'"=="" {
   capture use "C:\Documents and Settings\Jean-Benoit\Mes documents\THESE\clv\simul (rho=`rho')\result.dta", clear
   if _rc==0 {
      mkmat *,matrix(result)
      use "C:\Documents and Settings\Jean-Benoit\Mes documents\THESE\clv\simul (rho=`rho')\resultpoly.dta", clear
      mkmat *,matrix(resultpoly)
      use "C:\Documents and Settings\Jean-Benoit\Mes documents\THESE\clv\simul (rho=`rho')\result2.dta", clear
      mkmat *,matrix(result2)
      use "C:\Documents and Settings\Jean-Benoit\Mes documents\THESE\clv\simul (rho=`rho')\resultpoly2.dta", clear
      mkmat *,matrix(resultpoly2)

      count if resultpoly21!=0
      local deb=r(N)+1
   }
   else {
      matrix result=J(100,14,0)
      matrix resultpoly=J(100,14,0)
      matrix result2=J(100,14,0)
      matrix resultpoly2=J(100,14,0)
      local deb=1
   }
   forvalues i=`deb'/100 {
      di "`i'" _c
      qui simirt,dim(7 7) cov(1 1 `rho') clear nbobs(500) store(C:\Documents and Settings\Jean-Benoit\Mes documents\THESE\clv\simul (rho=`rho')\data`i') replace
      di "." _c
      qui clv i*,nodendro nobiplot
      di "." _c
      local group:word 1 of `r(bestthresold)'
      local group2:word 2 of `r(bestthresold)'
      qui clv,nodendro nobiplot cons(`group')
      di "." _c
      matrix tmp=r(affect)
      matrix result[`i',1]=tmp
      di "." _c
      qui clv,nodendro nobiplot cons(`group2')
      di "." _c
      matrix tmp=r(affect)
      matrix result2[`i',1]=tmp

      di "." _c
      qui clv i*,nodendro nobiplot method(polychoric)
      di "." _c
      local group:word 1 of `r(bestthresold)'
      local group2:word 2 of `r(bestthresold)'
      qui clv,nodendro nobiplot cons(`group')
      di "."
      matrix tmp=r(affect)
      matrix resultpoly[`i',1]=tmp
      di "." _c
      qui clv,nodendro nobiplot cons(`group2')
      di "."
      matrix tmp=r(affect)
      matrix resultpoly2[`i',1]=tmp

      drop _all
      svmat result
      save "C:\Documents and Settings\Jean-Benoit\Mes documents\THESE\clv\simul (rho=`rho')\result",replace

      drop _all
      svmat resultpoly
      save "C:\Documents and Settings\Jean-Benoit\Mes documents\THESE\clv\simul (rho=`rho')\resultpoly",replace

      drop _all
      svmat result2
      save "C:\Documents and Settings\Jean-Benoit\Mes documents\THESE\clv\simul (rho=`rho')\result2",replace

      drop _all
      svmat resultpoly2
      save "C:\Documents and Settings\Jean-Benoit\Mes documents\THESE\clv\simul (rho=`rho')\resultpoly2",replace

   }
}
foreach file in "result" "result2" "resultpoly" "resultpoly2" "result2nd" "result2nd2" {
   drop _all
   di "`file'"
   use "C:\Documents and Settings\Jean-Benoit\Mes documents\THESE\clv\simul (rho=`rho')\\`file'",clear
   quietly {
      forvalues i=1/7 {
         rename `file'`i' itemA`i'
         rename `file'`=`i'+7' itemB`i'
      }
      gen conc=0
      gen dis=0
      forvalues i=1/7 {
         forvalues j=`=`i'+1'/7 {
            replace conc=conc+1 if itemA`i'==itemA`j'
            replace conc=conc+1 if itemB`i'==itemB`j'
         }
      }
      forvalues i=1/7 {
         forvalues j=1/7 {
            replace dis=dis+1 if itemA`i'==itemB`j'
         }
      }
      gen result=.
      replace result=1 if conc==42&dis==0&result==.
      replace result=3 if conc<42&dis==0&result==.
      replace result=4 if dis>0&conc>0&result==.
      replace result=5 if dis==49&result==.
      egen nbcl=rowmax(item*)
   }
   if "`file'"=="result" {
      local text="CLV Classique"
   }
   if "`file'"=="result2" {
      local text="CLV Classique 2e choix"
   }
   if "`file'"=="resultpoly" {
      local text="CLV Polychoric"
   }
   if "`file'"=="resultpoly2" {
      local text="CLV Polychoric 2e choix"
   }
   if "`file'"=="result2nd" {
      local text="CLV 2nd ordre "
   }
   if "`file'"=="result2nd2" {
      local text="CLV 2nd ordre 2e choix"
   }
   di in gr "`text'"
   tab result
}

end