using System; // using System.Collections.Generic; // using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.IO; using System.Runtime.InteropServices; using NagCFunctionsAPI; namespace g02jace { class g02jace { static int Main(string[] args) { int exit_status = 0; int n=0, ncol=0, nfv=0, nrv=0, nvpr=0; int[] levels = null, rvid = null, fvid = null, vpr = null; int svid = 0, cwid = 0, fint = 0, rint = 0; int fl = 0, fnlevel = 0, rnlevel=0,lgamma=0,lb=0,tddat=0; int maxit=0,warnp=0; double tol = 0; int nff = 0,nrf=0,df=0; double like = 0; double[,] dat = null; double[] gamma = null, b = null, se = null; int i=0,j=0,k=0,l=0; int yvid = 0; NagError fail = new NagError(); String line; Regex spaces = new Regex(@"\s+"); FileStream g02jace_data_handle = new FileStream("g02jace.d", FileMode.Open, FileAccess.Read); StreamReader g02jace_data_reader = new StreamReader(g02jace_data_handle); // skip heading in data file line = g02jace_data_reader.ReadLine(); // Read in the problem size information line = g02jace_data_reader.ReadLine(); string[] field = spaces.Split(line.Trim()); n = int.Parse(field[0]); ncol = int.Parse(field[1]); nfv = int.Parse(field[2]); nrv = int.Parse(field[3]); nvpr = int.Parse(field[4]); // Check problem size if (n < 0 || ncol < 0 || nfv < 0 || nrv < 0 || nvpr < 0) { Console.WriteLine("Invalid problem size, at least one of n, ncol, nfv, nrv or nvpr is < 0"); exit_status=1; goto END; } // allocate some memory levels = new int[ncol]; fvid = new int[nfv]; rvid = new int[nrv]; vpr = new int[nrv]; // read the number of levels for each variable line = g02jace_data_reader.ReadLine(); field=spaces.Split(line.Trim()); for (i = 1; i <= ncol; ++i){ levels[i - 1] = int.Parse(field[i - 1]);} // read in model information line = g02jace_data_reader.ReadLine(); field = spaces.Split(line.Trim()); yvid = int.Parse(field[0]); for (i = 1; i <= nfv; ++i) { fvid[i - 1] = int.Parse(field[i]); } for(i=1; i<=nrv; ++i) { rvid[i-1] = int.Parse(field[nfv+i]); } svid = int.Parse(field[nfv + nrv + 1]); cwid = int.Parse(field[nfv + nrv + 2]); fint = int.Parse(field[nfv + nrv + 3]); rint = int.Parse(field[nfv + nrv + 4]); // read in the variance component flag line=g02jace_data_reader.ReadLine(); field=spaces.Split(line.Trim()); for (i = 1; i <= nrv; ++i){vpr[i - 1] = int.Parse(field[i - 1]);} // if no subject specified, ignore rint if (svid == 0) { rint = 0; } // count the number of levels in the fixed parameters for (i = 1, fnlevel = 0; i <= nfv; ++i) { fl = levels[fvid[i - 1] - 1] - 1; fnlevel += (fl < 1) ? 1 : fl; } if (fint == 1) { fnlevel++; } // count the number of levels in the random parameters for (i = 1, rnlevel = 0; i <= nrv; ++i) { rnlevel += levels[rvid[i - 1] - 1]; } if (rint!=0) { rnlevel++; } // calculate the sizes of the output arrays if (rint == 1) { lgamma = nvpr + 2; } else { lgamma = nvpr + 1; } if (svid!=0) { lb = fnlevel + levels[svid - 1] * rnlevel; } else { lb = fnlevel + rnlevel; } tddat = ncol; // allocate remaining memory dat = new double[n,ncol]; gamma = new double[lgamma]; b = new double[lb]; se = new double[lb]; // read the data matrix for (i = 0; i < n; ++i) { line = g02jace_data_reader.ReadLine(); field = spaces.Split(line.Trim()); for (j = 0; j < ncol; ++j) dat[i, j] = double.Parse(field[j]); } // read the initial values for gamma line = g02jace_data_reader.ReadLine(); field = spaces.Split(line.Trim()); for (i = 1; i < lgamma; ++i) { gamma[i - 1] = double.Parse(field[i - 1]); } // read the maximum number of iterations line = g02jace_data_reader.ReadLine(); field = spaces.Split(line.Trim()); maxit = int.Parse(field[0]); g02jace_data_reader.Close(); g02jace_data_handle.Close(); // Run the analysis NagFunctions.g02jac(n, ncol, dat, tddat, levels, yvid, cwid, nfv, fvid, fint, nrv, rvid, nvpr, vpr, rint, svid, gamma, ref nff, ref nrf, ref df, ref like, lb, b, se, maxit, tol, ref warnp, ref fail); if (fail.code == 0) { if (warnp != 0) { Console.WriteLine("Warning: At least one variance component was " + "estimated to be negative and then reset to zero"); } Console.WriteLine("Fixed effects (Estimate and Standard Deviation)"); Console.WriteLine(""); k = 1; if (fint == 1) { Console.WriteLine("Intercept " + b[k-1].ToString(" 0000.0000") + se[k - 1].ToString(" 0000.0000")); ++k; } for (i = 1; i <= nfv; ++i) { for (j = 1; j <= levels[fvid[i - 1] - 1]; ++j) { if (levels[fvid[i - 1] - 1] != 1 && j == 1) continue; Console.WriteLine("Variable " + i.ToString() + " Level " + j.ToString("#### ") + b[k - 1].ToString("#####.0000 ") + se[k - 1].ToString("#####.0000")); ++k; } } Console.WriteLine(""); Console.WriteLine("Random Effects (Estimate and Standard Deviation)"); if (svid == 0) { for (i = 1; i <= nrv; ++i) { for (j = 1; j <= levels[rvid[i - 1] - 1]; ++j) { Console.WriteLine("Variable " + i.ToString() + " Level " + j.ToString() + b[k-1].ToString("####.0000 ") + se[k-1].ToString("####.0000 ") ); ++k; } } } else { for (l = 1; l <= levels[svid - 1]; ++l) { if (rint == 1) { Console.WriteLine("Intercept for Subject Level " + l.ToString("## ") + " " +b[k-1].ToString("####.0000 ") + se[k-1].ToString("####.0000")); // "Intercept for Subject Level", l, " ", // b[k - 1], se[k - 1]); ++k; } for (i = 1; i <= nrv; ++i) { for (j = 1; j <= levels[rvid[i - 1] - 1]; ++j) { Console.WriteLine("Subject Level " + l.ToString() + " Variable " + i.ToString("## ") + " Level " + j.ToString("## ") + b[k - 1].ToString("####.0000 ") + se[k - 1].ToString("####.0000")); ++k; } } } } Console.WriteLine(""); Console.WriteLine("Variance Components"); for (i = 1; i <= nvpr + rint; ++i) { Console.WriteLine(i.ToString("#### ") + gamma[i - 1].ToString("#####.####")); } Console.WriteLine("SIGMA^2 = " + gamma[nvpr + rint].ToString("#####.####")); Console.WriteLine("-2LOG LIKE = " + like.ToString("#####.####")); Console.WriteLine("DF = " + df.ToString()); } else { Console.WriteLine("Routine nag_reml_mixed_regsn (g02jac) failed, with error " + "message:"); Console.WriteLine(fail.char_array); exit_status = fail.code; } END: return exit_status; } } }