/* nag_glm_est_func (g02gnc) Example Program. * * Copyright 1996 Numerical Algorithms Group. * * Mark 4, 1996. * * Mark 6 revised, 2000. * Mark 8 revised, 2004. */ #include #include #include #include #include #define X(I, J) x[(I) *tdx + J] #define V(I, J) v[(I) *tdv + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Nag_Boolean est; Integer exit_status = 0, i, ip, j, m, max_iter, n, nestfn, print_iter, rank; Integer *sx = 0, tdv, tdx; NagError fail; double dev, df, eps, ex_power, sestat, stat, tol, z; double *b = 0, *cov = 0, *f = 0, *se = 0, *v = 0, *wtptr, *x = 0; double *y = 0; 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_glm_est_func (g02gnc) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld %ld %ld", &n, &m, &print_iter); if (n >= 2 && m >= 1) { if (!(x = NAG_ALLOC(n*m, double)) || !(y = NAG_ALLOC(n, double)) || !(sx = NAG_ALLOC(m, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tdx = m; } else { fprintf(fpout, "Invalid n or m.\n"); exit_status = 1; return exit_status; } wtptr = (double *) 0; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) fscanf(fpin, "%lf", &X(i, j)); fscanf(fpin, "%lf", &y[i]); } for (j = 0; j < m; j++) fscanf(fpin, "%ld", &sx[j]); fscanf(fpin, "%ld", &ip); if (!(b = NAG_ALLOC(ip, double)) || !(f = NAG_ALLOC(ip, double)) || !(v = NAG_ALLOC(n*(ip+6), double)) || !(cov = NAG_ALLOC(ip*(ip+1)/2, double)) || !(se = NAG_ALLOC(ip, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tdv = ip+6; /* Set control parameters */ max_iter = 10; tol = 5e-5; eps = 1e-6; ex_power = 0.0; /* Fit Log-linear model using nag_glm_poisson (g02gcc) */ /* nag_glm_poisson (g02gcc). * Fits a generalized linear model with Poisson errors */ nag_glm_poisson(Nag_Log, Nag_MeanInclude, n, x, tdx, m, sx, ip, y, wtptr, (double *) 0, ex_power, &dev, &df, b, &rank, se, cov, v, tdv, tol, max_iter, print_iter, "", eps, &fail); if (fail.code == NE_NOERROR || fail.code == NE_LSQ_ITER_NOT_CONV || fail.code == NE_RANK_CHANGED || fail.code == NE_ZERO_DOF_ERROR) { fprintf(fpout, "\nDeviance = %13.4e\n", dev); fprintf(fpout, "Degrees of freedom = %3.1f\n\n", df); fprintf(fpout, " Estimate Standard error\n\n"); for (i = 0; i < ip; i++) fprintf(fpout, "%14.4f%14.4f\n", b[i], se[i]); fprintf(fpout, "\n"); fscanf(fpin, "%ld", &nestfn); for (i = 1; i <= nestfn; ++i) { for (j = 0; j < ip; ++j) fscanf(fpin, "%lf", &f[j]); /* nag_glm_est_func (g02gnc). * Estimable function and the standard error of a * generalized linear model */ nag_glm_est_func(ip, rank, b, cov, v, tdv, f, &est, &stat, &sestat, &z, tol, &fail); if (fail.code == NE_NOERROR || fail.code == NE_RANK_EQ_IP) { fprintf(fpout, "\n"); fprintf(fpout, "Function %ld\n\n", i); for (j = 0; j < ip; ++j) fprintf(fpout, "%8.2f%c", f[j], (j%5 == 4 || j == ip-1)?'\n':' '); fprintf(fpout, "\n"); if (est) fprintf(fpout, "stat = %10.4f sestat = %10.4f z = %10.4f\n", stat, sestat, z); else fprintf(fpout, "Function not estimable\n"); } else { fprintf(fpout, "Error from nag_glm_est_func (g02gnc).\n%s\n", fail.message); exit_status = 1; goto END; } } } else { fprintf(fpout, "Error from nag_glm_poisson (g02gcc).\n%s\n", fail.message); exit_status = 1; goto END; } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (x) NAG_FREE(x); if (y) NAG_FREE(y); if (sx) NAG_FREE(sx); if (b) NAG_FREE(b); if (f) NAG_FREE(f); if (v) NAG_FREE(v); if (cov) NAG_FREE(cov); if (se) NAG_FREE(se); return exit_status; }