/* nag_step_regsn (g02eec) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */

#include <stdio.h>
#include <string.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg02.h>

int main(void)
{
  /* Scalars */
  double chrss, f, fin, rss;
  Integer exit_status, i, idf, ifr, istep, j, m, maxip, n, nterm, pdq, pdx;
  /* Arrays */
  char nag_enum_arg[40];
  char *newvar = 0;
  double *exss = 0, *p = 0, *q = 0, *wt = 0, *x = 0, *y = 0;
  double *wtptr = 0;
  Integer *sx = 0;
  char **free_vars = 0, **model = 0;
  const char *vname[] = { "DAY", "BOD", "TKN", "TS", "TVS", "COD" };
  /* NAG Types */
  Nag_OrderType order;
  Nag_IncludeMean mean;
  Nag_Boolean addvar = Nag_FALSE, weight;
  NagError fail;

#ifdef NAG_COLUMN_MAJOR
#define X(I, J) x[(J-1)*pdx + I - 1]
  order = Nag_ColMajor;
#else
#define X(I, J) x[(I-1)*pdx + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  exit_status = 0;
  printf("nag_step_regsn (g02eec) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%" NAG_IFMT "", &n, &m);
  scanf(" %39s", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  mean = (Nag_IncludeMean) nag_enum_name_to_value(nag_enum_arg);
  scanf(" %39s", nag_enum_arg);
  weight = (Nag_Boolean) nag_enum_name_to_value(nag_enum_arg);
  maxip = m;

  /* Allocate memory */
  if (!(exss = NAG_ALLOC(maxip, double)) ||
      !(p = NAG_ALLOC(maxip + 1, double)) ||
      !(q = NAG_ALLOC(n * (maxip + 2), double)) ||
      !(wt = NAG_ALLOC(n, double)) ||
      !(x = NAG_ALLOC(n * m, double)) ||
      !(y = NAG_ALLOC(n, double)) ||
      !(sx = NAG_ALLOC(m, Integer)) ||
      !(free_vars = NAG_ALLOC(maxip, char *)) ||
      !(model = NAG_ALLOC(maxip, char *))
         )
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

#ifdef NAG_COLUMN_MAJOR
  pdx = n;
  pdq = n;
#else
  pdx = m;
  pdq = maxip + 2;
#endif

  if (weight) {
    for (i = 1; i <= n; ++i) {
      for (j = 1; j <= m; ++j)
        scanf("%lf", &X(i, j));
      scanf("%lf%lf%*[^\n]", &y[i - 1], &wt[i - 1]);
      wtptr = wt;
    }
  }
  else {
    for (i = 1; i <= n; ++i) {
      for (j = 1; j <= m; ++j)
        scanf("%lf", &X(i, j));
      scanf("%lf%*[^\n] ", &y[i - 1]);
    }
  }
  for (j = 0; j < m; ++j)
    scanf("%" NAG_IFMT "", &sx[j]);
  scanf("%*[^\n]");
  scanf("%lf%*[^\n]", &fin);
  printf("\n");

  istep = 0;
  for (i = 1; i <= m; ++i) {
    /* nag_step_regsn (g02eec).
     * Fits a linear regression model by forward selection
     */
    nag_step_regsn(order, &istep, mean, n, m, x, pdx, vname, sx, maxip, y,
                   wtptr, fin, &addvar, (const char **) &newvar, &chrss, &f,
                   (const char **) model, &nterm, &rss, &idf, &ifr,
                   (const char **) free_vars, exss, q, pdq, p, &fail);

    if (fail.code != NE_NOERROR) {
      printf("Error from nag_step_regsn (g02eec).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

    printf("Step %" NAG_IFMT "\n", istep);
    if (!addvar) {
      printf("No further variables added maximum F =%7.2f\n", f);
      printf("Free variables:        ");

      for (j = 1; j <= ifr; ++j)
        printf("%3.3s %s", free_vars[j - 1], j % 6 == 0
               || j == ifr ? "\n" : " ");

      printf("\nChange in residual sums of squares for free variables:\n");

      printf("                 ");
      for (j = 1; j <= ifr; ++j) {
        printf("%9.4f", exss[j - 1]);
        printf("%s", j % 6 == 0 || j == ifr ? "\n" : " ");
      }
      goto END;
    }
    else {
      printf("Added variable is %3s\n", newvar);
      printf("Change in residual sum of squares =%13.4e\n", chrss);
      printf("F Statistic = %7.2f\n\n", f);
      printf("Variables in model: ");

      for (j = 1; j <= nterm; ++j)
        printf("%3s %s", model[j - 1], j % 6 == 0 || j == nterm ? "\n" : " ");
      printf("Residual sum of squares = %13.4e\n", rss);
      printf("Degrees of freedom = %" NAG_IFMT "\n\n", idf);
      if (ifr == 0) {
        printf("No free variables remaining\n");
        goto END;
      }

      printf("%s%6s", "Free variables:  ", "");
      for (j = 1; j <= ifr; ++j) {
        printf("%3.3s ", free_vars[j - 1]);
        printf(j % 6 == 0 || j == ifr ? "\n" : " ");
      }
      printf("Change in residual sums of squares for free variables:\n");
      printf("                 ");

      for (j = 1; j <= ifr; ++j)
        printf("%9.4f%s", exss[j - 1], j % 6 == 0 || j == ifr ? "\n" : " ");
      printf("\n");
    }
  }

END:
  NAG_FREE(model);
  NAG_FREE(free_vars);
  NAG_FREE(exss);
  NAG_FREE(p);
  NAG_FREE(q);
  NAG_FREE(wt);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(sx);

  return exit_status;
}