/* nag_glm_binomial (g02gbc) Example Program. * * Copyright 1996 Numerical Algorithms Group. * * Mark 4, 1996. * Mark 8 revised, 2004. * */ #include #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; Integer exit_status = 0, i, *ivar = 0, j, m, max_iter, n, nvar, print_iter; Integer rank, tdv, tdx; Nag_IncludeMean mean; Nag_Link link; Nag_Boolean weight; char nag_enum_arg[40]; double dev, df, eps, tol; double *beta = 0, *binom = 0, *cov = 0, *offsetptr = 0, *se = 0; double *v = 0, *wt = 0, *wtptr, *x = 0, *y = 0; 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); fprintf(fpout, "nag_glm_binomial (g02gbc) Example Program Results\n"); /* Skip heading 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 */ link = (Nag_Link) nag_enum_name_to_value(nag_enum_arg); fscanf(fpin, "%s", nag_enum_arg); mean = (Nag_IncludeMean) 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 %ld %ld", &n, &m, &print_iter); if (n >= 2 && m >= 1) { if (!(binom = NAG_ALLOC(n, double)) || !(wt = NAG_ALLOC(n, double)) || !(x = NAG_ALLOC(n*m, double)) || !(y = NAG_ALLOC(n, double)) || !(ivar = 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; } if (weight) { wtptr = wt; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) fscanf(fpin, "%lf", &X(i, j)); fscanf(fpin, "%lf%lf%lf", &y[i], &binom[i], &wt[i]); } } else { wtptr = (double *) 0; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) fscanf(fpin, "%lf", &X(i, j)); fscanf(fpin, "%lf%lf", &y[i], &binom[i]); } } for (j = 0; j < m; j++) fscanf(fpin, "%ld", &ivar[j]); /* Calculate nvar */ nvar = 0; for (i = 0; i < m; i++) if (ivar[i] > 0) nvar += 1; if (mean == Nag_MeanInclude) nvar += 1; if (!(beta = NAG_ALLOC(nvar, double)) || !(v = NAG_ALLOC((n)*(nvar+6), double)) || !(se = NAG_ALLOC(nvar, double)) || !(cov = NAG_ALLOC(nvar*(nvar+1)/2, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tdv = nvar+6; /* Set other control parameters */ max_iter = 10; tol = 5e-5; eps = 1e-6; /* nag_glm_binomial (g02gbc). * Fits a generalized linear model with binomial errors */ nag_glm_binomial(link, mean, n, x, tdx, m, ivar, nvar, y, binom, wtptr, offsetptr, &dev, &df, beta, &rank, se, cov, v, tdv, tol, max_iter, print_iter, "", eps, &fail); if (fail.code == NE_NOERROR || fail.code == NE_SVD_NOT_CONV || fail.code == NE_LSQ_ITER_NOT_CONV || fail.code == NE_RANK_CHANGED || fail.code == NE_ZERO_DOF_ERROR) { if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_glm_binomial (g02gbc).\n%s\n", fail.message); } 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 < nvar; i++) fprintf(fpout, "%14.4f%14.4f\n", beta[i], se[i]); fprintf(fpout, "\n"); fprintf(fpout, " binom y fitted value Residual Leverage\n\n"); for (i = 0; i < n; ++i) { fprintf(fpout, "%10.1f%7.1f%10.2f%12.4f%10.3f\n", binom[i], y[i], V(i, 1), V(i, 4), V(i, 5)); } } else { fprintf(fpout, "Error from nag_glm_binomial (g02gbc).\n%s\n", fail.message); exit_status = 1; goto END; } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (binom) NAG_FREE(binom); if (wt) NAG_FREE(wt); if (x) NAG_FREE(x); if (y) NAG_FREE(y); if (beta) NAG_FREE(beta); if (v) NAG_FREE(v); if (se) NAG_FREE(se); if (cov) NAG_FREE(cov); if (ivar) NAG_FREE(ivar); return exit_status; }