program define simul2tl2plm
version 7.0
syntax  [, nbobs(integer 2000) dim1(real 7) dim2(real 7) corr(real 0) disc1(real 1) disc2(real 1) sigma1(real 1) sigma2(real 1)]

scalar define hour=real(substr("$S_TIME",1,2))
scalar define min=real(substr("$S_TIME",4,2))
scalar define sec=real(substr("$S_TIME",7,2))
scalar define jour=real(substr("$S_DATE",1,2))

if "$seed2"!="" {
global seed2=int($seed/100000)}
else {
global seed2=0
}
global seed=sec*1000000+min*10000+hour*100+jour+$seed2

set seed $seed
quietly {
drop _all
set obs `nbobs'
gen TL1=invnorm(uniform())*`sigma1'
gen TL2=invnorm(uniform())*`sigma2'
replace TL2=`corr'*TL1+sqrt(1-`corr'^2)*TL2

local items=1
global rep1
while `items'<=`dim1' {
local nitems=100+`items'
gen diff`nitems'=invnorm(`items'/(`dim1'+1))
gen prob`nitems'=1/(1+exp(-1.7*`disc1'*(TL1-diff`nitems')))
gen rep`nitems'=0
replace rep`nitems'=1 if prob`nitems'>=uniform()
global rep1 $rep1 rep`nitems'
local items=`items'+1
}

local items=1
global rep2
while `items'<=`dim2' {
local nitems=200+`items'
gen diff`nitems'=invnorm(`items'/(`dim2'+1))
gen prob`nitems'=1/(1+exp(-1.7*`disc2'*(TL2-diff`nitems')))
gen rep`nitems'=0
replace rep`nitems'=1 if prob`nitems'>=uniform()
global rep2 $rep2 rep`nitems'
local items=`items'+1
}
gen ind=_n
keep ind $rep1 $rep2}


quietly{
reshape long rep,i(ind) j(item)

local items=1
while `items'<=`dim1' {
local nitems=100+`items'
gen item`nitems'=0
replace item`nitems'=-1 if item==`nitems'
local items=`items'+1
}
local items=1
while `items'<=`dim2' {
local nitems=200+`items'
gen item`nitems'=0
replace item`nitems'=-1 if item==`nitems'
local items=`items'+1
}

}/*fin du quietly*/
end
