/* nag_full_step_regsn (g02efc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 8, 2004.
 */

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

int main(void)
{
  /* Scalars */
  double        fin, fout, rms, rsq, sw, tau;
  Integer       df, exit_status, i, j, m, n, pdx;

  /* Arrays */
  double        *b = 0, *c = 0, *se = 0, *wmean = 0, *x = 0;
  Integer       *isx = 0;

  /* Nag types */
  Nag_OrderType order;
  Nag_SumSquare mean;
  Nag_Comm      comm;
  NagError      fail;

#ifdef NAG_COLUMN_ORDER
#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_full_step_regsn (g02efc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%ld %ld %lf %lf %lf", &n, &m, &fin,
        &fout, &tau);
  scanf("%*[^\n]");

  if (n > 1 && m > 1) {
    /* Allocate memory */
    if (!(b = NAG_ALLOC(m+1, double)) ||
        !(c = NAG_ALLOC((m+1)*(m+2)/2, double)) ||
        !(se = NAG_ALLOC(m+1, double)) ||
        !(wmean = NAG_ALLOC(m+1, double)) ||
        !(x = NAG_ALLOC(n * (m+1), double)) ||
        !(isx = NAG_ALLOC(m, Integer)))
      {
        printf("Allocation failure\n");
        exit_status = -1;
        goto END;
      }
  }
  else {
    printf("Invalid n or m.\n");
    exit_status = 1;
    return exit_status;
  }      

#ifdef NAG_COLUMN_ORDER
  pdx = n;
#else
  pdx = m+1;
#endif

  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= m+1; ++j) {
      scanf("%lf", &X(i, j));
    }
  }
  scanf("%*[^\n]");

  for (j = 1; j <= m; ++j) {
    scanf("%ld", &isx[j-1]);
  }
  scanf("%*[^\n]");

  /* nag_sum_sqs (g02buc).
   * Computes sums of squares and cross-products of deviations
   * from the mean for the augmented matrix
   */
  mean = Nag_AboutMean;
  nag_sum_sqs(order, mean, n, m+1, x, pdx, 0, &sw, wmean, c, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sum_sqs (g02buc).\n%s\n.", fail.message);
    exit_status = 1;
    goto END;
  }

  fflush(stdout);

  /* Perform stepwise selection of variables using
   * nag_full_step_regsn (g02efc):
   *   Stepwise linear regression.
   */
  nag_full_step_regsn(m, n, wmean, c, sw, isx, fin, fout, tau, b, se, &rsq,
                      &rms, &df, nag_full_step_regsn_monfun, &comm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_full_step_regsn (g02efc).\n%s\n.",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Display summary information for fitted model */
  printf("\n");
  printf("Fitted Model Summary\n");
  printf("%-10s   %-10s%19s\n", "Term", " Estimate", "Standard Error");
  printf("%-10s   %11.3e%17.3e\n", "Intercept:", b[0], se[0]);
  for (i = 1; i <= m; ++i) {
    j = isx[i-1];
    if (j == 1 || j == 2) {
      printf("%-10s%3ld%11.3e%17.3e\n", "Variable:", i, b[i], se[i]);
    }
  }
  printf("\n");
  printf("RMS: %-12.3e\n\n", rms);

 END:
  NAG_FREE(b);
  NAG_FREE(c);
  NAG_FREE(se);
  NAG_FREE(wmean);
  NAG_FREE(x);
  NAG_FREE(isx);

  return exit_status;
}