/* nag_glm_tran_model (g02gkc) Example Program. * * Copyright 1996 Numerical Algorithms Group. * * Mark 4, 1996. * Mark 8 revised, 2004. * */ #include #include #include #include #include #define X(I, J) x[(I) *tdx + J] #define C(I, J) c[(I) *tdc + J] #define V(I, J) v[(I) *tdv + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, ip, j, m, max_iter, n, nclin, print_iter, rank; Integer *sx = 0, tdc, tdv, tdx; NagError fail; double dev, df, eps, ex_power; double *b = 0, *c = 0, *cov = 0, *se = 0, tol, *v = 0, *wtptr; double *x = 0, *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_tran_model (g02gkc) Example Program Results\n\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)) || !(cov = NAG_ALLOC(ip*(ip+1)/2, double)) || !(se = NAG_ALLOC(ip, double)) || !(v = NAG_ALLOC(n*(ip+6), 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); /* Input constraints */ nclin = ip - rank; if (!(c = NAG_ALLOC(ip*nclin, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tdc = nclin; for (i = 0; i < ip; ++i) for (j = 0; j < nclin; ++j) fscanf(fpin, "%lf", &C(i, j)); /* nag_glm_tran_model (g02gkc). * Estimates and standard errors of parameters of a general * linear model for given constraints */ nag_glm_tran_model(ip, nclin, v, tdv, c, tdc, b, 1.0e0, se, cov, &fail); if (fail.code == NE_NOERROR) { 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"); } else { fprintf(fpout, "Error from nag_glm_tran_model (g02gkc).\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 (c) NAG_FREE(c); if (b) NAG_FREE(b); if (cov) NAG_FREE(cov); if (se) NAG_FREE(se); if (v) NAG_FREE(v); return exit_status; }