Files
lazarus-ccr/applications/lazstats/source_orig/ANCOVAUNIT.PAS
wp_xxyyzz 9a42ad0d7a LazStats: Add original sources, part 1
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7872 8e941d3f-bd1b-0410-a28a-d453659cc2b4
2020-11-16 10:12:12 +00:00

1155 lines
44 KiB
Plaintext

unit ANCOVAUNIT;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
MainUnit, OutPutUnit, FunctionsLib, GraphLib,
Globals, DataProcs, MatrixLib, DictionaryUnit, StdCtrls, Buttons, ExtCtrls,contexthelpunit;
type
{ TANCOVAfrm }
TANCOVAfrm = class(TForm)
HelpBtn: TButton;
MultCompChk: TCheckBox;
ResetBtn: TButton;
CancelBtn: TButton;
ComputeBtn: TButton;
ReturnBtn: TButton;
PlotMeans: TCheckBox;
Inverses: TCheckBox;
RMats: TCheckBox;
Describe: TCheckBox;
DepIn: TBitBtn;
DepOut: TBitBtn;
FixedIn: TBitBtn;
FixedOut: TBitBtn;
CovIn: TBitBtn;
CovOut: TBitBtn;
DepVar: TEdit;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
FixedList: TListBox;
Label4: TLabel;
CovList: TListBox;
VarList: TListBox;
procedure ComputeBtnClick(Sender: TObject);
procedure CovInClick(Sender: TObject);
procedure CovOutClick(Sender: TObject);
procedure DepInClick(Sender: TObject);
procedure DepOutClick(Sender: TObject);
procedure FixedInClick(Sender: TObject);
procedure FixedOutClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure HelpBtnClick(Sender: TObject);
procedure ResetBtnClick(Sender: TObject);
private
{ private declarations }
NCases, NoSelected, NoFixed, NoCovs, DepColNo : integer;
ColNoSelected : IntDyneVec; // Grid col. no's of predictors
RowLabels, ColLabels : StrDyneVec;
CorMat : DblDyneMat; // correlation matrix
IndMat : DblDyneMat; // correlation matrix among independent variables
BetaWeights : DblDyneVec; // standardized regression weights
Means, Variances, StdDevs : DblDyneVec;
PrintIt : boolean; // true to print correlations in reg procedure
probout : double; // probability for removing a variable
Testout : boolean; // true if testing for retention of variables
plot : boolean; // if true, plot group means
StdErrEst : double; // standard error of estimate
multcomp : boolean; // if true make multiple comparisons
R2 : double; // squared multiple correlation coefficient
FixedCols : IntDyneVec; // grid columns of fixed variables
CovCols : IntDyneVec; // grid columns of covariates
mingrp, maxgrp : IntDyneVec; // min and max group codes
Block : IntDyneMat; // descriptors for group codings
// values 1 to 5 contain group min, max, startcol, endcol and no. of vectors
NoBlocks : integer; // number of vector blocks created for groups and inter.
errorcode : boolean; // returned by routines that use an errorcode
IndepIndex : IntDyneVec; // sequential number of predictors in corr. matrix
BlockLabel : StrDyneVec;
descript : boolean;
printinv : boolean;
NoTestVecs : integer; // no. of vectors for group interactions with covariates
constant : double; // regression constant
noind : integer; // no. of independent variables in a regression analysis
BWeights : DblDyneVec; // raw regression weights
BStdErrs : DblDyneVec; // standard errors of regression weights
BTtests : DblDyneVec;
procedure GetParms(Sender: TObject);
procedure CodeGroups(Sender: TObject);
procedure GenInteractions(Sender: TObject);
procedure DoRegs(Sender: TObject);
procedure CleanUp(Sender: TObject);
procedure EntryOpt1(Sender: TObject);
procedure GenCovInteracts(Sender: TObject);
procedure AdjustMeans(Sender: TObject);
procedure MultCompare(Sender: TObject);
public
{ public declarations }
end;
var
ANCOVAfrm: TANCOVAfrm;
implementation
{ TANCOVAfrm }
procedure TANCOVAfrm.ResetBtnClick(Sender: TObject);
VAR i : integer;
begin
DepVar.Text := '';
DepIn.Visible := true;
DepOut.Visible := false;
FixedIn.Visible := true;
FixedOut.Visible := false;
CovIn.Visible := true;
CovOut.Visible := false;
VarList.Clear;
CovList.Clear;
FixedList.Clear;
for i := 1 to NoVariables do
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]);
Describe.Checked := true;
Rmats.Checked := false;
Inverses.Checked := false;
PlotMeans.Checked := false;
NoBlocks := 0;
end;
procedure TANCOVAfrm.FormShow(Sender: TObject);
begin
ResetBtnClick(self);
end;
procedure TANCOVAfrm.HelpBtnClick(Sender: TObject);
begin
ContextHelpForm.HelpMessage((Sender as TButton).tag);
end;
procedure TANCOVAfrm.DepInClick(Sender: TObject);
VAR index : integer;
begin
index := VarList.ItemIndex;
DepVar.Text := VarList.Items.Strings[index];
VarList.Items.Delete(index);
DepIn.Visible := false;
DepOut.Visible := true;
end;
procedure TANCOVAfrm.CovInClick(Sender: TObject);
VAR i, index : integer;
begin
index := VarList.Items.Count;
i := 0;
while i < index do
begin
if (VarList.Selected[i]) then
begin
CovList.Items.Add(VarList.Items.Strings[i]);
VarList.Items.Delete(i);
index := index - 1;
i := 0;
end
else i := i + 1;
end;
CovOut.Visible := true;
end;
procedure TANCOVAfrm.ComputeBtnClick(Sender: TObject);
begin
NoFixed := FixedList.Items.Count;
NoCovs := CovList.Items.Count;
if (NoFixed <= 0) or (NoCovs <= 0) then
begin
ShowMessage('ERROR! You must have at least one group variable and one covariate');
exit;
end;
GetParms(self);
CodeGroups(self);
GenInteractions(self);
GenCovInteracts(self);
DoRegs(self);
CleanUp(self);
ReturnBtn.SetFocus;
end;
procedure TANCOVAfrm.CovOutClick(Sender: TObject);
VAR index : integer;
begin
index := CovList.ItemIndex;
if index < 0 then
begin
CovOut.Visible := false;
exit;
end;
VarList.Items.Add(CovList.Items.Strings[index]);
CovList.Items.Delete(index);
end;
procedure TANCOVAfrm.DepOutClick(Sender: TObject);
begin
VarList.Items.Add(DepVar.Text);
DepVar.Text := '';
DepIn.Visible := true;
DepOut.Visible := false;
end;
procedure TANCOVAfrm.FixedInClick(Sender: TObject);
VAR i, index : integer;
begin
index := VarList.Items.Count;
i := 0;
while i < index do
begin
if (VarList.Selected[i]) then
begin
FixedList.Items.Add(VarList.Items.Strings[i]);
VarList.Items.Delete(i);
index := index - 1;
i := 0;
end
else i := i + 1;
end;
FixedOut.Visible := true;
end;
procedure TANCOVAfrm.FixedOutClick(Sender: TObject);
VAR index : integer;
begin
index := FixedList.ItemIndex;
if index < 0 then
begin
FixedOut.Visible := false;
exit;
end;
VarList.Items.Add(FixedList.Items.Strings[index]);
FixedList.Items.Delete(index);
end;
procedure TANCOVAfrm.GetParms(Sender: TObject);
label covs;
VAR i, j : integer;
begin
SetLength(ColNoSelected,NoVariables);
SetLength(FixedCols,NoFixed);
SetLength(CovCols,NoCovs);
SetLength(mingrp,NoFixed);
SetLength(maxgrp,NoFixed);
SetLength(Block,100,5);
SetLength(BlockLabel,100);
NoSelected := 0;
NoBlocks := 0;
if Describe.Checked then descript := true else descript := false;
if inverses.Checked then printinv := true else printinv := false;
if plotmeans.Checked then plot := true else plot := false;
if multcompchk.Checked then multcomp := true else multcomp := false;
for i := 1 to NoVariables do
begin
if DepVar.Text = OS3MainFrm.DataGrid.Cells[i,0] then
begin
DepColNo := i;
ColNoSelected[0] := i;
NoSelected := 1;
break;
end;
end;
if NoFixed < 1 then goto covs;
for i := 0 to NoFixed - 1 do
begin
for j := 1 to NoVariables do
begin
if FixedList.Items.Strings[i] = OS3MainFrm.DataGrid.Cells[j,0] then
begin
FixedCols[i] := j;
ColNoSelected[NoSelected] := j;
NoSelected := NoSelected + 1;
break;
end;
end;
end;
covs: if NoCovs < 1 then exit;
for i := 0 to NoCovs - 1 do
begin
for j := 1 to NoVariables do
begin
if CovList.Items.Strings[i] = OS3MainFrm.DataGrid.Cells[j,0] then
begin
CovCols[i] := j;
ColNoSelected[NoSelected] := j;
NoSelected := NoSelected + 1;
break;
end;
end;
end;
// create a "Block" for each covariate
for i := 0 to NoCovs-1 do
begin
NoBlocks := NoBlocks + 1;
Block[i,0] := 0; // group min
Block[i,1] := 0; // group max
Block[i,2] := CovCols[i]; // start column in grid
Block[i,3] := CovCols[i]; // end column in grid
Block[i,4] := 1; // no. of vectors
BlockLabel[i] := 'Cov' + IntToStr(i);
end;
end;
procedure TANCOVAfrm.CodeGroups(Sender: TObject);
var
col, i, j, value, startcol, endcol, novectors : integer;
factlabel, cellstring : string;
begin
// startcol := Block[NoBlocks,4] + 1; // next column after last block
for i := 0 to NoFixed-1 do // create a block for code vectors of each fixed variable
begin
col := FixedCols[i];
factlabel := chr(ord('A')+i);
mingrp[i] := round(StrToFloat(Trim(OS3MainFrm.DataGrid.Cells[col,1])));
maxgrp[i] := mingrp[i];
for j := 1 to NoCases do
begin
if NOT GoodRecord(j,NoSelected,ColNoSelected) then continue;
cellstring := Trim(OS3MainFrm.DataGrid.Cells[col,j]);
value := round(StrToFloat(cellstring));
if value < mingrp[i] then mingrp[i] := value;
if value > maxgrp[i] then maxgrp[i] := value;
end;
// create fixed effect coding for levels - 1 of the fixed effect var.
EffectCode(col,mingrp[i],maxgrp[i],factlabel,startcol,
endcol,novectors);
NoBlocks := NoBlocks + 1;
Block[NoBlocks-1,0] := mingrp[i];
Block[NoBlocks-1,1] := maxgrp[i];
Block[NoBlocks-1,2] := startcol;
Block[NoBlocks-1,3] := endcol;
Block[NoBlocks-1,4] := novectors;
BlockLabel[NoBlocks-1] := factlabel;
end; // next factor block
end;
procedure TANCOVAfrm.GenInteractions(Sender: TObject);
type Twoway = array[0..9,0..1] of integer;
type Threeway = array[0..9,0..2] of integer;
type Fourway = array[0..4,0..3] of integer;
var
i, j, k, l, m, n, o, highest, col, vect1col, vect2col, vect3col, value : integer;
labelstr : string;
startcol, endcol, novectors, oldnovars : integer;
cell1, cell2, cell3, cell4 : string;
TwoWayCombos, ThreeWayCombos, FourwayCombos : double;
Block1, Block2, Block3, Block4, Start1, End1, Start2, End2, Start3, End3 : integer;
Start4, End4 : integer;
const Twoways: Twoway = ((1,2),(1,3),(2,3),(1,4),(2,4),(3,4),(1,5),(2,5),(3,5),(4,5));
const Threeways: Threeway = ((1,2,3),(1,2,4),(1,3,4),(2,3,4),(1,2,5),(1,3,5),
(1,4,5),(2,3,5),(2,4,5),(3,4,5));
const Fourways: Fourway = ((1,2,3,4),(1,2,3,5),(1,2,4,5),(1,3,4,5),(2,3,4,5));
begin
if NoFixed < 2 then exit;
novectors := 0;
// Do two-way interactions
// col := NoVariables;
TwoWayCombos := round(combos(2.0, NoFixed));
oldnovars := NoVariables;
for i := 0 to round(TwoWayCombos)-1 do
begin
Block1 := TwoWays[i,0] + NoCovs - 1;
Block2 := TwoWays[i,1] + NoCovs - 1;
Start1 := Block[Block1,2];
End1 := Block[Block1,3];
Start2 := Block[Block2,2];
End2 := Block[Block2,3];
oldnovars := NoVariables;
startcol := Block[NoBlocks-1,3] + 1;
col := NoVariables;
for j := Start1 to End1 do
begin
for k := Start2 to End2 do
begin
col := col + 1;
novectors := novectors + 1;
DictionaryFrm.NewVar(col);
labelstr := OS3MainFrm.DataGrid.Cells[j,0] + 'x';
labelstr := labelstr + OS3MainFrm.DataGrid.Cells[k,0];
OS3MainFrm.DataGrid.Cells[col,0] := labelstr;
DictionaryFrm.DictGrid.Cells[1,col] := labelstr;
for m := 1 to NoCases do
begin
if NOT GoodRecord(m,NoSelected,ColNoSelected) then Continue;
cell1 := Trim(OS3MainFrm.DataGrid.Cells[j,m]);
cell2 := Trim(OS3MainFrm.DataGrid.Cells[k,m]);
value := round(StrToFloat(cell1)) *
round(StrToFloat(cell2));
OS3MainFrm.DataGrid.Cells[col,m] := IntToStr(value);
end;
end;
endcol := col;
NoBlocks := NoBlocks + 1;
Block[NoBlocks-1,0] := 0; // zeroes for interactions
Block[NoBlocks-1,1] := 0; // zeroes for interactions
Block[NoBlocks-1,2] := startcol; // grid start col for 2-way interactions
Block[NoBlocks-1,3] := endcol; // grid end col for 2-way interactions
Block[NoBlocks-1,4] := novectors; // no. of vectors for 2-way interaction
BlockLabel[NoBlocks-1] := BlockLabel[Block1] + 'x' + BlockLabel[Block2];
NoVariables := oldnovars + novectors;
OS3MainFrm.NoVarsEdit.Text := IntToStr(NoVariables);
novectors := 0;
end; // end of interaction of fixed effect vectors j and fixed effect vectors k
end; // end of 2 way interactions
// do 3-way interactions using group vectors and two way interaction vectors
if (NoFixed < 3) then exit;
ThreeWayCombos := Combos(3.0,NoFixed);
for i := 0 to round(ThreeWayCombos)-1 do
begin
startcol := Block[NoBlocks-1,3] + 1; // next column after last block
col := NoVariables;
Block1 := ThreeWays[i,0] + NoCovs - 1;
Block2 := ThreeWays[i,1] + NoCovs - 1;
Block3 := ThreeWays[i,2] + NoCovs - 1;
Start1 := Block[Block1,2];
End1 := Block[Block1,3];
Start2 := Block[Block2,2];
End2 := Block[Block2,3];
Start3 := Block[Block3,2];
End3 := Block[Block3,3];
oldnovars := NoVariables;
novectors := 0;
for j := Start1 to End1 do
begin
for k := Start2 to End2 do
begin
for l := Start3 to End3 do // no. vectors in first factor
begin
col := col + 1;
novectors := novectors + 1;
DictionaryFrm.NewVar(col);
labelstr := OS3MainFrm.DataGrid.Cells[j,0] + 'x';
labelstr := labelstr + OS3MainFrm.DataGrid.Cells[k,0];
labelstr := labelstr + 'x' + OS3MainFrm.DataGrid.Cells[l,0];
OS3MainFrm.DataGrid.Cells[col,0] := labelstr;
DictionaryFrm.DictGrid.Cells[1,col] := labelstr;
for m := 1 to NoCases do
begin
if NOT GoodRecord(m,NoSelected,ColNoSelected) then Continue;
cell1 := Trim(OS3MainFrm.DataGrid.Cells[j,m]);
cell2 := Trim(OS3MainFrm.DataGrid.Cells[k,m]);
cell3 := Trim(OS3MainFrm.DataGrid.Cells[l,m]);
value := round(StrToFloat(cell1)) *
round(StrToFloat(cell2)) *
round(StrToFloat(cell3));
OS3MainFrm.DataGrid.Cells[col,m] := IntToStr(value);
end; // next case m
end; // next third variable
end; // next second variable
end; // end of interaction of fixed effects vectors for j, k and l
endcol := col; // last grid column containing three-way interaction vectors
NoBlocks := NoBlocks + 1;
Block[NoBlocks-1,0] := 0; // zeroes for interactions
Block[NoBlocks-1,1] := 0; // zeroes for interactions
Block[NoBlocks-1,2] := startcol; // grid start col for 2-way interactions
Block[NoBlocks-1,3] := endcol; // grid end col for 2-way interactions
Block[NoBlocks-1,4] := novectors; // no. of vectors for 2-way interaction
BlockLabel[NoBlocks-1] := BlockLabel[Block1] + 'x' + BlockLabel[Block2]
+ 'x' + BlockLabel[Block3];
NoVariables := oldnovars + novectors;
OS3MainFrm.NoVarsEdit.Text := IntToStr(NoVariables);
end; // end of three way interactions
// do 4-way interactions using group and 3-way interaction vectors
if (NoFixed < 4) then exit;
FourWayCombos := combos(4.0,NoFixed);
for i := 0 to round(FourWayCombos) - 1 do
begin
startcol := Block[NoBlocks-1][3] + 1;
col := NoVariables;
Block1 := FourWays[i][0] + NoCovs - 1; // block # for first fixed effect
Block2 := FourWays[i][1] + NoCovs - 1; // block # for second fixed effect
Block3 := FourWays[i][2] + NoCovs - 1; // block # for third fixed effect
Block4 := FourWays[i][3] + NoCovs - 1; // block # for fourth fixed effect
Start1 := Block[Block1][2];
End1 := Block[Block1][3];
Start2 := Block[Block2][2];
End2 := Block[Block2][3];
Start3 := Block[Block3][2];
End3 := Block[Block3][3];
Start4 := Block[Block4][2];
End4 := Block[Block4][3];
oldnovars := NoVariables;
novectors := 0;
for j := Start1 to End1 do // vector in first fixed factor
begin
for k := Start2 to End2 do // vector in second fixed factor
begin
for l := Start3 to End3 do // vector in third fixed factor
begin
for m := Start4 to End4 do // vecotr in fourth fixed factor
begin
col := col + 1;
novectors := novectors + 1;
DictionaryFrm.NewVar(col);
labelstr := OS3MainFrm.DataGrid.Cells[j,0] + 'x';
labelstr := labelstr + OS3MainFrm.DataGrid.Cells[k,0];
labelstr := labelstr + 'x' + OS3MainFrm.DataGrid.Cells[l,0];
OS3MainFrm.DataGrid.Cells[col,0] := labelstr;
DictionaryFrm.DictGrid.Cells[1,col] := labelstr;
for n := 1 to NoCases do
begin
// if (! ValidRecord(n,ColNoSelected,NoSelected)) continue;
cell1 := Trim(OS3MainFrm.DataGrid.Cells[j,n]);
cell2 := Trim(OS3MainFrm.DataGrid.Cells[k,n]);
cell3 := Trim(OS3MainFrm.DataGrid.Cells[l,n]);
cell4 := Trim(OS3MainFrm.DataGrid.Cells[m,n]);
value := round(StrToFloat(cell1)) *
round(StrToFloat(cell2)) *
round(StrToFloat(cell3)) *
round(StrToFloat(cell4));
OS3MainFrm.DataGrid.Cells[col,n] := IntToStr(value);
end; // next case n
end; // next fourth vector m
end; // next third vector
end; // next second vector
end; // end of interaction of fixed effects vectors for j, k and l and m
endcol := col; // last grid column containing four-way interaction vectors
NoBlocks := NoBlocks + 1;
Block[NoBlocks-1][0] := 0; // zeroes for interactions
Block[NoBlocks-1][1] := 0; // zeroes for interactions
Block[NoBlocks-1][2] := startcol; // grid start col for 4-way interactions
Block[NoBlocks-1][3] := endcol; // grid end col for 4-way interactions
Block[NoBlocks-1][4] := novectors; // no. of vectors for 2-way interaction
BlockLabel[NoBlocks-1] := BlockLabel[Block1] + 'x' + BlockLabel[Block2]
+ 'x' + BlockLabel[Block3] + 'x'
+ BlockLabel[Block4];
NoVariables := oldnovars + novectors;
OS3MainFrm.NoVarsEdit.Text := IntToStr(NoVariables);
end; // end of four-way combinations
end;
procedure TANCOVAfrm.DoRegs(Sender: TObject);
var
outline : string;
count : integer;
i, j : integer;
begin
// get count of variables used
count := 0;
for i := 0 to NoBlocks - 1 do
for j := 0 to Block[i,4] do count := count + 1;
SetLength(BetaWeights,count+1);
SetLength(BWeights,count+2);
SetLength(BStdErrs,count+1);
SetLength(BTtests,count+1);
SetLength(Means,count+1);
SetLength(Variances,count+1);
SetLength(StdDevs,count+1);
SetLength(RowLabels,count+1);
SetLength(ColLabels,count+1);
SetLength(Cormat,count+1,count+1);
SetLength(Indmat,count+1,count+1);
SetLength(IndepIndex,count+1);
SetLength(ColNoSelected,count+1);
if Rmats.Checked then PrintIt := true else PrintIt := false;
Testout := false;
Probout := 0.99;
OutPutFrm.RichEdit.Clear;
OutPutFrm.RichEdit.Lines.Add('ANALYSIS OF COVARIANCE USING MULTIPLE REGRESSION');
OutPutFrm.RichEdit.Lines.Add('');
outline := 'File Analyzed: ' + OS3MainFrm.FileNameEdit.Text;
OutPutFrm.RichEdit.Lines.Add(outline);
OutPutFrm.RichEdit.Lines.Add('');
EntryOpt1(self); // factors, interactions and covariats concurrently
OutPutFrm.ShowModal;
IndepIndex := nil;
end;
procedure TANCOVAfrm.CleanUp(Sender: TObject);
begin
Indmat := nil;
Cormat := nil;
ColLabels := nil;
RowLabels := nil;
StdDevs := nil;
Variances := nil;
Means := nil;
BTtests := nil;
BStdErrs := nil;
BWeights := nil;
BetaWeights := nil;
maxgrp := nil;
mingrp := nil;
CovCols := nil;
FixedCols := nil;
ColNoSelected := nil
end;
procedure TANCOVAfrm.EntryOpt1(Sender: TObject);
var
i, j, k, col, count : integer;
Title : string;
outline : string;
FullR2 : double;
F : double;
Prob : double;
df1, df2: double;
SSGroups : double;
MSGroups : double;
SSError : double;
MSError : double;
SSTotal : double;
SSExplained : double;
SSGrpTot : double;
tProbs : DblDyneVec;
begin
// factors, interactions and covariates concurrently (full model)
// get grid column numbers of all vectors and dependent variable
OutPutFrm.RichEdit.Lines.Add('');
OutPutFrm.RichEdit.Lines.Add('Model for Testing Assumption of Zero Interactions with Covariates');
OutPutFrm.RichEdit.Lines.Add('');
count := 0;
for i := 1 to NoBlocks do // no. of vector blocks
begin
for j := 1 to Block[i-1,4] do // no of vectors in block
begin
col := Block[i-1,2] + j - 1; // count from beginning col.
count := count + 1;
ColNoSelected[count-1] := col;
IndepIndex[count-1] := count;
RowLabels[count-1] := OS3MainFrm.DataGrid.Cells[col,0];
end;
end;
count := count + 1;
noind := count - 1;
ColNoSelected[count-1] := DepColNo;
IndepIndex[count-1] := count;
RowLabels[count-1] := OS3MainFrm.DataGrid.Cells[DepColNo,0];
// Get correlation matrix (note dependent is last variable)
Correlations(count,ColNoSelected,Cormat,Means,Variances,StdDevs,errorcode,NCases);
if Rmats.Checked then PrintIt := true else PrintIt := false;
if PrintIt then
begin
Title := 'Correlation Matrix';
Mat_Print(Cormat,count,count,title,RowLabels,RowLabels,NCases);
end;
if descript then
begin
DynVectorPrint(Means,count,'MEANS',RowLabels,NCases);
DynVectorPrint(Variances,count,'VARIANCES',RowLabels,NCases);
DynVectorPrint(StdDevs,count,'STD. DEV.S',RowLabels,NCases);
end;
// Get regression
SetLength(tProbs,count);
PrintIt := true;
printinv := false;
if Inverses.Checked = true then printinv := true;
SetLength(BStdErrs,noind+1);
SetLength(BTtests,noind+1);
MReg(noind,ColNoSelected,DepColNo,RowLabels,Means,Variances,StdDevs,BWeights,
BetaWeights,BStdErrs,BTtests,tProbs,R2,StdErrEst,NCases,errorcode,false);
FullR2 := R2;
SSTotal := Variances[count-1] * (NCases - 1);
SSGroups := FullR2 * SSTotal;
SSError := (1.0 - FullR2) * SSTotal;
df1 := noind;
df2 := NCases - noind - 1;
MSGroups := SSGroups / df1;
MSError := SSError / df2;
F := MSGroups / MSError;
Prob := probf(F,df1,df2);
OutPutFrm.RichEdit.Lines.Add('');
outline := 'Analysis of Variance for the Model to Test Regression Homogeneity';
OutPutFrm.RichEdit.Lines.Add(outline);
OutPutFrm.RichEdit.Lines.Add(' SOURCE Deg.F. SS MS F Prob>F');
outline := format('%10s %10.0f %10.2f %10.2f %10.3f %10.4f',
['Explained',df1,SSGroups,MSGroups,F,Prob]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('%10s %10.0f %10.2f %10.2f',
['Error',df2,SSError,MSError]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('%10s %10d %10.2f',
['Total',NCases-1,SSTotal]);
OutPutFrm.RichEdit.Lines.Add(outline);
OutPutFrm.RichEdit.Lines.Add('');
outline := format('%12s %10.3f',['R Squared = ',R2]);
OutPutFrm.RichEdit.Lines.Add(outline);
OutPutFrm.RichEdit.Lines.Add('');
// Now do analysis without the interactions (Ancova model)
OutPutFrm.RichEdit.Lines.Add('');
OutPutFrm.RichEdit.Lines.Add('Model for Analysis of Covariance');
OutPutFrm.RichEdit.Lines.Add('');
count := 0;
for i := 1 to NoBlocks - 1 do // no. of vector blocks
begin
for j := 1 to Block[i-1,4] do // no of vectors in block
begin
col := Block[i-1,2] + j - 1; // count from beginning col.
count := count + 1;
ColNoSelected[count-1] := col;
IndepIndex[count-1] := count;
RowLabels[count-1] := OS3MainFrm.DataGrid.Cells[col,0];
end;
end;
count := count + 1;
noind := count - 1;
ColNoSelected[count-1] := DepColNo;
IndepIndex[count-1] := count;
RowLabels[count-1] := OS3MainFrm.DataGrid.Cells[DepColNo,0];
// Get correlation matrix (note dependent is last variable)
Correlations(count,ColNoSelected,Cormat,Means,Variances,StdDevs,errorcode,NCases);
// save in IndMat
for i := 0 to count-1 do
for j := 0 to count - 1 do
IndMat[i,j] := Cormat[i,j];
if Rmats.Checked then PrintIt := true else PrintIt := false;
if PrintIt then
begin
Title := 'Correlation Matrix';
Mat_Print(Cormat,count,count,title,RowLabels,RowLabels,NCases);
end;
if descript then
begin
DynVectorPrint(Means,count,'MEANS',RowLabels,NCases);
DynVectorPrint(Variances,count,'VARIANCES',RowLabels,NCases);
DynVectorPrint(StdDevs,count,'STD. DEV.S',RowLabels,NCases);
end;
// Get regression
PrintIt := true;
printinv := false;
if Inverses.Checked = true then printinv := true;
SetLength(BStdErrs,noind+1);
SetLength(BTtests,noind+1);
MReg(noind,ColNoSelected,DepColNo,RowLabels,Means,Variances,StdDevs,BWeights,
BetaWeights,BStdErrs,BTtests,tProbs,R2,StdErrEst,NCases,errorcode,false);
// test differences between previous and current models (= beta test)
constant := BWeights[noind];
df1 := NoTestVecs;
F := ((FullR2 - R2) / df1) / ((1.0 - FullR2) / df2);
Prob := probf(F,df1,df2);
OutPutFrm.RichEdit.Lines.Add('');
OutPutFrm.RichEdit.Lines.Add('Test for Homogeneity of Group Regression Coefficients');
outline := format('Change in R2 = %6.4f. F = %10.3f Prob.> F = %6.4f with d.f. %8.0f and %8.0f',
[(FullR2 - R2),F,Prob, df1, df2]);
OutPutFrm.RichEdit.Lines.Add(outline);
OutPutFrm.RichEdit.Lines.Add('');
outline := format('%12s %10.3f',['R Squared = ',R2]);
OutPutFrm.RichEdit.Lines.Add(outline);
FullR2 := R2;
SSTotal := Variances[count-1] * (NCases - 1);
SSGroups := FullR2 * SSTotal;
// SSExplained := SSGroups;
SSError := (1.0 - FullR2) * SSTotal;
df1 := noind;
df2 := NCases - noind - 1;
MSGroups := SSGroups / df1;
MSError := SSError / df2;
// obtain Adjusted means
// AdjustMeans(self);
// Make Comparisons among means
// if multcomp then MultCompare(self);
F := MSGroups / MSError;
Prob := probf(F,df1,df2);
OutPutFrm.RichEdit.Lines.Add('');
outline := 'Analysis of Variance for the ANCOVA Model';
OutPutFrm.RichEdit.Lines.Add(outline);
OutPutFrm.RichEdit.Lines.Add(' SOURCE Deg.F. SS MS F Prob>F');
outline := format('%10s %10.0f %10.2f %10.2f %10.3f %10.4f',
['Explained',df1,SSGroups,MSGroups,F,Prob]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('%10s %10.0f %10.2f %10.2f',
['Error',df2,SSError,MSError]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('%10s %10d %10.2f',
['Total',NCases-1,SSTotal]);
OutPutFrm.RichEdit.Lines.Add(outline);
OutPutFrm.RichEdit.Lines.Add('');
OutPutFrm.RichEdit.Lines.Add('');
// Obtain adjusted means
AdjustMeans(self);
// make comparisons among groups
if multcomp then MultCompare(self);
// Now do regression, eliminating each block to test effects of that term
PrintIt := false;
OutPutFrm.RichEdit.Lines.Add('Test for Each Source of Variance - Type III SS');
OutPutFrm.RichEdit.Lines.Add('----------------------------------------------------------------------');
OutPutFrm.RichEdit.Lines.Add(' SOURCE Deg.F. SS MS F Prob>F');
OutPutFrm.RichEdit.Lines.Add('----------------------------------------------------------------------');
// SSGrpTot := 0.0;
for i := 1 to NoBlocks - 1 do // covariates, fixed effects, interactions
begin
count := 0;
for j := 1 to NoBlocks-1 do
begin
if j = i then continue; // exclude the factor to be tested
for k := 1 to Block[j-1,4] do // no of vectors in block
begin
col := Block[j-1,2] + k - 1; // count from beginning col.
count := count + 1;
ColNoSelected[count-1] := col;
IndepIndex[count-1] := count;
RowLabels[count-1] := OS3MainFrm.DataGrid.Cells[col,0];
end;
end; // get next block of vectors for factors to be included
count := count + 1;
noind := count - 1;
ColNoSelected[count-1] := DepColNo;
IndepIndex[count-1] := count;
RowLabels[count-1] := OS3MainFrm.DataGrid.Cells[DepColNo,0];
Correlations(count,ColNoSelected,Cormat,Means,Variances,StdDevs,errorcode,NCases);
// Get regression
printinv := false;
SetLength(BStdErrs,noind+1);
SetLength(BTtests,noind+1);
MReg(noind,ColNoSelected,DepColNo,RowLabels,Means,Variances,StdDevs,BWeights,
BetaWeights,BStdErrs,BTtests,tProbs,R2,StdErrEst,NCases,errorcode,false);
df1 := Block[i-1,4];
SSGroups := (FullR2 - R2)* SSTotal;
SSGrpTot := SSGrpTot + SSGroups;
MSGroups := SSGroups / df1;
F := MSGroups / MSError;
Prob := probf(F,df1,df2);
OutPutFrm.RichEdit.Lines.Add('');
outline := format('%10s %10.0f %10.2f %10.2f %10.3f %10.4f',
[BlockLabel[i-1],df1,SSGroups,MSGroups,F,Prob]);
OutPutFrm.RichEdit.Lines.Add(outline);
end; // get next Block to eliminate
OutPutFrm.RichEdit.Lines.Add('----------------------------------------------------------------------');
outline := format('%10s %10.0f %10.2f %10.2f',['ERROR',df2,SSError,MSError]);
OutPutFrm.RichEdit.Lines.Add(outline);
OutPutFrm.RichEdit.Lines.Add('----------------------------------------------------------------------');
outline := format('%10s %10d %10.2f',['TOTAL',NCases-1,SSTotal]);
{
df1 := NoCovs;
SSGroups := SSExplained - SSGrpTot;
MSGroups := SSGroups / df1;
F := MSGroups / MSError;
Prob := probf(F,df1,df2);
outline := format('%10s %10.0f %10.2f %10.2f %10.3f %10.4f',
['Covariates',df1,SSGroups,MSGroups,F,Prob]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('%10s %10.0f %10.2f %10.2f',
['Error',df2,SSError,MSError]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('%10s %10d %10.2f',
['Total',NCases-1,SSTotal]);
OutPutFrm.RichEdit.Lines.Add(outline);
OutPutFrm.RichEdit.Lines.Add('');
}
tProbs := nil;
end;
procedure TANCOVAfrm.GenCovInteracts(Sender: TObject);
var
i, j, k, l, m, vect1col, vect2col, col : integer;
value : double;
labelstr, cell1, cell2 : string;
startcol, endcol, novectors, oldnovars : integer;
lastblock, firstblock : integer;
begin
col := NoVariables;
oldnovars := NoVariables;
novectors := 0;
NoTestVecs := 0;
startcol := Block[NoBlocks-1,3] + 1;
lastblock := NoBlocks;
firstblock := NoCovs + 1;
for i := 1 to NoCovs do // product vectors for each covariate
begin
vect1col := Block[i-1,2];
for j := firstblock to lastblock do
begin
for l := 1 to Block[j-1,4] do
begin
vect2col := Block[j-1,2] + l - 1; // first vector col. of B
col := col + 1;
novectors := novectors + 1;
NoTestVecs := NoTestVecs + 1;
DictionaryFrm.NewVar(col);
labelstr := OS3MainFrm.DataGrid.Cells[vect1col,0] + 'x';
labelstr := labelstr + OS3MainFrm.DataGrid.Cells[vect2col,0];
OS3MainFrm.DataGrid.Cells[col,0] := labelstr;
DictionaryFrm.DictGrid.Cells[1,col] := labelstr;
for m := 1 to NoCases do
begin
if NOT GoodRecord(m,NoSelected,ColNoSelected) then Continue;
cell1 := Trim(OS3MainFrm.DataGrid.Cells[vect1col,m]);
cell2 := Trim(OS3MainFrm.DataGrid.Cells[vect2col,m]);
value := StrToFloat(cell1) * StrToFloat(cell2);
OS3MainFrm.DataGrid.Cells[col,m] := FloatToStr(value);
end; // next case m
end; // next l vector
end; // next fixed effects factor j and interactions
end; // next covariate i
endcol := col; // last grid column containing two-way interaction vectors
NoBlocks := NoBlocks + 1;
Block[NoBlocks-1,0] := 0; // zeroes for interactions
Block[NoBlocks-1,1] := 0; // zeroes for interactions
Block[NoBlocks-1,2] := startcol; // grid start col for 2-way interactions
Block[NoBlocks-1,3] := endcol; // grid end col for 2-way interactions
Block[NoBlocks-1,4] := novectors; // no. of vectors for 2-way interaction
BlockLabel[NoBlocks-1] := BlockLabel[i-1] + 'xFixed';
NoVariables := oldnovars + novectors;
OS3MainFrm.NoVarsEdit.Text := IntToStr(NoVariables);
end;
procedure TANCOVAfrm.AdjustMeans(Sender: TObject);
var
sum : double;
GrpCovMeans : DblDyneMat;
AdjMeans : DblDyneVec;
Intercepts : DblDyneVec;
i, j, k, col, grp, nogrps : integer;
value : double;
Labels : StrDyneVec;
outline : string;
noingrp : IntDyneVec;
XValue : DblDyneVec;
maxmean : double;
cell1 : string;
begin
SetLength(GrpCovMeans,noind,noind);
SetLength(AdjMeans,noind);
SetLength(Intercepts,noind);
SetLength(Labels,noind);
SetLength(noingrp,noind);
SetLength(XValue,noind);
// get means for groups and covariates
for j := 1 to NoFixed do // for each fixed variable
begin
nogrps := maxgrp[j-1] - mingrp[j-1] + 1;
maxmean := 0.0;
for i := 1 to nogrps do
begin
XValue[i-1] := i;
noingrp[i-1] := 0;
for k := 1 to NoCovs do GrpCovMeans[i-1,k-1] := 0.0;
end;
for i := 1 to nogrps do AdjMeans[i-1] := 0.0;
for i := 1 to NoCases do
begin
cell1 := Trim(OS3MainFrm.DataGrid.Cells[FixedCols[j-1],i]);
if cell1 = '' then continue;
grp := round(StrToFloat(cell1));
grp := grp - mingrp[j-1] + 1;
noingrp[grp-1] := noingrp[grp-1] + 1;
for k := 1 to NoCovs do
begin
col := CovCols[k-1];
cell1 := Trim(OS3MainFrm.DataGrid.Cells[col,i]);
if cell1 = '' then continue;
value := StrToFloat(cell1);
GrpCovMeans[grp-1,k-1] := GrpCovMeans[grp-1,k-1] + value;
end;
cell1 := Trim(OS3MainFrm.DataGrid.Cells[DepColNo,i]);
if cell1 = '' then continue;
value := StrToFloat(cell1);
AdjMeans[grp-1] := AdjMeans[grp-1] + value;
end; // next case i
SetLength(GraphFrm.Ypoints,1,nogrps);
SetLength(GraphFrm.Xpoints,1,nogrps);
for k := 1 to nogrps do
begin
AdjMeans[k-1] := AdjMeans[k-1] / noingrp[k-1];
GraphFrm.Ypoints[0,k-1] := AdjMeans[k-1];
GraphFrm.Xpoints[0,k-1] := k;
if AdjMeans[k-1] > maxmean then maxmean := AdjMeans[k-1];
for i := 1 to NoCovs do
begin
GrpCovMeans[k-1,i-1] := GrpCovMeans[k-1,i-1] / noingrp[k-1];
end;
end;
// print unadjusted means
OutPutFrm.RichEdit.Lines.Add('');
outline := 'Unadjusted Group Means for Group Variables '
+ OS3MainFrm.DataGrid.Cells[FixedCols[j-1] ,0];
OutPutFrm.RichEdit.Lines.Add(outline);
DynVectorPrint(AdjMeans,nogrps,'Means',Labels,NCases);
OutPutFrm.RichEdit.Lines.Add('');
// plot group means if requested
if plot then
begin
GraphFrm.nosets := 1;
GraphFrm.nbars := nogrps;
GraphFrm.Heading := 'Unadjusted Means';
GraphFrm.XTitle := 'GROUP';
GraphFrm.YTitle := 'Mean';
GraphFrm.barwideprop := 0.5;
GraphFrm.AutoScale := false;
GraphFrm.miny := 0.0;
GraphFrm.maxy := maxmean;
GraphFrm.GraphType := 2; // 3d Vertical Bar Chart
GraphFrm.BackColor := clYellow;
GraphFrm.WallColor := clBlack;
GraphFrm.FloorColor := clLtGray;
GraphFrm.ShowBackWall := true;
GraphFrm.ShowModal;
end;
// get intercepts for group equations for this fixed effect variable
sum := 0.0;
for k := 1 to nogrps - 1 do // no. vectors is 1 less than no. groups
begin
intercepts[k-1] := constant + BWeights[NoCovs+k-1];
sum := sum + BWeights[NoCovs+k-1];
end;
intercepts[nogrps-1] := constant - sum;
// get adjusted means
for k := 1 to nogrps do
begin
sum := 0.0;
for i := 1 to NoCovs do
sum := sum + BWeights[i-1] * (GrpCovMeans[k-1,i-1]-Means[i-1]);
AdjMeans[k-1] := AdjMeans[k-1] - sum;
GraphFrm.Ypoints[0,k-1] := AdjMeans[k-1];
Labels[k-1] := 'Group ' + IntToStr(k);
end;
// plot group means if requested
if plot then
begin
GraphFrm.nosets := 1;
GraphFrm.nbars := nogrps;
GraphFrm.Heading := 'Adjusted Means';
GraphFrm.XTitle := 'GROUP';
GraphFrm.YTitle := 'Mean';
GraphFrm.barwideprop := 0.5;
GraphFrm.AutoScale := false;
GraphFrm.miny := 0.0;
GraphFrm.maxy := maxmean;
GraphFrm.GraphType := 2; // 3d Vertical Bar Chart
GraphFrm.BackColor := clYellow;
GraphFrm.WallColor := clBlack;
GraphFrm.FloorColor := clLtGray;
GraphFrm.ShowBackWall := true;
GraphFrm.ShowModal;
end;
// print results for intercepts
OutPutFrm.RichEdit.Lines.Add('');
outline := 'Intercepts for Each Group Regression Equation for Variable: '
+ OS3MainFrm.DataGrid.Cells[FixedCols[j-1] ,0];
OutPutFrm.RichEdit.Lines.Add(outline);
DynVectorPrint(Intercepts,nogrps,'Inercepts',Labels,NCases);
OutPutFrm.RichEdit.Lines.Add('');
// print adjusted means
OutPutFrm.RichEdit.Lines.Add('');
outline := 'Adjusted Group Means for Group Variables '
+ OS3MainFrm.DataGrid.Cells[FixedCols[j-1] ,0];
OutPutFrm.RichEdit.Lines.Add(outline);
DynVectorPrint(AdjMeans,nogrps,'Means',Labels,NCases);
OutPutFrm.RichEdit.Lines.Add('');
end;
OutPutFrm.ShowModal;
OutPutFrm.RichEdit.Clear;
XValue := nil;
noingrp := nil;
Labels := nil;
intercepts := nil;
AdjMeans := nil;
GrpCovMeans := nil;
GraphFrm.Xpoints := nil;
GraphFrm.Ypoints := nil;
end;
procedure TANCOVAfrm.MultCompare(Sender: TObject);
var
i, j, size : integer;
covmat : DblDyneMat;
outline : string;
title : string;
Labels : StrDyneVec;
sum : double;
df1, df2, F, Prob : double;
begin
SetLength(covmat,noind,noind);
SetLength(Labels,noind);
OutPutFrm.RichEdit.Lines.Add('');
OutPutFrm.RichEdit.Lines.Add('Multiple Comparisons Among Group Means');
OutPutFrm.RichEdit.Lines.Add('');
SVDInverse(IndMat,noind);
size := noind - NoCovs;
title := 'Inverse of Independents Matrix';
for i := 1 to noind do Labels[i-1] := 'Group ' + IntToStr(i);
for i := 1 to noind-NoCovs do
for j := 1 to noind-NoCovs do
covmat[i-1,j-1] := sqr(StdErrEst) * IndMat[NoCovs+i-1,NoCovs+j-1] /
(Variances[NoCovs+j-1] * (NoCases-1));
for i := 1 to size+1 do Labels[i-1] := 'Group ' + IntToStr(i);
// augment matrix
for i := 1 to size do
begin
sum := 0.0;
for j := 1 to size do
begin
sum := sum + covmat[i-1,j-1];
end;
covmat[i-1,size] := -sum;
covmat[size,i-1] := -sum;
end;
sum := 0.0;
for i := 1 to size do sum := sum + covmat[i-1,size];
covmat[size,size] := -sum;
if Inverses.Checked then
begin
title := 'Augmented Covariance Among Group Vectors';
for i := 1 to size do Labels[i-1] := 'Group ' + IntToStr(i);
MAT_PRINT(covmat,size+1,size+1,title,Labels,Labels,NoCases);
end;
// Now, contrast the b coefficients
// Get last B weight from effect coding as - sum of other B weights
BWeights[noind] := 0.0;
for i := 0 to noind-1 do BWeights[noind] := BWeights[noind] - BWeights[i];
for i := 1 to size do
begin
for j := i + 1 to size + 1 do
begin
df1 := 1.0;
df2 := NoCases - noind - 1;
F := sqr(BWeights[NoCovs+i-1] - BWeights[NoCovs+j-1]);
F := F / (covmat[i-1,i-1] + covmat[j-1,j-1] - (covmat[i-1,j-1] + covmat[j-1,i-1]));
Prob := probf(F,df1,df2);
outline := format('Comparison of Group %3d with Group %3d',
[i,j]);
OutPutFrm.RichEdit.Lines.Add(outline);
outline := format('F = %10.3f, probability = %5.3f with degrees of freedom %5.0f and %5.0f',
[F, Prob, df1, df2]);
OutPutFrm.RichEdit.Lines.Add(outline);
end;
end;
OutPutFrm.RichEdit.Lines.Add('');
Labels := nil;
covmat := nil;
end;
initialization
{$I ancovaunit.lrs}
end.