/* nag_reml_hier_mixed_regsn (g02jdc) Example Program. * * Copyright 2008, Numerical Algorithms Group. * * Mark 9, 2009. */ /* Pre-processor includes */ #include #include #include #include #include #include void print_results(Nag_OrderType order, Integer n, Integer nff, Integer nlsv, Integer nrf, Integer fixed[], Integer nrndm, Integer rndm[], Integer lrndm, Integer nvpr, Integer vpr[], double gamma[], Integer effn, Integer rnkx, Integer ncov, double like, Integer id[], Integer pdid, double b[], double se[], FILE *fpout); #define RNDM(I, J) rndm[(order == Nag_ColMajor) \ ?((J-1)*lrndm+I-1):((I-1)*nrndm+J-1)] #define DAT(I, J) dat[(order == Nag_ColMajor) \ ?((J-1)*pddat+I-1):((I-1)*pddat+J-1)] #define ID(I, J) id[((J-1)*pdid+I-1)] int main(int argc, char *argv[]) { /* IO file pointers */ FILE *fpin, *fpout; /* Integer scalar and array declarations */ Integer exit_status = 0; Integer pdid, licomm, lrcomm, tdczz, lb, pdcxx, pdcxz, pdczz, pddat, effn, i, j, lvpr, n, ncol, ncov, lfixed, nff, nl, nlsv, nrndm, nrf, nv, nvpr, rnkx, lwt, size_dat, lrndm; Integer *fixed = 0, *icomm = 0, *id = 0, *levels = 0, *rndm = 0; Integer *vpr = 0; Integer ticomm[2]; /* NAG structures */ NagError fail; Nag_OrderType order = Nag_RowMajor; /* Double scalar and array declarations */ double like; double *b = 0, *cxx = 0, *cxz = 0, *czz = 0, *dat = 0, *gamma = 0; double *rcomm = 0, *se = 0, *wt = 0, *y = 0; double trcomm[1]; /* Character scalars */ char weight; /* Use the default options */ Integer *iopt = 0; Integer liopt = 0; double *ropt = 0; Integer lropt = 0; /* Initialise the error structure */ 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); fprintf(fpout, "nag_reml_hier_mixed_regsn (g02jdc) Example Program Results\n\n"); /* Skip headings in data file*/ fscanf(fpin, "%*[^\n] "); /* Read in the initial arguments */ fscanf(fpin, "%c%ld%ld%ld%ld%*[^\n] ", &weight, &n, &ncol, &nrndm, &nvpr); /* Maximum size for fixed and rndm */ lfixed = ncol + 2; lrndm = 2 * ncol + 3; if (order == Nag_ColMajor) { pddat = n; size_dat = pddat * ncol; } else { pddat = ncol; size_dat = pddat * n; } /* Allocate some memory */ if (!(y = NAG_ALLOC(n, double)) || !(vpr = NAG_ALLOC(nvpr, Integer)) || !(levels = NAG_ALLOC(ncol, Integer)) || !(gamma = NAG_ALLOC(nvpr+1, double)) || !(fixed = NAG_ALLOC(lfixed, Integer)) || !(rndm = NAG_ALLOC(lrndm * nrndm, Integer)) || !(dat = NAG_ALLOC(size_dat, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Check whether we are supplying weights and allocate memory if required */ if (weight == 'W') { lwt = n; if (!(wt = NAG_ALLOC(lwt, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } } else { lwt = 0; } /* Read in the number of levels associated with each of the independent variables */ for (i = 0; i < ncol; i++) fscanf(fpin, "%ld", &levels[i]); fscanf(fpin, "%*[^\n] "); /* Read in the fixed part of the model */ /* Skip the heading */ fscanf(fpin, "%*[^\n] "); /* Number of variables */ fscanf(fpin, "%ld%*[^\n] ", &fixed[0]); nv = fixed[0]; if (nv+2 > lfixed) { fprintf(fpout, " ** Problem size too large, increase array sizes\n"); fprintf(fpout, "LFIXED,NV+2 = %ld, %ld\n", lfixed, nv+2); exit_status = -1; goto END; } /* Intercept */ fscanf(fpin, "%ld%*[^\n] ", &fixed[1]); /* Variable IDs */ if (nv > 0) { for (i = 2; i < nv + 2; i++) fscanf(fpin, "%ld", &fixed[i]); fscanf(fpin, "%*[^\n] "); } /* Read in the random part of the model */ lvpr = 0; pdid = 0; for (j = 1; j <= nrndm; j++) { fscanf(fpin, "%*[^\n] "); /* Number of variables */ fscanf(fpin, "%ld%*[^\n] ", &RNDM(1, j)); nv = RNDM(1, j); if ((nv+3) > lrndm) { fprintf(fpout, " ** Problem size too large, increase array sizes\n"); fprintf(fpout, "LRNDM,NV+2 = %ld, %ld\n", lrndm, nv+2); exit_status = -1; goto END; } /* Intercept */ fscanf(fpin, "%ld%*[^\n] ", &RNDM(2, j)); /* Variable IDs */ if (nv > 0) { for (i = 3; i <= nv + 2; i++) fscanf(fpin, "%ld", &RNDM(i, j)); fscanf(fpin, "%*[^\n] "); } /* Number of subject variables */ fscanf(fpin, "%ld%*[^\n] ", &RNDM(nv+3, j)); nl = RNDM(nv+3, j); if (nv+nl+2 > lrndm) { fprintf(fpout, " ** Problem size too large, increase array sizes\n"); fprintf(fpout, "LRNDM,NV+NL++2 = %ld, %ld\n", lrndm, nv+nl+2); exit_status = -1; goto END; } /* Subject variable IDs */ if (nl > 0) { for (i = nv+4; i <= nv + nl + 3; i++) fscanf(fpin, "%ld", &RNDM(i, j)); fscanf(fpin, "%*[^\n] "); } pdid = MAX(pdid, nl); lvpr += RNDM(2, j) + nv; } pdid += 3; /* Read in the dependent and independent data */ for (i = 1; i <= n; i++) { fscanf(fpin, "%lf", &y[i - 1]); for (j = 1; j <= ncol; j++) fscanf(fpin, "%lf", &DAT(i, j)); if (lwt > 0) fscanf(fpin, "%lf", &wt[i - 1]); fscanf(fpin, "%*[^\n] "); } /* Read in VPR */ for (i = 0; i < lvpr; i++) fscanf(fpin, "%ld", &vpr[i]); fscanf(fpin, "%*[^\n] "); /* Read in GAMMA */ for (i = 0; i < nvpr; i++) fscanf(fpin, "%lf", &gamma[i]); fscanf(fpin, "%*[^\n] "); /* Get the size of the communication arrays */ licomm = 2; lrcomm = 1; nag_hier_mixed_init(order, n, ncol, dat, pddat, levels, y, wt, fixed, lfixed, nrndm, rndm, lrndm, &nff, &nlsv, &nrf, trcomm, lrcomm, ticomm, licomm, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_hier_mixed_init (g02jcc).\n%s\n", fail.message); exit_status = 1; goto END; } licomm = ticomm[0]; lrcomm = ticomm[1]; /* Allocate the communication arrays */ if (!(icomm = NAG_ALLOC(licomm, Integer)) || !(rcomm = NAG_ALLOC(lrcomm, double))) { fprintf(fpout, "Allocation failure 4\n"); exit_status = -1; goto END; } /* Pre-process the data */ nag_hier_mixed_init(order, n, ncol, dat, pddat, levels, y, wt, fixed, lfixed, nrndm, rndm, lrndm, &nff, &nlsv, &nrf, rcomm, lrcomm, icomm, licomm, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_hier_mixed_init (g02jcc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Allocate the output arrays */ lb = nff + nrf*nlsv; tdczz = nrf*nlsv; pdcxx = nff; pdcxz = nff; pdczz = nrf; if (!(b = NAG_ALLOC(lb, double)) || !(cxx = NAG_ALLOC(pdcxx*nff, double)) || !(cxz = NAG_ALLOC(pdcxz*tdczz, double)) || !(czz = NAG_ALLOC(pdczz*tdczz, double)) || !(se = NAG_ALLOC(lb, double)) || !(id = NAG_ALLOC(pdid*lb, Integer))) { fprintf(fpout, "Allocation failure 5\n"); exit_status = -1; goto END; } /* Perform the analysis */ nag_reml_hier_mixed_regsn(lvpr, vpr, nvpr, gamma, &effn, &rnkx, &ncov, &like, lb, id, pdid, b, se, czz, pdczz, cxx, pdcxx, cxz, pdcxz, rcomm, icomm, iopt, liopt, ropt, lropt, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_reml_hier_mixed_regsn (g02jdc).\n%s\n", fail.message); exit_status = 1; if (fail.code != NW_NOT_CONVERGED && fail.code != NW_TOO_MANY_ITER && fail.code != NW_KT_CONDITIONS && fail.code != NE_NEG_ELEMENT) goto END; } /* Display the output */ print_results(order, n, nff, nlsv, nrf, fixed, nrndm, rndm, lrndm, nvpr, vpr, gamma, effn, rnkx, ncov, like, id, pdid, b, se, fpout); END: if (fpin != stdin && fpin != 0) fclose(fpin); if (fpout != stdout && fpout != 0) fclose(fpout); if (wt) NAG_FREE(wt); if (y) NAG_FREE(y); if (vpr) NAG_FREE(vpr); if (levels) NAG_FREE(levels); if (gamma) NAG_FREE(gamma); if (fixed) NAG_FREE(fixed); if (rndm) NAG_FREE(rndm); if (dat) NAG_FREE(dat); if (icomm) NAG_FREE(icomm); if (rcomm) NAG_FREE(rcomm); if (b) NAG_FREE(b); if (cxx) NAG_FREE(cxx); if (cxz) NAG_FREE(cxz); if (czz) NAG_FREE(czz); if (se) NAG_FREE(se); if (id) NAG_FREE(id); return exit_status; } void print_results(Nag_OrderType order, Integer n, Integer nff, Integer nlsv, Integer nrf, Integer fixed[], Integer nrndm, Integer rndm[], Integer lrndm, Integer nvpr, Integer vpr[], double gamma[], Integer effn, Integer rnkx, Integer ncov, double like, Integer id[], Integer pdid, double b[], double se[], FILE *fpout) { Integer aid, i, k, l, ns, nv, p, pb, tb, tdid, vid, same; /* Display the output */ fprintf(fpout, " Number of observations (N) = %ld\n", n); fprintf(fpout, " Number of random factors (NRF) = %ld\n", nrf); fprintf(fpout, " Number of fixed factors (NFF) = %ld\n", nff); fprintf(fpout, " Number of subject levels (NLSV) = %ld\n", nlsv); fprintf(fpout, " Rank of X (RNKX) = %ld\n", rnkx); fprintf(fpout, " Effective N (EFFN) = %ld\n", effn); fprintf(fpout, " Number of non-zero variance components (NCOV) = %ld\n", ncov); fprintf(fpout, " Parameter Estimates\n"); tdid = nff + nrf*nlsv; if (nrf > 0) { fprintf(fpout, "\n"); fprintf(fpout, " Random Effects\n"); } pb = -999; for (k = 1; k <= nrf*nlsv; k++) { tb = ID(1, k); if (tb != -999) { vid = ID(2, k); nv = RNDM(1, tb); ns = RNDM(3+nv, tb); if (pb != tb) { same = 0; } else { same = 1; for (l = 1; l <= ns; l++) { if (ID(3+l, k) != ID(3+l, k-1)) { same = 0; break; } } } if (!same) { if (k != 1) fprintf(fpout, "\n"); fprintf(fpout, " Subject: "); for (l = 1; l <= ns; l++) fprintf(fpout, " Variable %2ld (Level %1ld) ", RNDM(3+nv+l, tb), ID(3+l, k)); fprintf(fpout, "\n"); } pb = tb; if (vid == 0) { /* Intercept */ fprintf(fpout, " Intercept %10.4f %10.4f\n", b[k], se[k]); } else { /* VID'th variable specified in RNDM */ aid = RNDM(2+vid, tb); if (ID(3, k) == 0) { fprintf(fpout, " Variable %2ld", aid); fprintf(fpout, " %10.4f %10.4f\n", b[k-1], se[k-1]); } else { fprintf(fpout, " Variable %2ld", aid); fprintf(fpout, " (Level %1ld) %10.4f %10.4f\n", ID(3, k), b[k-1], se[k-1]); } } } } if (nff > 0) { fprintf(fpout, "\n"); fprintf(fpout, " Fixed Effects\n"); } for (k = nrf*nlsv + 1; k <= tdid; k++) { vid = ID(2, k); if (vid != -999) { if (vid == 0) { /* Intercept */ fprintf(fpout, " Intercept %10.4f %10.4f\n", b[k - 1], se[k - 1]); } else { /* VID'th variable specified in FIXED */ aid = fixed[2+vid-1]; if (ID(3, k) == 0) { fprintf(fpout, " Variable %2ld", aid); fprintf(fpout, " %10.4f %10.4f\n", b[k - 1], se[k - 1]); } else { fprintf(fpout, " Variable %2ld", aid); fprintf(fpout, " (Level %1ld) %10.4f %10.4f\n", ID(3, k), b[k - 1], se[k - 1]); } } } } fprintf(fpout, "\n"); fprintf(fpout, " Variance Components\n"); fprintf(fpout, " Estimate Parameter Subject\n"); for (k = 1; k <= nvpr; k++) { fprintf(fpout, "%10.5f ", gamma[k - 1]); p = 0; for (tb = 1; tb <= nrndm; tb++) { nv = RNDM(1, tb); ns = RNDM(3+nv, tb); for (i = 1; i <= nv + RNDM(2, tb); i++) { p++; if (vpr[p-1] == k) { fprintf(fpout, "Variable %2ld Variables ", RNDM(2 + i, tb)); for (l = 1; l <= ns; l++) fprintf(fpout, "%2ld ", RNDM(3 + nv + l, tb)); } } } fprintf(fpout, "\n"); } fprintf(fpout, "\n"); fprintf(fpout, "SIGMA**2 = %15.5f\n", gamma[nvpr]); fprintf(fpout, "-2LOG LIKELIHOOD = %15.5f\n", like); }