0
|
1 |
#======================================================================
|
|
2 |
# P E A R S N . P L
|
|
3 |
# doc: Wed Mar 24 11:23:29 1999
|
|
4 |
# dlm: Mon Jul 24 15:02:14 2006
|
|
5 |
# (c) 1999 A.M. Thurnherr
|
|
6 |
# uE-Info: 26 0 NIL 0 0 72 66 2 4 NIL ofnI
|
|
7 |
#======================================================================
|
|
8 |
|
|
9 |
# HISTORY:
|
|
10 |
# Mar 24, 1999: - created from NR c-version
|
|
11 |
# Mar 26, 1999: - cosmetic changes
|
|
12 |
# May 23, 1999: - allowed for N==2
|
|
13 |
# Oct 04, 1999: - changed from specfuns to funs
|
|
14 |
# Oct 13, 1999: - BUG: had to change TINY from 1e-20 to 1e-19
|
|
15 |
# Nov 11, 1999: - BUG: had to change TINY from to 1e-16
|
|
16 |
# Dec 11, 2001: - BUG: NaNs had not been handled correctly
|
|
17 |
# Dec 12, 2001: - BUG: croak() had been used (this produces
|
|
18 |
# a pipe #ERROR# output which is wrong
|
|
19 |
# if pearsn is called within eval as in [fit])
|
|
20 |
# Jan 9, 2006: - removed @antsFlagged
|
|
21 |
|
|
22 |
require "$ANTS/libfuns.pl";
|
|
23 |
|
|
24 |
{ # static scope
|
|
25 |
|
|
26 |
my($TINY) = 1e-16; # for complete correlation
|
|
27 |
|
|
28 |
# get correlation coefficient (retval); N (ref); significance level at which
|
|
29 |
# null hypothesis of zero correlation is disproved (ref; small value
|
|
30 |
# indicates significant correlation); and Fisher's z (ref). Missing refs
|
|
31 |
# indicate values are not returned. Adapted for ANTS.
|
|
32 |
|
|
33 |
sub pearsn(@)
|
|
34 |
{
|
|
35 |
my($xfnr,$yfnr,$NR,$pR,$zR) = @_;
|
|
36 |
my($n,$r);
|
|
37 |
my($j);
|
|
38 |
my($yt,$xt,$t,$df);
|
|
39 |
my($syy,$sxy,$sxx,$ay,$ax);
|
|
40 |
|
|
41 |
for ($j=0; $j<=$#ants_; $j++) {
|
|
42 |
next unless (numberp($ants_[$j][$xfnr]) && numberp($ants_[$j][$yfnr]));
|
|
43 |
$n++;
|
|
44 |
$ax += $ants_[$j][$xfnr];
|
|
45 |
$ay += $ants_[$j][$yfnr];
|
|
46 |
}
|
|
47 |
die("$0 (pearsn.pl): no data\n") unless ($n >= 2);
|
|
48 |
$ax /= $n;
|
|
49 |
$ay /= $n;
|
|
50 |
for ($j=0; $j<=$#ants_; $j++) {
|
|
51 |
next unless (numberp($ants_[$j][$xfnr]) && numberp($ants_[$j][$yfnr]));
|
|
52 |
$xt = $ants_[$j][$xfnr] - $ax;
|
|
53 |
$yt = $ants_[$j][$yfnr] - $ay;
|
|
54 |
$sxx += $xt * $xt;
|
|
55 |
$syy += $yt * $yt;
|
|
56 |
$sxy += $xt * $yt;
|
|
57 |
}
|
|
58 |
$r = $sxy/(sqrt($sxx * $syy) + $TINY);
|
|
59 |
$df = $n - 2;
|
|
60 |
$t = $r * sqrt($df/((1-$r+$TINY) * (1+$r+$TINY)));
|
|
61 |
$$NR = $n if (defined($NR));
|
|
62 |
$$pR = ($n == 2) ? 0 : &betai(0.5*$df,0.5,$df/($df+$t*$t))
|
|
63 |
if (defined($pR));
|
|
64 |
$$zR = 0.5 * log((1+$r+$TINY) / (1-$r+$TINY))
|
|
65 |
if (defined($zR));
|
|
66 |
return $r;
|
|
67 |
}
|
|
68 |
|
|
69 |
} # end of static scope
|