/* nag_pls_orth_scores_pred (g02ldc) Example Program. * * Copyright 2008, Numerical Algorithms Group. * * Mark 9, 2009. */ /* Pre-processor includes */ #include #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; char *outfile = 0; /*Integer scalar and array declarations */ Integer exit_status = 0; Integer i, ip, j, l, my, mz, n; Integer pdb, pdyhat, pdz; Integer *isz = 0; /*Double scalar and array declarations */ double *b = 0, *xbar = 0, *xstd = 0, *ybar = 0, *yhat = 0; double *ystd = 0, *z = 0; /*Character scalar and array declarations */ char siscale[18], sorig[24]; /*NAG Types */ Nag_OrderType order; Nag_ScalePredictor iscale; Nag_EstimatesOption orig; NagError fail; INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); (void) nag_example_file_io(argc, argv, "-nag_write", &outfile); fprintf(fpout, "%s\n", "nag_pls_orth_scores_pred (g02ldc) Example Program Results"); /* Skip header in data file.*/ fscanf(fpin, "%*[^\n] "); /* Read data values.*/ fscanf(fpin, "%ld%ld%s %s %ld%ld%*[^\n] ", &ip, &my, sorig, siscale, &n, &mz); orig = (Nag_EstimatesOption) nag_enum_name_to_value(sorig); iscale = (Nag_ScalePredictor) nag_enum_name_to_value(siscale); #ifdef NAG_COLUMN_MAJOR pdb = ((orig == Nag_EstimatesStand)?ip:1+ip); #define B(I, J) b[(J-1)*pdb + I-1] pdyhat = n; #define YHAT(I, J) yhat[(J-1)*pdyhat + I-1] pdz = n; #define Z(I, J) z[(J-1)*pdz + I-1] order = Nag_ColMajor; #else pdb = my; #define B(I, J) b[(I-1)*pdb + J-1] pdyhat = my; #define YHAT(I, J) yhat[(I-1)*pdyhat + J-1] pdz = mz; #define Z(I, J) z[(I-1)*pdz + J-1] order = Nag_RowMajor; #endif if (!(b = NAG_ALLOC(pdb*(order == Nag_RowMajor?(1+ip):my), double)) || !(xbar = NAG_ALLOC(ip, double)) || !(xstd = NAG_ALLOC(ip, double)) || !(ybar = NAG_ALLOC(my, double)) || !(yhat = NAG_ALLOC(pdyhat*(order == Nag_RowMajor?n:my), double)) || !(ystd = NAG_ALLOC(my, double)) || !(z = NAG_ALLOC(pdz*(order == Nag_RowMajor?n:mz), double)) || !(isz = NAG_ALLOC(mz, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Read prediction x-data*/ for (i = 1; i <= n; i++) { for (j = 1; j <= mz; j++) fscanf(fpin, "%lf ", &Z(i, j)); } fscanf(fpin, "%*[^\n] "); /* Read elements of isz*/ for (j = 0; j < mz; j++) fscanf(fpin, "%ld ", &isz[j]); fscanf(fpin, "%*[^\n] "); /* Read parameter estimates*/ l = ip; if (orig != Nag_EstimatesStand) { l = l+1; } for (j = 1; j <= l; j++) { for (i = 1; i <= my; i++) fscanf(fpin, "%lf ", &B(j, i)); } fscanf(fpin, "%*[^\n] "); /* Read means*/ if (orig == Nag_EstimatesStand) { for (j = 0; j < ip; j++) fscanf(fpin, "%lf ", &xbar[j]); fscanf(fpin, "%*[^\n] "); for (l = 0; l < my; l++) fscanf(fpin, "%lf ", &ybar[l]); fscanf(fpin, "%*[^\n] "); } /* Read scalings*/ if ((orig == Nag_EstimatesStand) && (iscale != Nag_PredNoScale)) { for (j = 0; j < ip; j++) fscanf(fpin, "%lf ", &xstd[j]); fscanf(fpin, "%*[^\n] "); for (l = 0; l < my; l++) fscanf(fpin, "%lf ", &ystd[l]); fscanf(fpin, "%*[^\n] "); } /* Calculate predictions*/ /* * nag_pls_orth_scores_pred (g02ldc) * Partial least-squares */ nag_pls_orth_scores_pred(order, ip, my, orig, xbar, ybar, iscale, xstd, ystd, b, pdb, n, mz, isz, z, pdz, yhat, pdyhat, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_pls_orth_scores_pred (g02ldc).\n%s\n", fail.message); exit_status = 1; goto END; } /* * nag_gen_real_mat_print (x04cac) * Print real general matrix (easy-to-use) */ if (outfile) fclose(fpout); nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, my, yhat, pdyhat, "YHAT", outfile, &fail); if (outfile && !(fpout = fopen(outfile, "a"))) { exit_status = 2; goto END; } if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message); exit_status = 1; goto END; } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (b) NAG_FREE(b); if (xbar) NAG_FREE(xbar); if (xstd) NAG_FREE(xstd); if (ybar) NAG_FREE(ybar); if (yhat) NAG_FREE(yhat); if (ystd) NAG_FREE(ystd); if (z) NAG_FREE(z); if (isz) NAG_FREE(isz); return exit_status; }