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

NAG CL Interface Introduction
Example description
/* nag_correg_linregm_obs_edit (g02dcc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.2, 2024.
 */

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

#define XM(I, J) xm[(I)*tdxm + J]

int main(void) {
  Integer exit_status = 0, i, ip, j, m, n, rank, *sx = 0, tdq, tdxm;
  double *b = 0, *com_ar = 0, *cov = 0, df, *h = 0, *p = 0, *q = 0;
  double *res = 0, rss, *se = 0, tol, *wt = 0, *wtptr, *xm = 0;
  double *y = 0;
  char nag_enum_arg[40];
  Nag_Boolean svd, weight;
  Nag_IncludeMean mean;
  Nag_UpdateObserv update;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_correg_linregm_obs_edit (g02dcc) Example Program "
         "Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT " %" NAG_IFMT "", &n, &m);
  scanf(" %39s", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  weight = (Nag_Boolean)nag_enum_name_to_value(nag_enum_arg);
  scanf(" %39s", nag_enum_arg);
  mean = (Nag_IncludeMean)nag_enum_name_to_value(nag_enum_arg);

  if (weight)
    wtptr = wt;
  else
    wtptr = (double *)0;

  if (n >= 2 && m >= 1) {
    if (!(b = NAG_ALLOC(m, double)) || !(h = NAG_ALLOC(n, double)) ||
        !(res = NAG_ALLOC(n, double)) || !(wt = NAG_ALLOC(n, double)) ||
        !(xm = NAG_ALLOC(n * m, double)) || !(y = NAG_ALLOC(n, double)) ||
        !(sx = NAG_ALLOC(m, Integer))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    tdxm = m;
  } else {
    printf("Invalid n or m.\n");
    exit_status = 1;
    return exit_status;
  }
  if (wtptr) {
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++)
        scanf("%lf", &XM(i, j));
      scanf("%lf%lf", &y[i], &wt[i]);
    }
  } else {
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++)
        scanf("%lf", &XM(i, j));
      scanf("%lf", &y[i]);
    }
  }
  for (j = 0; j < m; ++j)
    scanf("%" NAG_IFMT "", &sx[j]);
  scanf("%" NAG_IFMT "", &ip);

  if (!(cov = NAG_ALLOC(ip * (ip + 1) / 2, double)) ||
      !(p = NAG_ALLOC(ip * (ip + 2), double)) ||
      !(q = NAG_ALLOC((n) * (ip + 1), double)) ||
      !(com_ar = NAG_ALLOC(ip + ip * ip, double)) ||
      !(se = NAG_ALLOC(ip, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  tdq = ip + 1;
  /* Set tolerance */
  tol = 0.00001e0;

  /* Fit initial model using nag_correg_linregm_fit (g02dac) */
  /* nag_correg_linregm_fit (g02dac).
   * Fits a general (multiple) linear regression model
   */
  nag_correg_linregm_fit(mean, n, xm, tdxm, m, sx, ip, y, wtptr, &rss, &df, b,
                         se, cov, res, h, q, tdq, &svd, &rank, p, tol, com_ar,
                         &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_correg_linregm_fit (g02dac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("Results from g02dac\n\n");
  if (svd)
    printf("Model not of full rank\n");
  printf("Residual sum of squares = %13.4e\n", rss);
  printf("Degrees of freedom = %3.1f\n\n", df);
  printf("Variable    Parameter estimate   Standard error\n\n");
  for (j = 0; j < ip; j++)
    printf("%6" NAG_IFMT "%20.4e%20.4e\n", j + 1, b[j], se[j]);
  printf("\n");
  update = Nag_ObservDel;
  /* nag_correg_linregm_obs_edit (g02dcc).
   * Add/delete an observation to/from a general linear
   * regression model
   */
  nag_correg_linregm_obs_edit(update, mean, m, sx, q, tdq, ip, xm, n, tdxm, n,
                              y[11], wtptr, &rss, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_correg_linregm_obs_edit (g02dcc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  printf("Results from dropping an observation using\n"
         "nag_correg_linregm_obs_edit (g02dcc)\n");
  n = n - 1;
  /* nag_correg_linregm_update (g02ddc).
   * Estimates of regression parameters from an updated model
   */
  nag_correg_linregm_update(n, ip, q, tdq, &rss, &df, b, se, cov, &svd, &rank,
                            p, tol, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_correg_linregm_update (g02ddc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  printf("Residual sum of squares = %13.4e\n", rss);
  printf("Degrees of freedom = %3.1f\n\n", df);
  printf("Variable    Parameter estimate   Standard error\n\n");
  for (j = 0; j < ip; j++)
    printf("%6" NAG_IFMT "%20.4e%20.4e\n", j + 1, b[j], se[j]);
END:
  NAG_FREE(b);
  NAG_FREE(h);
  NAG_FREE(res);
  NAG_FREE(wt);
  NAG_FREE(xm);
  NAG_FREE(y);
  NAG_FREE(sx);
  NAG_FREE(cov);
  NAG_FREE(p);
  NAG_FREE(q);
  NAG_FREE(com_ar);
  NAG_FREE(se);
  return exit_status;
}