*! version 1  january 25th, 2010
*! Jean-Benoit Hardouin
************************************************************************************************************
* raschpower: Estimation of the power of the Wald test in order to compare the means of the latent trait in two groups of individuals
*
* Version 1 : January 25, 2010 (Jean-Benoit Hardouin)
*
* Jean-benoit Hardouin, Faculty of Pharmaceutical Sciences - University of Nantes - France
* jean-benoit.hardouin@univ-nantes.fr
*
* News about this program : http://www.anaqol.org
* FreeIRT Project : http://www.freeirt.org
*
* Copyright 2010 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 raschpower1,rclass
syntax [varlist] [, n0(int 100) n1(int 100) gamma(real .5) d(string) var(real 1) nodes(int 12)]



if "`d'"=="" {
   tempname d
   matrix `d'=[-1\-.5\0\.5\1]
}

/*tempname abs weight
ghquadm `nodes' `abs' `weight'
matrix `abs'=`abs''
matrix `weight'=`weight''

matrix `abs'=`abs'*sqrt(`var')
*matrix list `abs'
matrix `abs'=[5.50090170446774,4.27182584793228,3.22370982877010,2.25946445100080,1.34037519715162,0.444403001944139,-5.50090170446774,-4.27182584793228,-3.22370982877010,-2.25946445100080,-1.34037519715162,-0.444403001944139]
matrix `abs'=`abs''*sqrt(`var')
matrix `weight'=[0.000000375975985,0.000121250244966,0.005523056331147,0.072984713184739,0.368391758069477,0.806292983509187,0.000000375975985,0.000121250244966,0.005523056331147,0.072984713184739,0.368391758069477,0.806292983509187]
matrix `weight'=`weight''
*matrix list `abs'
*/
local nbitems=rowsof(`d')

di in gr "Number of individuals in the first group:  " in ye `n0'
di in gr "Number of individuals in the second group: " in ye `n1'
di in green "Group effect: " in ye `gamma'
di in  gr "Variance of the latent trait: " in ye `var'
di in gr "Number of items: " in ye `nbitems'
di in green "Difficulties parameters of the items: " _c
tempname dd
matrix `dd'=`d''
matrix list `dd',noblank nohalf nonames noheader



clear
local temp=2^(`nbitems')
qui range x 0 `=`temp'-1' `temp'
qui g t=x
loc i=1
qui count if t>0
loc z=r(N)
qui while `z'>0 {
  qui g item`i'=mod(t,2^`i')==2^`=`i'-1'
  qui replace t=t-item`i'*2^`=`i'-1'
  qui count if t>0
  loc z=r(N)
  loc i=`i'+1
}
drop t
qui expand 2
qui gen group=0 in 1/`temp'
qui replace group=1 in `=`temp'+1'/`=2*`temp''
qui gen mean=(-1)^(1-group)*`gamma'*sqrt(`var')/2
qui gen proba=1
forvalues i=1/`nbitems' {
   qui gen eps`i'=exp(mean-`d'[`i',1])
   qui replace proba=proba*eps`i'^item`i'/(1+eps`i')
}
qui gen eff=.
forvalues i=0/1 {
   qui replace eff=proba*`n`i'' if group==`i'
}
forvalues i=1/`nbitems' {
   qui gen f`i'=eps`i'^item`i'/(1+eps`i')
   qui gen fp`i'=(item`i'*eps`i'^item`i'+(item`i'-1)*eps`i'^(item`i'+1))/(1+eps`i')^2
   qui gen fpp`i'=((item`i'^2*eps`i'^item`i'+(item`i'^2-1)*eps`i'^(item`i'+1))*(1+eps`i')^2-2*eps`i'*(1+eps`i')*(item`i'*eps`i'^item`i'+(item`i'-1)*eps`i'^(item`i'+1)))/(1+eps`i')^4
}
qui replace eff=proba
keep item* eff group proba

local p1=1/`n1'
local p0=1/`n0'
qui gen eff2=.
qui replace eff2=floor(eff/`p1') if group==1
qui replace eff2=floor(eff/`p0') if group==0
qui replace eff=eff-eff2*(`p1'*group+`p0'*(1-group))
qui su eff2 if group==1
local aff1=r(sum)
qui su eff2 if group==0
local aff0=r(sum)

*di "Nombre de patients affectes : `aff1' dans groupe 1 (sur `n1') et `aff0' dans le groupe 0 (sur `n0')"

local unaff1=`n1'-`aff1'
local unaff0=`n0'-`aff0'
*di "Nombre de patients non affectes : `unaff1' dans groupe 1 (sur `n1') et `unaff0' dans le groupe 0 (sur `n0')"

qui gsort + group - eff
qui replace eff2=eff2+1 in 1/`unaff0'
qui gsort - group - eff
qui replace eff2=eff2+1 in 1/`unaff1'

*list eff eff2 group proba

qui drop if eff2==0

qui expand eff2
qui gen i=_n

tempname diff
matrix `diff'=`d''

/***************************A REVOIR
forvalues i=1/`nbitems' {
    qui su item`i'
	local var=r(Var)
	if `var'==0 {
	    qui drop item`i'
    }
}
****************************FIN A REVOIR*/

*irtpoly item*,  fixedvar(1) rasch fixed(`diff') covariablemean(group) sasout

qui drop proba eff eff2
qui reshape long item, i(i)
qui rename item rep
qui rename _j item

qui gen  offset=0
forvalues i=1/`nbitems' {
    qui replace offset=-`diff'[1,`i'] if item==`i'
}
constraint 1 _cons=0
qui gen groupc=group-.5
 xtlogit rep groupc  ,nocons i(i) offset(offset) constraint(1)

tempname b V
matrix `b'=e(b)
matrix `V'=e(V)
local gammaest=`b'[1,1]
local se=`V'[1,1]^.5
di
di
di in gr "{hline 76}"
di _col(50)  "Estimation with the "
di _col(40)  "Cramer-Rao bound" _col(60) "classical formula"
di in gr "{hline 76}"
di in green "Estimated value of the group effect" _col(49) in ye  %7.2f `gammaest'
di in green "Standard Error of this estimation" _col(49) in ye %7.2f `se'
di in green "Variance if this estimation" _col(46) in ye %10.4f `=`se'^2'
local power=1-normal(1.96-`gamma'/`se')
local clpower=normal(sqrt(`n0'*`gamma'^2/2)-1.96)
di in green "Estimated value of the power" _col(50) in ye %6.4f `power' _col(71) in ye %6.4f `clpower'
local clnsn=2/`gamma'^2*(1.96-invnorm(1-`power'))^2
di in green "Number of patients for a power of" %6.2f `=`power'*100' "%" _col(49) in ye `n0' "/" `n1' _col(62) in ye %7.2f `clnsn' "/" %7.2f `clnsn'
di in green "Ratio of the number of patients" in ye %6.2f _col(55)`=(`n0'+`n1')/(2*`clnsn')'
di in gr "{hline 76}"

return scalar EstGamma=`gammaest'
return scalar CRbound=`=`se'^2'
return scalar CRPower=`power'
return scalar ClPower=`clpower'
return scalar ClSS=`clnsn'
return scalar Ratio=`=`n0'/`clnsn''




end
