00001 using System; 00002 00003 namespace StephenAshley.Biostatistics 00004 { 00005 using NUMBER = Decimal; 00006 00013 public class OddsRatioTest 00014 { 00019 public OddsRatioTest() { data = new Fields(); } 00020 00028 public OddsRatioTest(Int32 a, Int32 b, Int32 c, Int32 d) 00029 { 00030 data = new Fields(); 00031 SetA(a); 00032 SetB(b); 00033 SetC(c); 00034 SetD(d); 00035 } 00036 00037 private Fields data; 00038 00046 public Int32 A() 00047 { 00048 return data.iA; 00049 } 00050 00058 public void SetA(Int32 value) 00059 { 00060 if (value < 1) 00061 throw new BiostatisticsException("A value in OddsRatioText less than 1."); 00062 data.iA = value; 00063 } 00064 00072 public Int32 B() 00073 { 00074 return data.iB; 00075 } 00076 00084 public void SetB(Int32 value) 00085 { 00086 if (value < 1) 00087 throw new BiostatisticsException("B value in OddsRatioText less than 1."); 00088 data.iB = value; 00089 } 00090 00098 public Int32 C() 00099 { 00100 return data.iC; 00101 } 00102 00110 public void SetC(Int32 value) 00111 { 00112 if (value < 1) 00113 throw new BiostatisticsException("C value in OddsRatioText less than 1."); 00114 data.iC = value; 00115 } 00123 public Int32 D() 00124 { 00125 return data.iD; 00126 } 00127 00135 public void SetD(Int32 value) 00136 { 00137 if (value < 1) 00138 throw new BiostatisticsException("B value in OddsRatioText less than 1."); 00139 data.iD = value; 00140 } 00141 00150 public NUMBER ChiSquared() 00151 { 00152 Int32 iATotal = A() + C(); 00153 Int32 iBTotal = B() + D(); 00154 Int32 iTotal = iATotal + iBTotal; 00155 Int32 i1Total = A() + B(); 00156 Int32 i2Total = C() + D(); 00157 00158 NUMBER decResult; 00159 try 00160 { 00161 NUMBER decAe = (Convert.ToDecimal(iATotal) / Convert.ToDecimal(iTotal)) 00162 * Convert.ToDecimal(i1Total); 00163 00164 NUMBER decBe = (Convert.ToDecimal(iBTotal) / Convert.ToDecimal(iTotal)) 00165 * Convert.ToDecimal(i1Total); 00166 00167 NUMBER decCe = (Convert.ToDecimal(iATotal) / Convert.ToDecimal(iTotal)) 00168 * Convert.ToDecimal(i2Total); 00169 00170 NUMBER decDe = (Convert.ToDecimal(iBTotal) / Convert.ToDecimal(iTotal)) 00171 * Convert.ToDecimal(i2Total); 00172 00173 if (decAe == 0.0m || decBe == 0.0m || decCe == 0.0m || decDe == 0.0m) 00174 throw new BiostatisticsException( 00175 "Divide by zero in ChiSquared of OddsRatioTest."); 00176 00177 decResult = ((DecMath.Abs(Convert.ToDecimal(A()) - decAe) - 0.5m) * 00178 (DecMath.Abs(Convert.ToDecimal(A()) - decAe) - 0.5m) / 00179 decAe) 00180 + 00181 ((DecMath.Abs(Convert.ToDecimal(B()) - decBe) - 0.5m) * 00182 (DecMath.Abs(Convert.ToDecimal(B()) - decBe) - 0.5m) / 00183 decBe) 00184 + 00185 ((DecMath.Abs(Convert.ToDecimal(C()) - decCe) - 0.5m) * 00186 (DecMath.Abs(Convert.ToDecimal(C()) - decCe) - 0.5m) / 00187 decCe) 00188 + 00189 ((DecMath.Abs(Convert.ToDecimal(D()) - decDe) - 0.5m) * 00190 (DecMath.Abs(Convert.ToDecimal(D()) - decDe) - 0.5m) / 00191 decDe); 00192 } 00193 catch (Exception e) 00194 { throw new BiostatisticsException(e.Source + ": " + e.Message, e); } 00195 return decResult; 00196 } 00197 00204 public NUMBER ConfidenceIntervalUpperLimit() 00205 { 00206 NUMBER decResult; 00207 try 00208 { 00209 decResult = DecMath.Exp(LogOddsRatio() + 00210 1.959964394569397m * StandardErrorOfLogOddsRatio()); 00211 } 00212 catch (Exception e) 00213 { 00214 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00215 } 00216 return decResult; 00217 } 00218 00225 public NUMBER ConfidenceIntervalLowerLimit() 00226 { 00227 NUMBER decResult; 00228 try 00229 { 00230 decResult = DecMath.Exp(LogOddsRatio() - 00231 1.959964394569397m * StandardErrorOfLogOddsRatio()); 00232 } 00233 catch (Exception e) 00234 { 00235 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00236 } 00237 return decResult; 00238 } 00239 00246 public NUMBER LogOddsRatio() 00247 { 00248 NUMBER decResult; 00249 try 00250 { 00251 decResult = DecMath.Log(OddsRatio()); 00252 } 00253 catch (Exception e) 00254 { 00255 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00256 } 00257 return decResult; 00258 } 00259 00266 public NUMBER OddsRatio() 00267 { 00268 NUMBER decResult; 00269 try 00270 { 00271 decResult = Convert.ToDecimal(A() * D()) / Convert.ToDecimal(C() * B()); 00272 } 00273 catch (Exception e) 00274 { 00275 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00276 } 00277 return decResult; 00278 } 00279 00288 public NUMBER P() 00289 { 00290 NUMBER decResult; 00291 try 00292 { 00293 decResult = Convert.ToDecimal(Distributions.ProbabilityChiSq( 00294 Convert.ToDouble(ChiSquared()), 1)); 00295 } 00296 catch (Exception e) 00297 { 00298 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00299 } 00300 return decResult; 00301 } 00302 00309 public NUMBER StandardErrorOfLogOddsRatio() 00310 { 00311 NUMBER decResult; 00312 try 00313 { 00314 decResult = DecMath.Sqrt(1.0m / Convert.ToDecimal(A()) 00315 + 1.0m / Convert.ToDecimal(B()) 00316 + 1.0m / Convert.ToDecimal(C()) 00317 + 1.0m / Convert.ToDecimal(D())); 00318 } 00319 catch (Exception e) 00320 { 00321 throw new BiostatisticsException(e.Source + ": " + e.Message, e); 00322 } 00323 return decResult; 00324 } 00325 00326 private struct Fields 00327 { 00328 public Int32 iA, iB, iC, iD; 00329 } 00330 } 00331 }