#! /usr/bin/perl
#
#
# Compare two correlation coefficients ARGV[0] and ARGV[1] with numbers
# ARGV[2] and ARGV[3]
#
# 
# Where is the Normal-Z test
$Script = $ENV{'CGI_SCRIPT'}.'/Statistics';
#
#
# Get input 
$R1 = $ARGV[0];
$R2 = $ARGV[1];
$N1 = $ARGV[2];
$N2 = $ARGV[3];
# If one of the two coefficients and sample sizes is zero, calculate the 
# significance with 0
if(($R1 == 0 && $N1 == 0) || ($R2 == 0 && $N2 == 0))
{
  $R = $R1 == 0 ? $R2 : $R1;
  $N = $R1 == 0 ? $N2 : $N1;
  $DoF = $N - 2;
  # Check values
  if($DoF == 0 || abs($R) > 1)
  { print "#### (invalid input)";
    return;
  };
  $T = $R * sqrt($DoF/(1-$R*$R));
  $p = join(" ", `$Script/Student-t_dist.pl $T $DoF`);
  printf("%4.3g", $p);
  return;
};
#
# Check values
if($N1 <= 3 || $N2 <= 3 || abs($R1) > 1 || abs($R2) > 1)
{ print "#### (invalid input)";
  return;
};
# Fisher Z-transform
$Z1 = 0.5*log((1+$R1)/(1-$R1));
$Z2 = 0.5*log((1+$R2)/(1-$R2));
#
# Standard Normal distributed difference
$z = ($Z1-$Z2)/sqrt(1/($N1-3)+1/($N2-3));
# Determine level of significance
$p = join(" ", `$Script/Normal-Z.pl $z`);
# Output
printf("%4.3g", $p);
print " (note: samples should be larger than 10)" if($N1 <= 10 || $N2 <= 10);


