/* nag_condl_logistic (g11cac) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 7, 2002.
 */

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

int main(void)
{
  /* Scalars */
  double        dev, tol;
  Integer       iprint, exit_status, i, p, j, m, maxit, n, ns, pdz;
  NagError      fail;
  Nag_OrderType order;

  /* Arrays */
  double        *b = 0, *cov = 0, *sc = 0, *se = 0, *z = 0;
  Integer       *ic = 0, *isi = 0, *isz = 0, *nca = 0, *nct = 0;

#ifdef NAG_COLUMN_MAJOR
#define Z(I, J) z[(J-1)*pdz + I - 1]
  order = Nag_ColMajor;
#else
#define Z(I, J) z[(I-1)*pdz + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  exit_status = 0;

  printf("nag_condl_logistic (g11cac) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%ld%ld%ld%ld%*[^\n] ", &n, &m,
         &ns, &maxit);

  pdz = n;
  iprint = 0;
  tol = 1e-5;

  /* Allocate arrays z, ic, isi and isz */
  if (!(z = NAG_ALLOC(pdz * m, double)) ||
      !(ic = NAG_ALLOC(n, Integer)) ||
      !(isi = NAG_ALLOC(n, Integer)) ||
      !(isz = NAG_ALLOC(m, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

  if (order == Nag_ColMajor)
    pdz = n;
  else
    pdz = m;

  for (i = 1; i <= n; ++i)
    {
      scanf("%ld%ld", &isi[i-1], &ic[i-1]);
      for (j = 1; j <= m; ++j)
        scanf("%lf", &Z(i, j));
      scanf("%*[^\n] ");
    }

  for (j = 1; j <= m; ++j)
    scanf("%ld", &isz[j-1]);
  scanf("%ld%*[^\n] ", &p);

  /* Allocate other arrays */
  if (!(b = NAG_ALLOC(p, double)) ||
      !(cov = NAG_ALLOC(p*(p+1)/2, double)) ||
      !(sc = NAG_ALLOC(p, double)) ||
      !(se = NAG_ALLOC(p, double)) ||
      !(nca = NAG_ALLOC(ns, Integer)) ||
      !(nct = NAG_ALLOC(ns, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

  for (j = 1; j <= m; ++j)
    scanf("%lf", &b[j-1]);
  scanf("%*[^\n] ");

  /* nag_condl_logistic (g11cac).
   * Returns parameter estimates for the conditional analysis
   * of stratified data
   */
  nag_condl_logistic(order, n, m, ns, z, pdz, isz, p, ic, isi, &dev, b, se, sc,
                     cov, nca, nct, tol, maxit, iprint, 0, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_condl_logistic (g11cac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  printf("\n");
  printf(" Deviance = %13.4e\n", dev);
  printf("\n");

  printf(" Strata     No. Cases   No. Controls\n");
  printf("\n");
  for (i = 1; i <= ns; ++i)
    printf("   %3ld          %2ld          %2ld"
            "          \n", i, nca[i-1], nct[i-1]);
  printf("\n");

  printf(" Parameter      Estimate       Standard Error\n");
  printf("\n");
  for (i = 1; i <= p; ++i)
    printf("%5ld          %8.4f          %8.4f          \n", i,
            b[i-1], se[i-1]);

 END:
  NAG_FREE(b);
  NAG_FREE(cov);
  NAG_FREE(sc);
  NAG_FREE(se);
  NAG_FREE(z);
  NAG_FREE(ic);
  NAG_FREE(isi);
  NAG_FREE(isz);
  NAG_FREE(nca);
  NAG_FREE(nct);

  return exit_status;
}