/*
     feldti: Feldt's W statistic for two reliability
             coefficients comparison
     By: HervŽ CACI (hcaci@wanadoo.fr)

     feldti alpha1 alpha2, k1() n1() k2() n2()
*/

program define feldti
version 5.0
*! Version 1.0 -- August 28th, 1999

tempname W df1 df2 df3 df4 A B xn2 xk2
parse "`*'", parse(" ,")

confirm number `1'
local r1 `1' /* Reliability coefficient #1 */
if `r1'>=1 | `r1'<=0 {
  di in red "Alpha1 is not in range"
  exit
}
confirm number `2'
local r2 `2' /* Reliability coefficient #2 */
if `r2'>=1 | `r2'<=0 {
  di in red "Alpha2 is not in range"
  exit 198
}

mac shift 2
local options "K1(int 1) N1(int 1) K2(int 1) N2(int 1)"
parse "`*'"

if `n1'<=1 {
  di in red "Out of range or Missing: number of observations in test 1"
  exit 198
}
scalar `df1'=`n1'-1

if `n2'==1 {
  scalar `df2' = `df1'
  scalar `xn2' = `n1'
}
else if `n2'<=0 {
       di in red "Out of range: number of observations in test 2"
       exit 198
     }
     else {
       scalar `df2'=`n2'-1
       scalar `xn2'=`n2'
     }

if `k1'<=1 {
  di in red "Out of range or Missing: number of items in test 1"
  exit 198
}
scalar `df4'=`df1'*(`k1'-1)
if `k2'==1 {
  scalar `df3' = `df4'
  scalar `xk2' = `k1'
}
else if `k2'<=0 {
       di in red "Out of range: number of items in test 2"
       exit 198
     }
     else {
       scalar `df3'=`df2'*(`k2'-1)
       scalar `xk2'=`k2'
     }

if `n1'<=100 | `n2'<=100 {
  scalar `A'=(`df4'/(`df4'-2))*(`df2'/(`df2'-2))
  scalar `B'=((`df1'+2)*`df4'^2)/((`df4'-2)*(`df4'-4)*`df1')
  scalar `B'=`B'*((`df3'+2)*`df2'^2)/((`df2'-2)*(`df2'-4)*`df3')
  scalar `df1'=2*`A'^2/(2*`B'-`A'*`B'-`A'^2)
  scalar `df2'=2*`A'/(`A'-1)
}

scalar `W' = (1-`r1')/(1-`r2')

di in gr _n "Feldt's W-statistic" _n _dup(20) "-" _n
di in gr "Reliability of test 1 (" %3.0f `k1' " items," /*
       */ %6.0f `n1' " obs) = " %5.4f `r1'
di in gr "Reliability of test 2 (" %3.0f `xk2' " items," /*
       */ %6.0f `xn2' " obs) = " %5.4f `r2' _n
di in gr "W = " %6.4f `W' " Prob > F: " %6.5f fprob(`df1',`df2',`W')

end