// g01af Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class G01AFE { static string datafile = "ExampleData/g01afe.d"; static void Main(String[] args) { if (args.Length == 1) { datafile = args[0]; } StartExample(); } public static void StartExample() { try { DataReader sr = new DataReader(datafile); double chis = 0.0; int i, j, k, m, m1, m2, n, n1, n2, ndf, npos, nprob, num; double[] p = new double[21]; int ifail; Console.WriteLine("g01af Example Program Results"); // Skip heading in data file sr.Reset(); sr.Reset(); nprob = int.Parse(sr.Next()); for (i = 1; i <= nprob; i++) { sr.Reset(); m = int.Parse(sr.Next()); n = int.Parse(sr.Next()); double[,] pred = new double[m+1, n+1]; int[,] nobs = new int[m+1, n+1]; num = int.Parse(sr.Next()); if (m >= 1 && n >= 1) { Console.WriteLine(""); Console.WriteLine(" {0}{1,4}", "Problem ", i); for (j = 1; j <= m; j++) { sr.Reset(); for (k = 1; k <= n; k++) { nobs[j - 1, k - 1] = int.Parse(sr.Next()); } } Console.WriteLine(" {0}", "Data as input -"); Console.WriteLine(" {0}{1,3}", "Number of rows", m); Console.WriteLine(" {0}{1,3}", "Number of columns", n); Console.WriteLine(" {0}{1,3}{2}", "NUM =", num, " (NUM = 1 means table reduced in size if necessary)"); m = m + 1; n = n + 1; // G01.g01af(m, n, nobs, ref num, pred, out chis, p, out npos, out ndf, out m1, out n1, out ifail); // if (ifail == 0) { Console.WriteLine(""); if (num == 0) { Console.WriteLine(" {0}", "Successful call of g01af"); m2 = m - 1; n2 = n - 1; if (m1 != m2) { Console.WriteLine(" {0}{1,3}{2}{3,3}", "No. of rows reduced from ", m2, " to ", m1); } if (n1 != n2) { Console.WriteLine(" {0}{1,3}{2}{3,3}", "No. of cols reduced from ", n2, " to ", n1); } Console.WriteLine(""); Console.WriteLine(" {0}", "Table of observed and expected frequencies"); Console.WriteLine(""); Console.WriteLine(" {0}", " Column"); Console.Write("{0, 10}", ""); for (k = 1; k <= n1; k++) { Console.Write(" {0,4}", k); } Console.WriteLine(""); Console.WriteLine(" {0}", "Row"); for (j = 1; j <= m1; j++) { Console.Write(" {0, 2}\t", j); for (k = 1; k <= n1; k++) { Console.Write("{0,6}", nobs[j - 1, k - 1]); } Console.Write("\n\t"); for (k = 1; k <= n1; k++) { Console.Write("{0,6:f0}", pred[j - 1, k - 1]); } Console.WriteLine(""); Console.WriteLine("{0, 58}{1,7}", "Row total = ", nobs[j - 1, n - 1]); } Console.WriteLine(" {0}", "Column"); Console.Write(" {0}", "totals"); for (k = 1; k <= n1; k++) { Console.Write(" {0, 6}", nobs[m - 1, k - 1]); } Console.WriteLine(""); Console.WriteLine("{0, 60}{1,7}", "Grand total = ", nobs[m - 1, n - 1]); Console.WriteLine(""); Console.WriteLine(" {0}{1,10:f3}{2}{3,3}", "Chi-squared = ", chis, " D.F. = ", ndf); } else { Console.WriteLine(""); Console.WriteLine(" {0}", "Fisher's exact test for 2*2 table"); Console.WriteLine(""); Console.WriteLine(" {0}", "Table of observed frequencies"); Console.WriteLine(""); Console.WriteLine(" {0}", " Column"); Console.WriteLine(" {0}", " 1 2"); Console.WriteLine(" {0}", "Row"); for (j = 1; j <= 2; j++) { Console.WriteLine(" {0}", j); for (k = 1; k <= 2; k++) { Console.Write(" {0}", nobs[j - 1, k - 1]); } Console.WriteLine(""); Console.WriteLine(" {0}{1,7}", "Row total = ", nobs[j - 1, n - 1]); } Console.WriteLine(""); Console.WriteLine(" {0}", "Column"); Console.WriteLine(" {0}", "totals"); for (k = 1; k <= 2; k++) { Console.Write(" {0}", nobs[m - 1, k - 1]); } Console.WriteLine(""); Console.WriteLine(" {0}{1,7}", "Grand total = ", nobs[m - 1, n - 1]); Console.WriteLine(""); Console.WriteLine(" {0}{1,4}{2}", "This table corresponds to element ", npos, " in vector P below"); Console.WriteLine(""); Console.WriteLine(" {0}", "Vector P"); Console.WriteLine(""); Console.WriteLine(" {0}", " I P(I)"); for (j = 1; j <= num; j++) { Console.Write(" {0}", j, p[j - 1]); } Console.WriteLine(""); } } else { Console.WriteLine("** g01af failed with ifail = {0,5}", ifail); } } } // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine( "Exception Raised"); } } } }