void txtInput_TextChanged(object sender, TextChangedEventArgs e) { int validRows = 0; foreach (Row row in rows) { row.OddsRatio.Text = string.Empty; } foreach (Row row in rows) { double scoreTest; double casesTest; double controlsTest; if (string.IsNullOrEmpty(row.Score.Text) || string.IsNullOrEmpty(row.Cases.Text) || string.IsNullOrEmpty(row.Controls.Text) || !double.TryParse(row.Score.Text, out scoreTest) || !double.TryParse(row.Cases.Text, out casesTest) || !double.TryParse(row.Controls.Text, out controlsTest)) { break; } validRows++; } if (validRows > 0) { double[] scores = new double[validRows]; double[] cases = new double[validRows]; double[] controls = new double[validRows]; for (int x = 0; x < validRows; x++) { scores[x] = double.Parse(rows[x].Score.Text); cases[x] = double.Parse(rows[x].Cases.Text); controls[x] = double.Parse(rows[x].Controls.Text); } ChiSquareResult result = GetChiSquareForTrend(scores, cases, controls); for (int x = 0; x < result.GetOddsRatios().Length; x++) { rows[x].OddsRatio.Text = result.GetOddsRatios()[x].ToString("N3"); } txtChiSquare.Text = result.GetChi() == 0 ? "0" : result.GetChi().ToString("N5"); txtPValue.Text = result.GetPValue() == 0 ? "0" : result.GetPValue().ToString("N5"); } else { txtChiSquare.Text = "..."; txtPValue.Text = "..."; } }
public ChiSquareResult GetChiSquareForTrend(double[] col1, double[] col2, double[] col3) { ChiSquareResult result = new ChiSquareResult(); double[] acrude = new double[col1.Length]; double[] bcrude = new double[col1.Length]; double[] mhORad = new double[col1.Length]; double[] mhORbc = new double[col1.Length]; double[] oddsRatios = new double[col1.Length]; int levels = col1.Length; double ccrude = 0; double dcrude = 0; double ttot; double Vsum = 0; double V1sum = 0; double XMHchisq = 0; double T1 = 0; double T2 = 0; double T3 = 0; double n1 = 0; double n2 = 0; double n = 0; double OR = 1.0; double x, a, b, m; double abase = col2[0]; double bbase = col3[0]; ccrude += abase; dcrude += bbase; for (int t = 0; t < levels; t++) { x = col1[t]; a = col2[t]; b = col3[t]; acrude[t] += a; bcrude[t] += b; m = a + b; T1 += a * x; T2 += m * x; T3 += m * x * x; n1 += a; n2 += b; n += a + b; OR = (a * bbase) / (b * abase); if (t > 0) { ttot = a + bbase + b + abase; mhORad[t] += a * bbase / ttot; mhORbc[t] += b * abase / ttot; } oddsRatios[t] = OR; } result.SetOddsRatios(oddsRatios); Vsum += (n1 * n2 * (n * T3 - (T2 * T2))) / (n * n * (n - 1)); V1sum += T1 - ((n1 / n) * T2); XMHchisq += ((V1sum - 0.5) * (V1sum - 0.5)) / Vsum; result.SetChi(XMHchisq); result.SetPValue(calcP(XMHchisq, 1)); return(result); }