/* nag_glm_binomial (g02gbc) 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, *ivar = 0, j, m, max_iter, n, nvar, print_iter;
  Integer rank, tdv, tdx;
  Nag_IncludeMean mean;
  Nag_Link link;
  Nag_Boolean weight;
  char nag_enum_arg[40];
  double dev, df, eps, tol;
  double *beta = 0, *binom = 0, *cov = 0, *offsetptr = 0, *se = 0;
  double *v = 0, *wt = 0, *wtptr, *x = 0, *y = 0;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_glm_binomial (g02gbc) 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 "", &n, &m, &print_iter);

  if (n >= 2 && m >= 1) {
    if (!(binom = NAG_ALLOC(n, double)) ||
        !(wt = NAG_ALLOC(n, double)) ||
        !(x = NAG_ALLOC(n * m, double)) ||
        !(y = NAG_ALLOC(n, double)) || !(ivar = 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%lf", &y[i], &binom[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%lf", &y[i], &binom[i]);
    }
  }
  for (j = 0; j < m; j++)
    scanf("%" NAG_IFMT "", &ivar[j]);

  /* Calculate nvar */
  nvar = 0;
  for (i = 0; i < m; i++)
    if (ivar[i] > 0)
      nvar += 1;
  if (mean == Nag_MeanInclude)
    nvar += 1;

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

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

  /* nag_glm_binomial (g02gbc).
   * Fits a generalized linear model with binomial errors
   */
  nag_glm_binomial(link, mean, n, x, tdx, m, ivar, nvar, y, binom, wtptr,
                   offsetptr, &dev, &df, beta, &rank,
                   se, cov, v, tdv, tol, max_iter, print_iter, "",
                   eps, &fail);

  if (fail.code == NE_NOERROR || fail.code == NE_SVD_NOT_CONV ||
      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_binomial (g02gbc).\n%s\n", fail.message);
    }
    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 < nvar; i++)
      printf("%14.4f%14.4f\n", beta[i], se[i]);
    printf("\n");
    printf("     binom     y   fitted value  Residual  Leverage\n\n");
    for (i = 0; i < n; ++i) {
      printf("%10.1f%7.1f%10.2f%12.4f%10.3f\n", binom[i], y[i],
             V(i, 1), V(i, 4), V(i, 5));
    }
  }
  else {
    printf("Error from nag_glm_binomial (g02gbc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
END:
  NAG_FREE(binom);
  NAG_FREE(wt);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(beta);
  NAG_FREE(v);
  NAG_FREE(se);
  NAG_FREE(cov);
  NAG_FREE(ivar);

  return exit_status;
}