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

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

int main(void)
{

  /* Scalars */
  double tol;
  Integer exit_status, i, idist, p, j, nmax, ns, nsum;
  Integer pdx, pdparvar;
  NagError fail;
  Nag_OrderType order;

  /* Arrays */
  double *eta = 0, *parest = 0, *parvar = 0, *vapvec = 0, *x = 0;
  double *y = 0, *zin = 0;
  Integer *irank = 0, *nv = 0;

#ifdef NAG_COLUMN_MAJOR
#define X(I, J)      x[(J-1)*pdx + I - 1]
#define PARVAR(I, J) parvar[(J-1)*pdparvar + I - 1]
  order = Nag_ColMajor;
#else
#define X(I, J)      x[(I-1)*pdx + J - 1]
#define PARVAR(I, J) parvar[(I-1)*pdparvar + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  exit_status = 0;
  printf("nag_rank_regsn (g08rac) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  /* Read number of samples, number of parameters to be fitted,
   * error distribution parameter and tolerance criterion for ties.
   */
  scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%lf%*[^\n] ", &ns, &p, &idist,
        &tol);

  /* Allocate memory to nv only */
  if (!(nv = NAG_ALLOC(ns, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  printf("\n");
  printf("Number of samples =%2" NAG_IFMT "\n", ns);
  printf("Number of parameters fitted =%2" NAG_IFMT "\n", p);
  printf("Distribution =%2" NAG_IFMT "\n", idist);
  printf("Tolerance for ties =%8.5f\n", tol);

  /* Read the number of observations in each sample. */

  for (i = 1; i <= ns; ++i)
    scanf("%" NAG_IFMT "", &nv[i - 1]);
  scanf("%*[^\n] ");

  nmax = 0;
  nsum = 0;
  for (i = 1; i <= ns; ++i) {
    nsum += nv[i - 1];
    nmax = MAX(nmax, nv[i - 1]);
  }
  if (nmax > 0 && nmax <= 100 && nsum > 0 && nsum <= 100) {
    /* Allocate memory */
    if (!(eta = NAG_ALLOC(nmax, double)) ||
        !(parest = NAG_ALLOC(4 * p + 1, double)) ||
        !(parvar = NAG_ALLOC((p + 1) * p, double)) ||
        !(vapvec = NAG_ALLOC(nmax * (nmax + 1) / 2, double)) ||
        !(x = NAG_ALLOC(nsum * p, double)) ||
        !(y = NAG_ALLOC(nsum, double)) ||
        !(zin = NAG_ALLOC(nmax, double)) ||
        !(irank = NAG_ALLOC(nmax, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
#ifdef NAG_COLUMN_MAJOR
    pdx = nsum;
    pdparvar = p + 1;
#else
    pdx = p;
    pdparvar = p;
#endif

    /* Read in observations and design matrices for each sample. */
    for (i = 1; i <= nsum; ++i) {
      scanf("%lf", &y[i - 1]);
      for (j = 1; j <= p; ++j)
        scanf("%lf", &X(i, j));
    }
    scanf("%*[^\n] ");

    /* nag_rank_regsn (g08rac).
     * Regression using ranks, uncensored data
     */
    nag_rank_regsn(order, ns, nv, y, p, x, pdx, idist, nmax, tol,
                   parvar, pdparvar, irank, zin, eta, vapvec, parest, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_rank_regsn (g08rac).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

    printf("\n");
    printf("Score statistic\n");
    for (i = 1; i <= p; ++i)
      printf("%9.3f%s", parest[i - 1], i % 2 == 0 || i == p ? "\n" : " ");
    printf("\n");

    printf("Covariance matrix of score statistic\n");
    for (j = 1; j <= p; ++j) {
      for (i = 1; i <= j; ++i)
        printf("%9.3f%s", PARVAR(i, j), i % 2 == 0 || i == j ? "\n" : " ");
    }
    printf("\n");

    printf("Parameter estimates\n");
    for (i = 1; i <= p; ++i)
      printf("%9.3f%s", parest[p + i - 1], i % 2 == 0 || i == p ? "\n" : " ");
    printf("\n");

    printf("Covariance matrix of parameter estimates\n");
    for (i = 1; i <= p; ++i)
    {
      printf(" ");

      for (j = 1; j <= i; ++j)
        printf("%9.3f%s", PARVAR(i + 1, j),
               j % 2 == 0 || j == i ? "\n" : " ");
    }
    printf("\n");

    printf("Chi-squared statistic =%9.3f with%2" NAG_IFMT " d.f.\n",
           parest[p * 2], p);
    printf("\n");
    printf("Standard errors of estimates and\n");
    printf("approximate z-statistics\n");
    for (i = 1; i <= p; ++i)
      printf("%9.3f%14.3f\n", parest[2 * p + 1 + i - 1],
             parest[p * 3 + 1 + i - 1]);
    printf("\n");
  }
END:
  NAG_FREE(eta);
  NAG_FREE(parest);
  NAG_FREE(parvar);
  NAG_FREE(vapvec);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(zin);
  NAG_FREE(irank);
  NAG_FREE(nv);

  return exit_status;
}