NAG Library Manual, Mark 28.4
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_surviv_coxmodel_risksets (g12zac) Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 *
 * Mark 28.4, 2022.
 */

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

int main(void) {
  /* Scalars */
  double dev, tol;
  Integer exit_status, i, ip, iprint, j, lisi, m, maxit, mxn, n, nd, ns, num,
      nxs, pdx, pdz;
  NagError fail;
  Nag_OrderType order;

  /* Arrays */
  double *b = 0, *cov = 0, *sc = 0, *se = 0, *t = 0, *tp = 0, *x = 0, *z = 0;
  Integer *ic = 0, *id = 0, *irs = 0, *isi = 0, *isz = 0, *ixs = 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_surviv_coxmodel_risksets (g12zac) Example Program Results\n");

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

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

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

  if (ns > 0) {
    for (i = 1; i <= n; ++i) {
      scanf("%lf", &t[i - 1]);
      for (j = 1; j <= m; ++j)
        scanf("%lf", &Z(i, j));
      scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &ic[i - 1], &isi[i - 1]);
    }
  } else {
    for (i = 1; i <= n; ++i) {
      scanf("%lf", &t[i - 1]);
      for (j = 1; j <= m; ++j)
        scanf("%lf", &Z(i, j));
      scanf("%" NAG_IFMT "%*[^\n] ", &ic[i - 1]);
    }
  }

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

  /* Allocate other arrays for nag_surviv_coxmodel_risksets (g12zac) */
  mxn = 1000;

  if (order == Nag_ColMajor) {
    pdx = mxn;
  } else {
    pdx = ip;
  }

  if (!(cov = NAG_ALLOC(ip * (ip + 1) / 2, double)) ||
      !(sc = NAG_ALLOC(ip, double)) || !(se = NAG_ALLOC(ip, double)) ||
      !(tp = NAG_ALLOC(n, double)) || !(x = NAG_ALLOC(mxn * ip, double)) ||
      !(id = NAG_ALLOC(mxn, Integer)) || !(irs = NAG_ALLOC(n, Integer)) ||
      !(ixs = NAG_ALLOC(mxn, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* nag_surviv_coxmodel_risksets (g12zac).
   * Creates the risk sets associated with the Cox
   * proportional hazards model for fixed covariates
   */
  nag_surviv_coxmodel_risksets(order, n, m, ns, z, pdz, isz, ip, t, ic, isi,
                               &num, ixs, &nxs, x, mxn, id, &nd, tp, irs,
                               &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_surviv_coxmodel_risksets (g12zac).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Allocate arrays for nag_contab_condl_logistic (g11cac) */
  if (!(b = NAG_ALLOC(ip, double)) || !(nca = NAG_ALLOC(nxs, Integer)) ||
      !(nct = NAG_ALLOC(nxs, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

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

  tol = 1e-5;
  /* nag_contab_condl_logistic (g11cac).
   * Returns parameter estimates for the conditional analysis
   * of stratified data
   */
  nag_contab_condl_logistic(order, num, ip, nxs, x, pdx, isz, ip, id, ixs, &dev,
                            b, se, sc, cov, nca, nct, tol, maxit, iprint, 0,
                            &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_contab_condl_logistic (g11cac).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\n");
  printf(" Parameter      Estimate       Standard Error\n");
  printf("\n");
  for (i = 1; i <= ip; ++i)
    printf("%5" NAG_IFMT "          %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(t);
  NAG_FREE(tp);
  NAG_FREE(x);
  NAG_FREE(z);
  NAG_FREE(ic);
  NAG_FREE(id);
  NAG_FREE(irs);
  NAG_FREE(isi);
  NAG_FREE(isz);
  NAG_FREE(ixs);
  NAG_FREE(nca);
  NAG_FREE(nct);

  return exit_status;
}