00001 using System; 00002 00003 namespace StephenAshley.Biostatistics 00004 { 00005 using NUMBER = Decimal; 00006 00013 public class RelativeRiskTest 00014 { 00019 public RelativeRiskTest() { data = new Fields(); } 00020 00029 public RelativeRiskTest(Int32 a, Int32 b, Int32 c, Int32 d) 00030 { 00031 data = new Fields(a, b, c, d); 00032 } 00033 00034 private Fields data; 00035 00040 public Int32 A() 00041 { 00042 return data.A; 00043 } 00044 00049 public void SetA(Int32 value) 00050 { 00051 data.A = value; 00052 } 00053 00058 public Int32 B() 00059 { 00060 return data.B; 00061 } 00062 00067 public void SetB(Int32 value) 00068 { 00069 data.B = value; 00070 } 00071 00076 public Int32 C() 00077 { 00078 return data.C; 00079 } 00080 00085 public void SetC(Int32 value) 00086 { 00087 data.C = value; 00088 } 00089 00094 public Int32 D() 00095 { 00096 return data.D; 00097 } 00098 00103 public void SetD(Int32 value) 00104 { 00105 data.D = value; 00106 } 00107 00113 public NUMBER ChiSquared() 00114 { 00115 ChiSquaredArray chiSquaredArray = new ChiSquaredArray(2, 2); 00116 chiSquaredArray.Add(0, 0, A()); 00117 chiSquaredArray.Add(1, 0, B()); 00118 chiSquaredArray.Add(0, 1, C()); 00119 chiSquaredArray.Add(1, 1, D()); 00120 00121 NUMBER decResult = chiSquaredArray.ChiSquared(); 00122 return decResult; 00123 } 00124 00131 public NUMBER ConfidenceLevelUpperLimit() 00132 { 00133 NUMBER decD; 00134 try 00135 { 00136 NUMBER decA = 1.959964394569397m * StandardErrorOfLogOfRelativeRisk(); 00137 NUMBER decB = DecMath.Log(RelativeRisk()); 00138 NUMBER decC = decB + decA; 00139 decD = DecMath.Exp(decC); 00140 } 00141 catch (Exception e) 00142 { 00143 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00144 } 00145 return decD; 00146 } 00147 00154 public NUMBER ConfidenceLevelLowerLimit() 00155 { 00156 NUMBER decD; 00157 try 00158 { 00159 NUMBER decA = 1.959964394569397m * StandardErrorOfLogOfRelativeRisk(); 00160 NUMBER decB = DecMath.Log(RelativeRisk()); 00161 NUMBER decC = decB - decA; 00162 decD = DecMath.Exp(decC); 00163 } 00164 catch (Exception e) 00165 { 00166 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00167 } 00168 return decD; 00169 } 00170 00178 public NUMBER LogRelativeRisk() 00179 { 00180 NUMBER decResult; 00181 try 00182 { 00183 decResult = DecMath.Log(RelativeRisk()); 00184 } 00185 catch (Exception e) 00186 { 00187 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00188 } 00189 return decResult; 00190 } 00191 00197 public NUMBER p() 00198 { 00199 return Convert.ToDecimal(Distributions.ProbabilityChiSq( 00200 (double)ChiSquared(), 1)); 00201 } 00202 00208 public NUMBER RelativeRisk() 00209 { 00210 NUMBER decResult; 00211 try 00212 { 00213 decResult = (Convert.ToDecimal(A()) / Convert.ToDecimal(A() + B())) 00214 / 00215 (Convert.ToDecimal(C()) / Convert.ToDecimal(C() + D())); 00216 } 00217 catch (Exception e) 00218 { 00219 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00220 } 00221 return decResult; 00222 } 00223 00228 public NUMBER StandardErrorOfLogOfRelativeRisk() 00229 { 00230 NUMBER decStandardErrorOfLogOfRelativeRisk = 0.0m; 00231 try 00232 { 00233 NUMBER decA = (NUMBER)A() / (NUMBER)(A() + B()); 00234 NUMBER decB = 1.0m - decA; 00235 NUMBER decC = decB / (NUMBER)A(); 00236 00237 NUMBER decD = (NUMBER)C() / (NUMBER)(C() + D()); 00238 NUMBER decE = 1.0m - decD; 00239 NUMBER decF = decE / (NUMBER)C(); 00240 00241 NUMBER decG = decC + decF; 00242 decStandardErrorOfLogOfRelativeRisk = DecMath.Sqrt(decG); 00243 double StandardErrorOfRelativeRisk = 00244 Math.Exp((double)decStandardErrorOfLogOfRelativeRisk); 00245 } 00246 catch (Exception e) 00247 { 00248 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00249 } 00250 return decStandardErrorOfLogOfRelativeRisk; 00251 } 00252 00253 private struct Fields 00254 { 00255 public Fields(Int32 A, Int32 B, Int32 C, Int32 D) 00256 { 00257 if (A < 0) 00258 throw new BiostatisticsException( 00259 "A value in Fields constructor of RelativeRisk Text less than 0."); 00260 00261 if (B < 0) 00262 throw new BiostatisticsException( 00263 "B value in Fields constructor of RelativeRisk Text less than 0."); 00264 00265 if (C < 0) 00266 throw new BiostatisticsException( 00267 "C value in Fields constructor of RelativeRisk Text less than 0."); 00268 00269 if (D < 0) 00270 throw new BiostatisticsException( 00271 "D value in Fields constructor of RelativeRisk Text less than 0."); 00272 00273 iA = A; 00274 iB = B; 00275 iC = C; 00276 iD = D; 00277 } 00278 00279 public Fields(Fields fields) 00280 { 00281 iA = fields.iA; 00282 iB = fields.iB; 00283 iC = fields.iC; 00284 iD = fields.iD; 00285 } 00286 00287 public Int32 A 00288 { 00289 get 00290 { 00291 return iA; 00292 } 00293 set 00294 { 00295 if (value < 0) 00296 throw new BiostatisticsException( 00297 "A value in Fields constructor of RelativeRiskText less than 0."); 00298 iA = value; 00299 } 00300 } 00301 00302 public Int32 C 00303 { 00304 get 00305 { 00306 return iB; 00307 } 00308 set 00309 { 00310 if (value < 0) 00311 throw new BiostatisticsException( 00312 "B value in Fields constructor of RelativeRiskText less than 0."); 00313 iB = value; 00314 } 00315 } 00316 00317 public Int32 B 00318 { 00319 get 00320 { 00321 return iC; 00322 } 00323 set 00324 { 00325 if (value < 0) 00326 throw new BiostatisticsException( 00327 "C value in Fields constructor of RelativeRiskText less than 0."); 00328 iC = value; 00329 } 00330 } 00331 00332 public Int32 D 00333 { 00334 get 00335 { 00336 return iD; 00337 } 00338 set 00339 { 00340 if (value < 0) 00341 throw new BiostatisticsException( 00342 "D value in Fields constructor of RelativeRiskText less than 0."); 00343 iD = value; 00344 } 00345 } 00346 00347 private Int32 iA, iB, iC, iD; 00348 } 00349 } 00350 }