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

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

#define X(I, J) x[(I) *tdx + J]
#define V(I, J) v[(I) *tdv + J]

int main(void)
{
  Integer exit_status = 0, i, ip, j, m, max_iter, n, print_iter, rank;
  Integer *sx = 0;
  Integer tdv, tdx;
  Nag_IncludeMean mean;
  Nag_Link link;
  Nag_Boolean weight;
  char nag_enum_arg[40];
  double df, eps, ex_power, rss, scale, tol;
  double *b = 0, *cov = 0, *offsetptr = 0, *se = 0, *v = 0, *wt = 0;
  double *wtptr, *x = 0, *y = 0;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_glm_normal (g02gac) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%39s", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  link = (Nag_Link) nag_enum_name_to_value(nag_enum_arg);
  scanf("%39s", nag_enum_arg);
  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);
  scanf("%" NAG_IFMT " %" NAG_IFMT " %" NAG_IFMT " %lf", &n, &m, &print_iter,
        &scale);

  if (n >= 2 && m >= 1) {
    if (!(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("%" NAG_IFMT "", &sx[j]);

  /* Calculate ip */
  ip = 0;
  for (j = 0; j < m; j++)
    if (sx[j] > 0)
      ip += 1;
  if (mean == Nag_MeanInclude)
    ip += 1;
  if (link == Nag_Expo)
    scanf("%lf", &ex_power);
  else
    ex_power = 0.0;

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

  tdv = ip + 6;

  /* Set other control parameters */
  max_iter = 10;
  tol = 5e-5;
  eps = 1e-6;

  /* nag_glm_normal (g02gac).
   * Fits a generalized linear model with Normal errors
   */
  nag_glm_normal(link, mean, n, x, tdx, m, sx, ip, y, wtptr, offsetptr,
                 &scale, ex_power, &rss, &df, b, &rank, se, cov, v, tdv, tol,
                 max_iter, print_iter, "", eps, &fail);

  if (fail.code == NE_NOERROR || fail.code == NE_LSQ_ITER_NOT_CONV ||
      fail.code == NE_RANK_CHANGED || fail.code == NE_ZERO_DOF_ERROR) {
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_glm_normal (g02gac).\n%s\n", fail.message);
    }
    printf("\nResidual sum of squares = %13.4e\n", rss);
    printf("Degrees of freedom = %3.1f\n\n", df);
    printf("       Estimate     Standard error\n\n");
    for (i = 0; i < ip; i++)
      printf("%14.4f%14.4f\n", b[i], se[i]);
    printf("\n");
    printf("     y   fitted value  Residual   Leverage\n\n");
    for (i = 0; i < n; ++i) {
      printf("%7.1f%10.2f%12.4f%10.3f\n", y[i], V(i, 1), V(i, 4), V(i, 5));
    }
  }
  else {
    printf("Error from nag_glm_normal (g02gac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

END:
  NAG_FREE(wt);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(sx);
  NAG_FREE(b);
  NAG_FREE(v);
  NAG_FREE(se);
  NAG_FREE(cov);

  return exit_status;
}