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

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

#define X(I, J) x[(I) *tdx + J]
int main(void)
{
  Nag_Boolean est;
  Integer exit_status = 0, i, ip, j, m, max_iter, n, nestfn, print_iter, rank;
  Integer *sx = 0, tdv, tdx;
  NagError fail;
  double dev, df, eps, ex_power, sestat, stat, tol, z;
  double *b = 0, *cov = 0, *f = 0, *se = 0, *v = 0, *wtptr, *x = 0;
  double *y = 0;

  INIT_FAIL(fail);

  printf("nag_glm_est_func (g02gnc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT " %" NAG_IFMT " %" NAG_IFMT "", &n, &m, &print_iter);

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

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

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

  /* Fit Log-linear model using nag_glm_poisson (g02gcc) */

  /* nag_glm_poisson (g02gcc).
   * Fits a generalized linear model with Poisson errors
   */
  nag_glm_poisson(Nag_Log, Nag_MeanInclude, n, x, tdx,
                  m, sx, ip, y, wtptr, (double *) 0, ex_power, &dev, &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) {
    printf("\nDeviance = %13.4e\n", dev);
    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");
    scanf("%" NAG_IFMT "", &nestfn);
    for (i = 1; i <= nestfn; ++i) {
      for (j = 0; j < ip; ++j)
        scanf("%lf", &f[j]);

      /* nag_glm_est_func (g02gnc).
       * Estimable function and the standard error of a
       * generalized linear model
       */
      nag_glm_est_func(ip, rank, b, cov, v,
                       tdv, f, &est, &stat, &sestat, &z, tol, &fail);

      if (fail.code != NE_NOERROR && fail.code != NE_RANK_EQ_IP) {
        printf("Error from nag_glm_est_func (g02gnc).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }

      printf("\n");
      printf("Function %" NAG_IFMT "\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  sestat = %10.4f   z = %10.4f\n",
               stat, sestat, z);
      else
        printf("Function not estimable\n");
    }
  }
  else {
    printf("Error from nag_glm_poisson (g02gcc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
END:
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(sx);
  NAG_FREE(b);
  NAG_FREE(f);
  NAG_FREE(v);
  NAG_FREE(cov);
  NAG_FREE(se);
  return exit_status;
}