/* nag_regsn_mult_linear_est_func (g02dnc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 2, 1991.
 * Mark 8 revised, 2004.
 */

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

#define X(I, J) x[(I) *tdx + J]
#define Q(I, J) q[(I) *tdq + J]
int main(void)
{
  Integer         exit_status = 0, i, ip, j, m, n, nestern, rank, *sx = 0, tdq,
                  tdx;
  double          *b = 0, *com_ar = 0, *cov = 0, df, *f = 0, *h = 0, *p = 0;
  double          *q = 0, *res = 0, rss, *se = 0, sestat, stat, t, tol;
  double          *wt = 0, *wtptr, *x = 0, *y = 0;
  char            nag_enum_arg[40];
  Nag_Boolean     est, svd, weight;
  Nag_IncludeMean mean;
  NagError        fail;

  INIT_FAIL(fail);

  printf(
          "nag_regsn_mult_linear_est_func (g02dnc) Example Program Results\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]);
  scanf("%ld", &ip);

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


  /* Set tolerance */
  tol = 0.00001e0;

  /*
   *    Find initial estimates using g02dac
   */
  /* 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;
    }

  printf("\n");
  printf("Estimates from g02dac\n\n");
  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");
  scanf("%ld", &nestern);
  for (i = 1; i <= nestern; ++i)
    {
      for (j = 0; j < ip; ++j)
        scanf("%lf", &f[j]);

      /* nag_regsn_mult_linear_est_func (g02dnc).
       * Estimate of an estimable function for a general linear
       * regression model
       */
      nag_regsn_mult_linear_est_func(ip, rank, b, cov, p, f, &est, &stat,
                                     &sestat, &t, tol, &fail);

      if (fail.code == NE_NOERROR || fail.code == NE_RANK_EQ_IP)
        {
          printf("\n");
          printf("Function %ld\n\n", i);
          for (j = 0; j < ip; ++j)
            printf("%8.2f%c", f[j], (j%5 == 4 || j == ip-1)?'\n':' ');
          printf("\n");
          if (est)
            printf(" stat = %10.4f  se = %10.4f   t = %10.4f\n",
                    stat, sestat, t);
          else
            printf("Function not estimable\n");
        }
      else
        {
          printf(
                  "Error from nag_regsn_mult_linear_est_func (g02dnc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }
    }
 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(f);
  NAG_FREE(p);
  NAG_FREE(q);
  NAG_FREE(se);
  NAG_FREE(com_ar);
  return exit_status;
}