00001 using System;
00002 using System.Collections.Generic;
00003
00004 namespace StephenAshley.Biostatistics
00005 {
00006 using NUMBER = Decimal;
00007
00015 public class RepeatedMeasuresANOVAGroupsCollection : ANOVAGroupsCollection
00016 {
00021 public RepeatedMeasuresANOVAGroupsCollection()
00022 : base()
00023 {
00024 N = 0;
00025 }
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00046 public RepeatedMeasuresANOVAGroupsCollection(List<Group> groups)
00047 : base(groups)
00048 {
00049 N = groups[0].n();
00050 Int32 iSize = N;
00051 for (Int32 i = 1; i < k(); i++)
00052 if (groups[i].n() != N)
00053 throw new BiostatisticsException(
00054 "Groups in RepeatedMeasuresANOVAGroupsCollection have unequal sizes.");
00055 }
00056
00060 private Int32 N;
00061
00067 public override void Add(Group group)
00068 {
00069 if (group == null)
00070 throw new BiostatisticsException(
00071 "group parameter in Add method of RepeatedMeasuresGroupsCollection is null.");
00072 if (N == 0)
00073 N = group.n();
00074 else if (N != group.n())
00075 {
00076 throw new BiostatisticsException(
00077 "Group to be added to RepeatedMeasuresGroupsCollection does not have the same number of elements as the other Groups in the collection.");
00078 }
00079 base.Add(group);
00080 }
00081
00087 public override void AddRange(List<Group> lstGroups)
00088 {
00089 base.AddRange(lstGroups);
00090 }
00091
00096 public Int32 DegreesOfFreedomError()
00097 {
00098 return DegreesOfFreedomWithinGroups()
00099 - DegreesOfFreedomSubjects();
00100 }
00101
00106 public Int32 DegreesOfFreedomSubjects()
00107 { return N - 1; }
00108
00113 public new NUMBER F()
00114 { return MeanSquareBetweenGroups() / MeanSquareError(); }
00115
00120 public NUMBER MeanSquareError()
00121 {
00122 return SumOfSquaredDeviatesError()
00123 / DegreesOfFreedomError();
00124 }
00125
00133 public override NUMBER p()
00134 {
00135 return Distributions.ProbabilityF(F(), DegreesOfFreedomBetweenGroups(),
00136 DegreesOfFreedomError());
00137 }
00142 public NUMBER SumOfSquaredDeviatesError()
00143 {
00144 return SumOfSquaredDeviatesWithinGroups() - SumOfSquaredDeviatesForSubjects();
00145 }
00146
00152 public NUMBER SumOfSquaredDeviatesForSubjects()
00153 {
00154 Group grpSubjects = new Group();
00155 NUMBER decSumOfScores, decSumOfSquaredScores;
00156 for (Int32 i = 0; i < N; i++)
00157 {
00158 decSumOfScores = 0.0m;
00159 for (Int32 j = 0; j < k(); j++)
00160 {
00161 decSumOfScores += groupsArray[j][i];
00162 }
00163 grpSubjects.Add(decSumOfScores * decSumOfScores);
00164 }
00165 decSumOfSquaredScores = 0.0m;
00166
00167 for (Int32 i = 0; i < N; i++)
00168 decSumOfSquaredScores += grpSubjects[i];
00169 NUMBER decSumOfSquaredDeviatesForSubjects;
00170 try
00171 {
00172 NUMBER dec1stTerm = decSumOfSquaredScores / k();
00173
00174 NUMBER dec2ndTerm = (elementsANOVAGroup.Sum() * elementsANOVAGroup.Sum())
00175 / elementsANOVAGroup.n();
00176
00177 decSumOfSquaredDeviatesForSubjects = dec1stTerm - dec2ndTerm;
00178 }
00179 catch (Exception e)
00180 {
00181 throw new BiostatisticsException(e.Source + ": " + e.Message, e);
00182 }
00183 return decSumOfSquaredDeviatesForSubjects;
00184 }
00185 }
00186 }