/* nag_mv_factor (g03cac) Example Program. * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. * Mark 8 revised, 2004. * */ #include #include #include #include #include #include #include #define FL(I, J) fl[(I) *tdfl + J] #define X(I, J) x[(I) *tdx + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; char *outfile = 0; Integer exit_status = 0, i, *isx = 0, j, l, m, n, nfac, nvar, tdfl, tdx; double *com = 0, *e = 0, eps, *fl = 0, *psi = 0, *res = 0, *stat = 0; double *wt = 0, *wtptr = 0, *x = 0; char nag_enum_arg[40]; Nag_Boolean weight; Nag_E04_Opt options; Nag_FacMat matrix; 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); if (!outfile) { outfile = NAG_ALLOC(7, char); strcpy(outfile, "stdout"); } fprintf(fpout, "nag_mv_factor (g03cac) Example Program Results\n\n"); /* Skip headings in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%s", nag_enum_arg); /* nag_enum_name_to_value(x04nac). * Converts NAG enum member name to value */ matrix = (Nag_FacMat) nag_enum_name_to_value(nag_enum_arg); fscanf(fpin, "%s", nag_enum_arg); weight = (Nag_Boolean) nag_enum_name_to_value(nag_enum_arg); fscanf(fpin, "%ld", &n); fscanf(fpin, "%ld", &m); fscanf(fpin, "%ld", &nvar); fscanf(fpin, "%ld", &nfac); if (nvar >= 2 && m >= nvar && n > nvar) { if (!(com = NAG_ALLOC(nvar, double)) || !(e = NAG_ALLOC(nvar, double)) || !(fl = NAG_ALLOC(nvar*nfac, double)) || !(psi = NAG_ALLOC(nvar, double)) || !(res = NAG_ALLOC(nvar*(nvar-1)/2, double)) || !(stat = NAG_ALLOC(4, double)) || !(wt = NAG_ALLOC(n, double)) || !(x = NAG_ALLOC((matrix == Nag_MatCorr_Covar?m:n)*m, double)) || !(isx = NAG_ALLOC(m, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tdfl = nfac; tdx = m; } else { fprintf(fpout, "Invalid nvar or m or n.\n"); exit_status = 1; return exit_status; } if (matrix == Nag_MatCorr_Covar) { for (i = 0; i < m; ++i) { for (j = 0; j < m; ++j) fscanf(fpin, "%lf", &X(i, j)); } } else { if (weight) { for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) fscanf(fpin, "%lf", &X(i, j)); fscanf(fpin, "%lf", &wt[i]); } wtptr = wt; } else { for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) fscanf(fpin, "%lf", &X(i, j)); } } } for (j = 0; j < m; ++j) fscanf(fpin, "%ld", &isx[j]); /* nag_opt_init (e04xxc). * Initialization function for option setting */ nag_opt_init(&options); options.max_iter = 500; options.optim_tol = 1e-2; strcpy(options.outfile, outfile); eps = 1e-5; /* nag_mv_factor (g03cac). * Maximum likelihood estimates of parameters */ if (strcmp(outfile, "stdout")) fclose(fpout); nag_mv_factor(matrix, n, m, x, tdx, nvar, isx, nfac, wtptr, e, stat, com, psi, res, fl, tdfl, &options, eps, &fail); if (strcmp(outfile, "stdout")) { fpout = fopen(outfile, "a"); } if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_mv_factor (g03cac).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\nEigenvalues\n\n"); for (j = 0; j < m; ++j) { fprintf(fpout, " %13.4e%s", e[j], (j+1)%6 == 0?"\n":""); } fprintf(fpout, "\n\n%s%6.3f\n", " Test Statistic = ", stat[1]); fprintf(fpout, "%s%6.3f\n", " df = ", stat[2]); fprintf(fpout, "%s%6.3f\n\n", "Significance level = ", stat[3]); fprintf(fpout, "Residuals\n\n"); l = 1; for (i = 1; i <= nvar-1; ++i) { for (j = l; j <= l+i-1; ++j) fprintf(fpout, " %8.3f", res[j-1]); fprintf(fpout, "\n"); l += i; } fprintf(fpout, "\nLoadings, Communalities and PSI\n\n"); for (i = 0; i < nvar; ++i) { for (j = 0; j < nfac; ++j) fprintf(fpout, " %8.3f", FL(i, j)); fprintf(fpout, "%8.3f%8.3f\n", com[i], psi[i]); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (com) NAG_FREE(com); if (e) NAG_FREE(e); if (fl) NAG_FREE(fl); if (psi) NAG_FREE(psi); if (res) NAG_FREE(res); if (stat) NAG_FREE(stat); if (wt) NAG_FREE(wt); if (x) NAG_FREE(x); if (isx) NAG_FREE(isx); if (outfile) NAG_FREE(outfile); return exit_status; }