program define raschpce,rclass
syntax varlist [if] [in] [, rsm]
preserve
if "`if'"!=""|"`in'"!="" {
    di "if:`if' in: `in'"
    keep `if' `in'
}
tokenize `varlist'

local nbitems:word count `varlist'

local lines=1
local summ=0
local modamax=0
forvalues i=1/`nbitems' {
   qui su ``i''
   local moda`i'=r(max)
   local lines=`lines'*(`moda`i''+1)
   local summ=`summ'+`moda`i''
   if `modamax'<`moda`i'' {
       local modamax `moda`i''
   }
}
local col=1+`nbitems'
*di "matrix table=J(`lines',`col',.)"


*matrix table=J(`lines',`col',.)

local line=1
/*forvalues i=1/`nbitems' {
   forvalues j=`=`i'+1'/`nbitems' {
      forvalues mi=0/`moda`i'' {
         forvalues mj=0/`moda`j'' {
             matrix table[`line',`i']=`mi'
             matrix table[`line',`j']=`mj'
             qui count if ``i''==`mi'&``j''==`mj'
             matrix table[`line',`=`nbitems'+1']=r(N)
             local ++line
         }
      }
   }
}
*/

local line=1
forvalues i=1/`nbitems' {
   forvalues j=`=`i'+1'/`nbitems' {
      forvalues mi=0/`moda`i'' {
         forvalues mj=0/`moda`j'' {
             qui count if ``i''==`mi'&``j''==`mj'
             local table_`line'_`=`nbitems'+1'=r(N)
             local ++line
         }
      }
   }
}
qui drop _all
set obs `=`line'-1'
local line=1
forvalues i=1/`nbitems' {
   qui gen table`i'=.
}
qui gen table`=`nbitems'+1'=.
*set trace on
forvalues i=1/`nbitems' {
   forvalues j=`=`i'+1'/`nbitems' {
      forvalues mi=0/`moda`i'' {
         forvalues mj=0/`moda`j'' {
            qui replace table`i'=`mi' in `line'
            qui replace table`j'=`mj' in `line'
            qui replace table`=`nbitems'+1'=`table_`line'_`=`nbitems'+1'' in `line'
            local ++line
         }
      }
   }
}
*qui svmat table
local ti
local vi
forvalues i=1/`nbitems' {
    rename table`i' ``i''
}
rename table`=`nbitems'+1' freq
local s="0"
local max=0
forvalues i=1/`nbitems' {
   local max=`max'+`moda`i''
   local bi`i'm0=0
   local s="`s'+vi`i'mi`moda`i''"
   forvalues j=`=`i'+1'/`nbitems'  {
      qui gen ti`i'j`j'=``i''+``j''
      qui replace ti`i'j`j'=0 if ti`i'j`j'==.
      local ti "`ti' i.ti`i'j`j'"
   }
   forvalues mi=1/`moda`i'' {
      qui gen vi`i'mi`mi'=``i''==`mi'
      local vi "`vi' vi`i'mi`mi'"
   }
}
local s="(`s')/`max'"

count
if "`rsm'"=="" {
   *edit
   qui xi:poisson freq `vi' `ti', nocons  iterate(30)
   local ll=e(ll)
  *set trace on

   *lincom -(vi1mi1-(vi1mi3+vi2mi3+vi3mi3+vi4mi3+vi5mi3)/15)
   *lincom -(vi1mi2-(vi1mi3+vi2mi3+vi3mi3+vi4mi3+vi5mi3)/15-vi1mi1)
   *lincom -(vi1mi3-(vi1mi3+vi2mi3+vi3mi3+vi4mi3+vi5mi3)/15-vi1mi2)
   *lincom -.5*(vi1mi1-(vi1mi2+vi2mi2+vi3mi2+vi4mi2+vi5mi2+vi6mi2+vi7mi2)/14)-.5*(vi1mi2-(vi1mi2+vi2mi2+vi3mi2+vi4mi2+vi5mi2+vi6mi2+vi7mi2)/14-vi1mi1)

    qui matrix b=e(b)
    local col=1
    forvalues i=1/`nbitems' {
       forvalues mi=1/`moda`i'' {
          local bi`i'm`mi'=b[1,`col']
          local ++col
       }
    }
    *local s=`s'/`max'
    *set trace on
    forvalues i=1/`nbitems' {
       local location`i'="0"
       forvalues mi=1/`moda`i'' {
          local delta`i'm`mi'=-(`bi`i'm`mi''-`mi'*`s'-`bi`i'm`=`mi'-1'')
          if (`mi'==1) {
             qui di "-(vi`i'mi`mi'-`s')"
             local tdelta`i'_1 "-(vi`i'mi`mi'-`s')"
             qui lincom -(vi`i'mi`mi'-`s')
             local location`i' "`location`i''-(vi`i'mi`mi'-`s')"
          }
          else {
    /*QUESTION : DOIT-ON MULTIPLIER `s' par `mi'*/
             qui di " vi`i'mi`=`mi'-1'-vi`i'mi`mi'+`s'"
             local tdelta`i'_`mi' " vi`i'mi`=`mi'-1'-vi`i'mi`mi'+`s'"
             qui lincom vi`i'mi`=`mi'-1'-vi`i'mi`mi'+`s'
             local location`i' "`location`i''+vi`i'mi`=`mi'-1'-vi`i'mi`mi'+`s'"
          }
          local delta`i'_`mi'=r(estimate)
          local sedelta`i'_`mi'=r(se)
       }
       qui lincom  (`location`i'')/`moda`i''
       local delta`i'=r(estimate)
       local sedelta`i'=r(se)
    }
    forvalues i=1/`nbitems' {
       forvalues mi=1/`moda`i'' {
          di "delta`i'_`mi' : `delta`i'_`mi'' (`sedelta`i'_`mi'')"
       }
          di "delta`i' : `delta`i'' (`sedelta`i'')"
    }
}
else {
   qui gen vmi2=vi1mi2+vi2mi2+vi3mi2
   drop vi1mi2 vi2mi2 vi3mi2
   edit
   qui xi:poisson freq vi1mi1 vi2mi1 vi3mi1 vmi2 `ti', nocons
   local ll=e(ll)
   *set trace on

   *lincom -(vi1mi1-(vi1mi3+vi2mi3+vi3mi3+vi4mi3+vi5mi3)/15)
   *lincom -(vi1mi2-(vi1mi3+vi2mi3+vi3mi3+vi4mi3+vi5mi3)/15-vi1mi1)
   *lincom -(vi1mi3-(vi1mi3+vi2mi3+vi3mi3+vi4mi3+vi5mi3)/15-vi1mi2)
   *lincom -.5*(vi1mi1-(vi1mi2+vi2mi2+vi3mi2+vi4mi2+vi5mi2+vi6mi2+vi7mi2)/14)-.5*(vi1mi2-(vi1mi2+vi2mi2+vi3mi2+vi4mi2+vi5mi2+vi6mi2+vi7mi2)/14-vi1mi1)

    forvalues i=1/`nbitems' {
       local location`i'="0"
       forvalues mi=1/`moda`i'' {
*          local delta`i'm`mi'=-(`bi`i'm`mi''-`mi'*`s'-`bi`i'm`=`mi'-1'')
          if (`mi'==1) {
             qui di "-(vi`i'mi1-vi2)"
             local tdelta`i'_1 "-(vi`i'mi1-vi2)"
             qui lincom -(vi`i'mi1-vi2)
             local location`i' "`location`i''-(vi`i'mi1)"
          }
          else {
    /*QUESTION : DOIT-ON MULTIPLIER `s' par `mi'*/
             qui di " vi`i'mi`=`mi'-1'"
             local tdelta`i'_`mi' " vi`i'mi`=`mi'-1'"
             qui lincom vi`i'mi`=`mi'-1'
             local location`i' "`location`i''+vi`i'mi`=`mi'-1'"
          }
          local delta`i'_`mi'=r(estimate)
          local sedelta`i'_`mi'=r(se)
       }
       qui lincom  (`location`i'')/`moda`i''
       local delta`i'=r(estimate)
       local sedelta`i'=r(se)
    }
    forvalues i=1/`nbitems' {
       forvalues mi=1/`moda`i'' {
          di "delta`i'_`mi' : `delta`i'_`mi'' (`sedelta`i'_`mi'')"
       }
          di "delta`i' : `delta`i'' (`sedelta`i'')"
    }
}
*set trace on
matrix delta=J(`nbitems',`modamax',.)
matrix sedelta=J(`nbitems',`modamax',.)
forvalues i=1/`nbitems' {
   forvalues mi=1/`moda`i'' {
       matrix delta[`i',`mi']=`delta`i'_`mi''
       matrix sedelta[`i',`mi']=`sedelta`i'_`mi''
   }
}
*set trace on
matrix b=delta
matrix list delta
*matrix delta=delta'
*matrix sedelta=sedelta'
return matrix b=delta
return matrix sedelta=sedelta
return scalar ll=`ll'
restore



*set trace on
matrix list b
*pcm item*, fixed(b)



end
