/* nag_rank_regsn_censored (g08rbc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 7, 2001.
 */

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

int main(void)
{

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

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

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

  INIT_FAIL(fail);

  exit_status = 0;
  printf("nag_rank_regsn_censored (g08rbc) Example Program Results\n");

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

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

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

  printf("Number of samples =%2ld\n", ns);
  printf("Number of parameters fitted =%2ld\n", p);
  printf("Distribution power parameter =%10.5f\n", gamma);

  printf("Tolerance for ties =%10.5f\n", tol);

  printf("\n");
  /* Read the number of observations in each sample */

  for (i = 1; i <= ns; ++i)
    scanf("%ld", &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]);
    }

  /* Allocate memory */
  if (!(eta = NAG_ALLOC(nmax, double)) ||
      !(parest = NAG_ALLOC(4*p+1, double)) ||
      !(prvr = NAG_ALLOC(7 * 6, 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)) ||
      !(icen = NAG_ALLOC(nsum, Integer)) ||
      !(irank = NAG_ALLOC(nmax, Integer)) ||
      !(iwa = NAG_ALLOC(400, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
#ifdef NAG_COLUMN_MAJOR
  pdx = nsum;
  pdprvr = p+1;
#else
  pdx = p;
  pdprvr = p;
#endif

  /* Read in observations, design matrix and censoring variable */

  for (i = 1; i <= nsum; ++i)
    {
      scanf("%lf", &y[i - 1]);

      for (j = 1; j <= p; ++j)
        {
          scanf("%lf", &X(i, j));
        }
      scanf("%ld", &icen[i - 1]);
    }
  scanf("%*[^\n] ");

  /* nag_rank_regsn_censored (g08rbc).
   * Regression using ranks, right-censored data
   */
  nag_rank_regsn_censored(order, ns, nv, y, p, x, pdx, icen, gamma,
                          nmax, tol, prvr, pdprvr, irank, zin, eta, vapvec,
                          parest, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_rank_regsn_censored (g08rbc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  printf("Score statistic\n");

  for (i = 1; i <= p; ++i)
    printf("%9.3f\n", parest[i - 1]);
  printf("\n");

  printf("Covariance matrix of score statistic\n");
  for (j = 1; j <= p; ++j)
    {
      for (i = 1; i <= j; ++i)
        printf("%9.3f\n", PRVR(i, j));
      printf("\n");
    }


  printf("Parameter estimates\n");
  for (i = 1; i <= p; ++i)
    printf("%9.3f\n", parest[p + i - 1]);
  printf("\n");
  printf("Covariance matrix of parameter estimates\n");
  for (i = 1; i <= p; ++i)
    {
      for (j = 1; j <= i; ++j)
        printf("%9.3f\n", PRVR(i + 1, j));
      printf("\n");
    }


  printf("Chi-squared statistic =%9.3f with%2ld 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]);
 END:
  NAG_FREE(eta);
  NAG_FREE(parest);
  NAG_FREE(prvr);
  NAG_FREE(vapvec);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(zin);
  NAG_FREE(icen);
  NAG_FREE(irank);
  NAG_FREE(iwa);
  NAG_FREE(nv);

  return exit_status;
}