/* nag_anova_confid_interval (g04dbc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 6, 2000.
 */

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

int main(void)
{

  Integer          exit_status = 0, nt, i, ij, irdf, j, n, nblock;
  Integer          *irep = 0, *isig = 0, *it = 0;
  const char       *fmt_99998[] = { "", "   %3.0f  ", "%10.1f  ", "%10.1f  ",
                                    "%10.3f  ", "%9.4f" };
  char             star[1*2+1];
  char             nag_enum_arg[40];
  double           clevel, gmean, rdf, tol;
  double           *bmean = 0, *c = 0, *cil = 0, *ciu = 0, *ef = 0, *r = 0;
  double           *table = 0, *tmean = 0, *y = 0;
  Nag_IntervalType type;
  NagError         fail;

  #define TABLE(I, J) table[((I) -1)*5 + (J) -1]

  INIT_FAIL(fail);

  printf(
          "nag_anova_confid_interval (g04dbc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%ld %ld ", &n, &nt);
  if (!(y = NAG_ALLOC(n, double))
     || !(it = NAG_ALLOC(n, Integer))
     || !(tmean = NAG_ALLOC(nt, double))
     || !(table = NAG_ALLOC(4*5, double))
     || !(c = NAG_ALLOC(nt*nt, double))
     || !(irep = NAG_ALLOC(nt, Integer))
     || !(r = NAG_ALLOC(n, double))
     || !(ef = NAG_ALLOC(nt, double))
     || !(isig = NAG_ALLOC(nt*(nt-1)/2, Integer))
     || !(cil = NAG_ALLOC(nt*(nt-1)/2, double))
     || !(ciu = NAG_ALLOC(nt*(nt-1)/2, double)))
    {
      printf("Allocation failure\n");
      exit_status = 1;
      goto END;
    }


  for (i = 1; i <= n; ++i)
    scanf("%lf ", &y[i - 1]);
  for (i = 1; i <= n; ++i)
    scanf("%ld ", &it[i - 1]);
  tol = 5e-6;
  irdf = 0;
  nblock = 1;
  if (!(bmean = NAG_ALLOC(nblock, double)))
    {
      exit_status = -1;
      printf("Allocation failure\n");
      goto END;
    }
  /* nag_anova_random (g04bbc).
   * General block design or completely randomized design
   */
  nag_anova_random(n, y, Nag_NoBlocks, nblock, nt, it, &gmean, bmean, tmean,
                   table, c, nt, irep, r, ef, tol, irdf, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_anova_random (g04bbc).\n%s\n",
              fail.message);
      exit_status = -1;
      goto END;
    }

  printf("\n%s\n\n", "ANOVA table");
  printf("%s\n\n",
          "  Source        df         SS          MS          F        Prob");
  printf(" Treatments");
  for (j = 1; j <= 5; ++j)
    printf(fmt_99998[j], TABLE(2, j));
  printf("\n");
  printf(" Residual  ");
  for (j = 1; j <= 3; ++j)
    printf(fmt_99998[j], TABLE(3, j));
  printf("\n");
  printf(" Total     ");
  for (j = 1; j <= 2; ++j)
    printf(fmt_99998[j], TABLE(4, j));
  printf("\n");
  printf("\n Treatment means\n");
  printf("\n");
  for (j = 1; j <= nt; ++j)
    printf("%8.3f%s", tmean[j - 1], j%8?"":"\n");
  printf("\n");
  printf("\n Simultaneous Confidence Intervals\n\n");
  rdf = TABLE(3, 1);
  scanf(" %39s %lf", nag_enum_arg, &clevel);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  type = (Nag_IntervalType) nag_enum_name_to_value(nag_enum_arg);

  /* nag_anova_confid_interval (g04dbc).
   * Computes confidence intervals for differences between
   * means computed by nag_anova_random (g04bbc) or
   * nag_anova_row_col (g04bcc)
   */
  nag_anova_confid_interval(type, nt, tmean, rdf, c, nt, clevel, cil, ciu,
                            isig, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_anova_confid_interval (g04dbc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  star[1] = '*';
  star[0] = ' ';
  star[2] = '\0';
  ij = 0;
  for (i = 1; i <= nt; ++i)
    {
      for (j = 1; j <= i - 1; ++j)
        {
          ++ij;
          printf("  %2ld%2ld   %10.3f  %10.3f  %c\n",
                  i, j, cil[ij - 1], ciu[ij - 1], star[isig[ij - 1]]);
        }
    }

 END:
  NAG_FREE(y);
  NAG_FREE(it);
  NAG_FREE(tmean);
  NAG_FREE(table);
  NAG_FREE(c);
  NAG_FREE(irep);
  NAG_FREE(r);
  NAG_FREE(ef);
  NAG_FREE(isig);
  NAG_FREE(cil);
  NAG_FREE(ciu);
  NAG_FREE(bmean);

  return exit_status;
}