// g02gp Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; using System.IO; namespace NagDotNetExamples { public class G02GPE { static string datafile = "ExampleData/g02gpe.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 a, eps, rss, s, tol; int i, idf, ip, iprint, irank, j, m, maxit, n; bool vfobs = false; string error = "", link = "", mean = "", offset = "", weight = ""; int ifail; Console.WriteLine("g02gp Example Program Results"); // // Skip headings in data file a = 0.0; sr.Reset(); sr.Reset(); // Read in training data for model that will be used for prediction sr.Reset(); link = sr.Next(); mean = sr.Next(); offset = sr.Next(); weight = sr.Next(); n = int.Parse(sr.Next()); m = int.Parse(sr.Next()); s = double.Parse(sr.Next(), CultureInfo.InvariantCulture); iprint = int.Parse(sr.Next()); double[] eta = new double[n]; double[] fwt = new double[n]; double[] off = new double[n]; double[] pred = new double[n]; double[] seeta = new double[n]; double[] sepred = new double[n]; double[] t = new double[1]; double[] wt = new double[n]; double[,] x = new double[n, m]; double[] y = new double[n]; int[] isx = new int[m]; if (n < 2 || m < 1) { Console.Write(" ** Problem size is too small."); goto L200; } if ((weight == "W") || (weight == "w")) { 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); fwt[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } else { 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); } } sr.Reset(); for (j = 1; j <= m; j++) { isx[j - 1] = int.Parse(sr.Next()); } // Calculate ip ip = 0; for (j = 1; j <= m; j++) { if (isx[j - 1] > 0) { ip = ip + 1; } } if ((mean == "M") || (mean == "m")) { ip = ip + 1; } double[] b = new double[ip]; double[] cov = new double[(ip*ip+ip)/2]; double[,] v = new double[n, 7 + ip]; double[] se = new double[ip]; if ((link == "E") || (link == "e")) { sr.Reset(); a = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } // Set control parameters eps = 0.0000010e0; tol = 0.000050e0; maxit = 10; // Call method to fit model to training data G02.g02ga(link, mean, offset, weight, n, x, m, isx, ip, y, fwt, ref s, a, out rss, out idf, b, out irank, se, cov, v, tol, maxit, iprint, eps, out ifail); if (ifail != 0) { Console.WriteLine("** g02ga failed with ifail = {0,5}", ifail); if (ifail < 6) { goto L200; } } // Display parameter estimates for training data Console.WriteLine(" "); Console.WriteLine(" Residual sum of squares = {0,12:e4} Degrees of freedom = {1,2}", rss, idf); Console.WriteLine(" "); Console.Write(" {0}", " Estimate Standard error"); Console.WriteLine(" "); for (i = 1; i <= ip; i++) { Console.WriteLine(" {0,14:f4}{1,14:f4}", b[i - 1], se[i - 1]); } // // Skip second lot of headings in data file sr.Reset(); // Read in data to predict from and check array sizes sr.Reset(); n = int.Parse(sr.Next()); vfobs = bool.Parse(sr.Next()); offset = sr.Next(); weight = sr.Next(); if (n < 1) { Console.Write(" {0}", "n < 1"); goto L200; } if ((offset == "Y") && (weight == "W")) { 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); } off[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); wt[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } else if (offset == "Y") { 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); } off[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } else if (weight == "W") { 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); } wt[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } else { 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); } } } // Using g02ga to fit training model, so error structure is normal error = "N"; // Call prediction method G02.g02gp(error, link, mean, offset, weight, n, x, m, isx, ip, t, off, wt, s, a, b, cov, vfobs, eta, seeta, pred, sepred, out ifail); // errfn, link, mean, offset, weight, n, x, m, isx, ip, t, off, wt, s, a, b, cov, vfobs, eta, seeta, pred, sepred, out ifail) if (ifail != 0) { Console.WriteLine("** g02gp failed with ifail = {0,5}", ifail); if (ifail != 22) { goto L200; } } // Display predicted values Console.WriteLine(" "); Console.Write(" {0} {1}", " I ETA SE(ETA) Predicted ", " SE(Predicted)"); Console.WriteLine(" "); for (i = 1; i <= n; i++) { Console.WriteLine(" {0,3}){1,10:f5} {2,10:f5} {3,10:f5} {4,10:f5} ", i, eta[i - 1], seeta[i - 1], pred[i - 1], sepred[i - 1]); } // L200: ; // } catch (Exception e) { Console.WriteLine(e.Message); Console.Write( "Exception Raised"); } } } }