00001 using System;
00002 using System.Collections.Generic;
00003
00004 namespace StephenAshley.Biostatistics
00005 {
00006 using NUMBER = Decimal;
00007
00016 public class ANOVAGroupsCollection : GroupsCollection
00017 {
00018 #region *** constructors ***
00022 public ANOVAGroupsCollection() : base()
00023 {
00024 elementsANOVAGroup = new Group(0);
00025 }
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00042 public ANOVAGroupsCollection(List<Group> groups)
00043 : base(groups)
00044 {
00045 elementsANOVAGroup = new Group(0);
00046 foreach (Group group in groups)
00047 elementsANOVAGroup.AddRange(group.ToArray());
00048 }
00049 #endregion
00050 #region *** fields ***
00055 protected Group elementsANOVAGroup;
00056 #endregion
00057 #region *** methods ***
00058
00066 public override void Add(Group group)
00067 {
00068 base.Add(group);
00069
00070 elementsANOVAGroup.AddRange(group.ToArray());
00071 }
00072
00080 public override void AddRange(List<Group> lstGroups)
00081 {
00082 base.AddRange(lstGroups);
00083 foreach (Group grp in lstGroups)
00084 elementsANOVAGroup.AddRange(grp.ToArray());
00085 }
00086
00095 public new Group this[int index]
00096 {
00097 get
00098 {
00099 Group group;
00100 try
00101 {
00102 group = groupsArray[index];
00103 }
00104 catch (Exception e)
00105 {
00106 throw new BiostatisticsException(e.Source + ": " + e.Message, e);
00107 }
00108 return group;
00109 }
00110 set
00111 {
00112 try
00113 {
00114 groupsArray[index] = value;
00115 }
00116 catch (Exception e)
00117 {
00118 throw new BiostatisticsException(e.Source + ": " + e.Message, e);
00119 }
00120 elementsANOVAGroup = new Group(0);
00121 foreach (Group group in elementsANOVAGroup)
00122 elementsANOVAGroup.AddRange(group.ToArray());
00123 }
00124 }
00125
00132 public Int32 DegreesOfFreedomBetweenGroups()
00133 {
00134 if (k() < 2)
00135 throw new BiostatisticsException(
00136 "ANOVAGroupsCollection DegreesOfFreedomBetweenGroups requires more than 1 Group.");
00137 return k() - 1;
00138 }
00139
00146 public Int32 DegreesOfFreedomTotal()
00147 {
00148 if (k() < 2)
00149 throw new BiostatisticsException(
00150 "ANOVAGroupsCollection DegreesOfFreedomTotal requires more than 1 Group.");
00151 return nTotal() - 1;
00152 }
00153
00159 public Int32 DegreesOfFreedomWithinGroups()
00160 {return nTotal() - k();}
00161
00168 public NUMBER F()
00169 {
00170 NUMBER decF;
00171 try
00172 {
00173 decF = MeanSquareBetweenGroups() / MeanSquareWithinGroups();
00174 }
00175 catch ( Exception e)
00176 {
00177 throw new BiostatisticsException(e.Source + ": " + e.Message, e);
00178 }
00179 return decF;
00180 }
00181
00189 public NUMBER MeanSquareBetweenGroups()
00190 {
00191 NUMBER decMeanSquareBetweenGroups;
00192 try
00193 {
00194 decMeanSquareBetweenGroups = SumOfSquaredDeviatesBetweenGroups() /
00195 DegreesOfFreedomBetweenGroups();
00196 }
00197 catch (Exception e)
00198 {
00199 throw new BiostatisticsException(e.Source + ": " + e.Message, e);
00200 }
00201 return decMeanSquareBetweenGroups;
00202 }
00210 public NUMBER MeanSquareWithinGroups()
00211 {
00212 NUMBER decMeanSquareWithinGroups;
00213 try
00214 {
00215 decMeanSquareWithinGroups = SumOfSquaredDeviatesWithinGroups() /
00216 DegreesOfFreedomWithinGroups();
00217 }
00218 catch (Exception e)
00219 {
00220 throw new BiostatisticsException(e.Source + ": " + e.Message, e);
00221 }
00222 return decMeanSquareWithinGroups;
00223 }
00224
00232 public NUMBER MeanTotalElements()
00233 {return elementsANOVAGroup.Mean();}
00234
00240 public Int32 nTotalElements()
00241 {return elementsANOVAGroup.n();}
00242
00250 public virtual NUMBER p()
00251 {
00252 return Distributions.ProbabilityF(F(), DegreesOfFreedomBetweenGroups(),
00253 DegreesOfFreedomWithinGroups());
00254 }
00255
00263 public NUMBER SampleStandardDeviation()
00264 {return elementsANOVAGroup.SampleStandardDeviation();}
00265
00274 public NUMBER StandardErrorOfMean()
00275 {return elementsANOVAGroup.StandardErrorOfMean();}
00276
00283 public NUMBER SumTotalElements()
00284 {return elementsANOVAGroup.Sum();}
00285
00291 public NUMBER SumOfSquaresTotal()
00292 {return elementsANOVAGroup.SumOfSquares();}
00293
00302 public NUMBER SumOfSquaredDeviatesTotal()
00303 {return elementsANOVAGroup.SumOfSquaredDeviates();}
00304
00312 public NUMBER SumOfSquaredDeviatesBetweenGroups()
00313 {
00314 return SumOfSquaredDeviatesTotal() - SumOfSquaredDeviatesWithinGroups();
00315 }
00316
00322 public NUMBER SumOfSquaredDeviatesWithinGroups()
00323 {
00324 NUMBER decResult = 0m;
00325 foreach (Group group in groupsArray)
00326 decResult += group.SumOfSquaredDeviates();
00327 return decResult;
00328 }
00329 #endregion
00330
00331 }
00332 }