00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005
00006 namespace StephenAshley.Biostatistics
00007 {
00008 using NUMBER = Decimal;
00009
00016 public class KruskalWallisGroupsCollection : GroupsCollection
00017 {
00021 public KruskalWallisGroupsCollection() : base() { }
00022
00028 public KruskalWallisGroupsCollection(List<Group> groups) : base(groups) { }
00029
00039 public override void Add(Group group)
00040 {
00041 if (group == null)
00042 throw new BiostatisticsException(
00043 "group is a null reference.");
00044
00045 if (groupsArray != null && k() > 19)
00046 throw new BiostatisticsException(
00047 "Addition of Group to a full KruskalWallisGroupsCollection.");
00048
00049 if (group.n() == 0)
00050 throw new BiostatisticsException(
00051 "Group has no members.");
00052
00053 if (groupsArray == null || k() < 20)
00054 {
00055 base.Add(group);
00056 if (groupsArray != null && k() > 1)
00057 Initialize();
00058 return;
00059 }
00060 }
00061
00062 private void Initialize()
00063 {
00064
00065 if (groupsArray == null)
00066 throw new BiostatisticsException("groupsArray contains no Groups");
00067 if (k() < 2)
00068 throw new BiostatisticsException(
00069 "Fewer than 2 Groups for Kruskal-Wallis Test.");
00070 foreach (Group group in groupsArray)
00071 {
00072 if (group.n() == 0)
00073 throw new BiostatisticsException(
00074 "KruskalWallisGroupsCollection contains a Group with no members.");
00075 }
00076
00077
00078 List<Rank> lstRanks = new List<Rank>();
00079 for (int i = 0; i < k(); i++)
00080 {
00081 foreach (NUMBER decValue in groupsArray[i])
00082 {
00083 lstRanks.Add(new Rank(i, decValue, 0));
00084 }
00085 }
00086
00087
00088 lstRanks.Sort(Rank.CompareRanks);
00089
00090
00091 for (int i = 0; i < lstRanks.Count; i++)
00092 {
00093 lstRanks[i] = new Rank(lstRanks[i].iGroup, lstRanks[i].decValue,
00094 (NUMBER)(i + 1));
00095 }
00096
00097
00098 Int32 iTies = Rank.AdjustTies(ref lstRanks);
00099
00100
00101 iN = 0;
00102 foreach (Group group in groupsArray)
00103 {
00104 iN += group.n();
00105 }
00106
00107
00108 R = new NUMBER[k()];
00109 for (int i = 0; i < k(); i++)
00110 {
00111 R[i] = 0.0m;
00112 }
00113 foreach (Rank rank in lstRanks)
00114 {
00115 R[rank.iGroup] += rank.decRank;
00116 }
00117
00118
00119 NUMBER decA = 0.0m;
00120 for (int i = 0; i < k(); i++)
00121 {
00122 decA += (NUMBER)(R[i] * R[i]) / (NUMBER)groupsArray[i].n();
00123 }
00124 NUMBER decB = 12.0m / (NUMBER)(N() * (N() + 1));
00125 decH = decB * decA - 3.0m * (N() + 1);
00126
00127 NUMBER decC = (NUMBER)iTies / (NUMBER)(N() * N() * N() - N());
00128 decCorrection = 1.0m - decC;
00129
00130 decHc = decH / decCorrection;
00131
00132
00133 }
00134
00140 public Int32 N() { return iN; }
00141
00146 public NUMBER H() { return decHc; }
00147
00152 public Int32 DegreesOfFreedom() { return k() - 1; }
00153
00159 public NUMBER p()
00160 {
00161 return (NUMBER)(Distributions.ProbabilityChiSq((double)H(),
00162 DegreesOfFreedom()));
00163 }
00164
00165 private Int32 iN;
00166 private NUMBER decH;
00167 private NUMBER decHc;
00168 private NUMBER decCorrection;
00169
00174 public NUMBER[] R;
00175 }
00176 }