public string printResult(ref int primaryMore, ref int matchMore, string name) { StringBuilder result = new StringBuilder(); group sum = new group() { Postive = primaryGroup.Postive + matchedGroup.Postive, Negative = primaryGroup.Negative + matchedGroup.Negative }; result.AppendLine("Prm_Grp" + getGroupResult(primaryGroup)); result.AppendLine("Mch_Grp" + getGroupResult(matchedGroup)); result.AppendLine("total" + getGroupResult(sum)); double oddsRatio = Math.Round((double)primaryGroup.Postive * matchedGroup.Negative / primaryGroup.Negative / matchedGroup.Postive, 2); double LNofOR = Math.Log(oddsRatio); double SEofLN = Math.Pow(1 / (double)primaryGroup.Postive + 1 / (double)primaryGroup.Negative + 1 / (double)matchedGroup.Postive + 1 / (double)matchedGroup.Negative, 0.5); double UL = LNofOR + 1.96 * SEofLN; double LL = LNofOR - 1.96 * SEofLN; double CI_Upper = Math.Round(Math.Exp(UL), 2); double CI_Lower = Math.Round(Math.Exp(LL), 2); result.AppendLine($"[Odds Ratio] = {oddsRatio}, [95%CI] = {CI_Lower}~{CI_Upper}"); double relativeRisk = Math.Round( (double)primaryGroup.Postive / ((double)primaryGroup.Negative + primaryGroup.Postive) / ((double)matchedGroup.Postive / ((double)matchedGroup.Postive + matchedGroup.Negative)), 2); double LNofRR = Math.Log(relativeRisk); double SEofLNofRR = Math.Pow( (double)primaryGroup.Negative / primaryGroup.Postive / ((double)primaryGroup.Postive + primaryGroup.Negative) + (double)matchedGroup.Negative / matchedGroup.Postive / ((double)matchedGroup.Postive + matchedGroup.Negative) , 0.5); double UL_RR = LNofRR + 1.96 * SEofLNofRR; double LL_RR = LNofRR - 1.96 * SEofLNofRR; double CI_Upper_RR = Math.Round(Math.Exp(UL_RR), 2); double CI_Lower_RR = Math.Round(Math.Exp(LL_RR), 2); result.AppendLine($"[Relative Risk] = {relativeRisk}, [95%CI] = {CI_Lower_RR}~{CI_Upper_RR}"); double primaryGroupPositiveExpect = ((double)primaryGroup.Postive + primaryGroup.Negative) * sum.Postive / (sum.Postive + sum.Negative); double primaryGroupNegativeExpect = ((double)primaryGroup.Postive + primaryGroup.Negative) * sum.Negative / (sum.Postive + sum.Negative); double matchGroupPositiveExpect = ((double)matchedGroup.Postive + matchedGroup.Negative) * sum.Postive / (sum.Postive + sum.Negative); double matchGroupNegativeExpect = ((double)matchedGroup.Postive + matchedGroup.Negative) * sum.Negative / (sum.Postive + sum.Negative); double ChiValue = Math.Pow(primaryGroupPositiveExpect - primaryGroup.Postive, 2) / primaryGroupPositiveExpect + Math.Pow(primaryGroupPositiveExpect - primaryGroup.Postive, 2) / primaryGroupPositiveExpect + Math.Pow(primaryGroupPositiveExpect - primaryGroup.Postive, 2) / primaryGroupPositiveExpect + Math.Pow(primaryGroupPositiveExpect - primaryGroup.Postive, 2) / primaryGroupPositiveExpect; bool significant = ChiValue > CHI_CRITICAL; OddsRatioTable.addOddRatio(name, oddsRatio, CI_Lower, CI_Upper, relativeRisk, CI_Lower_RR, CI_Upper_RR, primaryGroup.Postive, primaryGroup.Negative, matchedGroup.Postive, matchedGroup.Negative, significant); result.AppendLine($"[Chi Value] = {Math.Round(ChiValue, 3)}, Significant = {(significant ? "*" : " none ")} "); if (ChiValue > CHI_CRITICAL) { if ((double)primaryGroup.Postive / (primaryGroup.Postive + primaryGroup.Negative) > (double)matchedGroup.Postive / (matchedGroup.Postive + matchedGroup.Negative)) { primaryMore++; } else { matchMore++; } } return(result.ToString());; }
static void study_RR() { DataSet originDataSet = DataReader.LoadData(@"D:\GI DATA\FA_19609筆_修改 AGE GROUP_BREAST FEEDING.txt"); showDataCount(originDataSet); Criteria.index = originDataSet.index; FieldNameToTest.index = originDataSet.index; Console.WriteLine("remove data without CASESEX and AGE data..."); List <Criteria> BadData_toExclude = new List <Criteria>() { new Criteria("CASESEX", ""), new Criteria("AGE", "9999") }; DataSet dataSet_removeNoAgeOrNoSex = originDataSet.select(null, BadData_toExclude); showDataCount(dataSet_removeNoAgeOrNoSex); Console.WriteLine("select Children..."); DataSet DataSets_Children = new DataSet(); DataSets_Children.copyIndexFromDataSet(originDataSet); for (int i = 0; i < 7; i++) { List <Criteria> ageGroupToInclude = new List <Criteria>() { new Criteria("AGEGROUP", (i + 1).ToString()) }; DataSets_Children = DataSets_Children.joinData(dataSet_removeNoAgeOrNoSex.select(ageGroupToInclude, null)); } showDataCount(DataSets_Children); List <Criteria> non_Vegetarian_toInclude = new List <Criteria>() { new Criteria("PS03", "1") }; List <Criteria> EggMilkVeg_toInclude = new List <Criteria>() { new Criteria("PS03", "2"), }; List <Criteria> EggVeg_toInclude = new List <Criteria>() { new Criteria("PS03", "3"), }; List <Criteria> MilkVeg_toInclude = new List <Criteria>() { new Criteria("PS03", "4"), }; List <Criteria> Pure_Veg_toInclude = new List <Criteria>() { new Criteria("PS03", "5"), }; //Console.WriteLine("select Vegetarian..."); //DataSet dataSet_vegetarian = DataSets_Children.select(Vegetarian_toInclude, null); //showDataCount(dataSet_vegetarian); //Console.WriteLine("select non-Vegetarian..."); //DataSet dataSet_non_vegetarian = DataSets_Children.select(non_Vegetarian_toInclude, null); //showDataCount(dataSet_non_vegetarian); //Console.WriteLine("select Egg-Milk-Vegetarian..."); //DataSet dataSet_Egg_Milk_vegetarian = DataSets_Children.select(EggMilkVeg_toInclude, null); //showDataCount(dataSet_Egg_Milk_vegetarian); //Console.WriteLine("select Egg-Vegetarian..."); //DataSet dataSet_Egg_vegetarian = DataSets_Children.select(EggVeg_toInclude, null); //showDataCount(dataSet_Egg_vegetarian); //Console.WriteLine("select Milk-Vegetarian..."); //DataSet dataSet_Milk_vegetarian = DataSets_Children.select(MilkVeg_toInclude, null); //showDataCount(dataSet_Milk_vegetarian); //Console.WriteLine("select Pure-Vegetarian..."); //DataSet dataSet_pure_vegetarian = DataSets_Children.select(Pure_Veg_toInclude, null); //showDataCount(dataSet_pure_vegetarian); //int agegroups = 7; //int firstAgeGroup = 1; //DataSet[] dataSet_vegetarian_byAge = new DataSet[agegroups]; //DataSet[] dataSet_non_vegetarian_byAge = new DataSet[agegroups]; //for (int i = 0; i < agegroups; i++) //{ // var AgeToInclude = new List<Criteria>() // { // new Criteria("AGEGROUP", (i + firstAgeGroup).ToString()) // }; // Console.WriteLine($"select Vegetarian. age group{i + 1}"); // dataSet_vegetarian_byAge[i] = dataSet_vegetarian.select(AgeToInclude, null); // showDataCount(dataSet_vegetarian_byAge[i]); // Console.WriteLine($"select non-Vegetarian. age group{i + 1}"); // dataSet_non_vegetarian_byAge[i] = dataSet_non_vegetarian.select(AgeToInclude, null); // showDataCount(dataSet_non_vegetarian_byAge[i]); //} //initializeTestList(); //repeat = 100; //basefolder = $@"D:\GI Data\result Veg Subgrouping"; //OddsRatioTable.clear(); //OddsRatioTable.setPath(basefolder + $@"\Odd Ratio Table match=4.txt"); //DoTestAndWriteResultAllTest(dataSet_vegetarian, dataSet_non_vegetarian, "Veg vs NonVeg"); //DoTestAndWriteResultAllTest(dataSet_Egg_Milk_vegetarian, dataSet_non_vegetarian, "EggMilkVeg vs NonVeg"); //DoTestAndWriteResultAllTest(dataSet_Egg_vegetarian, dataSet_non_vegetarian, "EggVeg vs NonVeg"); //DoTestAndWriteResultAllTest(dataSet_Milk_vegetarian, dataSet_non_vegetarian, "MilkVeg vs NonVeg"); //DoTestAndWriteResultAllTest(dataSet_pure_vegetarian, dataSet_non_vegetarian, "PureVeg vs NonVeg"); //for (int i = 0; i < dataSet_vegetarian_byAge.Length; i++) //{ // DoTestAndWriteResultAllTest(dataSet_vegetarian_byAge[i], dataSet_non_vegetarian_byAge[i], $"Veg vs NonVeg _subgrouping by age {i + 1}"); //} OddsRatioTable.writeToFile(); }