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

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

int main(void)
{
  Nag_Boolean svd;
  Integer exit_status = 0, i, *ifact = 0, ip, irank, *isx = 0, j,
         levels, m, n;
  Integer tdq, tdx;
  char nag_enum_arg[40];
  double *b = 0, *com_ar = 0, *cov = 0, df, *h = 0, *p = 0, *q = 0;
  double *rep = 0, *res = 0, tol, *v = 0, *wt = 0, *wtptr = 0, *x = 0;
  double *y = 0, rss, *se = 0;
  Nag_DummyType dum_type;
  Nag_IncludeMean mean;
  Nag_Boolean weight;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_dummy_vars (g04eac) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT " %" NAG_IFMT "", &n, &levels);
  scanf(" %39s", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  dum_type = (Nag_DummyType) nag_enum_name_to_value(nag_enum_arg);
  scanf(" %39s", nag_enum_arg);
  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 (dum_type == Nag_AllLevels)
    tdx = levels;
  else
    tdx = levels - 1;

  if (!(x = NAG_ALLOC(n * tdx, double))
      || !(rep = NAG_ALLOC(levels, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  if (dum_type == Nag_Poly) {
    if (!(v = NAG_ALLOC(levels, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  }
  else {
    if (!(v = NAG_ALLOC(1, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  }
  if (!(wt = NAG_ALLOC(n, double))
      || !(y = NAG_ALLOC(n, double))
      || !(ifact = NAG_ALLOC(n, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  printf("\n");
  if (weight) {
    for (i = 1; i <= n; ++i)
      scanf("%" NAG_IFMT " %lf %lf", &ifact[i - 1], &y[i - 1], &wt[i - 1]);
    wtptr = wt;
  }
  else {
    for (i = 1; i <= n; ++i)
      scanf("%" NAG_IFMT " %lf", &ifact[i - 1], &y[i - 1]);
    wtptr = 0;
  }
  if (dum_type == Nag_Poly)
    for (j = 1; j <= levels; ++j)
      scanf("%lf", &v[j - 1]);

  /* Calculate dummy variables */
  /* nag_dummy_vars (g04eac).
   * Computes orthogonal polynomials or dummy variables for
   * factor/classification variable
   */
  nag_dummy_vars(dum_type, n, levels, ifact, x, tdx, v, rep, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dummy_vars (g04eac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

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

  for (j = 1; j <= m; ++j)
    isx[j - 1] = 1;

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

  if (svd)
    printf(" %s%4" NAG_IFMT "\n\n", "Model not of full rank, rank = ", irank);

  printf(" Residual sum of squares =  %13.4e\n", rss);
  printf(" Degrees of freedom = %4.0f\n\n", df);
  printf("Variable       Parameter estimate    Standard error\n\n");
  for (j = 1; j <= ip; ++j)
    printf(" %6" NAG_IFMT " %20.4e %20.4e\n", j, b[j - 1], se[j - 1]);

END:
  NAG_FREE(x);
  NAG_FREE(rep);
  NAG_FREE(v);
  NAG_FREE(v);
  NAG_FREE(wt);
  NAG_FREE(y);
  NAG_FREE(ifact);
  NAG_FREE(b);
  NAG_FREE(se);
  NAG_FREE(cov);
  NAG_FREE(p);
  NAG_FREE(com_ar);
  NAG_FREE(h);
  NAG_FREE(res);
  NAG_FREE(q);
  NAG_FREE(isx);

  return exit_status;
}