00001 using System; 00002 using System.Collections.Generic; 00003 00004 namespace StephenAshley.Biostatistics 00005 { 00006 using NUMBER = Decimal; 00014 public class CorrelationRegressionGroupsCollection : GroupsCollection 00015 { 00016 #region *** constructors *** 00020 public CorrelationRegressionGroupsCollection() : base() { } 00021 00027 public CorrelationRegressionGroupsCollection(List<Group> lstGroups) 00028 : base(lstGroups) 00029 { 00030 } 00031 #endregion 00032 #region ***** methods ****** 00043 public override void Add(Group group) 00044 { 00045 if (group == null) 00046 throw new BiostatisticsException( 00047 "group parameter in Add method of CorrelationRegressionGroupsCollection is null."); 00048 00049 if (groupsArray != null && groupsArray.Length > 0) 00050 { 00051 if (groupsArray[0].n() != group.n()) 00052 throw new BiostatisticsException( 00053 "Added group does not have an equal number of elements to the other Group objects in the collection."); 00054 } 00055 00056 base.Add(group); 00057 } 00058 00071 public override void AddRange(List<Group> lstGroups) 00072 { 00073 if (lstGroups == null) 00074 throw new BiostatisticsException( 00075 "lstGroups paramter in AddRange method of CorrelationRegressionGroupsCollection is null."); 00076 00077 foreach (Group group in lstGroups) 00078 Add(group); 00079 } 00080 00088 public NUMBER CoefficientOfDetermination() 00089 { 00090 return CorrelationCoefficient() * CorrelationCoefficient(); 00091 } 00092 00099 public Int32 DegreesOfFreedom() 00100 { 00101 return N() - 2; 00102 } 00103 00110 public Int32 DF() { return DegreesOfFreedom(); } 00111 00117 public Int32 N() { return groupsArray[0].n(); } 00118 00125 public NUMBER p() { return Distributions.ProbabilityT(t(), DegreesOfFreedom()); } 00126 00134 public NUMBER RSquared() { return CoefficientOfDetermination(); } 00135 00147 public NUMBER CorrelationCoefficient() 00148 { 00149 NUMBER decCorrelationCoefficient; 00150 try 00151 { 00152 decCorrelationCoefficient = SumOfCoDeviatesXY() 00153 / DecMath.Sqrt(groupsArray[0].SumOfSquaredDeviates() 00154 * groupsArray[1].SumOfSquaredDeviates()); 00155 } 00156 catch (Exception e) 00157 { 00158 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00159 } 00160 return decCorrelationCoefficient; 00161 } 00162 00174 public NUMBER R() { return CorrelationCoefficient(); } 00175 00183 public NUMBER ResidualVarianceOfY() 00184 { 00185 NUMBER decResidualVarianceOfY; 00186 try 00187 { 00188 decResidualVarianceOfY = SumOfSquaredResiduals() / 00189 N(); 00190 } 00191 catch (Exception e) 00192 { 00193 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00194 } 00195 return decResidualVarianceOfY; 00196 } 00197 00203 public NUMBER Slope() 00204 { 00205 NUMBER decSlope; 00206 try 00207 { 00208 decSlope = SumOfCoDeviatesXY() / 00209 groupsArray[0].SumOfSquaredDeviates(); 00210 } 00211 catch (Exception e) 00212 { 00213 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00214 } 00215 return decSlope; 00216 } 00217 00225 public NUMBER StandardDeviationOfResiduals() 00226 { 00227 return DecMath.Sqrt(ResidualVarianceOfY()); 00228 } 00229 00239 public NUMBER StandardErrorOfEstimate() 00240 { 00241 NUMBER decStandardErrorOfEstimate; 00242 try 00243 { 00244 decStandardErrorOfEstimate = DecMath.Sqrt(SumOfSquaredResiduals() 00245 / (N() - 2)); 00246 } 00247 catch (Exception e) 00248 { 00249 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00250 } 00251 return decStandardErrorOfEstimate; 00252 } 00253 00263 public NUMBER SE() { return StandardErrorOfEstimate(); } 00264 00272 public NUMBER StandardErrorOfIntercept() 00273 { 00274 NUMBER decStandardErrorOfIntercept; 00275 try 00276 { 00277 decStandardErrorOfIntercept = StandardErrorOfEstimate() * DecMath.Sqrt((1.0m / N()) 00278 + (((groupsArray[0].Sum() / N()) * (groupsArray[0].Sum() / N())) 00279 / groupsArray[0].SumOfSquaredDeviates())); 00280 } 00281 catch (Exception e) 00282 { 00283 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00284 } 00285 return decStandardErrorOfIntercept; 00286 } 00287 00295 public NUMBER StandardErrorOfSlope() 00296 { 00297 NUMBER decStandardErrorOfSlope; 00298 try 00299 { 00300 decStandardErrorOfSlope = StandardErrorOfEstimate() / 00301 DecMath.Sqrt(groupsArray[0].SumOfSquaredDeviates()); 00302 } 00303 catch (Exception e) 00304 { 00305 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00306 } 00307 return decStandardErrorOfSlope; 00308 } 00309 00319 public NUMBER SumOfCoDeviatesXY() 00320 { 00321 NUMBER decSumOfCoDeviatesXY; 00322 try 00323 { 00324 Group grpX = groupsArray[0]; 00325 Group grpY = groupsArray[1]; 00326 NUMBER decSumOfProducts = 0.0m; 00327 for (Int32 i = 0; i < grpX.n(); i++) 00328 decSumOfProducts += grpX[i] * grpY[i]; 00329 decSumOfCoDeviatesXY = decSumOfProducts - ((grpX.Sum() * grpY.Sum()) / grpX.n()); 00330 } 00331 catch (Exception e) 00332 { 00333 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00334 } 00335 00336 return decSumOfCoDeviatesXY; 00337 } 00338 00348 public NUMBER SCxy() { return SumOfCoDeviatesXY(); } 00349 00357 public NUMBER SumOfSquaredResiduals() 00358 { 00359 return groupsArray[1].SumOfSquaredDeviates() * 00360 (1.0m - CoefficientOfDetermination()); 00361 } 00362 00367 public NUMBER SSResidual() { return SumOfSquaredResiduals(); } 00368 00374 public NUMBER t() 00375 { 00376 if (StandardErrorOfSlope() == 0.0m) 00377 return Decimal.MaxValue; 00378 else return Slope() / StandardErrorOfSlope(); 00379 } 00380 00386 public NUMBER YIntercept() 00387 { 00388 return groupsArray[1].Mean() - Slope() * 00389 groupsArray[0].Mean(); 00390 } 00391 #endregion 00392 } 00393 }