/* nag_glm_tran_model (g02gkc) 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]
#define C(I, J) c[(I) *tdc + J]
int main(void)
{
  Integer exit_status = 0, i, ip, j, m, max_iter, n, nclin, print_iter, rank;
  Integer *sx = 0, tdc, tdv, tdx;
  NagError fail;
  double dev, df, eps, ex_power;
  double *b = 0, *c = 0, *cov = 0, *se = 0, tol, *v = 0, *wtptr;
  double *x = 0, *y = 0;

  INIT_FAIL(fail);

  printf("nag_glm_tran_model (g02gkc) Example Program Results\n\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)) ||
      !(cov = NAG_ALLOC(ip * (ip + 1) / 2, double)) ||
      !(se = NAG_ALLOC(ip, double)) || !(v = NAG_ALLOC(n * (ip + 6), 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);
    /* Input constraints */
    nclin = ip - rank;

    if (!(c = NAG_ALLOC(ip * nclin, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    tdc = nclin;

    for (i = 0; i < ip; ++i)
      for (j = 0; j < nclin; ++j)
        scanf("%lf", &C(i, j));

    /* nag_glm_tran_model (g02gkc).
     * Estimates and standard errors of parameters of a general
     * linear model for given constraints
     */
    nag_glm_tran_model(ip, nclin, v, tdv, c, tdc, b, 1.0e0, se, cov, &fail);

    if (fail.code == NE_NOERROR) {
      printf("       Estimate     Standard error\n\n");
      for (i = 0; i < ip; i++)
        printf(" %14.4f%14.4f\n", b[i], se[i]);
      printf("\n");
    }
    else {
      printf("Error from nag_glm_tran_model (g02gkc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
  }
  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(c);
  NAG_FREE(b);
  NAG_FREE(cov);
  NAG_FREE(se);
  NAG_FREE(v);
  return exit_status;
}