// g02kb Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class G02KBE { static string datafile = "ExampleData/g02kbe.d"; static void Main(String[] args) { if (args.Length == 1) { datafile = args[0]; } StartExample(); } public static void StartExample() { try { DataReader sr = new DataReader(datafile); int i, ip, ip1, j, lh, lpec, m, n, pl, wantb, wantvf; int ldb, tdb, ldpe, tdpe, ldvf, tdvf; // int ifail; Console.WriteLine("g02kb Example Program Results"); // // Skip heading in data file sr.Reset(); // // Read in the problem size information sr.Reset(); n = int.Parse(sr.Next()); m = int.Parse(sr.Next()); lh = int.Parse(sr.Next()); lpec = int.Parse(sr.Next()); wantb = int.Parse(sr.Next()); wantvf = 1; ldpe = 5; double[] h = new double[lh]; double[] nep = new double[lh]; double[,] x = new double[n, m]; double[] y = new double[n]; int[] isx = new int[m]; string[] pec = new string[lpec]; // // Check array sizes if (n < 1 || m > n || lh < 1 || ldpe < lpec) { Console.Write(" ** Problem size is too small or inconsistent"); goto L160; } // // Read in the data if (lpec > 0) { sr.Reset(); for (i = 1; i <= lpec; i++) { pec[i - 1] = sr.Next(); } } for (i = 1; i <= n; i++) { sr.Reset(); for (j = 1; j <= m; j++) { x[i - 1, j - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } y[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } // // Read in the isx flags sr.Reset(); for (i = 1; i <= m; i++) { isx[i - 1] = int.Parse(sr.Next()); } // // Read in the ridge coefficients sr.Reset(); for (i = 1; i <= lh; i++) { h[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } // // Total number of variables ip = 0; for (j = 1; j <= m; j++) { if (isx[j - 1] == 1) { ip = ip + 1; } } if (wantb != 0) { ldb = ip + 1; tdb = lh; } else { ldb = 1; tdb = 1; } if (wantvf != 0) { ldvf = ip; tdvf = lh; } else { ldvf = 0; tdvf = 0; } if (lpec>0) { ldpe = lpec; tdpe = lh; } else { ldpe = 0; tdpe = 0; } double[,] pe = new double[ldpe, tdpe]; double[,] vf = new double[ldvf, tdvf]; double[,] b = new double[ldb, tdb]; // // Output the variance inflation factors and parameter estimates // (original scalings) // // Run the analysis G02.g02kb( n, m, x, isx, ip, y, lh ,h, nep, wantb, b, wantvf, vf, lpec,pec, pe, out ifail); if (ifail != 0) { Console.WriteLine("** g02kb failed with ifail = {0,5}", ifail); goto L160; } // Output results ip1 = ip - 1; // // Summaries Console.WriteLine("{0}{1,10}", "Number of parameters used = ", ip + 1); Console.WriteLine(" {0}", "Effective number of parameters (NEP):"); Console.WriteLine(" {0}", " Ridge "); Console.WriteLine(" {0} {1}", " Coeff. ", "NEP"); for (i = 1; i <= lh; i++) { Console.WriteLine("{0,10:f4}{1,10:f4}", h[i - 1], nep[i - 1]); } // // Parameter estimates if (wantb != 0) { Console.WriteLine(" "); if (wantb == 1) { Console.WriteLine(" {0}", "Parameter Estimates (Original scalings)"); } else { Console.WriteLine(" {0}", "Parameter Estimates (Standarised)"); } pl = Math.Min(ip, 4); Console.WriteLine(" {0}", " Ridge "); Console.WriteLine(" "); Console.Write(" {0}", " Coeff. "); Console.Write(" {0}", " Intercept "); for (i = 1; i <= pl; i++) { Console.Write(" {0}", i); } Console.WriteLine(" "); if (pl < ip1) { for (i = pl + 1; i <= ip1; i++) { Console.Write(" {0}", i); } Console.WriteLine(" "); } pl = Math.Min(ip + 1, 5); for (i = 1; i <= lh; i++) { Console.Write(" {0, 10:f4}", h[i - 1]); for (j = 1; j <= pl; j++) { Console.Write(" {0, 10:f4}", b[j - 1, i - 1]); } Console.WriteLine(" "); if (pl < ip) { for (j = pl + 1; j <= ip; j++) { Console.Write(" {0, 10:f4}", b[j - 1, i - 1]); } Console.WriteLine(" "); } } } // // Variance inflation factors if (wantvf != 0) { Console.WriteLine(" "); Console.WriteLine(" {0}", "Variance Inflation Factors"); pl = Math.Min(ip, 5); Console.Write(" {0}", " Ridge "); Console.WriteLine(" "); Console.Write(" {0}", " Coeff. "); for (i = 1; i <= pl; i++) { Console.Write(" {0, 10}", i); } Console.WriteLine(" "); if (pl < ip) { for (i = pl + 1; i <= ip; i++) { Console.Write(" {0, 10}", i); } Console.WriteLine(" "); } for (i = 1; i <= lh; i++) { Console.Write(" {0, 10:f4}", h[i - 1]); for (j = 1; j <= pl; j++) { Console.Write(" {0, 10:f4}", vf[j - 1, i - 1]); } Console.WriteLine(" "); if (pl < ip) { for (j = pl + 1; j <= ip; j++) { Console.Write(" {0, 10:f4}", vf[j - 1, i - 1]); } Console.WriteLine(" "); } } } // // Prediction error criterion if (lpec > 0) { Console.WriteLine(" "); Console.WriteLine(" {0}", "Prediction error criterion"); pl = Math.Min(lpec, 5); Console.Write(" {0}", " Ridge "); Console.WriteLine(" "); Console.Write(" {0}", " Coeff. "); for (i = 1; i <= pl; i++) { Console.Write(" {0, 10}", i); } Console.WriteLine(" "); if (pl < lpec) { for (i = pl + 1; i <= lpec; i++) { Console.Write(" {0, 10}", i); } Console.WriteLine(" "); } for (i = 1; i <= lh; i++) { Console.Write(" {0, 10:f4}", h[i - 1]); for (j = 1; j <= pl; j++) { Console.Write(" {0, 10:f4}", pe[j - 1, i - 1]); } Console.WriteLine(" "); if (pl < ip) { for (j = pl + 1; j <= ip; j++) { Console.Write(" {0, 10:f4}", pe[j - 1, i - 1]); } Console.WriteLine(" "); } } Console.WriteLine(" "); Console.WriteLine(" {0}", "Key:"); for (i = 1; i <= lpec; i++) { if (pec[i - 1] == "L") { Console.WriteLine(" {0,5} {1}", i, "Leave one out cross-validation"); } else if (pec[i - 1] == "G") { Console.WriteLine(" {0,5} {1}", i, "Generalised cross-validation"); } else if (pec[i - 1] == "U") { Console.WriteLine(" {0,5} {1}", i, "Unbiased estimate of variance"); } else if (pec[i - 1] == "F") { Console.WriteLine(" {0,5} {1}", i, "Final prediction error"); } else if (pec[i - 1] == "B") { Console.WriteLine(" {0,5} {1}", i, "Bayesian information criterion"); } } } // L160: ; // } catch (Exception e) { Console.WriteLine(e.Message); Console.Write( "Exception Raised"); } } } }