/* nag_regsn_mult_linear (g02dac) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 5 revised, 1998.
 * Mark 6 revised, 2000.
 * Mark 8 revised, 2004.
 */

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

static int ex1(void);
static int ex2(void);

int main(void)
{
  Integer  exit_status_ex1 = 0;
  Integer  exit_status_ex2 = 0;

  printf("nag_regsn_mult_linear (g02dac) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  exit_status_ex1 = ex1();
  exit_status_ex2 = ex2();

  return (exit_status_ex1 == 0 && exit_status_ex2 == 0) ? 0 : 1;
}

#define X(I, J) x[(I) *tdx + J]
#define Q(I, J) q[(I) *tdq + J]

static int ex1(void)
{
  Integer         exit_status = 0, i, ip, j, m, n, rank, *sx = 0, tdq, tdx;
  char            nag_enum_arg[40];
  double          *b = 0, *com_ar = 0, *cov = 0, df, *h = 0, *p = 0, *q = 0;
  double          *res = 0, rss, *se = 0, tol, *wt = 0, *wtptr, *x = 0, *y = 0;
  Nag_Boolean     svd, weight;
  Nag_IncludeMean mean;
  NagError        fail;

  INIT_FAIL(fail);

  printf("Example 1\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%ld %ld", &n, &m);
  scanf(" %39s", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  weight = (Nag_Boolean) nag_enum_name_to_value(nag_enum_arg);
  scanf(" %39s", nag_enum_arg);
  mean = (Nag_IncludeMean) nag_enum_name_to_value(nag_enum_arg);
  if (n >= 2 && m >= 1)
    {
      if (!(h = NAG_ALLOC(n, double)) ||
          !(res = NAG_ALLOC(n, double)) ||
          !(wt = NAG_ALLOC(n, double)) ||
          !(x = NAG_ALLOC(n*m, double)) ||
          !(y = NAG_ALLOC(n, double)) ||
          !(sx = NAG_ALLOC(m, Integer)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }
      tdx = m;
    }
  else
    {
      printf("Invalid n or m.\n");
      exit_status = 1;
      return exit_status;
    }
  if (weight)
    {
      wtptr = wt;
      for (i = 0; i < n; i++)
        {
          for (j = 0; j < m; j++)
            scanf("%lf", &X(i, j));
          scanf("%lf%lf", &y[i], &wt[i]);
        }
    }
  else
    {
      wtptr = (double *) 0;
      for (i = 0; i < n; i++)
        {
          for (j = 0; j < m; j++)
            scanf("%lf", &X(i, j));
          scanf("%lf", &y[i]);
        }
    }
  for (j = 0; j < m; j++)
    scanf("%ld", &sx[j]);
  /* Calculate ip */
  ip = 0;
  if (mean == Nag_MeanInclude)
    ip += 1;
  for (i = 0; i < m; i++)
    if (sx[i] > 0) ip += 1;

  if (!(b = NAG_ALLOC(ip, double)) ||
      !(cov = NAG_ALLOC((ip*ip+ip)/2, double)) ||
      !(p = NAG_ALLOC(ip*(ip+2), double)) ||
      !(q = NAG_ALLOC(n*(ip+1), double)) ||
      !(com_ar = NAG_ALLOC(ip*ip+5*(ip-1), double)) ||
      !(se = NAG_ALLOC(ip, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  tdq = ip+1;

  /* Set tolerance */
  tol = 0.00001e0;
  /* nag_regsn_mult_linear (g02dac).
   * Fits a general (multiple) linear regression model
   */
  nag_regsn_mult_linear(mean, n, x, tdx, m, sx, ip, y,
                        wtptr, &rss, &df, b, se, cov, res, h, q,
                        tdq, &svd, &rank, p, tol, com_ar, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_regsn_mult_linear (g02dac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  if (svd)
    printf("Model not of full rank, rank = %4ld\n\n", rank);
  printf("Residual sum of squares = %13.4e\n", rss);
  printf("Degrees of freedom = %3.1f\n\n", df);
  printf("Variable    Parameter estimate   Standard error\n\n");
  for (j = 0; j < ip; j++)
    printf("%6ld%20.4e%20.4e\n", j+1, b[j], se[j]);
  printf("\n");
  printf("    Obs         Residuals              h\n\n");
  for (i = 0; i < n; i++)
    printf("%6ld%20.4e%20.4e\n", i+1, res[i], h[i]);

 END:
  NAG_FREE(h);
  NAG_FREE(res);
  NAG_FREE(wt);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(sx);
  NAG_FREE(b);
  NAG_FREE(cov);
  NAG_FREE(p);
  NAG_FREE(q);
  NAG_FREE(com_ar);
  NAG_FREE(se);

  return exit_status;
}
#undef x
#undef q

#define X(I, J) x[(I) *tdx + J]
#define Q(I, J) q[(I) *tdq + J]
static int ex2(void)
{
  Integer         exit_status = 0;
  double          rss, tol;
  Integer         i, ip, rank, j, m, mmax, n, degree, digits, tdx, tdq;
  double          df;
  Nag_Boolean     svd;
  Nag_IncludeMean mean;
  double          *h = 0, *res = 0, *wt = 0, *x = 0, *y = 0;
  double          *b = 0, *cov = 0, *p = 0, *q = 0, *com_ar = 0, *se = 0;
  double          *wtptr = (double *) 0; /* don't use weights */
  Integer         *sx = 0;
  NagError        fail;

  INIT_FAIL(fail);

  printf(
          "\n\n\nExample 2\n");
  /* Skip heading in data file */
  scanf(" %*[^\n]");

  /* Use mean = Nag_MeanInclude */

  mean = Nag_MeanInclude;
  scanf("%ld%ld%ld", &degree, &n, &digits);
  mmax = degree+1;
  if (n >= 1)
    {
      if (!(h = NAG_ALLOC(n, double)) ||
          !(res = NAG_ALLOC(n, double)) ||
          !(wt = NAG_ALLOC(n, double)) ||
          !(x = NAG_ALLOC(n*mmax, double)) ||
          !(y = NAG_ALLOC(n, double)) ||
          !(sx = NAG_ALLOC(mmax, Integer)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }
      tdx = mmax;
    }
  else
    {
      printf("Invalid n.\n");
      exit_status = 1;
      return exit_status;
    }

  /* Set tolerance */
  tol = pow(10.0, -(double) digits);
  m = degree;
  ip = degree + 1;
  if (!(b = NAG_ALLOC(ip, double)) ||
      !(cov = NAG_ALLOC((ip*ip+ip)/2, double)) ||
      !(p = NAG_ALLOC(ip*(ip+2), double)) ||
      !(q = NAG_ALLOC(n*(ip+1), double)) ||
      !(com_ar = NAG_ALLOC(ip*ip+5*(ip-1), double)) ||
      !(se = NAG_ALLOC(ip, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  tdq = ip+1;

  for (i = 0; i < ip-1; ++i)
    sx[i] = 1;

  for (i = 0; i < n; i++)
    {
      scanf("%lf%lf", &X(i, degree-1), &y[i]);
      for (j = 0; j < degree; ++j)
        X(i, j) = pow(X(i, degree-1), (double)(degree-j));
    }

  /* nag_regsn_mult_linear (g02dac), see above. */
  nag_regsn_mult_linear(mean, n, x, tdx, m, sx, ip, y,
                        wtptr, &rss, &df, b, se, cov, res, h, q,
                        tdq, &svd, &rank, p, tol, com_ar, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_regsn_mult_linear (g02dac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  printf("Regression estimates  (mean = Nag_MeanInclude) \n\n");
  printf("Coefficient    Estimate         Standard error\n\n");
  for (j = 1; j < ip; j++)
    printf("a(%ld)%20.4e%20.4e\n", degree+1-j, b[j], se[j]);
  printf("a(0)%20.4e%20.4e\n", b[0], se[0]);
  printf("\n\n");

  /* Use mean = Nag_MeanZero */

  mean = Nag_MeanZero;

  m = degree + 1;
  for (i = 0; i < ip; ++i)
    sx[i] = 1;

  for (i = 0; i < n; i++)
    X(i, m-1) = 1.0;

  /* nag_regsn_mult_linear (g02dac), see above. */
  nag_regsn_mult_linear(mean, n, x, tdx, m, sx, ip, y,
                        wtptr, &rss, &df, b, se, cov, res, h, q,
                        tdq, &svd, &rank, p, tol, com_ar, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_regsn_mult_linear (g02dac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  printf("Regression estimates  (mean = Nag_MeanZero) \n\n");
  printf("Coefficient    Estimate         Standard error\n\n");
  for (j = 0; j < ip; j++)
    printf("a(%ld)%20.4e%20.4e\n", degree-j, b[j], se[j]);
  printf("\n\n");

 END:
  NAG_FREE(h);
  NAG_FREE(res);
  NAG_FREE(wt);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(sx);
  NAG_FREE(b);
  NAG_FREE(cov);
  NAG_FREE(p);
  NAG_FREE(q);
  NAG_FREE(com_ar);
  NAG_FREE(se);

  return exit_status;
}