4
uses define_types, statcr, distr, dialogs;
5
function Sum2Power(lOutImgSum: SingleP; lVolVox,lnTotal,lnDeficit: integer; lBinomial: boolean): boolean;
6
function Sum2PowerCont(lOutImgSum: SingleP; lVolVox,lnTotal: integer): boolean;
7
function Sum2PowerBinom(lOutImgSum: SingleP; lVolVox,lnTotal,lnDeficit: integer): boolean;
8
function k_out_n (k,n: integer): double; //total possible permutations
12
function k_out_n (k,n: integer): double; //total possible permutations
13
//k= smaller group, n=sum of both groups
15
if not gFactRAready then InitFact;
16
result := round(gFactRA[n] / (gFactRA[k]*gFactRA[n-k] ) );
17
// k out n = n!/(k!*(n-k)! which is equal to the PROD(i=k; 1){(n-i+1)/i}
20
function Sum2Power(lOutImgSum: SingleP; lVolVox,lnTotal,lnDeficit: integer; lBinomial: boolean): boolean;
23
result := Sum2PowerBinom(lOutImgSum, lVolVox,lnTotal,lnDeficit)
25
result := Sum2PowerCont(lOutImgSum, lVolVox,lnTotal)
28
function Sum2PowerCont(lOutImgSum: SingleP; lVolVox,lnTotal: integer): boolean;
29
//convert Sum image to power map showing maximum possible effect size
30
//'Cont' version is for continuous data
32
lDensity,lN,lRank: integer;
33
lDensityPowerRA: singleP;
36
if (lnTotal < 2) or (lVolVox < 1) then
38
getmem(lDensityPowerRA,lnTotal* sizeof(single));
39
//no need to compute power for [lnTotal] and [0] - no variability when everyone or no one has a lesion
40
//lDensityPowerRA[lnTotal] := 0; //everyone has a lesion = no variability
42
for lN := 1 to (lnTotal -1) do begin
43
//most power when all participants with a lesion have most extreme behavioural data
44
//therefore, they will have the lowest ranks: rank 1,2,3,4
46
if (lnTotal > 360) then //cannot calculate values this large...
47
lDensityPowerRA^[lN] := 0
48
else if (lN > 10) and (lnTotal > 64) then //avoid overflow...
49
lDensityPowerRA^[lN] := pNormalInv ( 1/(k_out_n(10,lnTotal)) )
51
lDensityPowerRA^[lN] := 1/(k_out_n(lN,lnTotal)); //compute Wilcoxon probability
52
lDensityPowerRA^[lN] := pNormalInv (lDensityPowerRA^[lN]);//convert p to z-score
54
//max power when every possible person with a lesion has a defict, and everyone w/o lesion does not...
55
//lDensityPowerRA[lN] := Liebermeister (lLD,lnoLD,lLnoD,lnoLnoD); //probability of this observation
56
//lDensityPowerRA[lN] := pNormalInv (lDensityPowerRA[lN]);//convert p to z-score
57
//fx(lDensityPowerRA[lN]);
59
//now use lookup table to convert overlay density to effective power
60
for lN := 1 to lVolVox do begin
61
lDensity := round( lOutImgSum^[lN]);
62
if (lDensity > 0) and (lDensity < lnTotal) then
63
lOutImgSum^[lN] := lDensityPowerRA^[lDensity]
67
freemem(lDensityPowerRA);
71
function Sum2PowerBinom(lOutImgSum: SingleP; lVolVox,lnTotal,lnDeficit: integer): boolean;
72
//convert Sum image to power map showing maximum possible effect size
74
lDensity,lN,lLD,lLnoD,lnoLD,lnoLnoD: integer;
75
lDensityPowerRA: singleP;
78
if (lnTotal < 2) or (lnDeficit < 1) or (lVolVox < 1) then
80
if(lnDeficit >= lnTotal) then begin
81
showmessage('Sum2Power error: people with deficit must be less than sample size');
84
getmem(lDensityPowerRA,lnTotal* sizeof(single));
85
//no need to compute power for lnTotal and 0 - no variability when everyone or no one has a lesion
86
//lDensityPowerRA[lnTotal] := 0; //everyone has a lesion = no variability
87
for lN := 1 to (lnTotal -1) do begin
88
//max power when every possible person with a lesion has a defict, and everyone w/o lesion does not...
89
if lN > lnDeficit then begin
91
lLnoD := lN - lnDeficit;
96
lnoLD := lnDeficit-lLD; //number of people with deficit who do not have a lesion - as close to zero as possible
97
lnoLnoD := lnTotal-lnoLD-lLnoD-lLD;
98
lDensityPowerRA^[lN] := Liebermeister (lLD,lnoLD,lLnoD,lnoLnoD); //probability of this observation
99
lDensityPowerRA^[lN] := pNormalInv (lDensityPowerRA^[lN]);//convert p to z-score
100
//fx(lLD,lnoLD,lLnoD,lnoLnoD,lDensityPowerRA[lN]);
102
//now use lookup table to convert overlay density to effective power
103
for lN := 1 to lVolVox do begin
104
lDensity := round( lOutImgSum^[lN]);
105
if (lDensity > 0) and (lDensity < lnTotal) then
106
lOutImgSum^[lN] := lDensityPowerRA^[lDensity]
108
lOutImgSum^[lN] := 0;
110
end; //for each voxel
111
freemem(lDensityPowerRA);