Annotation of imach/src/imach.c, revision 1.126
1.126 ! brouard 1: /* $Id: imach.c,v 1.125 2006/04/04 15:20:31 lievre Exp $
! 2: $State: Exp $
! 3: $Log: imach.c,v $
! 4: Revision 1.125 2006/04/04 15:20:31 lievre
! 5: Errors in calculation of health expectancies. Age was not initialized.
! 6: Forecasting file added.
! 7:
! 8: Revision 1.124 2006/03/22 17:13:53 lievre
! 9: Parameters are printed with %lf instead of %f (more numbers after the comma).
! 10: The log-likelihood is printed in the log file
! 11:
! 12: Revision 1.123 2006/03/20 10:52:43 brouard
! 13: * imach.c (Module): <title> changed, corresponds to .htm file
! 14: name. <head> headers where missing.
! 15:
! 16: * imach.c (Module): Weights can have a decimal point as for
! 17: English (a comma might work with a correct LC_NUMERIC environment,
! 18: otherwise the weight is truncated).
! 19: Modification of warning when the covariates values are not 0 or
! 20: 1.
! 21: Version 0.98g
! 22:
! 23: Revision 1.122 2006/03/20 09:45:41 brouard
! 24: (Module): Weights can have a decimal point as for
! 25: English (a comma might work with a correct LC_NUMERIC environment,
! 26: otherwise the weight is truncated).
! 27: Modification of warning when the covariates values are not 0 or
! 28: 1.
! 29: Version 0.98g
! 30:
! 31: Revision 1.121 2006/03/16 17:45:01 lievre
! 32: * imach.c (Module): Comments concerning covariates added
! 33:
! 34: * imach.c (Module): refinements in the computation of lli if
! 35: status=-2 in order to have more reliable computation if stepm is
! 36: not 1 month. Version 0.98f
! 37:
! 38: Revision 1.120 2006/03/16 15:10:38 lievre
! 39: (Module): refinements in the computation of lli if
! 40: status=-2 in order to have more reliable computation if stepm is
! 41: not 1 month. Version 0.98f
! 42:
! 43: Revision 1.119 2006/03/15 17:42:26 brouard
! 44: (Module): Bug if status = -2, the loglikelihood was
! 45: computed as likelihood omitting the logarithm. Version O.98e
! 46:
! 47: Revision 1.118 2006/03/14 18:20:07 brouard
! 48: (Module): varevsij Comments added explaining the second
! 49: table of variances if popbased=1 .
! 50: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
! 51: (Module): Function pstamp added
! 52: (Module): Version 0.98d
! 53:
! 54: Revision 1.117 2006/03/14 17:16:22 brouard
! 55: (Module): varevsij Comments added explaining the second
! 56: table of variances if popbased=1 .
! 57: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
! 58: (Module): Function pstamp added
! 59: (Module): Version 0.98d
! 60:
! 61: Revision 1.116 2006/03/06 10:29:27 brouard
! 62: (Module): Variance-covariance wrong links and
! 63: varian-covariance of ej. is needed (Saito).
! 64:
! 65: Revision 1.115 2006/02/27 12:17:45 brouard
! 66: (Module): One freematrix added in mlikeli! 0.98c
! 67:
! 68: Revision 1.114 2006/02/26 12:57:58 brouard
! 69: (Module): Some improvements in processing parameter
! 70: filename with strsep.
! 71:
! 72: Revision 1.113 2006/02/24 14:20:24 brouard
! 73: (Module): Memory leaks checks with valgrind and:
! 74: datafile was not closed, some imatrix were not freed and on matrix
! 75: allocation too.
! 76:
! 77: Revision 1.112 2006/01/30 09:55:26 brouard
! 78: (Module): Back to gnuplot.exe instead of wgnuplot.exe
! 79:
! 80: Revision 1.111 2006/01/25 20:38:18 brouard
! 81: (Module): Lots of cleaning and bugs added (Gompertz)
! 82: (Module): Comments can be added in data file. Missing date values
! 83: can be a simple dot '.'.
! 84:
! 85: Revision 1.110 2006/01/25 00:51:50 brouard
! 86: (Module): Lots of cleaning and bugs added (Gompertz)
! 87:
! 88: Revision 1.109 2006/01/24 19:37:15 brouard
! 89: (Module): Comments (lines starting with a #) are allowed in data.
! 90:
! 91: Revision 1.108 2006/01/19 18:05:42 lievre
! 92: Gnuplot problem appeared...
! 93: To be fixed
! 94:
! 95: Revision 1.107 2006/01/19 16:20:37 brouard
! 96: Test existence of gnuplot in imach path
! 97:
! 98: Revision 1.106 2006/01/19 13:24:36 brouard
! 99: Some cleaning and links added in html output
! 100:
! 101: Revision 1.105 2006/01/05 20:23:19 lievre
! 102: *** empty log message ***
! 103:
! 104: Revision 1.104 2005/09/30 16:11:43 lievre
! 105: (Module): sump fixed, loop imx fixed, and simplifications.
! 106: (Module): If the status is missing at the last wave but we know
! 107: that the person is alive, then we can code his/her status as -2
! 108: (instead of missing=-1 in earlier versions) and his/her
! 109: contributions to the likelihood is 1 - Prob of dying from last
! 110: health status (= 1-p13= p11+p12 in the easiest case of somebody in
! 111: the healthy state at last known wave). Version is 0.98
! 112:
! 113: Revision 1.103 2005/09/30 15:54:49 lievre
! 114: (Module): sump fixed, loop imx fixed, and simplifications.
! 115:
! 116: Revision 1.102 2004/09/15 17:31:30 brouard
! 117: Add the possibility to read data file including tab characters.
! 118:
! 119: Revision 1.101 2004/09/15 10:38:38 brouard
! 120: Fix on curr_time
! 121:
! 122: Revision 1.100 2004/07/12 18:29:06 brouard
! 123: Add version for Mac OS X. Just define UNIX in Makefile
! 124:
! 125: Revision 1.99 2004/06/05 08:57:40 brouard
! 126: *** empty log message ***
! 127:
! 128: Revision 1.98 2004/05/16 15:05:56 brouard
! 129: New version 0.97 . First attempt to estimate force of mortality
! 130: directly from the data i.e. without the need of knowing the health
! 131: state at each age, but using a Gompertz model: log u =a + b*age .
! 132: This is the basic analysis of mortality and should be done before any
! 133: other analysis, in order to test if the mortality estimated from the
! 134: cross-longitudinal survey is different from the mortality estimated
! 135: from other sources like vital statistic data.
! 136:
! 137: The same imach parameter file can be used but the option for mle should be -3.
! 138:
! 139: Agnès, who wrote this part of the code, tried to keep most of the
! 140: former routines in order to include the new code within the former code.
! 141:
! 142: The output is very simple: only an estimate of the intercept and of
! 143: the slope with 95% confident intervals.
! 144:
! 145: Current limitations:
! 146: A) Even if you enter covariates, i.e. with the
! 147: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
! 148: B) There is no computation of Life Expectancy nor Life Table.
! 149:
! 150: Revision 1.97 2004/02/20 13:25:42 lievre
! 151: Version 0.96d. Population forecasting command line is (temporarily)
! 152: suppressed.
! 153:
! 154: Revision 1.96 2003/07/15 15:38:55 brouard
! 155: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
! 156: rewritten within the same printf. Workaround: many printfs.
! 157:
! 158: Revision 1.95 2003/07/08 07:54:34 brouard
! 159: * imach.c (Repository):
! 160: (Repository): Using imachwizard code to output a more meaningful covariance
! 161: matrix (cov(a12,c31) instead of numbers.
! 162:
! 163: Revision 1.94 2003/06/27 13:00:02 brouard
! 164: Just cleaning
! 165:
! 166: Revision 1.93 2003/06/25 16:33:55 brouard
! 167: (Module): On windows (cygwin) function asctime_r doesn't
! 168: exist so I changed back to asctime which exists.
! 169: (Module): Version 0.96b
! 170:
! 171: Revision 1.92 2003/06/25 16:30:45 brouard
! 172: (Module): On windows (cygwin) function asctime_r doesn't
! 173: exist so I changed back to asctime which exists.
! 174:
! 175: Revision 1.91 2003/06/25 15:30:29 brouard
! 176: * imach.c (Repository): Duplicated warning errors corrected.
! 177: (Repository): Elapsed time after each iteration is now output. It
! 178: helps to forecast when convergence will be reached. Elapsed time
! 179: is stamped in powell. We created a new html file for the graphs
! 180: concerning matrix of covariance. It has extension -cov.htm.
! 181:
! 182: Revision 1.90 2003/06/24 12:34:15 brouard
! 183: (Module): Some bugs corrected for windows. Also, when
! 184: mle=-1 a template is output in file "or"mypar.txt with the design
! 185: of the covariance matrix to be input.
! 186:
! 187: Revision 1.89 2003/06/24 12:30:52 brouard
! 188: (Module): Some bugs corrected for windows. Also, when
! 189: mle=-1 a template is output in file "or"mypar.txt with the design
! 190: of the covariance matrix to be input.
! 191:
! 192: Revision 1.88 2003/06/23 17:54:56 brouard
! 193: * imach.c (Repository): Create a sub-directory where all the secondary files are. Only imach, htm, gp and r(imach) are on the main directory. Correct time and other things.
! 194:
! 195: Revision 1.87 2003/06/18 12:26:01 brouard
! 196: Version 0.96
! 197:
! 198: Revision 1.86 2003/06/17 20:04:08 brouard
! 199: (Module): Change position of html and gnuplot routines and added
! 200: routine fileappend.
! 201:
! 202: Revision 1.85 2003/06/17 13:12:43 brouard
! 203: * imach.c (Repository): Check when date of death was earlier that
! 204: current date of interview. It may happen when the death was just
! 205: prior to the death. In this case, dh was negative and likelihood
! 206: was wrong (infinity). We still send an "Error" but patch by
! 207: assuming that the date of death was just one stepm after the
! 208: interview.
! 209: (Repository): Because some people have very long ID (first column)
! 210: we changed int to long in num[] and we added a new lvector for
! 211: memory allocation. But we also truncated to 8 characters (left
! 212: truncation)
! 213: (Repository): No more line truncation errors.
! 214:
! 215: Revision 1.84 2003/06/13 21:44:43 brouard
! 216: * imach.c (Repository): Replace "freqsummary" at a correct
! 217: place. It differs from routine "prevalence" which may be called
! 218: many times. Probs is memory consuming and must be used with
! 219: parcimony.
! 220: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
! 221:
! 222: Revision 1.83 2003/06/10 13:39:11 lievre
! 223: *** empty log message ***
! 224:
! 225: Revision 1.82 2003/06/05 15:57:20 brouard
! 226: Add log in imach.c and fullversion number is now printed.
! 227:
! 228: */
! 229: /*
! 230: Interpolated Markov Chain
! 231:
! 232: Short summary of the programme:
! 233:
! 234: This program computes Healthy Life Expectancies from
! 235: cross-longitudinal data. Cross-longitudinal data consist in: -1- a
! 236: first survey ("cross") where individuals from different ages are
! 237: interviewed on their health status or degree of disability (in the
! 238: case of a health survey which is our main interest) -2- at least a
! 239: second wave of interviews ("longitudinal") which measure each change
! 240: (if any) in individual health status. Health expectancies are
! 241: computed from the time spent in each health state according to a
! 242: model. More health states you consider, more time is necessary to reach the
! 243: Maximum Likelihood of the parameters involved in the model. The
! 244: simplest model is the multinomial logistic model where pij is the
! 245: probability to be observed in state j at the second wave
! 246: conditional to be observed in state i at the first wave. Therefore
! 247: the model is: log(pij/pii)= aij + bij*age+ cij*sex + etc , where
! 248: 'age' is age and 'sex' is a covariate. If you want to have a more
! 249: complex model than "constant and age", you should modify the program
! 250: where the markup *Covariates have to be included here again* invites
! 251: you to do it. More covariates you add, slower the
! 252: convergence.
! 253:
! 254: The advantage of this computer programme, compared to a simple
! 255: multinomial logistic model, is clear when the delay between waves is not
! 256: identical for each individual. Also, if a individual missed an
! 257: intermediate interview, the information is lost, but taken into
! 258: account using an interpolation or extrapolation.
! 259:
! 260: hPijx is the probability to be observed in state i at age x+h
! 261: conditional to the observed state i at age x. The delay 'h' can be
! 262: split into an exact number (nh*stepm) of unobserved intermediate
! 263: states. This elementary transition (by month, quarter,
! 264: semester or year) is modelled as a multinomial logistic. The hPx
! 265: matrix is simply the matrix product of nh*stepm elementary matrices
! 266: and the contribution of each individual to the likelihood is simply
! 267: hPijx.
! 268:
! 269: Also this programme outputs the covariance matrix of the parameters but also
! 270: of the life expectancies. It also computes the period (stable) prevalence.
! 271:
! 272: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
! 273: Institut national d'études démographiques, Paris.
! 274: This software have been partly granted by Euro-REVES, a concerted action
! 275: from the European Union.
! 276: It is copyrighted identically to a GNU software product, ie programme and
! 277: software can be distributed freely for non commercial use. Latest version
! 278: can be accessed at http://euroreves.ined.fr/imach .
! 279:
! 280: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
! 281: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
! 282:
! 283: **********************************************************************/
! 284: /*
! 285: main
! 286: read parameterfile
! 287: read datafile
! 288: concatwav
! 289: freqsummary
! 290: if (mle >= 1)
! 291: mlikeli
! 292: print results files
! 293: if mle==1
! 294: computes hessian
! 295: read end of parameter file: agemin, agemax, bage, fage, estepm
! 296: begin-prev-date,...
! 297: open gnuplot file
! 298: open html file
! 299: period (stable) prevalence
! 300: for age prevalim()
! 301: h Pij x
! 302: variance of p varprob
! 303: forecasting if prevfcast==1 prevforecast call prevalence()
! 304: health expectancies
! 305: Variance-covariance of DFLE
! 306: prevalence()
! 307: movingaverage()
! 308: varevsij()
! 309: if popbased==1 varevsij(,popbased)
! 310: total life expectancies
! 311: Variance of period (stable) prevalence
! 312: end
! 313: */
! 314:
! 315:
! 316:
! 317:
! 318: #include <math.h>
! 319: #include <stdio.h>
! 320: #include <stdlib.h>
! 321: #include <string.h>
! 322: #include <unistd.h>
! 323:
! 324: #include <limits.h>
! 325: #include <sys/types.h>
! 326: #include <sys/stat.h>
! 327: #include <errno.h>
! 328: extern int errno;
! 329:
! 330: /* #include <sys/time.h> */
! 331: #include <time.h>
! 332: #include "timeval.h"
! 333:
! 334: /* #include <libintl.h> */
! 335: /* #define _(String) gettext (String) */
! 336:
! 337: #define MAXLINE 256
! 338:
! 339: #define GNUPLOTPROGRAM "gnuplot"
! 340: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
! 341: #define FILENAMELENGTH 132
! 342:
! 343: #define GLOCK_ERROR_NOPATH -1 /* empty path */
! 344: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
! 345:
! 346: #define MAXPARM 30 /* Maximum number of parameters for the optimization */
! 347: #define NPARMAX 64 /* (nlstate+ndeath-1)*nlstate*ncovmodel */
! 348:
! 349: #define NINTERVMAX 8
! 350: #define NLSTATEMAX 8 /* Maximum number of live states (for func) */
! 351: #define NDEATHMAX 8 /* Maximum number of dead states (for func) */
! 352: #define NCOVMAX 8 /* Maximum number of covariates */
! 353: #define MAXN 20000
! 354: #define YEARM 12. /* Number of months per year */
! 355: #define AGESUP 130
! 356: #define AGEBASE 40
! 357: #define AGEGOMP 10. /* Minimal age for Gompertz adjustment */
! 358: #ifdef UNIX
! 359: #define DIRSEPARATOR '/'
! 360: #define CHARSEPARATOR "/"
! 361: #define ODIRSEPARATOR '\\'
! 362: #else
! 363: #define DIRSEPARATOR '\\'
! 364: #define CHARSEPARATOR "\\"
! 365: #define ODIRSEPARATOR '/'
! 366: #endif
! 367:
! 368: /* $Id: imach.c,v 1.125 2006/04/04 15:20:31 lievre Exp $ */
! 369: /* $State: Exp $ */
! 370:
! 371: char version[]="Imach version 0.98h, April 2006, INED-EUROREVES-Institut de longevite ";
! 372: char fullversion[]="$Revision: 1.125 $ $Date: 2006/04/04 15:20:31 $";
! 373: char strstart[80];
! 374: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
! 375: int erreur, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
! 376: int nvar;
! 377: int cptcovn=0, cptcovage=0, cptcoveff=0,cptcov;
! 378: int npar=NPARMAX;
! 379: int nlstate=2; /* Number of live states */
! 380: int ndeath=1; /* Number of dead states */
! 381: int ncovmodel, ncovcol; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
! 382: int popbased=0;
! 383:
! 384: int *wav; /* Number of waves for this individuual 0 is possible */
! 385: int maxwav; /* Maxim number of waves */
! 386: int jmin, jmax; /* min, max spacing between 2 waves */
! 387: int ijmin, ijmax; /* Individuals having jmin and jmax */
! 388: int gipmx, gsw; /* Global variables on the number of contributions
! 389: to the likelihood and the sum of weights (done by funcone)*/
! 390: int mle, weightopt;
! 391: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
! 392: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
! 393: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
! 394: * wave mi and wave mi+1 is not an exact multiple of stepm. */
! 395: double jmean; /* Mean space between 2 waves */
! 396: double **oldm, **newm, **savm; /* Working pointers to matrices */
! 397: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
! 398: FILE *fic,*ficpar, *ficparo,*ficres, *ficresp, *ficrespl, *ficrespij, *ficrest,*ficresf,*ficrespop;
! 399: FILE *ficlog, *ficrespow;
! 400: int globpr; /* Global variable for printing or not */
! 401: double fretone; /* Only one call to likelihood */
! 402: long ipmx; /* Number of contributions */
! 403: double sw; /* Sum of weights */
! 404: char filerespow[FILENAMELENGTH];
! 405: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
! 406: FILE *ficresilk;
! 407: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
! 408: FILE *ficresprobmorprev;
! 409: FILE *fichtm, *fichtmcov; /* Html File */
! 410: FILE *ficreseij;
! 411: char filerese[FILENAMELENGTH];
! 412: FILE *ficresstdeij;
! 413: char fileresstde[FILENAMELENGTH];
! 414: FILE *ficrescveij;
! 415: char filerescve[FILENAMELENGTH];
! 416: FILE *ficresvij;
! 417: char fileresv[FILENAMELENGTH];
! 418: FILE *ficresvpl;
! 419: char fileresvpl[FILENAMELENGTH];
! 420: char title[MAXLINE];
! 421: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH];
! 422: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
! 423: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
! 424: char command[FILENAMELENGTH];
! 425: int outcmd=0;
! 426:
! 427: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
! 428:
! 429: char filelog[FILENAMELENGTH]; /* Log file */
! 430: char filerest[FILENAMELENGTH];
! 431: char fileregp[FILENAMELENGTH];
! 432: char popfile[FILENAMELENGTH];
! 433:
! 434: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
! 435:
! 436: struct timeval start_time, end_time, curr_time, last_time, forecast_time;
! 437: struct timezone tzp;
! 438: extern int gettimeofday();
! 439: struct tm tmg, tm, tmf, *gmtime(), *localtime();
! 440: long time_value;
! 441: extern long time();
! 442: char strcurr[80], strfor[80];
! 443:
! 444: char *endptr;
! 445: long lval;
! 446: double dval;
! 447:
! 448: #define NR_END 1
! 449: #define FREE_ARG char*
! 450: #define FTOL 1.0e-10
! 451:
! 452: #define NRANSI
! 453: #define ITMAX 200
! 454:
! 455: #define TOL 2.0e-4
! 456:
! 457: #define CGOLD 0.3819660
! 458: #define ZEPS 1.0e-10
! 459: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
! 460:
! 461: #define GOLD 1.618034
! 462: #define GLIMIT 100.0
! 463: #define TINY 1.0e-20
! 464:
! 465: static double maxarg1,maxarg2;
! 466: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
! 467: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
! 468:
! 469: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
! 470: #define rint(a) floor(a+0.5)
! 471:
! 472: static double sqrarg;
! 473: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
! 474: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
! 475: int agegomp= AGEGOMP;
! 476:
! 477: int imx;
! 478: int stepm=1;
! 479: /* Stepm, step in month: minimum step interpolation*/
! 480:
! 481: int estepm;
! 482: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
! 483:
! 484: int m,nb;
! 485: long *num;
! 486: int firstpass=0, lastpass=4,*cod, *ncodemax, *Tage,*cens;
! 487: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
! 488: double **pmmij, ***probs;
! 489: double *ageexmed,*agecens;
! 490: double dateintmean=0;
! 491:
! 492: double *weight;
! 493: int **s; /* Status */
! 494: double *agedc, **covar, idx;
! 495: int **nbcode, *Tcode, *Tvar, **codtab, **Tvard, *Tprod, cptcovprod, *Tvaraff;
! 496: double *lsurv, *lpop, *tpop;
! 497:
! 498: double ftol=FTOL; /* Tolerance for computing Max Likelihood */
! 499: double ftolhess; /* Tolerance for computing hessian */
! 500:
! 501: /**************** split *************************/
! 502: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
! 503: {
! 504: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
! 505: the name of the file (name), its extension only (ext) and its first part of the name (finame)
! 506: */
! 507: char *ss; /* pointer */
! 508: int l1, l2; /* length counters */
! 509:
! 510: l1 = strlen(path ); /* length of path */
! 511: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
! 512: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
! 513: if ( ss == NULL ) { /* no directory, so determine current directory */
! 514: strcpy( name, path ); /* we got the fullname name because no directory */
! 515: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
! 516: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
! 517: /* get current working directory */
! 518: /* extern char* getcwd ( char *buf , int len);*/
! 519: if ( getcwd( dirc, FILENAME_MAX ) == NULL ) {
! 520: return( GLOCK_ERROR_GETCWD );
! 521: }
! 522: /* got dirc from getcwd*/
! 523: printf(" DIRC = %s \n",dirc);
! 524: } else { /* strip direcotry from path */
! 525: ss++; /* after this, the filename */
! 526: l2 = strlen( ss ); /* length of filename */
! 527: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
! 528: strcpy( name, ss ); /* save file name */
! 529: strncpy( dirc, path, l1 - l2 ); /* now the directory */
! 530: dirc[l1-l2] = 0; /* add zero */
! 531: printf(" DIRC2 = %s \n",dirc);
! 532: }
! 533: /* We add a separator at the end of dirc if not exists */
! 534: l1 = strlen( dirc ); /* length of directory */
! 535: if( dirc[l1-1] != DIRSEPARATOR ){
! 536: dirc[l1] = DIRSEPARATOR;
! 537: dirc[l1+1] = 0;
! 538: printf(" DIRC3 = %s \n",dirc);
! 539: }
! 540: ss = strrchr( name, '.' ); /* find last / */
! 541: if (ss >0){
! 542: ss++;
! 543: strcpy(ext,ss); /* save extension */
! 544: l1= strlen( name);
! 545: l2= strlen(ss)+1;
! 546: strncpy( finame, name, l1-l2);
! 547: finame[l1-l2]= 0;
! 548: }
! 549:
! 550: return( 0 ); /* we're done */
! 551: }
! 552:
! 553:
! 554: /******************************************/
! 555:
! 556: void replace_back_to_slash(char *s, char*t)
! 557: {
! 558: int i;
! 559: int lg=0;
! 560: i=0;
! 561: lg=strlen(t);
! 562: for(i=0; i<= lg; i++) {
! 563: (s[i] = t[i]);
! 564: if (t[i]== '\\') s[i]='/';
! 565: }
! 566: }
! 567:
! 568: int nbocc(char *s, char occ)
! 569: {
! 570: int i,j=0;
! 571: int lg=20;
! 572: i=0;
! 573: lg=strlen(s);
! 574: for(i=0; i<= lg; i++) {
! 575: if (s[i] == occ ) j++;
! 576: }
! 577: return j;
! 578: }
! 579:
! 580: void cutv(char *u,char *v, char*t, char occ)
! 581: {
! 582: /* cuts string t into u and v where u ends before first occurence of char 'occ'
! 583: and v starts after first occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2')
! 584: gives u="abcedf" and v="ghi2j" */
! 585: int i,lg,j,p=0;
! 586: i=0;
! 587: for(j=0; j<=strlen(t)-1; j++) {
! 588: if((t[j]!= occ) && (t[j+1]== occ)) p=j+1;
! 589: }
! 590:
! 591: lg=strlen(t);
! 592: for(j=0; j<p; j++) {
! 593: (u[j] = t[j]);
! 594: }
! 595: u[p]='\0';
! 596:
! 597: for(j=0; j<= lg; j++) {
! 598: if (j>=(p+1))(v[j-p-1] = t[j]);
! 599: }
! 600: }
! 601:
! 602: /********************** nrerror ********************/
! 603:
! 604: void nrerror(char error_text[])
! 605: {
! 606: fprintf(stderr,"ERREUR ...\n");
! 607: fprintf(stderr,"%s\n",error_text);
! 608: exit(EXIT_FAILURE);
! 609: }
! 610: /*********************** vector *******************/
! 611: double *vector(int nl, int nh)
! 612: {
! 613: double *v;
! 614: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
! 615: if (!v) nrerror("allocation failure in vector");
! 616: return v-nl+NR_END;
! 617: }
! 618:
! 619: /************************ free vector ******************/
! 620: void free_vector(double*v, int nl, int nh)
! 621: {
! 622: free((FREE_ARG)(v+nl-NR_END));
! 623: }
! 624:
! 625: /************************ivector *******************************/
! 626: int *ivector(long nl,long nh)
! 627: {
! 628: int *v;
! 629: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
! 630: if (!v) nrerror("allocation failure in ivector");
! 631: return v-nl+NR_END;
! 632: }
! 633:
! 634: /******************free ivector **************************/
! 635: void free_ivector(int *v, long nl, long nh)
! 636: {
! 637: free((FREE_ARG)(v+nl-NR_END));
! 638: }
! 639:
! 640: /************************lvector *******************************/
! 641: long *lvector(long nl,long nh)
! 642: {
! 643: long *v;
! 644: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
! 645: if (!v) nrerror("allocation failure in ivector");
! 646: return v-nl+NR_END;
! 647: }
! 648:
! 649: /******************free lvector **************************/
! 650: void free_lvector(long *v, long nl, long nh)
! 651: {
! 652: free((FREE_ARG)(v+nl-NR_END));
! 653: }
! 654:
! 655: /******************* imatrix *******************************/
! 656: int **imatrix(long nrl, long nrh, long ncl, long nch)
! 657: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
! 658: {
! 659: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
! 660: int **m;
! 661:
! 662: /* allocate pointers to rows */
! 663: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
! 664: if (!m) nrerror("allocation failure 1 in matrix()");
! 665: m += NR_END;
! 666: m -= nrl;
! 667:
! 668:
! 669: /* allocate rows and set pointers to them */
! 670: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
! 671: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
! 672: m[nrl] += NR_END;
! 673: m[nrl] -= ncl;
! 674:
! 675: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
! 676:
! 677: /* return pointer to array of pointers to rows */
! 678: return m;
! 679: }
! 680:
! 681: /****************** free_imatrix *************************/
! 682: void free_imatrix(m,nrl,nrh,ncl,nch)
! 683: int **m;
! 684: long nch,ncl,nrh,nrl;
! 685: /* free an int matrix allocated by imatrix() */
! 686: {
! 687: free((FREE_ARG) (m[nrl]+ncl-NR_END));
! 688: free((FREE_ARG) (m+nrl-NR_END));
! 689: }
! 690:
! 691: /******************* matrix *******************************/
! 692: double **matrix(long nrl, long nrh, long ncl, long nch)
! 693: {
! 694: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
! 695: double **m;
! 696:
! 697: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
! 698: if (!m) nrerror("allocation failure 1 in matrix()");
! 699: m += NR_END;
! 700: m -= nrl;
! 701:
! 702: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
! 703: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
! 704: m[nrl] += NR_END;
! 705: m[nrl] -= ncl;
! 706:
! 707: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
! 708: return m;
! 709: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1])
! 710: */
! 711: }
! 712:
! 713: /*************************free matrix ************************/
! 714: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
! 715: {
! 716: free((FREE_ARG)(m[nrl]+ncl-NR_END));
! 717: free((FREE_ARG)(m+nrl-NR_END));
! 718: }
! 719:
! 720: /******************* ma3x *******************************/
! 721: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
! 722: {
! 723: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
! 724: double ***m;
! 725:
! 726: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
! 727: if (!m) nrerror("allocation failure 1 in matrix()");
! 728: m += NR_END;
! 729: m -= nrl;
! 730:
! 731: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
! 732: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
! 733: m[nrl] += NR_END;
! 734: m[nrl] -= ncl;
! 735:
! 736: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
! 737:
! 738: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
! 739: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
! 740: m[nrl][ncl] += NR_END;
! 741: m[nrl][ncl] -= nll;
! 742: for (j=ncl+1; j<=nch; j++)
! 743: m[nrl][j]=m[nrl][j-1]+nlay;
! 744:
! 745: for (i=nrl+1; i<=nrh; i++) {
! 746: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
! 747: for (j=ncl+1; j<=nch; j++)
! 748: m[i][j]=m[i][j-1]+nlay;
! 749: }
! 750: return m;
! 751: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
! 752: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
! 753: */
! 754: }
! 755:
! 756: /*************************free ma3x ************************/
! 757: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
! 758: {
! 759: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
! 760: free((FREE_ARG)(m[nrl]+ncl-NR_END));
! 761: free((FREE_ARG)(m+nrl-NR_END));
! 762: }
! 763:
! 764: /*************** function subdirf ***********/
! 765: char *subdirf(char fileres[])
! 766: {
! 767: /* Caution optionfilefiname is hidden */
! 768: strcpy(tmpout,optionfilefiname);
! 769: strcat(tmpout,"/"); /* Add to the right */
! 770: strcat(tmpout,fileres);
! 771: return tmpout;
! 772: }
! 773:
! 774: /*************** function subdirf2 ***********/
! 775: char *subdirf2(char fileres[], char *preop)
! 776: {
! 777:
! 778: /* Caution optionfilefiname is hidden */
! 779: strcpy(tmpout,optionfilefiname);
! 780: strcat(tmpout,"/");
! 781: strcat(tmpout,preop);
! 782: strcat(tmpout,fileres);
! 783: return tmpout;
! 784: }
! 785:
! 786: /*************** function subdirf3 ***********/
! 787: char *subdirf3(char fileres[], char *preop, char *preop2)
! 788: {
! 789:
! 790: /* Caution optionfilefiname is hidden */
! 791: strcpy(tmpout,optionfilefiname);
! 792: strcat(tmpout,"/");
! 793: strcat(tmpout,preop);
! 794: strcat(tmpout,preop2);
! 795: strcat(tmpout,fileres);
! 796: return tmpout;
! 797: }
! 798:
! 799: /***************** f1dim *************************/
! 800: extern int ncom;
! 801: extern double *pcom,*xicom;
! 802: extern double (*nrfunc)(double []);
! 803:
! 804: double f1dim(double x)
! 805: {
! 806: int j;
! 807: double f;
! 808: double *xt;
! 809:
! 810: xt=vector(1,ncom);
! 811: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
! 812: f=(*nrfunc)(xt);
! 813: free_vector(xt,1,ncom);
! 814: return f;
! 815: }
! 816:
! 817: /*****************brent *************************/
! 818: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
! 819: {
! 820: int iter;
! 821: double a,b,d,etemp;
! 822: double fu,fv,fw,fx;
! 823: double ftemp;
! 824: double p,q,r,tol1,tol2,u,v,w,x,xm;
! 825: double e=0.0;
! 826:
! 827: a=(ax < cx ? ax : cx);
! 828: b=(ax > cx ? ax : cx);
! 829: x=w=v=bx;
! 830: fw=fv=fx=(*f)(x);
! 831: for (iter=1;iter<=ITMAX;iter++) {
! 832: xm=0.5*(a+b);
! 833: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
! 834: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
! 835: printf(".");fflush(stdout);
! 836: fprintf(ficlog,".");fflush(ficlog);
! 837: #ifdef DEBUG
! 838: printf("br %d,x=%.10e xm=%.10e b=%.10e a=%.10e tol=%.10e tol1=%.10e tol2=%.10e x-xm=%.10e fx=%.12e fu=%.12e,fw=%.12e,ftemp=%.12e,ftol=%.12e\n",iter,x,xm,b,a,tol,tol1,tol2,(x-xm),fx,fu,fw,ftemp,ftol);
! 839: fprintf(ficlog,"br %d,x=%.10e xm=%.10e b=%.10e a=%.10e tol=%.10e tol1=%.10e tol2=%.10e x-xm=%.10e fx=%.12e fu=%.12e,fw=%.12e,ftemp=%.12e,ftol=%.12e\n",iter,x,xm,b,a,tol,tol1,tol2,(x-xm),fx,fu,fw,ftemp,ftol);
! 840: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
! 841: #endif
! 842: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
! 843: *xmin=x;
! 844: return fx;
! 845: }
! 846: ftemp=fu;
! 847: if (fabs(e) > tol1) {
! 848: r=(x-w)*(fx-fv);
! 849: q=(x-v)*(fx-fw);
! 850: p=(x-v)*q-(x-w)*r;
! 851: q=2.0*(q-r);
! 852: if (q > 0.0) p = -p;
! 853: q=fabs(q);
! 854: etemp=e;
! 855: e=d;
! 856: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
! 857: d=CGOLD*(e=(x >= xm ? a-x : b-x));
! 858: else {
! 859: d=p/q;
! 860: u=x+d;
! 861: if (u-a < tol2 || b-u < tol2)
! 862: d=SIGN(tol1,xm-x);
! 863: }
! 864: } else {
! 865: d=CGOLD*(e=(x >= xm ? a-x : b-x));
! 866: }
! 867: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
! 868: fu=(*f)(u);
! 869: if (fu <= fx) {
! 870: if (u >= x) a=x; else b=x;
! 871: SHFT(v,w,x,u)
! 872: SHFT(fv,fw,fx,fu)
! 873: } else {
! 874: if (u < x) a=u; else b=u;
! 875: if (fu <= fw || w == x) {
! 876: v=w;
! 877: w=u;
! 878: fv=fw;
! 879: fw=fu;
! 880: } else if (fu <= fv || v == x || v == w) {
! 881: v=u;
! 882: fv=fu;
! 883: }
! 884: }
! 885: }
! 886: nrerror("Too many iterations in brent");
! 887: *xmin=x;
! 888: return fx;
! 889: }
! 890:
! 891: /****************** mnbrak ***********************/
! 892:
! 893: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
! 894: double (*func)(double))
! 895: {
! 896: double ulim,u,r,q, dum;
! 897: double fu;
! 898:
! 899: *fa=(*func)(*ax);
! 900: *fb=(*func)(*bx);
! 901: if (*fb > *fa) {
! 902: SHFT(dum,*ax,*bx,dum)
! 903: SHFT(dum,*fb,*fa,dum)
! 904: }
! 905: *cx=(*bx)+GOLD*(*bx-*ax);
! 906: *fc=(*func)(*cx);
! 907: while (*fb > *fc) {
! 908: r=(*bx-*ax)*(*fb-*fc);
! 909: q=(*bx-*cx)*(*fb-*fa);
! 910: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
! 911: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r));
! 912: ulim=(*bx)+GLIMIT*(*cx-*bx);
! 913: if ((*bx-u)*(u-*cx) > 0.0) {
! 914: fu=(*func)(u);
! 915: } else if ((*cx-u)*(u-ulim) > 0.0) {
! 916: fu=(*func)(u);
! 917: if (fu < *fc) {
! 918: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
! 919: SHFT(*fb,*fc,fu,(*func)(u))
! 920: }
! 921: } else if ((u-ulim)*(ulim-*cx) >= 0.0) {
! 922: u=ulim;
! 923: fu=(*func)(u);
! 924: } else {
! 925: u=(*cx)+GOLD*(*cx-*bx);
! 926: fu=(*func)(u);
! 927: }
! 928: SHFT(*ax,*bx,*cx,u)
! 929: SHFT(*fa,*fb,*fc,fu)
! 930: }
! 931: }
! 932:
! 933: /*************** linmin ************************/
! 934:
! 935: int ncom;
! 936: double *pcom,*xicom;
! 937: double (*nrfunc)(double []);
! 938:
! 939: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
! 940: {
! 941: double brent(double ax, double bx, double cx,
! 942: double (*f)(double), double tol, double *xmin);
! 943: double f1dim(double x);
! 944: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
! 945: double *fc, double (*func)(double));
! 946: int j;
! 947: double xx,xmin,bx,ax;
! 948: double fx,fb,fa;
! 949:
! 950: ncom=n;
! 951: pcom=vector(1,n);
! 952: xicom=vector(1,n);
! 953: nrfunc=func;
! 954: for (j=1;j<=n;j++) {
! 955: pcom[j]=p[j];
! 956: xicom[j]=xi[j];
! 957: }
! 958: ax=0.0;
! 959: xx=1.0;
! 960: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim);
! 961: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin);
! 962: #ifdef DEBUG
! 963: printf("retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
! 964: fprintf(ficlog,"retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
! 965: #endif
! 966: for (j=1;j<=n;j++) {
! 967: xi[j] *= xmin;
! 968: p[j] += xi[j];
! 969: }
! 970: free_vector(xicom,1,n);
! 971: free_vector(pcom,1,n);
! 972: }
! 973:
! 974: char *asc_diff_time(long time_sec, char ascdiff[])
! 975: {
! 976: long sec_left, days, hours, minutes;
! 977: days = (time_sec) / (60*60*24);
! 978: sec_left = (time_sec) % (60*60*24);
! 979: hours = (sec_left) / (60*60) ;
! 980: sec_left = (sec_left) %(60*60);
! 981: minutes = (sec_left) /60;
! 982: sec_left = (sec_left) % (60);
! 983: sprintf(ascdiff,"%d day(s) %d hour(s) %d minute(s) %d second(s)",days, hours, minutes, sec_left);
! 984: return ascdiff;
! 985: }
! 986:
! 987: /*************** powell ************************/
! 988: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
! 989: double (*func)(double []))
! 990: {
! 991: void linmin(double p[], double xi[], int n, double *fret,
! 992: double (*func)(double []));
! 993: int i,ibig,j;
! 994: double del,t,*pt,*ptt,*xit;
! 995: double fp,fptt;
! 996: double *xits;
! 997: int niterf, itmp;
! 998:
! 999: pt=vector(1,n);
! 1000: ptt=vector(1,n);
! 1001: xit=vector(1,n);
! 1002: xits=vector(1,n);
! 1003: *fret=(*func)(p);
! 1004: for (j=1;j<=n;j++) pt[j]=p[j];
! 1005: for (*iter=1;;++(*iter)) {
! 1006: fp=(*fret);
! 1007: ibig=0;
! 1008: del=0.0;
! 1009: last_time=curr_time;
! 1010: (void) gettimeofday(&curr_time,&tzp);
! 1011: printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, curr_time.tv_sec-last_time.tv_sec, curr_time.tv_sec-start_time.tv_sec);fflush(stdout);
! 1012: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, curr_time.tv_sec-last_time.tv_sec, curr_time.tv_sec-start_time.tv_sec); fflush(ficlog);
! 1013: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tv_sec-start_time.tv_sec); */
! 1014: for (i=1;i<=n;i++) {
! 1015: printf(" %d %.12f",i, p[i]);
! 1016: fprintf(ficlog," %d %.12lf",i, p[i]);
! 1017: fprintf(ficrespow," %.12lf", p[i]);
! 1018: }
! 1019: printf("\n");
! 1020: fprintf(ficlog,"\n");
! 1021: fprintf(ficrespow,"\n");fflush(ficrespow);
! 1022: if(*iter <=3){
! 1023: tm = *localtime(&curr_time.tv_sec);
! 1024: strcpy(strcurr,asctime(&tm));
! 1025: /* asctime_r(&tm,strcurr); */
! 1026: forecast_time=curr_time;
! 1027: itmp = strlen(strcurr);
! 1028: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
! 1029: strcurr[itmp-1]='\0';
! 1030: printf("\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,curr_time.tv_sec-last_time.tv_sec);
! 1031: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,curr_time.tv_sec-last_time.tv_sec);
! 1032: for(niterf=10;niterf<=30;niterf+=10){
! 1033: forecast_time.tv_sec=curr_time.tv_sec+(niterf-*iter)*(curr_time.tv_sec-last_time.tv_sec);
! 1034: tmf = *localtime(&forecast_time.tv_sec);
! 1035: /* asctime_r(&tmf,strfor); */
! 1036: strcpy(strfor,asctime(&tmf));
! 1037: itmp = strlen(strfor);
! 1038: if(strfor[itmp-1]=='\n')
! 1039: strfor[itmp-1]='\0';
! 1040: printf(" - if your program needs %d iterations to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",niterf, asc_diff_time(forecast_time.tv_sec-curr_time.tv_sec,tmpout),strfor,strcurr);
! 1041: fprintf(ficlog," - if your program needs %d iterations to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",niterf, asc_diff_time(forecast_time.tv_sec-curr_time.tv_sec,tmpout),strfor,strcurr);
! 1042: }
! 1043: }
! 1044: for (i=1;i<=n;i++) {
! 1045: for (j=1;j<=n;j++) xit[j]=xi[j][i];
! 1046: fptt=(*fret);
! 1047: #ifdef DEBUG
! 1048: printf("fret=%lf \n",*fret);
! 1049: fprintf(ficlog,"fret=%lf \n",*fret);
! 1050: #endif
! 1051: printf("%d",i);fflush(stdout);
! 1052: fprintf(ficlog,"%d",i);fflush(ficlog);
! 1053: linmin(p,xit,n,fret,func);
! 1054: if (fabs(fptt-(*fret)) > del) {
! 1055: del=fabs(fptt-(*fret));
! 1056: ibig=i;
! 1057: }
! 1058: #ifdef DEBUG
! 1059: printf("%d %.12e",i,(*fret));
! 1060: fprintf(ficlog,"%d %.12e",i,(*fret));
! 1061: for (j=1;j<=n;j++) {
! 1062: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
! 1063: printf(" x(%d)=%.12e",j,xit[j]);
! 1064: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
! 1065: }
! 1066: for(j=1;j<=n;j++) {
! 1067: printf(" p=%.12e",p[j]);
! 1068: fprintf(ficlog," p=%.12e",p[j]);
! 1069: }
! 1070: printf("\n");
! 1071: fprintf(ficlog,"\n");
! 1072: #endif
! 1073: }
! 1074: if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) {
! 1075: #ifdef DEBUG
! 1076: int k[2],l;
! 1077: k[0]=1;
! 1078: k[1]=-1;
! 1079: printf("Max: %.12e",(*func)(p));
! 1080: fprintf(ficlog,"Max: %.12e",(*func)(p));
! 1081: for (j=1;j<=n;j++) {
! 1082: printf(" %.12e",p[j]);
! 1083: fprintf(ficlog," %.12e",p[j]);
! 1084: }
! 1085: printf("\n");
! 1086: fprintf(ficlog,"\n");
! 1087: for(l=0;l<=1;l++) {
! 1088: for (j=1;j<=n;j++) {
! 1089: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
! 1090: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
! 1091: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
! 1092: }
! 1093: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
! 1094: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
! 1095: }
! 1096: #endif
! 1097:
! 1098:
! 1099: free_vector(xit,1,n);
! 1100: free_vector(xits,1,n);
! 1101: free_vector(ptt,1,n);
! 1102: free_vector(pt,1,n);
! 1103: return;
! 1104: }
! 1105: if (*iter == ITMAX) nrerror("powell exceeding maximum iterations.");
! 1106: for (j=1;j<=n;j++) {
! 1107: ptt[j]=2.0*p[j]-pt[j];
! 1108: xit[j]=p[j]-pt[j];
! 1109: pt[j]=p[j];
! 1110: }
! 1111: fptt=(*func)(ptt);
! 1112: if (fptt < fp) {
! 1113: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt);
! 1114: if (t < 0.0) {
! 1115: linmin(p,xit,n,fret,func);
! 1116: for (j=1;j<=n;j++) {
! 1117: xi[j][ibig]=xi[j][n];
! 1118: xi[j][n]=xit[j];
! 1119: }
! 1120: #ifdef DEBUG
! 1121: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
! 1122: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
! 1123: for(j=1;j<=n;j++){
! 1124: printf(" %.12e",xit[j]);
! 1125: fprintf(ficlog," %.12e",xit[j]);
! 1126: }
! 1127: printf("\n");
! 1128: fprintf(ficlog,"\n");
! 1129: #endif
! 1130: }
! 1131: }
! 1132: }
! 1133: }
! 1134:
! 1135: /**** Prevalence limit (stable or period prevalence) ****************/
! 1136:
! 1137: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int ij)
! 1138: {
! 1139: /* Computes the prevalence limit in each live state at age x by left multiplying the unit
! 1140: matrix by transitions matrix until convergence is reached */
! 1141:
! 1142: int i, ii,j,k;
! 1143: double min, max, maxmin, maxmax,sumnew=0.;
! 1144: double **matprod2();
! 1145: double **out, cov[NCOVMAX], **pmij();
! 1146: double **newm;
! 1147: double agefin, delaymax=50 ; /* Max number of years to converge */
! 1148:
! 1149: for (ii=1;ii<=nlstate+ndeath;ii++)
! 1150: for (j=1;j<=nlstate+ndeath;j++){
! 1151: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 1152: }
! 1153:
! 1154: cov[1]=1.;
! 1155:
! 1156: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
! 1157: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
! 1158: newm=savm;
! 1159: /* Covariates have to be included here again */
! 1160: cov[2]=agefin;
! 1161:
! 1162: for (k=1; k<=cptcovn;k++) {
! 1163: cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];
! 1164: /* printf("ij=%d k=%d Tvar[k]=%d nbcode=%d cov=%lf codtab[ij][Tvar[k]]=%d \n",ij,k, Tvar[k],nbcode[Tvar[k]][codtab[ij][Tvar[k]]],cov[2+k], codtab[ij][Tvar[k]]);*/
! 1165: }
! 1166: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
! 1167: for (k=1; k<=cptcovprod;k++)
! 1168: cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];
! 1169:
! 1170: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
! 1171: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
! 1172: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
! 1173: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
! 1174:
! 1175: savm=oldm;
! 1176: oldm=newm;
! 1177: maxmax=0.;
! 1178: for(j=1;j<=nlstate;j++){
! 1179: min=1.;
! 1180: max=0.;
! 1181: for(i=1; i<=nlstate; i++) {
! 1182: sumnew=0;
! 1183: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
! 1184: prlim[i][j]= newm[i][j]/(1-sumnew);
! 1185: max=FMAX(max,prlim[i][j]);
! 1186: min=FMIN(min,prlim[i][j]);
! 1187: }
! 1188: maxmin=max-min;
! 1189: maxmax=FMAX(maxmax,maxmin);
! 1190: }
! 1191: if(maxmax < ftolpl){
! 1192: return prlim;
! 1193: }
! 1194: }
! 1195: }
! 1196:
! 1197: /*************** transition probabilities ***************/
! 1198:
! 1199: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
! 1200: {
! 1201: double s1, s2;
! 1202: /*double t34;*/
! 1203: int i,j,j1, nc, ii, jj;
! 1204:
! 1205: for(i=1; i<= nlstate; i++){
! 1206: for(j=1; j<i;j++){
! 1207: for (nc=1, s2=0.;nc <=ncovmodel; nc++){
! 1208: /*s2 += param[i][j][nc]*cov[nc];*/
! 1209: s2 += x[(i-1)*nlstate*ncovmodel+(j-1)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];
! 1210: /* printf("Int j<i s1=%.17e, s2=%.17e\n",s1,s2); */
! 1211: }
! 1212: ps[i][j]=s2;
! 1213: /* printf("s1=%.17e, s2=%.17e\n",s1,s2); */
! 1214: }
! 1215: for(j=i+1; j<=nlstate+ndeath;j++){
! 1216: for (nc=1, s2=0.;nc <=ncovmodel; nc++){
! 1217: s2 += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];
! 1218: /* printf("Int j>i s1=%.17e, s2=%.17e %lx %lx\n",s1,s2,s1,s2); */
! 1219: }
! 1220: ps[i][j]=s2;
! 1221: }
! 1222: }
! 1223: /*ps[3][2]=1;*/
! 1224:
! 1225: for(i=1; i<= nlstate; i++){
! 1226: s1=0;
! 1227: for(j=1; j<i; j++)
! 1228: s1+=exp(ps[i][j]);
! 1229: for(j=i+1; j<=nlstate+ndeath; j++)
! 1230: s1+=exp(ps[i][j]);
! 1231: ps[i][i]=1./(s1+1.);
! 1232: for(j=1; j<i; j++)
! 1233: ps[i][j]= exp(ps[i][j])*ps[i][i];
! 1234: for(j=i+1; j<=nlstate+ndeath; j++)
! 1235: ps[i][j]= exp(ps[i][j])*ps[i][i];
! 1236: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
! 1237: } /* end i */
! 1238:
! 1239: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
! 1240: for(jj=1; jj<= nlstate+ndeath; jj++){
! 1241: ps[ii][jj]=0;
! 1242: ps[ii][ii]=1;
! 1243: }
! 1244: }
! 1245:
! 1246:
! 1247: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
! 1248: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
! 1249: /* printf("ddd %lf ",ps[ii][jj]); */
! 1250: /* } */
! 1251: /* printf("\n "); */
! 1252: /* } */
! 1253: /* printf("\n ");printf("%lf ",cov[2]); */
! 1254: /*
! 1255: for(i=1; i<= npar; i++) printf("%f ",x[i]);
! 1256: goto end;*/
! 1257: return ps;
! 1258: }
! 1259:
! 1260: /**************** Product of 2 matrices ******************/
! 1261:
! 1262: double **matprod2(double **out, double **in,long nrl, long nrh, long ncl, long nch, long ncolol, long ncoloh, double **b)
! 1263: {
! 1264: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
! 1265: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
! 1266: /* in, b, out are matrice of pointers which should have been initialized
! 1267: before: only the contents of out is modified. The function returns
! 1268: a pointer to pointers identical to out */
! 1269: long i, j, k;
! 1270: for(i=nrl; i<= nrh; i++)
! 1271: for(k=ncolol; k<=ncoloh; k++)
! 1272: for(j=ncl,out[i][k]=0.; j<=nch; j++)
! 1273: out[i][k] +=in[i][j]*b[j][k];
! 1274:
! 1275: return out;
! 1276: }
! 1277:
! 1278:
! 1279: /************* Higher Matrix Product ***************/
! 1280:
! 1281: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij )
! 1282: {
! 1283: /* Computes the transition matrix starting at age 'age' over
! 1284: 'nhstepm*hstepm*stepm' months (i.e. until
! 1285: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
! 1286: nhstepm*hstepm matrices.
! 1287: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
! 1288: (typically every 2 years instead of every month which is too big
! 1289: for the memory).
! 1290: Model is determined by parameters x and covariates have to be
! 1291: included manually here.
! 1292:
! 1293: */
! 1294:
! 1295: int i, j, d, h, k;
! 1296: double **out, cov[NCOVMAX];
! 1297: double **newm;
! 1298:
! 1299: /* Hstepm could be zero and should return the unit matrix */
! 1300: for (i=1;i<=nlstate+ndeath;i++)
! 1301: for (j=1;j<=nlstate+ndeath;j++){
! 1302: oldm[i][j]=(i==j ? 1.0 : 0.0);
! 1303: po[i][j][0]=(i==j ? 1.0 : 0.0);
! 1304: }
! 1305: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
! 1306: for(h=1; h <=nhstepm; h++){
! 1307: for(d=1; d <=hstepm; d++){
! 1308: newm=savm;
! 1309: /* Covariates have to be included here again */
! 1310: cov[1]=1.;
! 1311: cov[2]=age+((h-1)*hstepm + (d-1))*stepm/YEARM;
! 1312: for (k=1; k<=cptcovn;k++) cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];
! 1313: for (k=1; k<=cptcovage;k++)
! 1314: cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
! 1315: for (k=1; k<=cptcovprod;k++)
! 1316: cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];
! 1317:
! 1318:
! 1319: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
! 1320: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
! 1321: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
! 1322: pmij(pmmij,cov,ncovmodel,x,nlstate));
! 1323: savm=oldm;
! 1324: oldm=newm;
! 1325: }
! 1326: for(i=1; i<=nlstate+ndeath; i++)
! 1327: for(j=1;j<=nlstate+ndeath;j++) {
! 1328: po[i][j][h]=newm[i][j];
! 1329: /*printf("i=%d j=%d h=%d po[i][j][h]=%f ",i,j,h,po[i][j][h]);
! 1330: */
! 1331: }
! 1332: } /* end h */
! 1333: return po;
! 1334: }
! 1335:
! 1336:
! 1337: /*************** log-likelihood *************/
! 1338: double func( double *x)
! 1339: {
! 1340: int i, ii, j, k, mi, d, kk;
! 1341: double l, ll[NLSTATEMAX], cov[NCOVMAX];
! 1342: double **out;
! 1343: double sw; /* Sum of weights */
! 1344: double lli; /* Individual log likelihood */
! 1345: int s1, s2;
! 1346: double bbh, survp;
! 1347: long ipmx;
! 1348: /*extern weight */
! 1349: /* We are differentiating ll according to initial status */
! 1350: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
! 1351: /*for(i=1;i<imx;i++)
! 1352: printf(" %d\n",s[4][i]);
! 1353: */
! 1354: cov[1]=1.;
! 1355:
! 1356: for(k=1; k<=nlstate; k++) ll[k]=0.;
! 1357:
! 1358: if(mle==1){
! 1359: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
! 1360: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
! 1361: for(mi=1; mi<= wav[i]-1; mi++){
! 1362: for (ii=1;ii<=nlstate+ndeath;ii++)
! 1363: for (j=1;j<=nlstate+ndeath;j++){
! 1364: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 1365: savm[ii][j]=(ii==j ? 1.0 : 0.0);
! 1366: }
! 1367: for(d=0; d<dh[mi][i]; d++){
! 1368: newm=savm;
! 1369: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
! 1370: for (kk=1; kk<=cptcovage;kk++) {
! 1371: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
! 1372: }
! 1373: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
! 1374: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
! 1375: savm=oldm;
! 1376: oldm=newm;
! 1377: } /* end mult */
! 1378:
! 1379: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
! 1380: /* But now since version 0.9 we anticipate for bias at large stepm.
! 1381: * If stepm is larger than one month (smallest stepm) and if the exact delay
! 1382: * (in months) between two waves is not a multiple of stepm, we rounded to
! 1383: * the nearest (and in case of equal distance, to the lowest) interval but now
! 1384: * we keep into memory the bias bh[mi][i] and also the previous matrix product
! 1385: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
! 1386: * probability in order to take into account the bias as a fraction of the way
! 1387: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
! 1388: * -stepm/2 to stepm/2 .
! 1389: * For stepm=1 the results are the same as for previous versions of Imach.
! 1390: * For stepm > 1 the results are less biased than in previous versions.
! 1391: */
! 1392: s1=s[mw[mi][i]][i];
! 1393: s2=s[mw[mi+1][i]][i];
! 1394: bbh=(double)bh[mi][i]/(double)stepm;
! 1395: /* bias bh is positive if real duration
! 1396: * is higher than the multiple of stepm and negative otherwise.
! 1397: */
! 1398: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
! 1399: if( s2 > nlstate){
! 1400: /* i.e. if s2 is a death state and if the date of death is known
! 1401: then the contribution to the likelihood is the probability to
! 1402: die between last step unit time and current step unit time,
! 1403: which is also equal to probability to die before dh
! 1404: minus probability to die before dh-stepm .
! 1405: In version up to 0.92 likelihood was computed
! 1406: as if date of death was unknown. Death was treated as any other
! 1407: health state: the date of the interview describes the actual state
! 1408: and not the date of a change in health state. The former idea was
! 1409: to consider that at each interview the state was recorded
! 1410: (healthy, disable or death) and IMaCh was corrected; but when we
! 1411: introduced the exact date of death then we should have modified
! 1412: the contribution of an exact death to the likelihood. This new
! 1413: contribution is smaller and very dependent of the step unit
! 1414: stepm. It is no more the probability to die between last interview
! 1415: and month of death but the probability to survive from last
! 1416: interview up to one month before death multiplied by the
! 1417: probability to die within a month. Thanks to Chris
! 1418: Jackson for correcting this bug. Former versions increased
! 1419: mortality artificially. The bad side is that we add another loop
! 1420: which slows down the processing. The difference can be up to 10%
! 1421: lower mortality.
! 1422: */
! 1423: lli=log(out[s1][s2] - savm[s1][s2]);
! 1424:
! 1425:
! 1426: } else if (s2==-2) {
! 1427: for (j=1,survp=0. ; j<=nlstate; j++)
! 1428: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
! 1429: /*survp += out[s1][j]; */
! 1430: lli= log(survp);
! 1431: }
! 1432:
! 1433: else if (s2==-4) {
! 1434: for (j=3,survp=0. ; j<=nlstate; j++)
! 1435: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
! 1436: lli= log(survp);
! 1437: }
! 1438:
! 1439: else if (s2==-5) {
! 1440: for (j=1,survp=0. ; j<=2; j++)
! 1441: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
! 1442: lli= log(survp);
! 1443: }
! 1444:
! 1445: else{
! 1446: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
! 1447: /* lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*(savm[s1][s2])):log((1.+bbh)*out[s1][s2]));*/ /* linear interpolation */
! 1448: }
! 1449: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
! 1450: /*if(lli ==000.0)*/
! 1451: /*printf("bbh= %f lli=%f savm=%f out=%f %d\n",bbh,lli,savm[s1][s2], out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]],i); */
! 1452: ipmx +=1;
! 1453: sw += weight[i];
! 1454: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
! 1455: } /* end of wave */
! 1456: } /* end of individual */
! 1457: } else if(mle==2){
! 1458: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
! 1459: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
! 1460: for(mi=1; mi<= wav[i]-1; mi++){
! 1461: for (ii=1;ii<=nlstate+ndeath;ii++)
! 1462: for (j=1;j<=nlstate+ndeath;j++){
! 1463: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 1464: savm[ii][j]=(ii==j ? 1.0 : 0.0);
! 1465: }
! 1466: for(d=0; d<=dh[mi][i]; d++){
! 1467: newm=savm;
! 1468: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
! 1469: for (kk=1; kk<=cptcovage;kk++) {
! 1470: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
! 1471: }
! 1472: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
! 1473: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
! 1474: savm=oldm;
! 1475: oldm=newm;
! 1476: } /* end mult */
! 1477:
! 1478: s1=s[mw[mi][i]][i];
! 1479: s2=s[mw[mi+1][i]][i];
! 1480: bbh=(double)bh[mi][i]/(double)stepm;
! 1481: lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*(savm[s1][s2])):log((1.+bbh)*out[s1][s2])); /* linear interpolation */
! 1482: ipmx +=1;
! 1483: sw += weight[i];
! 1484: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
! 1485: } /* end of wave */
! 1486: } /* end of individual */
! 1487: } else if(mle==3){ /* exponential inter-extrapolation */
! 1488: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
! 1489: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
! 1490: for(mi=1; mi<= wav[i]-1; mi++){
! 1491: for (ii=1;ii<=nlstate+ndeath;ii++)
! 1492: for (j=1;j<=nlstate+ndeath;j++){
! 1493: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 1494: savm[ii][j]=(ii==j ? 1.0 : 0.0);
! 1495: }
! 1496: for(d=0; d<dh[mi][i]; d++){
! 1497: newm=savm;
! 1498: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
! 1499: for (kk=1; kk<=cptcovage;kk++) {
! 1500: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
! 1501: }
! 1502: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
! 1503: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
! 1504: savm=oldm;
! 1505: oldm=newm;
! 1506: } /* end mult */
! 1507:
! 1508: s1=s[mw[mi][i]][i];
! 1509: s2=s[mw[mi+1][i]][i];
! 1510: bbh=(double)bh[mi][i]/(double)stepm;
! 1511: lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* exponential inter-extrapolation */
! 1512: ipmx +=1;
! 1513: sw += weight[i];
! 1514: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
! 1515: } /* end of wave */
! 1516: } /* end of individual */
! 1517: }else if (mle==4){ /* ml=4 no inter-extrapolation */
! 1518: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
! 1519: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
! 1520: for(mi=1; mi<= wav[i]-1; mi++){
! 1521: for (ii=1;ii<=nlstate+ndeath;ii++)
! 1522: for (j=1;j<=nlstate+ndeath;j++){
! 1523: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 1524: savm[ii][j]=(ii==j ? 1.0 : 0.0);
! 1525: }
! 1526: for(d=0; d<dh[mi][i]; d++){
! 1527: newm=savm;
! 1528: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
! 1529: for (kk=1; kk<=cptcovage;kk++) {
! 1530: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
! 1531: }
! 1532:
! 1533: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
! 1534: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
! 1535: savm=oldm;
! 1536: oldm=newm;
! 1537: } /* end mult */
! 1538:
! 1539: s1=s[mw[mi][i]][i];
! 1540: s2=s[mw[mi+1][i]][i];
! 1541: if( s2 > nlstate){
! 1542: lli=log(out[s1][s2] - savm[s1][s2]);
! 1543: }else{
! 1544: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
! 1545: }
! 1546: ipmx +=1;
! 1547: sw += weight[i];
! 1548: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
! 1549: /* printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */
! 1550: } /* end of wave */
! 1551: } /* end of individual */
! 1552: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
! 1553: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
! 1554: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
! 1555: for(mi=1; mi<= wav[i]-1; mi++){
! 1556: for (ii=1;ii<=nlstate+ndeath;ii++)
! 1557: for (j=1;j<=nlstate+ndeath;j++){
! 1558: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 1559: savm[ii][j]=(ii==j ? 1.0 : 0.0);
! 1560: }
! 1561: for(d=0; d<dh[mi][i]; d++){
! 1562: newm=savm;
! 1563: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
! 1564: for (kk=1; kk<=cptcovage;kk++) {
! 1565: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
! 1566: }
! 1567:
! 1568: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
! 1569: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
! 1570: savm=oldm;
! 1571: oldm=newm;
! 1572: } /* end mult */
! 1573:
! 1574: s1=s[mw[mi][i]][i];
! 1575: s2=s[mw[mi+1][i]][i];
! 1576: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
! 1577: ipmx +=1;
! 1578: sw += weight[i];
! 1579: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
! 1580: /*printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]);*/
! 1581: } /* end of wave */
! 1582: } /* end of individual */
! 1583: } /* End of if */
! 1584: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
! 1585: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
! 1586: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
! 1587: return -l;
! 1588: }
! 1589:
! 1590: /*************** log-likelihood *************/
! 1591: double funcone( double *x)
! 1592: {
! 1593: /* Same as likeli but slower because of a lot of printf and if */
! 1594: int i, ii, j, k, mi, d, kk;
! 1595: double l, ll[NLSTATEMAX], cov[NCOVMAX];
! 1596: double **out;
! 1597: double lli; /* Individual log likelihood */
! 1598: double llt;
! 1599: int s1, s2;
! 1600: double bbh, survp;
! 1601: /*extern weight */
! 1602: /* We are differentiating ll according to initial status */
! 1603: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
! 1604: /*for(i=1;i<imx;i++)
! 1605: printf(" %d\n",s[4][i]);
! 1606: */
! 1607: cov[1]=1.;
! 1608:
! 1609: for(k=1; k<=nlstate; k++) ll[k]=0.;
! 1610:
! 1611: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
! 1612: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
! 1613: for(mi=1; mi<= wav[i]-1; mi++){
! 1614: for (ii=1;ii<=nlstate+ndeath;ii++)
! 1615: for (j=1;j<=nlstate+ndeath;j++){
! 1616: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 1617: savm[ii][j]=(ii==j ? 1.0 : 0.0);
! 1618: }
! 1619: for(d=0; d<dh[mi][i]; d++){
! 1620: newm=savm;
! 1621: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
! 1622: for (kk=1; kk<=cptcovage;kk++) {
! 1623: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
! 1624: }
! 1625: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
! 1626: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
! 1627: savm=oldm;
! 1628: oldm=newm;
! 1629: } /* end mult */
! 1630:
! 1631: s1=s[mw[mi][i]][i];
! 1632: s2=s[mw[mi+1][i]][i];
! 1633: bbh=(double)bh[mi][i]/(double)stepm;
! 1634: /* bias is positive if real duration
! 1635: * is higher than the multiple of stepm and negative otherwise.
! 1636: */
! 1637: if( s2 > nlstate && (mle <5) ){ /* Jackson */
! 1638: lli=log(out[s1][s2] - savm[s1][s2]);
! 1639: } else if (s2==-2) {
! 1640: for (j=1,survp=0. ; j<=nlstate; j++)
! 1641: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
! 1642: lli= log(survp);
! 1643: }else if (mle==1){
! 1644: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
! 1645: } else if(mle==2){
! 1646: lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* linear interpolation */
! 1647: } else if(mle==3){ /* exponential inter-extrapolation */
! 1648: lli= (savm[s1][s2]>(double)1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* exponential inter-extrapolation */
! 1649: } else if (mle==4){ /* mle=4 no inter-extrapolation */
! 1650: lli=log(out[s1][s2]); /* Original formula */
! 1651: } else{ /* ml>=5 no inter-extrapolation no jackson =0.8a */
! 1652: lli=log(out[s1][s2]); /* Original formula */
! 1653: } /* End of if */
! 1654: ipmx +=1;
! 1655: sw += weight[i];
! 1656: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
! 1657: /* printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */
! 1658: if(globpr){
! 1659: fprintf(ficresilk,"%9d %6d %2d %2d %1d %1d %3d %11.6f %8.4f\
! 1660: %11.6f %11.6f %11.6f ", \
! 1661: num[i],i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],
! 1662: 2*weight[i]*lli,out[s1][s2],savm[s1][s2]);
! 1663: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
! 1664: llt +=ll[k]*gipmx/gsw;
! 1665: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
! 1666: }
! 1667: fprintf(ficresilk," %10.6f\n", -llt);
! 1668: }
! 1669: } /* end of wave */
! 1670: } /* end of individual */
! 1671: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
! 1672: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
! 1673: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
! 1674: if(globpr==0){ /* First time we count the contributions and weights */
! 1675: gipmx=ipmx;
! 1676: gsw=sw;
! 1677: }
! 1678: return -l;
! 1679: }
! 1680:
! 1681:
! 1682: /*************** function likelione ***********/
! 1683: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*funcone)(double []))
! 1684: {
! 1685: /* This routine should help understanding what is done with
! 1686: the selection of individuals/waves and
! 1687: to check the exact contribution to the likelihood.
! 1688: Plotting could be done.
! 1689: */
! 1690: int k;
! 1691:
! 1692: if(*globpri !=0){ /* Just counts and sums, no printings */
! 1693: strcpy(fileresilk,"ilk");
! 1694: strcat(fileresilk,fileres);
! 1695: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
! 1696: printf("Problem with resultfile: %s\n", fileresilk);
! 1697: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
! 1698: }
! 1699: fprintf(ficresilk, "#individual(line's_record) s1 s2 wave# effective_wave# number_of_matrices_product pij weight -2ln(pij)*weight 0pij_x 0pij_(x-stepm) cumulating_loglikeli_by_health_state(reweighted=-2ll*weightXnumber_of_contribs/sum_of_weights) and_total\n");
! 1700: fprintf(ficresilk, "#num_i i s1 s2 mi mw dh likeli weight 2wlli out sav ");
! 1701: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
! 1702: for(k=1; k<=nlstate; k++)
! 1703: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
! 1704: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
! 1705: }
! 1706:
! 1707: *fretone=(*funcone)(p);
! 1708: if(*globpri !=0){
! 1709: fclose(ficresilk);
! 1710: fprintf(fichtm,"\n<br>File of contributions to the likelihood: <a href=\"%s\">%s</a><br>\n",subdirf(fileresilk),subdirf(fileresilk));
! 1711: fflush(fichtm);
! 1712: }
! 1713: return;
! 1714: }
! 1715:
! 1716:
! 1717: /*********** Maximum Likelihood Estimation ***************/
! 1718:
! 1719: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
! 1720: {
! 1721: int i,j, iter;
! 1722: double **xi;
! 1723: double fret;
! 1724: double fretone; /* Only one call to likelihood */
! 1725: /* char filerespow[FILENAMELENGTH];*/
! 1726: xi=matrix(1,npar,1,npar);
! 1727: for (i=1;i<=npar;i++)
! 1728: for (j=1;j<=npar;j++)
! 1729: xi[i][j]=(i==j ? 1.0 : 0.0);
! 1730: printf("Powell\n"); fprintf(ficlog,"Powell\n");
! 1731: strcpy(filerespow,"pow");
! 1732: strcat(filerespow,fileres);
! 1733: if((ficrespow=fopen(filerespow,"w"))==NULL) {
! 1734: printf("Problem with resultfile: %s\n", filerespow);
! 1735: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
! 1736: }
! 1737: fprintf(ficrespow,"# Powell\n# iter -2*LL");
! 1738: for (i=1;i<=nlstate;i++)
! 1739: for(j=1;j<=nlstate+ndeath;j++)
! 1740: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
! 1741: fprintf(ficrespow,"\n");
! 1742:
! 1743: powell(p,xi,npar,ftol,&iter,&fret,func);
! 1744:
! 1745: free_matrix(xi,1,npar,1,npar);
! 1746: fclose(ficrespow);
! 1747: printf("\n#Number of iterations = %d, -2 Log likelihood = %.12f\n",iter,func(p));
! 1748: fprintf(ficlog,"\n#Number of iterations = %d, -2 Log likelihood = %.12f \n",iter,func(p));
! 1749: fprintf(ficres,"#Number of iterations = %d, -2 Log likelihood = %.12f \n",iter,func(p));
! 1750:
! 1751: }
! 1752:
! 1753: /**** Computes Hessian and covariance matrix ***/
! 1754: void hesscov(double **matcov, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
! 1755: {
! 1756: double **a,**y,*x,pd;
! 1757: double **hess;
! 1758: int i, j,jk;
! 1759: int *indx;
! 1760:
! 1761: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
! 1762: double hessij(double p[], double delti[], int i, int j,double (*func)(double []),int npar);
! 1763: void lubksb(double **a, int npar, int *indx, double b[]) ;
! 1764: void ludcmp(double **a, int npar, int *indx, double *d) ;
! 1765: double gompertz(double p[]);
! 1766: hess=matrix(1,npar,1,npar);
! 1767:
! 1768: printf("\nCalculation of the hessian matrix. Wait...\n");
! 1769: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
! 1770: for (i=1;i<=npar;i++){
! 1771: printf("%d",i);fflush(stdout);
! 1772: fprintf(ficlog,"%d",i);fflush(ficlog);
! 1773:
! 1774: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
! 1775:
! 1776: /* printf(" %f ",p[i]);
! 1777: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
! 1778: }
! 1779:
! 1780: for (i=1;i<=npar;i++) {
! 1781: for (j=1;j<=npar;j++) {
! 1782: if (j>i) {
! 1783: printf(".%d%d",i,j);fflush(stdout);
! 1784: fprintf(ficlog,".%d%d",i,j);fflush(ficlog);
! 1785: hess[i][j]=hessij(p,delti,i,j,func,npar);
! 1786:
! 1787: hess[j][i]=hess[i][j];
! 1788: /*printf(" %lf ",hess[i][j]);*/
! 1789: }
! 1790: }
! 1791: }
! 1792: printf("\n");
! 1793: fprintf(ficlog,"\n");
! 1794:
! 1795: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
! 1796: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
! 1797:
! 1798: a=matrix(1,npar,1,npar);
! 1799: y=matrix(1,npar,1,npar);
! 1800: x=vector(1,npar);
! 1801: indx=ivector(1,npar);
! 1802: for (i=1;i<=npar;i++)
! 1803: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
! 1804: ludcmp(a,npar,indx,&pd);
! 1805:
! 1806: for (j=1;j<=npar;j++) {
! 1807: for (i=1;i<=npar;i++) x[i]=0;
! 1808: x[j]=1;
! 1809: lubksb(a,npar,indx,x);
! 1810: for (i=1;i<=npar;i++){
! 1811: matcov[i][j]=x[i];
! 1812: }
! 1813: }
! 1814:
! 1815: printf("\n#Hessian matrix#\n");
! 1816: fprintf(ficlog,"\n#Hessian matrix#\n");
! 1817: for (i=1;i<=npar;i++) {
! 1818: for (j=1;j<=npar;j++) {
! 1819: printf("%.3e ",hess[i][j]);
! 1820: fprintf(ficlog,"%.3e ",hess[i][j]);
! 1821: }
! 1822: printf("\n");
! 1823: fprintf(ficlog,"\n");
! 1824: }
! 1825:
! 1826: /* Recompute Inverse */
! 1827: for (i=1;i<=npar;i++)
! 1828: for (j=1;j<=npar;j++) a[i][j]=matcov[i][j];
! 1829: ludcmp(a,npar,indx,&pd);
! 1830:
! 1831: /* printf("\n#Hessian matrix recomputed#\n");
! 1832:
! 1833: for (j=1;j<=npar;j++) {
! 1834: for (i=1;i<=npar;i++) x[i]=0;
! 1835: x[j]=1;
! 1836: lubksb(a,npar,indx,x);
! 1837: for (i=1;i<=npar;i++){
! 1838: y[i][j]=x[i];
! 1839: printf("%.3e ",y[i][j]);
! 1840: fprintf(ficlog,"%.3e ",y[i][j]);
! 1841: }
! 1842: printf("\n");
! 1843: fprintf(ficlog,"\n");
! 1844: }
! 1845: */
! 1846:
! 1847: free_matrix(a,1,npar,1,npar);
! 1848: free_matrix(y,1,npar,1,npar);
! 1849: free_vector(x,1,npar);
! 1850: free_ivector(indx,1,npar);
! 1851: free_matrix(hess,1,npar,1,npar);
! 1852:
! 1853:
! 1854: }
! 1855:
! 1856: /*************** hessian matrix ****************/
! 1857: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
! 1858: {
! 1859: int i;
! 1860: int l=1, lmax=20;
! 1861: double k1,k2;
! 1862: double p2[NPARMAX+1];
! 1863: double res;
! 1864: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
! 1865: double fx;
! 1866: int k=0,kmax=10;
! 1867: double l1;
! 1868:
! 1869: fx=func(x);
! 1870: for (i=1;i<=npar;i++) p2[i]=x[i];
! 1871: for(l=0 ; l <=lmax; l++){
! 1872: l1=pow(10,l);
! 1873: delts=delt;
! 1874: for(k=1 ; k <kmax; k=k+1){
! 1875: delt = delta*(l1*k);
! 1876: p2[theta]=x[theta] +delt;
! 1877: k1=func(p2)-fx;
! 1878: p2[theta]=x[theta]-delt;
! 1879: k2=func(p2)-fx;
! 1880: /*res= (k1-2.0*fx+k2)/delt/delt; */
! 1881: res= (k1+k2)/delt/delt/2.; /* Divided by because L and not 2*L */
! 1882:
! 1883: #ifdef DEBUG
! 1884: printf("%d %d k1=%.12e k2=%.12e xk1=%.12e xk2=%.12e delt=%.12e res=%.12e l=%d k=%d,fx=%.12e\n",theta,theta,k1,k2,x[theta]+delt,x[theta]-delt,delt,res, l, k,fx);
! 1885: fprintf(ficlog,"%d %d k1=%.12e k2=%.12e xk1=%.12e xk2=%.12e delt=%.12e res=%.12e l=%d k=%d,fx=%.12e\n",theta,theta,k1,k2,x[theta]+delt,x[theta]-delt,delt,res, l, k,fx);
! 1886: #endif
! 1887: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
! 1888: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
! 1889: k=kmax;
! 1890: }
! 1891: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
! 1892: k=kmax; l=lmax*10.;
! 1893: }
! 1894: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
! 1895: delts=delt;
! 1896: }
! 1897: }
! 1898: }
! 1899: delti[theta]=delts;
! 1900: return res;
! 1901:
! 1902: }
! 1903:
! 1904: double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
! 1905: {
! 1906: int i;
! 1907: int l=1, l1, lmax=20;
! 1908: double k1,k2,k3,k4,res,fx;
! 1909: double p2[NPARMAX+1];
! 1910: int k;
! 1911:
! 1912: fx=func(x);
! 1913: for (k=1; k<=2; k++) {
! 1914: for (i=1;i<=npar;i++) p2[i]=x[i];
! 1915: p2[thetai]=x[thetai]+delti[thetai]/k;
! 1916: p2[thetaj]=x[thetaj]+delti[thetaj]/k;
! 1917: k1=func(p2)-fx;
! 1918:
! 1919: p2[thetai]=x[thetai]+delti[thetai]/k;
! 1920: p2[thetaj]=x[thetaj]-delti[thetaj]/k;
! 1921: k2=func(p2)-fx;
! 1922:
! 1923: p2[thetai]=x[thetai]-delti[thetai]/k;
! 1924: p2[thetaj]=x[thetaj]+delti[thetaj]/k;
! 1925: k3=func(p2)-fx;
! 1926:
! 1927: p2[thetai]=x[thetai]-delti[thetai]/k;
! 1928: p2[thetaj]=x[thetaj]-delti[thetaj]/k;
! 1929: k4=func(p2)-fx;
! 1930: res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /* Because of L not 2*L */
! 1931: #ifdef DEBUG
! 1932: printf("%d %d k=%d, k1=%.12e k2=%.12e k3=%.12e k4=%.12e delti/k=%.12e deltj/k=%.12e, xi-de/k=%.12e xj-de/k=%.12e res=%.12e k1234=%.12e,k1-2=%.12e,k3-4=%.12e\n",thetai,thetaj,k,k1,k2,k3,k4,delti[thetai]/k,delti[thetaj]/k,x[thetai]-delti[thetai]/k,x[thetaj]-delti[thetaj]/k, res,k1-k2-k3+k4,k1-k2,k3-k4);
! 1933: fprintf(ficlog,"%d %d k=%d, k1=%.12e k2=%.12e k3=%.12e k4=%.12e delti/k=%.12e deltj/k=%.12e, xi-de/k=%.12e xj-de/k=%.12e res=%.12e k1234=%.12e,k1-2=%.12e,k3-4=%.12e\n",thetai,thetaj,k,k1,k2,k3,k4,delti[thetai]/k,delti[thetaj]/k,x[thetai]-delti[thetai]/k,x[thetaj]-delti[thetaj]/k, res,k1-k2-k3+k4,k1-k2,k3-k4);
! 1934: #endif
! 1935: }
! 1936: return res;
! 1937: }
! 1938:
! 1939: /************** Inverse of matrix **************/
! 1940: void ludcmp(double **a, int n, int *indx, double *d)
! 1941: {
! 1942: int i,imax,j,k;
! 1943: double big,dum,sum,temp;
! 1944: double *vv;
! 1945:
! 1946: vv=vector(1,n);
! 1947: *d=1.0;
! 1948: for (i=1;i<=n;i++) {
! 1949: big=0.0;
! 1950: for (j=1;j<=n;j++)
! 1951: if ((temp=fabs(a[i][j])) > big) big=temp;
! 1952: if (big == 0.0) nrerror("Singular matrix in routine ludcmp");
! 1953: vv[i]=1.0/big;
! 1954: }
! 1955: for (j=1;j<=n;j++) {
! 1956: for (i=1;i<j;i++) {
! 1957: sum=a[i][j];
! 1958: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
! 1959: a[i][j]=sum;
! 1960: }
! 1961: big=0.0;
! 1962: for (i=j;i<=n;i++) {
! 1963: sum=a[i][j];
! 1964: for (k=1;k<j;k++)
! 1965: sum -= a[i][k]*a[k][j];
! 1966: a[i][j]=sum;
! 1967: if ( (dum=vv[i]*fabs(sum)) >= big) {
! 1968: big=dum;
! 1969: imax=i;
! 1970: }
! 1971: }
! 1972: if (j != imax) {
! 1973: for (k=1;k<=n;k++) {
! 1974: dum=a[imax][k];
! 1975: a[imax][k]=a[j][k];
! 1976: a[j][k]=dum;
! 1977: }
! 1978: *d = -(*d);
! 1979: vv[imax]=vv[j];
! 1980: }
! 1981: indx[j]=imax;
! 1982: if (a[j][j] == 0.0) a[j][j]=TINY;
! 1983: if (j != n) {
! 1984: dum=1.0/(a[j][j]);
! 1985: for (i=j+1;i<=n;i++) a[i][j] *= dum;
! 1986: }
! 1987: }
! 1988: free_vector(vv,1,n); /* Doesn't work */
! 1989: ;
! 1990: }
! 1991:
! 1992: void lubksb(double **a, int n, int *indx, double b[])
! 1993: {
! 1994: int i,ii=0,ip,j;
! 1995: double sum;
! 1996:
! 1997: for (i=1;i<=n;i++) {
! 1998: ip=indx[i];
! 1999: sum=b[ip];
! 2000: b[ip]=b[i];
! 2001: if (ii)
! 2002: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
! 2003: else if (sum) ii=i;
! 2004: b[i]=sum;
! 2005: }
! 2006: for (i=n;i>=1;i--) {
! 2007: sum=b[i];
! 2008: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
! 2009: b[i]=sum/a[i][i];
! 2010: }
! 2011: }
! 2012:
! 2013: void pstamp(FILE *fichier)
! 2014: {
! 2015: fprintf(fichier,"# %s.%s\n#%s\n#%s\n# %s", optionfilefiname,optionfilext,version,fullversion,strstart);
! 2016: }
! 2017:
! 2018: /************ Frequencies ********************/
! 2019: void freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, int *Tvaraff, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[])
! 2020: { /* Some frequencies */
! 2021:
! 2022: int i, m, jk, k1,i1, j1, bool, z1,z2,j;
! 2023: int first;
! 2024: double ***freq; /* Frequencies */
! 2025: double *pp, **prop;
! 2026: double pos,posprop, k2, dateintsum=0,k2cpt=0;
! 2027: char fileresp[FILENAMELENGTH];
! 2028:
! 2029: pp=vector(1,nlstate);
! 2030: prop=matrix(1,nlstate,iagemin,iagemax+3);
! 2031: strcpy(fileresp,"p");
! 2032: strcat(fileresp,fileres);
! 2033: if((ficresp=fopen(fileresp,"w"))==NULL) {
! 2034: printf("Problem with prevalence resultfile: %s\n", fileresp);
! 2035: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
! 2036: exit(0);
! 2037: }
! 2038: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin,iagemax+3);
! 2039: j1=0;
! 2040:
! 2041: j=cptcoveff;
! 2042: if (cptcovn<1) {j=1;ncodemax[1]=1;}
! 2043:
! 2044: first=1;
! 2045:
! 2046: for(k1=1; k1<=j;k1++){
! 2047: for(i1=1; i1<=ncodemax[k1];i1++){
! 2048: j1++;
! 2049: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
! 2050: scanf("%d", i);*/
! 2051: for (i=-5; i<=nlstate+ndeath; i++)
! 2052: for (jk=-5; jk<=nlstate+ndeath; jk++)
! 2053: for(m=iagemin; m <= iagemax+3; m++)
! 2054: freq[i][jk][m]=0;
! 2055:
! 2056: for (i=1; i<=nlstate; i++)
! 2057: for(m=iagemin; m <= iagemax+3; m++)
! 2058: prop[i][m]=0;
! 2059:
! 2060: dateintsum=0;
! 2061: k2cpt=0;
! 2062: for (i=1; i<=imx; i++) {
! 2063: bool=1;
! 2064: if (cptcovn>0) {
! 2065: for (z1=1; z1<=cptcoveff; z1++)
! 2066: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtab[j1][z1]])
! 2067: bool=0;
! 2068: }
! 2069: if (bool==1){
! 2070: for(m=firstpass; m<=lastpass; m++){
! 2071: k2=anint[m][i]+(mint[m][i]/12.);
! 2072: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
! 2073: if(agev[m][i]==0) agev[m][i]=iagemax+1;
! 2074: if(agev[m][i]==1) agev[m][i]=iagemax+2;
! 2075: if (s[m][i]>0 && s[m][i]<=nlstate) prop[s[m][i]][(int)agev[m][i]] += weight[i];
! 2076: if (m<lastpass) {
! 2077: freq[s[m][i]][s[m+1][i]][(int)agev[m][i]] += weight[i];
! 2078: freq[s[m][i]][s[m+1][i]][iagemax+3] += weight[i];
! 2079: }
! 2080:
! 2081: if ((agev[m][i]>1) && (agev[m][i]< (iagemax+3))) {
! 2082: dateintsum=dateintsum+k2;
! 2083: k2cpt++;
! 2084: }
! 2085: /*}*/
! 2086: }
! 2087: }
! 2088: }
! 2089:
! 2090: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
! 2091: pstamp(ficresp);
! 2092: if (cptcovn>0) {
! 2093: fprintf(ficresp, "\n#********** Variable ");
! 2094: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
! 2095: fprintf(ficresp, "**********\n#");
! 2096: }
! 2097: for(i=1; i<=nlstate;i++)
! 2098: fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
! 2099: fprintf(ficresp, "\n");
! 2100:
! 2101: for(i=iagemin; i <= iagemax+3; i++){
! 2102: if(i==iagemax+3){
! 2103: fprintf(ficlog,"Total");
! 2104: }else{
! 2105: if(first==1){
! 2106: first=0;
! 2107: printf("See log file for details...\n");
! 2108: }
! 2109: fprintf(ficlog,"Age %d", i);
! 2110: }
! 2111: for(jk=1; jk <=nlstate ; jk++){
! 2112: for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
! 2113: pp[jk] += freq[jk][m][i];
! 2114: }
! 2115: for(jk=1; jk <=nlstate ; jk++){
! 2116: for(m=-1, pos=0; m <=0 ; m++)
! 2117: pos += freq[jk][m][i];
! 2118: if(pp[jk]>=1.e-10){
! 2119: if(first==1){
! 2120: printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
! 2121: }
! 2122: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
! 2123: }else{
! 2124: if(first==1)
! 2125: printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
! 2126: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
! 2127: }
! 2128: }
! 2129:
! 2130: for(jk=1; jk <=nlstate ; jk++){
! 2131: for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)
! 2132: pp[jk] += freq[jk][m][i];
! 2133: }
! 2134: for(jk=1,pos=0,posprop=0; jk <=nlstate ; jk++){
! 2135: pos += pp[jk];
! 2136: posprop += prop[jk][i];
! 2137: }
! 2138: for(jk=1; jk <=nlstate ; jk++){
! 2139: if(pos>=1.e-5){
! 2140: if(first==1)
! 2141: printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
! 2142: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
! 2143: }else{
! 2144: if(first==1)
! 2145: printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
! 2146: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
! 2147: }
! 2148: if( i <= iagemax){
! 2149: if(pos>=1.e-5){
! 2150: fprintf(ficresp," %d %.5f %.0f %.0f",i,prop[jk][i]/posprop, prop[jk][i],posprop);
! 2151: /*probs[i][jk][j1]= pp[jk]/pos;*/
! 2152: /*printf("\ni=%d jk=%d j1=%d %.5f %.0f %.0f %f",i,jk,j1,pp[jk]/pos, pp[jk],pos,probs[i][jk][j1]);*/
! 2153: }
! 2154: else
! 2155: fprintf(ficresp," %d NaNq %.0f %.0f",i,prop[jk][i],posprop);
! 2156: }
! 2157: }
! 2158:
! 2159: for(jk=-1; jk <=nlstate+ndeath; jk++)
! 2160: for(m=-1; m <=nlstate+ndeath; m++)
! 2161: if(freq[jk][m][i] !=0 ) {
! 2162: if(first==1)
! 2163: printf(" %d%d=%.0f",jk,m,freq[jk][m][i]);
! 2164: fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][i]);
! 2165: }
! 2166: if(i <= iagemax)
! 2167: fprintf(ficresp,"\n");
! 2168: if(first==1)
! 2169: printf("Others in log...\n");
! 2170: fprintf(ficlog,"\n");
! 2171: }
! 2172: }
! 2173: }
! 2174: dateintmean=dateintsum/k2cpt;
! 2175:
! 2176: fclose(ficresp);
! 2177: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin, iagemax+3);
! 2178: free_vector(pp,1,nlstate);
! 2179: free_matrix(prop,1,nlstate,iagemin, iagemax+3);
! 2180: /* End of Freq */
! 2181: }
! 2182:
! 2183: /************ Prevalence ********************/
! 2184: void prevalence(double ***probs, double agemin, double agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2, int firstpass, int lastpass)
! 2185: {
! 2186: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
! 2187: in each health status at the date of interview (if between dateprev1 and dateprev2).
! 2188: We still use firstpass and lastpass as another selection.
! 2189: */
! 2190:
! 2191: int i, m, jk, k1, i1, j1, bool, z1,z2,j;
! 2192: double ***freq; /* Frequencies */
! 2193: double *pp, **prop;
! 2194: double pos,posprop;
! 2195: double y2; /* in fractional years */
! 2196: int iagemin, iagemax;
! 2197:
! 2198: iagemin= (int) agemin;
! 2199: iagemax= (int) agemax;
! 2200: /*pp=vector(1,nlstate);*/
! 2201: prop=matrix(1,nlstate,iagemin,iagemax+3);
! 2202: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
! 2203: j1=0;
! 2204:
! 2205: j=cptcoveff;
! 2206: if (cptcovn<1) {j=1;ncodemax[1]=1;}
! 2207:
! 2208: for(k1=1; k1<=j;k1++){
! 2209: for(i1=1; i1<=ncodemax[k1];i1++){
! 2210: j1++;
! 2211:
! 2212: for (i=1; i<=nlstate; i++)
! 2213: for(m=iagemin; m <= iagemax+3; m++)
! 2214: prop[i][m]=0.0;
! 2215:
! 2216: for (i=1; i<=imx; i++) { /* Each individual */
! 2217: bool=1;
! 2218: if (cptcovn>0) {
! 2219: for (z1=1; z1<=cptcoveff; z1++)
! 2220: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtab[j1][z1]])
! 2221: bool=0;
! 2222: }
! 2223: if (bool==1) {
! 2224: for(m=firstpass; m<=lastpass; m++){/* Other selection (we can limit to certain interviews*/
! 2225: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
! 2226: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
! 2227: if(agev[m][i]==0) agev[m][i]=iagemax+1;
! 2228: if(agev[m][i]==1) agev[m][i]=iagemax+2;
! 2229: if((int)agev[m][i] <iagemin || (int)agev[m][i] >iagemax+3) printf("Error on individual =%d agev[m][i]=%f m=%d\n",i, agev[m][i],m);
! 2230: if (s[m][i]>0 && s[m][i]<=nlstate) {
! 2231: /*if(i>4620) printf(" i=%d m=%d s[m][i]=%d (int)agev[m][i]=%d weight[i]=%f prop=%f\n",i,m,s[m][i],(int)agev[m][m],weight[i],prop[s[m][i]][(int)agev[m][i]]);*/
! 2232: prop[s[m][i]][(int)agev[m][i]] += weight[i];
! 2233: prop[s[m][i]][iagemax+3] += weight[i];
! 2234: }
! 2235: }
! 2236: } /* end selection of waves */
! 2237: }
! 2238: }
! 2239: for(i=iagemin; i <= iagemax+3; i++){
! 2240:
! 2241: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
! 2242: posprop += prop[jk][i];
! 2243: }
! 2244:
! 2245: for(jk=1; jk <=nlstate ; jk++){
! 2246: if( i <= iagemax){
! 2247: if(posprop>=1.e-5){
! 2248: probs[i][jk][j1]= prop[jk][i]/posprop;
! 2249: }
! 2250: }
! 2251: }/* end jk */
! 2252: }/* end i */
! 2253: } /* end i1 */
! 2254: } /* end k1 */
! 2255:
! 2256: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
! 2257: /*free_vector(pp,1,nlstate);*/
! 2258: free_matrix(prop,1,nlstate, iagemin,iagemax+3);
! 2259: } /* End of prevalence */
! 2260:
! 2261: /************* Waves Concatenation ***************/
! 2262:
! 2263: void concatwav(int wav[], int **dh, int **bh, int **mw, int **s, double *agedc, double **agev, int firstpass, int lastpass, int imx, int nlstate, int stepm)
! 2264: {
! 2265: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
! 2266: Death is a valid wave (if date is known).
! 2267: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
! 2268: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
! 2269: and mw[mi+1][i]. dh depends on stepm.
! 2270: */
! 2271:
! 2272: int i, mi, m;
! 2273: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
! 2274: double sum=0., jmean=0.;*/
! 2275: int first;
! 2276: int j, k=0,jk, ju, jl;
! 2277: double sum=0.;
! 2278: first=0;
! 2279: jmin=1e+5;
! 2280: jmax=-1;
! 2281: jmean=0.;
! 2282: for(i=1; i<=imx; i++){
! 2283: mi=0;
! 2284: m=firstpass;
! 2285: while(s[m][i] <= nlstate){
! 2286: if(s[m][i]>=1 || s[m][i]==-2 || s[m][i]==-4 || s[m][i]==-5)
! 2287: mw[++mi][i]=m;
! 2288: if(m >=lastpass)
! 2289: break;
! 2290: else
! 2291: m++;
! 2292: }/* end while */
! 2293: if (s[m][i] > nlstate){
! 2294: mi++; /* Death is another wave */
! 2295: /* if(mi==0) never been interviewed correctly before death */
! 2296: /* Only death is a correct wave */
! 2297: mw[mi][i]=m;
! 2298: }
! 2299:
! 2300: wav[i]=mi;
! 2301: if(mi==0){
! 2302: nbwarn++;
! 2303: if(first==0){
! 2304: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
! 2305: first=1;
! 2306: }
! 2307: if(first==1){
! 2308: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
! 2309: }
! 2310: } /* end mi==0 */
! 2311: } /* End individuals */
! 2312:
! 2313: for(i=1; i<=imx; i++){
! 2314: for(mi=1; mi<wav[i];mi++){
! 2315: if (stepm <=0)
! 2316: dh[mi][i]=1;
! 2317: else{
! 2318: if (s[mw[mi+1][i]][i] > nlstate) { /* A death */
! 2319: if (agedc[i] < 2*AGESUP) {
! 2320: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
! 2321: if(j==0) j=1; /* Survives at least one month after exam */
! 2322: else if(j<0){
! 2323: nberr++;
! 2324: printf("Error! Negative delay (%d to death) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
! 2325: j=1; /* Temporary Dangerous patch */
! 2326: printf(" We assumed that the date of interview was correct (and not the date of death) and postponed the death %d month(s) (one stepm) after the interview. You MUST fix the contradiction between dates.\n",stepm);
! 2327: fprintf(ficlog,"Error! Negative delay (%d to death) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
! 2328: fprintf(ficlog," We assumed that the date of interview was correct (and not the date of death) and postponed the death %d month(s) (one stepm) after the interview. You MUST fix the contradiction between dates.\n",stepm);
! 2329: }
! 2330: k=k+1;
! 2331: if (j >= jmax){
! 2332: jmax=j;
! 2333: ijmax=i;
! 2334: }
! 2335: if (j <= jmin){
! 2336: jmin=j;
! 2337: ijmin=i;
! 2338: }
! 2339: sum=sum+j;
! 2340: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
! 2341: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
! 2342: }
! 2343: }
! 2344: else{
! 2345: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
! 2346: /* if (j<0) printf("%d %lf %lf %d %d %d\n", i,agev[mw[mi+1][i]][i], agev[mw[mi][i]][i],j,s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]); */
! 2347:
! 2348: k=k+1;
! 2349: if (j >= jmax) {
! 2350: jmax=j;
! 2351: ijmax=i;
! 2352: }
! 2353: else if (j <= jmin){
! 2354: jmin=j;
! 2355: ijmin=i;
! 2356: }
! 2357: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
! 2358: /*printf("%d %lf %d %d %d\n", i,agev[mw[mi][i]][i],j,s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);*/
! 2359: if(j<0){
! 2360: nberr++;
! 2361: printf("Error! Negative delay (%d) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
! 2362: fprintf(ficlog,"Error! Negative delay (%d) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
! 2363: }
! 2364: sum=sum+j;
! 2365: }
! 2366: jk= j/stepm;
! 2367: jl= j -jk*stepm;
! 2368: ju= j -(jk+1)*stepm;
! 2369: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
! 2370: if(jl==0){
! 2371: dh[mi][i]=jk;
! 2372: bh[mi][i]=0;
! 2373: }else{ /* We want a negative bias in order to only have interpolation ie
! 2374: * at the price of an extra matrix product in likelihood */
! 2375: dh[mi][i]=jk+1;
! 2376: bh[mi][i]=ju;
! 2377: }
! 2378: }else{
! 2379: if(jl <= -ju){
! 2380: dh[mi][i]=jk;
! 2381: bh[mi][i]=jl; /* bias is positive if real duration
! 2382: * is higher than the multiple of stepm and negative otherwise.
! 2383: */
! 2384: }
! 2385: else{
! 2386: dh[mi][i]=jk+1;
! 2387: bh[mi][i]=ju;
! 2388: }
! 2389: if(dh[mi][i]==0){
! 2390: dh[mi][i]=1; /* At least one step */
! 2391: bh[mi][i]=ju; /* At least one step */
! 2392: /* printf(" bh=%d ju=%d jl=%d dh=%d jk=%d stepm=%d %d\n",bh[mi][i],ju,jl,dh[mi][i],jk,stepm,i);*/
! 2393: }
! 2394: } /* end if mle */
! 2395: }
! 2396: } /* end wave */
! 2397: }
! 2398: jmean=sum/k;
! 2399: printf("Delay (in months) between two waves Min=%d (for indiviudal %ld) Max=%d (%ld) Mean=%f\n\n ",jmin, num[ijmin], jmax, num[ijmax], jmean);
! 2400: fprintf(ficlog,"Delay (in months) between two waves Min=%d (for indiviudal %ld) Max=%d (%ld) Mean=%f\n\n ",jmin, ijmin, jmax, ijmax, jmean);
! 2401: }
! 2402:
! 2403: /*********** Tricode ****************************/
! 2404: void tricode(int *Tvar, int **nbcode, int imx)
! 2405: {
! 2406:
! 2407: int Ndum[20],ij=1, k, j, i, maxncov=19;
! 2408: int cptcode=0;
! 2409: cptcoveff=0;
! 2410:
! 2411: for (k=0; k<maxncov; k++) Ndum[k]=0;
! 2412: for (k=1; k<=7; k++) ncodemax[k]=0;
! 2413:
! 2414: for (j=1; j<=(cptcovn+2*cptcovprod); j++) {
! 2415: for (i=1; i<=imx; i++) { /*reads the data file to get the maximum
! 2416: modality*/
! 2417: ij=(int)(covar[Tvar[j]][i]); /* ij is the modality of this individual*/
! 2418: Ndum[ij]++; /*store the modality */
! 2419: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
! 2420: if (ij > cptcode) cptcode=ij; /* getting the maximum of covariable
! 2421: Tvar[j]. If V=sex and male is 0 and
! 2422: female is 1, then cptcode=1.*/
! 2423: }
! 2424:
! 2425: for (i=0; i<=cptcode; i++) {
! 2426: if(Ndum[i]!=0) ncodemax[j]++; /* Nomber of modalities of the j th covariates. In fact ncodemax[j]=2 (dichotom. variables) but it can be more */
! 2427: }
! 2428:
! 2429: ij=1;
! 2430: for (i=1; i<=ncodemax[j]; i++) {
! 2431: for (k=0; k<= maxncov; k++) {
! 2432: if (Ndum[k] != 0) {
! 2433: nbcode[Tvar[j]][ij]=k;
! 2434: /* store the modality in an array. k is a modality. If we have model=V1+V1*sex then: nbcode[1][1]=0 ; nbcode[1][2]=1; nbcode[2][1]=0 ; nbcode[2][2]=1; */
! 2435:
! 2436: ij++;
! 2437: }
! 2438: if (ij > ncodemax[j]) break;
! 2439: }
! 2440: }
! 2441: }
! 2442:
! 2443: for (k=0; k< maxncov; k++) Ndum[k]=0;
! 2444:
! 2445: for (i=1; i<=ncovmodel-2; i++) {
! 2446: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
! 2447: ij=Tvar[i];
! 2448: Ndum[ij]++;
! 2449: }
! 2450:
! 2451: ij=1;
! 2452: for (i=1; i<= maxncov; i++) {
! 2453: if((Ndum[i]!=0) && (i<=ncovcol)){
! 2454: Tvaraff[ij]=i; /*For printing */
! 2455: ij++;
! 2456: }
! 2457: }
! 2458:
! 2459: cptcoveff=ij-1; /*Number of simple covariates*/
! 2460: }
! 2461:
! 2462: /*********** Health Expectancies ****************/
! 2463:
! 2464: void evsij(char fileres[], double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,char strstart[] )
! 2465:
! 2466: {
! 2467: /* Health expectancies, no variances */
! 2468: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2;
! 2469: int nhstepma, nstepma; /* Decreasing with age */
! 2470: double age, agelim, hf;
! 2471: double ***p3mat;
! 2472: double eip;
! 2473:
! 2474: pstamp(ficreseij);
! 2475: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
! 2476: fprintf(ficreseij,"# Age");
! 2477: for(i=1; i<=nlstate;i++){
! 2478: for(j=1; j<=nlstate;j++){
! 2479: fprintf(ficreseij," e%1d%1d ",i,j);
! 2480: }
! 2481: fprintf(ficreseij," e%1d. ",i);
! 2482: }
! 2483: fprintf(ficreseij,"\n");
! 2484:
! 2485:
! 2486: if(estepm < stepm){
! 2487: printf ("Problem %d lower than %d\n",estepm, stepm);
! 2488: }
! 2489: else hstepm=estepm;
! 2490: /* We compute the life expectancy from trapezoids spaced every estepm months
! 2491: * This is mainly to measure the difference between two models: for example
! 2492: * if stepm=24 months pijx are given only every 2 years and by summing them
! 2493: * we are calculating an estimate of the Life Expectancy assuming a linear
! 2494: * progression in between and thus overestimating or underestimating according
! 2495: * to the curvature of the survival function. If, for the same date, we
! 2496: * estimate the model with stepm=1 month, we can keep estepm to 24 months
! 2497: * to compare the new estimate of Life expectancy with the same linear
! 2498: * hypothesis. A more precise result, taking into account a more precise
! 2499: * curvature will be obtained if estepm is as small as stepm. */
! 2500:
! 2501: /* For example we decided to compute the life expectancy with the smallest unit */
! 2502: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
! 2503: nhstepm is the number of hstepm from age to agelim
! 2504: nstepm is the number of stepm from age to agelin.
! 2505: Look at hpijx to understand the reason of that which relies in memory size
! 2506: and note for a fixed period like estepm months */
! 2507: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
! 2508: survival function given by stepm (the optimization length). Unfortunately it
! 2509: means that if the survival funtion is printed only each two years of age and if
! 2510: you sum them up and add 1 year (area under the trapezoids) you won't get the same
! 2511: results. So we changed our mind and took the option of the best precision.
! 2512: */
! 2513: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
! 2514:
! 2515: agelim=AGESUP;
! 2516: /* If stepm=6 months */
! 2517: /* Computed by stepm unit matrices, product of hstepm matrices, stored
! 2518: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
! 2519:
! 2520: /* nhstepm age range expressed in number of stepm */
! 2521: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
! 2522: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
! 2523: /* if (stepm >= YEARM) hstepm=1;*/
! 2524: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
! 2525: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 2526:
! 2527: for (age=bage; age<=fage; age ++){
! 2528: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
! 2529: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
! 2530: /* if (stepm >= YEARM) hstepm=1;*/
! 2531: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
! 2532:
! 2533: /* If stepm=6 months */
! 2534: /* Computed by stepm unit matrices, product of hstepma matrices, stored
! 2535: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
! 2536:
! 2537: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
! 2538:
! 2539: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
! 2540:
! 2541: printf("%d|",(int)age);fflush(stdout);
! 2542: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
! 2543:
! 2544: /* Computing expectancies */
! 2545: for(i=1; i<=nlstate;i++)
! 2546: for(j=1; j<=nlstate;j++)
! 2547: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
! 2548: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
! 2549:
! 2550: /* if((int)age==70)printf("i=%2d,j=%2d,h=%2d,age=%3d,%9.4f,%9.4f,%9.4f\n",i,j,h,(int)age,p3mat[i][j][h],hf,eij[i][j][(int)age]);*/
! 2551:
! 2552: }
! 2553:
! 2554: fprintf(ficreseij,"%3.0f",age );
! 2555: for(i=1; i<=nlstate;i++){
! 2556: eip=0;
! 2557: for(j=1; j<=nlstate;j++){
! 2558: eip +=eij[i][j][(int)age];
! 2559: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
! 2560: }
! 2561: fprintf(ficreseij,"%9.4f", eip );
! 2562: }
! 2563: fprintf(ficreseij,"\n");
! 2564:
! 2565: }
! 2566: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 2567: printf("\n");
! 2568: fprintf(ficlog,"\n");
! 2569:
! 2570: }
! 2571:
! 2572: void cvevsij(char fileres[], double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,double delti[],double **matcov,char strstart[] )
! 2573:
! 2574: {
! 2575: /* Covariances of health expectancies eij and of total life expectancies according
! 2576: to initial status i, ei. .
! 2577: */
! 2578: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
! 2579: int nhstepma, nstepma; /* Decreasing with age */
! 2580: double age, agelim, hf;
! 2581: double ***p3matp, ***p3matm, ***varhe;
! 2582: double **dnewm,**doldm;
! 2583: double *xp, *xm;
! 2584: double **gp, **gm;
! 2585: double ***gradg, ***trgradg;
! 2586: int theta;
! 2587:
! 2588: double eip, vip;
! 2589:
! 2590: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
! 2591: xp=vector(1,npar);
! 2592: xm=vector(1,npar);
! 2593: dnewm=matrix(1,nlstate*nlstate,1,npar);
! 2594: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
! 2595:
! 2596: pstamp(ficresstdeij);
! 2597: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
! 2598: fprintf(ficresstdeij,"# Age");
! 2599: for(i=1; i<=nlstate;i++){
! 2600: for(j=1; j<=nlstate;j++)
! 2601: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
! 2602: fprintf(ficresstdeij," e%1d. ",i);
! 2603: }
! 2604: fprintf(ficresstdeij,"\n");
! 2605:
! 2606: pstamp(ficrescveij);
! 2607: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
! 2608: fprintf(ficrescveij,"# Age");
! 2609: for(i=1; i<=nlstate;i++)
! 2610: for(j=1; j<=nlstate;j++){
! 2611: cptj= (j-1)*nlstate+i;
! 2612: for(i2=1; i2<=nlstate;i2++)
! 2613: for(j2=1; j2<=nlstate;j2++){
! 2614: cptj2= (j2-1)*nlstate+i2;
! 2615: if(cptj2 <= cptj)
! 2616: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
! 2617: }
! 2618: }
! 2619: fprintf(ficrescveij,"\n");
! 2620:
! 2621: if(estepm < stepm){
! 2622: printf ("Problem %d lower than %d\n",estepm, stepm);
! 2623: }
! 2624: else hstepm=estepm;
! 2625: /* We compute the life expectancy from trapezoids spaced every estepm months
! 2626: * This is mainly to measure the difference between two models: for example
! 2627: * if stepm=24 months pijx are given only every 2 years and by summing them
! 2628: * we are calculating an estimate of the Life Expectancy assuming a linear
! 2629: * progression in between and thus overestimating or underestimating according
! 2630: * to the curvature of the survival function. If, for the same date, we
! 2631: * estimate the model with stepm=1 month, we can keep estepm to 24 months
! 2632: * to compare the new estimate of Life expectancy with the same linear
! 2633: * hypothesis. A more precise result, taking into account a more precise
! 2634: * curvature will be obtained if estepm is as small as stepm. */
! 2635:
! 2636: /* For example we decided to compute the life expectancy with the smallest unit */
! 2637: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
! 2638: nhstepm is the number of hstepm from age to agelim
! 2639: nstepm is the number of stepm from age to agelin.
! 2640: Look at hpijx to understand the reason of that which relies in memory size
! 2641: and note for a fixed period like estepm months */
! 2642: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
! 2643: survival function given by stepm (the optimization length). Unfortunately it
! 2644: means that if the survival funtion is printed only each two years of age and if
! 2645: you sum them up and add 1 year (area under the trapezoids) you won't get the same
! 2646: results. So we changed our mind and took the option of the best precision.
! 2647: */
! 2648: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
! 2649:
! 2650: /* If stepm=6 months */
! 2651: /* nhstepm age range expressed in number of stepm */
! 2652: agelim=AGESUP;
! 2653: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
! 2654: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
! 2655: /* if (stepm >= YEARM) hstepm=1;*/
! 2656: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
! 2657:
! 2658: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 2659: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 2660: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
! 2661: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
! 2662: gp=matrix(0,nhstepm,1,nlstate*nlstate);
! 2663: gm=matrix(0,nhstepm,1,nlstate*nlstate);
! 2664:
! 2665: for (age=bage; age<=fage; age ++){
! 2666: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
! 2667: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
! 2668: /* if (stepm >= YEARM) hstepm=1;*/
! 2669: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
! 2670:
! 2671: /* If stepm=6 months */
! 2672: /* Computed by stepm unit matrices, product of hstepma matrices, stored
! 2673: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
! 2674:
! 2675: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
! 2676:
! 2677: /* Computing Variances of health expectancies */
! 2678: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
! 2679: decrease memory allocation */
! 2680: for(theta=1; theta <=npar; theta++){
! 2681: for(i=1; i<=npar; i++){
! 2682: xp[i] = x[i] + (i==theta ?delti[theta]:0);
! 2683: xm[i] = x[i] - (i==theta ?delti[theta]:0);
! 2684: }
! 2685: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij);
! 2686: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij);
! 2687:
! 2688: for(j=1; j<= nlstate; j++){
! 2689: for(i=1; i<=nlstate; i++){
! 2690: for(h=0; h<=nhstepm-1; h++){
! 2691: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
! 2692: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
! 2693: }
! 2694: }
! 2695: }
! 2696:
! 2697: for(ij=1; ij<= nlstate*nlstate; ij++)
! 2698: for(h=0; h<=nhstepm-1; h++){
! 2699: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
! 2700: }
! 2701: }/* End theta */
! 2702:
! 2703:
! 2704: for(h=0; h<=nhstepm-1; h++)
! 2705: for(j=1; j<=nlstate*nlstate;j++)
! 2706: for(theta=1; theta <=npar; theta++)
! 2707: trgradg[h][j][theta]=gradg[h][theta][j];
! 2708:
! 2709:
! 2710: for(ij=1;ij<=nlstate*nlstate;ij++)
! 2711: for(ji=1;ji<=nlstate*nlstate;ji++)
! 2712: varhe[ij][ji][(int)age] =0.;
! 2713:
! 2714: printf("%d|",(int)age);fflush(stdout);
! 2715: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
! 2716: for(h=0;h<=nhstepm-1;h++){
! 2717: for(k=0;k<=nhstepm-1;k++){
! 2718: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
! 2719: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
! 2720: for(ij=1;ij<=nlstate*nlstate;ij++)
! 2721: for(ji=1;ji<=nlstate*nlstate;ji++)
! 2722: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
! 2723: }
! 2724: }
! 2725:
! 2726: /* Computing expectancies */
! 2727: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
! 2728: for(i=1; i<=nlstate;i++)
! 2729: for(j=1; j<=nlstate;j++)
! 2730: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
! 2731: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
! 2732:
! 2733: /* if((int)age==70)printf("i=%2d,j=%2d,h=%2d,age=%3d,%9.4f,%9.4f,%9.4f\n",i,j,h,(int)age,p3mat[i][j][h],hf,eij[i][j][(int)age]);*/
! 2734:
! 2735: }
! 2736:
! 2737: fprintf(ficresstdeij,"%3.0f",age );
! 2738: for(i=1; i<=nlstate;i++){
! 2739: eip=0.;
! 2740: vip=0.;
! 2741: for(j=1; j<=nlstate;j++){
! 2742: eip += eij[i][j][(int)age];
! 2743: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
! 2744: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
! 2745: fprintf(ficresstdeij," %9.4f (%.4f)", eij[i][j][(int)age], sqrt(varhe[(j-1)*nlstate+i][(j-1)*nlstate+i][(int)age]) );
! 2746: }
! 2747: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
! 2748: }
! 2749: fprintf(ficresstdeij,"\n");
! 2750:
! 2751: fprintf(ficrescveij,"%3.0f",age );
! 2752: for(i=1; i<=nlstate;i++)
! 2753: for(j=1; j<=nlstate;j++){
! 2754: cptj= (j-1)*nlstate+i;
! 2755: for(i2=1; i2<=nlstate;i2++)
! 2756: for(j2=1; j2<=nlstate;j2++){
! 2757: cptj2= (j2-1)*nlstate+i2;
! 2758: if(cptj2 <= cptj)
! 2759: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
! 2760: }
! 2761: }
! 2762: fprintf(ficrescveij,"\n");
! 2763:
! 2764: }
! 2765: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
! 2766: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
! 2767: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
! 2768: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
! 2769: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 2770: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 2771: printf("\n");
! 2772: fprintf(ficlog,"\n");
! 2773:
! 2774: free_vector(xm,1,npar);
! 2775: free_vector(xp,1,npar);
! 2776: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
! 2777: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
! 2778: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
! 2779: }
! 2780:
! 2781: /************ Variance ******************/
! 2782: void varevsij(char optionfilefiname[], double ***vareij, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int ij, int estepm, int cptcov, int cptcod, int popbased, int mobilav, char strstart[])
! 2783: {
! 2784: /* Variance of health expectancies */
! 2785: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
! 2786: /* double **newm;*/
! 2787: double **dnewm,**doldm;
! 2788: double **dnewmp,**doldmp;
! 2789: int i, j, nhstepm, hstepm, h, nstepm ;
! 2790: int k, cptcode;
! 2791: double *xp;
! 2792: double **gp, **gm; /* for var eij */
! 2793: double ***gradg, ***trgradg; /*for var eij */
! 2794: double **gradgp, **trgradgp; /* for var p point j */
! 2795: double *gpp, *gmp; /* for var p point j */
! 2796: double **varppt; /* for var p point j nlstate to nlstate+ndeath */
! 2797: double ***p3mat;
! 2798: double age,agelim, hf;
! 2799: double ***mobaverage;
! 2800: int theta;
! 2801: char digit[4];
! 2802: char digitp[25];
! 2803:
! 2804: char fileresprobmorprev[FILENAMELENGTH];
! 2805:
! 2806: if(popbased==1){
! 2807: if(mobilav!=0)
! 2808: strcpy(digitp,"-populbased-mobilav-");
! 2809: else strcpy(digitp,"-populbased-nomobil-");
! 2810: }
! 2811: else
! 2812: strcpy(digitp,"-stablbased-");
! 2813:
! 2814: if (mobilav!=0) {
! 2815: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
! 2816: if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){
! 2817: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
! 2818: printf(" Error in movingaverage mobilav=%d\n",mobilav);
! 2819: }
! 2820: }
! 2821:
! 2822: strcpy(fileresprobmorprev,"prmorprev");
! 2823: sprintf(digit,"%-d",ij);
! 2824: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
! 2825: strcat(fileresprobmorprev,digit); /* Tvar to be done */
! 2826: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
! 2827: strcat(fileresprobmorprev,fileres);
! 2828: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
! 2829: printf("Problem with resultfile: %s\n", fileresprobmorprev);
! 2830: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
! 2831: }
! 2832: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
! 2833:
! 2834: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
! 2835: pstamp(ficresprobmorprev);
! 2836: fprintf(ficresprobmorprev,"# probabilities of dying before estepm=%d months for people of exact age and weighted probabilities w1*p1j+w2*p2j+... stand dev in()\n",estepm);
! 2837: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
! 2838: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
! 2839: fprintf(ficresprobmorprev," p.%-d SE",j);
! 2840: for(i=1; i<=nlstate;i++)
! 2841: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
! 2842: }
! 2843: fprintf(ficresprobmorprev,"\n");
! 2844: fprintf(ficgp,"\n# Routine varevsij");
! 2845: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
! 2846: fprintf(fichtm,"\n<li><h4> Computing probabilities of dying over estepm months as a weighted average (i.e global mortality independent of initial healh state)</h4></li>\n");
! 2847: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
! 2848: /* } */
! 2849: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
! 2850: pstamp(ficresvij);
! 2851: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
! 2852: if(popbased==1)
! 2853: fprintf(ficresvij,"the age specific prevalence observed in the population i.e cross-sectionally\n in each health state (popbased=1)");
! 2854: else
! 2855: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
! 2856: fprintf(ficresvij,"# Age");
! 2857: for(i=1; i<=nlstate;i++)
! 2858: for(j=1; j<=nlstate;j++)
! 2859: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
! 2860: fprintf(ficresvij,"\n");
! 2861:
! 2862: xp=vector(1,npar);
! 2863: dnewm=matrix(1,nlstate,1,npar);
! 2864: doldm=matrix(1,nlstate,1,nlstate);
! 2865: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
! 2866: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
! 2867:
! 2868: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
! 2869: gpp=vector(nlstate+1,nlstate+ndeath);
! 2870: gmp=vector(nlstate+1,nlstate+ndeath);
! 2871: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
! 2872:
! 2873: if(estepm < stepm){
! 2874: printf ("Problem %d lower than %d\n",estepm, stepm);
! 2875: }
! 2876: else hstepm=estepm;
! 2877: /* For example we decided to compute the life expectancy with the smallest unit */
! 2878: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
! 2879: nhstepm is the number of hstepm from age to agelim
! 2880: nstepm is the number of stepm from age to agelin.
! 2881: Look at hpijx to understand the reason of that which relies in memory size
! 2882: and note for a fixed period like k years */
! 2883: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
! 2884: survival function given by stepm (the optimization length). Unfortunately it
! 2885: means that if the survival funtion is printed every two years of age and if
! 2886: you sum them up and add 1 year (area under the trapezoids) you won't get the same
! 2887: results. So we changed our mind and took the option of the best precision.
! 2888: */
! 2889: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
! 2890: agelim = AGESUP;
! 2891: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
! 2892: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
! 2893: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
! 2894: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 2895: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
! 2896: gp=matrix(0,nhstepm,1,nlstate);
! 2897: gm=matrix(0,nhstepm,1,nlstate);
! 2898:
! 2899:
! 2900: for(theta=1; theta <=npar; theta++){
! 2901: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
! 2902: xp[i] = x[i] + (i==theta ?delti[theta]:0);
! 2903: }
! 2904: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
! 2905: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
! 2906:
! 2907: if (popbased==1) {
! 2908: if(mobilav ==0){
! 2909: for(i=1; i<=nlstate;i++)
! 2910: prlim[i][i]=probs[(int)age][i][ij];
! 2911: }else{ /* mobilav */
! 2912: for(i=1; i<=nlstate;i++)
! 2913: prlim[i][i]=mobaverage[(int)age][i][ij];
! 2914: }
! 2915: }
! 2916:
! 2917: for(j=1; j<= nlstate; j++){
! 2918: for(h=0; h<=nhstepm; h++){
! 2919: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
! 2920: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
! 2921: }
! 2922: }
! 2923: /* This for computing probability of death (h=1 means
! 2924: computed over hstepm matrices product = hstepm*stepm months)
! 2925: as a weighted average of prlim.
! 2926: */
! 2927: for(j=nlstate+1;j<=nlstate+ndeath;j++){
! 2928: for(i=1,gpp[j]=0.; i<= nlstate; i++)
! 2929: gpp[j] += prlim[i][i]*p3mat[i][j][1];
! 2930: }
! 2931: /* end probability of death */
! 2932:
! 2933: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
! 2934: xp[i] = x[i] - (i==theta ?delti[theta]:0);
! 2935: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
! 2936: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
! 2937:
! 2938: if (popbased==1) {
! 2939: if(mobilav ==0){
! 2940: for(i=1; i<=nlstate;i++)
! 2941: prlim[i][i]=probs[(int)age][i][ij];
! 2942: }else{ /* mobilav */
! 2943: for(i=1; i<=nlstate;i++)
! 2944: prlim[i][i]=mobaverage[(int)age][i][ij];
! 2945: }
! 2946: }
! 2947:
! 2948: for(j=1; j<= nlstate; j++){
! 2949: for(h=0; h<=nhstepm; h++){
! 2950: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
! 2951: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
! 2952: }
! 2953: }
! 2954: /* This for computing probability of death (h=1 means
! 2955: computed over hstepm matrices product = hstepm*stepm months)
! 2956: as a weighted average of prlim.
! 2957: */
! 2958: for(j=nlstate+1;j<=nlstate+ndeath;j++){
! 2959: for(i=1,gmp[j]=0.; i<= nlstate; i++)
! 2960: gmp[j] += prlim[i][i]*p3mat[i][j][1];
! 2961: }
! 2962: /* end probability of death */
! 2963:
! 2964: for(j=1; j<= nlstate; j++) /* vareij */
! 2965: for(h=0; h<=nhstepm; h++){
! 2966: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
! 2967: }
! 2968:
! 2969: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu */
! 2970: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
! 2971: }
! 2972:
! 2973: } /* End theta */
! 2974:
! 2975: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
! 2976:
! 2977: for(h=0; h<=nhstepm; h++) /* veij */
! 2978: for(j=1; j<=nlstate;j++)
! 2979: for(theta=1; theta <=npar; theta++)
! 2980: trgradg[h][j][theta]=gradg[h][theta][j];
! 2981:
! 2982: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
! 2983: for(theta=1; theta <=npar; theta++)
! 2984: trgradgp[j][theta]=gradgp[theta][j];
! 2985:
! 2986:
! 2987: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
! 2988: for(i=1;i<=nlstate;i++)
! 2989: for(j=1;j<=nlstate;j++)
! 2990: vareij[i][j][(int)age] =0.;
! 2991:
! 2992: for(h=0;h<=nhstepm;h++){
! 2993: for(k=0;k<=nhstepm;k++){
! 2994: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
! 2995: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
! 2996: for(i=1;i<=nlstate;i++)
! 2997: for(j=1;j<=nlstate;j++)
! 2998: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
! 2999: }
! 3000: }
! 3001:
! 3002: /* pptj */
! 3003: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
! 3004: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
! 3005: for(j=nlstate+1;j<=nlstate+ndeath;j++)
! 3006: for(i=nlstate+1;i<=nlstate+ndeath;i++)
! 3007: varppt[j][i]=doldmp[j][i];
! 3008: /* end ppptj */
! 3009: /* x centered again */
! 3010: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij);
! 3011: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ij);
! 3012:
! 3013: if (popbased==1) {
! 3014: if(mobilav ==0){
! 3015: for(i=1; i<=nlstate;i++)
! 3016: prlim[i][i]=probs[(int)age][i][ij];
! 3017: }else{ /* mobilav */
! 3018: for(i=1; i<=nlstate;i++)
! 3019: prlim[i][i]=mobaverage[(int)age][i][ij];
! 3020: }
! 3021: }
! 3022:
! 3023: /* This for computing probability of death (h=1 means
! 3024: computed over hstepm (estepm) matrices product = hstepm*stepm months)
! 3025: as a weighted average of prlim.
! 3026: */
! 3027: for(j=nlstate+1;j<=nlstate+ndeath;j++){
! 3028: for(i=1,gmp[j]=0.;i<= nlstate; i++)
! 3029: gmp[j] += prlim[i][i]*p3mat[i][j][1];
! 3030: }
! 3031: /* end probability of death */
! 3032:
! 3033: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
! 3034: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
! 3035: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
! 3036: for(i=1; i<=nlstate;i++){
! 3037: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
! 3038: }
! 3039: }
! 3040: fprintf(ficresprobmorprev,"\n");
! 3041:
! 3042: fprintf(ficresvij,"%.0f ",age );
! 3043: for(i=1; i<=nlstate;i++)
! 3044: for(j=1; j<=nlstate;j++){
! 3045: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
! 3046: }
! 3047: fprintf(ficresvij,"\n");
! 3048: free_matrix(gp,0,nhstepm,1,nlstate);
! 3049: free_matrix(gm,0,nhstepm,1,nlstate);
! 3050: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
! 3051: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
! 3052: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 3053: } /* End age */
! 3054: free_vector(gpp,nlstate+1,nlstate+ndeath);
! 3055: free_vector(gmp,nlstate+1,nlstate+ndeath);
! 3056: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
! 3057: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
! 3058: fprintf(ficgp,"\nset noparametric;set nolabel; set ter png small;set size 0.65, 0.65");
! 3059: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
! 3060: fprintf(ficgp,"\n set log y; set nolog x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
! 3061: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
! 3062: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
! 3063: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
! 3064: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l 1 ",subdirf(fileresprobmorprev));
! 3065: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95\%% interval\" w l 2 ",subdirf(fileresprobmorprev));
! 3066: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l 2 ",subdirf(fileresprobmorprev));
! 3067: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
! 3068: fprintf(fichtm,"\n<br> Probability is computed over estepm=%d months. <br> <img src=\"%s%s.png\"> <br>\n", estepm,subdirf3(optionfilefiname,"varmuptjgr",digitp),digit);
! 3069: /* fprintf(fichtm,"\n<br> Probability is computed over estepm=%d months and then divided by estepm and multiplied by %.0f in order to have the probability to die over a year <br> <img src=\"varmuptjgr%s%s.png\"> <br>\n", stepm,YEARM,digitp,digit);
! 3070: */
! 3071: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.png\";replot;",digitp,optionfilefiname,digit); */
! 3072: fprintf(ficgp,"\nset out \"%s%s.png\";replot;\n",subdirf3(optionfilefiname,"varmuptjgr",digitp),digit);
! 3073:
! 3074: free_vector(xp,1,npar);
! 3075: free_matrix(doldm,1,nlstate,1,nlstate);
! 3076: free_matrix(dnewm,1,nlstate,1,npar);
! 3077: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
! 3078: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
! 3079: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
! 3080: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
! 3081: fclose(ficresprobmorprev);
! 3082: fflush(ficgp);
! 3083: fflush(fichtm);
! 3084: } /* end varevsij */
! 3085:
! 3086: /************ Variance of prevlim ******************/
! 3087: void varprevlim(char fileres[], double **varpl, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int ij, char strstart[])
! 3088: {
! 3089: /* Variance of prevalence limit */
! 3090: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
! 3091: double **newm;
! 3092: double **dnewm,**doldm;
! 3093: int i, j, nhstepm, hstepm;
! 3094: int k, cptcode;
! 3095: double *xp;
! 3096: double *gp, *gm;
! 3097: double **gradg, **trgradg;
! 3098: double age,agelim;
! 3099: int theta;
! 3100:
! 3101: pstamp(ficresvpl);
! 3102: fprintf(ficresvpl,"# Standard deviation of period (stable) prevalences \n");
! 3103: fprintf(ficresvpl,"# Age");
! 3104: for(i=1; i<=nlstate;i++)
! 3105: fprintf(ficresvpl," %1d-%1d",i,i);
! 3106: fprintf(ficresvpl,"\n");
! 3107:
! 3108: xp=vector(1,npar);
! 3109: dnewm=matrix(1,nlstate,1,npar);
! 3110: doldm=matrix(1,nlstate,1,nlstate);
! 3111:
! 3112: hstepm=1*YEARM; /* Every year of age */
! 3113: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
! 3114: agelim = AGESUP;
! 3115: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
! 3116: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
! 3117: if (stepm >= YEARM) hstepm=1;
! 3118: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
! 3119: gradg=matrix(1,npar,1,nlstate);
! 3120: gp=vector(1,nlstate);
! 3121: gm=vector(1,nlstate);
! 3122:
! 3123: for(theta=1; theta <=npar; theta++){
! 3124: for(i=1; i<=npar; i++){ /* Computes gradient */
! 3125: xp[i] = x[i] + (i==theta ?delti[theta]:0);
! 3126: }
! 3127: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
! 3128: for(i=1;i<=nlstate;i++)
! 3129: gp[i] = prlim[i][i];
! 3130:
! 3131: for(i=1; i<=npar; i++) /* Computes gradient */
! 3132: xp[i] = x[i] - (i==theta ?delti[theta]:0);
! 3133: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
! 3134: for(i=1;i<=nlstate;i++)
! 3135: gm[i] = prlim[i][i];
! 3136:
! 3137: for(i=1;i<=nlstate;i++)
! 3138: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
! 3139: } /* End theta */
! 3140:
! 3141: trgradg =matrix(1,nlstate,1,npar);
! 3142:
! 3143: for(j=1; j<=nlstate;j++)
! 3144: for(theta=1; theta <=npar; theta++)
! 3145: trgradg[j][theta]=gradg[theta][j];
! 3146:
! 3147: for(i=1;i<=nlstate;i++)
! 3148: varpl[i][(int)age] =0.;
! 3149: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
! 3150: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
! 3151: for(i=1;i<=nlstate;i++)
! 3152: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
! 3153:
! 3154: fprintf(ficresvpl,"%.0f ",age );
! 3155: for(i=1; i<=nlstate;i++)
! 3156: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
! 3157: fprintf(ficresvpl,"\n");
! 3158: free_vector(gp,1,nlstate);
! 3159: free_vector(gm,1,nlstate);
! 3160: free_matrix(gradg,1,npar,1,nlstate);
! 3161: free_matrix(trgradg,1,nlstate,1,npar);
! 3162: } /* End age */
! 3163:
! 3164: free_vector(xp,1,npar);
! 3165: free_matrix(doldm,1,nlstate,1,npar);
! 3166: free_matrix(dnewm,1,nlstate,1,nlstate);
! 3167:
! 3168: }
! 3169:
! 3170: /************ Variance of one-step probabilities ******************/
! 3171: void varprob(char optionfilefiname[], double **matcov, double x[], double delti[], int nlstate, double bage, double fage, int ij, int *Tvar, int **nbcode, int *ncodemax, char strstart[])
! 3172: {
! 3173: int i, j=0, i1, k1, l1, t, tj;
! 3174: int k2, l2, j1, z1;
! 3175: int k=0,l, cptcode;
! 3176: int first=1, first1;
! 3177: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
! 3178: double **dnewm,**doldm;
! 3179: double *xp;
! 3180: double *gp, *gm;
! 3181: double **gradg, **trgradg;
! 3182: double **mu;
! 3183: double age,agelim, cov[NCOVMAX];
! 3184: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
! 3185: int theta;
! 3186: char fileresprob[FILENAMELENGTH];
! 3187: char fileresprobcov[FILENAMELENGTH];
! 3188: char fileresprobcor[FILENAMELENGTH];
! 3189:
! 3190: double ***varpij;
! 3191:
! 3192: strcpy(fileresprob,"prob");
! 3193: strcat(fileresprob,fileres);
! 3194: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
! 3195: printf("Problem with resultfile: %s\n", fileresprob);
! 3196: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
! 3197: }
! 3198: strcpy(fileresprobcov,"probcov");
! 3199: strcat(fileresprobcov,fileres);
! 3200: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
! 3201: printf("Problem with resultfile: %s\n", fileresprobcov);
! 3202: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
! 3203: }
! 3204: strcpy(fileresprobcor,"probcor");
! 3205: strcat(fileresprobcor,fileres);
! 3206: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
! 3207: printf("Problem with resultfile: %s\n", fileresprobcor);
! 3208: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
! 3209: }
! 3210: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
! 3211: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
! 3212: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
! 3213: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
! 3214: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
! 3215: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
! 3216: pstamp(ficresprob);
! 3217: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
! 3218: fprintf(ficresprob,"# Age");
! 3219: pstamp(ficresprobcov);
! 3220: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
! 3221: fprintf(ficresprobcov,"# Age");
! 3222: pstamp(ficresprobcor);
! 3223: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
! 3224: fprintf(ficresprobcor,"# Age");
! 3225:
! 3226:
! 3227: for(i=1; i<=nlstate;i++)
! 3228: for(j=1; j<=(nlstate+ndeath);j++){
! 3229: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
! 3230: fprintf(ficresprobcov," p%1d-%1d ",i,j);
! 3231: fprintf(ficresprobcor," p%1d-%1d ",i,j);
! 3232: }
! 3233: /* fprintf(ficresprob,"\n");
! 3234: fprintf(ficresprobcov,"\n");
! 3235: fprintf(ficresprobcor,"\n");
! 3236: */
! 3237: xp=vector(1,npar);
! 3238: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
! 3239: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
! 3240: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
! 3241: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
! 3242: first=1;
! 3243: fprintf(ficgp,"\n# Routine varprob");
! 3244: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
! 3245: fprintf(fichtm,"\n");
! 3246:
! 3247: fprintf(fichtm,"\n<li><h4> <a href=\"%s\">Matrix of variance-covariance of pairs of step probabilities (drawings)</a></h4></li>\n",optionfilehtmcov);
! 3248: fprintf(fichtmcov,"\n<h4>Matrix of variance-covariance of pairs of step probabilities</h4>\n\
! 3249: file %s<br>\n",optionfilehtmcov);
! 3250: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated\
! 3251: and drawn. It helps understanding how is the covariance between two incidences.\
! 3252: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
! 3253: fprintf(fichtmcov,"\n<br> Contour plot corresponding to x'cov<sup>-1</sup>x = 4 (where x is the column vector (pij,pkl)) are drawn. \
! 3254: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
! 3255: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
! 3256: standard deviations wide on each axis. <br>\
! 3257: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
! 3258: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
! 3259: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
! 3260:
! 3261: cov[1]=1;
! 3262: tj=cptcoveff;
! 3263: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
! 3264: j1=0;
! 3265: for(t=1; t<=tj;t++){
! 3266: for(i1=1; i1<=ncodemax[t];i1++){
! 3267: j1++;
! 3268: if (cptcovn>0) {
! 3269: fprintf(ficresprob, "\n#********** Variable ");
! 3270: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
! 3271: fprintf(ficresprob, "**********\n#\n");
! 3272: fprintf(ficresprobcov, "\n#********** Variable ");
! 3273: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
! 3274: fprintf(ficresprobcov, "**********\n#\n");
! 3275:
! 3276: fprintf(ficgp, "\n#********** Variable ");
! 3277: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
! 3278: fprintf(ficgp, "**********\n#\n");
! 3279:
! 3280:
! 3281: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
! 3282: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
! 3283: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
! 3284:
! 3285: fprintf(ficresprobcor, "\n#********** Variable ");
! 3286: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
! 3287: fprintf(ficresprobcor, "**********\n#");
! 3288: }
! 3289:
! 3290: for (age=bage; age<=fage; age ++){
! 3291: cov[2]=age;
! 3292: for (k=1; k<=cptcovn;k++) {
! 3293: cov[2+k]=nbcode[Tvar[k]][codtab[j1][Tvar[k]]];
! 3294: }
! 3295: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
! 3296: for (k=1; k<=cptcovprod;k++)
! 3297: cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];
! 3298:
! 3299: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
! 3300: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
! 3301: gp=vector(1,(nlstate)*(nlstate+ndeath));
! 3302: gm=vector(1,(nlstate)*(nlstate+ndeath));
! 3303:
! 3304: for(theta=1; theta <=npar; theta++){
! 3305: for(i=1; i<=npar; i++)
! 3306: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
! 3307:
! 3308: pmij(pmmij,cov,ncovmodel,xp,nlstate);
! 3309:
! 3310: k=0;
! 3311: for(i=1; i<= (nlstate); i++){
! 3312: for(j=1; j<=(nlstate+ndeath);j++){
! 3313: k=k+1;
! 3314: gp[k]=pmmij[i][j];
! 3315: }
! 3316: }
! 3317:
! 3318: for(i=1; i<=npar; i++)
! 3319: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
! 3320:
! 3321: pmij(pmmij,cov,ncovmodel,xp,nlstate);
! 3322: k=0;
! 3323: for(i=1; i<=(nlstate); i++){
! 3324: for(j=1; j<=(nlstate+ndeath);j++){
! 3325: k=k+1;
! 3326: gm[k]=pmmij[i][j];
! 3327: }
! 3328: }
! 3329:
! 3330: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
! 3331: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
! 3332: }
! 3333:
! 3334: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
! 3335: for(theta=1; theta <=npar; theta++)
! 3336: trgradg[j][theta]=gradg[theta][j];
! 3337:
! 3338: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
! 3339: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
! 3340: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
! 3341: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
! 3342: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
! 3343: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
! 3344:
! 3345: pmij(pmmij,cov,ncovmodel,x,nlstate);
! 3346:
! 3347: k=0;
! 3348: for(i=1; i<=(nlstate); i++){
! 3349: for(j=1; j<=(nlstate+ndeath);j++){
! 3350: k=k+1;
! 3351: mu[k][(int) age]=pmmij[i][j];
! 3352: }
! 3353: }
! 3354: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
! 3355: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
! 3356: varpij[i][j][(int)age] = doldm[i][j];
! 3357:
! 3358: /*printf("\n%d ",(int)age);
! 3359: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
! 3360: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
! 3361: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
! 3362: }*/
! 3363:
! 3364: fprintf(ficresprob,"\n%d ",(int)age);
! 3365: fprintf(ficresprobcov,"\n%d ",(int)age);
! 3366: fprintf(ficresprobcor,"\n%d ",(int)age);
! 3367:
! 3368: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
! 3369: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
! 3370: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
! 3371: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
! 3372: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
! 3373: }
! 3374: i=0;
! 3375: for (k=1; k<=(nlstate);k++){
! 3376: for (l=1; l<=(nlstate+ndeath);l++){
! 3377: i=i++;
! 3378: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
! 3379: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
! 3380: for (j=1; j<=i;j++){
! 3381: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
! 3382: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
! 3383: }
! 3384: }
! 3385: }/* end of loop for state */
! 3386: } /* end of loop for age */
! 3387:
! 3388: /* Confidence intervalle of pij */
! 3389: /*
! 3390: fprintf(ficgp,"\nset noparametric;unset label");
! 3391: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
! 3392: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
! 3393: fprintf(fichtm,"\n<br>Probability with confidence intervals expressed in year<sup>-1</sup> :<a href=\"pijgr%s.png\">pijgr%s.png</A>, ",optionfilefiname,optionfilefiname);
! 3394: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
! 3395: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
! 3396: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
! 3397: */
! 3398:
! 3399: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
! 3400: first1=1;
! 3401: for (k2=1; k2<=(nlstate);k2++){
! 3402: for (l2=1; l2<=(nlstate+ndeath);l2++){
! 3403: if(l2==k2) continue;
! 3404: j=(k2-1)*(nlstate+ndeath)+l2;
! 3405: for (k1=1; k1<=(nlstate);k1++){
! 3406: for (l1=1; l1<=(nlstate+ndeath);l1++){
! 3407: if(l1==k1) continue;
! 3408: i=(k1-1)*(nlstate+ndeath)+l1;
! 3409: if(i<=j) continue;
! 3410: for (age=bage; age<=fage; age ++){
! 3411: if ((int)age %5==0){
! 3412: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
! 3413: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
! 3414: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
! 3415: mu1=mu[i][(int) age]/stepm*YEARM ;
! 3416: mu2=mu[j][(int) age]/stepm*YEARM;
! 3417: c12=cv12/sqrt(v1*v2);
! 3418: /* Computing eigen value of matrix of covariance */
! 3419: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
! 3420: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
! 3421: /* Eigen vectors */
! 3422: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
! 3423: /*v21=sqrt(1.-v11*v11); *//* error */
! 3424: v21=(lc1-v1)/cv12*v11;
! 3425: v12=-v21;
! 3426: v22=v11;
! 3427: tnalp=v21/v11;
! 3428: if(first1==1){
! 3429: first1=0;
! 3430: printf("%d %d%d-%d%d mu %.4e %.4e Var %.4e %.4e cor %.3f cov %.4e Eig %.3e %.3e 1stv %.3f %.3f tang %.3f\nOthers in log...\n",(int) age,k1,l1,k2,l2,mu1,mu2,v1,v2,c12,cv12,lc1,lc2,v11,v21,tnalp);
! 3431: }
! 3432: fprintf(ficlog,"%d %d%d-%d%d mu %.4e %.4e Var %.4e %.4e cor %.3f cov %.4e Eig %.3e %.3e 1stv %.3f %.3f tan %.3f\n",(int) age,k1,l1,k2,l2,mu1,mu2,v1,v2,c12,cv12,lc1,lc2,v11,v21,tnalp);
! 3433: /*printf(fignu*/
! 3434: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
! 3435: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
! 3436: if(first==1){
! 3437: first=0;
! 3438: fprintf(ficgp,"\nset parametric;unset label");
! 3439: fprintf(ficgp,"\nset log y;set log x; set xlabel \"p%1d%1d (year-1)\";set ylabel \"p%1d%1d (year-1)\"",k1,l1,k2,l2);
! 3440: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
! 3441: fprintf(fichtmcov,"\n<br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
! 3442: :<a href=\"%s%d%1d%1d-%1d%1d.png\">\
! 3443: %s%d%1d%1d-%1d%1d.png</A>, ",k1,l1,k2,l2,\
! 3444: subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2,\
! 3445: subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
! 3446: fprintf(fichtmcov,"\n<br><img src=\"%s%d%1d%1d-%1d%1d.png\"> ",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
! 3447: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
! 3448: fprintf(ficgp,"\nset out \"%s%d%1d%1d-%1d%1d.png\"",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
! 3449: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
! 3450: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
! 3451: fprintf(ficgp,"\nplot [-pi:pi] %11.3e+ %.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)), %11.3e +%.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)) not",\
! 3452: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),\
! 3453: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
! 3454: }else{
! 3455: first=0;
! 3456: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
! 3457: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
! 3458: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
! 3459: fprintf(ficgp,"\nreplot %11.3e+ %.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)), %11.3e +%.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)) not",\
! 3460: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),\
! 3461: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
! 3462: }/* if first */
! 3463: } /* age mod 5 */
! 3464: } /* end loop age */
! 3465: fprintf(ficgp,"\nset out \"%s%d%1d%1d-%1d%1d.png\";replot;",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
! 3466: first=1;
! 3467: } /*l12 */
! 3468: } /* k12 */
! 3469: } /*l1 */
! 3470: }/* k1 */
! 3471: } /* loop covariates */
! 3472: }
! 3473: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
! 3474: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
! 3475: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
! 3476: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
! 3477: free_vector(xp,1,npar);
! 3478: fclose(ficresprob);
! 3479: fclose(ficresprobcov);
! 3480: fclose(ficresprobcor);
! 3481: fflush(ficgp);
! 3482: fflush(fichtmcov);
! 3483: }
! 3484:
! 3485:
! 3486: /******************* Printing html file ***********/
! 3487: void printinghtml(char fileres[], char title[], char datafile[], int firstpass, \
! 3488: int lastpass, int stepm, int weightopt, char model[],\
! 3489: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
! 3490: int popforecast, int estepm ,\
! 3491: double jprev1, double mprev1,double anprev1, \
! 3492: double jprev2, double mprev2,double anprev2){
! 3493: int jj1, k1, i1, cpt;
! 3494:
! 3495: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
! 3496: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
! 3497: </ul>");
! 3498: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n \
! 3499: - Observed prevalence in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): <a href=\"%s\">%s</a> <br>\n ",
! 3500: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirf2(fileres,"p"),subdirf2(fileres,"p"));
! 3501: fprintf(fichtm,"\
! 3502: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
! 3503: stepm,subdirf2(fileres,"pij"),subdirf2(fileres,"pij"));
! 3504: fprintf(fichtm,"\
! 3505: - Period (stable) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
! 3506: subdirf2(fileres,"pl"),subdirf2(fileres,"pl"));
! 3507: fprintf(fichtm,"\
! 3508: - (a) Life expectancies by health status at initial age, (b) health expectancies by health status at initial age: ei., eij . If one or more covariate are included, specific tables for each value of the covariate are output in sequences within the same file (estepm=%2d months): \
! 3509: <a href=\"%s\">%s</a> <br>\n",
! 3510: estepm,subdirf2(fileres,"e"),subdirf2(fileres,"e"));
! 3511: fprintf(fichtm,"\
! 3512: - Population projections by age and states: \
! 3513: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileres,"f"),subdirf2(fileres,"f"));
! 3514:
! 3515: fprintf(fichtm," \n<ul><li><b>Graphs</b></li><p>");
! 3516:
! 3517: m=cptcoveff;
! 3518: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
! 3519:
! 3520: jj1=0;
! 3521: for(k1=1; k1<=m;k1++){
! 3522: for(i1=1; i1<=ncodemax[k1];i1++){
! 3523: jj1++;
! 3524: if (cptcovn > 0) {
! 3525: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
! 3526: for (cpt=1; cpt<=cptcoveff;cpt++)
! 3527: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtab[jj1][cpt]]);
! 3528: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
! 3529: }
! 3530: /* Pij */
! 3531: fprintf(fichtm,"<br>- Pij or Conditional probabilities to be observed in state j being in state i, %d (stepm) months before: <a href=\"%s%d1.png\">%s%d1.png</a><br> \
! 3532: <img src=\"%s%d1.png\">",stepm,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1);
! 3533: /* Quasi-incidences */
! 3534: fprintf(fichtm,"<br>- Pij or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
! 3535: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too: <a href=\"%s%d2.png\">%s%d2.png</a><br> \
! 3536: <img src=\"%s%d2.png\">",stepm,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1);
! 3537: /* Period (stable) prevalence in each health state */
! 3538: for(cpt=1; cpt<nlstate;cpt++){
! 3539: fprintf(fichtm,"<br>- Period (stable) prevalence in each health state : <a href=\"%s%d%d.png\">%s%d%d.png</a><br> \
! 3540: <img src=\"%s%d%d.png\">",subdirf2(optionfilefiname,"p"),cpt,jj1,subdirf2(optionfilefiname,"p"),cpt,jj1,subdirf2(optionfilefiname,"p"),cpt,jj1);
! 3541: }
! 3542: for(cpt=1; cpt<=nlstate;cpt++) {
! 3543: fprintf(fichtm,"\n<br>- Life expectancy by health state (%d) at initial age and its decomposition into health expectancies : <a href=\"%s%d%d.png\">%s%d%d.png</a> <br> \
! 3544: <img src=\"%s%d%d.png\">",cpt,subdirf2(optionfilefiname,"exp"),cpt,jj1,subdirf2(optionfilefiname,"exp"),cpt,jj1,subdirf2(optionfilefiname,"exp"),cpt,jj1);
! 3545: }
! 3546: } /* end i1 */
! 3547: }/* End k1 */
! 3548: fprintf(fichtm,"</ul>");
! 3549:
! 3550:
! 3551: fprintf(fichtm,"\
! 3552: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
! 3553: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br>\n", rfileres,rfileres);
! 3554:
! 3555: fprintf(fichtm," - Variance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
! 3556: subdirf2(fileres,"prob"),subdirf2(fileres,"prob"));
! 3557: fprintf(fichtm,"\
! 3558: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
! 3559: subdirf2(fileres,"probcov"),subdirf2(fileres,"probcov"));
! 3560:
! 3561: fprintf(fichtm,"\
! 3562: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
! 3563: subdirf2(fileres,"probcor"),subdirf2(fileres,"probcor"));
! 3564: fprintf(fichtm,"\
! 3565: - Variances and covariances of health expectancies by age and <b>initial health status</b> (cov(e<sup>ij</sup>,e<sup>kl</sup>)(estepm=%2d months): \
! 3566: <a href=\"%s\">%s</a> <br>\n</li>",
! 3567: estepm,subdirf2(fileres,"cve"),subdirf2(fileres,"cve"));
! 3568: fprintf(fichtm,"\
! 3569: - (a) Health expectancies by health status at initial age (e<sup>ij</sup>) and standard errors (in parentheses) (b) life expectancies and standard errors (e<sup>i.</sup>=e<sup>i1</sup>+e<sup>i2</sup>+...)(estepm=%2d months): \
! 3570: <a href=\"%s\">%s</a> <br>\n</li>",
! 3571: estepm,subdirf2(fileres,"stde"),subdirf2(fileres,"stde"));
! 3572: fprintf(fichtm,"\
! 3573: - Variances and covariances of health expectancies by age. Status (i) based health expectancies (in state j), eij are weighted by the period prevalences in each state i (if popbased=1, an additional computation is done using the cross-sectional prevalences (i.e population based) (estepm=%d months): <a href=\"%s\">%s</a><br>\n",
! 3574: estepm, subdirf2(fileres,"v"),subdirf2(fileres,"v"));
! 3575: fprintf(fichtm,"\
! 3576: - Total life expectancy and total health expectancies to be spent in each health state e<sup>.j</sup> with their standard errors: <a href=\"%s\">%s</a> <br>\n",
! 3577: subdirf2(fileres,"t"),subdirf2(fileres,"t"));
! 3578: fprintf(fichtm,"\
! 3579: - Standard deviation of period (stable) prevalences: <a href=\"%s\">%s</a> <br>\n",\
! 3580: subdirf2(fileres,"vpl"),subdirf2(fileres,"vpl"));
! 3581:
! 3582: /* if(popforecast==1) fprintf(fichtm,"\n */
! 3583: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
! 3584: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
! 3585: /* <br>",fileres,fileres,fileres,fileres); */
! 3586: /* else */
! 3587: /* fprintf(fichtm,"\n No population forecast: popforecast = %d (instead of 1) or stepm = %d (instead of 1) or model=%s (instead of .)<br><br></li>\n",popforecast, stepm, model); */
! 3588: fflush(fichtm);
! 3589: fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
! 3590:
! 3591: m=cptcoveff;
! 3592: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
! 3593:
! 3594: jj1=0;
! 3595: for(k1=1; k1<=m;k1++){
! 3596: for(i1=1; i1<=ncodemax[k1];i1++){
! 3597: jj1++;
! 3598: if (cptcovn > 0) {
! 3599: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
! 3600: for (cpt=1; cpt<=cptcoveff;cpt++)
! 3601: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtab[jj1][cpt]]);
! 3602: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
! 3603: }
! 3604: for(cpt=1; cpt<=nlstate;cpt++) {
! 3605: fprintf(fichtm,"<br>- Observed (cross-sectional) and period (incidence based) \
! 3606: prevalence (with 95%% confidence interval) in state (%d): %s%d%d.png <br>\
! 3607: <img src=\"%s%d%d.png\">",cpt,subdirf2(optionfilefiname,"v"),cpt,jj1,subdirf2(optionfilefiname,"v"),cpt,jj1);
! 3608: }
! 3609: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
! 3610: health expectancies in states (1) and (2): %s%d.png<br>\
! 3611: <img src=\"%s%d.png\">",subdirf2(optionfilefiname,"e"),jj1,subdirf2(optionfilefiname,"e"),jj1);
! 3612: } /* end i1 */
! 3613: }/* End k1 */
! 3614: fprintf(fichtm,"</ul>");
! 3615: fflush(fichtm);
! 3616: }
! 3617:
! 3618: /******************* Gnuplot file **************/
! 3619: void printinggnuplot(char fileres[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
! 3620:
! 3621: char dirfileres[132],optfileres[132];
! 3622: int m,cpt,k1,i,k,j,jk,k2,k3,ij,l;
! 3623: int ng;
! 3624: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
! 3625: /* printf("Problem with file %s",optionfilegnuplot); */
! 3626: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
! 3627: /* } */
! 3628:
! 3629: /*#ifdef windows */
! 3630: fprintf(ficgp,"cd \"%s\" \n",pathc);
! 3631: /*#endif */
! 3632: m=pow(2,cptcoveff);
! 3633:
! 3634: strcpy(dirfileres,optionfilefiname);
! 3635: strcpy(optfileres,"vpl");
! 3636: /* 1eme*/
! 3637: for (cpt=1; cpt<= nlstate ; cpt ++) {
! 3638: for (k1=1; k1<= m ; k1 ++) {
! 3639: fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"v"),cpt,k1);
! 3640: fprintf(ficgp,"\n#set out \"v%s%d%d.png\" \n",optionfilefiname,cpt,k1);
! 3641: fprintf(ficgp,"set xlabel \"Age\" \n\
! 3642: set ylabel \"Probability\" \n\
! 3643: set ter png small\n\
! 3644: set size 0.65,0.65\n\
! 3645: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:2 \"\%%lf",ageminpar,fage,subdirf2(fileres,"vpl"),k1-1,k1-1);
! 3646:
! 3647: for (i=1; i<= nlstate ; i ++) {
! 3648: if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
! 3649: else fprintf(ficgp," \%%*lf (\%%*lf)");
! 3650: }
! 3651: fprintf(ficgp,"\" t\"Period (stable) prevalence\" w l 0,\"%s\" every :::%d::%d u 1:($2+1.96*$3) \"\%%lf",subdirf2(fileres,"vpl"),k1-1,k1-1);
! 3652: for (i=1; i<= nlstate ; i ++) {
! 3653: if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
! 3654: else fprintf(ficgp," \%%*lf (\%%*lf)");
! 3655: }
! 3656: fprintf(ficgp,"\" t\"95\%% CI\" w l 1,\"%s\" every :::%d::%d u 1:($2-1.96*$3) \"\%%lf",subdirf2(fileres,"vpl"),k1-1,k1-1);
! 3657: for (i=1; i<= nlstate ; i ++) {
! 3658: if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
! 3659: else fprintf(ficgp," \%%*lf (\%%*lf)");
! 3660: }
! 3661: fprintf(ficgp,"\" t\"\" w l 1,\"%s\" every :::%d::%d u 1:($%d) t\"Observed prevalence \" w l 2",subdirf2(fileres,"p"),k1-1,k1-1,2+4*(cpt-1));
! 3662: }
! 3663: }
! 3664: /*2 eme*/
! 3665:
! 3666: for (k1=1; k1<= m ; k1 ++) {
! 3667: fprintf(ficgp,"\nset out \"%s%d.png\" \n",subdirf2(optionfilefiname,"e"),k1);
! 3668: fprintf(ficgp,"set ylabel \"Years\" \nset ter png small\nset size 0.65,0.65\nplot [%.f:%.f] ",ageminpar,fage);
! 3669:
! 3670: for (i=1; i<= nlstate+1 ; i ++) {
! 3671: k=2*i;
! 3672: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:2 \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1);
! 3673: for (j=1; j<= nlstate+1 ; j ++) {
! 3674: if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
! 3675: else fprintf(ficgp," \%%*lf (\%%*lf)");
! 3676: }
! 3677: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l ,");
! 3678: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l ,",i-1);
! 3679: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2-$3*2) \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1);
! 3680: for (j=1; j<= nlstate+1 ; j ++) {
! 3681: if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
! 3682: else fprintf(ficgp," \%%*lf (\%%*lf)");
! 3683: }
! 3684: fprintf(ficgp,"\" t\"\" w l 0,");
! 3685: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2+$3*2) \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1);
! 3686: for (j=1; j<= nlstate+1 ; j ++) {
! 3687: if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
! 3688: else fprintf(ficgp," \%%*lf (\%%*lf)");
! 3689: }
! 3690: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l 0");
! 3691: else fprintf(ficgp,"\" t\"\" w l 0,");
! 3692: }
! 3693: }
! 3694:
! 3695: /*3eme*/
! 3696:
! 3697: for (k1=1; k1<= m ; k1 ++) {
! 3698: for (cpt=1; cpt<= nlstate ; cpt ++) {
! 3699: /* k=2+nlstate*(2*cpt-2); */
! 3700: k=2+(nlstate+1)*(cpt-1);
! 3701: fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"exp"),cpt,k1);
! 3702: fprintf(ficgp,"set ter png small\n\
! 3703: set size 0.65,0.65\n\
! 3704: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",ageminpar,fage,subdirf2(fileres,"e"),k1-1,k1-1,k,cpt);
! 3705: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
! 3706: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
! 3707: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
! 3708: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
! 3709: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
! 3710: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
! 3711:
! 3712: */
! 3713: for (i=1; i< nlstate ; i ++) {
! 3714: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+i,cpt,i+1);
! 3715: /* fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+2*i,cpt,i+1);*/
! 3716:
! 3717: }
! 3718: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+nlstate,cpt);
! 3719: }
! 3720: }
! 3721:
! 3722: /* CV preval stable (period) */
! 3723: for (k1=1; k1<= m ; k1 ++) {
! 3724: for (cpt=1; cpt<=nlstate ; cpt ++) {
! 3725: k=3;
! 3726: fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"p"),cpt,k1);
! 3727: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
! 3728: set ter png small\nset size 0.65,0.65\n\
! 3729: unset log y\n\
! 3730: plot [%.f:%.f] \"%s\" u ($1==%d ? ($3):1/0):($%d/($%d",ageminpar,agemaxpar,subdirf2(fileres,"pij"),k1,k+cpt+1,k+1);
! 3731:
! 3732: for (i=1; i< nlstate ; i ++)
! 3733: fprintf(ficgp,"+$%d",k+i+1);
! 3734: fprintf(ficgp,")) t\"prev(%d,%d)\" w l",cpt,cpt+1);
! 3735:
! 3736: l=3+(nlstate+ndeath)*cpt;
! 3737: fprintf(ficgp,",\"%s\" u ($1==%d ? ($3):1/0):($%d/($%d",subdirf2(fileres,"pij"),k1,l+cpt+1,l+1);
! 3738: for (i=1; i< nlstate ; i ++) {
! 3739: l=3+(nlstate+ndeath)*cpt;
! 3740: fprintf(ficgp,"+$%d",l+i+1);
! 3741: }
! 3742: fprintf(ficgp,")) t\"prev(%d,%d)\" w l\n",cpt+1,cpt+1);
! 3743: }
! 3744: }
! 3745:
! 3746: /* proba elementaires */
! 3747: for(i=1,jk=1; i <=nlstate; i++){
! 3748: for(k=1; k <=(nlstate+ndeath); k++){
! 3749: if (k != i) {
! 3750: for(j=1; j <=ncovmodel; j++){
! 3751: fprintf(ficgp,"p%d=%f ",jk,p[jk]);
! 3752: jk++;
! 3753: fprintf(ficgp,"\n");
! 3754: }
! 3755: }
! 3756: }
! 3757: }
! 3758:
! 3759: for(ng=1; ng<=2;ng++){ /* Number of graphics: first is probabilities second is incidence per year*/
! 3760: for(jk=1; jk <=m; jk++) {
! 3761: fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"pe"),jk,ng);
! 3762: if (ng==2)
! 3763: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
! 3764: else
! 3765: fprintf(ficgp,"\nset title \"Probability\"\n");
! 3766: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65\nset log y\nplot [%.f:%.f] ",ageminpar,agemaxpar);
! 3767: i=1;
! 3768: for(k2=1; k2<=nlstate; k2++) {
! 3769: k3=i;
! 3770: for(k=1; k<=(nlstate+ndeath); k++) {
! 3771: if (k != k2){
! 3772: if(ng==2)
! 3773: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
! 3774: else
! 3775: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
! 3776: ij=1;
! 3777: for(j=3; j <=ncovmodel; j++) {
! 3778: if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) {
! 3779: fprintf(ficgp,"+p%d*%d*x",i+j-1,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);
! 3780: ij++;
! 3781: }
! 3782: else
! 3783: fprintf(ficgp,"+p%d*%d",i+j-1,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
! 3784: }
! 3785: fprintf(ficgp,")/(1");
! 3786:
! 3787: for(k1=1; k1 <=nlstate; k1++){
! 3788: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
! 3789: ij=1;
! 3790: for(j=3; j <=ncovmodel; j++){
! 3791: if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) {
! 3792: fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);
! 3793: ij++;
! 3794: }
! 3795: else
! 3796: fprintf(ficgp,"+p%d*%d",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
! 3797: }
! 3798: fprintf(ficgp,")");
! 3799: }
! 3800: fprintf(ficgp,") t \"p%d%d\" ", k2,k);
! 3801: if ((k+k2)!= (nlstate*2+ndeath)) fprintf(ficgp,",");
! 3802: i=i+ncovmodel;
! 3803: }
! 3804: } /* end k */
! 3805: } /* end k2 */
! 3806: } /* end jk */
! 3807: } /* end ng */
! 3808: fflush(ficgp);
! 3809: } /* end gnuplot */
! 3810:
! 3811:
! 3812: /*************** Moving average **************/
! 3813: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav){
! 3814:
! 3815: int i, cpt, cptcod;
! 3816: int modcovmax =1;
! 3817: int mobilavrange, mob;
! 3818: double age;
! 3819:
! 3820: modcovmax=2*cptcoveff;/* Max number of modalities. We suppose
! 3821: a covariate has 2 modalities */
! 3822: if (cptcovn<1) modcovmax=1; /* At least 1 pass */
! 3823:
! 3824: if(mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
! 3825: if(mobilav==1) mobilavrange=5; /* default */
! 3826: else mobilavrange=mobilav;
! 3827: for (age=bage; age<=fage; age++)
! 3828: for (i=1; i<=nlstate;i++)
! 3829: for (cptcod=1;cptcod<=modcovmax;cptcod++)
! 3830: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
! 3831: /* We keep the original values on the extreme ages bage, fage and for
! 3832: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
! 3833: we use a 5 terms etc. until the borders are no more concerned.
! 3834: */
! 3835: for (mob=3;mob <=mobilavrange;mob=mob+2){
! 3836: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
! 3837: for (i=1; i<=nlstate;i++){
! 3838: for (cptcod=1;cptcod<=modcovmax;cptcod++){
! 3839: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
! 3840: for (cpt=1;cpt<=(mob-1)/2;cpt++){
! 3841: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
! 3842: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
! 3843: }
! 3844: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
! 3845: }
! 3846: }
! 3847: }/* end age */
! 3848: }/* end mob */
! 3849: }else return -1;
! 3850: return 0;
! 3851: }/* End movingaverage */
! 3852:
! 3853:
! 3854: /************** Forecasting ******************/
! 3855: prevforecast(char fileres[], double anproj1, double mproj1, double jproj1, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double bage, double fage, int firstpass, int lastpass, double anproj2, double p[], int cptcoveff){
! 3856: /* proj1, year, month, day of starting projection
! 3857: agemin, agemax range of age
! 3858: dateprev1 dateprev2 range of dates during which prevalence is computed
! 3859: anproj2 year of en of projection (same day and month as proj1).
! 3860: */
! 3861: int yearp, stepsize, hstepm, nhstepm, j, k, c, cptcod, i, h, i1;
! 3862: int *popage;
! 3863: double agec; /* generic age */
! 3864: double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
! 3865: double *popeffectif,*popcount;
! 3866: double ***p3mat;
! 3867: double ***mobaverage;
! 3868: char fileresf[FILENAMELENGTH];
! 3869:
! 3870: agelim=AGESUP;
! 3871: prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
! 3872:
! 3873: strcpy(fileresf,"f");
! 3874: strcat(fileresf,fileres);
! 3875: if((ficresf=fopen(fileresf,"w"))==NULL) {
! 3876: printf("Problem with forecast resultfile: %s\n", fileresf);
! 3877: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
! 3878: }
! 3879: printf("Computing forecasting: result on file '%s' \n", fileresf);
! 3880: fprintf(ficlog,"Computing forecasting: result on file '%s' \n", fileresf);
! 3881:
! 3882: if (cptcoveff==0) ncodemax[cptcoveff]=1;
! 3883:
! 3884: if (mobilav!=0) {
! 3885: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
! 3886: if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){
! 3887: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
! 3888: printf(" Error in movingaverage mobilav=%d\n",mobilav);
! 3889: }
! 3890: }
! 3891:
! 3892: stepsize=(int) (stepm+YEARM-1)/YEARM;
! 3893: if (stepm<=12) stepsize=1;
! 3894: if(estepm < stepm){
! 3895: printf ("Problem %d lower than %d\n",estepm, stepm);
! 3896: }
! 3897: else hstepm=estepm;
! 3898:
! 3899: hstepm=hstepm/stepm;
! 3900: yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp and
! 3901: fractional in yp1 */
! 3902: anprojmean=yp;
! 3903: yp2=modf((yp1*12),&yp);
! 3904: mprojmean=yp;
! 3905: yp1=modf((yp2*30.5),&yp);
! 3906: jprojmean=yp;
! 3907: if(jprojmean==0) jprojmean=1;
! 3908: if(mprojmean==0) jprojmean=1;
! 3909:
! 3910: i1=cptcoveff;
! 3911: if (cptcovn < 1){i1=1;}
! 3912:
! 3913: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2);
! 3914:
! 3915: fprintf(ficresf,"#****** Routine prevforecast **\n");
! 3916:
! 3917: /* if (h==(int)(YEARM*yearp)){ */
! 3918: for(cptcov=1, k=0;cptcov<=i1;cptcov++){
! 3919: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
! 3920: k=k+1;
! 3921: fprintf(ficresf,"\n#******");
! 3922: for(j=1;j<=cptcoveff;j++) {
! 3923: fprintf(ficresf," V%d=%d, hpijx=probability over h years, hp.jx is weighted by observed prev ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 3924: }
! 3925: fprintf(ficresf,"******\n");
! 3926: fprintf(ficresf,"# Covariate valuofcovar yearproj age");
! 3927: for(j=1; j<=nlstate+ndeath;j++){
! 3928: for(i=1; i<=nlstate;i++)
! 3929: fprintf(ficresf," p%d%d",i,j);
! 3930: fprintf(ficresf," p.%d",j);
! 3931: }
! 3932: for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) {
! 3933: fprintf(ficresf,"\n");
! 3934: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);
! 3935:
! 3936: for (agec=fage; agec>=(ageminpar-1); agec--){
! 3937: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
! 3938: nhstepm = nhstepm/hstepm;
! 3939: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 3940: oldm=oldms;savm=savms;
! 3941: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k);
! 3942:
! 3943: for (h=0; h<=nhstepm; h++){
! 3944: if (h*hstepm/YEARM*stepm ==yearp) {
! 3945: fprintf(ficresf,"\n");
! 3946: for(j=1;j<=cptcoveff;j++)
! 3947: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 3948: fprintf(ficresf,"%.f %.f ",anproj1+yearp,agec+h*hstepm/YEARM*stepm);
! 3949: }
! 3950: for(j=1; j<=nlstate+ndeath;j++) {
! 3951: ppij=0.;
! 3952: for(i=1; i<=nlstate;i++) {
! 3953: if (mobilav==1)
! 3954: ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod];
! 3955: else {
! 3956: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod];
! 3957: }
! 3958: if (h*hstepm/YEARM*stepm== yearp) {
! 3959: fprintf(ficresf," %.3f", p3mat[i][j][h]);
! 3960: }
! 3961: } /* end i */
! 3962: if (h*hstepm/YEARM*stepm==yearp) {
! 3963: fprintf(ficresf," %.3f", ppij);
! 3964: }
! 3965: }/* end j */
! 3966: } /* end h */
! 3967: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 3968: } /* end agec */
! 3969: } /* end yearp */
! 3970: } /* end cptcod */
! 3971: } /* end cptcov */
! 3972:
! 3973: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
! 3974:
! 3975: fclose(ficresf);
! 3976: }
! 3977:
! 3978: /************** Forecasting *****not tested NB*************/
! 3979: populforecast(char fileres[], double anpyram,double mpyram,double jpyram,double ageminpar, double agemax,double dateprev1, double dateprev2, int mobilav, double agedeb, double fage, int popforecast, char popfile[], double anpyram1,double p[], int i2){
! 3980:
! 3981: int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h;
! 3982: int *popage;
! 3983: double calagedatem, agelim, kk1, kk2;
! 3984: double *popeffectif,*popcount;
! 3985: double ***p3mat,***tabpop,***tabpopprev;
! 3986: double ***mobaverage;
! 3987: char filerespop[FILENAMELENGTH];
! 3988:
! 3989: tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
! 3990: tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
! 3991: agelim=AGESUP;
! 3992: calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM;
! 3993:
! 3994: prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
! 3995:
! 3996:
! 3997: strcpy(filerespop,"pop");
! 3998: strcat(filerespop,fileres);
! 3999: if((ficrespop=fopen(filerespop,"w"))==NULL) {
! 4000: printf("Problem with forecast resultfile: %s\n", filerespop);
! 4001: fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop);
! 4002: }
! 4003: printf("Computing forecasting: result on file '%s' \n", filerespop);
! 4004: fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop);
! 4005:
! 4006: if (cptcoveff==0) ncodemax[cptcoveff]=1;
! 4007:
! 4008: if (mobilav!=0) {
! 4009: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
! 4010: if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){
! 4011: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
! 4012: printf(" Error in movingaverage mobilav=%d\n",mobilav);
! 4013: }
! 4014: }
! 4015:
! 4016: stepsize=(int) (stepm+YEARM-1)/YEARM;
! 4017: if (stepm<=12) stepsize=1;
! 4018:
! 4019: agelim=AGESUP;
! 4020:
! 4021: hstepm=1;
! 4022: hstepm=hstepm/stepm;
! 4023:
! 4024: if (popforecast==1) {
! 4025: if((ficpop=fopen(popfile,"r"))==NULL) {
! 4026: printf("Problem with population file : %s\n",popfile);exit(0);
! 4027: fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0);
! 4028: }
! 4029: popage=ivector(0,AGESUP);
! 4030: popeffectif=vector(0,AGESUP);
! 4031: popcount=vector(0,AGESUP);
! 4032:
! 4033: i=1;
! 4034: while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1;
! 4035:
! 4036: imx=i;
! 4037: for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i];
! 4038: }
! 4039:
! 4040: for(cptcov=1,k=0;cptcov<=i2;cptcov++){
! 4041: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
! 4042: k=k+1;
! 4043: fprintf(ficrespop,"\n#******");
! 4044: for(j=1;j<=cptcoveff;j++) {
! 4045: fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 4046: }
! 4047: fprintf(ficrespop,"******\n");
! 4048: fprintf(ficrespop,"# Age");
! 4049: for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j);
! 4050: if (popforecast==1) fprintf(ficrespop," [Population]");
! 4051:
! 4052: for (cpt=0; cpt<=0;cpt++) {
! 4053: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
! 4054:
! 4055: for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){
! 4056: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
! 4057: nhstepm = nhstepm/hstepm;
! 4058:
! 4059: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 4060: oldm=oldms;savm=savms;
! 4061: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
! 4062:
! 4063: for (h=0; h<=nhstepm; h++){
! 4064: if (h==(int) (calagedatem+YEARM*cpt)) {
! 4065: fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
! 4066: }
! 4067: for(j=1; j<=nlstate+ndeath;j++) {
! 4068: kk1=0.;kk2=0;
! 4069: for(i=1; i<=nlstate;i++) {
! 4070: if (mobilav==1)
! 4071: kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod];
! 4072: else {
! 4073: kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod];
! 4074: }
! 4075: }
! 4076: if (h==(int)(calagedatem+12*cpt)){
! 4077: tabpop[(int)(agedeb)][j][cptcod]=kk1;
! 4078: /*fprintf(ficrespop," %.3f", kk1);
! 4079: if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*/
! 4080: }
! 4081: }
! 4082: for(i=1; i<=nlstate;i++){
! 4083: kk1=0.;
! 4084: for(j=1; j<=nlstate;j++){
! 4085: kk1= kk1+tabpop[(int)(agedeb)][j][cptcod];
! 4086: }
! 4087: tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)];
! 4088: }
! 4089:
! 4090: if (h==(int)(calagedatem+12*cpt)) for(j=1; j<=nlstate;j++)
! 4091: fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]);
! 4092: }
! 4093: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 4094: }
! 4095: }
! 4096:
! 4097: /******/
! 4098:
! 4099: for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) {
! 4100: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
! 4101: for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){
! 4102: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
! 4103: nhstepm = nhstepm/hstepm;
! 4104:
! 4105: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 4106: oldm=oldms;savm=savms;
! 4107: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
! 4108: for (h=0; h<=nhstepm; h++){
! 4109: if (h==(int) (calagedatem+YEARM*cpt)) {
! 4110: fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
! 4111: }
! 4112: for(j=1; j<=nlstate+ndeath;j++) {
! 4113: kk1=0.;kk2=0;
! 4114: for(i=1; i<=nlstate;i++) {
! 4115: kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod];
! 4116: }
! 4117: if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1);
! 4118: }
! 4119: }
! 4120: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 4121: }
! 4122: }
! 4123: }
! 4124: }
! 4125:
! 4126: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
! 4127:
! 4128: if (popforecast==1) {
! 4129: free_ivector(popage,0,AGESUP);
! 4130: free_vector(popeffectif,0,AGESUP);
! 4131: free_vector(popcount,0,AGESUP);
! 4132: }
! 4133: free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
! 4134: free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
! 4135: fclose(ficrespop);
! 4136: } /* End of popforecast */
! 4137:
! 4138: int fileappend(FILE *fichier, char *optionfich)
! 4139: {
! 4140: if((fichier=fopen(optionfich,"a"))==NULL) {
! 4141: printf("Problem with file: %s\n", optionfich);
! 4142: fprintf(ficlog,"Problem with file: %s\n", optionfich);
! 4143: return (0);
! 4144: }
! 4145: fflush(fichier);
! 4146: return (1);
! 4147: }
! 4148:
! 4149:
! 4150: /**************** function prwizard **********************/
! 4151: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
! 4152: {
! 4153:
! 4154: /* Wizard to print covariance matrix template */
! 4155:
! 4156: char ca[32], cb[32], cc[32];
! 4157: int i,j, k, l, li, lj, lk, ll, jj, npar, itimes;
! 4158: int numlinepar;
! 4159:
! 4160: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
! 4161: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
! 4162: for(i=1; i <=nlstate; i++){
! 4163: jj=0;
! 4164: for(j=1; j <=nlstate+ndeath; j++){
! 4165: if(j==i) continue;
! 4166: jj++;
! 4167: /*ca[0]= k+'a'-1;ca[1]='\0';*/
! 4168: printf("%1d%1d",i,j);
! 4169: fprintf(ficparo,"%1d%1d",i,j);
! 4170: for(k=1; k<=ncovmodel;k++){
! 4171: /* printf(" %lf",param[i][j][k]); */
! 4172: /* fprintf(ficparo," %lf",param[i][j][k]); */
! 4173: printf(" 0.");
! 4174: fprintf(ficparo," 0.");
! 4175: }
! 4176: printf("\n");
! 4177: fprintf(ficparo,"\n");
! 4178: }
! 4179: }
! 4180: printf("# Scales (for hessian or gradient estimation)\n");
! 4181: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
! 4182: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
! 4183: for(i=1; i <=nlstate; i++){
! 4184: jj=0;
! 4185: for(j=1; j <=nlstate+ndeath; j++){
! 4186: if(j==i) continue;
! 4187: jj++;
! 4188: fprintf(ficparo,"%1d%1d",i,j);
! 4189: printf("%1d%1d",i,j);
! 4190: fflush(stdout);
! 4191: for(k=1; k<=ncovmodel;k++){
! 4192: /* printf(" %le",delti3[i][j][k]); */
! 4193: /* fprintf(ficparo," %le",delti3[i][j][k]); */
! 4194: printf(" 0.");
! 4195: fprintf(ficparo," 0.");
! 4196: }
! 4197: numlinepar++;
! 4198: printf("\n");
! 4199: fprintf(ficparo,"\n");
! 4200: }
! 4201: }
! 4202: printf("# Covariance matrix\n");
! 4203: /* # 121 Var(a12)\n\ */
! 4204: /* # 122 Cov(b12,a12) Var(b12)\n\ */
! 4205: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
! 4206: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
! 4207: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
! 4208: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
! 4209: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
! 4210: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
! 4211: fflush(stdout);
! 4212: fprintf(ficparo,"# Covariance matrix\n");
! 4213: /* # 121 Var(a12)\n\ */
! 4214: /* # 122 Cov(b12,a12) Var(b12)\n\ */
! 4215: /* # ...\n\ */
! 4216: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
! 4217:
! 4218: for(itimes=1;itimes<=2;itimes++){
! 4219: jj=0;
! 4220: for(i=1; i <=nlstate; i++){
! 4221: for(j=1; j <=nlstate+ndeath; j++){
! 4222: if(j==i) continue;
! 4223: for(k=1; k<=ncovmodel;k++){
! 4224: jj++;
! 4225: ca[0]= k+'a'-1;ca[1]='\0';
! 4226: if(itimes==1){
! 4227: printf("#%1d%1d%d",i,j,k);
! 4228: fprintf(ficparo,"#%1d%1d%d",i,j,k);
! 4229: }else{
! 4230: printf("%1d%1d%d",i,j,k);
! 4231: fprintf(ficparo,"%1d%1d%d",i,j,k);
! 4232: /* printf(" %.5le",matcov[i][j]); */
! 4233: }
! 4234: ll=0;
! 4235: for(li=1;li <=nlstate; li++){
! 4236: for(lj=1;lj <=nlstate+ndeath; lj++){
! 4237: if(lj==li) continue;
! 4238: for(lk=1;lk<=ncovmodel;lk++){
! 4239: ll++;
! 4240: if(ll<=jj){
! 4241: cb[0]= lk +'a'-1;cb[1]='\0';
! 4242: if(ll<jj){
! 4243: if(itimes==1){
! 4244: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
! 4245: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
! 4246: }else{
! 4247: printf(" 0.");
! 4248: fprintf(ficparo," 0.");
! 4249: }
! 4250: }else{
! 4251: if(itimes==1){
! 4252: printf(" Var(%s%1d%1d)",ca,i,j);
! 4253: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
! 4254: }else{
! 4255: printf(" 0.");
! 4256: fprintf(ficparo," 0.");
! 4257: }
! 4258: }
! 4259: }
! 4260: } /* end lk */
! 4261: } /* end lj */
! 4262: } /* end li */
! 4263: printf("\n");
! 4264: fprintf(ficparo,"\n");
! 4265: numlinepar++;
! 4266: } /* end k*/
! 4267: } /*end j */
! 4268: } /* end i */
! 4269: } /* end itimes */
! 4270:
! 4271: } /* end of prwizard */
! 4272: /******************* Gompertz Likelihood ******************************/
! 4273: double gompertz(double x[])
! 4274: {
! 4275: double A,B,L=0.0,sump=0.,num=0.;
! 4276: int i,n=0; /* n is the size of the sample */
! 4277:
! 4278: for (i=0;i<=imx-1 ; i++) {
! 4279: sump=sump+weight[i];
! 4280: /* sump=sump+1;*/
! 4281: num=num+1;
! 4282: }
! 4283:
! 4284:
! 4285: /* for (i=0; i<=imx; i++)
! 4286: if (wav[i]>0) printf("i=%d ageex=%lf agecens=%lf agedc=%lf cens=%d %d\n" ,i,ageexmed[i],agecens[i],agedc[i],cens[i],wav[i]);*/
! 4287:
! 4288: for (i=1;i<=imx ; i++)
! 4289: {
! 4290: if (cens[i] == 1 && wav[i]>1)
! 4291: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
! 4292:
! 4293: if (cens[i] == 0 && wav[i]>1)
! 4294: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
! 4295: +log(x[1]/YEARM)+x[2]*(agedc[i]-agegomp)+log(YEARM);
! 4296:
! 4297: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
! 4298: if (wav[i] > 1 ) { /* ??? */
! 4299: L=L+A*weight[i];
! 4300: /* printf("\ni=%d A=%f L=%lf x[1]=%lf x[2]=%lf ageex=%lf agecens=%lf cens=%d agedc=%lf weight=%lf\n",i,A,L,x[1],x[2],ageexmed[i]*12,agecens[i]*12,cens[i],agedc[i]*12,weight[i]);*/
! 4301: }
! 4302: }
! 4303:
! 4304: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
! 4305:
! 4306: return -2*L*num/sump;
! 4307: }
! 4308:
! 4309: /******************* Printing html file ***********/
! 4310: void printinghtmlmort(char fileres[], char title[], char datafile[], int firstpass, \
! 4311: int lastpass, int stepm, int weightopt, char model[],\
! 4312: int imx, double p[],double **matcov,double agemortsup){
! 4313: int i,k;
! 4314:
! 4315: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
! 4316: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
! 4317: for (i=1;i<=2;i++)
! 4318: fprintf(fichtm," p[%d] = %lf [%f ; %f]<br>\n",i,p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
! 4319: fprintf(fichtm,"<br><br><img src=\"graphmort.png\">");
! 4320: fprintf(fichtm,"</ul>");
! 4321:
! 4322: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
! 4323:
! 4324: fprintf(fichtm,"\nAge l<inf>x</inf> q<inf>x</inf> d(x,x+1) L<inf>x</inf> T<inf>x</inf> e<infx</inf><br>");
! 4325:
! 4326: for (k=agegomp;k<(agemortsup-2);k++)
! 4327: fprintf(fichtm,"%d %.0lf %lf %.0lf %.0lf %.0lf %lf<br>\n",k,lsurv[k],p[1]*exp(p[2]*(k-agegomp)),(p[1]*exp(p[2]*(k-agegomp)))*lsurv[k],lpop[k],tpop[k],tpop[k]/lsurv[k]);
! 4328:
! 4329:
! 4330: fflush(fichtm);
! 4331: }
! 4332:
! 4333: /******************* Gnuplot file **************/
! 4334: void printinggnuplotmort(char fileres[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
! 4335:
! 4336: char dirfileres[132],optfileres[132];
! 4337: int m,cpt,k1,i,k,j,jk,k2,k3,ij,l;
! 4338: int ng;
! 4339:
! 4340:
! 4341: /*#ifdef windows */
! 4342: fprintf(ficgp,"cd \"%s\" \n",pathc);
! 4343: /*#endif */
! 4344:
! 4345:
! 4346: strcpy(dirfileres,optionfilefiname);
! 4347: strcpy(optfileres,"vpl");
! 4348: fprintf(ficgp,"set out \"graphmort.png\"\n ");
! 4349: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
! 4350: fprintf(ficgp, "set ter png small\n set log y\n");
! 4351: fprintf(ficgp, "set size 0.65,0.65\n");
! 4352: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
! 4353:
! 4354: }
! 4355:
! 4356:
! 4357:
! 4358:
! 4359:
! 4360: /***********************************************/
! 4361: /**************** Main Program *****************/
! 4362: /***********************************************/
! 4363:
! 4364: int main(int argc, char *argv[])
! 4365: {
! 4366: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
! 4367: int i,j, k, n=MAXN,iter,m,size=100,cptcode, cptcod;
! 4368: int linei, month, year,iout;
! 4369: int jj, ll, li, lj, lk, imk;
! 4370: int numlinepar=0; /* Current linenumber of parameter file */
! 4371: int itimes;
! 4372: int NDIM=2;
! 4373:
! 4374: char ca[32], cb[32], cc[32];
! 4375: char dummy[]=" ";
! 4376: /* FILE *fichtm; *//* Html File */
! 4377: /* FILE *ficgp;*/ /*Gnuplot File */
! 4378: struct stat info;
! 4379: double agedeb, agefin,hf;
! 4380: double ageminpar=1.e20,agemin=1.e20, agemaxpar=-1.e20, agemax=-1.e20;
! 4381:
! 4382: double fret;
! 4383: double **xi,tmp,delta;
! 4384:
! 4385: double dum; /* Dummy variable */
! 4386: double ***p3mat;
! 4387: double ***mobaverage;
! 4388: int *indx;
! 4389: char line[MAXLINE], linepar[MAXLINE];
! 4390: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE],model[MAXLINE];
! 4391: char pathr[MAXLINE], pathimach[MAXLINE];
! 4392: char **bp, *tok, *val; /* pathtot */
! 4393: int firstobs=1, lastobs=10;
! 4394: int sdeb, sfin; /* Status at beginning and end */
! 4395: int c, h , cpt,l;
! 4396: int ju,jl, mi;
! 4397: int i1,j1, k1,k2,k3,jk,aa,bb, stepsize, ij;
! 4398: int jnais,jdc,jint4,jint1,jint2,jint3,**outcome,*tab;
! 4399: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
! 4400: int mobilav=0,popforecast=0;
! 4401: int hstepm, nhstepm;
! 4402: int agemortsup;
! 4403: float sumlpop=0.;
! 4404: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
! 4405: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
! 4406:
! 4407: double bage, fage, age, agelim, agebase;
! 4408: double ftolpl=FTOL;
! 4409: double **prlim;
! 4410: double *severity;
! 4411: double ***param; /* Matrix of parameters */
! 4412: double *p;
! 4413: double **matcov; /* Matrix of covariance */
! 4414: double ***delti3; /* Scale */
! 4415: double *delti; /* Scale */
! 4416: double ***eij, ***vareij;
! 4417: double **varpl; /* Variances of prevalence limits by age */
! 4418: double *epj, vepp;
! 4419: double kk1, kk2;
! 4420: double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
! 4421: double **ximort;
! 4422: char *alph[]={"a","a","b","c","d","e"}, str[4];
! 4423: int *dcwave;
! 4424:
! 4425: char z[1]="c", occ;
! 4426:
! 4427: char stra[80], strb[80], strc[80], strd[80],stre[80],modelsav[80];
! 4428: char *strt, strtend[80];
! 4429: char *stratrunc;
! 4430: int lstra;
! 4431:
! 4432: long total_usecs;
! 4433:
! 4434: /* setlocale (LC_ALL, ""); */
! 4435: /* bindtextdomain (PACKAGE, LOCALEDIR); */
! 4436: /* textdomain (PACKAGE); */
! 4437: /* setlocale (LC_CTYPE, ""); */
! 4438: /* setlocale (LC_MESSAGES, ""); */
! 4439:
! 4440: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
! 4441: (void) gettimeofday(&start_time,&tzp);
! 4442: curr_time=start_time;
! 4443: tm = *localtime(&start_time.tv_sec);
! 4444: tmg = *gmtime(&start_time.tv_sec);
! 4445: strcpy(strstart,asctime(&tm));
! 4446:
! 4447: /* printf("Localtime (at start)=%s",strstart); */
! 4448: /* tp.tv_sec = tp.tv_sec +86400; */
! 4449: /* tm = *localtime(&start_time.tv_sec); */
! 4450: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
! 4451: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
! 4452: /* tmg.tm_hour=tmg.tm_hour + 1; */
! 4453: /* tp.tv_sec = mktime(&tmg); */
! 4454: /* strt=asctime(&tmg); */
! 4455: /* printf("Time(after) =%s",strstart); */
! 4456: /* (void) time (&time_value);
! 4457: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
! 4458: * tm = *localtime(&time_value);
! 4459: * strstart=asctime(&tm);
! 4460: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
! 4461: */
! 4462:
! 4463: nberr=0; /* Number of errors and warnings */
! 4464: nbwarn=0;
! 4465: getcwd(pathcd, size);
! 4466:
! 4467: printf("\n%s\n%s",version,fullversion);
! 4468: if(argc <=1){
! 4469: printf("\nEnter the parameter file name: ");
! 4470: fgets(pathr,FILENAMELENGTH,stdin);
! 4471: i=strlen(pathr);
! 4472: if(pathr[i-1]=='\n')
! 4473: pathr[i-1]='\0';
! 4474: for (tok = pathr; tok != NULL; ){
! 4475: printf("Pathr |%s|\n",pathr);
! 4476: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
! 4477: printf("val= |%s| pathr=%s\n",val,pathr);
! 4478: strcpy (pathtot, val);
! 4479: if(pathr[0] == '\0') break; /* Dirty */
! 4480: }
! 4481: }
! 4482: else{
! 4483: strcpy(pathtot,argv[1]);
! 4484: }
! 4485: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
! 4486: /*cygwin_split_path(pathtot,path,optionfile);
! 4487: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
! 4488: /* cutv(path,optionfile,pathtot,'\\');*/
! 4489:
! 4490: /* Split argv[0], imach program to get pathimach */
! 4491: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
! 4492: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
! 4493: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
! 4494: /* strcpy(pathimach,argv[0]); */
! 4495: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
! 4496: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
! 4497: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
! 4498: chdir(path); /* Can be a relative path */
! 4499: if(getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
! 4500: printf("Current directory %s!\n",pathcd);
! 4501: strcpy(command,"mkdir ");
! 4502: strcat(command,optionfilefiname);
! 4503: if((outcmd=system(command)) != 0){
! 4504: printf("Problem creating directory or it already exists %s%s, err=%d\n",path,optionfilefiname,outcmd);
! 4505: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
! 4506: /* fclose(ficlog); */
! 4507: /* exit(1); */
! 4508: }
! 4509: /* if((imk=mkdir(optionfilefiname))<0){ */
! 4510: /* perror("mkdir"); */
! 4511: /* } */
! 4512:
! 4513: /*-------- arguments in the command line --------*/
! 4514:
! 4515: /* Log file */
! 4516: strcat(filelog, optionfilefiname);
! 4517: strcat(filelog,".log"); /* */
! 4518: if((ficlog=fopen(filelog,"w"))==NULL) {
! 4519: printf("Problem with logfile %s\n",filelog);
! 4520: goto end;
! 4521: }
! 4522: fprintf(ficlog,"Log filename:%s\n",filelog);
! 4523: fprintf(ficlog,"\n%s\n%s",version,fullversion);
! 4524: fprintf(ficlog,"\nEnter the parameter file name: \n");
! 4525: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
! 4526: path=%s \n\
! 4527: optionfile=%s\n\
! 4528: optionfilext=%s\n\
! 4529: optionfilefiname=%s\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
! 4530:
! 4531: printf("Local time (at start):%s",strstart);
! 4532: fprintf(ficlog,"Local time (at start): %s",strstart);
! 4533: fflush(ficlog);
! 4534: /* (void) gettimeofday(&curr_time,&tzp); */
! 4535: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tv_sec-start_time.tv_sec,tmpout)); */
! 4536:
! 4537: /* */
! 4538: strcpy(fileres,"r");
! 4539: strcat(fileres, optionfilefiname);
! 4540: strcat(fileres,".txt"); /* Other files have txt extension */
! 4541:
! 4542: /*---------arguments file --------*/
! 4543:
! 4544: if((ficpar=fopen(optionfile,"r"))==NULL) {
! 4545: printf("Problem with optionfile %s\n",optionfile);
! 4546: fprintf(ficlog,"Problem with optionfile %s\n",optionfile);
! 4547: fflush(ficlog);
! 4548: goto end;
! 4549: }
! 4550:
! 4551:
! 4552:
! 4553: strcpy(filereso,"o");
! 4554: strcat(filereso,fileres);
! 4555: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
! 4556: printf("Problem with Output resultfile: %s\n", filereso);
! 4557: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
! 4558: fflush(ficlog);
! 4559: goto end;
! 4560: }
! 4561:
! 4562: /* Reads comments: lines beginning with '#' */
! 4563: numlinepar=0;
! 4564: while((c=getc(ficpar))=='#' && c!= EOF){
! 4565: ungetc(c,ficpar);
! 4566: fgets(line, MAXLINE, ficpar);
! 4567: numlinepar++;
! 4568: puts(line);
! 4569: fputs(line,ficparo);
! 4570: fputs(line,ficlog);
! 4571: }
! 4572: ungetc(c,ficpar);
! 4573:
! 4574: fscanf(ficpar,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%lf stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d model=%s\n",title, datafile, &lastobs, &firstpass,&lastpass,&ftol, &stepm, &ncovcol, &nlstate,&ndeath, &maxwav, &mle, &weightopt,model);
! 4575: numlinepar++;
! 4576: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate,ndeath, maxwav, mle, weightopt,model);
! 4577: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol,nlstate,ndeath,maxwav, mle, weightopt,model);
! 4578: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol,nlstate,ndeath,maxwav, mle, weightopt,model);
! 4579: fflush(ficlog);
! 4580: while((c=getc(ficpar))=='#' && c!= EOF){
! 4581: ungetc(c,ficpar);
! 4582: fgets(line, MAXLINE, ficpar);
! 4583: numlinepar++;
! 4584: puts(line);
! 4585: fputs(line,ficparo);
! 4586: fputs(line,ficlog);
! 4587: }
! 4588: ungetc(c,ficpar);
! 4589:
! 4590:
! 4591: covar=matrix(0,NCOVMAX,1,n);
! 4592: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement*/
! 4593: if (strlen(model)>1) cptcovn=nbocc(model,'+')+1;
! 4594:
! 4595: ncovmodel=2+cptcovn; /*Number of variables = cptcovn + intercept + age */
! 4596: nvar=ncovmodel-1; /* Suppressing age as a basic covariate */
! 4597: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
! 4598:
! 4599: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
! 4600: delti=delti3[1][1];
! 4601: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
! 4602: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
! 4603: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
! 4604: printf(" You choose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
! 4605: fprintf(ficlog," You choose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
! 4606: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
! 4607: fclose (ficparo);
! 4608: fclose (ficlog);
! 4609: goto end;
! 4610: exit(0);
! 4611: }
! 4612: else if(mle==-3) {
! 4613: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
! 4614: printf(" You choose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
! 4615: fprintf(ficlog," You choose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
! 4616: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
! 4617: matcov=matrix(1,npar,1,npar);
! 4618: }
! 4619: else{
! 4620: /* Read guess parameters */
! 4621: /* Reads comments: lines beginning with '#' */
! 4622: while((c=getc(ficpar))=='#' && c!= EOF){
! 4623: ungetc(c,ficpar);
! 4624: fgets(line, MAXLINE, ficpar);
! 4625: numlinepar++;
! 4626: puts(line);
! 4627: fputs(line,ficparo);
! 4628: fputs(line,ficlog);
! 4629: }
! 4630: ungetc(c,ficpar);
! 4631:
! 4632: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
! 4633: for(i=1; i <=nlstate; i++){
! 4634: j=0;
! 4635: for(jj=1; jj <=nlstate+ndeath; jj++){
! 4636: if(jj==i) continue;
! 4637: j++;
! 4638: fscanf(ficpar,"%1d%1d",&i1,&j1);
! 4639: if ((i1 != i) && (j1 != j)){
! 4640: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
! 4641: It might be a problem of design; if ncovcol and the model are correct\n \
! 4642: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
! 4643: exit(1);
! 4644: }
! 4645: fprintf(ficparo,"%1d%1d",i1,j1);
! 4646: if(mle==1)
! 4647: printf("%1d%1d",i,j);
! 4648: fprintf(ficlog,"%1d%1d",i,j);
! 4649: for(k=1; k<=ncovmodel;k++){
! 4650: fscanf(ficpar," %lf",¶m[i][j][k]);
! 4651: if(mle==1){
! 4652: printf(" %lf",param[i][j][k]);
! 4653: fprintf(ficlog," %lf",param[i][j][k]);
! 4654: }
! 4655: else
! 4656: fprintf(ficlog," %lf",param[i][j][k]);
! 4657: fprintf(ficparo," %lf",param[i][j][k]);
! 4658: }
! 4659: fscanf(ficpar,"\n");
! 4660: numlinepar++;
! 4661: if(mle==1)
! 4662: printf("\n");
! 4663: fprintf(ficlog,"\n");
! 4664: fprintf(ficparo,"\n");
! 4665: }
! 4666: }
! 4667: fflush(ficlog);
! 4668:
! 4669: p=param[1][1];
! 4670:
! 4671: /* Reads comments: lines beginning with '#' */
! 4672: while((c=getc(ficpar))=='#' && c!= EOF){
! 4673: ungetc(c,ficpar);
! 4674: fgets(line, MAXLINE, ficpar);
! 4675: numlinepar++;
! 4676: puts(line);
! 4677: fputs(line,ficparo);
! 4678: fputs(line,ficlog);
! 4679: }
! 4680: ungetc(c,ficpar);
! 4681:
! 4682: for(i=1; i <=nlstate; i++){
! 4683: for(j=1; j <=nlstate+ndeath-1; j++){
! 4684: fscanf(ficpar,"%1d%1d",&i1,&j1);
! 4685: if ((i1-i)*(j1-j)!=0){
! 4686: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
! 4687: exit(1);
! 4688: }
! 4689: printf("%1d%1d",i,j);
! 4690: fprintf(ficparo,"%1d%1d",i1,j1);
! 4691: fprintf(ficlog,"%1d%1d",i1,j1);
! 4692: for(k=1; k<=ncovmodel;k++){
! 4693: fscanf(ficpar,"%le",&delti3[i][j][k]);
! 4694: printf(" %le",delti3[i][j][k]);
! 4695: fprintf(ficparo," %le",delti3[i][j][k]);
! 4696: fprintf(ficlog," %le",delti3[i][j][k]);
! 4697: }
! 4698: fscanf(ficpar,"\n");
! 4699: numlinepar++;
! 4700: printf("\n");
! 4701: fprintf(ficparo,"\n");
! 4702: fprintf(ficlog,"\n");
! 4703: }
! 4704: }
! 4705: fflush(ficlog);
! 4706:
! 4707: delti=delti3[1][1];
! 4708:
! 4709:
! 4710: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
! 4711:
! 4712: /* Reads comments: lines beginning with '#' */
! 4713: while((c=getc(ficpar))=='#' && c!= EOF){
! 4714: ungetc(c,ficpar);
! 4715: fgets(line, MAXLINE, ficpar);
! 4716: numlinepar++;
! 4717: puts(line);
! 4718: fputs(line,ficparo);
! 4719: fputs(line,ficlog);
! 4720: }
! 4721: ungetc(c,ficpar);
! 4722:
! 4723: matcov=matrix(1,npar,1,npar);
! 4724: for(i=1; i <=npar; i++){
! 4725: fscanf(ficpar,"%s",&str);
! 4726: if(mle==1)
! 4727: printf("%s",str);
! 4728: fprintf(ficlog,"%s",str);
! 4729: fprintf(ficparo,"%s",str);
! 4730: for(j=1; j <=i; j++){
! 4731: fscanf(ficpar," %le",&matcov[i][j]);
! 4732: if(mle==1){
! 4733: printf(" %.5le",matcov[i][j]);
! 4734: }
! 4735: fprintf(ficlog," %.5le",matcov[i][j]);
! 4736: fprintf(ficparo," %.5le",matcov[i][j]);
! 4737: }
! 4738: fscanf(ficpar,"\n");
! 4739: numlinepar++;
! 4740: if(mle==1)
! 4741: printf("\n");
! 4742: fprintf(ficlog,"\n");
! 4743: fprintf(ficparo,"\n");
! 4744: }
! 4745: for(i=1; i <=npar; i++)
! 4746: for(j=i+1;j<=npar;j++)
! 4747: matcov[i][j]=matcov[j][i];
! 4748:
! 4749: if(mle==1)
! 4750: printf("\n");
! 4751: fprintf(ficlog,"\n");
! 4752:
! 4753: fflush(ficlog);
! 4754:
! 4755: /*-------- Rewriting parameter file ----------*/
! 4756: strcpy(rfileres,"r"); /* "Rparameterfile */
! 4757: strcat(rfileres,optionfilefiname); /* Parameter file first name*/
! 4758: strcat(rfileres,"."); /* */
! 4759: strcat(rfileres,optionfilext); /* Other files have txt extension */
! 4760: if((ficres =fopen(rfileres,"w"))==NULL) {
! 4761: printf("Problem writing new parameter file: %s\n", fileres);goto end;
! 4762: fprintf(ficlog,"Problem writing new parameter file: %s\n", fileres);goto end;
! 4763: }
! 4764: fprintf(ficres,"#%s\n",version);
! 4765: } /* End of mle != -3 */
! 4766:
! 4767: /*-------- data file ----------*/
! 4768: if((fic=fopen(datafile,"r"))==NULL) {
! 4769: printf("Problem while opening datafile: %s\n", datafile);goto end;
! 4770: fprintf(ficlog,"Problem while opening datafile: %s\n", datafile);goto end;
! 4771: }
! 4772:
! 4773: n= lastobs;
! 4774: severity = vector(1,maxwav);
! 4775: outcome=imatrix(1,maxwav+1,1,n);
! 4776: num=lvector(1,n);
! 4777: moisnais=vector(1,n);
! 4778: annais=vector(1,n);
! 4779: moisdc=vector(1,n);
! 4780: andc=vector(1,n);
! 4781: agedc=vector(1,n);
! 4782: cod=ivector(1,n);
! 4783: weight=vector(1,n);
! 4784: for(i=1;i<=n;i++) weight[i]=1.0; /* Equal weights, 1 by default */
! 4785: mint=matrix(1,maxwav,1,n);
! 4786: anint=matrix(1,maxwav,1,n);
! 4787: s=imatrix(1,maxwav+1,1,n);
! 4788: tab=ivector(1,NCOVMAX);
! 4789: ncodemax=ivector(1,8);
! 4790:
! 4791: i=1;
! 4792: linei=0;
! 4793: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
! 4794: linei=linei+1;
! 4795: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
! 4796: if(line[j] == '\t')
! 4797: line[j] = ' ';
! 4798: }
! 4799: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
! 4800: ;
! 4801: };
! 4802: line[j+1]=0; /* Trims blanks at end of line */
! 4803: if(line[0]=='#'){
! 4804: fprintf(ficlog,"Comment line\n%s\n",line);
! 4805: printf("Comment line\n%s\n",line);
! 4806: continue;
! 4807: }
! 4808:
! 4809: for (j=maxwav;j>=1;j--){
! 4810: cutv(stra, strb,line,' ');
! 4811: errno=0;
! 4812: lval=strtol(strb,&endptr,10);
! 4813: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
! 4814: if( strb[0]=='\0' || (*endptr != '\0')){
! 4815: printf("Error reading data around '%d' at line number %d %s for individual %d, '%s'\nShould be a status of wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,j,maxwav);
! 4816: exit(1);
! 4817: }
! 4818: s[j][i]=lval;
! 4819:
! 4820: strcpy(line,stra);
! 4821: cutv(stra, strb,line,' ');
! 4822: if(iout=sscanf(strb,"%d/%d",&month, &year) != 0){
! 4823: }
! 4824: else if(iout=sscanf(strb,"%s.") != 0){
! 4825: month=99;
! 4826: year=9999;
! 4827: }else{
! 4828: printf("Error reading data around '%s' at line number %ld %s for individual %d, '%s'\nShould be a date of interview (mm/yyyy or .) at wave %d. Exiting.\n",strb, linei,i, line,j);
! 4829: exit(1);
! 4830: }
! 4831: anint[j][i]= (double) year;
! 4832: mint[j][i]= (double)month;
! 4833: strcpy(line,stra);
! 4834: } /* ENd Waves */
! 4835:
! 4836: cutv(stra, strb,line,' ');
! 4837: if(iout=sscanf(strb,"%d/%d",&month, &year) != 0){
! 4838: }
! 4839: else if(iout=sscanf(strb,"%s.",dummy) != 0){
! 4840: month=99;
! 4841: year=9999;
! 4842: }else{
! 4843: printf("Error reading data around '%s' at line number %ld %s for individual %d, '%s'\nShould be a date of death (mm/yyyy or .). Exiting.\n",strb, linei,i,line);
! 4844: exit(1);
! 4845: }
! 4846: andc[i]=(double) year;
! 4847: moisdc[i]=(double) month;
! 4848: strcpy(line,stra);
! 4849:
! 4850: cutv(stra, strb,line,' ');
! 4851: if(iout=sscanf(strb,"%d/%d",&month, &year) != 0){
! 4852: }
! 4853: else if(iout=sscanf(strb,"%s.") != 0){
! 4854: month=99;
! 4855: year=9999;
! 4856: }else{
! 4857: printf("Error reading data around '%s' at line number %ld %s for individual %d, '%s'\nShould be a date of birth (mm/yyyy or .). Exiting.\n",strb, linei,i,line,j);
! 4858: exit(1);
! 4859: }
! 4860: annais[i]=(double)(year);
! 4861: moisnais[i]=(double)(month);
! 4862: strcpy(line,stra);
! 4863:
! 4864: cutv(stra, strb,line,' ');
! 4865: errno=0;
! 4866: dval=strtod(strb,&endptr);
! 4867: if( strb[0]=='\0' || (*endptr != '\0')){
! 4868: printf("Error reading data around '%f' at line number %ld, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
! 4869: exit(1);
! 4870: }
! 4871: weight[i]=dval;
! 4872: strcpy(line,stra);
! 4873:
! 4874: for (j=ncovcol;j>=1;j--){
! 4875: cutv(stra, strb,line,' ');
! 4876: errno=0;
! 4877: lval=strtol(strb,&endptr,10);
! 4878: if( strb[0]=='\0' || (*endptr != '\0')){
! 4879: printf("Error reading data around '%d' at line number %ld %s for individual %d, '%s'\nShould be a covar (meaning 0 for the reference or 1). Exiting.\n",lval, linei,i, line);
! 4880: exit(1);
! 4881: }
! 4882: if(lval <-1 || lval >1){
! 4883: printf("Error reading data around '%d' at line number %ld for individual %d, '%s'\n \
! 4884: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
! 4885: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
! 4886: For example, for multinomial values like 1, 2 and 3,\n \
! 4887: build V1=0 V2=0 for the reference value (1),\n \
! 4888: V1=1 V2=0 for (2) \n \
! 4889: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
! 4890: output of IMaCh is often meaningless.\n \
! 4891: Exiting.\n",lval,linei, i,line,j);
! 4892: exit(1);
! 4893: }
! 4894: covar[j][i]=(double)(lval);
! 4895: strcpy(line,stra);
! 4896: }
! 4897: lstra=strlen(stra);
! 4898:
! 4899: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
! 4900: stratrunc = &(stra[lstra-9]);
! 4901: num[i]=atol(stratrunc);
! 4902: }
! 4903: else
! 4904: num[i]=atol(stra);
! 4905: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
! 4906: printf("%ld %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]),weight[i], (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]), (mint[2][i]), (anint[2][i]), (s[2][i]), (mint[3][i]), (anint[3][i]), (s[3][i]), (mint[4][i]), (anint[4][i]), (s[4][i])); ij=ij+1;}*/
! 4907:
! 4908: i=i+1;
! 4909: } /* End loop reading data */
! 4910: fclose(fic);
! 4911: /* printf("ii=%d", ij);
! 4912: scanf("%d",i);*/
! 4913: imx=i-1; /* Number of individuals */
! 4914:
! 4915: /* for (i=1; i<=imx; i++){
! 4916: if ((s[1][i]==3) && (s[2][i]==2)) s[2][i]=3;
! 4917: if ((s[2][i]==3) && (s[3][i]==2)) s[3][i]=3;
! 4918: if ((s[3][i]==3) && (s[4][i]==2)) s[4][i]=3;
! 4919: }*/
! 4920: /* for (i=1; i<=imx; i++){
! 4921: if (s[4][i]==9) s[4][i]=-1;
! 4922: printf("%ld %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]), (weight[i]), (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]), (mint[2][i]), (anint[2][i]), (s[2][i]), (mint[3][i]), (anint[3][i]), (s[3][i]), (mint[4][i]), (anint[4][i]), (s[4][i]));}*/
! 4923:
! 4924: /* for (i=1; i<=imx; i++) */
! 4925:
! 4926: /*if ((s[3][i]==3) || (s[4][i]==3)) weight[i]=0.08;
! 4927: else weight[i]=1;*/
! 4928:
! 4929: /* Calculation of the number of parameters from char model */
! 4930: Tvar=ivector(1,15); /* stores the number n of the covariates in Vm+Vn at 1 and m at 2 */
! 4931: Tprod=ivector(1,15);
! 4932: Tvaraff=ivector(1,15);
! 4933: Tvard=imatrix(1,15,1,2);
! 4934: Tage=ivector(1,15);
! 4935:
! 4936: if (strlen(model) >1){ /* If there is at least 1 covariate */
! 4937: j=0, j1=0, k1=1, k2=1;
! 4938: j=nbocc(model,'+'); /* j=Number of '+' */
! 4939: j1=nbocc(model,'*'); /* j1=Number of '*' */
! 4940: cptcovn=j+1;
! 4941: cptcovprod=j1; /*Number of products */
! 4942:
! 4943: strcpy(modelsav,model);
! 4944: if ((strcmp(model,"age")==0) || (strcmp(model,"age*age")==0)){
! 4945: printf("Error. Non available option model=%s ",model);
! 4946: fprintf(ficlog,"Error. Non available option model=%s ",model);
! 4947: goto end;
! 4948: }
! 4949:
! 4950: /* This loop fills the array Tvar from the string 'model'.*/
! 4951:
! 4952: for(i=(j+1); i>=1;i--){
! 4953: cutv(stra,strb,modelsav,'+'); /* keeps in strb after the last + */
! 4954: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
! 4955: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
! 4956: /*scanf("%d",i);*/
! 4957: if (strchr(strb,'*')) { /* Model includes a product */
! 4958: cutv(strd,strc,strb,'*'); /* strd*strc Vm*Vn (if not *age)*/
! 4959: if (strcmp(strc,"age")==0) { /* Vn*age */
! 4960: cptcovprod--;
! 4961: cutv(strb,stre,strd,'V');
! 4962: Tvar[i]=atoi(stre); /* computes n in Vn and stores in Tvar*/
! 4963: cptcovage++;
! 4964: Tage[cptcovage]=i;
! 4965: /*printf("stre=%s ", stre);*/
! 4966: }
! 4967: else if (strcmp(strd,"age")==0) { /* or age*Vn */
! 4968: cptcovprod--;
! 4969: cutv(strb,stre,strc,'V');
! 4970: Tvar[i]=atoi(stre);
! 4971: cptcovage++;
! 4972: Tage[cptcovage]=i;
! 4973: }
! 4974: else { /* Age is not in the model */
! 4975: cutv(strb,stre,strc,'V'); /* strc= Vn, stre is n*/
! 4976: Tvar[i]=ncovcol+k1;
! 4977: cutv(strb,strc,strd,'V'); /* strd was Vm, strc is m */
! 4978: Tprod[k1]=i;
! 4979: Tvard[k1][1]=atoi(strc); /* m*/
! 4980: Tvard[k1][2]=atoi(stre); /* n */
! 4981: Tvar[cptcovn+k2]=Tvard[k1][1];
! 4982: Tvar[cptcovn+k2+1]=Tvard[k1][2];
! 4983: for (k=1; k<=lastobs;k++)
! 4984: covar[ncovcol+k1][k]=covar[atoi(stre)][k]*covar[atoi(strc)][k];
! 4985: k1++;
! 4986: k2=k2+2;
! 4987: }
! 4988: }
! 4989: else { /* no more sum */
! 4990: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
! 4991: /* scanf("%d",i);*/
! 4992: cutv(strd,strc,strb,'V');
! 4993: Tvar[i]=atoi(strc);
! 4994: }
! 4995: strcpy(modelsav,stra);
! 4996: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
! 4997: scanf("%d",i);*/
! 4998: } /* end of loop + */
! 4999: } /* end model */
! 5000:
! 5001: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
! 5002: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
! 5003:
! 5004: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
! 5005: printf("cptcovprod=%d ", cptcovprod);
! 5006: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
! 5007:
! 5008: scanf("%d ",i);*/
! 5009:
! 5010: /* if(mle==1){*/
! 5011: if (weightopt != 1) { /* Maximisation without weights*/
! 5012: for(i=1;i<=n;i++) weight[i]=1.0;
! 5013: }
! 5014: /*-calculation of age at interview from date of interview and age at death -*/
! 5015: agev=matrix(1,maxwav,1,imx);
! 5016:
! 5017: for (i=1; i<=imx; i++) {
! 5018: for(m=2; (m<= maxwav); m++) {
! 5019: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
! 5020: anint[m][i]=9999;
! 5021: s[m][i]=-1;
! 5022: }
! 5023: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
! 5024: nberr++;
! 5025: printf("Error! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results are biased\n",(int)moisdc[i],(int)andc[i],num[i],i);
! 5026: fprintf(ficlog,"Error! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results are biased\n",(int)moisdc[i],(int)andc[i],num[i],i);
! 5027: s[m][i]=-1;
! 5028: }
! 5029: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
! 5030: nberr++;
! 5031: printf("Error! Month of death of individual %ld on line %d was unknown %2d, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,(int)moisdc[i]);
! 5032: fprintf(ficlog,"Error! Month of death of individual %ld on line %d was unknown %f, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,moisdc[i]);
! 5033: s[m][i]=-1; /* We prefer to skip it (and to skip it in version 0.8a1 too */
! 5034: }
! 5035: }
! 5036: }
! 5037:
! 5038: for (i=1; i<=imx; i++) {
! 5039: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
! 5040: for(m=firstpass; (m<= lastpass); m++){
! 5041: if(s[m][i] >0 || s[m][i]==-2 || s[m][i]==-4 || s[m][i]==-5){
! 5042: if (s[m][i] >= nlstate+1) {
! 5043: if(agedc[i]>0)
! 5044: if((int)moisdc[i]!=99 && (int)andc[i]!=9999)
! 5045: agev[m][i]=agedc[i];
! 5046: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
! 5047: else {
! 5048: if ((int)andc[i]!=9999){
! 5049: nbwarn++;
! 5050: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
! 5051: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
! 5052: agev[m][i]=-1;
! 5053: }
! 5054: }
! 5055: }
! 5056: else if(s[m][i] !=9){ /* Standard case, age in fractional
! 5057: years but with the precision of a month */
! 5058: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
! 5059: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
! 5060: agev[m][i]=1;
! 5061: else if(agev[m][i] <agemin){
! 5062: agemin=agev[m][i];
! 5063: /*printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], agemin);*/
! 5064: }
! 5065: else if(agev[m][i] >agemax){
! 5066: agemax=agev[m][i];
! 5067: /* printf(" anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.0f\n",m,i,anint[m][i], i,annais[i], agemax);*/
! 5068: }
! 5069: /*agev[m][i]=anint[m][i]-annais[i];*/
! 5070: /* agev[m][i] = age[i]+2*m;*/
! 5071: }
! 5072: else { /* =9 */
! 5073: agev[m][i]=1;
! 5074: s[m][i]=-1;
! 5075: }
! 5076: }
! 5077: else /*= 0 Unknown */
! 5078: agev[m][i]=1;
! 5079: }
! 5080:
! 5081: }
! 5082: for (i=1; i<=imx; i++) {
! 5083: for(m=firstpass; (m<=lastpass); m++){
! 5084: if (s[m][i] > (nlstate+ndeath)) {
! 5085: nberr++;
! 5086: printf("Error: on wave %d of individual %d status %d > (nlstate+ndeath)=(%d+%d)=%d\n",m,i,s[m][i],nlstate, ndeath, nlstate+ndeath);
! 5087: fprintf(ficlog,"Error: on wave %d of individual %d status %d > (nlstate+ndeath)=(%d+%d)=%d\n",m,i,s[m][i],nlstate, ndeath, nlstate+ndeath);
! 5088: goto end;
! 5089: }
! 5090: }
! 5091: }
! 5092:
! 5093: /*for (i=1; i<=imx; i++){
! 5094: for (m=firstpass; (m<lastpass); m++){
! 5095: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
! 5096: }
! 5097:
! 5098: }*/
! 5099:
! 5100:
! 5101: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, agemin, agemax);
! 5102: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, agemin, agemax);
! 5103:
! 5104: agegomp=(int)agemin;
! 5105: free_vector(severity,1,maxwav);
! 5106: free_imatrix(outcome,1,maxwav+1,1,n);
! 5107: free_vector(moisnais,1,n);
! 5108: free_vector(annais,1,n);
! 5109: /* free_matrix(mint,1,maxwav,1,n);
! 5110: free_matrix(anint,1,maxwav,1,n);*/
! 5111: free_vector(moisdc,1,n);
! 5112: free_vector(andc,1,n);
! 5113:
! 5114:
! 5115: wav=ivector(1,imx);
! 5116: dh=imatrix(1,lastpass-firstpass+1,1,imx);
! 5117: bh=imatrix(1,lastpass-firstpass+1,1,imx);
! 5118: mw=imatrix(1,lastpass-firstpass+1,1,imx);
! 5119:
! 5120: /* Concatenates waves */
! 5121: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
! 5122:
! 5123: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
! 5124:
! 5125: Tcode=ivector(1,100);
! 5126: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
! 5127: ncodemax[1]=1;
! 5128: if (cptcovn > 0) tricode(Tvar,nbcode,imx);
! 5129:
! 5130: codtab=imatrix(1,100,1,10); /* Cross tabulation to get the order of
! 5131: the estimations*/
! 5132: h=0;
! 5133: m=pow(2,cptcoveff);
! 5134:
! 5135: for(k=1;k<=cptcoveff; k++){
! 5136: for(i=1; i <=(m/pow(2,k));i++){
! 5137: for(j=1; j <= ncodemax[k]; j++){
! 5138: for(cpt=1; cpt <=(m/pow(2,cptcoveff+1-k)); cpt++){
! 5139: h++;
! 5140: if (h>m) h=1;codtab[h][k]=j;codtab[h][Tvar[k]]=j;
! 5141: /* printf("h=%d k=%d j=%d codtab[h][k]=%d tvar[k]=%d \n",h, k,j,codtab[h][k],Tvar[k]);*/
! 5142: }
! 5143: }
! 5144: }
! 5145: }
! 5146: /* printf("codtab[1][2]=%d codtab[2][2]=%d",codtab[1][2],codtab[2][2]);
! 5147: codtab[1][2]=1;codtab[2][2]=2; */
! 5148: /* for(i=1; i <=m ;i++){
! 5149: for(k=1; k <=cptcovn; k++){
! 5150: printf("i=%d k=%d %d %d ",i,k,codtab[i][k], cptcoveff);
! 5151: }
! 5152: printf("\n");
! 5153: }
! 5154: scanf("%d",i);*/
! 5155:
! 5156: /*------------ gnuplot -------------*/
! 5157: strcpy(optionfilegnuplot,optionfilefiname);
! 5158: if(mle==-3)
! 5159: strcat(optionfilegnuplot,"-mort");
! 5160: strcat(optionfilegnuplot,".gp");
! 5161:
! 5162: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
! 5163: printf("Problem with file %s",optionfilegnuplot);
! 5164: }
! 5165: else{
! 5166: fprintf(ficgp,"\n# %s\n", version);
! 5167: fprintf(ficgp,"# %s\n", optionfilegnuplot);
! 5168: fprintf(ficgp,"set missing 'NaNq'\n");
! 5169: }
! 5170: /* fclose(ficgp);*/
! 5171: /*--------- index.htm --------*/
! 5172:
! 5173: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
! 5174: if(mle==-3)
! 5175: strcat(optionfilehtm,"-mort");
! 5176: strcat(optionfilehtm,".htm");
! 5177: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
! 5178: printf("Problem with %s \n",optionfilehtm), exit(0);
! 5179: }
! 5180:
! 5181: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
! 5182: strcat(optionfilehtmcov,"-cov.htm");
! 5183: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
! 5184: printf("Problem with %s \n",optionfilehtmcov), exit(0);
! 5185: }
! 5186: else{
! 5187: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
! 5188: <hr size=\"2\" color=\"#EC5E5E\"> \n\
! 5189: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>\n",\
! 5190: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
! 5191: }
! 5192:
! 5193: fprintf(fichtm,"<html><head>\n<title>IMaCh %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
! 5194: <hr size=\"2\" color=\"#EC5E5E\"> \n\
! 5195: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>\n\
! 5196: \n\
! 5197: <hr size=\"2\" color=\"#EC5E5E\">\
! 5198: <ul><li><h4>Parameter files</h4>\n\
! 5199: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
! 5200: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
! 5201: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
! 5202: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
! 5203: - Date and time at start: %s</ul>\n",\
! 5204: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
! 5205: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
! 5206: fileres,fileres,\
! 5207: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
! 5208: fflush(fichtm);
! 5209:
! 5210: strcpy(pathr,path);
! 5211: strcat(pathr,optionfilefiname);
! 5212: chdir(optionfilefiname); /* Move to directory named optionfile */
! 5213:
! 5214: /* Calculates basic frequencies. Computes observed prevalence at single age
! 5215: and prints on file fileres'p'. */
! 5216: freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart);
! 5217:
! 5218: fprintf(fichtm,"\n");
! 5219: fprintf(fichtm,"<br>Total number of observations=%d <br>\n\
! 5220: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
! 5221: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
! 5222: imx,agemin,agemax,jmin,jmax,jmean);
! 5223: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
! 5224: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
! 5225: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
! 5226: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
! 5227: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
! 5228:
! 5229:
! 5230: /* For Powell, parameters are in a vector p[] starting at p[1]
! 5231: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
! 5232: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
! 5233:
! 5234: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
! 5235:
! 5236: if (mle==-3){
! 5237: ximort=matrix(1,NDIM,1,NDIM);
! 5238: cens=ivector(1,n);
! 5239: ageexmed=vector(1,n);
! 5240: agecens=vector(1,n);
! 5241: dcwave=ivector(1,n);
! 5242:
! 5243: for (i=1; i<=imx; i++){
! 5244: dcwave[i]=-1;
! 5245: for (m=firstpass; m<=lastpass; m++)
! 5246: if (s[m][i]>nlstate) {
! 5247: dcwave[i]=m;
! 5248: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
! 5249: break;
! 5250: }
! 5251: }
! 5252:
! 5253: for (i=1; i<=imx; i++) {
! 5254: if (wav[i]>0){
! 5255: ageexmed[i]=agev[mw[1][i]][i];
! 5256: j=wav[i];
! 5257: agecens[i]=1.;
! 5258:
! 5259: if (ageexmed[i]> 1 && wav[i] > 0){
! 5260: agecens[i]=agev[mw[j][i]][i];
! 5261: cens[i]= 1;
! 5262: }else if (ageexmed[i]< 1)
! 5263: cens[i]= -1;
! 5264: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
! 5265: cens[i]=0 ;
! 5266: }
! 5267: else cens[i]=-1;
! 5268: }
! 5269:
! 5270: for (i=1;i<=NDIM;i++) {
! 5271: for (j=1;j<=NDIM;j++)
! 5272: ximort[i][j]=(i == j ? 1.0 : 0.0);
! 5273: }
! 5274:
! 5275: p[1]=0.0268; p[NDIM]=0.083;
! 5276: /*printf("%lf %lf", p[1], p[2]);*/
! 5277:
! 5278:
! 5279: printf("Powell\n"); fprintf(ficlog,"Powell\n");
! 5280: strcpy(filerespow,"pow-mort");
! 5281: strcat(filerespow,fileres);
! 5282: if((ficrespow=fopen(filerespow,"w"))==NULL) {
! 5283: printf("Problem with resultfile: %s\n", filerespow);
! 5284: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
! 5285: }
! 5286: fprintf(ficrespow,"# Powell\n# iter -2*LL");
! 5287: /* for (i=1;i<=nlstate;i++)
! 5288: for(j=1;j<=nlstate+ndeath;j++)
! 5289: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
! 5290: */
! 5291: fprintf(ficrespow,"\n");
! 5292:
! 5293: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
! 5294: fclose(ficrespow);
! 5295:
! 5296: hesscov(matcov, p, NDIM, delti, 1e-4, gompertz);
! 5297:
! 5298: for(i=1; i <=NDIM; i++)
! 5299: for(j=i+1;j<=NDIM;j++)
! 5300: matcov[i][j]=matcov[j][i];
! 5301:
! 5302: printf("\nCovariance matrix\n ");
! 5303: for(i=1; i <=NDIM; i++) {
! 5304: for(j=1;j<=NDIM;j++){
! 5305: printf("%f ",matcov[i][j]);
! 5306: }
! 5307: printf("\n ");
! 5308: }
! 5309:
! 5310: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
! 5311: for (i=1;i<=NDIM;i++)
! 5312: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
! 5313:
! 5314: lsurv=vector(1,AGESUP);
! 5315: lpop=vector(1,AGESUP);
! 5316: tpop=vector(1,AGESUP);
! 5317: lsurv[agegomp]=100000;
! 5318:
! 5319: for (k=agegomp;k<=AGESUP;k++) {
! 5320: agemortsup=k;
! 5321: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
! 5322: }
! 5323:
! 5324: for (k=agegomp;k<agemortsup;k++)
! 5325: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
! 5326:
! 5327: for (k=agegomp;k<agemortsup;k++){
! 5328: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
! 5329: sumlpop=sumlpop+lpop[k];
! 5330: }
! 5331:
! 5332: tpop[agegomp]=sumlpop;
! 5333: for (k=agegomp;k<(agemortsup-3);k++){
! 5334: /* tpop[k+1]=2;*/
! 5335: tpop[k+1]=tpop[k]-lpop[k];
! 5336: }
! 5337:
! 5338:
! 5339: printf("\nAge lx qx dx Lx Tx e(x)\n");
! 5340: for (k=agegomp;k<(agemortsup-2);k++)
! 5341: printf("%d %.0lf %lf %.0lf %.0lf %.0lf %lf\n",k,lsurv[k],p[1]*exp(p[2]*(k-agegomp)),(p[1]*exp(p[2]*(k-agegomp)))*lsurv[k],lpop[k],tpop[k],tpop[k]/lsurv[k]);
! 5342:
! 5343:
! 5344: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
! 5345: printinggnuplotmort(fileres, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
! 5346:
! 5347: printinghtmlmort(fileres,title,datafile, firstpass, lastpass, \
! 5348: stepm, weightopt,\
! 5349: model,imx,p,matcov,agemortsup);
! 5350:
! 5351: free_vector(lsurv,1,AGESUP);
! 5352: free_vector(lpop,1,AGESUP);
! 5353: free_vector(tpop,1,AGESUP);
! 5354: } /* Endof if mle==-3 */
! 5355:
! 5356: else{ /* For mle >=1 */
! 5357:
! 5358: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
! 5359: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
! 5360: for (k=1; k<=npar;k++)
! 5361: printf(" %d %8.5f",k,p[k]);
! 5362: printf("\n");
! 5363: globpr=1; /* to print the contributions */
! 5364: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
! 5365: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
! 5366: for (k=1; k<=npar;k++)
! 5367: printf(" %d %8.5f",k,p[k]);
! 5368: printf("\n");
! 5369: if(mle>=1){ /* Could be 1 or 2 */
! 5370: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
! 5371: }
! 5372:
! 5373: /*--------- results files --------------*/
! 5374: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle= 0 weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate, ndeath, maxwav, weightopt,model);
! 5375:
! 5376:
! 5377: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
! 5378: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
! 5379: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
! 5380: for(i=1,jk=1; i <=nlstate; i++){
! 5381: for(k=1; k <=(nlstate+ndeath); k++){
! 5382: if (k != i) {
! 5383: printf("%d%d ",i,k);
! 5384: fprintf(ficlog,"%d%d ",i,k);
! 5385: fprintf(ficres,"%1d%1d ",i,k);
! 5386: for(j=1; j <=ncovmodel; j++){
! 5387: printf("%lf ",p[jk]);
! 5388: fprintf(ficlog,"%lf ",p[jk]);
! 5389: fprintf(ficres,"%lf ",p[jk]);
! 5390: jk++;
! 5391: }
! 5392: printf("\n");
! 5393: fprintf(ficlog,"\n");
! 5394: fprintf(ficres,"\n");
! 5395: }
! 5396: }
! 5397: }
! 5398: if(mle!=0){
! 5399: /* Computing hessian and covariance matrix */
! 5400: ftolhess=ftol; /* Usually correct */
! 5401: hesscov(matcov, p, npar, delti, ftolhess, func);
! 5402: }
! 5403: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
! 5404: printf("# Scales (for hessian or gradient estimation)\n");
! 5405: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
! 5406: for(i=1,jk=1; i <=nlstate; i++){
! 5407: for(j=1; j <=nlstate+ndeath; j++){
! 5408: if (j!=i) {
! 5409: fprintf(ficres,"%1d%1d",i,j);
! 5410: printf("%1d%1d",i,j);
! 5411: fprintf(ficlog,"%1d%1d",i,j);
! 5412: for(k=1; k<=ncovmodel;k++){
! 5413: printf(" %.5e",delti[jk]);
! 5414: fprintf(ficlog," %.5e",delti[jk]);
! 5415: fprintf(ficres," %.5e",delti[jk]);
! 5416: jk++;
! 5417: }
! 5418: printf("\n");
! 5419: fprintf(ficlog,"\n");
! 5420: fprintf(ficres,"\n");
! 5421: }
! 5422: }
! 5423: }
! 5424:
! 5425: fprintf(ficres,"# Covariance matrix \n# 121 Var(a12)\n# 122 Cov(b12,a12) Var(b12)\n# ...\n# 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n");
! 5426: if(mle>=1)
! 5427: printf("# Covariance matrix \n# 121 Var(a12)\n# 122 Cov(b12,a12) Var(b12)\n# ...\n# 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n");
! 5428: fprintf(ficlog,"# Covariance matrix \n# 121 Var(a12)\n# 122 Cov(b12,a12) Var(b12)\n# ...\n# 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n");
! 5429: /* # 121 Var(a12)\n\ */
! 5430: /* # 122 Cov(b12,a12) Var(b12)\n\ */
! 5431: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
! 5432: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
! 5433: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
! 5434: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
! 5435: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
! 5436: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
! 5437:
! 5438:
! 5439: /* Just to have a covariance matrix which will be more understandable
! 5440: even is we still don't want to manage dictionary of variables
! 5441: */
! 5442: for(itimes=1;itimes<=2;itimes++){
! 5443: jj=0;
! 5444: for(i=1; i <=nlstate; i++){
! 5445: for(j=1; j <=nlstate+ndeath; j++){
! 5446: if(j==i) continue;
! 5447: for(k=1; k<=ncovmodel;k++){
! 5448: jj++;
! 5449: ca[0]= k+'a'-1;ca[1]='\0';
! 5450: if(itimes==1){
! 5451: if(mle>=1)
! 5452: printf("#%1d%1d%d",i,j,k);
! 5453: fprintf(ficlog,"#%1d%1d%d",i,j,k);
! 5454: fprintf(ficres,"#%1d%1d%d",i,j,k);
! 5455: }else{
! 5456: if(mle>=1)
! 5457: printf("%1d%1d%d",i,j,k);
! 5458: fprintf(ficlog,"%1d%1d%d",i,j,k);
! 5459: fprintf(ficres,"%1d%1d%d",i,j,k);
! 5460: }
! 5461: ll=0;
! 5462: for(li=1;li <=nlstate; li++){
! 5463: for(lj=1;lj <=nlstate+ndeath; lj++){
! 5464: if(lj==li) continue;
! 5465: for(lk=1;lk<=ncovmodel;lk++){
! 5466: ll++;
! 5467: if(ll<=jj){
! 5468: cb[0]= lk +'a'-1;cb[1]='\0';
! 5469: if(ll<jj){
! 5470: if(itimes==1){
! 5471: if(mle>=1)
! 5472: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
! 5473: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
! 5474: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
! 5475: }else{
! 5476: if(mle>=1)
! 5477: printf(" %.5e",matcov[jj][ll]);
! 5478: fprintf(ficlog," %.5e",matcov[jj][ll]);
! 5479: fprintf(ficres," %.5e",matcov[jj][ll]);
! 5480: }
! 5481: }else{
! 5482: if(itimes==1){
! 5483: if(mle>=1)
! 5484: printf(" Var(%s%1d%1d)",ca,i,j);
! 5485: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
! 5486: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
! 5487: }else{
! 5488: if(mle>=1)
! 5489: printf(" %.5e",matcov[jj][ll]);
! 5490: fprintf(ficlog," %.5e",matcov[jj][ll]);
! 5491: fprintf(ficres," %.5e",matcov[jj][ll]);
! 5492: }
! 5493: }
! 5494: }
! 5495: } /* end lk */
! 5496: } /* end lj */
! 5497: } /* end li */
! 5498: if(mle>=1)
! 5499: printf("\n");
! 5500: fprintf(ficlog,"\n");
! 5501: fprintf(ficres,"\n");
! 5502: numlinepar++;
! 5503: } /* end k*/
! 5504: } /*end j */
! 5505: } /* end i */
! 5506: } /* end itimes */
! 5507:
! 5508: fflush(ficlog);
! 5509: fflush(ficres);
! 5510:
! 5511: while((c=getc(ficpar))=='#' && c!= EOF){
! 5512: ungetc(c,ficpar);
! 5513: fgets(line, MAXLINE, ficpar);
! 5514: puts(line);
! 5515: fputs(line,ficparo);
! 5516: }
! 5517: ungetc(c,ficpar);
! 5518:
! 5519: estepm=0;
! 5520: fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm);
! 5521: if (estepm==0 || estepm < stepm) estepm=stepm;
! 5522: if (fage <= 2) {
! 5523: bage = ageminpar;
! 5524: fage = agemaxpar;
! 5525: }
! 5526:
! 5527: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
! 5528: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d\n",ageminpar,agemaxpar,bage,fage, estepm);
! 5529: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d\n",ageminpar,agemaxpar,bage,fage, estepm);
! 5530:
! 5531: while((c=getc(ficpar))=='#' && c!= EOF){
! 5532: ungetc(c,ficpar);
! 5533: fgets(line, MAXLINE, ficpar);
! 5534: puts(line);
! 5535: fputs(line,ficparo);
! 5536: }
! 5537: ungetc(c,ficpar);
! 5538:
! 5539: fscanf(ficpar,"begin-prev-date=%lf/%lf/%lf end-prev-date=%lf/%lf/%lf mov_average=%d\n",&jprev1, &mprev1,&anprev1,&jprev2, &mprev2,&anprev2,&mobilav);
! 5540: fprintf(ficparo,"begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
! 5541: fprintf(ficres,"begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
! 5542: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
! 5543: fprintf(ficlog,"begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
! 5544:
! 5545: while((c=getc(ficpar))=='#' && c!= EOF){
! 5546: ungetc(c,ficpar);
! 5547: fgets(line, MAXLINE, ficpar);
! 5548: puts(line);
! 5549: fputs(line,ficparo);
! 5550: }
! 5551: ungetc(c,ficpar);
! 5552:
! 5553:
! 5554: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
! 5555: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
! 5556:
! 5557: fscanf(ficpar,"pop_based=%d\n",&popbased);
! 5558: fprintf(ficparo,"pop_based=%d\n",popbased);
! 5559: fprintf(ficres,"pop_based=%d\n",popbased);
! 5560:
! 5561: while((c=getc(ficpar))=='#' && c!= EOF){
! 5562: ungetc(c,ficpar);
! 5563: fgets(line, MAXLINE, ficpar);
! 5564: puts(line);
! 5565: fputs(line,ficparo);
! 5566: }
! 5567: ungetc(c,ficpar);
! 5568:
! 5569: fscanf(ficpar,"prevforecast=%d starting-proj-date=%lf/%lf/%lf final-proj-date=%lf/%lf/%lf mobil_average=%d\n",&prevfcast,&jproj1,&mproj1,&anproj1,&jproj2,&mproj2,&anproj2,&mobilavproj);
! 5570: fprintf(ficparo,"prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
! 5571: printf("prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
! 5572: fprintf(ficlog,"prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
! 5573: fprintf(ficres,"prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
! 5574: /* day and month of proj2 are not used but only year anproj2.*/
! 5575:
! 5576:
! 5577:
! 5578: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint);*/
! 5579: /*,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
! 5580:
! 5581: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
! 5582: printinggnuplot(fileres, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
! 5583:
! 5584: printinghtml(fileres,title,datafile, firstpass, lastpass, stepm, weightopt,\
! 5585: model,imx,jmin,jmax,jmean,rfileres,popforecast,estepm,\
! 5586: jprev1,mprev1,anprev1,jprev2,mprev2,anprev2);
! 5587:
! 5588: /*------------ free_vector -------------*/
! 5589: /* chdir(path); */
! 5590:
! 5591: free_ivector(wav,1,imx);
! 5592: free_imatrix(dh,1,lastpass-firstpass+1,1,imx);
! 5593: free_imatrix(bh,1,lastpass-firstpass+1,1,imx);
! 5594: free_imatrix(mw,1,lastpass-firstpass+1,1,imx);
! 5595: free_lvector(num,1,n);
! 5596: free_vector(agedc,1,n);
! 5597: /*free_matrix(covar,0,NCOVMAX,1,n);*/
! 5598: /*free_matrix(covar,1,NCOVMAX,1,n);*/
! 5599: fclose(ficparo);
! 5600: fclose(ficres);
! 5601:
! 5602:
! 5603: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
! 5604:
! 5605: strcpy(filerespl,"pl");
! 5606: strcat(filerespl,fileres);
! 5607: if((ficrespl=fopen(filerespl,"w"))==NULL) {
! 5608: printf("Problem with period (stable) prevalence resultfile: %s\n", filerespl);goto end;
! 5609: fprintf(ficlog,"Problem with period (stable) prevalence resultfile: %s\n", filerespl);goto end;
! 5610: }
! 5611: printf("Computing period (stable) prevalence: result on file '%s' \n", filerespl);
! 5612: fprintf(ficlog,"Computing period (stable) prevalence: result on file '%s' \n", filerespl);
! 5613: pstamp(ficrespl);
! 5614: fprintf(ficrespl,"# Period (stable) prevalence \n");
! 5615: fprintf(ficrespl,"#Age ");
! 5616: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
! 5617: fprintf(ficrespl,"\n");
! 5618:
! 5619: prlim=matrix(1,nlstate,1,nlstate);
! 5620:
! 5621: agebase=ageminpar;
! 5622: agelim=agemaxpar;
! 5623: ftolpl=1.e-10;
! 5624: i1=cptcoveff;
! 5625: if (cptcovn < 1){i1=1;}
! 5626:
! 5627: for(cptcov=1,k=0;cptcov<=i1;cptcov++){
! 5628: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
! 5629: k=k+1;
! 5630: /*printf("cptcov=%d cptcod=%d codtab=%d nbcode=%d\n",cptcov, cptcod,Tcode[cptcode],codtab[cptcod][cptcov]);*/
! 5631: fprintf(ficrespl,"\n#******");
! 5632: printf("\n#******");
! 5633: fprintf(ficlog,"\n#******");
! 5634: for(j=1;j<=cptcoveff;j++) {
! 5635: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 5636: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 5637: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 5638: }
! 5639: fprintf(ficrespl,"******\n");
! 5640: printf("******\n");
! 5641: fprintf(ficlog,"******\n");
! 5642:
! 5643: for (age=agebase; age<=agelim; age++){
! 5644: prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
! 5645: fprintf(ficrespl,"%.0f ",age );
! 5646: for(j=1;j<=cptcoveff;j++)
! 5647: fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 5648: for(i=1; i<=nlstate;i++)
! 5649: fprintf(ficrespl," %.5f", prlim[i][i]);
! 5650: fprintf(ficrespl,"\n");
! 5651: }
! 5652: }
! 5653: }
! 5654: fclose(ficrespl);
! 5655:
! 5656: /*------------- h Pij x at various ages ------------*/
! 5657:
! 5658: strcpy(filerespij,"pij"); strcat(filerespij,fileres);
! 5659: if((ficrespij=fopen(filerespij,"w"))==NULL) {
! 5660: printf("Problem with Pij resultfile: %s\n", filerespij);goto end;
! 5661: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij);goto end;
! 5662: }
! 5663: printf("Computing pij: result on file '%s' \n", filerespij);
! 5664: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
! 5665:
! 5666: stepsize=(int) (stepm+YEARM-1)/YEARM;
! 5667: /*if (stepm<=24) stepsize=2;*/
! 5668:
! 5669: agelim=AGESUP;
! 5670: hstepm=stepsize*YEARM; /* Every year of age */
! 5671: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
! 5672:
! 5673: /* hstepm=1; aff par mois*/
! 5674: pstamp(ficrespij);
! 5675: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
! 5676: for(cptcov=1,k=0;cptcov<=i1;cptcov++){
! 5677: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
! 5678: k=k+1;
! 5679: fprintf(ficrespij,"\n#****** ");
! 5680: for(j=1;j<=cptcoveff;j++)
! 5681: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 5682: fprintf(ficrespij,"******\n");
! 5683:
! 5684: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
! 5685: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
! 5686: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
! 5687:
! 5688: /* nhstepm=nhstepm*YEARM; aff par mois*/
! 5689:
! 5690: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 5691: oldm=oldms;savm=savms;
! 5692: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
! 5693: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
! 5694: for(i=1; i<=nlstate;i++)
! 5695: for(j=1; j<=nlstate+ndeath;j++)
! 5696: fprintf(ficrespij," %1d-%1d",i,j);
! 5697: fprintf(ficrespij,"\n");
! 5698: for (h=0; h<=nhstepm; h++){
! 5699: fprintf(ficrespij,"%d %3.f %3.f",k,agedeb, agedeb+ h*hstepm/YEARM*stepm );
! 5700: for(i=1; i<=nlstate;i++)
! 5701: for(j=1; j<=nlstate+ndeath;j++)
! 5702: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
! 5703: fprintf(ficrespij,"\n");
! 5704: }
! 5705: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 5706: fprintf(ficrespij,"\n");
! 5707: }
! 5708: }
! 5709: }
! 5710:
! 5711: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
! 5712:
! 5713: fclose(ficrespij);
! 5714:
! 5715: probs= ma3x(1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
! 5716: for(i=1;i<=AGESUP;i++)
! 5717: for(j=1;j<=NCOVMAX;j++)
! 5718: for(k=1;k<=NCOVMAX;k++)
! 5719: probs[i][j][k]=0.;
! 5720:
! 5721: /*---------- Forecasting ------------------*/
! 5722: /*if((stepm == 1) && (strcmp(model,".")==0)){*/
! 5723: if(prevfcast==1){
! 5724: /* if(stepm ==1){*/
! 5725: prevforecast(fileres, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);
! 5726: /* (popforecast==1) populforecast(fileres, anpyram,mpyram,jpyram, agemin,agemax, dateprev1, dateprev2,mobilav, agedeb, fage, popforecast, popfile, anpyram1,p, i1);*/
! 5727: /* } */
! 5728: /* else{ */
! 5729: /* erreur=108; */
! 5730: /* printf("Warning %d!! You can only forecast the prevalences if the optimization\n has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model); */
! 5731: /* fprintf(ficlog,"Warning %d!! You can only forecast the prevalences if the optimization\n has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model); */
! 5732: /* } */
! 5733: }
! 5734:
! 5735:
! 5736: /*---------- Health expectancies and variances ------------*/
! 5737:
! 5738: strcpy(filerest,"t");
! 5739: strcat(filerest,fileres);
! 5740: if((ficrest=fopen(filerest,"w"))==NULL) {
! 5741: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
! 5742: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
! 5743: }
! 5744: printf("Computing Total Life expectancies with their standard errors: file '%s' \n", filerest);
! 5745: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' \n", filerest);
! 5746:
! 5747:
! 5748: strcpy(filerese,"e");
! 5749: strcat(filerese,fileres);
! 5750: if((ficreseij=fopen(filerese,"w"))==NULL) {
! 5751: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
! 5752: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
! 5753: }
! 5754: printf("Computing Health Expectancies: result on file '%s' \n", filerese);
! 5755: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' \n", filerese);
! 5756:
! 5757: strcpy(fileresstde,"stde");
! 5758: strcat(fileresstde,fileres);
! 5759: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
! 5760: printf("Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
! 5761: fprintf(ficlog,"Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
! 5762: }
! 5763: printf("Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
! 5764: fprintf(ficlog,"Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
! 5765:
! 5766: strcpy(filerescve,"cve");
! 5767: strcat(filerescve,fileres);
! 5768: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
! 5769: printf("Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
! 5770: fprintf(ficlog,"Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
! 5771: }
! 5772: printf("Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
! 5773: fprintf(ficlog,"Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
! 5774:
! 5775: strcpy(fileresv,"v");
! 5776: strcat(fileresv,fileres);
! 5777: if((ficresvij=fopen(fileresv,"w"))==NULL) {
! 5778: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
! 5779: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
! 5780: }
! 5781: printf("Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
! 5782: fprintf(ficlog,"Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
! 5783:
! 5784: /* Computes prevalence between agemin (i.e minimal age computed) and no more ageminpar */
! 5785: prevalence(probs, agemin, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
! 5786: /* printf("ageminpar=%f, agemax=%f, s[lastpass][imx]=%d, agev[lastpass][imx]=%f, nlstate=%d, imx=%d, mint[lastpass][imx]=%f, anint[lastpass][imx]=%f,dateprev1=%f, dateprev2=%f, firstpass=%d, lastpass=%d\n",\
! 5787: ageminpar, agemax, s[lastpass][imx], agev[lastpass][imx], nlstate, imx, mint[lastpass][imx],anint[lastpass][imx], dateprev1, dateprev2, firstpass, lastpass);
! 5788: */
! 5789:
! 5790: if (mobilav!=0) {
! 5791: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
! 5792: if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){
! 5793: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
! 5794: printf(" Error in movingaverage mobilav=%d\n",mobilav);
! 5795: }
! 5796: }
! 5797:
! 5798: for(cptcov=1,k=0;cptcov<=i1;cptcov++){
! 5799: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
! 5800: k=k+1;
! 5801: fprintf(ficrest,"\n#****** ");
! 5802: for(j=1;j<=cptcoveff;j++)
! 5803: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 5804: fprintf(ficrest,"******\n");
! 5805:
! 5806: fprintf(ficreseij,"\n#****** ");
! 5807: fprintf(ficresstdeij,"\n#****** ");
! 5808: fprintf(ficrescveij,"\n#****** ");
! 5809: for(j=1;j<=cptcoveff;j++) {
! 5810: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 5811: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 5812: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 5813: }
! 5814: fprintf(ficreseij,"******\n");
! 5815: fprintf(ficresstdeij,"******\n");
! 5816: fprintf(ficrescveij,"******\n");
! 5817:
! 5818: fprintf(ficresvij,"\n#****** ");
! 5819: for(j=1;j<=cptcoveff;j++)
! 5820: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 5821: fprintf(ficresvij,"******\n");
! 5822:
! 5823: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
! 5824: oldm=oldms;savm=savms;
! 5825: evsij(fileres, eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart);
! 5826: cvevsij(fileres, eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart);
! 5827:
! 5828: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
! 5829: oldm=oldms;savm=savms;
! 5830: varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k, estepm, cptcov,cptcod,0, mobilav, strstart);
! 5831: if(popbased==1){
! 5832: varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k, estepm, cptcov,cptcod,popbased,mobilav, strstart);
! 5833: }
! 5834:
! 5835: pstamp(ficrest);
! 5836: fprintf(ficrest,"# Total life expectancy with std error and decomposition into time to be expected in each health state\n# Age ( e.. (std) ");
! 5837: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
! 5838: fprintf(ficrest,"\n");
! 5839:
! 5840: epj=vector(1,nlstate+1);
! 5841: for(age=bage; age <=fage ;age++){
! 5842: prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
! 5843: if (popbased==1) {
! 5844: if(mobilav ==0){
! 5845: for(i=1; i<=nlstate;i++)
! 5846: prlim[i][i]=probs[(int)age][i][k];
! 5847: }else{ /* mobilav */
! 5848: for(i=1; i<=nlstate;i++)
! 5849: prlim[i][i]=mobaverage[(int)age][i][k];
! 5850: }
! 5851: }
! 5852:
! 5853: fprintf(ficrest," %4.0f",age);
! 5854: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
! 5855: for(i=1, epj[j]=0.;i <=nlstate;i++) {
! 5856: epj[j] += prlim[i][i]*eij[i][j][(int)age];
! 5857: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
! 5858: }
! 5859: epj[nlstate+1] +=epj[j];
! 5860: }
! 5861:
! 5862: for(i=1, vepp=0.;i <=nlstate;i++)
! 5863: for(j=1;j <=nlstate;j++)
! 5864: vepp += vareij[i][j][(int)age];
! 5865: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
! 5866: for(j=1;j <=nlstate;j++){
! 5867: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
! 5868: }
! 5869: fprintf(ficrest,"\n");
! 5870: }
! 5871: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
! 5872: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
! 5873: free_vector(epj,1,nlstate+1);
! 5874: }
! 5875: }
! 5876: free_vector(weight,1,n);
! 5877: free_imatrix(Tvard,1,15,1,2);
! 5878: free_imatrix(s,1,maxwav+1,1,n);
! 5879: free_matrix(anint,1,maxwav,1,n);
! 5880: free_matrix(mint,1,maxwav,1,n);
! 5881: free_ivector(cod,1,n);
! 5882: free_ivector(tab,1,NCOVMAX);
! 5883: fclose(ficreseij);
! 5884: fclose(ficresstdeij);
! 5885: fclose(ficrescveij);
! 5886: fclose(ficresvij);
! 5887: fclose(ficrest);
! 5888: fclose(ficpar);
! 5889:
! 5890: /*------- Variance of period (stable) prevalence------*/
! 5891:
! 5892: strcpy(fileresvpl,"vpl");
! 5893: strcat(fileresvpl,fileres);
! 5894: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
! 5895: printf("Problem with variance of period (stable) prevalence resultfile: %s\n", fileresvpl);
! 5896: exit(0);
! 5897: }
! 5898: printf("Computing Variance-covariance of period (stable) prevalence: file '%s' \n", fileresvpl);
! 5899:
! 5900: for(cptcov=1,k=0;cptcov<=i1;cptcov++){
! 5901: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
! 5902: k=k+1;
! 5903: fprintf(ficresvpl,"\n#****** ");
! 5904: for(j=1;j<=cptcoveff;j++)
! 5905: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 5906: fprintf(ficresvpl,"******\n");
! 5907:
! 5908: varpl=matrix(1,nlstate,(int) bage, (int) fage);
! 5909: oldm=oldms;savm=savms;
! 5910: varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k,strstart);
! 5911: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
! 5912: }
! 5913: }
! 5914:
! 5915: fclose(ficresvpl);
! 5916:
! 5917: /*---------- End : free ----------------*/
! 5918: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
! 5919: free_ma3x(probs,1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
! 5920:
! 5921: } /* mle==-3 arrives here for freeing */
! 5922: free_matrix(prlim,1,nlstate,1,nlstate);
! 5923: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
! 5924: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
! 5925: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
! 5926: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
! 5927: free_matrix(covar,0,NCOVMAX,1,n);
! 5928: free_matrix(matcov,1,npar,1,npar);
! 5929: /*free_vector(delti,1,npar);*/
! 5930: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
! 5931: free_matrix(agev,1,maxwav,1,imx);
! 5932: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
! 5933:
! 5934: free_ivector(ncodemax,1,8);
! 5935: free_ivector(Tvar,1,15);
! 5936: free_ivector(Tprod,1,15);
! 5937: free_ivector(Tvaraff,1,15);
! 5938: free_ivector(Tage,1,15);
! 5939: free_ivector(Tcode,1,100);
! 5940:
! 5941: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
! 5942: free_imatrix(codtab,1,100,1,10);
! 5943: fflush(fichtm);
! 5944: fflush(ficgp);
! 5945:
! 5946:
! 5947: if((nberr >0) || (nbwarn>0)){
! 5948: printf("End of Imach with %d errors and/or %d warnings\n",nberr,nbwarn);
! 5949: fprintf(ficlog,"End of Imach with %d errors and/or warnings %d\n",nberr,nbwarn);
! 5950: }else{
! 5951: printf("End of Imach\n");
! 5952: fprintf(ficlog,"End of Imach\n");
! 5953: }
! 5954: printf("See log file on %s\n",filelog);
! 5955: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
! 5956: (void) gettimeofday(&end_time,&tzp);
! 5957: tm = *localtime(&end_time.tv_sec);
! 5958: tmg = *gmtime(&end_time.tv_sec);
! 5959: strcpy(strtend,asctime(&tm));
! 5960: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
! 5961: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
! 5962: printf("Total time used %s\n", asc_diff_time(end_time.tv_sec -start_time.tv_sec,tmpout));
! 5963:
! 5964: printf("Total time was %d Sec.\n", end_time.tv_sec -start_time.tv_sec);
! 5965: fprintf(ficlog,"Total time used %s\n", asc_diff_time(end_time.tv_sec -start_time.tv_sec,tmpout));
! 5966: fprintf(ficlog,"Total time was %d Sec.\n", end_time.tv_sec -start_time.tv_sec);
! 5967: /* printf("Total time was %d uSec.\n", total_usecs);*/
! 5968: /* if(fileappend(fichtm,optionfilehtm)){ */
! 5969: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
! 5970: fclose(fichtm);
! 5971: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
! 5972: fclose(fichtmcov);
! 5973: fclose(ficgp);
! 5974: fclose(ficlog);
! 5975: /*------ End -----------*/
! 5976:
! 5977:
! 5978: printf("Before Current directory %s!\n",pathcd);
! 5979: if(chdir(pathcd) != 0)
! 5980: printf("Can't move to directory %s!\n",path);
! 5981: if(getcwd(pathcd,MAXLINE) > 0)
! 5982: printf("Current directory %s!\n",pathcd);
! 5983: /*strcat(plotcmd,CHARSEPARATOR);*/
! 5984: sprintf(plotcmd,"gnuplot");
! 5985: #ifndef UNIX
! 5986: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
! 5987: #endif
! 5988: if(!stat(plotcmd,&info)){
! 5989: printf("Error gnuplot program not found: %s\n",plotcmd);fflush(stdout);
! 5990: if(!stat(getenv("GNUPLOTBIN"),&info)){
! 5991: printf("Error gnuplot program not found: %s Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
! 5992: }else
! 5993: strcpy(pplotcmd,plotcmd);
! 5994: #ifdef UNIX
! 5995: strcpy(plotcmd,GNUPLOTPROGRAM);
! 5996: if(!stat(plotcmd,&info)){
! 5997: printf("Error gnuplot program not found: %s\n",plotcmd);fflush(stdout);
! 5998: }else
! 5999: strcpy(pplotcmd,plotcmd);
! 6000: #endif
! 6001: }else
! 6002: strcpy(pplotcmd,plotcmd);
! 6003:
! 6004: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
! 6005: printf("Starting graphs with: %s\n",plotcmd);fflush(stdout);
! 6006:
! 6007: if((outcmd=system(plotcmd)) != 0){
! 6008: printf("\n Problem with gnuplot\n");
! 6009: }
! 6010: printf(" Wait...");
! 6011: while (z[0] != 'q') {
! 6012: /* chdir(path); */
! 6013: printf("\nType e to edit output files, g to graph again and q for exiting: ");
! 6014: scanf("%s",z);
! 6015: /* if (z[0] == 'c') system("./imach"); */
! 6016: if (z[0] == 'e') {
! 6017: printf("Starting browser with: %s",optionfilehtm);fflush(stdout);
! 6018: system(optionfilehtm);
! 6019: }
! 6020: else if (z[0] == 'g') system(plotcmd);
! 6021: else if (z[0] == 'q') exit(0);
! 6022: }
! 6023: end:
! 6024: while (z[0] != 'q') {
! 6025: printf("\nType q for exiting: ");
! 6026: scanf("%s",z);
! 6027: }
! 6028: }
! 6029:
! 6030:
! 6031:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>