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

NAG CL Interface Introduction
Example description
/* nag_tsa_uni_dickey_fuller_unit (g13awc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.1, 2024.
 */

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

int main(void) {
  /* Integer scalar and array declarations */
  Integer n, nsamp = 0, p, i;
  Integer exit_status = 0;
  Integer *state = 0;

  /* NAG structures and types */
  NagError fail;
  Nag_TS_URProbMethod method;
  Nag_TS_URTestType type;

  /* Double scalar and array declarations */
  double pvalue, ts;
  double *y = 0;

  /* Character scalar and array declarations */
  char ctype[30];

  /* Initialize the error structure */
  INIT_FAIL(fail);

  printf("nag_tsa_uni_dickey_fuller_unit (g13awc) Example Program Results\n\n");

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

  /* Read in the problem size, test type, order of the AR process */
  scanf("%" NAG_IFMT "%29s%" NAG_IFMT "%*[^\n] ", &n, ctype, &p);
  type = (Nag_TS_URTestType)nag_enum_name_to_value(ctype);

  /* Allocate memory */
  if (!(y = NAG_ALLOC(n, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read in the time series */
  for (i = 0; i < n; i++)
    scanf("%lf", &y[i]);
  scanf("%*[^\n] ");

  /* nag_tsa_uni_dickey_fuller_unit (g13awc):
     Calculate the Dickey-Fuller test statistic */
  ts = nag_tsa_uni_dickey_fuller_unit(type, p, n, y, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_tsa_uni_dickey_fuller_unit (g13awc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* g01ewc: Get the associated p-value using simulation */
  method = Nag_ViaLookUp;
  pvalue = nag_stat_prob_dickey_fuller_unit(method, type, n, ts, nsamp, state,
                                            &fail);
  if (fail.code != NE_NOERROR && fail.code != NW_EXTRAPOLATION) {
    printf("Error from nag_stat_prob_dickey_fuller_unit (g01ewc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Display the results */
  printf("Dickey-Fuller test statistic     = %6.3f\n", ts);
  printf("associated p-value               = %6.3f\n", pvalue);
  if (fail.code == NW_EXTRAPOLATION) {
    printf("NB: p-value obtained via extrapolation\n");
  }

END:
  NAG_FREE(y);

  return exit_status;
}