Sample program: |
The following sample code lets you select two different layers of the currently loaded dataset. The frequency distributions of the data of the two layers are then displayed and compared by means of a chi2-test.
program FrequDistribution;
const
PAGE = 1; // chart page
NBINS = 100; // number of bins of the frequency distribution
{---------------------------------------------------------------------}
procedure DrawDistribution (Distri: TIntArray; Color: TColor; FirstBin,
Binwidth, Offset: double);
{---------------------------------------------------------------------}
var
i : integer;
begin
with ChartBook.Charts[PAGE] do
begin
DataColor := Color;
for i:=1 to length(Distri) do // lines
begin
MoveTo (FirstBin+Binwidth*(i-1)+offset, 0);
DrawTo (FirstBin+Binwidth*(i-1)+offset, Distri[i-1]);
end;
MoveTo (FirstBin, Distri[0]); // envelope
for i:=1 to length(Distri) do
DrawTo (FirstBin+Binwidth*(i-1)+offset, Distri[i-1]);
AutoRange (1,4);
Update;
end;
end;
{---------------------------------------------------------------------}
// main program
{---------------------------------------------------------------------}
var
Data : TDouble2DArray;
ll1, ll2 : integer;
FrequDist1 : TIntArray;
FrequDist2 : TIntArray;
Underflow,
Overflow,
MaxCnt : longint;
LList : TIntArray;
Chi2, p : double;
df : integer;
nObs, nExp : integer;
min1, max1 : double;
min2, max2 : double;
offset : double;
begin
ll1 := SelectLayer (1, false, false, LList, 'Please select a layer');
GetCubeLayer (ll1,1,Data);
MinMaxMatrix (Data, 0, 0, 0, 0, min1, max1);
ll2 := SelectLayer (1, false, false, LList, 'Please select a layer to compare');
GetCubeLayer (ll2,1,Data);
MinMaxMatrix (Data, 0, 0, 0, 0, min2, max2);
MinMaxVector([Min1, min2, max1, max2], 0, 3, min1, max1); // common min/max
if ll1 = ll2 // displace plots if the distributions
then offset := 0.2 // come from the same layers
else offset := 0.0;
if FrequDistOfMatrix (Data,
0,0,0,0, // LoX, LoY, HiX, HiY,
min1, max1, // FirstBin, LastBin,
(max1-min1)/NBINS, // BinWidth,
FrequDist1,
Underflow, Overflow, MaxCnt) = 0 then
begin
ChartBook.Reset;
ChartBook.Configure
(PAGE, // page 1
true, false, false, // only the chart is visible
250, 250); // default width & height
ChartBook.TabCaption[PAGE] := 'Frequency Distribution';
with ChartBook.Charts[PAGE] do
begin
Reset;
GridStyle := gsDotLines;
GridColor := clSilver;
ScalePropsY[1].ShortTicks := false;
end;
DrawDistribution (FrequDist1, clBlue, min1, (max1-min1)/NBINS, -offset);
GetCubeLayer (ll1,1,Data);
if FrequDistOfMatrix (Data,
0,0,0,0, // LoX, LoY, HiX, HiY,
min1, max1, // FirstBin, LastBin
(max1-min1)/NBINS, // BinWidth,
FrequDist2,
Underflow, Overflow, MaxCnt) = 0 then
begin
DrawDistribution (FrequDist2, clRed, min1, (max1-min1)/NBINS, +offset);
PerformChi2DistComp (FrequDist1, FrequDist2, Chi2, p, df, nobs, nexp);
ChartBook.Charts[PAGE].Caption := 'Frequency Distribution of Layer '+
IntToStr(ll1)+' (red) and Layer '+
IntToStr(ll2)+' (blue). p = '+
SigDigStr(p,12,4,true);
end;
end;
end.
|