00001 using System;
00002 using System.Collections.Generic;
00003
00004 namespace StephenAshley.Biostatistics
00005 {
00006 using NUMBER = Decimal;
00007
00016 public class SpearmanRankSumGroupsCollection : GroupsCollection
00017 {
00018 #region *** constructors ***
00022 public SpearmanRankSumGroupsCollection() : base() { }
00023
00029 public SpearmanRankSumGroupsCollection(List<Group> lstGroups)
00030 : base(lstGroups)
00031 {
00032 }
00033 #endregion
00034 #region ***** methods ******
00040 public override void Add(Group group)
00041 {
00042 if (group == null)
00043 throw new BiostatisticsException(
00044 "group parameter in the Add method of the SpearmanRankSumGroupsCollection is null.");
00045
00046 if (groupsArray != null && groupsArray.Length > 0)
00047 {
00048 if (groupsArray[0].n() != group.n())
00049 throw new BiostatisticsException(
00050 "Addition of Group with a different number of elements than other Groups in SpearmanRankSumGroupsCollection.");
00051 }
00052
00053 base.Add(group);
00054 }
00055
00061 public override void AddRange(List<Group> lstGroups)
00062 {
00063 if (lstGroups == null)
00064 throw new BiostatisticsException(
00065 "lstGroups parameter in AddRange method of SpearmanRankSumGroupsCollection is null.");
00066
00067 foreach (Group group in lstGroups)
00068 Add(group);
00069 }
00070 #endregion
00071 #region ***** properties *****
00076 public Int32 N()
00077 { return groupsArray[0].n(); }
00078
00083 public NUMBER SpearmanCoefficient()
00084 {
00085 List<Spearman> lstX = new List<Spearman>(N());
00086 List<Spearman> lstY = new List<Spearman>(N());
00087 List<NUMBER> lstRanksAscending = new List<decimal>(N());
00088 List<NUMBER> lstRanksDescending = new List<decimal>(N());
00089
00090 foreach (NUMBER decNumber in groupsArray[0])
00091 lstX.Add(new Spearman(decNumber));
00092 foreach (NUMBER decNumber in groupsArray[1])
00093 lstY.Add(new Spearman(decNumber));
00094
00095 foreach (Spearman sp in lstX)
00096 {
00097 lstRanksAscending.Add(sp.Data);
00098 }
00099 lstRanksAscending.Sort();
00100 lstRanksDescending.AddRange(lstRanksAscending);
00101 lstRanksDescending.Reverse();
00102
00103 foreach (Spearman sp in lstX)
00104 {
00105 NUMBER a = GetRank(sp.Data, lstRanksDescending);
00106 NUMBER b = GetRank(sp.Data, lstRanksAscending);
00107 sp.Rank = ((a + N() -
00108 b + 1.0m) / 2.0m);
00109 }
00110 lstRanksAscending.Clear();
00111 lstRanksDescending.Clear();
00112
00113 foreach (Spearman sp in lstY)
00114 {
00115 lstRanksAscending.Add(sp.Data);
00116 }
00117 lstRanksAscending.Sort();
00118 lstRanksDescending.AddRange(lstRanksAscending);
00119 lstRanksDescending.Reverse();
00120
00121 foreach (Spearman sp in lstY)
00122 {
00123 NUMBER a = GetRank(sp.Data, lstRanksDescending);
00124 NUMBER b = GetRank(sp.Data, lstRanksAscending);
00125 sp.Rank = ((a + N() -
00126 b + 1.0m) / 2.0m);
00127 }
00128
00129 NUMBER decSumOfSquaredDifferences = 0.0m;
00130 for (int i = 0; i < N(); i++)
00131 {
00132 NUMBER decDifference = lstX[i].Rank - lstY[i].Rank;
00133 decSumOfSquaredDifferences += decDifference * decDifference;
00134 }
00135
00136 return 1.0m - ((6.0m * decSumOfSquaredDifferences) / (N() * (N() * N() - 1)));
00137 }
00138
00147 public string p()
00148 {
00149 NUMBER decTemp = DecMath.Abs(SpearmanCoefficient());
00150
00151 switch (N())
00152 {
00153 case 0:
00154 case 1:
00155 case 2:
00156 case 3:
00157 case 4:
00158 throw new BiostatisticsException(
00159 "Sample size too small to reach statistical significance.");
00160 case 5:
00161 if (decTemp == 1.0m)
00162 return "< 0.02";
00163 else return "> 0.05";
00164 case 6:
00165 if (decTemp == 1.0m)
00166 return "< 0.01";
00167 else if (decTemp > 0.943m)
00168 return "< 0.02";
00169 else if (decTemp > 0.886m)
00170 return "< 0.05";
00171 else return "> 0.05";
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 default:
00295 return string.Format("{0:N27}", Distributions.ProbabilityRho(
00296 SpearmanCoefficient(), (Int16)N())).TrimEnd(new char[] { '0' });
00297 }
00298 }
00299
00306 public NUMBER Rs()
00307 {return SpearmanCoefficient(); }
00308
00309 #endregion
00310
00311 private UInt16 GetRank(NUMBER decTarget, List<NUMBER> lstRanks)
00312 {
00313 UInt16 i = 0;
00314 for (; i < N(); i++)
00315 if (decTarget == lstRanks[i])
00316 return i;
00317 return 0;
00318 }
00319 }
00320
00321 class Spearman
00322 {
00323 public Spearman(NUMBER decNumber)
00324 {
00325 decData = decNumber;
00326 }
00327 NUMBER decData;
00328 NUMBER decRank;
00329
00330
00331 public NUMBER Data { get { return decData; } set { decData = value; } }
00332 public NUMBER Rank { get { return decRank; } set { decRank = value; } }
00333 }
00334 }