/*
     cortesti: Test of equality of two correlation coefficients
     Version 1.2 -- Date: November 7th, 2000
     By: HervŽ CACI (hcaci@wanadoo.fr)

     cortesti corr1 n1 corr2 n2
     cortesti rxy rxv rvy n

     Version 1.1 -- Corrected some minor bugs (03/13/2000)
     Version 1.0 -- Initial release (02/22/2000)
*/

program define cortesti
*! Version 1.2 -- November 7th, 2000
version 5.0

parse "`*'", parse(" ,")

confirm number `1'
local corr1 `1'
if `corr1' <-1 | `corr1' >1  {
     di in red "The first correlation coefficient is out of range"
     exit
}

confirm number `2'
capture confirm integer number `2'
if _rc==7 { /* If not integer it should be rxv */
  local rxy `corr1'
  local rxv `2'
  if `rxv' <-1 | `rxv'>1 {
    di in red "The second correlation coefficient is out of range"
    exit
  }
  confirm number `3'
  local ryv `3'
  if `ryv' <-1 | `ryv'>1 {
    di in red "The third correlation coefficient is out of range"
    exit
  }
  confirm integer number `4'
  local n `4'
  if `n'<=3 {
    di in red "The sample size is too small"
    exit
  }
  local detR=(1-`rxy'^2-`ryv'^2-`rxv'^2+2*`rxy'*`rxv'*`ryv')
  local diffr=`rxy'-`ryv'
  local tnum=(`n'-1)*(1+`rxv')
  local tden=2*((`n'-1)/(`n'-3))*`detR'+0.5*(`rxy'+`ryv')*(((1-`rxv')^3))
  local t=`diffr'*sqrt(`tnum'/`tden')
  di _n in gr "Comparison of correlation coefficients"
  di in gr "drawn from the same sample (n = " %6.0f `n' " obs)"
  di in gr _dup(43) "-" _n
  di in gr "Coefficient r(x,y) = " %4.3f `rxy'
  di in gr "Coefficient r(x,v) = " %4.3f `rxv'
  di in gr "Coefficient r(v,y) = " %4.3f `ryv'
  di in gr _n "t = " in ye %7.3f `t' /*
   */in gr " (df=" in ye %6.0f `n'-3 in gr ")"
  local p=tprob(`n'-3,`t')
	 if `t' < 0 {
		  local pl = `p'/2
		  local pr = 1 - `pl'
	   }
	 else {
		  local pr = `p'/2
		  local pl = 1 - `pr'
	 }
  di in gr _n "Ha: r(x,y) < r(v,y) --- P < t = " in ye %4.3f `pl'
  di in gr "Ha: r(x,y) = r(v,y) --- P = t = " in ye %4.3f `p'
  di in gr "Ha: r(x,y) > r(v,y) --- P > t = " in ye %4.3f `pr'
}
else { local n1 `2' /* If `2' is integer, it is "n1" */
       if `n1'<=10  {
         di in red "The first sample should be larger than 10"
         exit
       }
       confirm number `3'
       local corr2 `3'
       if `corr2' <-1 | `corr2' > 1 {
         di in red "The second correlation coefficient is out of range"
         exit
       }
       confirm integer number `4'
       local n2 `4'
       if `n2'<=10  {
         di in red "The second sample should be larger than 10"
         exit
       }
       local Z1=ln((1+`corr1')/(1-`corr1'))/2
       local Z2=ln((1+`corr2')/(1-`corr2'))/2
       local z=(`Z1'-`Z2')/sqrt((1/(`n1'-3))+(1/(`n2'-3)))

       di _n in gr "Test of equality of two correlation coefficients"
       di in gr "drawn from the two different samples"
       di in gr _dup(48) "-" _n
       di in gr "Coefficient 1 = " %4.3f `corr1' " (n1 = "%6.0f `n1' ")"
       di in gr "Coefficient 2 = " %4.3f `corr2' " (n2 = "%6.0f `n2' ")" _n
       di in gr "Ho: coefficient 1 = coefficient 2"
       di in gr _col(10) "z = " in ye %7.3f `z'
       di in gr "Prob > |z| = " in ye %8.4f 2*normprob(-abs(`z'))
     }


end