// g02ka Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class G02KAE { static string datafile = "ExampleData/g02kae.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 h, nep, rss, tau, tol; int df, i, ip, j, m, n, niter, opt, optloo, orig; int ifail; Console.WriteLine("g02ka Example Program Results"); // // Skip heading in data file sr.Reset(); // Read in data and check array limits sr.Reset(); n = int.Parse(sr.Next()); m = int.Parse(sr.Next()); h = double.Parse(sr.Next(), CultureInfo.InvariantCulture); opt = int.Parse(sr.Next()); tol = double.Parse(sr.Next(), CultureInfo.InvariantCulture); niter = int.Parse(sr.Next()); orig = int.Parse(sr.Next()); optloo = int.Parse(sr.Next()); double[] perr = new double[5]; double[] res = new double[n]; double[,] x = new double[n, m]; double[] y = new double[n]; int[] isx = new int[m]; if (n < 1 || m > n) { Console.WriteLine(" ** Problem size is too small."); goto L40; } sr.Reset(); for (i = 1; i <= n; i++) { 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); } sr.Reset(); for (j = 1; j <= m; j++) { isx[j - 1] = int.Parse(sr.Next()); } // // Total number of variables. ip = 0; for (j = 1; j <= m; j++) { if (isx[j - 1] == 1) { ip = ip + 1; } } // // Tolerance for setting singular values of H to zero. tau = 0.00e0; df = 0; int blength = ip + 1; double[] b = new double[blength]; double[] vif = new double[ip]; // // Call function. G02.g02ka(n, m, x, isx, ip, tau, y, ref h, opt, ref niter, tol, out nep, orig, b, vif, res, out rss, out df, optloo, perr, out ifail); if (ifail != 0) { Console.WriteLine("** g02ka failed with ifail = {0,5}", ifail); if (ifail != -1) { goto L40; } } // Print results: Console.WriteLine(""); Console.WriteLine("{0} {1,10:f4}", "Value of ridge parameter:", h); Console.WriteLine(""); Console.WriteLine("{0} {1,10:e4}", "Sum of squares of residuals:", rss); Console.WriteLine("{0} {1,5}", "Degrees of freedom: ", df); Console.WriteLine("{0} {1,10:f4}", "Number of effective parameters:", nep); Console.WriteLine(""); printArrays(blength,1,b,"Parameter estimates" ); Console.WriteLine(""); Console.WriteLine(" {0} {1}", "Number of iterations:", niter); Console.WriteLine(""); if (opt == 1) { Console.WriteLine(" {0}", "Ridge parameter minimises GCV"); } else if (opt == 2) { Console.WriteLine(" {0}", "Ridge parameter minimises UEV"); } else if (opt == 3) { Console.WriteLine(" {0}", "Ridge parameter minimises FPE"); } else if (opt == 4) { Console.WriteLine(" {0}", "Ridge parameter minimises BIC"); } Console.WriteLine(""); Console.WriteLine(" {0}", "Estimated prediction errors:"); Console.WriteLine("{0} {1,10:f4}", "GCV =", perr[0]); Console.WriteLine("{0} {1,10:f4}", "UEV =", perr[1]); Console.WriteLine("{0} {1,10:f4}", "FPE =", perr[2]); Console.WriteLine("{0} {1,10:f4}", "BIC =", perr[3]); if (optloo == 2) { Console.WriteLine("{0} {1,10:f4}", "LOO CV =", perr[4]); } Console.WriteLine(""); printArrays(n, 1, res, "Residuals" ); Console.WriteLine(""); // printArrays(ip, 1, vif, "Variance inflation factors" ); L40: ; // } catch (Exception e) { Console.WriteLine(e.Message); Console.Write("Exception Raised"); } } internal static void printArrays(int m, int n, double[] array ,string title) { Console.WriteLine(title); Console.WriteLine("{0}\t{1,8}", " ", 1); for (int bindex = 0; bindex < m; bindex++) { Console.WriteLine("{0}\t{1,8:f4}", bindex + 1, array[bindex]); } } } }