Annotation of imach/src/imach.c, revision 1.117
1.117 ! brouard 1: /* $Id: imach.c,v 1.116 2006/03/06 10:29:27 brouard Exp $
1.83 lievre 2: $State: Exp $
3: $Log: imach.c,v $
1.117 ! brouard 4: Revision 1.116 2006/03/06 10:29:27 brouard
! 5: (Module): Variance-covariance wrong links and
! 6: varian-covariance of ej. is needed (Saito).
! 7:
1.116 brouard 8: Revision 1.115 2006/02/27 12:17:45 brouard
9: (Module): One freematrix added in mlikeli! 0.98c
10:
1.115 brouard 11: Revision 1.114 2006/02/26 12:57:58 brouard
12: (Module): Some improvements in processing parameter
13: filename with strsep.
14:
1.114 brouard 15: Revision 1.113 2006/02/24 14:20:24 brouard
16: (Module): Memory leaks checks with valgrind and:
17: datafile was not closed, some imatrix were not freed and on matrix
18: allocation too.
19:
1.113 brouard 20: Revision 1.112 2006/01/30 09:55:26 brouard
21: (Module): Back to gnuplot.exe instead of wgnuplot.exe
22:
1.112 brouard 23: Revision 1.111 2006/01/25 20:38:18 brouard
24: (Module): Lots of cleaning and bugs added (Gompertz)
25: (Module): Comments can be added in data file. Missing date values
26: can be a simple dot '.'.
27:
1.111 brouard 28: Revision 1.110 2006/01/25 00:51:50 brouard
29: (Module): Lots of cleaning and bugs added (Gompertz)
30:
1.110 brouard 31: Revision 1.109 2006/01/24 19:37:15 brouard
32: (Module): Comments (lines starting with a #) are allowed in data.
33:
1.109 brouard 34: Revision 1.108 2006/01/19 18:05:42 lievre
35: Gnuplot problem appeared...
36: To be fixed
37:
1.108 lievre 38: Revision 1.107 2006/01/19 16:20:37 brouard
39: Test existence of gnuplot in imach path
40:
1.107 brouard 41: Revision 1.106 2006/01/19 13:24:36 brouard
42: Some cleaning and links added in html output
43:
1.106 brouard 44: Revision 1.105 2006/01/05 20:23:19 lievre
45: *** empty log message ***
46:
1.105 lievre 47: Revision 1.104 2005/09/30 16:11:43 lievre
48: (Module): sump fixed, loop imx fixed, and simplifications.
49: (Module): If the status is missing at the last wave but we know
50: that the person is alive, then we can code his/her status as -2
51: (instead of missing=-1 in earlier versions) and his/her
52: contributions to the likelihood is 1 - Prob of dying from last
53: health status (= 1-p13= p11+p12 in the easiest case of somebody in
54: the healthy state at last known wave). Version is 0.98
55:
1.104 lievre 56: Revision 1.103 2005/09/30 15:54:49 lievre
57: (Module): sump fixed, loop imx fixed, and simplifications.
58:
1.103 lievre 59: Revision 1.102 2004/09/15 17:31:30 brouard
60: Add the possibility to read data file including tab characters.
61:
1.102 brouard 62: Revision 1.101 2004/09/15 10:38:38 brouard
63: Fix on curr_time
64:
1.101 brouard 65: Revision 1.100 2004/07/12 18:29:06 brouard
66: Add version for Mac OS X. Just define UNIX in Makefile
67:
1.100 brouard 68: Revision 1.99 2004/06/05 08:57:40 brouard
69: *** empty log message ***
70:
1.99 brouard 71: Revision 1.98 2004/05/16 15:05:56 brouard
72: New version 0.97 . First attempt to estimate force of mortality
73: directly from the data i.e. without the need of knowing the health
74: state at each age, but using a Gompertz model: log u =a + b*age .
75: This is the basic analysis of mortality and should be done before any
76: other analysis, in order to test if the mortality estimated from the
77: cross-longitudinal survey is different from the mortality estimated
78: from other sources like vital statistic data.
79:
80: The same imach parameter file can be used but the option for mle should be -3.
81:
82: Agnès, who wrote this part of the code, tried to keep most of the
83: former routines in order to include the new code within the former code.
84:
85: The output is very simple: only an estimate of the intercept and of
86: the slope with 95% confident intervals.
87:
88: Current limitations:
89: A) Even if you enter covariates, i.e. with the
90: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
91: B) There is no computation of Life Expectancy nor Life Table.
92:
1.98 brouard 93: Revision 1.97 2004/02/20 13:25:42 lievre
94: Version 0.96d. Population forecasting command line is (temporarily)
95: suppressed.
96:
1.97 lievre 97: Revision 1.96 2003/07/15 15:38:55 brouard
98: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
99: rewritten within the same printf. Workaround: many printfs.
100:
1.96 brouard 101: Revision 1.95 2003/07/08 07:54:34 brouard
102: * imach.c (Repository):
103: (Repository): Using imachwizard code to output a more meaningful covariance
104: matrix (cov(a12,c31) instead of numbers.
105:
1.95 brouard 106: Revision 1.94 2003/06/27 13:00:02 brouard
107: Just cleaning
108:
1.94 brouard 109: Revision 1.93 2003/06/25 16:33:55 brouard
110: (Module): On windows (cygwin) function asctime_r doesn't
111: exist so I changed back to asctime which exists.
112: (Module): Version 0.96b
113:
1.93 brouard 114: Revision 1.92 2003/06/25 16:30:45 brouard
115: (Module): On windows (cygwin) function asctime_r doesn't
116: exist so I changed back to asctime which exists.
117:
1.92 brouard 118: Revision 1.91 2003/06/25 15:30:29 brouard
119: * imach.c (Repository): Duplicated warning errors corrected.
120: (Repository): Elapsed time after each iteration is now output. It
121: helps to forecast when convergence will be reached. Elapsed time
122: is stamped in powell. We created a new html file for the graphs
123: concerning matrix of covariance. It has extension -cov.htm.
124:
1.91 brouard 125: Revision 1.90 2003/06/24 12:34:15 brouard
126: (Module): Some bugs corrected for windows. Also, when
127: mle=-1 a template is output in file "or"mypar.txt with the design
128: of the covariance matrix to be input.
129:
1.90 brouard 130: Revision 1.89 2003/06/24 12:30:52 brouard
131: (Module): Some bugs corrected for windows. Also, when
132: mle=-1 a template is output in file "or"mypar.txt with the design
133: of the covariance matrix to be input.
134:
1.89 brouard 135: Revision 1.88 2003/06/23 17:54:56 brouard
136: * 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.
137:
1.88 brouard 138: Revision 1.87 2003/06/18 12:26:01 brouard
139: Version 0.96
140:
1.87 brouard 141: Revision 1.86 2003/06/17 20:04:08 brouard
142: (Module): Change position of html and gnuplot routines and added
143: routine fileappend.
144:
1.86 brouard 145: Revision 1.85 2003/06/17 13:12:43 brouard
146: * imach.c (Repository): Check when date of death was earlier that
147: current date of interview. It may happen when the death was just
148: prior to the death. In this case, dh was negative and likelihood
149: was wrong (infinity). We still send an "Error" but patch by
150: assuming that the date of death was just one stepm after the
151: interview.
152: (Repository): Because some people have very long ID (first column)
153: we changed int to long in num[] and we added a new lvector for
154: memory allocation. But we also truncated to 8 characters (left
155: truncation)
156: (Repository): No more line truncation errors.
157:
1.85 brouard 158: Revision 1.84 2003/06/13 21:44:43 brouard
159: * imach.c (Repository): Replace "freqsummary" at a correct
160: place. It differs from routine "prevalence" which may be called
161: many times. Probs is memory consuming and must be used with
162: parcimony.
1.86 brouard 163: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
1.85 brouard 164:
1.84 brouard 165: Revision 1.83 2003/06/10 13:39:11 lievre
166: *** empty log message ***
167:
1.83 lievre 168: Revision 1.82 2003/06/05 15:57:20 brouard
169: Add log in imach.c and fullversion number is now printed.
170:
1.82 brouard 171: */
172: /*
1.53 brouard 173: Interpolated Markov Chain
174:
175: Short summary of the programme:
176:
177: This program computes Healthy Life Expectancies from
178: cross-longitudinal data. Cross-longitudinal data consist in: -1- a
179: first survey ("cross") where individuals from different ages are
180: interviewed on their health status or degree of disability (in the
181: case of a health survey which is our main interest) -2- at least a
182: second wave of interviews ("longitudinal") which measure each change
183: (if any) in individual health status. Health expectancies are
184: computed from the time spent in each health state according to a
185: model. More health states you consider, more time is necessary to reach the
186: Maximum Likelihood of the parameters involved in the model. The
187: simplest model is the multinomial logistic model where pij is the
188: probability to be observed in state j at the second wave
189: conditional to be observed in state i at the first wave. Therefore
190: the model is: log(pij/pii)= aij + bij*age+ cij*sex + etc , where
191: 'age' is age and 'sex' is a covariate. If you want to have a more
192: complex model than "constant and age", you should modify the program
193: where the markup *Covariates have to be included here again* invites
194: you to do it. More covariates you add, slower the
195: convergence.
196:
197: The advantage of this computer programme, compared to a simple
198: multinomial logistic model, is clear when the delay between waves is not
199: identical for each individual. Also, if a individual missed an
200: intermediate interview, the information is lost, but taken into
201: account using an interpolation or extrapolation.
202:
203: hPijx is the probability to be observed in state i at age x+h
204: conditional to the observed state i at age x. The delay 'h' can be
205: split into an exact number (nh*stepm) of unobserved intermediate
1.66 brouard 206: states. This elementary transition (by month, quarter,
207: semester or year) is modelled as a multinomial logistic. The hPx
1.53 brouard 208: matrix is simply the matrix product of nh*stepm elementary matrices
209: and the contribution of each individual to the likelihood is simply
210: hPijx.
211:
212: Also this programme outputs the covariance matrix of the parameters but also
1.117 ! brouard 213: of the life expectancies. It also computes the period (stable) prevalence.
1.53 brouard 214:
215: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
216: Institut national d'études démographiques, Paris.
217: This software have been partly granted by Euro-REVES, a concerted action
218: from the European Union.
219: It is copyrighted identically to a GNU software product, ie programme and
220: software can be distributed freely for non commercial use. Latest version
221: can be accessed at http://euroreves.ined.fr/imach .
1.74 brouard 222:
223: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
224: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
225:
1.53 brouard 226: **********************************************************************/
1.74 brouard 227: /*
228: main
229: read parameterfile
230: read datafile
231: concatwav
1.84 brouard 232: freqsummary
1.74 brouard 233: if (mle >= 1)
234: mlikeli
235: print results files
236: if mle==1
237: computes hessian
238: read end of parameter file: agemin, agemax, bage, fage, estepm
239: begin-prev-date,...
240: open gnuplot file
241: open html file
1.117 ! brouard 242: period (stable) prevalence
1.74 brouard 243: for age prevalim()
244: h Pij x
245: variance of p varprob
246: forecasting if prevfcast==1 prevforecast call prevalence()
247: health expectancies
248: Variance-covariance of DFLE
249: prevalence()
250: movingaverage()
251: varevsij()
252: if popbased==1 varevsij(,popbased)
253: total life expectancies
1.117 ! brouard 254: Variance of period (stable) prevalence
1.74 brouard 255: end
256: */
257:
258:
259:
1.53 brouard 260:
261: #include <math.h>
262: #include <stdio.h>
263: #include <stdlib.h>
1.106 brouard 264: #include <string.h>
1.53 brouard 265: #include <unistd.h>
266:
1.109 brouard 267: #include <limits.h>
1.107 brouard 268: #include <sys/types.h>
269: #include <sys/stat.h>
270: #include <errno.h>
271: extern int errno;
272:
1.99 brouard 273: /* #include <sys/time.h> */
1.86 brouard 274: #include <time.h>
275: #include "timeval.h"
276:
1.95 brouard 277: /* #include <libintl.h> */
278: /* #define _(String) gettext (String) */
279:
1.53 brouard 280: #define MAXLINE 256
1.108 lievre 281:
1.53 brouard 282: #define GNUPLOTPROGRAM "gnuplot"
283: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
1.85 brouard 284: #define FILENAMELENGTH 132
1.108 lievre 285:
1.53 brouard 286: #define GLOCK_ERROR_NOPATH -1 /* empty path */
287: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
288:
289: #define MAXPARM 30 /* Maximum number of parameters for the optimization */
290: #define NPARMAX 64 /* (nlstate+ndeath-1)*nlstate*ncovmodel */
291:
292: #define NINTERVMAX 8
293: #define NLSTATEMAX 8 /* Maximum number of live states (for func) */
294: #define NDEATHMAX 8 /* Maximum number of dead states (for func) */
295: #define NCOVMAX 8 /* Maximum number of covariates */
296: #define MAXN 20000
297: #define YEARM 12. /* Number of months per year */
298: #define AGESUP 130
299: #define AGEBASE 40
1.98 brouard 300: #define AGEGOMP 10. /* Minimal age for Gompertz adjustment */
1.100 brouard 301: #ifdef UNIX
1.85 brouard 302: #define DIRSEPARATOR '/'
1.107 brouard 303: #define CHARSEPARATOR "/"
1.85 brouard 304: #define ODIRSEPARATOR '\\'
305: #else
1.53 brouard 306: #define DIRSEPARATOR '\\'
1.107 brouard 307: #define CHARSEPARATOR "\\"
1.53 brouard 308: #define ODIRSEPARATOR '/'
309: #endif
310:
1.117 ! brouard 311: /* $Id: imach.c,v 1.116 2006/03/06 10:29:27 brouard Exp $ */
1.81 brouard 312: /* $State: Exp $ */
1.80 brouard 313:
1.117 ! brouard 314: char version[]="Imach version 0.98d, March 2006, INED-EUROREVES-Institut de longevite ";
! 315: char fullversion[]="$Revision: 1.116 $ $Date: 2006/03/06 10:29:27 $";
! 316: char strstart[80];
! 317: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.91 brouard 318: int erreur, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.53 brouard 319: int nvar;
320: int cptcovn=0, cptcovage=0, cptcoveff=0,cptcov;
321: int npar=NPARMAX;
322: int nlstate=2; /* Number of live states */
323: int ndeath=1; /* Number of dead states */
324: int ncovmodel, ncovcol; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
325: int popbased=0;
326:
327: int *wav; /* Number of waves for this individuual 0 is possible */
328: int maxwav; /* Maxim number of waves */
329: int jmin, jmax; /* min, max spacing between 2 waves */
1.110 brouard 330: int ijmin, ijmax; /* Individuals having jmin and jmax */
1.87 brouard 331: int gipmx, gsw; /* Global variables on the number of contributions
332: to the likelihood and the sum of weights (done by funcone)*/
1.53 brouard 333: int mle, weightopt;
334: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
335: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
1.59 brouard 336: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
337: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.53 brouard 338: double jmean; /* Mean space between 2 waves */
339: double **oldm, **newm, **savm; /* Working pointers to matrices */
340: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.117 ! brouard 341: FILE *fic,*ficpar, *ficparo,*ficres, *ficresp, *ficrespl, *ficrespij, *ficrest,*ficresf,*ficrespop;
1.76 brouard 342: FILE *ficlog, *ficrespow;
1.85 brouard 343: int globpr; /* Global variable for printing or not */
344: double fretone; /* Only one call to likelihood */
345: long ipmx; /* Number of contributions */
346: double sw; /* Sum of weights */
1.98 brouard 347: char filerespow[FILENAMELENGTH];
1.85 brouard 348: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
349: FILE *ficresilk;
1.53 brouard 350: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
351: FILE *ficresprobmorprev;
1.91 brouard 352: FILE *fichtm, *fichtmcov; /* Html File */
1.53 brouard 353: FILE *ficreseij;
354: char filerese[FILENAMELENGTH];
1.117 ! brouard 355: FILE *ficresstdeij;
! 356: char fileresstde[FILENAMELENGTH];
! 357: FILE *ficrescveij;
! 358: char filerescve[FILENAMELENGTH];
1.53 brouard 359: FILE *ficresvij;
360: char fileresv[FILENAMELENGTH];
361: FILE *ficresvpl;
362: char fileresvpl[FILENAMELENGTH];
363: char title[MAXLINE];
364: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH];
1.117 ! brouard 365: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
1.96 brouard 366: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
1.88 brouard 367: char command[FILENAMELENGTH];
368: int outcmd=0;
1.53 brouard 369:
370: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.94 brouard 371:
1.53 brouard 372: char filelog[FILENAMELENGTH]; /* Log file */
373: char filerest[FILENAMELENGTH];
374: char fileregp[FILENAMELENGTH];
375: char popfile[FILENAMELENGTH];
376:
1.91 brouard 377: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
378:
379: struct timeval start_time, end_time, curr_time, last_time, forecast_time;
380: struct timezone tzp;
381: extern int gettimeofday();
382: struct tm tmg, tm, tmf, *gmtime(), *localtime();
383: long time_value;
384: extern long time();
385: char strcurr[80], strfor[80];
1.53 brouard 386:
1.109 brouard 387: char *endptr;
388: long lval;
389:
1.53 brouard 390: #define NR_END 1
391: #define FREE_ARG char*
392: #define FTOL 1.0e-10
393:
394: #define NRANSI
395: #define ITMAX 200
396:
397: #define TOL 2.0e-4
398:
399: #define CGOLD 0.3819660
400: #define ZEPS 1.0e-10
401: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
402:
403: #define GOLD 1.618034
404: #define GLIMIT 100.0
405: #define TINY 1.0e-20
406:
407: static double maxarg1,maxarg2;
408: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
409: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
410:
411: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
412: #define rint(a) floor(a+0.5)
413:
414: static double sqrarg;
415: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
416: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1.98 brouard 417: int agegomp= AGEGOMP;
1.53 brouard 418:
419: int imx;
1.98 brouard 420: int stepm=1;
1.53 brouard 421: /* Stepm, step in month: minimum step interpolation*/
422:
423: int estepm;
424: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
425:
426: int m,nb;
1.85 brouard 427: long *num;
1.98 brouard 428: int firstpass=0, lastpass=4,*cod, *ncodemax, *Tage,*cens;
1.53 brouard 429: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.55 lievre 430: double **pmmij, ***probs;
1.98 brouard 431: double *ageexmed,*agecens;
1.53 brouard 432: double dateintmean=0;
433:
434: double *weight;
435: int **s; /* Status */
436: double *agedc, **covar, idx;
437: int **nbcode, *Tcode, *Tvar, **codtab, **Tvard, *Tprod, cptcovprod, *Tvaraff;
1.105 lievre 438: double *lsurv, *lpop, *tpop;
1.53 brouard 439:
440: double ftol=FTOL; /* Tolerance for computing Max Likelihood */
441: double ftolhess; /* Tolerance for computing hessian */
442:
443: /**************** split *************************/
444: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
445: {
1.107 brouard 446: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1.99 brouard 447: the name of the file (name), its extension only (ext) and its first part of the name (finame)
448: */
1.59 brouard 449: char *ss; /* pointer */
450: int l1, l2; /* length counters */
1.53 brouard 451:
1.59 brouard 452: l1 = strlen(path ); /* length of path */
453: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
454: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1.107 brouard 455: if ( ss == NULL ) { /* no directory, so determine current directory */
456: strcpy( name, path ); /* we got the fullname name because no directory */
1.59 brouard 457: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
458: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1.74 brouard 459: /* get current working directory */
460: /* extern char* getcwd ( char *buf , int len);*/
1.59 brouard 461: if ( getcwd( dirc, FILENAME_MAX ) == NULL ) {
462: return( GLOCK_ERROR_GETCWD );
463: }
1.107 brouard 464: /* got dirc from getcwd*/
465: printf(" DIRC = %s \n",dirc);
1.59 brouard 466: } else { /* strip direcotry from path */
467: ss++; /* after this, the filename */
468: l2 = strlen( ss ); /* length of filename */
469: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
470: strcpy( name, ss ); /* save file name */
471: strncpy( dirc, path, l1 - l2 ); /* now the directory */
472: dirc[l1-l2] = 0; /* add zero */
1.107 brouard 473: printf(" DIRC2 = %s \n",dirc);
1.59 brouard 474: }
1.107 brouard 475: /* We add a separator at the end of dirc if not exists */
1.59 brouard 476: l1 = strlen( dirc ); /* length of directory */
1.107 brouard 477: if( dirc[l1-1] != DIRSEPARATOR ){
478: dirc[l1] = DIRSEPARATOR;
479: dirc[l1+1] = 0;
480: printf(" DIRC3 = %s \n",dirc);
481: }
1.59 brouard 482: ss = strrchr( name, '.' ); /* find last / */
1.99 brouard 483: if (ss >0){
484: ss++;
485: strcpy(ext,ss); /* save extension */
486: l1= strlen( name);
487: l2= strlen(ss)+1;
488: strncpy( finame, name, l1-l2);
489: finame[l1-l2]= 0;
490: }
1.107 brouard 491:
1.59 brouard 492: return( 0 ); /* we're done */
1.53 brouard 493: }
494:
495:
496: /******************************************/
497:
1.89 brouard 498: void replace_back_to_slash(char *s, char*t)
1.53 brouard 499: {
500: int i;
1.89 brouard 501: int lg=0;
1.53 brouard 502: i=0;
503: lg=strlen(t);
504: for(i=0; i<= lg; i++) {
505: (s[i] = t[i]);
506: if (t[i]== '\\') s[i]='/';
507: }
508: }
509:
510: int nbocc(char *s, char occ)
511: {
512: int i,j=0;
513: int lg=20;
514: i=0;
515: lg=strlen(s);
516: for(i=0; i<= lg; i++) {
517: if (s[i] == occ ) j++;
518: }
519: return j;
520: }
521:
522: void cutv(char *u,char *v, char*t, char occ)
523: {
1.102 brouard 524: /* cuts string t into u and v where u ends before first occurence of char 'occ'
525: and v starts after first occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2')
1.53 brouard 526: gives u="abcedf" and v="ghi2j" */
527: int i,lg,j,p=0;
528: i=0;
529: for(j=0; j<=strlen(t)-1; j++) {
530: if((t[j]!= occ) && (t[j+1]== occ)) p=j+1;
531: }
532:
533: lg=strlen(t);
534: for(j=0; j<p; j++) {
535: (u[j] = t[j]);
536: }
537: u[p]='\0';
538:
539: for(j=0; j<= lg; j++) {
540: if (j>=(p+1))(v[j-p-1] = t[j]);
541: }
542: }
543:
544: /********************** nrerror ********************/
545:
546: void nrerror(char error_text[])
547: {
548: fprintf(stderr,"ERREUR ...\n");
549: fprintf(stderr,"%s\n",error_text);
1.59 brouard 550: exit(EXIT_FAILURE);
1.53 brouard 551: }
552: /*********************** vector *******************/
553: double *vector(int nl, int nh)
554: {
555: double *v;
556: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
557: if (!v) nrerror("allocation failure in vector");
558: return v-nl+NR_END;
559: }
560:
561: /************************ free vector ******************/
562: void free_vector(double*v, int nl, int nh)
563: {
564: free((FREE_ARG)(v+nl-NR_END));
565: }
566:
567: /************************ivector *******************************/
1.85 brouard 568: int *ivector(long nl,long nh)
1.76 brouard 569: {
1.85 brouard 570: int *v;
571: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
572: if (!v) nrerror("allocation failure in ivector");
1.76 brouard 573: return v-nl+NR_END;
574: }
575:
576: /******************free ivector **************************/
1.85 brouard 577: void free_ivector(int *v, long nl, long nh)
1.76 brouard 578: {
579: free((FREE_ARG)(v+nl-NR_END));
580: }
581:
1.85 brouard 582: /************************lvector *******************************/
583: long *lvector(long nl,long nh)
1.53 brouard 584: {
1.85 brouard 585: long *v;
586: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
1.53 brouard 587: if (!v) nrerror("allocation failure in ivector");
588: return v-nl+NR_END;
589: }
590:
1.85 brouard 591: /******************free lvector **************************/
592: void free_lvector(long *v, long nl, long nh)
1.53 brouard 593: {
594: free((FREE_ARG)(v+nl-NR_END));
595: }
596:
597: /******************* imatrix *******************************/
598: int **imatrix(long nrl, long nrh, long ncl, long nch)
599: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
600: {
601: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
602: int **m;
603:
604: /* allocate pointers to rows */
605: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
606: if (!m) nrerror("allocation failure 1 in matrix()");
607: m += NR_END;
608: m -= nrl;
609:
610:
611: /* allocate rows and set pointers to them */
612: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
613: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
614: m[nrl] += NR_END;
615: m[nrl] -= ncl;
616:
617: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
618:
619: /* return pointer to array of pointers to rows */
620: return m;
621: }
622:
623: /****************** free_imatrix *************************/
624: void free_imatrix(m,nrl,nrh,ncl,nch)
625: int **m;
626: long nch,ncl,nrh,nrl;
627: /* free an int matrix allocated by imatrix() */
628: {
629: free((FREE_ARG) (m[nrl]+ncl-NR_END));
630: free((FREE_ARG) (m+nrl-NR_END));
631: }
632:
633: /******************* matrix *******************************/
634: double **matrix(long nrl, long nrh, long ncl, long nch)
635: {
636: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
637: double **m;
638:
639: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
640: if (!m) nrerror("allocation failure 1 in matrix()");
641: m += NR_END;
642: m -= nrl;
643:
644: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
645: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
646: m[nrl] += NR_END;
647: m[nrl] -= ncl;
648:
649: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
650: return m;
1.85 brouard 651: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1])
1.74 brouard 652: */
1.53 brouard 653: }
654:
655: /*************************free matrix ************************/
656: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
657: {
658: free((FREE_ARG)(m[nrl]+ncl-NR_END));
659: free((FREE_ARG)(m+nrl-NR_END));
660: }
661:
662: /******************* ma3x *******************************/
663: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
664: {
665: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
666: double ***m;
667:
668: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
669: if (!m) nrerror("allocation failure 1 in matrix()");
670: m += NR_END;
671: m -= nrl;
672:
673: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
674: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
675: m[nrl] += NR_END;
676: m[nrl] -= ncl;
677:
678: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
679:
680: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
681: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
682: m[nrl][ncl] += NR_END;
683: m[nrl][ncl] -= nll;
684: for (j=ncl+1; j<=nch; j++)
685: m[nrl][j]=m[nrl][j-1]+nlay;
686:
687: for (i=nrl+1; i<=nrh; i++) {
688: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
689: for (j=ncl+1; j<=nch; j++)
690: m[i][j]=m[i][j-1]+nlay;
691: }
1.74 brouard 692: return m;
693: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
694: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
695: */
1.53 brouard 696: }
697:
698: /*************************free ma3x ************************/
699: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
700: {
701: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
702: free((FREE_ARG)(m[nrl]+ncl-NR_END));
703: free((FREE_ARG)(m+nrl-NR_END));
704: }
705:
1.94 brouard 706: /*************** function subdirf ***********/
707: char *subdirf(char fileres[])
708: {
709: /* Caution optionfilefiname is hidden */
710: strcpy(tmpout,optionfilefiname);
711: strcat(tmpout,"/"); /* Add to the right */
712: strcat(tmpout,fileres);
713: return tmpout;
714: }
715:
716: /*************** function subdirf2 ***********/
717: char *subdirf2(char fileres[], char *preop)
718: {
719:
720: /* Caution optionfilefiname is hidden */
721: strcpy(tmpout,optionfilefiname);
722: strcat(tmpout,"/");
723: strcat(tmpout,preop);
724: strcat(tmpout,fileres);
725: return tmpout;
726: }
727:
728: /*************** function subdirf3 ***********/
729: char *subdirf3(char fileres[], char *preop, char *preop2)
730: {
731:
732: /* Caution optionfilefiname is hidden */
733: strcpy(tmpout,optionfilefiname);
734: strcat(tmpout,"/");
735: strcat(tmpout,preop);
736: strcat(tmpout,preop2);
737: strcat(tmpout,fileres);
738: return tmpout;
739: }
740:
1.53 brouard 741: /***************** f1dim *************************/
742: extern int ncom;
743: extern double *pcom,*xicom;
744: extern double (*nrfunc)(double []);
745:
746: double f1dim(double x)
747: {
748: int j;
749: double f;
750: double *xt;
751:
752: xt=vector(1,ncom);
753: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
754: f=(*nrfunc)(xt);
755: free_vector(xt,1,ncom);
756: return f;
757: }
758:
759: /*****************brent *************************/
760: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
761: {
762: int iter;
763: double a,b,d,etemp;
764: double fu,fv,fw,fx;
765: double ftemp;
766: double p,q,r,tol1,tol2,u,v,w,x,xm;
767: double e=0.0;
768:
769: a=(ax < cx ? ax : cx);
770: b=(ax > cx ? ax : cx);
771: x=w=v=bx;
772: fw=fv=fx=(*f)(x);
773: for (iter=1;iter<=ITMAX;iter++) {
774: xm=0.5*(a+b);
775: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
776: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
777: printf(".");fflush(stdout);
778: fprintf(ficlog,".");fflush(ficlog);
779: #ifdef DEBUG
780: 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);
781: 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);
782: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
783: #endif
784: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
785: *xmin=x;
786: return fx;
787: }
788: ftemp=fu;
789: if (fabs(e) > tol1) {
790: r=(x-w)*(fx-fv);
791: q=(x-v)*(fx-fw);
792: p=(x-v)*q-(x-w)*r;
793: q=2.0*(q-r);
794: if (q > 0.0) p = -p;
795: q=fabs(q);
796: etemp=e;
797: e=d;
798: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
799: d=CGOLD*(e=(x >= xm ? a-x : b-x));
800: else {
801: d=p/q;
802: u=x+d;
803: if (u-a < tol2 || b-u < tol2)
804: d=SIGN(tol1,xm-x);
805: }
806: } else {
807: d=CGOLD*(e=(x >= xm ? a-x : b-x));
808: }
809: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
810: fu=(*f)(u);
811: if (fu <= fx) {
812: if (u >= x) a=x; else b=x;
813: SHFT(v,w,x,u)
814: SHFT(fv,fw,fx,fu)
815: } else {
816: if (u < x) a=u; else b=u;
817: if (fu <= fw || w == x) {
818: v=w;
819: w=u;
820: fv=fw;
821: fw=fu;
822: } else if (fu <= fv || v == x || v == w) {
823: v=u;
824: fv=fu;
825: }
826: }
827: }
828: nrerror("Too many iterations in brent");
829: *xmin=x;
830: return fx;
831: }
832:
833: /****************** mnbrak ***********************/
834:
835: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
836: double (*func)(double))
837: {
838: double ulim,u,r,q, dum;
839: double fu;
840:
841: *fa=(*func)(*ax);
842: *fb=(*func)(*bx);
843: if (*fb > *fa) {
844: SHFT(dum,*ax,*bx,dum)
845: SHFT(dum,*fb,*fa,dum)
846: }
847: *cx=(*bx)+GOLD*(*bx-*ax);
848: *fc=(*func)(*cx);
849: while (*fb > *fc) {
850: r=(*bx-*ax)*(*fb-*fc);
851: q=(*bx-*cx)*(*fb-*fa);
852: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
853: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r));
854: ulim=(*bx)+GLIMIT*(*cx-*bx);
855: if ((*bx-u)*(u-*cx) > 0.0) {
856: fu=(*func)(u);
857: } else if ((*cx-u)*(u-ulim) > 0.0) {
858: fu=(*func)(u);
859: if (fu < *fc) {
860: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
861: SHFT(*fb,*fc,fu,(*func)(u))
862: }
863: } else if ((u-ulim)*(ulim-*cx) >= 0.0) {
864: u=ulim;
865: fu=(*func)(u);
866: } else {
867: u=(*cx)+GOLD*(*cx-*bx);
868: fu=(*func)(u);
869: }
870: SHFT(*ax,*bx,*cx,u)
871: SHFT(*fa,*fb,*fc,fu)
872: }
873: }
874:
875: /*************** linmin ************************/
876:
877: int ncom;
878: double *pcom,*xicom;
879: double (*nrfunc)(double []);
880:
881: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
882: {
883: double brent(double ax, double bx, double cx,
884: double (*f)(double), double tol, double *xmin);
885: double f1dim(double x);
886: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
887: double *fc, double (*func)(double));
888: int j;
889: double xx,xmin,bx,ax;
890: double fx,fb,fa;
891:
892: ncom=n;
893: pcom=vector(1,n);
894: xicom=vector(1,n);
895: nrfunc=func;
896: for (j=1;j<=n;j++) {
897: pcom[j]=p[j];
898: xicom[j]=xi[j];
899: }
900: ax=0.0;
901: xx=1.0;
902: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim);
903: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin);
904: #ifdef DEBUG
905: printf("retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
906: fprintf(ficlog,"retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
907: #endif
908: for (j=1;j<=n;j++) {
909: xi[j] *= xmin;
910: p[j] += xi[j];
911: }
912: free_vector(xicom,1,n);
913: free_vector(pcom,1,n);
914: }
915:
1.91 brouard 916: char *asc_diff_time(long time_sec, char ascdiff[])
917: {
918: long sec_left, days, hours, minutes;
919: days = (time_sec) / (60*60*24);
920: sec_left = (time_sec) % (60*60*24);
921: hours = (sec_left) / (60*60) ;
922: sec_left = (sec_left) %(60*60);
923: minutes = (sec_left) /60;
924: sec_left = (sec_left) % (60);
925: sprintf(ascdiff,"%d day(s) %d hour(s) %d minute(s) %d second(s)",days, hours, minutes, sec_left);
926: return ascdiff;
927: }
928:
1.53 brouard 929: /*************** powell ************************/
930: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
931: double (*func)(double []))
932: {
933: void linmin(double p[], double xi[], int n, double *fret,
934: double (*func)(double []));
935: int i,ibig,j;
936: double del,t,*pt,*ptt,*xit;
937: double fp,fptt;
938: double *xits;
1.91 brouard 939: int niterf, itmp;
940:
1.53 brouard 941: pt=vector(1,n);
942: ptt=vector(1,n);
943: xit=vector(1,n);
944: xits=vector(1,n);
945: *fret=(*func)(p);
946: for (j=1;j<=n;j++) pt[j]=p[j];
947: for (*iter=1;;++(*iter)) {
948: fp=(*fret);
949: ibig=0;
950: del=0.0;
1.91 brouard 951: last_time=curr_time;
952: (void) gettimeofday(&curr_time,&tzp);
953: 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);
1.98 brouard 954: /* 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);
1.91 brouard 955: fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tv_sec-start_time.tv_sec);
1.98 brouard 956: */
957: for (i=1;i<=n;i++) {
1.53 brouard 958: printf(" %d %.12f",i, p[i]);
1.76 brouard 959: fprintf(ficlog," %d %.12lf",i, p[i]);
960: fprintf(ficrespow," %.12lf", p[i]);
961: }
1.53 brouard 962: printf("\n");
963: fprintf(ficlog,"\n");
1.91 brouard 964: fprintf(ficrespow,"\n");fflush(ficrespow);
965: if(*iter <=3){
966: tm = *localtime(&curr_time.tv_sec);
1.101 brouard 967: strcpy(strcurr,asctime(&tm));
1.92 brouard 968: /* asctime_r(&tm,strcurr); */
1.101 brouard 969: forecast_time=curr_time;
1.91 brouard 970: itmp = strlen(strcurr);
1.101 brouard 971: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1.91 brouard 972: strcurr[itmp-1]='\0';
973: printf("\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,curr_time.tv_sec-last_time.tv_sec);
974: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,curr_time.tv_sec-last_time.tv_sec);
975: for(niterf=10;niterf<=30;niterf+=10){
976: forecast_time.tv_sec=curr_time.tv_sec+(niterf-*iter)*(curr_time.tv_sec-last_time.tv_sec);
977: tmf = *localtime(&forecast_time.tv_sec);
1.92 brouard 978: /* asctime_r(&tmf,strfor); */
979: strcpy(strfor,asctime(&tmf));
1.91 brouard 980: itmp = strlen(strfor);
981: if(strfor[itmp-1]=='\n')
982: strfor[itmp-1]='\0';
1.101 brouard 983: 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);
984: 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);
1.91 brouard 985: }
986: }
1.53 brouard 987: for (i=1;i<=n;i++) {
988: for (j=1;j<=n;j++) xit[j]=xi[j][i];
989: fptt=(*fret);
990: #ifdef DEBUG
991: printf("fret=%lf \n",*fret);
992: fprintf(ficlog,"fret=%lf \n",*fret);
993: #endif
994: printf("%d",i);fflush(stdout);
995: fprintf(ficlog,"%d",i);fflush(ficlog);
996: linmin(p,xit,n,fret,func);
997: if (fabs(fptt-(*fret)) > del) {
998: del=fabs(fptt-(*fret));
999: ibig=i;
1000: }
1001: #ifdef DEBUG
1002: printf("%d %.12e",i,(*fret));
1003: fprintf(ficlog,"%d %.12e",i,(*fret));
1004: for (j=1;j<=n;j++) {
1005: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
1006: printf(" x(%d)=%.12e",j,xit[j]);
1007: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1008: }
1009: for(j=1;j<=n;j++) {
1010: printf(" p=%.12e",p[j]);
1011: fprintf(ficlog," p=%.12e",p[j]);
1012: }
1013: printf("\n");
1014: fprintf(ficlog,"\n");
1015: #endif
1016: }
1017: if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) {
1018: #ifdef DEBUG
1019: int k[2],l;
1020: k[0]=1;
1021: k[1]=-1;
1022: printf("Max: %.12e",(*func)(p));
1023: fprintf(ficlog,"Max: %.12e",(*func)(p));
1024: for (j=1;j<=n;j++) {
1025: printf(" %.12e",p[j]);
1026: fprintf(ficlog," %.12e",p[j]);
1027: }
1028: printf("\n");
1029: fprintf(ficlog,"\n");
1030: for(l=0;l<=1;l++) {
1031: for (j=1;j<=n;j++) {
1032: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
1033: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
1034: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
1035: }
1036: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
1037: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
1038: }
1039: #endif
1040:
1041:
1042: free_vector(xit,1,n);
1043: free_vector(xits,1,n);
1044: free_vector(ptt,1,n);
1045: free_vector(pt,1,n);
1046: return;
1047: }
1048: if (*iter == ITMAX) nrerror("powell exceeding maximum iterations.");
1049: for (j=1;j<=n;j++) {
1050: ptt[j]=2.0*p[j]-pt[j];
1051: xit[j]=p[j]-pt[j];
1052: pt[j]=p[j];
1053: }
1054: fptt=(*func)(ptt);
1055: if (fptt < fp) {
1056: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt);
1057: if (t < 0.0) {
1058: linmin(p,xit,n,fret,func);
1059: for (j=1;j<=n;j++) {
1060: xi[j][ibig]=xi[j][n];
1061: xi[j][n]=xit[j];
1062: }
1063: #ifdef DEBUG
1064: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
1065: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
1066: for(j=1;j<=n;j++){
1067: printf(" %.12e",xit[j]);
1068: fprintf(ficlog," %.12e",xit[j]);
1069: }
1070: printf("\n");
1071: fprintf(ficlog,"\n");
1072: #endif
1.54 brouard 1073: }
1.53 brouard 1074: }
1075: }
1076: }
1077:
1.117 ! brouard 1078: /**** Prevalence limit (stable or period prevalence) ****************/
1.53 brouard 1079:
1080: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int ij)
1081: {
1082: /* Computes the prevalence limit in each live state at age x by left multiplying the unit
1083: matrix by transitions matrix until convergence is reached */
1084:
1085: int i, ii,j,k;
1086: double min, max, maxmin, maxmax,sumnew=0.;
1087: double **matprod2();
1088: double **out, cov[NCOVMAX], **pmij();
1089: double **newm;
1090: double agefin, delaymax=50 ; /* Max number of years to converge */
1091:
1092: for (ii=1;ii<=nlstate+ndeath;ii++)
1093: for (j=1;j<=nlstate+ndeath;j++){
1094: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1095: }
1096:
1097: cov[1]=1.;
1098:
1099: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1100: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1101: newm=savm;
1102: /* Covariates have to be included here again */
1103: cov[2]=agefin;
1104:
1105: for (k=1; k<=cptcovn;k++) {
1106: cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];
1107: /* 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]]);*/
1108: }
1109: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
1110: for (k=1; k<=cptcovprod;k++)
1111: cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];
1112:
1113: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
1114: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
1115: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1116: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
1117:
1118: savm=oldm;
1119: oldm=newm;
1120: maxmax=0.;
1121: for(j=1;j<=nlstate;j++){
1122: min=1.;
1123: max=0.;
1124: for(i=1; i<=nlstate; i++) {
1125: sumnew=0;
1126: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
1127: prlim[i][j]= newm[i][j]/(1-sumnew);
1128: max=FMAX(max,prlim[i][j]);
1129: min=FMIN(min,prlim[i][j]);
1130: }
1131: maxmin=max-min;
1132: maxmax=FMAX(maxmax,maxmin);
1133: }
1134: if(maxmax < ftolpl){
1135: return prlim;
1136: }
1137: }
1138: }
1139:
1140: /*************** transition probabilities ***************/
1141:
1142: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1143: {
1144: double s1, s2;
1145: /*double t34;*/
1146: int i,j,j1, nc, ii, jj;
1147:
1148: for(i=1; i<= nlstate; i++){
1.99 brouard 1149: for(j=1; j<i;j++){
1150: for (nc=1, s2=0.;nc <=ncovmodel; nc++){
1151: /*s2 += param[i][j][nc]*cov[nc];*/
1152: s2 += x[(i-1)*nlstate*ncovmodel+(j-1)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];
1153: /* printf("Int j<i s1=%.17e, s2=%.17e\n",s1,s2); */
1154: }
1155: ps[i][j]=s2;
1156: /* printf("s1=%.17e, s2=%.17e\n",s1,s2); */
1157: }
1158: for(j=i+1; j<=nlstate+ndeath;j++){
1159: for (nc=1, s2=0.;nc <=ncovmodel; nc++){
1160: s2 += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];
1161: /* printf("Int j>i s1=%.17e, s2=%.17e %lx %lx\n",s1,s2,s1,s2); */
1162: }
1163: ps[i][j]=s2;
1.53 brouard 1164: }
1165: }
1166: /*ps[3][2]=1;*/
1.99 brouard 1167:
1168: for(i=1; i<= nlstate; i++){
1169: s1=0;
1170: for(j=1; j<i; j++)
1171: s1+=exp(ps[i][j]);
1172: for(j=i+1; j<=nlstate+ndeath; j++)
1173: s1+=exp(ps[i][j]);
1174: ps[i][i]=1./(s1+1.);
1175: for(j=1; j<i; j++)
1176: ps[i][j]= exp(ps[i][j])*ps[i][i];
1177: for(j=i+1; j<=nlstate+ndeath; j++)
1178: ps[i][j]= exp(ps[i][j])*ps[i][i];
1179: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
1180: } /* end i */
1181:
1182: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
1183: for(jj=1; jj<= nlstate+ndeath; jj++){
1184: ps[ii][jj]=0;
1185: ps[ii][ii]=1;
1186: }
1.53 brouard 1187: }
1.99 brouard 1188:
1.53 brouard 1189:
1.99 brouard 1190: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
1191: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
1192: /* printf("ddd %lf ",ps[ii][jj]); */
1193: /* } */
1194: /* printf("\n "); */
1195: /* } */
1196: /* printf("\n ");printf("%lf ",cov[2]); */
1197: /*
1198: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1199: goto end;*/
1.53 brouard 1200: return ps;
1201: }
1202:
1203: /**************** Product of 2 matrices ******************/
1204:
1205: double **matprod2(double **out, double **in,long nrl, long nrh, long ncl, long nch, long ncolol, long ncoloh, double **b)
1206: {
1207: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
1208: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
1209: /* in, b, out are matrice of pointers which should have been initialized
1210: before: only the contents of out is modified. The function returns
1211: a pointer to pointers identical to out */
1212: long i, j, k;
1213: for(i=nrl; i<= nrh; i++)
1214: for(k=ncolol; k<=ncoloh; k++)
1215: for(j=ncl,out[i][k]=0.; j<=nch; j++)
1216: out[i][k] +=in[i][j]*b[j][k];
1217:
1218: return out;
1219: }
1220:
1221:
1222: /************* Higher Matrix Product ***************/
1223:
1224: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij )
1225: {
1.66 brouard 1226: /* Computes the transition matrix starting at age 'age' over
1227: 'nhstepm*hstepm*stepm' months (i.e. until
1228: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1229: nhstepm*hstepm matrices.
1.53 brouard 1230: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
1.66 brouard 1231: (typically every 2 years instead of every month which is too big
1232: for the memory).
1.53 brouard 1233: Model is determined by parameters x and covariates have to be
1234: included manually here.
1235:
1236: */
1237:
1238: int i, j, d, h, k;
1239: double **out, cov[NCOVMAX];
1240: double **newm;
1241:
1242: /* Hstepm could be zero and should return the unit matrix */
1243: for (i=1;i<=nlstate+ndeath;i++)
1244: for (j=1;j<=nlstate+ndeath;j++){
1245: oldm[i][j]=(i==j ? 1.0 : 0.0);
1246: po[i][j][0]=(i==j ? 1.0 : 0.0);
1247: }
1248: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1249: for(h=1; h <=nhstepm; h++){
1250: for(d=1; d <=hstepm; d++){
1251: newm=savm;
1252: /* Covariates have to be included here again */
1253: cov[1]=1.;
1254: cov[2]=age+((h-1)*hstepm + (d-1))*stepm/YEARM;
1255: for (k=1; k<=cptcovn;k++) cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];
1256: for (k=1; k<=cptcovage;k++)
1257: cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
1258: for (k=1; k<=cptcovprod;k++)
1259: cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];
1260:
1261:
1262: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
1263: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1264: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
1265: pmij(pmmij,cov,ncovmodel,x,nlstate));
1266: savm=oldm;
1267: oldm=newm;
1268: }
1269: for(i=1; i<=nlstate+ndeath; i++)
1270: for(j=1;j<=nlstate+ndeath;j++) {
1271: po[i][j][h]=newm[i][j];
1272: /*printf("i=%d j=%d h=%d po[i][j][h]=%f ",i,j,h,po[i][j][h]);
1273: */
1274: }
1275: } /* end h */
1276: return po;
1277: }
1278:
1279:
1280: /*************** log-likelihood *************/
1281: double func( double *x)
1282: {
1283: int i, ii, j, k, mi, d, kk;
1284: double l, ll[NLSTATEMAX], cov[NCOVMAX];
1285: double **out;
1286: double sw; /* Sum of weights */
1287: double lli; /* Individual log likelihood */
1.59 brouard 1288: int s1, s2;
1.68 lievre 1289: double bbh, survp;
1.53 brouard 1290: long ipmx;
1291: /*extern weight */
1292: /* We are differentiating ll according to initial status */
1293: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
1294: /*for(i=1;i<imx;i++)
1295: printf(" %d\n",s[4][i]);
1296: */
1297: cov[1]=1.;
1298:
1299: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.61 brouard 1300:
1301: if(mle==1){
1302: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1303: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1304: for(mi=1; mi<= wav[i]-1; mi++){
1305: for (ii=1;ii<=nlstate+ndeath;ii++)
1306: for (j=1;j<=nlstate+ndeath;j++){
1307: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1308: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1309: }
1310: for(d=0; d<dh[mi][i]; d++){
1311: newm=savm;
1312: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1313: for (kk=1; kk<=cptcovage;kk++) {
1314: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1315: }
1316: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
1317: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1318: savm=oldm;
1319: oldm=newm;
1320: } /* end mult */
1.53 brouard 1321:
1.61 brouard 1322: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
1.101 brouard 1323: /* But now since version 0.9 we anticipate for bias at large stepm.
1.61 brouard 1324: * If stepm is larger than one month (smallest stepm) and if the exact delay
1325: * (in months) between two waves is not a multiple of stepm, we rounded to
1326: * the nearest (and in case of equal distance, to the lowest) interval but now
1327: * we keep into memory the bias bh[mi][i] and also the previous matrix product
1.101 brouard 1328: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
1.61 brouard 1329: * probability in order to take into account the bias as a fraction of the way
1.101 brouard 1330: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
1.61 brouard 1331: * -stepm/2 to stepm/2 .
1332: * For stepm=1 the results are the same as for previous versions of Imach.
1333: * For stepm > 1 the results are less biased than in previous versions.
1334: */
1335: s1=s[mw[mi][i]][i];
1336: s2=s[mw[mi+1][i]][i];
1.64 lievre 1337: bbh=(double)bh[mi][i]/(double)stepm;
1.101 brouard 1338: /* bias bh is positive if real duration
1.64 lievre 1339: * is higher than the multiple of stepm and negative otherwise.
1340: */
1341: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
1.71 brouard 1342: if( s2 > nlstate){
1.104 lievre 1343: /* i.e. if s2 is a death state and if the date of death is known
1344: then the contribution to the likelihood is the probability to
1345: die between last step unit time and current step unit time,
1346: which is also equal to probability to die before dh
1.101 brouard 1347: minus probability to die before dh-stepm .
1.71 brouard 1348: In version up to 0.92 likelihood was computed
1349: as if date of death was unknown. Death was treated as any other
1350: health state: the date of the interview describes the actual state
1351: and not the date of a change in health state. The former idea was
1352: to consider that at each interview the state was recorded
1353: (healthy, disable or death) and IMaCh was corrected; but when we
1354: introduced the exact date of death then we should have modified
1355: the contribution of an exact death to the likelihood. This new
1356: contribution is smaller and very dependent of the step unit
1357: stepm. It is no more the probability to die between last interview
1358: and month of death but the probability to survive from last
1359: interview up to one month before death multiplied by the
1360: probability to die within a month. Thanks to Chris
1361: Jackson for correcting this bug. Former versions increased
1362: mortality artificially. The bad side is that we add another loop
1363: which slows down the processing. The difference can be up to 10%
1364: lower mortality.
1365: */
1366: lli=log(out[s1][s2] - savm[s1][s2]);
1.104 lievre 1367:
1368:
1369: } else if (s2==-2) {
1370: for (j=1,survp=0. ; j<=nlstate; j++)
1371: survp += out[s1][j];
1372: lli= survp;
1373: }
1.105 lievre 1374:
1375: else if (s2==-4) {
1376: for (j=3,survp=0. ; j<=nlstate; j++)
1377: survp += out[s1][j];
1378: lli= survp;
1379: }
1380:
1381: else if (s2==-5) {
1382: for (j=1,survp=0. ; j<=2; j++)
1383: survp += out[s1][j];
1384: lli= survp;
1385: }
1.104 lievre 1386:
1387:
1388: else{
1.71 brouard 1389: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1390: /* 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 */
1391: }
1.64 lievre 1392: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
1393: /*if(lli ==000.0)*/
1394: /*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); */
1.71 brouard 1395: ipmx +=1;
1.64 lievre 1396: sw += weight[i];
1397: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1398: } /* end of wave */
1399: } /* end of individual */
1400: } else if(mle==2){
1401: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1402: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1403: for(mi=1; mi<= wav[i]-1; mi++){
1404: for (ii=1;ii<=nlstate+ndeath;ii++)
1405: for (j=1;j<=nlstate+ndeath;j++){
1406: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1407: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1408: }
1409: for(d=0; d<=dh[mi][i]; d++){
1410: newm=savm;
1411: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1412: for (kk=1; kk<=cptcovage;kk++) {
1413: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1414: }
1415: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
1416: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1417: savm=oldm;
1418: oldm=newm;
1419: } /* end mult */
1420:
1421: s1=s[mw[mi][i]][i];
1422: s2=s[mw[mi+1][i]][i];
1423: bbh=(double)bh[mi][i]/(double)stepm;
1.63 lievre 1424: 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 */
1.64 lievre 1425: ipmx +=1;
1426: sw += weight[i];
1427: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1428: } /* end of wave */
1429: } /* end of individual */
1430: } else if(mle==3){ /* exponential inter-extrapolation */
1431: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1432: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1433: for(mi=1; mi<= wav[i]-1; mi++){
1434: for (ii=1;ii<=nlstate+ndeath;ii++)
1435: for (j=1;j<=nlstate+ndeath;j++){
1436: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1437: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1438: }
1439: for(d=0; d<dh[mi][i]; d++){
1440: newm=savm;
1441: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1442: for (kk=1; kk<=cptcovage;kk++) {
1443: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1444: }
1445: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
1446: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1447: savm=oldm;
1448: oldm=newm;
1449: } /* end mult */
1450:
1451: s1=s[mw[mi][i]][i];
1452: s2=s[mw[mi+1][i]][i];
1453: bbh=(double)bh[mi][i]/(double)stepm;
1454: 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 */
1.61 brouard 1455: ipmx +=1;
1456: sw += weight[i];
1457: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1458: } /* end of wave */
1459: } /* end of individual */
1.84 brouard 1460: }else if (mle==4){ /* ml=4 no inter-extrapolation */
1.61 brouard 1461: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1462: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1463: for(mi=1; mi<= wav[i]-1; mi++){
1464: for (ii=1;ii<=nlstate+ndeath;ii++)
1465: for (j=1;j<=nlstate+ndeath;j++){
1466: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1467: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1468: }
1469: for(d=0; d<dh[mi][i]; d++){
1470: newm=savm;
1471: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1472: for (kk=1; kk<=cptcovage;kk++) {
1473: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1474: }
1475:
1476: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
1477: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1478: savm=oldm;
1479: oldm=newm;
1480: } /* end mult */
1481:
1.84 brouard 1482: s1=s[mw[mi][i]][i];
1483: s2=s[mw[mi+1][i]][i];
1484: if( s2 > nlstate){
1485: lli=log(out[s1][s2] - savm[s1][s2]);
1486: }else{
1487: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
1488: }
1489: ipmx +=1;
1490: sw += weight[i];
1491: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.85 brouard 1492: /* 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]); */
1.84 brouard 1493: } /* end of wave */
1494: } /* end of individual */
1495: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
1496: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1497: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1498: for(mi=1; mi<= wav[i]-1; mi++){
1499: for (ii=1;ii<=nlstate+ndeath;ii++)
1500: for (j=1;j<=nlstate+ndeath;j++){
1501: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1502: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1503: }
1504: for(d=0; d<dh[mi][i]; d++){
1505: newm=savm;
1506: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1507: for (kk=1; kk<=cptcovage;kk++) {
1508: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1509: }
1510:
1511: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
1512: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1513: savm=oldm;
1514: oldm=newm;
1515: } /* end mult */
1516:
1517: s1=s[mw[mi][i]][i];
1518: s2=s[mw[mi+1][i]][i];
1.61 brouard 1519: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
1520: ipmx +=1;
1521: sw += weight[i];
1522: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.84 brouard 1523: /*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]);*/
1.61 brouard 1524: } /* end of wave */
1525: } /* end of individual */
1526: } /* End of if */
1.53 brouard 1527: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
1528: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
1529: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
1.85 brouard 1530: return -l;
1531: }
1532:
1533: /*************** log-likelihood *************/
1534: double funcone( double *x)
1535: {
1.87 brouard 1536: /* Same as likeli but slower because of a lot of printf and if */
1.85 brouard 1537: int i, ii, j, k, mi, d, kk;
1538: double l, ll[NLSTATEMAX], cov[NCOVMAX];
1539: double **out;
1540: double lli; /* Individual log likelihood */
1.87 brouard 1541: double llt;
1.85 brouard 1542: int s1, s2;
1543: double bbh, survp;
1544: /*extern weight */
1545: /* We are differentiating ll according to initial status */
1546: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
1547: /*for(i=1;i<imx;i++)
1548: printf(" %d\n",s[4][i]);
1549: */
1550: cov[1]=1.;
1551:
1552: for(k=1; k<=nlstate; k++) ll[k]=0.;
1553:
1554: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1555: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1556: for(mi=1; mi<= wav[i]-1; mi++){
1557: for (ii=1;ii<=nlstate+ndeath;ii++)
1558: for (j=1;j<=nlstate+ndeath;j++){
1559: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1560: savm[ii][j]=(ii==j ? 1.0 : 0.0);
1561: }
1562: for(d=0; d<dh[mi][i]; d++){
1563: newm=savm;
1564: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
1565: for (kk=1; kk<=cptcovage;kk++) {
1566: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
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: bbh=(double)bh[mi][i]/(double)stepm;
1577: /* bias is positive if real duration
1578: * is higher than the multiple of stepm and negative otherwise.
1579: */
1580: if( s2 > nlstate && (mle <5) ){ /* Jackson */
1581: lli=log(out[s1][s2] - savm[s1][s2]);
1582: } else if (mle==1){
1583: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1584: } else if(mle==2){
1585: 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 */
1586: } else if(mle==3){ /* exponential inter-extrapolation */
1587: 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 */
1588: } else if (mle==4){ /* mle=4 no inter-extrapolation */
1589: lli=log(out[s1][s2]); /* Original formula */
1590: } else{ /* ml>=5 no inter-extrapolation no jackson =0.8a */
1591: lli=log(out[s1][s2]); /* Original formula */
1592: } /* End of if */
1593: ipmx +=1;
1594: sw += weight[i];
1595: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1596: /* 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]); */
1597: if(globpr){
1.88 brouard 1598: fprintf(ficresilk,"%9d %6d %1d %1d %1d %1d %3d %10.6f %6.4f\
1.86 brouard 1599: %10.6f %10.6f %10.6f ", \
1600: num[i],i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],
1601: 2*weight[i]*lli,out[s1][s2],savm[s1][s2]);
1.87 brouard 1602: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
1603: llt +=ll[k]*gipmx/gsw;
1604: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
1605: }
1606: fprintf(ficresilk," %10.6f\n", -llt);
1.85 brouard 1607: }
1608: } /* end of wave */
1609: } /* end of individual */
1610: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
1611: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
1612: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
1.87 brouard 1613: if(globpr==0){ /* First time we count the contributions and weights */
1614: gipmx=ipmx;
1615: gsw=sw;
1616: }
1.53 brouard 1617: return -l;
1618: }
1619:
1620:
1.94 brouard 1621: /*************** function likelione ***********/
1.87 brouard 1622: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*funcone)(double []))
1.85 brouard 1623: {
1.87 brouard 1624: /* This routine should help understanding what is done with
1625: the selection of individuals/waves and
1.85 brouard 1626: to check the exact contribution to the likelihood.
1627: Plotting could be done.
1628: */
1629: int k;
1.87 brouard 1630:
1.88 brouard 1631: if(*globpri !=0){ /* Just counts and sums, no printings */
1.85 brouard 1632: strcpy(fileresilk,"ilk");
1633: strcat(fileresilk,fileres);
1634: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
1635: printf("Problem with resultfile: %s\n", fileresilk);
1636: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
1637: }
1.87 brouard 1638: 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");
1.88 brouard 1639: fprintf(ficresilk, "#num_i i s1 s2 mi mw dh likeli weight 2wlli out sav ");
1.85 brouard 1640: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
1641: for(k=1; k<=nlstate; k++)
1.87 brouard 1642: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
1643: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
1.85 brouard 1644: }
1645:
1646: *fretone=(*funcone)(p);
1.87 brouard 1647: if(*globpri !=0){
1.85 brouard 1648: fclose(ficresilk);
1.88 brouard 1649: fprintf(fichtm,"\n<br>File of contributions to the likelihood: <a href=\"%s\">%s</a><br>\n",subdirf(fileresilk),subdirf(fileresilk));
1.87 brouard 1650: fflush(fichtm);
1651: }
1.85 brouard 1652: return;
1653: }
1654:
1.88 brouard 1655:
1.53 brouard 1656: /*********** Maximum Likelihood Estimation ***************/
1657:
1658: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
1659: {
1660: int i,j, iter;
1.74 brouard 1661: double **xi;
1.53 brouard 1662: double fret;
1.85 brouard 1663: double fretone; /* Only one call to likelihood */
1.98 brouard 1664: /* char filerespow[FILENAMELENGTH];*/
1.53 brouard 1665: xi=matrix(1,npar,1,npar);
1666: for (i=1;i<=npar;i++)
1667: for (j=1;j<=npar;j++)
1668: xi[i][j]=(i==j ? 1.0 : 0.0);
1669: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.76 brouard 1670: strcpy(filerespow,"pow");
1671: strcat(filerespow,fileres);
1672: if((ficrespow=fopen(filerespow,"w"))==NULL) {
1673: printf("Problem with resultfile: %s\n", filerespow);
1674: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
1675: }
1676: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1677: for (i=1;i<=nlstate;i++)
1678: for(j=1;j<=nlstate+ndeath;j++)
1679: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
1680: fprintf(ficrespow,"\n");
1.85 brouard 1681:
1.53 brouard 1682: powell(p,xi,npar,ftol,&iter,&fret,func);
1683:
1.115 brouard 1684: free_matrix(xi,1,npar,1,npar);
1.76 brouard 1685: fclose(ficrespow);
1686: printf("\n#Number of iterations = %d, -2 Log likelihood = %.12f\n",iter,func(p));
1.65 lievre 1687: fprintf(ficlog,"\n#Number of iterations = %d, -2 Log likelihood = %.12f \n",iter,func(p));
1.53 brouard 1688: fprintf(ficres,"#Number of iterations = %d, -2 Log likelihood = %.12f \n",iter,func(p));
1689:
1690: }
1691:
1692: /**** Computes Hessian and covariance matrix ***/
1693: void hesscov(double **matcov, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1694: {
1695: double **a,**y,*x,pd;
1696: double **hess;
1697: int i, j,jk;
1698: int *indx;
1699:
1.98 brouard 1700: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1701: double hessij(double p[], double delti[], int i, int j,double (*func)(double []),int npar);
1.53 brouard 1702: void lubksb(double **a, int npar, int *indx, double b[]) ;
1703: void ludcmp(double **a, int npar, int *indx, double *d) ;
1.98 brouard 1704: double gompertz(double p[]);
1.53 brouard 1705: hess=matrix(1,npar,1,npar);
1706:
1707: printf("\nCalculation of the hessian matrix. Wait...\n");
1708: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
1709: for (i=1;i<=npar;i++){
1710: printf("%d",i);fflush(stdout);
1711: fprintf(ficlog,"%d",i);fflush(ficlog);
1.98 brouard 1712:
1713: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
1714:
1715: /* printf(" %f ",p[i]);
1716: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
1.53 brouard 1717: }
1718:
1719: for (i=1;i<=npar;i++) {
1720: for (j=1;j<=npar;j++) {
1721: if (j>i) {
1722: printf(".%d%d",i,j);fflush(stdout);
1723: fprintf(ficlog,".%d%d",i,j);fflush(ficlog);
1.98 brouard 1724: hess[i][j]=hessij(p,delti,i,j,func,npar);
1725:
1.53 brouard 1726: hess[j][i]=hess[i][j];
1727: /*printf(" %lf ",hess[i][j]);*/
1728: }
1729: }
1730: }
1731: printf("\n");
1732: fprintf(ficlog,"\n");
1733:
1734: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
1735: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
1736:
1737: a=matrix(1,npar,1,npar);
1738: y=matrix(1,npar,1,npar);
1739: x=vector(1,npar);
1740: indx=ivector(1,npar);
1741: for (i=1;i<=npar;i++)
1742: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
1743: ludcmp(a,npar,indx,&pd);
1744:
1745: for (j=1;j<=npar;j++) {
1746: for (i=1;i<=npar;i++) x[i]=0;
1747: x[j]=1;
1748: lubksb(a,npar,indx,x);
1749: for (i=1;i<=npar;i++){
1750: matcov[i][j]=x[i];
1751: }
1752: }
1753:
1754: printf("\n#Hessian matrix#\n");
1755: fprintf(ficlog,"\n#Hessian matrix#\n");
1756: for (i=1;i<=npar;i++) {
1757: for (j=1;j<=npar;j++) {
1758: printf("%.3e ",hess[i][j]);
1759: fprintf(ficlog,"%.3e ",hess[i][j]);
1760: }
1761: printf("\n");
1762: fprintf(ficlog,"\n");
1763: }
1764:
1765: /* Recompute Inverse */
1766: for (i=1;i<=npar;i++)
1767: for (j=1;j<=npar;j++) a[i][j]=matcov[i][j];
1768: ludcmp(a,npar,indx,&pd);
1769:
1770: /* printf("\n#Hessian matrix recomputed#\n");
1771:
1772: for (j=1;j<=npar;j++) {
1773: for (i=1;i<=npar;i++) x[i]=0;
1774: x[j]=1;
1775: lubksb(a,npar,indx,x);
1776: for (i=1;i<=npar;i++){
1777: y[i][j]=x[i];
1778: printf("%.3e ",y[i][j]);
1779: fprintf(ficlog,"%.3e ",y[i][j]);
1780: }
1781: printf("\n");
1782: fprintf(ficlog,"\n");
1783: }
1784: */
1785:
1786: free_matrix(a,1,npar,1,npar);
1787: free_matrix(y,1,npar,1,npar);
1788: free_vector(x,1,npar);
1789: free_ivector(indx,1,npar);
1790: free_matrix(hess,1,npar,1,npar);
1791:
1792:
1793: }
1794:
1795: /*************** hessian matrix ****************/
1.98 brouard 1796: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.53 brouard 1797: {
1798: int i;
1799: int l=1, lmax=20;
1800: double k1,k2;
1801: double p2[NPARMAX+1];
1802: double res;
1.98 brouard 1803: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
1.53 brouard 1804: double fx;
1805: int k=0,kmax=10;
1806: double l1;
1807:
1808: fx=func(x);
1809: for (i=1;i<=npar;i++) p2[i]=x[i];
1810: for(l=0 ; l <=lmax; l++){
1811: l1=pow(10,l);
1812: delts=delt;
1813: for(k=1 ; k <kmax; k=k+1){
1814: delt = delta*(l1*k);
1815: p2[theta]=x[theta] +delt;
1816: k1=func(p2)-fx;
1817: p2[theta]=x[theta]-delt;
1818: k2=func(p2)-fx;
1819: /*res= (k1-2.0*fx+k2)/delt/delt; */
1820: res= (k1+k2)/delt/delt/2.; /* Divided by because L and not 2*L */
1821:
1822: #ifdef DEBUG
1823: 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);
1824: 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);
1825: #endif
1826: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
1827: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
1828: k=kmax;
1829: }
1830: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1831: k=kmax; l=lmax*10.;
1832: }
1833: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
1834: delts=delt;
1835: }
1836: }
1837: }
1838: delti[theta]=delts;
1839: return res;
1840:
1841: }
1842:
1.98 brouard 1843: double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.53 brouard 1844: {
1845: int i;
1846: int l=1, l1, lmax=20;
1847: double k1,k2,k3,k4,res,fx;
1848: double p2[NPARMAX+1];
1849: int k;
1850:
1851: fx=func(x);
1852: for (k=1; k<=2; k++) {
1853: for (i=1;i<=npar;i++) p2[i]=x[i];
1854: p2[thetai]=x[thetai]+delti[thetai]/k;
1855: p2[thetaj]=x[thetaj]+delti[thetaj]/k;
1856: k1=func(p2)-fx;
1857:
1858: p2[thetai]=x[thetai]+delti[thetai]/k;
1859: p2[thetaj]=x[thetaj]-delti[thetaj]/k;
1860: k2=func(p2)-fx;
1861:
1862: p2[thetai]=x[thetai]-delti[thetai]/k;
1863: p2[thetaj]=x[thetaj]+delti[thetaj]/k;
1864: k3=func(p2)-fx;
1865:
1866: p2[thetai]=x[thetai]-delti[thetai]/k;
1867: p2[thetaj]=x[thetaj]-delti[thetaj]/k;
1868: k4=func(p2)-fx;
1869: res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /* Because of L not 2*L */
1870: #ifdef DEBUG
1871: 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);
1872: 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);
1873: #endif
1874: }
1875: return res;
1876: }
1877:
1878: /************** Inverse of matrix **************/
1879: void ludcmp(double **a, int n, int *indx, double *d)
1880: {
1881: int i,imax,j,k;
1882: double big,dum,sum,temp;
1883: double *vv;
1884:
1885: vv=vector(1,n);
1886: *d=1.0;
1887: for (i=1;i<=n;i++) {
1888: big=0.0;
1889: for (j=1;j<=n;j++)
1890: if ((temp=fabs(a[i][j])) > big) big=temp;
1891: if (big == 0.0) nrerror("Singular matrix in routine ludcmp");
1892: vv[i]=1.0/big;
1893: }
1894: for (j=1;j<=n;j++) {
1895: for (i=1;i<j;i++) {
1896: sum=a[i][j];
1897: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
1898: a[i][j]=sum;
1899: }
1900: big=0.0;
1901: for (i=j;i<=n;i++) {
1902: sum=a[i][j];
1903: for (k=1;k<j;k++)
1904: sum -= a[i][k]*a[k][j];
1905: a[i][j]=sum;
1906: if ( (dum=vv[i]*fabs(sum)) >= big) {
1907: big=dum;
1908: imax=i;
1909: }
1910: }
1911: if (j != imax) {
1912: for (k=1;k<=n;k++) {
1913: dum=a[imax][k];
1914: a[imax][k]=a[j][k];
1915: a[j][k]=dum;
1916: }
1917: *d = -(*d);
1918: vv[imax]=vv[j];
1919: }
1920: indx[j]=imax;
1921: if (a[j][j] == 0.0) a[j][j]=TINY;
1922: if (j != n) {
1923: dum=1.0/(a[j][j]);
1924: for (i=j+1;i<=n;i++) a[i][j] *= dum;
1925: }
1926: }
1927: free_vector(vv,1,n); /* Doesn't work */
1928: ;
1929: }
1930:
1931: void lubksb(double **a, int n, int *indx, double b[])
1932: {
1933: int i,ii=0,ip,j;
1934: double sum;
1935:
1936: for (i=1;i<=n;i++) {
1937: ip=indx[i];
1938: sum=b[ip];
1939: b[ip]=b[i];
1940: if (ii)
1941: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
1942: else if (sum) ii=i;
1943: b[i]=sum;
1944: }
1945: for (i=n;i>=1;i--) {
1946: sum=b[i];
1947: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
1948: b[i]=sum/a[i][i];
1949: }
1950: }
1951:
1.117 ! brouard 1952: void pstamp(FILE *fichier)
! 1953: {
! 1954: fprintf(fichier,"# %s.%s\n#%s\n#%s\n# %s", optionfilefiname,optionfilext,version,fullversion,strstart);
! 1955: }
! 1956:
1.53 brouard 1957: /************ Frequencies ********************/
1.105 lievre 1958: 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[])
1.53 brouard 1959: { /* Some frequencies */
1960:
1961: int i, m, jk, k1,i1, j1, bool, z1,z2,j;
1962: int first;
1963: double ***freq; /* Frequencies */
1.73 lievre 1964: double *pp, **prop;
1965: double pos,posprop, k2, dateintsum=0,k2cpt=0;
1.53 brouard 1966: char fileresp[FILENAMELENGTH];
1967:
1968: pp=vector(1,nlstate);
1.74 brouard 1969: prop=matrix(1,nlstate,iagemin,iagemax+3);
1.53 brouard 1970: strcpy(fileresp,"p");
1971: strcat(fileresp,fileres);
1972: if((ficresp=fopen(fileresp,"w"))==NULL) {
1973: printf("Problem with prevalence resultfile: %s\n", fileresp);
1974: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
1975: exit(0);
1976: }
1.105 lievre 1977: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin,iagemax+3);
1.53 brouard 1978: j1=0;
1979:
1980: j=cptcoveff;
1981: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1982:
1983: first=1;
1984:
1985: for(k1=1; k1<=j;k1++){
1986: for(i1=1; i1<=ncodemax[k1];i1++){
1987: j1++;
1988: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
1989: scanf("%d", i);*/
1.105 lievre 1990: for (i=-5; i<=nlstate+ndeath; i++)
1991: for (jk=-5; jk<=nlstate+ndeath; jk++)
1.74 brouard 1992: for(m=iagemin; m <= iagemax+3; m++)
1.53 brouard 1993: freq[i][jk][m]=0;
1.73 lievre 1994:
1995: for (i=1; i<=nlstate; i++)
1.74 brouard 1996: for(m=iagemin; m <= iagemax+3; m++)
1.73 lievre 1997: prop[i][m]=0;
1.53 brouard 1998:
1999: dateintsum=0;
2000: k2cpt=0;
2001: for (i=1; i<=imx; i++) {
2002: bool=1;
2003: if (cptcovn>0) {
2004: for (z1=1; z1<=cptcoveff; z1++)
2005: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtab[j1][z1]])
2006: bool=0;
2007: }
1.58 lievre 2008: if (bool==1){
1.53 brouard 2009: for(m=firstpass; m<=lastpass; m++){
2010: k2=anint[m][i]+(mint[m][i]/12.);
1.84 brouard 2011: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
1.74 brouard 2012: if(agev[m][i]==0) agev[m][i]=iagemax+1;
2013: if(agev[m][i]==1) agev[m][i]=iagemax+2;
1.73 lievre 2014: if (s[m][i]>0 && s[m][i]<=nlstate) prop[s[m][i]][(int)agev[m][i]] += weight[i];
1.53 brouard 2015: if (m<lastpass) {
2016: freq[s[m][i]][s[m+1][i]][(int)agev[m][i]] += weight[i];
1.74 brouard 2017: freq[s[m][i]][s[m+1][i]][iagemax+3] += weight[i];
1.53 brouard 2018: }
2019:
1.74 brouard 2020: if ((agev[m][i]>1) && (agev[m][i]< (iagemax+3))) {
1.53 brouard 2021: dateintsum=dateintsum+k2;
2022: k2cpt++;
2023: }
1.84 brouard 2024: /*}*/
1.53 brouard 2025: }
2026: }
2027: }
2028:
1.84 brouard 2029: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
1.117 ! brouard 2030: pstamp(ficresp);
1.53 brouard 2031: if (cptcovn>0) {
2032: fprintf(ficresp, "\n#********** Variable ");
2033: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
2034: fprintf(ficresp, "**********\n#");
2035: }
2036: for(i=1; i<=nlstate;i++)
2037: fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
2038: fprintf(ficresp, "\n");
2039:
1.74 brouard 2040: for(i=iagemin; i <= iagemax+3; i++){
2041: if(i==iagemax+3){
1.53 brouard 2042: fprintf(ficlog,"Total");
2043: }else{
2044: if(first==1){
2045: first=0;
2046: printf("See log file for details...\n");
2047: }
2048: fprintf(ficlog,"Age %d", i);
2049: }
2050: for(jk=1; jk <=nlstate ; jk++){
2051: for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
2052: pp[jk] += freq[jk][m][i];
2053: }
2054: for(jk=1; jk <=nlstate ; jk++){
2055: for(m=-1, pos=0; m <=0 ; m++)
2056: pos += freq[jk][m][i];
2057: if(pp[jk]>=1.e-10){
2058: if(first==1){
2059: printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
2060: }
2061: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
2062: }else{
2063: if(first==1)
2064: printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
2065: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
2066: }
2067: }
2068:
2069: for(jk=1; jk <=nlstate ; jk++){
2070: for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)
2071: pp[jk] += freq[jk][m][i];
1.73 lievre 2072: }
2073: for(jk=1,pos=0,posprop=0; jk <=nlstate ; jk++){
2074: pos += pp[jk];
2075: posprop += prop[jk][i];
1.53 brouard 2076: }
2077: for(jk=1; jk <=nlstate ; jk++){
2078: if(pos>=1.e-5){
2079: if(first==1)
2080: printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
2081: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
2082: }else{
2083: if(first==1)
2084: printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
2085: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
2086: }
1.74 brouard 2087: if( i <= iagemax){
1.53 brouard 2088: if(pos>=1.e-5){
1.73 lievre 2089: fprintf(ficresp," %d %.5f %.0f %.0f",i,prop[jk][i]/posprop, prop[jk][i],posprop);
1.84 brouard 2090: /*probs[i][jk][j1]= pp[jk]/pos;*/
1.53 brouard 2091: /*printf("\ni=%d jk=%d j1=%d %.5f %.0f %.0f %f",i,jk,j1,pp[jk]/pos, pp[jk],pos,probs[i][jk][j1]);*/
2092: }
2093: else
1.73 lievre 2094: fprintf(ficresp," %d NaNq %.0f %.0f",i,prop[jk][i],posprop);
1.53 brouard 2095: }
2096: }
2097:
1.69 brouard 2098: for(jk=-1; jk <=nlstate+ndeath; jk++)
2099: for(m=-1; m <=nlstate+ndeath; m++)
1.53 brouard 2100: if(freq[jk][m][i] !=0 ) {
2101: if(first==1)
2102: printf(" %d%d=%.0f",jk,m,freq[jk][m][i]);
2103: fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][i]);
2104: }
1.74 brouard 2105: if(i <= iagemax)
1.53 brouard 2106: fprintf(ficresp,"\n");
2107: if(first==1)
2108: printf("Others in log...\n");
2109: fprintf(ficlog,"\n");
2110: }
2111: }
2112: }
2113: dateintmean=dateintsum/k2cpt;
2114:
2115: fclose(ficresp);
1.105 lievre 2116: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin, iagemax+3);
1.53 brouard 2117: free_vector(pp,1,nlstate);
1.74 brouard 2118: free_matrix(prop,1,nlstate,iagemin, iagemax+3);
1.53 brouard 2119: /* End of Freq */
2120: }
2121:
2122: /************ Prevalence ********************/
1.84 brouard 2123: 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)
1.69 brouard 2124: {
2125: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
2126: in each health status at the date of interview (if between dateprev1 and dateprev2).
2127: We still use firstpass and lastpass as another selection.
2128: */
1.53 brouard 2129:
2130: int i, m, jk, k1, i1, j1, bool, z1,z2,j;
2131: double ***freq; /* Frequencies */
1.73 lievre 2132: double *pp, **prop;
2133: double pos,posprop;
1.69 brouard 2134: double y2; /* in fractional years */
1.74 brouard 2135: int iagemin, iagemax;
1.53 brouard 2136:
1.74 brouard 2137: iagemin= (int) agemin;
2138: iagemax= (int) agemax;
2139: /*pp=vector(1,nlstate);*/
2140: prop=matrix(1,nlstate,iagemin,iagemax+3);
2141: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
1.53 brouard 2142: j1=0;
2143:
2144: j=cptcoveff;
2145: if (cptcovn<1) {j=1;ncodemax[1]=1;}
2146:
2147: for(k1=1; k1<=j;k1++){
2148: for(i1=1; i1<=ncodemax[k1];i1++){
2149: j1++;
2150:
1.73 lievre 2151: for (i=1; i<=nlstate; i++)
1.74 brouard 2152: for(m=iagemin; m <= iagemax+3; m++)
2153: prop[i][m]=0.0;
1.53 brouard 2154:
1.69 brouard 2155: for (i=1; i<=imx; i++) { /* Each individual */
1.53 brouard 2156: bool=1;
2157: if (cptcovn>0) {
2158: for (z1=1; z1<=cptcoveff; z1++)
2159: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtab[j1][z1]])
2160: bool=0;
2161: }
2162: if (bool==1) {
1.69 brouard 2163: for(m=firstpass; m<=lastpass; m++){/* Other selection (we can limit to certain interviews*/
2164: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
2165: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
1.74 brouard 2166: if(agev[m][i]==0) agev[m][i]=iagemax+1;
2167: if(agev[m][i]==1) agev[m][i]=iagemax+2;
2168: 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);
2169: if (s[m][i]>0 && s[m][i]<=nlstate) {
2170: /*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]]);*/
2171: prop[s[m][i]][(int)agev[m][i]] += weight[i];
2172: prop[s[m][i]][iagemax+3] += weight[i];
2173: }
1.53 brouard 2174: }
1.69 brouard 2175: } /* end selection of waves */
1.53 brouard 2176: }
2177: }
1.74 brouard 2178: for(i=iagemin; i <= iagemax+3; i++){
1.53 brouard 2179:
1.74 brouard 2180: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
2181: posprop += prop[jk][i];
2182: }
2183:
2184: for(jk=1; jk <=nlstate ; jk++){
2185: if( i <= iagemax){
2186: if(posprop>=1.e-5){
2187: probs[i][jk][j1]= prop[jk][i]/posprop;
2188: }
2189: }
2190: }/* end jk */
2191: }/* end i */
1.53 brouard 2192: } /* end i1 */
2193: } /* end k1 */
2194:
1.74 brouard 2195: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
2196: /*free_vector(pp,1,nlstate);*/
2197: free_matrix(prop,1,nlstate, iagemin,iagemax+3);
2198: } /* End of prevalence */
1.53 brouard 2199:
2200: /************* Waves Concatenation ***************/
2201:
1.59 brouard 2202: 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)
1.53 brouard 2203: {
2204: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
2205: Death is a valid wave (if date is known).
2206: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
1.59 brouard 2207: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
1.53 brouard 2208: and mw[mi+1][i]. dh depends on stepm.
2209: */
2210:
2211: int i, mi, m;
2212: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
2213: double sum=0., jmean=0.;*/
2214: int first;
2215: int j, k=0,jk, ju, jl;
2216: double sum=0.;
2217: first=0;
2218: jmin=1e+5;
2219: jmax=-1;
2220: jmean=0.;
2221: for(i=1; i<=imx; i++){
2222: mi=0;
2223: m=firstpass;
2224: while(s[m][i] <= nlstate){
1.105 lievre 2225: if(s[m][i]>=1 || s[m][i]==-2 || s[m][i]==-4 || s[m][i]==-5)
1.53 brouard 2226: mw[++mi][i]=m;
2227: if(m >=lastpass)
2228: break;
2229: else
2230: m++;
2231: }/* end while */
2232: if (s[m][i] > nlstate){
2233: mi++; /* Death is another wave */
2234: /* if(mi==0) never been interviewed correctly before death */
2235: /* Only death is a correct wave */
2236: mw[mi][i]=m;
2237: }
2238:
2239: wav[i]=mi;
2240: if(mi==0){
1.91 brouard 2241: nbwarn++;
1.53 brouard 2242: if(first==0){
1.111 brouard 2243: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
1.53 brouard 2244: first=1;
2245: }
2246: if(first==1){
1.111 brouard 2247: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
1.53 brouard 2248: }
2249: } /* end mi==0 */
1.77 brouard 2250: } /* End individuals */
1.53 brouard 2251:
2252: for(i=1; i<=imx; i++){
2253: for(mi=1; mi<wav[i];mi++){
2254: if (stepm <=0)
2255: dh[mi][i]=1;
2256: else{
1.77 brouard 2257: if (s[mw[mi+1][i]][i] > nlstate) { /* A death */
1.53 brouard 2258: if (agedc[i] < 2*AGESUP) {
1.85 brouard 2259: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
2260: if(j==0) j=1; /* Survives at least one month after exam */
2261: else if(j<0){
1.91 brouard 2262: nberr++;
1.85 brouard 2263: 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]);
1.91 brouard 2264: j=1; /* Temporary Dangerous patch */
1.105 lievre 2265: 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);
1.91 brouard 2266: 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]);
1.105 lievre 2267: 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);
1.85 brouard 2268: }
2269: k=k+1;
1.110 brouard 2270: if (j >= jmax){
2271: jmax=j;
2272: ijmax=i;
2273: }
2274: if (j <= jmin){
2275: jmin=j;
2276: ijmin=i;
2277: }
1.85 brouard 2278: sum=sum+j;
2279: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
2280: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
1.53 brouard 2281: }
2282: }
2283: else{
2284: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1.104 lievre 2285: /* 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]); */
2286:
1.53 brouard 2287: k=k+1;
1.110 brouard 2288: if (j >= jmax) {
2289: jmax=j;
2290: ijmax=i;
2291: }
2292: else if (j <= jmin){
2293: jmin=j;
2294: ijmin=i;
2295: }
1.53 brouard 2296: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
1.73 lievre 2297: /*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]);*/
1.85 brouard 2298: if(j<0){
1.91 brouard 2299: nberr++;
1.85 brouard 2300: 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]);
2301: 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]);
2302: }
1.53 brouard 2303: sum=sum+j;
2304: }
2305: jk= j/stepm;
2306: jl= j -jk*stepm;
2307: ju= j -(jk+1)*stepm;
1.85 brouard 2308: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
1.64 lievre 2309: if(jl==0){
2310: dh[mi][i]=jk;
2311: bh[mi][i]=0;
2312: }else{ /* We want a negative bias in order to only have interpolation ie
2313: * at the price of an extra matrix product in likelihood */
2314: dh[mi][i]=jk+1;
2315: bh[mi][i]=ju;
2316: }
2317: }else{
2318: if(jl <= -ju){
2319: dh[mi][i]=jk;
2320: bh[mi][i]=jl; /* bias is positive if real duration
2321: * is higher than the multiple of stepm and negative otherwise.
2322: */
2323: }
2324: else{
2325: dh[mi][i]=jk+1;
2326: bh[mi][i]=ju;
2327: }
2328: if(dh[mi][i]==0){
2329: dh[mi][i]=1; /* At least one step */
2330: bh[mi][i]=ju; /* At least one step */
1.71 brouard 2331: /* 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);*/
1.64 lievre 2332: }
1.85 brouard 2333: } /* end if mle */
2334: }
1.64 lievre 2335: } /* end wave */
1.53 brouard 2336: }
2337: jmean=sum/k;
1.110 brouard 2338: 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);
2339: 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);
1.53 brouard 2340: }
2341:
2342: /*********** Tricode ****************************/
2343: void tricode(int *Tvar, int **nbcode, int imx)
2344: {
1.58 lievre 2345:
2346: int Ndum[20],ij=1, k, j, i, maxncov=19;
1.53 brouard 2347: int cptcode=0;
2348: cptcoveff=0;
2349:
1.58 lievre 2350: for (k=0; k<maxncov; k++) Ndum[k]=0;
1.53 brouard 2351: for (k=1; k<=7; k++) ncodemax[k]=0;
2352:
2353: for (j=1; j<=(cptcovn+2*cptcovprod); j++) {
1.58 lievre 2354: for (i=1; i<=imx; i++) { /*reads the data file to get the maximum
2355: modality*/
2356: ij=(int)(covar[Tvar[j]][i]); /* ij is the modality of this individual*/
2357: Ndum[ij]++; /*store the modality */
1.53 brouard 2358: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
1.58 lievre 2359: if (ij > cptcode) cptcode=ij; /* getting the maximum of covariable
2360: Tvar[j]. If V=sex and male is 0 and
2361: female is 1, then cptcode=1.*/
1.53 brouard 2362: }
2363:
2364: for (i=0; i<=cptcode; i++) {
1.58 lievre 2365: 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 */
1.53 brouard 2366: }
1.58 lievre 2367:
1.53 brouard 2368: ij=1;
2369: for (i=1; i<=ncodemax[j]; i++) {
1.58 lievre 2370: for (k=0; k<= maxncov; k++) {
1.53 brouard 2371: if (Ndum[k] != 0) {
2372: nbcode[Tvar[j]][ij]=k;
1.58 lievre 2373: /* 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; */
1.53 brouard 2374:
2375: ij++;
2376: }
2377: if (ij > ncodemax[j]) break;
2378: }
2379: }
2380: }
2381:
1.58 lievre 2382: for (k=0; k< maxncov; k++) Ndum[k]=0;
1.53 brouard 2383:
1.58 lievre 2384: for (i=1; i<=ncovmodel-2; i++) {
1.104 lievre 2385: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
1.53 brouard 2386: ij=Tvar[i];
1.58 lievre 2387: Ndum[ij]++;
1.53 brouard 2388: }
2389:
2390: ij=1;
1.58 lievre 2391: for (i=1; i<= maxncov; i++) {
1.53 brouard 2392: if((Ndum[i]!=0) && (i<=ncovcol)){
1.58 lievre 2393: Tvaraff[ij]=i; /*For printing */
1.53 brouard 2394: ij++;
2395: }
2396: }
2397:
1.58 lievre 2398: cptcoveff=ij-1; /*Number of simple covariates*/
1.53 brouard 2399: }
2400:
2401: /*********** Health Expectancies ****************/
2402:
1.117 ! brouard 2403: 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[] )
1.53 brouard 2404:
2405: {
1.117 ! brouard 2406: /* Health expectancies, no variances */
! 2407: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2;
1.53 brouard 2408: double age, agelim, hf;
1.117 ! brouard 2409: double ***p3mat;
! 2410: double eip;
! 2411:
! 2412: pstamp(ficreseij);
! 2413: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
! 2414: fprintf(ficreseij,"# Age");
! 2415: for(i=1; i<=nlstate;i++){
! 2416: for(j=1; j<=nlstate;j++){
! 2417: fprintf(ficreseij," e%1d%1d ",i,j);
! 2418: }
! 2419: fprintf(ficreseij," e%1d. ",i);
! 2420: }
! 2421: fprintf(ficreseij,"\n");
! 2422:
! 2423:
! 2424: if(estepm < stepm){
! 2425: printf ("Problem %d lower than %d\n",estepm, stepm);
! 2426: }
! 2427: else hstepm=estepm;
! 2428: /* We compute the life expectancy from trapezoids spaced every estepm months
! 2429: * This is mainly to measure the difference between two models: for example
! 2430: * if stepm=24 months pijx are given only every 2 years and by summing them
! 2431: * we are calculating an estimate of the Life Expectancy assuming a linear
! 2432: * progression in between and thus overestimating or underestimating according
! 2433: * to the curvature of the survival function. If, for the same date, we
! 2434: * estimate the model with stepm=1 month, we can keep estepm to 24 months
! 2435: * to compare the new estimate of Life expectancy with the same linear
! 2436: * hypothesis. A more precise result, taking into account a more precise
! 2437: * curvature will be obtained if estepm is as small as stepm. */
! 2438:
! 2439: /* For example we decided to compute the life expectancy with the smallest unit */
! 2440: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
! 2441: nhstepm is the number of hstepm from age to agelim
! 2442: nstepm is the number of stepm from age to agelin.
! 2443: Look at hpijx to understand the reason of that which relies in memory size
! 2444: and note for a fixed period like estepm months */
! 2445: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
! 2446: survival function given by stepm (the optimization length). Unfortunately it
! 2447: means that if the survival funtion is printed only each two years of age and if
! 2448: you sum them up and add 1 year (area under the trapezoids) you won't get the same
! 2449: results. So we changed our mind and took the option of the best precision.
! 2450: */
! 2451: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
! 2452:
! 2453: agelim=AGESUP;
! 2454: /* nhstepm age range expressed in number of stepm */
! 2455: nstepm=(int) rint((agelim-age)*YEARM/stepm);
! 2456: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
! 2457: /* if (stepm >= YEARM) hstepm=1;*/
! 2458: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
! 2459: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 2460:
! 2461: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
! 2462: /* Computed by stepm unit matrices, product of hstepm matrices, stored
! 2463: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
! 2464:
! 2465: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
! 2466:
! 2467: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
! 2468:
! 2469: /* Computing Variances of health expectancies */
! 2470: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
! 2471: decrease memory allocation */
! 2472: printf("%d|",(int)age);fflush(stdout);
! 2473: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
! 2474: /* Computing expectancies */
! 2475: for(i=1; i<=nlstate;i++)
! 2476: for(j=1; j<=nlstate;j++)
! 2477: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
! 2478: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
! 2479:
! 2480: /* 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]);*/
! 2481:
! 2482: }
! 2483:
! 2484: fprintf(ficreseij,"%3.0f",age );
! 2485: for(i=1; i<=nlstate;i++){
! 2486: eip=0;
! 2487: for(j=1; j<=nlstate;j++){
! 2488: eip +=eij[i][j][(int)age];
! 2489: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
! 2490: }
! 2491: fprintf(ficreseij,"%9.4f", eip );
! 2492: }
! 2493: fprintf(ficreseij,"\n");
! 2494:
! 2495: }
! 2496: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 2497: printf("\n");
! 2498: fprintf(ficlog,"\n");
! 2499:
! 2500: }
! 2501:
! 2502: 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[] )
! 2503:
! 2504: {
! 2505: /* Covariances of health expectancies eij and of total life expectancies according
! 2506: to initial status i, ei. .
! 2507: */
! 2508: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
! 2509: double age, agelim, hf;
! 2510: double ***p3matp, ***p3matm, ***varhe;
1.53 brouard 2511: double **dnewm,**doldm;
1.117 ! brouard 2512: double *xp, *xm;
1.53 brouard 2513: double **gp, **gm;
2514: double ***gradg, ***trgradg;
2515: int theta;
2516:
1.117 ! brouard 2517: double eip, vip;
! 2518:
1.74 brouard 2519: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
1.53 brouard 2520: xp=vector(1,npar);
1.117 ! brouard 2521: xm=vector(1,npar);
1.74 brouard 2522: dnewm=matrix(1,nlstate*nlstate,1,npar);
2523: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
1.53 brouard 2524:
1.117 ! brouard 2525: pstamp(ficresstdeij);
! 2526: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
! 2527: fprintf(ficresstdeij,"# Age");
! 2528: for(i=1; i<=nlstate;i++){
1.53 brouard 2529: for(j=1; j<=nlstate;j++)
1.117 ! brouard 2530: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
! 2531: fprintf(ficresstdeij," e%1d. ",i);
! 2532: }
! 2533: fprintf(ficresstdeij,"\n");
1.53 brouard 2534:
1.117 ! brouard 2535: pstamp(ficrescveij);
! 2536: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
! 2537: fprintf(ficrescveij,"# Age");
! 2538: for(i=1; i<=nlstate;i++)
! 2539: for(j=1; j<=nlstate;j++){
! 2540: cptj= (j-1)*nlstate+i;
! 2541: for(i2=1; i2<=nlstate;i2++)
! 2542: for(j2=1; j2<=nlstate;j2++){
! 2543: cptj2= (j2-1)*nlstate+i2;
! 2544: if(cptj2 <= cptj)
! 2545: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
! 2546: }
! 2547: }
! 2548: fprintf(ficrescveij,"\n");
! 2549:
1.53 brouard 2550: if(estepm < stepm){
2551: printf ("Problem %d lower than %d\n",estepm, stepm);
2552: }
2553: else hstepm=estepm;
2554: /* We compute the life expectancy from trapezoids spaced every estepm months
2555: * This is mainly to measure the difference between two models: for example
2556: * if stepm=24 months pijx are given only every 2 years and by summing them
2557: * we are calculating an estimate of the Life Expectancy assuming a linear
1.66 brouard 2558: * progression in between and thus overestimating or underestimating according
1.53 brouard 2559: * to the curvature of the survival function. If, for the same date, we
2560: * estimate the model with stepm=1 month, we can keep estepm to 24 months
2561: * to compare the new estimate of Life expectancy with the same linear
2562: * hypothesis. A more precise result, taking into account a more precise
2563: * curvature will be obtained if estepm is as small as stepm. */
2564:
2565: /* For example we decided to compute the life expectancy with the smallest unit */
2566: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
2567: nhstepm is the number of hstepm from age to agelim
2568: nstepm is the number of stepm from age to agelin.
2569: Look at hpijx to understand the reason of that which relies in memory size
2570: and note for a fixed period like estepm months */
2571: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
2572: survival function given by stepm (the optimization length). Unfortunately it
2573: means that if the survival funtion is printed only each two years of age and if
2574: you sum them up and add 1 year (area under the trapezoids) you won't get the same
2575: results. So we changed our mind and took the option of the best precision.
2576: */
2577: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
2578:
1.117 ! brouard 2579: /* If stepm=6 months */
! 2580: /* nhstepm age range expressed in number of stepm */
1.53 brouard 2581: agelim=AGESUP;
1.117 ! brouard 2582: nstepm=(int) rint((agelim-age)*YEARM/stepm);
! 2583: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
! 2584: /* if (stepm >= YEARM) hstepm=1;*/
! 2585: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
! 2586:
! 2587: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 2588: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 2589: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
! 2590: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
! 2591: gp=matrix(0,nhstepm,1,nlstate*nlstate);
! 2592: gm=matrix(0,nhstepm,1,nlstate*nlstate);
! 2593:
! 2594: for (age=bage; age<=fage; age ++){
1.53 brouard 2595:
2596: /* Computed by stepm unit matrices, product of hstepm matrices, stored
2597: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
2598:
2599: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
2600:
1.95 brouard 2601: /* Computing Variances of health expectancies */
1.117 ! brouard 2602: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
! 2603: decrease memory allocation */
! 2604: for(theta=1; theta <=npar; theta++){
1.53 brouard 2605: for(i=1; i<=npar; i++){
2606: xp[i] = x[i] + (i==theta ?delti[theta]:0);
1.117 ! brouard 2607: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.53 brouard 2608: }
1.117 ! brouard 2609: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij);
! 2610: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij);
1.53 brouard 2611:
2612: for(j=1; j<= nlstate; j++){
2613: for(i=1; i<=nlstate; i++){
1.117 ! brouard 2614: for(h=0; h<=nhstepm-1; h++){
! 2615: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
! 2616: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
1.53 brouard 2617: }
2618: }
2619: }
2620:
1.117 ! brouard 2621: for(ij=1; ij<= nlstate*nlstate; ij++)
1.53 brouard 2622: for(h=0; h<=nhstepm-1; h++){
1.117 ! brouard 2623: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
1.53 brouard 2624: }
1.117 ! brouard 2625: }/* End theta */
! 2626:
! 2627:
! 2628: for(h=0; h<=nhstepm-1; h++)
1.74 brouard 2629: for(j=1; j<=nlstate*nlstate;j++)
1.53 brouard 2630: for(theta=1; theta <=npar; theta++)
2631: trgradg[h][j][theta]=gradg[h][theta][j];
1.117 ! brouard 2632:
1.53 brouard 2633:
1.117 ! brouard 2634: for(ij=1;ij<=nlstate*nlstate;ij++)
! 2635: for(ji=1;ji<=nlstate*nlstate;ji++)
! 2636: varhe[ij][ji][(int)age] =0.;
1.53 brouard 2637:
2638: printf("%d|",(int)age);fflush(stdout);
2639: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
2640: for(h=0;h<=nhstepm-1;h++){
2641: for(k=0;k<=nhstepm-1;k++){
1.74 brouard 2642: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
2643: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
1.117 ! brouard 2644: for(ij=1;ij<=nlstate*nlstate;ij++)
! 2645: for(ji=1;ji<=nlstate*nlstate;ji++)
! 2646: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.53 brouard 2647: }
2648: }
2649: /* Computing expectancies */
1.117 ! brouard 2650: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
1.53 brouard 2651: for(i=1; i<=nlstate;i++)
2652: for(j=1; j<=nlstate;j++)
2653: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
1.117 ! brouard 2654: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
1.53 brouard 2655:
1.117 ! brouard 2656: /* 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]);*/
1.53 brouard 2657:
2658: }
2659:
1.117 ! brouard 2660: fprintf(ficresstdeij,"%3.0f",age );
! 2661: for(i=1; i<=nlstate;i++){
! 2662: eip=0.;
! 2663: vip=0.;
! 2664: for(j=1; j<=nlstate;j++){
! 2665: eip += eij[i][j][(int)age];
! 2666: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
! 2667: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
! 2668: fprintf(ficresstdeij," %9.4f (%.4f)", eij[i][j][(int)age], sqrt(varhe[(j-1)*nlstate+i][(j-1)*nlstate+i][(int)age]) );
! 2669: }
! 2670: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
! 2671: }
! 2672: fprintf(ficresstdeij,"\n");
! 2673:
! 2674: fprintf(ficrescveij,"%3.0f",age );
1.53 brouard 2675: for(i=1; i<=nlstate;i++)
2676: for(j=1; j<=nlstate;j++){
1.117 ! brouard 2677: cptj= (j-1)*nlstate+i;
! 2678: for(i2=1; i2<=nlstate;i2++)
! 2679: for(j2=1; j2<=nlstate;j2++){
! 2680: cptj2= (j2-1)*nlstate+i2;
! 2681: if(cptj2 <= cptj)
! 2682: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
! 2683: }
1.53 brouard 2684: }
1.117 ! brouard 2685: fprintf(ficrescveij,"\n");
1.53 brouard 2686:
2687: }
1.117 ! brouard 2688: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
! 2689: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
! 2690: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
! 2691: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
! 2692: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 2693: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1.53 brouard 2694: printf("\n");
2695: fprintf(ficlog,"\n");
2696:
1.117 ! brouard 2697: free_vector(xm,1,npar);
1.53 brouard 2698: free_vector(xp,1,npar);
1.74 brouard 2699: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
2700: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
2701: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
1.53 brouard 2702: }
2703:
2704: /************ Variance ******************/
1.105 lievre 2705: 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[])
1.53 brouard 2706: {
2707: /* Variance of health expectancies */
2708: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
2709: /* double **newm;*/
2710: double **dnewm,**doldm;
2711: double **dnewmp,**doldmp;
2712: int i, j, nhstepm, hstepm, h, nstepm ;
2713: int k, cptcode;
2714: double *xp;
2715: double **gp, **gm; /* for var eij */
2716: double ***gradg, ***trgradg; /*for var eij */
2717: double **gradgp, **trgradgp; /* for var p point j */
2718: double *gpp, *gmp; /* for var p point j */
2719: double **varppt; /* for var p point j nlstate to nlstate+ndeath */
2720: double ***p3mat;
2721: double age,agelim, hf;
2722: double ***mobaverage;
2723: int theta;
2724: char digit[4];
1.55 lievre 2725: char digitp[25];
1.53 brouard 2726:
2727: char fileresprobmorprev[FILENAMELENGTH];
2728:
1.55 lievre 2729: if(popbased==1){
1.58 lievre 2730: if(mobilav!=0)
1.55 lievre 2731: strcpy(digitp,"-populbased-mobilav-");
2732: else strcpy(digitp,"-populbased-nomobil-");
2733: }
2734: else
1.53 brouard 2735: strcpy(digitp,"-stablbased-");
1.56 lievre 2736:
1.54 brouard 2737: if (mobilav!=0) {
1.53 brouard 2738: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
1.54 brouard 2739: if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){
2740: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
2741: printf(" Error in movingaverage mobilav=%d\n",mobilav);
2742: }
1.53 brouard 2743: }
2744:
2745: strcpy(fileresprobmorprev,"prmorprev");
2746: sprintf(digit,"%-d",ij);
2747: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
2748: strcat(fileresprobmorprev,digit); /* Tvar to be done */
2749: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
2750: strcat(fileresprobmorprev,fileres);
2751: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
2752: printf("Problem with resultfile: %s\n", fileresprobmorprev);
2753: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
2754: }
2755: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
1.105 lievre 2756:
1.53 brouard 2757: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
1.117 ! brouard 2758: pstamp(ficresprobmorprev);
1.66 brouard 2759: 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);
1.53 brouard 2760: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
2761: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
2762: fprintf(ficresprobmorprev," p.%-d SE",j);
2763: for(i=1; i<=nlstate;i++)
2764: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
2765: }
2766: fprintf(ficresprobmorprev,"\n");
1.88 brouard 2767: fprintf(ficgp,"\n# Routine varevsij");
1.106 brouard 2768: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
1.87 brouard 2769: 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");
2770: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
2771: /* } */
1.53 brouard 2772: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
1.117 ! brouard 2773: pstamp(ficresvij);
! 2774: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
! 2775: if(popbased==1)
! 2776: fprintf(ficresvij,"the age specific prevalence observed in the population i.e cross-sectionally\n in each health state (popbased=1)");
! 2777: else
! 2778: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
1.53 brouard 2779: fprintf(ficresvij,"# Age");
2780: for(i=1; i<=nlstate;i++)
2781: for(j=1; j<=nlstate;j++)
1.117 ! brouard 2782: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
1.53 brouard 2783: fprintf(ficresvij,"\n");
2784:
2785: xp=vector(1,npar);
2786: dnewm=matrix(1,nlstate,1,npar);
2787: doldm=matrix(1,nlstate,1,nlstate);
2788: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
2789: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
2790:
2791: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
2792: gpp=vector(nlstate+1,nlstate+ndeath);
2793: gmp=vector(nlstate+1,nlstate+ndeath);
2794: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
2795:
2796: if(estepm < stepm){
2797: printf ("Problem %d lower than %d\n",estepm, stepm);
2798: }
2799: else hstepm=estepm;
2800: /* For example we decided to compute the life expectancy with the smallest unit */
2801: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
2802: nhstepm is the number of hstepm from age to agelim
2803: nstepm is the number of stepm from age to agelin.
2804: Look at hpijx to understand the reason of that which relies in memory size
2805: and note for a fixed period like k years */
2806: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
2807: survival function given by stepm (the optimization length). Unfortunately it
1.66 brouard 2808: means that if the survival funtion is printed every two years of age and if
1.53 brouard 2809: you sum them up and add 1 year (area under the trapezoids) you won't get the same
2810: results. So we changed our mind and took the option of the best precision.
2811: */
2812: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
2813: agelim = AGESUP;
2814: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
2815: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
2816: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
2817: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
2818: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
2819: gp=matrix(0,nhstepm,1,nlstate);
2820: gm=matrix(0,nhstepm,1,nlstate);
2821:
2822:
2823: for(theta=1; theta <=npar; theta++){
1.66 brouard 2824: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
1.53 brouard 2825: xp[i] = x[i] + (i==theta ?delti[theta]:0);
2826: }
2827: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
2828: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
2829:
2830: if (popbased==1) {
1.54 brouard 2831: if(mobilav ==0){
1.53 brouard 2832: for(i=1; i<=nlstate;i++)
2833: prlim[i][i]=probs[(int)age][i][ij];
1.54 brouard 2834: }else{ /* mobilav */
1.53 brouard 2835: for(i=1; i<=nlstate;i++)
2836: prlim[i][i]=mobaverage[(int)age][i][ij];
2837: }
2838: }
2839:
2840: for(j=1; j<= nlstate; j++){
2841: for(h=0; h<=nhstepm; h++){
2842: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
2843: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
2844: }
2845: }
1.66 brouard 2846: /* This for computing probability of death (h=1 means
2847: computed over hstepm matrices product = hstepm*stepm months)
2848: as a weighted average of prlim.
2849: */
1.69 brouard 2850: for(j=nlstate+1;j<=nlstate+ndeath;j++){
1.68 lievre 2851: for(i=1,gpp[j]=0.; i<= nlstate; i++)
1.53 brouard 2852: gpp[j] += prlim[i][i]*p3mat[i][j][1];
2853: }
1.66 brouard 2854: /* end probability of death */
1.53 brouard 2855:
1.66 brouard 2856: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
1.53 brouard 2857: xp[i] = x[i] - (i==theta ?delti[theta]:0);
2858: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
2859: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
2860:
2861: if (popbased==1) {
1.54 brouard 2862: if(mobilav ==0){
1.53 brouard 2863: for(i=1; i<=nlstate;i++)
2864: prlim[i][i]=probs[(int)age][i][ij];
1.54 brouard 2865: }else{ /* mobilav */
1.53 brouard 2866: for(i=1; i<=nlstate;i++)
2867: prlim[i][i]=mobaverage[(int)age][i][ij];
2868: }
2869: }
2870:
2871: for(j=1; j<= nlstate; j++){
2872: for(h=0; h<=nhstepm; h++){
2873: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
2874: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
2875: }
2876: }
1.66 brouard 2877: /* This for computing probability of death (h=1 means
2878: computed over hstepm matrices product = hstepm*stepm months)
2879: as a weighted average of prlim.
2880: */
1.69 brouard 2881: for(j=nlstate+1;j<=nlstate+ndeath;j++){
1.68 lievre 2882: for(i=1,gmp[j]=0.; i<= nlstate; i++)
2883: gmp[j] += prlim[i][i]*p3mat[i][j][1];
1.53 brouard 2884: }
1.66 brouard 2885: /* end probability of death */
1.53 brouard 2886:
2887: for(j=1; j<= nlstate; j++) /* vareij */
2888: for(h=0; h<=nhstepm; h++){
2889: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
2890: }
1.68 lievre 2891:
1.53 brouard 2892: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu */
2893: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
2894: }
2895:
2896: } /* End theta */
2897:
2898: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
2899:
2900: for(h=0; h<=nhstepm; h++) /* veij */
2901: for(j=1; j<=nlstate;j++)
2902: for(theta=1; theta <=npar; theta++)
2903: trgradg[h][j][theta]=gradg[h][theta][j];
2904:
2905: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
1.69 brouard 2906: for(theta=1; theta <=npar; theta++)
1.53 brouard 2907: trgradgp[j][theta]=gradgp[theta][j];
1.69 brouard 2908:
1.53 brouard 2909:
2910: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
2911: for(i=1;i<=nlstate;i++)
2912: for(j=1;j<=nlstate;j++)
2913: vareij[i][j][(int)age] =0.;
2914:
2915: for(h=0;h<=nhstepm;h++){
2916: for(k=0;k<=nhstepm;k++){
2917: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
2918: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
2919: for(i=1;i<=nlstate;i++)
2920: for(j=1;j<=nlstate;j++)
2921: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
2922: }
2923: }
1.70 brouard 2924:
1.53 brouard 2925: /* pptj */
2926: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
2927: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
1.70 brouard 2928: for(j=nlstate+1;j<=nlstate+ndeath;j++)
2929: for(i=nlstate+1;i<=nlstate+ndeath;i++)
1.53 brouard 2930: varppt[j][i]=doldmp[j][i];
2931: /* end ppptj */
1.66 brouard 2932: /* x centered again */
1.53 brouard 2933: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij);
2934: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ij);
2935:
2936: if (popbased==1) {
1.54 brouard 2937: if(mobilav ==0){
1.53 brouard 2938: for(i=1; i<=nlstate;i++)
2939: prlim[i][i]=probs[(int)age][i][ij];
1.54 brouard 2940: }else{ /* mobilav */
1.53 brouard 2941: for(i=1; i<=nlstate;i++)
2942: prlim[i][i]=mobaverage[(int)age][i][ij];
2943: }
2944: }
1.70 brouard 2945:
1.66 brouard 2946: /* This for computing probability of death (h=1 means
2947: computed over hstepm (estepm) matrices product = hstepm*stepm months)
2948: as a weighted average of prlim.
2949: */
1.68 lievre 2950: for(j=nlstate+1;j<=nlstate+ndeath;j++){
2951: for(i=1,gmp[j]=0.;i<= nlstate; i++)
1.53 brouard 2952: gmp[j] += prlim[i][i]*p3mat[i][j][1];
2953: }
1.66 brouard 2954: /* end probability of death */
1.53 brouard 2955:
2956: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
2957: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
2958: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
2959: for(i=1; i<=nlstate;i++){
2960: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
2961: }
2962: }
2963: fprintf(ficresprobmorprev,"\n");
2964:
2965: fprintf(ficresvij,"%.0f ",age );
2966: for(i=1; i<=nlstate;i++)
2967: for(j=1; j<=nlstate;j++){
2968: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
2969: }
2970: fprintf(ficresvij,"\n");
2971: free_matrix(gp,0,nhstepm,1,nlstate);
2972: free_matrix(gm,0,nhstepm,1,nlstate);
2973: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
2974: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
2975: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
2976: } /* End age */
2977: free_vector(gpp,nlstate+1,nlstate+ndeath);
2978: free_vector(gmp,nlstate+1,nlstate+ndeath);
2979: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
2980: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
2981: fprintf(ficgp,"\nset noparametric;set nolabel; set ter png small;set size 0.65, 0.65");
2982: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
2983: fprintf(ficgp,"\n set log y; set nolog x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
1.67 brouard 2984: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
2985: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
2986: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
1.88 brouard 2987: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l 1 ",subdirf(fileresprobmorprev));
2988: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95\%% interval\" w l 2 ",subdirf(fileresprobmorprev));
2989: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l 2 ",subdirf(fileresprobmorprev));
2990: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
2991: 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);
1.53 brouard 2992: /* 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);
2993: */
1.88 brouard 2994: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.png\";replot;",digitp,optionfilefiname,digit); */
1.89 brouard 2995: fprintf(ficgp,"\nset out \"%s%s.png\";replot;\n",subdirf3(optionfilefiname,"varmuptjgr",digitp),digit);
1.53 brouard 2996:
2997: free_vector(xp,1,npar);
2998: free_matrix(doldm,1,nlstate,1,nlstate);
2999: free_matrix(dnewm,1,nlstate,1,npar);
3000: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
3001: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
3002: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
1.55 lievre 3003: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
1.53 brouard 3004: fclose(ficresprobmorprev);
1.88 brouard 3005: fflush(ficgp);
3006: fflush(fichtm);
1.84 brouard 3007: } /* end varevsij */
1.53 brouard 3008:
3009: /************ Variance of prevlim ******************/
1.105 lievre 3010: 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[])
1.53 brouard 3011: {
3012: /* Variance of prevalence limit */
1.59 brouard 3013: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.53 brouard 3014: double **newm;
3015: double **dnewm,**doldm;
3016: int i, j, nhstepm, hstepm;
3017: int k, cptcode;
3018: double *xp;
3019: double *gp, *gm;
3020: double **gradg, **trgradg;
3021: double age,agelim;
3022: int theta;
1.117 ! brouard 3023:
! 3024: pstamp(ficresvpl);
! 3025: fprintf(ficresvpl,"# Standard deviation of period (stable) prevalences \n");
1.53 brouard 3026: fprintf(ficresvpl,"# Age");
3027: for(i=1; i<=nlstate;i++)
3028: fprintf(ficresvpl," %1d-%1d",i,i);
3029: fprintf(ficresvpl,"\n");
3030:
3031: xp=vector(1,npar);
3032: dnewm=matrix(1,nlstate,1,npar);
3033: doldm=matrix(1,nlstate,1,nlstate);
3034:
3035: hstepm=1*YEARM; /* Every year of age */
3036: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
3037: agelim = AGESUP;
3038: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
3039: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
3040: if (stepm >= YEARM) hstepm=1;
3041: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
3042: gradg=matrix(1,npar,1,nlstate);
3043: gp=vector(1,nlstate);
3044: gm=vector(1,nlstate);
3045:
3046: for(theta=1; theta <=npar; theta++){
3047: for(i=1; i<=npar; i++){ /* Computes gradient */
3048: xp[i] = x[i] + (i==theta ?delti[theta]:0);
3049: }
3050: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
3051: for(i=1;i<=nlstate;i++)
3052: gp[i] = prlim[i][i];
3053:
3054: for(i=1; i<=npar; i++) /* Computes gradient */
3055: xp[i] = x[i] - (i==theta ?delti[theta]:0);
3056: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
3057: for(i=1;i<=nlstate;i++)
3058: gm[i] = prlim[i][i];
3059:
3060: for(i=1;i<=nlstate;i++)
3061: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
3062: } /* End theta */
3063:
3064: trgradg =matrix(1,nlstate,1,npar);
3065:
3066: for(j=1; j<=nlstate;j++)
3067: for(theta=1; theta <=npar; theta++)
3068: trgradg[j][theta]=gradg[theta][j];
3069:
3070: for(i=1;i<=nlstate;i++)
3071: varpl[i][(int)age] =0.;
3072: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
3073: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
3074: for(i=1;i<=nlstate;i++)
3075: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
3076:
3077: fprintf(ficresvpl,"%.0f ",age );
3078: for(i=1; i<=nlstate;i++)
3079: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
3080: fprintf(ficresvpl,"\n");
3081: free_vector(gp,1,nlstate);
3082: free_vector(gm,1,nlstate);
3083: free_matrix(gradg,1,npar,1,nlstate);
3084: free_matrix(trgradg,1,nlstate,1,npar);
3085: } /* End age */
3086:
3087: free_vector(xp,1,npar);
3088: free_matrix(doldm,1,nlstate,1,npar);
3089: free_matrix(dnewm,1,nlstate,1,nlstate);
3090:
3091: }
3092:
3093: /************ Variance of one-step probabilities ******************/
1.105 lievre 3094: 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[])
1.53 brouard 3095: {
3096: int i, j=0, i1, k1, l1, t, tj;
3097: int k2, l2, j1, z1;
3098: int k=0,l, cptcode;
3099: int first=1, first1;
3100: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
3101: double **dnewm,**doldm;
3102: double *xp;
3103: double *gp, *gm;
3104: double **gradg, **trgradg;
3105: double **mu;
3106: double age,agelim, cov[NCOVMAX];
3107: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
3108: int theta;
3109: char fileresprob[FILENAMELENGTH];
3110: char fileresprobcov[FILENAMELENGTH];
3111: char fileresprobcor[FILENAMELENGTH];
3112:
3113: double ***varpij;
3114:
3115: strcpy(fileresprob,"prob");
3116: strcat(fileresprob,fileres);
3117: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
3118: printf("Problem with resultfile: %s\n", fileresprob);
3119: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
3120: }
3121: strcpy(fileresprobcov,"probcov");
3122: strcat(fileresprobcov,fileres);
3123: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
3124: printf("Problem with resultfile: %s\n", fileresprobcov);
3125: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
3126: }
3127: strcpy(fileresprobcor,"probcor");
3128: strcat(fileresprobcor,fileres);
3129: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
3130: printf("Problem with resultfile: %s\n", fileresprobcor);
3131: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
3132: }
3133: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
3134: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
3135: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
3136: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
3137: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
3138: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
1.117 ! brouard 3139: pstamp(ficresprob);
1.53 brouard 3140: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
3141: fprintf(ficresprob,"# Age");
1.117 ! brouard 3142: pstamp(ficresprobcov);
1.53 brouard 3143: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
3144: fprintf(ficresprobcov,"# Age");
1.117 ! brouard 3145: pstamp(ficresprobcor);
1.53 brouard 3146: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
1.117 ! brouard 3147: fprintf(ficresprobcor,"# Age");
1.53 brouard 3148:
3149:
3150: for(i=1; i<=nlstate;i++)
3151: for(j=1; j<=(nlstate+ndeath);j++){
3152: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
3153: fprintf(ficresprobcov," p%1d-%1d ",i,j);
3154: fprintf(ficresprobcor," p%1d-%1d ",i,j);
3155: }
1.69 brouard 3156: /* fprintf(ficresprob,"\n");
1.53 brouard 3157: fprintf(ficresprobcov,"\n");
3158: fprintf(ficresprobcor,"\n");
1.69 brouard 3159: */
3160: xp=vector(1,npar);
1.53 brouard 3161: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
3162: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
3163: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
3164: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
3165: first=1;
1.88 brouard 3166: fprintf(ficgp,"\n# Routine varprob");
3167: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
3168: fprintf(fichtm,"\n");
3169:
1.95 brouard 3170: fprintf(fichtm,"\n<li><h4> <a href=\"%s\">Matrix of variance-covariance of pairs of step probabilities (drawings)</a></h4></li>\n",optionfilehtmcov);
3171: fprintf(fichtmcov,"\n<h4>Matrix of variance-covariance of pairs of step probabilities</h4>\n\
1.91 brouard 3172: file %s<br>\n",optionfilehtmcov);
3173: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated\
3174: and drawn. It helps understanding how is the covariance between two incidences.\
3175: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
3176: 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. \
3177: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
3178: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
3179: standard deviations wide on each axis. <br>\
3180: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
3181: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
3182: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
1.53 brouard 3183:
3184: cov[1]=1;
3185: tj=cptcoveff;
3186: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
3187: j1=0;
3188: for(t=1; t<=tj;t++){
3189: for(i1=1; i1<=ncodemax[t];i1++){
3190: j1++;
3191: if (cptcovn>0) {
3192: fprintf(ficresprob, "\n#********** Variable ");
3193: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
1.69 brouard 3194: fprintf(ficresprob, "**********\n#\n");
1.53 brouard 3195: fprintf(ficresprobcov, "\n#********** Variable ");
3196: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
1.69 brouard 3197: fprintf(ficresprobcov, "**********\n#\n");
1.53 brouard 3198:
3199: fprintf(ficgp, "\n#********** Variable ");
1.69 brouard 3200: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
3201: fprintf(ficgp, "**********\n#\n");
1.53 brouard 3202:
3203:
1.96 brouard 3204: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
1.53 brouard 3205: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
1.96 brouard 3206: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
1.53 brouard 3207:
3208: fprintf(ficresprobcor, "\n#********** Variable ");
3209: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
1.69 brouard 3210: fprintf(ficresprobcor, "**********\n#");
1.53 brouard 3211: }
3212:
3213: for (age=bage; age<=fage; age ++){
3214: cov[2]=age;
3215: for (k=1; k<=cptcovn;k++) {
3216: cov[2+k]=nbcode[Tvar[k]][codtab[j1][Tvar[k]]];
3217: }
3218: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
3219: for (k=1; k<=cptcovprod;k++)
3220: cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];
3221:
3222: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
3223: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
3224: gp=vector(1,(nlstate)*(nlstate+ndeath));
3225: gm=vector(1,(nlstate)*(nlstate+ndeath));
3226:
3227: for(theta=1; theta <=npar; theta++){
3228: for(i=1; i<=npar; i++)
1.74 brouard 3229: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
1.53 brouard 3230:
3231: pmij(pmmij,cov,ncovmodel,xp,nlstate);
3232:
3233: k=0;
3234: for(i=1; i<= (nlstate); i++){
3235: for(j=1; j<=(nlstate+ndeath);j++){
3236: k=k+1;
3237: gp[k]=pmmij[i][j];
3238: }
3239: }
3240:
3241: for(i=1; i<=npar; i++)
1.74 brouard 3242: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
1.53 brouard 3243:
3244: pmij(pmmij,cov,ncovmodel,xp,nlstate);
3245: k=0;
3246: for(i=1; i<=(nlstate); i++){
3247: for(j=1; j<=(nlstate+ndeath);j++){
3248: k=k+1;
3249: gm[k]=pmmij[i][j];
3250: }
3251: }
3252:
3253: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
1.74 brouard 3254: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
1.53 brouard 3255: }
3256:
3257: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
3258: for(theta=1; theta <=npar; theta++)
3259: trgradg[j][theta]=gradg[theta][j];
3260:
3261: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
3262: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
1.59 brouard 3263: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
3264: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
3265: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
3266: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
3267:
1.53 brouard 3268: pmij(pmmij,cov,ncovmodel,x,nlstate);
3269:
3270: k=0;
3271: for(i=1; i<=(nlstate); i++){
3272: for(j=1; j<=(nlstate+ndeath);j++){
3273: k=k+1;
3274: mu[k][(int) age]=pmmij[i][j];
3275: }
3276: }
3277: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
3278: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
3279: varpij[i][j][(int)age] = doldm[i][j];
3280:
3281: /*printf("\n%d ",(int)age);
1.59 brouard 3282: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
3283: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
3284: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
3285: }*/
1.53 brouard 3286:
3287: fprintf(ficresprob,"\n%d ",(int)age);
3288: fprintf(ficresprobcov,"\n%d ",(int)age);
3289: fprintf(ficresprobcor,"\n%d ",(int)age);
3290:
3291: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
3292: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
3293: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
3294: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
3295: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
3296: }
3297: i=0;
3298: for (k=1; k<=(nlstate);k++){
3299: for (l=1; l<=(nlstate+ndeath);l++){
3300: i=i++;
3301: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
3302: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
3303: for (j=1; j<=i;j++){
3304: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
3305: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
3306: }
3307: }
3308: }/* end of loop for state */
3309: } /* end of loop for age */
3310:
3311: /* Confidence intervalle of pij */
3312: /*
1.59 brouard 3313: fprintf(ficgp,"\nset noparametric;unset label");
3314: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
3315: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
3316: 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);
3317: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
3318: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
3319: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
1.53 brouard 3320: */
3321:
3322: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
3323: first1=1;
3324: for (k2=1; k2<=(nlstate);k2++){
3325: for (l2=1; l2<=(nlstate+ndeath);l2++){
3326: if(l2==k2) continue;
3327: j=(k2-1)*(nlstate+ndeath)+l2;
3328: for (k1=1; k1<=(nlstate);k1++){
3329: for (l1=1; l1<=(nlstate+ndeath);l1++){
3330: if(l1==k1) continue;
3331: i=(k1-1)*(nlstate+ndeath)+l1;
3332: if(i<=j) continue;
3333: for (age=bage; age<=fage; age ++){
3334: if ((int)age %5==0){
3335: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
3336: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
3337: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
3338: mu1=mu[i][(int) age]/stepm*YEARM ;
3339: mu2=mu[j][(int) age]/stepm*YEARM;
3340: c12=cv12/sqrt(v1*v2);
3341: /* Computing eigen value of matrix of covariance */
3342: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3343: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3344: /* Eigen vectors */
3345: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
3346: /*v21=sqrt(1.-v11*v11); *//* error */
3347: v21=(lc1-v1)/cv12*v11;
3348: v12=-v21;
3349: v22=v11;
3350: tnalp=v21/v11;
3351: if(first1==1){
3352: first1=0;
3353: 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);
3354: }
3355: 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);
3356: /*printf(fignu*/
3357: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
3358: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
3359: if(first==1){
3360: first=0;
3361: fprintf(ficgp,"\nset parametric;unset label");
3362: 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);
3363: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
1.91 brouard 3364: fprintf(fichtmcov,"\n<br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.88 brouard 3365: :<a href=\"%s%d%1d%1d-%1d%1d.png\">\
3366: %s%d%1d%1d-%1d%1d.png</A>, ",k1,l1,k2,l2,\
3367: subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2,\
3368: subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
1.91 brouard 3369: fprintf(fichtmcov,"\n<br><img src=\"%s%d%1d%1d-%1d%1d.png\"> ",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
3370: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
1.88 brouard 3371: fprintf(ficgp,"\nset out \"%s%d%1d%1d-%1d%1d.png\"",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
1.53 brouard 3372: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
3373: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
3374: 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",\
3375: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),\
3376: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
3377: }else{
3378: first=0;
1.91 brouard 3379: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
1.53 brouard 3380: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
3381: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
3382: 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",\
3383: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),\
3384: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
3385: }/* if first */
3386: } /* age mod 5 */
3387: } /* end loop age */
1.88 brouard 3388: fprintf(ficgp,"\nset out \"%s%d%1d%1d-%1d%1d.png\";replot;",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
1.53 brouard 3389: first=1;
3390: } /*l12 */
3391: } /* k12 */
3392: } /*l1 */
3393: }/* k1 */
3394: } /* loop covariates */
3395: }
1.59 brouard 3396: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
3397: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
1.113 brouard 3398: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
3399: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
1.53 brouard 3400: free_vector(xp,1,npar);
3401: fclose(ficresprob);
3402: fclose(ficresprobcov);
3403: fclose(ficresprobcor);
1.91 brouard 3404: fflush(ficgp);
3405: fflush(fichtmcov);
1.53 brouard 3406: }
3407:
3408:
3409: /******************* Printing html file ***********/
3410: void printinghtml(char fileres[], char title[], char datafile[], int firstpass, \
3411: int lastpass, int stepm, int weightopt, char model[],\
3412: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
3413: int popforecast, int estepm ,\
3414: double jprev1, double mprev1,double anprev1, \
3415: double jprev2, double mprev2,double anprev2){
3416: int jj1, k1, i1, cpt;
3417:
1.107 brouard 3418: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
3419: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
1.106 brouard 3420: </ul>");
1.107 brouard 3421: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n \
1.96 brouard 3422: - Observed prevalence in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): <a href=\"%s\">%s</a> <br>\n ",
3423: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirf2(fileres,"p"),subdirf2(fileres,"p"));
3424: fprintf(fichtm,"\
3425: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
3426: stepm,subdirf2(fileres,"pij"),subdirf2(fileres,"pij"));
3427: fprintf(fichtm,"\
1.117 ! brouard 3428: - Period (stable) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.96 brouard 3429: subdirf2(fileres,"pl"),subdirf2(fileres,"pl"));
3430: fprintf(fichtm,"\
1.117 ! brouard 3431: - (a) Life expectancies by health status at initial age, (b) health expectancies by health status at initial age: ei., eij (estepm=%2d months): \
1.96 brouard 3432: <a href=\"%s\">%s</a> <br>\n</li>",
1.88 brouard 3433: estepm,subdirf2(fileres,"e"),subdirf2(fileres,"e"));
1.116 brouard 3434:
1.53 brouard 3435:
3436: fprintf(fichtm," \n<ul><li><b>Graphs</b></li><p>");
3437:
3438: m=cptcoveff;
3439: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
3440:
3441: jj1=0;
3442: for(k1=1; k1<=m;k1++){
3443: for(i1=1; i1<=ncodemax[k1];i1++){
3444: jj1++;
3445: if (cptcovn > 0) {
3446: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
3447: for (cpt=1; cpt<=cptcoveff;cpt++)
3448: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtab[jj1][cpt]]);
3449: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
3450: }
3451: /* Pij */
1.117 ! brouard 3452: 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> \
! 3453: <img src=\"%s%d1.png\">",stepm,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1);
1.53 brouard 3454: /* Quasi-incidences */
1.85 brouard 3455: fprintf(fichtm,"<br>- Pij or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
1.117 ! brouard 3456: 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> \
! 3457: <img src=\"%s%d2.png\">",stepm,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1);
! 3458: /* Period (stable) prevalence in each health state */
1.53 brouard 3459: for(cpt=1; cpt<nlstate;cpt++){
1.117 ! brouard 3460: fprintf(fichtm,"<br>- Period (stable) prevalence in each health state : <a href=\"%s%d%d.png\">%s%d%d.png</a><br> \
! 3461: <img src=\"%s%d%d.png\">",subdirf2(optionfilefiname,"p"),cpt,jj1,subdirf2(optionfilefiname,"p"),cpt,jj1,subdirf2(optionfilefiname,"p"),cpt,jj1);
1.53 brouard 3462: }
3463: for(cpt=1; cpt<=nlstate;cpt++) {
1.117 ! brouard 3464: 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> \
! 3465: <img src=\"%s%d%d.png\">",cpt,subdirf2(optionfilefiname,"exp"),cpt,jj1,subdirf2(optionfilefiname,"exp"),cpt,jj1,subdirf2(optionfilefiname,"exp"),cpt,jj1);
1.53 brouard 3466: }
3467: } /* end i1 */
3468: }/* End k1 */
3469: fprintf(fichtm,"</ul>");
3470:
3471:
1.96 brouard 3472: fprintf(fichtm,"\
1.107 brouard 3473: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.96 brouard 3474: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br>\n", rfileres,rfileres);
3475:
3476: fprintf(fichtm," - Variance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
3477: subdirf2(fileres,"prob"),subdirf2(fileres,"prob"));
3478: fprintf(fichtm,"\
3479: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
3480: subdirf2(fileres,"probcov"),subdirf2(fileres,"probcov"));
3481:
3482: fprintf(fichtm,"\
3483: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
3484: subdirf2(fileres,"probcor"),subdirf2(fileres,"probcor"));
3485: fprintf(fichtm,"\
1.117 ! brouard 3486: - 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): \
! 3487: <a href=\"%s\">%s</a> <br>\n</li>",
! 3488: estepm,subdirf2(fileres,"cve"),subdirf2(fileres,"cve"));
! 3489: fprintf(fichtm,"\
! 3490: - (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): \
! 3491: <a href=\"%s\">%s</a> <br>\n</li>",
! 3492: estepm,subdirf2(fileres,"stde"),subdirf2(fileres,"stde"));
! 3493: fprintf(fichtm,"\
! 3494: - 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",
1.96 brouard 3495: estepm, subdirf2(fileres,"v"),subdirf2(fileres,"v"));
3496: fprintf(fichtm,"\
1.117 ! brouard 3497: - 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",
1.96 brouard 3498: subdirf2(fileres,"t"),subdirf2(fileres,"t"));
3499: fprintf(fichtm,"\
1.117 ! brouard 3500: - Standard deviation of period (stable) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.88 brouard 3501: subdirf2(fileres,"vpl"),subdirf2(fileres,"vpl"));
1.53 brouard 3502:
1.76 brouard 3503: /* if(popforecast==1) fprintf(fichtm,"\n */
3504: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
3505: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
3506: /* <br>",fileres,fileres,fileres,fileres); */
3507: /* else */
3508: /* 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); */
1.96 brouard 3509: fflush(fichtm);
3510: fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
1.53 brouard 3511:
3512: m=cptcoveff;
3513: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
3514:
3515: jj1=0;
3516: for(k1=1; k1<=m;k1++){
3517: for(i1=1; i1<=ncodemax[k1];i1++){
3518: jj1++;
3519: if (cptcovn > 0) {
3520: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
3521: for (cpt=1; cpt<=cptcoveff;cpt++)
3522: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtab[jj1][cpt]]);
3523: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
3524: }
3525: for(cpt=1; cpt<=nlstate;cpt++) {
1.95 brouard 3526: fprintf(fichtm,"<br>- Observed (cross-sectional) and period (incidence based) \
3527: prevalence (with 95%% confidence interval) in state (%d): %s%d%d.png <br>\
1.90 brouard 3528: <img src=\"%s%d%d.png\">",cpt,subdirf2(optionfilefiname,"v"),cpt,jj1,subdirf2(optionfilefiname,"v"),cpt,jj1);
1.53 brouard 3529: }
1.96 brouard 3530: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
3531: health expectancies in states (1) and (2): %s%d.png<br>\
3532: <img src=\"%s%d.png\">",subdirf2(optionfilefiname,"e"),jj1,subdirf2(optionfilefiname,"e"),jj1);
1.53 brouard 3533: } /* end i1 */
3534: }/* End k1 */
3535: fprintf(fichtm,"</ul>");
1.87 brouard 3536: fflush(fichtm);
1.53 brouard 3537: }
3538:
3539: /******************* Gnuplot file **************/
1.89 brouard 3540: void printinggnuplot(char fileres[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.53 brouard 3541:
1.88 brouard 3542: char dirfileres[132],optfileres[132];
1.53 brouard 3543: int m,cpt,k1,i,k,j,jk,k2,k3,ij,l;
3544: int ng;
1.88 brouard 3545: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
3546: /* printf("Problem with file %s",optionfilegnuplot); */
3547: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
3548: /* } */
1.53 brouard 3549:
1.54 brouard 3550: /*#ifdef windows */
1.89 brouard 3551: fprintf(ficgp,"cd \"%s\" \n",pathc);
1.54 brouard 3552: /*#endif */
1.88 brouard 3553: m=pow(2,cptcoveff);
3554:
3555: strcpy(dirfileres,optionfilefiname);
3556: strcpy(optfileres,"vpl");
1.53 brouard 3557: /* 1eme*/
3558: for (cpt=1; cpt<= nlstate ; cpt ++) {
3559: for (k1=1; k1<= m ; k1 ++) {
1.88 brouard 3560: fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"v"),cpt,k1);
3561: fprintf(ficgp,"\n#set out \"v%s%d%d.png\" \n",optionfilefiname,cpt,k1);
3562: fprintf(ficgp,"set xlabel \"Age\" \n\
3563: set ylabel \"Probability\" \n\
3564: set ter png small\n\
3565: set size 0.65,0.65\n\
3566: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:2 \"\%%lf",ageminpar,fage,subdirf2(fileres,"vpl"),k1-1,k1-1);
1.53 brouard 3567:
3568: for (i=1; i<= nlstate ; i ++) {
3569: if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
3570: else fprintf(ficgp," \%%*lf (\%%*lf)");
3571: }
1.117 ! brouard 3572: 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);
1.53 brouard 3573: for (i=1; i<= nlstate ; i ++) {
3574: if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
3575: else fprintf(ficgp," \%%*lf (\%%*lf)");
3576: }
1.88 brouard 3577: 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);
1.53 brouard 3578: for (i=1; i<= nlstate ; i ++) {
3579: if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
3580: else fprintf(ficgp," \%%*lf (\%%*lf)");
3581: }
1.88 brouard 3582: 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));
1.53 brouard 3583: }
3584: }
3585: /*2 eme*/
3586:
3587: for (k1=1; k1<= m ; k1 ++) {
1.88 brouard 3588: fprintf(ficgp,"\nset out \"%s%d.png\" \n",subdirf2(optionfilefiname,"e"),k1);
1.53 brouard 3589: fprintf(ficgp,"set ylabel \"Years\" \nset ter png small\nset size 0.65,0.65\nplot [%.f:%.f] ",ageminpar,fage);
3590:
3591: for (i=1; i<= nlstate+1 ; i ++) {
3592: k=2*i;
1.88 brouard 3593: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:2 \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1);
1.53 brouard 3594: for (j=1; j<= nlstate+1 ; j ++) {
3595: if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
3596: else fprintf(ficgp," \%%*lf (\%%*lf)");
3597: }
3598: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l ,");
3599: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l ,",i-1);
1.88 brouard 3600: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2-$3*2) \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1);
1.53 brouard 3601: for (j=1; j<= nlstate+1 ; j ++) {
3602: if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
3603: else fprintf(ficgp," \%%*lf (\%%*lf)");
3604: }
3605: fprintf(ficgp,"\" t\"\" w l 0,");
1.88 brouard 3606: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2+$3*2) \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1);
1.53 brouard 3607: for (j=1; j<= nlstate+1 ; j ++) {
3608: if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
3609: else fprintf(ficgp," \%%*lf (\%%*lf)");
3610: }
3611: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l 0");
3612: else fprintf(ficgp,"\" t\"\" w l 0,");
3613: }
3614: }
3615:
3616: /*3eme*/
3617:
3618: for (k1=1; k1<= m ; k1 ++) {
3619: for (cpt=1; cpt<= nlstate ; cpt ++) {
1.117 ! brouard 3620: /* k=2+nlstate*(2*cpt-2); */
! 3621: k=2+(nlstate+1)*(cpt-1);
1.88 brouard 3622: fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"exp"),cpt,k1);
3623: fprintf(ficgp,"set ter png small\n\
3624: set size 0.65,0.65\n\
3625: 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);
1.53 brouard 3626: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
3627: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
3628: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
3629: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
3630: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
3631: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
3632:
3633: */
3634: for (i=1; i< nlstate ; i ++) {
1.117 ! brouard 3635: 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);
! 3636: /* 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);*/
1.53 brouard 3637:
3638: }
1.117 ! brouard 3639: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+nlstate,cpt);
1.53 brouard 3640: }
3641: }
3642:
1.76 brouard 3643: /* CV preval stable (period) */
1.53 brouard 3644: for (k1=1; k1<= m ; k1 ++) {
1.76 brouard 3645: for (cpt=1; cpt<=nlstate ; cpt ++) {
1.53 brouard 3646: k=3;
1.88 brouard 3647: fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"p"),cpt,k1);
3648: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
3649: set ter png small\nset size 0.65,0.65\n\
1.89 brouard 3650: unset log y\n\
1.88 brouard 3651: plot [%.f:%.f] \"%s\" u ($1==%d ? ($3):1/0):($%d/($%d",ageminpar,agemaxpar,subdirf2(fileres,"pij"),k1,k+cpt+1,k+1);
1.53 brouard 3652:
1.83 lievre 3653: for (i=1; i< nlstate ; i ++)
1.53 brouard 3654: fprintf(ficgp,"+$%d",k+i+1);
3655: fprintf(ficgp,")) t\"prev(%d,%d)\" w l",cpt,cpt+1);
3656:
3657: l=3+(nlstate+ndeath)*cpt;
1.88 brouard 3658: fprintf(ficgp,",\"%s\" u ($1==%d ? ($3):1/0):($%d/($%d",subdirf2(fileres,"pij"),k1,l+cpt+1,l+1);
1.53 brouard 3659: for (i=1; i< nlstate ; i ++) {
3660: l=3+(nlstate+ndeath)*cpt;
3661: fprintf(ficgp,"+$%d",l+i+1);
3662: }
3663: fprintf(ficgp,")) t\"prev(%d,%d)\" w l\n",cpt+1,cpt+1);
3664: }
3665: }
3666:
3667: /* proba elementaires */
3668: for(i=1,jk=1; i <=nlstate; i++){
3669: for(k=1; k <=(nlstate+ndeath); k++){
3670: if (k != i) {
3671: for(j=1; j <=ncovmodel; j++){
3672: fprintf(ficgp,"p%d=%f ",jk,p[jk]);
3673: jk++;
3674: fprintf(ficgp,"\n");
3675: }
3676: }
3677: }
3678: }
3679:
3680: for(ng=1; ng<=2;ng++){ /* Number of graphics: first is probabilities second is incidence per year*/
3681: for(jk=1; jk <=m; jk++) {
1.88 brouard 3682: fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"pe"),jk,ng);
1.53 brouard 3683: if (ng==2)
3684: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
3685: else
3686: fprintf(ficgp,"\nset title \"Probability\"\n");
3687: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65\nset log y\nplot [%.f:%.f] ",ageminpar,agemaxpar);
3688: i=1;
3689: for(k2=1; k2<=nlstate; k2++) {
3690: k3=i;
3691: for(k=1; k<=(nlstate+ndeath); k++) {
3692: if (k != k2){
3693: if(ng==2)
3694: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
3695: else
3696: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
3697: ij=1;
3698: for(j=3; j <=ncovmodel; j++) {
3699: if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) {
3700: fprintf(ficgp,"+p%d*%d*x",i+j-1,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);
3701: ij++;
3702: }
3703: else
3704: fprintf(ficgp,"+p%d*%d",i+j-1,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
3705: }
3706: fprintf(ficgp,")/(1");
3707:
3708: for(k1=1; k1 <=nlstate; k1++){
3709: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
3710: ij=1;
3711: for(j=3; j <=ncovmodel; j++){
3712: if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) {
3713: fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);
3714: ij++;
3715: }
3716: else
3717: fprintf(ficgp,"+p%d*%d",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
3718: }
3719: fprintf(ficgp,")");
3720: }
3721: fprintf(ficgp,") t \"p%d%d\" ", k2,k);
3722: if ((k+k2)!= (nlstate*2+ndeath)) fprintf(ficgp,",");
3723: i=i+ncovmodel;
3724: }
3725: } /* end k */
3726: } /* end k2 */
3727: } /* end jk */
3728: } /* end ng */
1.88 brouard 3729: fflush(ficgp);
1.53 brouard 3730: } /* end gnuplot */
3731:
3732:
3733: /*************** Moving average **************/
1.54 brouard 3734: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav){
1.53 brouard 3735:
3736: int i, cpt, cptcod;
1.58 lievre 3737: int modcovmax =1;
1.54 brouard 3738: int mobilavrange, mob;
1.53 brouard 3739: double age;
1.58 lievre 3740:
3741: modcovmax=2*cptcoveff;/* Max number of modalities. We suppose
3742: a covariate has 2 modalities */
3743: if (cptcovn<1) modcovmax=1; /* At least 1 pass */
3744:
1.54 brouard 3745: if(mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
3746: if(mobilav==1) mobilavrange=5; /* default */
3747: else mobilavrange=mobilav;
3748: for (age=bage; age<=fage; age++)
3749: for (i=1; i<=nlstate;i++)
1.58 lievre 3750: for (cptcod=1;cptcod<=modcovmax;cptcod++)
1.54 brouard 3751: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
3752: /* We keep the original values on the extreme ages bage, fage and for
3753: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
3754: we use a 5 terms etc. until the borders are no more concerned.
3755: */
3756: for (mob=3;mob <=mobilavrange;mob=mob+2){
3757: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
3758: for (i=1; i<=nlstate;i++){
1.58 lievre 3759: for (cptcod=1;cptcod<=modcovmax;cptcod++){
1.54 brouard 3760: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
3761: for (cpt=1;cpt<=(mob-1)/2;cpt++){
3762: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
3763: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
3764: }
3765: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
3766: }
1.53 brouard 3767: }
1.54 brouard 3768: }/* end age */
3769: }/* end mob */
3770: }else return -1;
3771: return 0;
3772: }/* End movingaverage */
1.53 brouard 3773:
3774:
3775: /************** Forecasting ******************/
1.70 brouard 3776: 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){
1.69 brouard 3777: /* proj1, year, month, day of starting projection
3778: agemin, agemax range of age
3779: dateprev1 dateprev2 range of dates during which prevalence is computed
1.70 brouard 3780: anproj2 year of en of projection (same day and month as proj1).
1.69 brouard 3781: */
1.73 lievre 3782: int yearp, stepsize, hstepm, nhstepm, j, k, c, cptcod, i, h, i1;
1.53 brouard 3783: int *popage;
1.70 brouard 3784: double agec; /* generic age */
3785: double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
1.53 brouard 3786: double *popeffectif,*popcount;
3787: double ***p3mat;
1.55 lievre 3788: double ***mobaverage;
1.53 brouard 3789: char fileresf[FILENAMELENGTH];
3790:
1.69 brouard 3791: agelim=AGESUP;
1.84 brouard 3792: prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
1.53 brouard 3793:
3794: strcpy(fileresf,"f");
3795: strcat(fileresf,fileres);
3796: if((ficresf=fopen(fileresf,"w"))==NULL) {
3797: printf("Problem with forecast resultfile: %s\n", fileresf);
3798: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
3799: }
3800: printf("Computing forecasting: result on file '%s' \n", fileresf);
3801: fprintf(ficlog,"Computing forecasting: result on file '%s' \n", fileresf);
3802:
3803: if (cptcoveff==0) ncodemax[cptcoveff]=1;
3804:
1.54 brouard 3805: if (mobilav!=0) {
1.53 brouard 3806: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
1.54 brouard 3807: if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){
3808: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
3809: printf(" Error in movingaverage mobilav=%d\n",mobilav);
3810: }
1.53 brouard 3811: }
3812:
3813: stepsize=(int) (stepm+YEARM-1)/YEARM;
3814: if (stepm<=12) stepsize=1;
1.74 brouard 3815: if(estepm < stepm){
3816: printf ("Problem %d lower than %d\n",estepm, stepm);
3817: }
3818: else hstepm=estepm;
3819:
1.53 brouard 3820: hstepm=hstepm/stepm;
1.69 brouard 3821: yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp and
3822: fractional in yp1 */
1.53 brouard 3823: anprojmean=yp;
3824: yp2=modf((yp1*12),&yp);
3825: mprojmean=yp;
3826: yp1=modf((yp2*30.5),&yp);
3827: jprojmean=yp;
3828: if(jprojmean==0) jprojmean=1;
3829: if(mprojmean==0) jprojmean=1;
1.73 lievre 3830:
3831: i1=cptcoveff;
3832: if (cptcovn < 1){i1=1;}
1.53 brouard 3833:
1.70 brouard 3834: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2);
1.53 brouard 3835:
1.70 brouard 3836: fprintf(ficresf,"#****** Routine prevforecast **\n");
1.73 lievre 3837:
1.75 brouard 3838: /* if (h==(int)(YEARM*yearp)){ */
1.73 lievre 3839: for(cptcov=1, k=0;cptcov<=i1;cptcov++){
1.53 brouard 3840: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
3841: k=k+1;
3842: fprintf(ficresf,"\n#******");
3843: for(j=1;j<=cptcoveff;j++) {
1.70 brouard 3844: 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]]);
1.53 brouard 3845: }
3846: fprintf(ficresf,"******\n");
1.70 brouard 3847: fprintf(ficresf,"# Covariate valuofcovar yearproj age");
3848: for(j=1; j<=nlstate+ndeath;j++){
3849: for(i=1; i<=nlstate;i++)
3850: fprintf(ficresf," p%d%d",i,j);
3851: fprintf(ficresf," p.%d",j);
3852: }
1.74 brouard 3853: for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) {
1.53 brouard 3854: fprintf(ficresf,"\n");
1.70 brouard 3855: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);
1.53 brouard 3856:
1.71 brouard 3857: for (agec=fage; agec>=(ageminpar-1); agec--){
1.70 brouard 3858: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
1.53 brouard 3859: nhstepm = nhstepm/hstepm;
3860: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3861: oldm=oldms;savm=savms;
1.70 brouard 3862: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k);
1.53 brouard 3863:
3864: for (h=0; h<=nhstepm; h++){
1.75 brouard 3865: if (h*hstepm/YEARM*stepm ==yearp) {
1.69 brouard 3866: fprintf(ficresf,"\n");
3867: for(j=1;j<=cptcoveff;j++)
3868: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
1.70 brouard 3869: fprintf(ficresf,"%.f %.f ",anproj1+yearp,agec+h*hstepm/YEARM*stepm);
1.53 brouard 3870: }
3871: for(j=1; j<=nlstate+ndeath;j++) {
1.70 brouard 3872: ppij=0.;
1.71 brouard 3873: for(i=1; i<=nlstate;i++) {
1.53 brouard 3874: if (mobilav==1)
1.71 brouard 3875: ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod];
1.53 brouard 3876: else {
1.71 brouard 3877: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod];
1.53 brouard 3878: }
1.75 brouard 3879: if (h*hstepm/YEARM*stepm== yearp) {
1.70 brouard 3880: fprintf(ficresf," %.3f", p3mat[i][j][h]);
1.75 brouard 3881: }
3882: } /* end i */
3883: if (h*hstepm/YEARM*stepm==yearp) {
1.70 brouard 3884: fprintf(ficresf," %.3f", ppij);
1.53 brouard 3885: }
1.75 brouard 3886: }/* end j */
3887: } /* end h */
1.53 brouard 3888: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1.75 brouard 3889: } /* end agec */
3890: } /* end yearp */
3891: } /* end cptcod */
3892: } /* end cptcov */
1.53 brouard 3893:
1.54 brouard 3894: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
1.53 brouard 3895:
3896: fclose(ficresf);
3897: }
1.70 brouard 3898:
3899: /************** Forecasting *****not tested NB*************/
1.53 brouard 3900: 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){
3901:
3902: int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h;
3903: int *popage;
1.69 brouard 3904: double calagedatem, agelim, kk1, kk2;
1.53 brouard 3905: double *popeffectif,*popcount;
3906: double ***p3mat,***tabpop,***tabpopprev;
1.55 lievre 3907: double ***mobaverage;
1.53 brouard 3908: char filerespop[FILENAMELENGTH];
3909:
3910: tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
3911: tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
3912: agelim=AGESUP;
1.69 brouard 3913: calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM;
1.53 brouard 3914:
1.84 brouard 3915: prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
1.53 brouard 3916:
3917:
3918: strcpy(filerespop,"pop");
3919: strcat(filerespop,fileres);
3920: if((ficrespop=fopen(filerespop,"w"))==NULL) {
3921: printf("Problem with forecast resultfile: %s\n", filerespop);
3922: fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop);
3923: }
3924: printf("Computing forecasting: result on file '%s' \n", filerespop);
3925: fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop);
3926:
3927: if (cptcoveff==0) ncodemax[cptcoveff]=1;
3928:
1.54 brouard 3929: if (mobilav!=0) {
1.53 brouard 3930: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
1.54 brouard 3931: if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){
3932: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
3933: printf(" Error in movingaverage mobilav=%d\n",mobilav);
3934: }
1.53 brouard 3935: }
3936:
3937: stepsize=(int) (stepm+YEARM-1)/YEARM;
3938: if (stepm<=12) stepsize=1;
3939:
3940: agelim=AGESUP;
3941:
3942: hstepm=1;
3943: hstepm=hstepm/stepm;
3944:
3945: if (popforecast==1) {
3946: if((ficpop=fopen(popfile,"r"))==NULL) {
3947: printf("Problem with population file : %s\n",popfile);exit(0);
3948: fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0);
3949: }
3950: popage=ivector(0,AGESUP);
3951: popeffectif=vector(0,AGESUP);
3952: popcount=vector(0,AGESUP);
3953:
3954: i=1;
3955: while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1;
3956:
3957: imx=i;
3958: for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i];
3959: }
3960:
1.69 brouard 3961: for(cptcov=1,k=0;cptcov<=i2;cptcov++){
1.53 brouard 3962: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
3963: k=k+1;
3964: fprintf(ficrespop,"\n#******");
3965: for(j=1;j<=cptcoveff;j++) {
3966: fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
3967: }
3968: fprintf(ficrespop,"******\n");
3969: fprintf(ficrespop,"# Age");
3970: for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j);
3971: if (popforecast==1) fprintf(ficrespop," [Population]");
3972:
3973: for (cpt=0; cpt<=0;cpt++) {
3974: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
3975:
1.69 brouard 3976: for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){
1.53 brouard 3977: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
3978: nhstepm = nhstepm/hstepm;
3979:
3980: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3981: oldm=oldms;savm=savms;
3982: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
3983:
3984: for (h=0; h<=nhstepm; h++){
1.69 brouard 3985: if (h==(int) (calagedatem+YEARM*cpt)) {
1.53 brouard 3986: fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
3987: }
3988: for(j=1; j<=nlstate+ndeath;j++) {
3989: kk1=0.;kk2=0;
3990: for(i=1; i<=nlstate;i++) {
3991: if (mobilav==1)
3992: kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod];
3993: else {
3994: kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod];
3995: }
3996: }
1.69 brouard 3997: if (h==(int)(calagedatem+12*cpt)){
1.53 brouard 3998: tabpop[(int)(agedeb)][j][cptcod]=kk1;
3999: /*fprintf(ficrespop," %.3f", kk1);
4000: if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*/
4001: }
4002: }
4003: for(i=1; i<=nlstate;i++){
4004: kk1=0.;
4005: for(j=1; j<=nlstate;j++){
4006: kk1= kk1+tabpop[(int)(agedeb)][j][cptcod];
4007: }
1.69 brouard 4008: tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)];
1.53 brouard 4009: }
4010:
1.69 brouard 4011: if (h==(int)(calagedatem+12*cpt)) for(j=1; j<=nlstate;j++)
1.53 brouard 4012: fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]);
4013: }
4014: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4015: }
4016: }
4017:
4018: /******/
4019:
4020: for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) {
4021: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
1.69 brouard 4022: for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){
1.53 brouard 4023: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
4024: nhstepm = nhstepm/hstepm;
4025:
4026: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4027: oldm=oldms;savm=savms;
4028: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
4029: for (h=0; h<=nhstepm; h++){
1.69 brouard 4030: if (h==(int) (calagedatem+YEARM*cpt)) {
1.53 brouard 4031: fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
4032: }
4033: for(j=1; j<=nlstate+ndeath;j++) {
4034: kk1=0.;kk2=0;
4035: for(i=1; i<=nlstate;i++) {
4036: kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod];
4037: }
1.69 brouard 4038: if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1);
1.53 brouard 4039: }
4040: }
4041: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4042: }
4043: }
4044: }
4045: }
4046:
1.54 brouard 4047: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
1.53 brouard 4048:
4049: if (popforecast==1) {
4050: free_ivector(popage,0,AGESUP);
4051: free_vector(popeffectif,0,AGESUP);
4052: free_vector(popcount,0,AGESUP);
4053: }
4054: free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4055: free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
4056: fclose(ficrespop);
1.84 brouard 4057: } /* End of popforecast */
1.53 brouard 4058:
1.87 brouard 4059: int fileappend(FILE *fichier, char *optionfich)
1.86 brouard 4060: {
1.87 brouard 4061: if((fichier=fopen(optionfich,"a"))==NULL) {
4062: printf("Problem with file: %s\n", optionfich);
4063: fprintf(ficlog,"Problem with file: %s\n", optionfich);
4064: return (0);
1.86 brouard 4065: }
1.87 brouard 4066: fflush(fichier);
4067: return (1);
1.86 brouard 4068: }
1.94 brouard 4069:
4070:
4071: /**************** function prwizard **********************/
1.88 brouard 4072: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
4073: {
4074:
1.94 brouard 4075: /* Wizard to print covariance matrix template */
4076:
1.88 brouard 4077: char ca[32], cb[32], cc[32];
4078: int i,j, k, l, li, lj, lk, ll, jj, npar, itimes;
4079: int numlinepar;
4080:
4081: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
4082: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
4083: for(i=1; i <=nlstate; i++){
4084: jj=0;
4085: for(j=1; j <=nlstate+ndeath; j++){
4086: if(j==i) continue;
4087: jj++;
4088: /*ca[0]= k+'a'-1;ca[1]='\0';*/
4089: printf("%1d%1d",i,j);
4090: fprintf(ficparo,"%1d%1d",i,j);
4091: for(k=1; k<=ncovmodel;k++){
4092: /* printf(" %lf",param[i][j][k]); */
4093: /* fprintf(ficparo," %lf",param[i][j][k]); */
4094: printf(" 0.");
4095: fprintf(ficparo," 0.");
4096: }
4097: printf("\n");
4098: fprintf(ficparo,"\n");
4099: }
4100: }
4101: printf("# Scales (for hessian or gradient estimation)\n");
4102: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
4103: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
4104: for(i=1; i <=nlstate; i++){
4105: jj=0;
4106: for(j=1; j <=nlstate+ndeath; j++){
4107: if(j==i) continue;
4108: jj++;
4109: fprintf(ficparo,"%1d%1d",i,j);
4110: printf("%1d%1d",i,j);
4111: fflush(stdout);
4112: for(k=1; k<=ncovmodel;k++){
4113: /* printf(" %le",delti3[i][j][k]); */
4114: /* fprintf(ficparo," %le",delti3[i][j][k]); */
4115: printf(" 0.");
4116: fprintf(ficparo," 0.");
4117: }
4118: numlinepar++;
4119: printf("\n");
4120: fprintf(ficparo,"\n");
4121: }
4122: }
4123: printf("# Covariance matrix\n");
4124: /* # 121 Var(a12)\n\ */
4125: /* # 122 Cov(b12,a12) Var(b12)\n\ */
4126: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
4127: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
4128: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
4129: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
4130: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
4131: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
4132: fflush(stdout);
4133: fprintf(ficparo,"# Covariance matrix\n");
4134: /* # 121 Var(a12)\n\ */
4135: /* # 122 Cov(b12,a12) Var(b12)\n\ */
4136: /* # ...\n\ */
4137: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
4138:
4139: for(itimes=1;itimes<=2;itimes++){
4140: jj=0;
4141: for(i=1; i <=nlstate; i++){
4142: for(j=1; j <=nlstate+ndeath; j++){
4143: if(j==i) continue;
4144: for(k=1; k<=ncovmodel;k++){
4145: jj++;
4146: ca[0]= k+'a'-1;ca[1]='\0';
4147: if(itimes==1){
4148: printf("#%1d%1d%d",i,j,k);
4149: fprintf(ficparo,"#%1d%1d%d",i,j,k);
4150: }else{
4151: printf("%1d%1d%d",i,j,k);
4152: fprintf(ficparo,"%1d%1d%d",i,j,k);
4153: /* printf(" %.5le",matcov[i][j]); */
4154: }
4155: ll=0;
4156: for(li=1;li <=nlstate; li++){
4157: for(lj=1;lj <=nlstate+ndeath; lj++){
4158: if(lj==li) continue;
4159: for(lk=1;lk<=ncovmodel;lk++){
4160: ll++;
4161: if(ll<=jj){
4162: cb[0]= lk +'a'-1;cb[1]='\0';
4163: if(ll<jj){
4164: if(itimes==1){
4165: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
4166: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
4167: }else{
4168: printf(" 0.");
4169: fprintf(ficparo," 0.");
4170: }
4171: }else{
4172: if(itimes==1){
4173: printf(" Var(%s%1d%1d)",ca,i,j);
4174: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
4175: }else{
4176: printf(" 0.");
4177: fprintf(ficparo," 0.");
4178: }
4179: }
4180: }
4181: } /* end lk */
4182: } /* end lj */
4183: } /* end li */
4184: printf("\n");
4185: fprintf(ficparo,"\n");
4186: numlinepar++;
4187: } /* end k*/
4188: } /*end j */
4189: } /* end i */
1.95 brouard 4190: } /* end itimes */
1.88 brouard 4191:
4192: } /* end of prwizard */
1.98 brouard 4193: /******************* Gompertz Likelihood ******************************/
4194: double gompertz(double x[])
4195: {
4196: double A,B,L=0.0,sump=0.,num=0.;
4197: int i,n=0; /* n is the size of the sample */
1.110 brouard 4198:
1.98 brouard 4199: for (i=0;i<=imx-1 ; i++) {
4200: sump=sump+weight[i];
1.103 lievre 4201: /* sump=sump+1;*/
1.98 brouard 4202: num=num+1;
4203: }
4204:
4205:
1.103 lievre 4206: /* for (i=0; i<=imx; i++)
1.98 brouard 4207: 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]);*/
4208:
1.103 lievre 4209: for (i=1;i<=imx ; i++)
1.98 brouard 4210: {
1.110 brouard 4211: if (cens[i] == 1 && wav[i]>1)
1.103 lievre 4212: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
1.98 brouard 4213:
1.110 brouard 4214: if (cens[i] == 0 && wav[i]>1)
1.103 lievre 4215: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
4216: +log(x[1]/YEARM)+x[2]*(agedc[i]-agegomp)+log(YEARM);
1.98 brouard 4217:
1.110 brouard 4218: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
4219: if (wav[i] > 1 ) { /* ??? */
1.98 brouard 4220: L=L+A*weight[i];
4221: /* 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]);*/
4222: }
4223: }
4224:
4225: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
4226:
4227: return -2*L*num/sump;
4228: }
4229:
4230: /******************* Printing html file ***********/
4231: void printinghtmlmort(char fileres[], char title[], char datafile[], int firstpass, \
4232: int lastpass, int stepm, int weightopt, char model[],\
1.105 lievre 4233: int imx, double p[],double **matcov,double agemortsup){
4234: int i,k;
1.98 brouard 4235:
4236: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
4237: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
4238: for (i=1;i<=2;i++)
4239: 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]));
4240: fprintf(fichtm,"<br><br><img src=\"graphmort.png\">");
4241: fprintf(fichtm,"</ul>");
1.105 lievre 4242:
4243: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
4244:
1.106 brouard 4245: 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>");
1.105 lievre 4246:
4247: for (k=agegomp;k<(agemortsup-2);k++)
4248: 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]);
4249:
4250:
1.98 brouard 4251: fflush(fichtm);
4252: }
4253:
4254: /******************* Gnuplot file **************/
4255: void printinggnuplotmort(char fileres[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
4256:
4257: char dirfileres[132],optfileres[132];
4258: int m,cpt,k1,i,k,j,jk,k2,k3,ij,l;
4259: int ng;
4260:
4261:
4262: /*#ifdef windows */
4263: fprintf(ficgp,"cd \"%s\" \n",pathc);
4264: /*#endif */
4265:
4266:
4267: strcpy(dirfileres,optionfilefiname);
4268: strcpy(optfileres,"vpl");
4269: fprintf(ficgp,"set out \"graphmort.png\"\n ");
4270: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
4271: fprintf(ficgp, "set ter png small\n set log y\n");
4272: fprintf(ficgp, "set size 0.65,0.65\n");
4273: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
4274:
4275: }
4276:
4277:
1.88 brouard 4278:
1.91 brouard 4279:
1.117 ! brouard 4280:
1.53 brouard 4281: /***********************************************/
4282: /**************** Main Program *****************/
4283: /***********************************************/
4284:
4285: int main(int argc, char *argv[])
4286: {
1.61 brouard 4287: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.74 brouard 4288: int i,j, k, n=MAXN,iter,m,size=100,cptcode, cptcod;
1.110 brouard 4289: int linei, month, year,iout;
1.95 brouard 4290: int jj, ll, li, lj, lk, imk;
1.85 brouard 4291: int numlinepar=0; /* Current linenumber of parameter file */
1.95 brouard 4292: int itimes;
1.98 brouard 4293: int NDIM=2;
1.95 brouard 4294:
4295: char ca[32], cb[32], cc[32];
1.111 brouard 4296: char dummy[]=" ";
1.87 brouard 4297: /* FILE *fichtm; *//* Html File */
4298: /* FILE *ficgp;*/ /*Gnuplot File */
1.107 brouard 4299: struct stat info;
1.53 brouard 4300: double agedeb, agefin,hf;
4301: double ageminpar=1.e20,agemin=1.e20, agemaxpar=-1.e20, agemax=-1.e20;
4302:
4303: double fret;
4304: double **xi,tmp,delta;
4305:
4306: double dum; /* Dummy variable */
4307: double ***p3mat;
4308: double ***mobaverage;
4309: int *indx;
4310: char line[MAXLINE], linepar[MAXLINE];
1.88 brouard 4311: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE],model[MAXLINE];
1.99 brouard 4312: char pathr[MAXLINE], pathimach[MAXLINE];
1.114 brouard 4313: char **bp, *tok, *val; /* pathtot */
1.53 brouard 4314: int firstobs=1, lastobs=10;
4315: int sdeb, sfin; /* Status at beginning and end */
4316: int c, h , cpt,l;
4317: int ju,jl, mi;
4318: int i1,j1, k1,k2,k3,jk,aa,bb, stepsize, ij;
1.59 brouard 4319: int jnais,jdc,jint4,jint1,jint2,jint3,**outcome,*tab;
1.69 brouard 4320: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.53 brouard 4321: int mobilav=0,popforecast=0;
4322: int hstepm, nhstepm;
1.105 lievre 4323: int agemortsup;
4324: float sumlpop=0.;
1.74 brouard 4325: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
4326: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
1.53 brouard 4327:
4328: double bage, fage, age, agelim, agebase;
4329: double ftolpl=FTOL;
4330: double **prlim;
4331: double *severity;
4332: double ***param; /* Matrix of parameters */
4333: double *p;
4334: double **matcov; /* Matrix of covariance */
4335: double ***delti3; /* Scale */
4336: double *delti; /* Scale */
4337: double ***eij, ***vareij;
4338: double **varpl; /* Variances of prevalence limits by age */
4339: double *epj, vepp;
4340: double kk1, kk2;
1.74 brouard 4341: double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
1.98 brouard 4342: double **ximort;
1.53 brouard 4343: char *alph[]={"a","a","b","c","d","e"}, str[4];
1.98 brouard 4344: int *dcwave;
1.53 brouard 4345:
4346: char z[1]="c", occ;
1.86 brouard 4347:
1.53 brouard 4348: char stra[80], strb[80], strc[80], strd[80],stre[80],modelsav[80];
1.117 ! brouard 4349: char *strt, strtend[80];
1.85 brouard 4350: char *stratrunc;
4351: int lstra;
4352:
4353: long total_usecs;
1.53 brouard 4354:
1.95 brouard 4355: /* setlocale (LC_ALL, ""); */
4356: /* bindtextdomain (PACKAGE, LOCALEDIR); */
4357: /* textdomain (PACKAGE); */
4358: /* setlocale (LC_CTYPE, ""); */
4359: /* setlocale (LC_MESSAGES, ""); */
4360:
1.85 brouard 4361: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
4362: (void) gettimeofday(&start_time,&tzp);
1.91 brouard 4363: curr_time=start_time;
1.85 brouard 4364: tm = *localtime(&start_time.tv_sec);
4365: tmg = *gmtime(&start_time.tv_sec);
1.88 brouard 4366: strcpy(strstart,asctime(&tm));
1.86 brouard 4367:
1.88 brouard 4368: /* printf("Localtime (at start)=%s",strstart); */
1.85 brouard 4369: /* tp.tv_sec = tp.tv_sec +86400; */
4370: /* tm = *localtime(&start_time.tv_sec); */
4371: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
4372: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
4373: /* tmg.tm_hour=tmg.tm_hour + 1; */
4374: /* tp.tv_sec = mktime(&tmg); */
4375: /* strt=asctime(&tmg); */
1.88 brouard 4376: /* printf("Time(after) =%s",strstart); */
1.85 brouard 4377: /* (void) time (&time_value);
4378: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
4379: * tm = *localtime(&time_value);
1.88 brouard 4380: * strstart=asctime(&tm);
4381: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
1.85 brouard 4382: */
4383:
1.91 brouard 4384: nberr=0; /* Number of errors and warnings */
4385: nbwarn=0;
1.53 brouard 4386: getcwd(pathcd, size);
4387:
1.81 brouard 4388: printf("\n%s\n%s",version,fullversion);
1.53 brouard 4389: if(argc <=1){
4390: printf("\nEnter the parameter file name: ");
1.114 brouard 4391: fgets(pathr,FILENAMELENGTH,stdin);
4392: i=strlen(pathr);
4393: if(pathr[i-1]=='\n')
4394: pathr[i-1]='\0';
4395: for (tok = pathr; tok != NULL; ){
4396: printf("Pathr |%s|\n",pathr);
4397: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
4398: printf("val= |%s| pathr=%s\n",val,pathr);
4399: strcpy (pathtot, val);
4400: if(pathr[0] == '\0') break; /* Un peu sale */
4401: }
1.53 brouard 4402: }
4403: else{
4404: strcpy(pathtot,argv[1]);
4405: }
1.88 brouard 4406: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
1.53 brouard 4407: /*cygwin_split_path(pathtot,path,optionfile);
4408: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
4409: /* cutv(path,optionfile,pathtot,'\\');*/
4410:
1.107 brouard 4411: /* Split argv[0], imach program to get pathimach */
4412: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
1.99 brouard 4413: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
1.107 brouard 4414: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
1.99 brouard 4415: /* strcpy(pathimach,argv[0]); */
1.107 brouard 4416: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
1.53 brouard 4417: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
1.107 brouard 4418: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.53 brouard 4419: chdir(path);
1.88 brouard 4420: strcpy(command,"mkdir ");
4421: strcat(command,optionfilefiname);
4422: if((outcmd=system(command)) != 0){
4423: printf("Problem creating directory or it already exists %s%s, err=%d\n",path,optionfilefiname,outcmd);
4424: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
4425: /* fclose(ficlog); */
4426: /* exit(1); */
4427: }
4428: /* if((imk=mkdir(optionfilefiname))<0){ */
4429: /* perror("mkdir"); */
4430: /* } */
1.53 brouard 4431:
1.59 brouard 4432: /*-------- arguments in the command line --------*/
1.53 brouard 4433:
4434: /* Log file */
4435: strcat(filelog, optionfilefiname);
4436: strcat(filelog,".log"); /* */
4437: if((ficlog=fopen(filelog,"w"))==NULL) {
4438: printf("Problem with logfile %s\n",filelog);
4439: goto end;
4440: }
4441: fprintf(ficlog,"Log filename:%s\n",filelog);
1.85 brouard 4442: fprintf(ficlog,"\n%s\n%s",version,fullversion);
1.99 brouard 4443: fprintf(ficlog,"\nEnter the parameter file name: \n");
4444: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
1.88 brouard 4445: path=%s \n\
4446: optionfile=%s\n\
4447: optionfilext=%s\n\
1.99 brouard 4448: optionfilefiname=%s\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.86 brouard 4449:
1.94 brouard 4450: printf("Local time (at start):%s",strstart);
4451: fprintf(ficlog,"Local time (at start): %s",strstart);
1.53 brouard 4452: fflush(ficlog);
1.91 brouard 4453: /* (void) gettimeofday(&curr_time,&tzp); */
4454: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tv_sec-start_time.tv_sec,tmpout)); */
1.53 brouard 4455:
4456: /* */
4457: strcpy(fileres,"r");
4458: strcat(fileres, optionfilefiname);
4459: strcat(fileres,".txt"); /* Other files have txt extension */
4460:
4461: /*---------arguments file --------*/
4462:
4463: if((ficpar=fopen(optionfile,"r"))==NULL) {
4464: printf("Problem with optionfile %s\n",optionfile);
4465: fprintf(ficlog,"Problem with optionfile %s\n",optionfile);
1.85 brouard 4466: fflush(ficlog);
1.53 brouard 4467: goto end;
4468: }
4469:
1.88 brouard 4470:
4471:
1.53 brouard 4472: strcpy(filereso,"o");
4473: strcat(filereso,fileres);
1.88 brouard 4474: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
1.53 brouard 4475: printf("Problem with Output resultfile: %s\n", filereso);
4476: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
1.85 brouard 4477: fflush(ficlog);
1.53 brouard 4478: goto end;
4479: }
4480:
4481: /* Reads comments: lines beginning with '#' */
1.85 brouard 4482: numlinepar=0;
1.53 brouard 4483: while((c=getc(ficpar))=='#' && c!= EOF){
4484: ungetc(c,ficpar);
4485: fgets(line, MAXLINE, ficpar);
1.85 brouard 4486: numlinepar++;
1.53 brouard 4487: puts(line);
4488: fputs(line,ficparo);
1.85 brouard 4489: fputs(line,ficlog);
1.53 brouard 4490: }
4491: ungetc(c,ficpar);
4492:
4493: 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);
1.85 brouard 4494: numlinepar++;
1.53 brouard 4495: 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);
4496: 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);
1.85 brouard 4497: 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);
4498: fflush(ficlog);
1.59 brouard 4499: while((c=getc(ficpar))=='#' && c!= EOF){
1.53 brouard 4500: ungetc(c,ficpar);
4501: fgets(line, MAXLINE, ficpar);
1.85 brouard 4502: numlinepar++;
1.53 brouard 4503: puts(line);
4504: fputs(line,ficparo);
1.85 brouard 4505: fputs(line,ficlog);
1.53 brouard 4506: }
4507: ungetc(c,ficpar);
1.85 brouard 4508:
1.53 brouard 4509:
4510: covar=matrix(0,NCOVMAX,1,n);
1.58 lievre 4511: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement*/
1.53 brouard 4512: if (strlen(model)>1) cptcovn=nbocc(model,'+')+1;
4513:
1.58 lievre 4514: ncovmodel=2+cptcovn; /*Number of variables = cptcovn + intercept + age */
1.53 brouard 4515: nvar=ncovmodel-1; /* Suppressing age as a basic covariate */
1.98 brouard 4516: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
4517:
4518: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
4519: delti=delti3[1][1];
4520: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
1.88 brouard 4521: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
4522: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
4523: printf(" You choose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
4524: fprintf(ficlog," You choose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.98 brouard 4525: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
1.88 brouard 4526: fclose (ficparo);
4527: fclose (ficlog);
4528: exit(0);
4529: }
1.98 brouard 4530: else if(mle==-3) {
4531: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
4532: printf(" You choose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
4533: fprintf(ficlog," You choose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
4534: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
4535: matcov=matrix(1,npar,1,npar);
1.53 brouard 4536: }
1.98 brouard 4537: else{
4538: /* Read guess parameters */
4539: /* Reads comments: lines beginning with '#' */
4540: while((c=getc(ficpar))=='#' && c!= EOF){
4541: ungetc(c,ficpar);
4542: fgets(line, MAXLINE, ficpar);
1.85 brouard 4543: numlinepar++;
1.98 brouard 4544: puts(line);
4545: fputs(line,ficparo);
4546: fputs(line,ficlog);
1.53 brouard 4547: }
1.98 brouard 4548: ungetc(c,ficpar);
4549:
4550: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
4551: for(i=1; i <=nlstate; i++){
4552: j=0;
4553: for(jj=1; jj <=nlstate+ndeath; jj++){
4554: if(jj==i) continue;
4555: j++;
4556: fscanf(ficpar,"%1d%1d",&i1,&j1);
4557: if ((i1 != i) && (j1 != j)){
4558: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
4559: exit(1);
4560: }
4561: fprintf(ficparo,"%1d%1d",i1,j1);
4562: if(mle==1)
4563: printf("%1d%1d",i,j);
4564: fprintf(ficlog,"%1d%1d",i,j);
4565: for(k=1; k<=ncovmodel;k++){
4566: fscanf(ficpar," %lf",¶m[i][j][k]);
4567: if(mle==1){
4568: printf(" %lf",param[i][j][k]);
4569: fprintf(ficlog," %lf",param[i][j][k]);
4570: }
4571: else
4572: fprintf(ficlog," %lf",param[i][j][k]);
4573: fprintf(ficparo," %lf",param[i][j][k]);
4574: }
4575: fscanf(ficpar,"\n");
4576: numlinepar++;
4577: if(mle==1)
4578: printf("\n");
4579: fprintf(ficlog,"\n");
4580: fprintf(ficparo,"\n");
4581: }
4582: }
4583: fflush(ficlog);
1.85 brouard 4584:
1.98 brouard 4585: p=param[1][1];
4586:
4587: /* Reads comments: lines beginning with '#' */
4588: while((c=getc(ficpar))=='#' && c!= EOF){
4589: ungetc(c,ficpar);
4590: fgets(line, MAXLINE, ficpar);
4591: numlinepar++;
4592: puts(line);
4593: fputs(line,ficparo);
4594: fputs(line,ficlog);
4595: }
1.53 brouard 4596: ungetc(c,ficpar);
4597:
1.98 brouard 4598: for(i=1; i <=nlstate; i++){
4599: for(j=1; j <=nlstate+ndeath-1; j++){
4600: fscanf(ficpar,"%1d%1d",&i1,&j1);
4601: if ((i1-i)*(j1-j)!=0){
4602: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
4603: exit(1);
4604: }
4605: printf("%1d%1d",i,j);
4606: fprintf(ficparo,"%1d%1d",i1,j1);
4607: fprintf(ficlog,"%1d%1d",i1,j1);
4608: for(k=1; k<=ncovmodel;k++){
4609: fscanf(ficpar,"%le",&delti3[i][j][k]);
4610: printf(" %le",delti3[i][j][k]);
4611: fprintf(ficparo," %le",delti3[i][j][k]);
4612: fprintf(ficlog," %le",delti3[i][j][k]);
4613: }
4614: fscanf(ficpar,"\n");
4615: numlinepar++;
4616: printf("\n");
4617: fprintf(ficparo,"\n");
4618: fprintf(ficlog,"\n");
1.85 brouard 4619: }
1.53 brouard 4620: }
1.98 brouard 4621: fflush(ficlog);
1.85 brouard 4622:
1.98 brouard 4623: delti=delti3[1][1];
1.74 brouard 4624:
4625:
1.98 brouard 4626: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
1.53 brouard 4627:
1.98 brouard 4628: /* Reads comments: lines beginning with '#' */
4629: while((c=getc(ficpar))=='#' && c!= EOF){
4630: ungetc(c,ficpar);
4631: fgets(line, MAXLINE, ficpar);
4632: numlinepar++;
4633: puts(line);
4634: fputs(line,ficparo);
4635: fputs(line,ficlog);
4636: }
1.53 brouard 4637: ungetc(c,ficpar);
4638:
1.98 brouard 4639: matcov=matrix(1,npar,1,npar);
4640: for(i=1; i <=npar; i++){
4641: fscanf(ficpar,"%s",&str);
4642: if(mle==1)
4643: printf("%s",str);
4644: fprintf(ficlog,"%s",str);
4645: fprintf(ficparo,"%s",str);
4646: for(j=1; j <=i; j++){
4647: fscanf(ficpar," %le",&matcov[i][j]);
4648: if(mle==1){
4649: printf(" %.5le",matcov[i][j]);
4650: }
4651: fprintf(ficlog," %.5le",matcov[i][j]);
4652: fprintf(ficparo," %.5le",matcov[i][j]);
1.53 brouard 4653: }
1.98 brouard 4654: fscanf(ficpar,"\n");
4655: numlinepar++;
4656: if(mle==1)
4657: printf("\n");
4658: fprintf(ficlog,"\n");
4659: fprintf(ficparo,"\n");
1.53 brouard 4660: }
1.98 brouard 4661: for(i=1; i <=npar; i++)
4662: for(j=i+1;j<=npar;j++)
4663: matcov[i][j]=matcov[j][i];
4664:
1.53 brouard 4665: if(mle==1)
4666: printf("\n");
4667: fprintf(ficlog,"\n");
1.98 brouard 4668:
4669: fflush(ficlog);
4670:
4671: /*-------- Rewriting parameter file ----------*/
4672: strcpy(rfileres,"r"); /* "Rparameterfile */
4673: strcat(rfileres,optionfilefiname); /* Parameter file first name*/
4674: strcat(rfileres,"."); /* */
4675: strcat(rfileres,optionfilext); /* Other files have txt extension */
4676: if((ficres =fopen(rfileres,"w"))==NULL) {
4677: printf("Problem writing new parameter file: %s\n", fileres);goto end;
4678: fprintf(ficlog,"Problem writing new parameter file: %s\n", fileres);goto end;
4679: }
4680: fprintf(ficres,"#%s\n",version);
4681: } /* End of mle != -3 */
1.53 brouard 4682:
1.59 brouard 4683: /*-------- data file ----------*/
4684: if((fic=fopen(datafile,"r"))==NULL) {
4685: printf("Problem with datafile: %s\n", datafile);goto end;
4686: fprintf(ficlog,"Problem with datafile: %s\n", datafile);goto end;
4687: }
4688:
4689: n= lastobs;
4690: severity = vector(1,maxwav);
4691: outcome=imatrix(1,maxwav+1,1,n);
1.85 brouard 4692: num=lvector(1,n);
1.59 brouard 4693: moisnais=vector(1,n);
4694: annais=vector(1,n);
4695: moisdc=vector(1,n);
4696: andc=vector(1,n);
4697: agedc=vector(1,n);
4698: cod=ivector(1,n);
4699: weight=vector(1,n);
4700: for(i=1;i<=n;i++) weight[i]=1.0; /* Equal weights, 1 by default */
4701: mint=matrix(1,maxwav,1,n);
4702: anint=matrix(1,maxwav,1,n);
4703: s=imatrix(1,maxwav+1,1,n);
4704: tab=ivector(1,NCOVMAX);
4705: ncodemax=ivector(1,8);
4706:
4707: i=1;
1.109 brouard 4708: linei=0;
1.111 brouard 4709: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
1.109 brouard 4710: linei=linei+1;
4711: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
1.111 brouard 4712: if(line[j] == '\t')
4713: line[j] = ' ';
4714: }
4715: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
4716: ;
4717: };
4718: line[j+1]=0; /* Trims blanks at end of line */
4719: if(line[0]=='#'){
4720: fprintf(ficlog,"Comment line\n%s\n",line);
4721: printf("Comment line\n%s\n",line);
4722: continue;
4723: }
1.109 brouard 4724:
1.111 brouard 4725: for (j=maxwav;j>=1;j--){
1.110 brouard 4726: cutv(stra, strb,line,' ');
1.111 brouard 4727: errno=0;
4728: lval=strtol(strb,&endptr,10);
4729: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
4730: if( strb[0]=='\0' || (*endptr != '\0')){
4731: 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);
1.109 brouard 4732: exit(1);
4733: }
1.111 brouard 4734: s[j][i]=lval;
4735:
1.109 brouard 4736: strcpy(line,stra);
1.111 brouard 4737: cutv(stra, strb,line,' ');
1.110 brouard 4738: if(iout=sscanf(strb,"%d/%d",&month, &year) != 0){
1.109 brouard 4739: }
1.111 brouard 4740: else if(iout=sscanf(strb,"%s.") != 0){
1.110 brouard 4741: month=99;
4742: year=9999;
4743: }else{
1.111 brouard 4744: 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);
1.109 brouard 4745: exit(1);
4746: }
1.111 brouard 4747: anint[j][i]= (double) year;
4748: mint[j][i]= (double)month;
1.109 brouard 4749: strcpy(line,stra);
1.111 brouard 4750: } /* ENd Waves */
4751:
4752: cutv(stra, strb,line,' ');
4753: if(iout=sscanf(strb,"%d/%d",&month, &year) != 0){
4754: }
4755: else if(iout=sscanf(strb,"%s.",dummy) != 0){
4756: month=99;
4757: year=9999;
4758: }else{
4759: 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);
4760: exit(1);
4761: }
4762: andc[i]=(double) year;
4763: moisdc[i]=(double) month;
4764: strcpy(line,stra);
4765:
4766: cutv(stra, strb,line,' ');
4767: if(iout=sscanf(strb,"%d/%d",&month, &year) != 0){
4768: }
4769: else if(iout=sscanf(strb,"%s.") != 0){
4770: month=99;
4771: year=9999;
4772: }else{
4773: 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);
4774: exit(1);
4775: }
4776: annais[i]=(double)(year);
4777: moisnais[i]=(double)(month);
4778: strcpy(line,stra);
4779:
4780: cutv(stra, strb,line,' ');
4781: errno=0;
4782: lval=strtol(strb,&endptr,10);
4783: if( strb[0]=='\0' || (*endptr != '\0')){
4784: printf("Error reading data around '%d' at line number %ld %s for individual %d\nShould be a weight. Exiting.\n",lval, i,line,linei);
4785: exit(1);
4786: }
4787: weight[i]=(double)(lval);
4788: strcpy(line,stra);
4789:
4790: for (j=ncovcol;j>=1;j--){
1.109 brouard 4791: cutv(stra, strb,line,' ');
4792: errno=0;
4793: lval=strtol(strb,&endptr,10);
4794: if( strb[0]=='\0' || (*endptr != '\0')){
1.111 brouard 4795: 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);
4796: exit(1);
4797: }
4798: if(lval <-1 || lval >1){
4799: printf("Error reading data around '%d' at line number %ld %s for individual %d, '%s'\nShould be a value of the %d covar (meaning 0 for the reference or 1. IMaCh does not build design variables, do it your self). Exiting.\n",lval,linei, i,line,j);
1.109 brouard 4800: exit(1);
4801: }
1.111 brouard 4802: covar[j][i]=(double)(lval);
1.109 brouard 4803: strcpy(line,stra);
1.111 brouard 4804: }
4805: lstra=strlen(stra);
4806:
4807: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
4808: stratrunc = &(stra[lstra-9]);
4809: num[i]=atol(stratrunc);
4810: }
4811: else
4812: num[i]=atol(stra);
4813: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
4814: 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;}*/
4815:
4816: i=i+1;
1.109 brouard 4817: } /* End loop reading data */
1.113 brouard 4818: fclose(fic);
1.59 brouard 4819: /* printf("ii=%d", ij);
4820: scanf("%d",i);*/
1.53 brouard 4821: imx=i-1; /* Number of individuals */
4822:
4823: /* for (i=1; i<=imx; i++){
4824: if ((s[1][i]==3) && (s[2][i]==2)) s[2][i]=3;
4825: if ((s[2][i]==3) && (s[3][i]==2)) s[3][i]=3;
4826: if ((s[3][i]==3) && (s[4][i]==2)) s[4][i]=3;
4827: }*/
4828: /* for (i=1; i<=imx; i++){
4829: if (s[4][i]==9) s[4][i]=-1;
1.85 brouard 4830: 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]));}*/
1.53 brouard 4831:
1.106 brouard 4832: /* for (i=1; i<=imx; i++) */
1.53 brouard 4833:
1.71 brouard 4834: /*if ((s[3][i]==3) || (s[4][i]==3)) weight[i]=0.08;
4835: else weight[i]=1;*/
4836:
1.106 brouard 4837: /* Calculation of the number of parameters from char model */
1.53 brouard 4838: Tvar=ivector(1,15); /* stores the number n of the covariates in Vm+Vn at 1 and m at 2 */
4839: Tprod=ivector(1,15);
4840: Tvaraff=ivector(1,15);
4841: Tvard=imatrix(1,15,1,2);
4842: Tage=ivector(1,15);
4843:
1.58 lievre 4844: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.53 brouard 4845: j=0, j1=0, k1=1, k2=1;
1.58 lievre 4846: j=nbocc(model,'+'); /* j=Number of '+' */
4847: j1=nbocc(model,'*'); /* j1=Number of '*' */
4848: cptcovn=j+1;
4849: cptcovprod=j1; /*Number of products */
1.53 brouard 4850:
4851: strcpy(modelsav,model);
4852: if ((strcmp(model,"age")==0) || (strcmp(model,"age*age")==0)){
4853: printf("Error. Non available option model=%s ",model);
4854: fprintf(ficlog,"Error. Non available option model=%s ",model);
4855: goto end;
4856: }
4857:
1.59 brouard 4858: /* This loop fills the array Tvar from the string 'model'.*/
1.58 lievre 4859:
1.53 brouard 4860: for(i=(j+1); i>=1;i--){
4861: cutv(stra,strb,modelsav,'+'); /* keeps in strb after the last + */
1.59 brouard 4862: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
1.53 brouard 4863: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
4864: /*scanf("%d",i);*/
4865: if (strchr(strb,'*')) { /* Model includes a product */
4866: cutv(strd,strc,strb,'*'); /* strd*strc Vm*Vn (if not *age)*/
4867: if (strcmp(strc,"age")==0) { /* Vn*age */
4868: cptcovprod--;
4869: cutv(strb,stre,strd,'V');
4870: Tvar[i]=atoi(stre); /* computes n in Vn and stores in Tvar*/
4871: cptcovage++;
4872: Tage[cptcovage]=i;
4873: /*printf("stre=%s ", stre);*/
4874: }
4875: else if (strcmp(strd,"age")==0) { /* or age*Vn */
4876: cptcovprod--;
4877: cutv(strb,stre,strc,'V');
4878: Tvar[i]=atoi(stre);
4879: cptcovage++;
4880: Tage[cptcovage]=i;
4881: }
4882: else { /* Age is not in the model */
4883: cutv(strb,stre,strc,'V'); /* strc= Vn, stre is n*/
4884: Tvar[i]=ncovcol+k1;
4885: cutv(strb,strc,strd,'V'); /* strd was Vm, strc is m */
4886: Tprod[k1]=i;
4887: Tvard[k1][1]=atoi(strc); /* m*/
4888: Tvard[k1][2]=atoi(stre); /* n */
4889: Tvar[cptcovn+k2]=Tvard[k1][1];
4890: Tvar[cptcovn+k2+1]=Tvard[k1][2];
4891: for (k=1; k<=lastobs;k++)
4892: covar[ncovcol+k1][k]=covar[atoi(stre)][k]*covar[atoi(strc)][k];
4893: k1++;
4894: k2=k2+2;
4895: }
4896: }
4897: else { /* no more sum */
4898: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
4899: /* scanf("%d",i);*/
4900: cutv(strd,strc,strb,'V');
4901: Tvar[i]=atoi(strc);
4902: }
4903: strcpy(modelsav,stra);
4904: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
4905: scanf("%d",i);*/
4906: } /* end of loop + */
4907: } /* end model */
4908:
1.58 lievre 4909: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
4910: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
4911:
1.53 brouard 4912: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
4913: printf("cptcovprod=%d ", cptcovprod);
4914: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
1.58 lievre 4915:
1.113 brouard 4916: scanf("%d ",i);*/
1.53 brouard 4917:
4918: /* if(mle==1){*/
1.59 brouard 4919: if (weightopt != 1) { /* Maximisation without weights*/
4920: for(i=1;i<=n;i++) weight[i]=1.0;
4921: }
1.53 brouard 4922: /*-calculation of age at interview from date of interview and age at death -*/
1.59 brouard 4923: agev=matrix(1,maxwav,1,imx);
1.53 brouard 4924:
1.59 brouard 4925: for (i=1; i<=imx; i++) {
4926: for(m=2; (m<= maxwav); m++) {
1.76 brouard 4927: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
1.59 brouard 4928: anint[m][i]=9999;
4929: s[m][i]=-1;
4930: }
1.76 brouard 4931: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.91 brouard 4932: nberr++;
1.85 brouard 4933: 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);
4934: 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);
1.76 brouard 4935: s[m][i]=-1;
4936: }
4937: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.91 brouard 4938: nberr++;
1.85 brouard 4939: 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]);
4940: 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]);
1.84 brouard 4941: s[m][i]=-1; /* We prefer to skip it (and to skip it in version 0.8a1 too */
1.76 brouard 4942: }
1.53 brouard 4943: }
1.59 brouard 4944: }
1.53 brouard 4945:
1.59 brouard 4946: for (i=1; i<=imx; i++) {
4947: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
1.71 brouard 4948: for(m=firstpass; (m<= lastpass); m++){
1.105 lievre 4949: if(s[m][i] >0 || s[m][i]==-2 || s[m][i]==-4 || s[m][i]==-5){
1.59 brouard 4950: if (s[m][i] >= nlstate+1) {
4951: if(agedc[i]>0)
1.76 brouard 4952: if((int)moisdc[i]!=99 && (int)andc[i]!=9999)
1.69 brouard 4953: agev[m][i]=agedc[i];
1.59 brouard 4954: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
4955: else {
1.76 brouard 4956: if ((int)andc[i]!=9999){
1.91 brouard 4957: nbwarn++;
1.85 brouard 4958: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
4959: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
1.59 brouard 4960: agev[m][i]=-1;
1.53 brouard 4961: }
4962: }
1.70 brouard 4963: }
1.69 brouard 4964: else if(s[m][i] !=9){ /* Standard case, age in fractional
1.108 lievre 4965: years but with the precision of a month */
1.59 brouard 4966: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
1.76 brouard 4967: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
1.59 brouard 4968: agev[m][i]=1;
4969: else if(agev[m][i] <agemin){
4970: agemin=agev[m][i];
4971: /*printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], agemin);*/
1.53 brouard 4972: }
1.59 brouard 4973: else if(agev[m][i] >agemax){
4974: agemax=agev[m][i];
4975: /* printf(" anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.0f\n",m,i,anint[m][i], i,annais[i], agemax);*/
1.53 brouard 4976: }
1.59 brouard 4977: /*agev[m][i]=anint[m][i]-annais[i];*/
4978: /* agev[m][i] = age[i]+2*m;*/
1.53 brouard 4979: }
1.59 brouard 4980: else { /* =9 */
1.53 brouard 4981: agev[m][i]=1;
1.59 brouard 4982: s[m][i]=-1;
4983: }
1.53 brouard 4984: }
1.59 brouard 4985: else /*= 0 Unknown */
4986: agev[m][i]=1;
4987: }
1.53 brouard 4988:
1.59 brouard 4989: }
4990: for (i=1; i<=imx; i++) {
1.71 brouard 4991: for(m=firstpass; (m<=lastpass); m++){
1.59 brouard 4992: if (s[m][i] > (nlstate+ndeath)) {
1.91 brouard 4993: nberr++;
1.59 brouard 4994: 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);
4995: 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);
4996: goto end;
1.53 brouard 4997: }
4998: }
1.59 brouard 4999: }
1.53 brouard 5000:
1.71 brouard 5001: /*for (i=1; i<=imx; i++){
5002: for (m=firstpass; (m<lastpass); m++){
1.85 brouard 5003: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
1.71 brouard 5004: }
5005:
5006: }*/
5007:
1.97 lievre 5008:
1.59 brouard 5009: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, agemin, agemax);
5010: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, agemin, agemax);
5011:
1.98 brouard 5012: agegomp=(int)agemin;
1.59 brouard 5013: free_vector(severity,1,maxwav);
5014: free_imatrix(outcome,1,maxwav+1,1,n);
5015: free_vector(moisnais,1,n);
5016: free_vector(annais,1,n);
5017: /* free_matrix(mint,1,maxwav,1,n);
5018: free_matrix(anint,1,maxwav,1,n);*/
5019: free_vector(moisdc,1,n);
5020: free_vector(andc,1,n);
1.53 brouard 5021:
5022:
1.59 brouard 5023: wav=ivector(1,imx);
5024: dh=imatrix(1,lastpass-firstpass+1,1,imx);
5025: bh=imatrix(1,lastpass-firstpass+1,1,imx);
5026: mw=imatrix(1,lastpass-firstpass+1,1,imx);
1.69 brouard 5027:
1.59 brouard 5028: /* Concatenates waves */
5029: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.53 brouard 5030:
1.59 brouard 5031: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
1.53 brouard 5032:
1.59 brouard 5033: Tcode=ivector(1,100);
5034: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
5035: ncodemax[1]=1;
5036: if (cptcovn > 0) tricode(Tvar,nbcode,imx);
1.53 brouard 5037:
1.59 brouard 5038: codtab=imatrix(1,100,1,10); /* Cross tabulation to get the order of
5039: the estimations*/
5040: h=0;
5041: m=pow(2,cptcoveff);
1.53 brouard 5042:
1.59 brouard 5043: for(k=1;k<=cptcoveff; k++){
5044: for(i=1; i <=(m/pow(2,k));i++){
5045: for(j=1; j <= ncodemax[k]; j++){
5046: for(cpt=1; cpt <=(m/pow(2,cptcoveff+1-k)); cpt++){
5047: h++;
5048: if (h>m) h=1;codtab[h][k]=j;codtab[h][Tvar[k]]=j;
5049: /* printf("h=%d k=%d j=%d codtab[h][k]=%d tvar[k]=%d \n",h, k,j,codtab[h][k],Tvar[k]);*/
5050: }
5051: }
5052: }
5053: }
5054: /* printf("codtab[1][2]=%d codtab[2][2]=%d",codtab[1][2],codtab[2][2]);
5055: codtab[1][2]=1;codtab[2][2]=2; */
5056: /* for(i=1; i <=m ;i++){
5057: for(k=1; k <=cptcovn; k++){
5058: printf("i=%d k=%d %d %d ",i,k,codtab[i][k], cptcoveff);
5059: }
5060: printf("\n");
1.53 brouard 5061: }
1.59 brouard 5062: scanf("%d",i);*/
1.53 brouard 5063:
1.86 brouard 5064: /*------------ gnuplot -------------*/
5065: strcpy(optionfilegnuplot,optionfilefiname);
1.98 brouard 5066: if(mle==-3)
5067: strcat(optionfilegnuplot,"-mort");
1.86 brouard 5068: strcat(optionfilegnuplot,".gp");
1.98 brouard 5069:
1.86 brouard 5070: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
5071: printf("Problem with file %s",optionfilegnuplot);
5072: }
5073: else{
5074: fprintf(ficgp,"\n# %s\n", version);
5075: fprintf(ficgp,"# %s\n", optionfilegnuplot);
5076: fprintf(ficgp,"set missing 'NaNq'\n");
5077: }
1.88 brouard 5078: /* fclose(ficgp);*/
1.86 brouard 5079: /*--------- index.htm --------*/
5080:
1.91 brouard 5081: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
1.98 brouard 5082: if(mle==-3)
5083: strcat(optionfilehtm,"-mort");
1.86 brouard 5084: strcat(optionfilehtm,".htm");
5085: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
5086: printf("Problem with %s \n",optionfilehtm), exit(0);
5087: }
5088:
1.91 brouard 5089: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
5090: strcat(optionfilehtmcov,"-cov.htm");
5091: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
5092: printf("Problem with %s \n",optionfilehtmcov), exit(0);
5093: }
5094: else{
5095: fprintf(fichtmcov,"<body>\n<title>IMaCh Cov %s</title>\n <font size=\"2\">%s <br> %s</font> \
5096: <hr size=\"2\" color=\"#EC5E5E\"> \n\
5097: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>\n",\
5098: fileres,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
5099: }
5100:
1.87 brouard 5101: fprintf(fichtm,"<body>\n<title>IMaCh %s</title>\n <font size=\"2\">%s <br> %s</font> \
1.86 brouard 5102: <hr size=\"2\" color=\"#EC5E5E\"> \n\
5103: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>\n\
5104: \n\
5105: <hr size=\"2\" color=\"#EC5E5E\">\
5106: <ul><li><h4>Parameter files</h4>\n\
1.117 ! brouard 5107: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
1.86 brouard 5108: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
5109: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
1.87 brouard 5110: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
1.86 brouard 5111: - Date and time at start: %s</ul>\n",\
1.91 brouard 5112: fileres,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
1.117 ! brouard 5113: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
1.91 brouard 5114: fileres,fileres,\
1.88 brouard 5115: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
1.87 brouard 5116: fflush(fichtm);
1.86 brouard 5117:
1.88 brouard 5118: strcpy(pathr,path);
5119: strcat(pathr,optionfilefiname);
5120: chdir(optionfilefiname); /* Move to directory named optionfile */
5121:
1.59 brouard 5122: /* Calculates basic frequencies. Computes observed prevalence at single age
5123: and prints on file fileres'p'. */
1.105 lievre 5124: freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart);
1.53 brouard 5125:
1.88 brouard 5126: fprintf(fichtm,"\n");
5127: fprintf(fichtm,"<br>Total number of observations=%d <br>\n\
1.86 brouard 5128: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
5129: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
1.88 brouard 5130: imx,agemin,agemax,jmin,jmax,jmean);
5131: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.60 brouard 5132: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
5133: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
5134: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
5135: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.53 brouard 5136:
5137:
1.59 brouard 5138: /* For Powell, parameters are in a vector p[] starting at p[1]
5139: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
5140: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
1.53 brouard 5141:
1.86 brouard 5142: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.110 brouard 5143:
1.98 brouard 5144: if (mle==-3){
5145: ximort=matrix(1,NDIM,1,NDIM);
5146: cens=ivector(1,n);
5147: ageexmed=vector(1,n);
5148: agecens=vector(1,n);
5149: dcwave=ivector(1,n);
5150:
5151: for (i=1; i<=imx; i++){
5152: dcwave[i]=-1;
1.110 brouard 5153: for (m=firstpass; m<=lastpass; m++)
5154: if (s[m][i]>nlstate) {
5155: dcwave[i]=m;
1.98 brouard 5156: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
5157: break;
5158: }
5159: }
5160:
5161: for (i=1; i<=imx; i++) {
5162: if (wav[i]>0){
5163: ageexmed[i]=agev[mw[1][i]][i];
1.110 brouard 5164: j=wav[i];
5165: agecens[i]=1.;
5166:
5167: if (ageexmed[i]> 1 && wav[i] > 0){
5168: agecens[i]=agev[mw[j][i]][i];
5169: cens[i]= 1;
5170: }else if (ageexmed[i]< 1)
5171: cens[i]= -1;
5172: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
5173: cens[i]=0 ;
1.98 brouard 5174: }
5175: else cens[i]=-1;
5176: }
5177:
5178: for (i=1;i<=NDIM;i++) {
5179: for (j=1;j<=NDIM;j++)
5180: ximort[i][j]=(i == j ? 1.0 : 0.0);
5181: }
1.110 brouard 5182:
1.111 brouard 5183: p[1]=0.0268; p[NDIM]=0.083;
1.98 brouard 5184: /*printf("%lf %lf", p[1], p[2]);*/
5185:
5186:
1.110 brouard 5187: printf("Powell\n"); fprintf(ficlog,"Powell\n");
5188: strcpy(filerespow,"pow-mort");
5189: strcat(filerespow,fileres);
5190: if((ficrespow=fopen(filerespow,"w"))==NULL) {
5191: printf("Problem with resultfile: %s\n", filerespow);
5192: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
5193: }
5194: fprintf(ficrespow,"# Powell\n# iter -2*LL");
5195: /* for (i=1;i<=nlstate;i++)
5196: for(j=1;j<=nlstate+ndeath;j++)
5197: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
5198: */
5199: fprintf(ficrespow,"\n");
5200:
1.98 brouard 5201: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
5202: fclose(ficrespow);
1.53 brouard 5203:
1.110 brouard 5204: hesscov(matcov, p, NDIM, delti, 1e-4, gompertz);
1.53 brouard 5205:
1.98 brouard 5206: for(i=1; i <=NDIM; i++)
5207: for(j=i+1;j<=NDIM;j++)
5208: matcov[i][j]=matcov[j][i];
5209:
5210: printf("\nCovariance matrix\n ");
5211: for(i=1; i <=NDIM; i++) {
5212: for(j=1;j<=NDIM;j++){
5213: printf("%f ",matcov[i][j]);
1.95 brouard 5214: }
1.98 brouard 5215: printf("\n ");
5216: }
5217:
5218: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
5219: for (i=1;i<=NDIM;i++)
5220: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.105 lievre 5221:
1.110 brouard 5222: lsurv=vector(1,AGESUP);
1.105 lievre 5223: lpop=vector(1,AGESUP);
5224: tpop=vector(1,AGESUP);
5225: lsurv[agegomp]=100000;
1.110 brouard 5226:
5227: for (k=agegomp;k<=AGESUP;k++) {
1.105 lievre 5228: agemortsup=k;
5229: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
5230: }
1.110 brouard 5231:
5232: for (k=agegomp;k<agemortsup;k++)
1.105 lievre 5233: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
1.110 brouard 5234:
1.105 lievre 5235: for (k=agegomp;k<agemortsup;k++){
5236: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
5237: sumlpop=sumlpop+lpop[k];
5238: }
1.110 brouard 5239:
5240: tpop[agegomp]=sumlpop;
1.105 lievre 5241: for (k=agegomp;k<(agemortsup-3);k++){
5242: /* tpop[k+1]=2;*/
5243: tpop[k+1]=tpop[k]-lpop[k];
1.110 brouard 5244: }
5245:
5246:
5247: printf("\nAge lx qx dx Lx Tx e(x)\n");
1.105 lievre 5248: for (k=agegomp;k<(agemortsup-2);k++)
5249: 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]);
1.110 brouard 5250:
5251:
1.98 brouard 5252: replace_back_to_slash(pathc,path); /* Even gnuplot wants a / */
5253: printinggnuplotmort(fileres, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
5254:
5255: printinghtmlmort(fileres,title,datafile, firstpass, lastpass, \
5256: stepm, weightopt,\
1.105 lievre 5257: model,imx,p,matcov,agemortsup);
1.110 brouard 5258:
1.105 lievre 5259: free_vector(lsurv,1,AGESUP);
5260: free_vector(lpop,1,AGESUP);
5261: free_vector(tpop,1,AGESUP);
1.98 brouard 5262: } /* Endof if mle==-3 */
1.110 brouard 5263:
1.98 brouard 5264: else{ /* For mle >=1 */
5265:
5266: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
5267: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
5268: for (k=1; k<=npar;k++)
5269: printf(" %d %8.5f",k,p[k]);
5270: printf("\n");
5271: globpr=1; /* to print the contributions */
5272: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
5273: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
5274: for (k=1; k<=npar;k++)
5275: printf(" %d %8.5f",k,p[k]);
5276: printf("\n");
5277: if(mle>=1){ /* Could be 1 or 2 */
5278: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
1.59 brouard 5279: }
1.98 brouard 5280:
5281: /*--------- results files --------------*/
5282: 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);
5283:
5284:
5285: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
5286: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
5287: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
5288: for(i=1,jk=1; i <=nlstate; i++){
5289: for(k=1; k <=(nlstate+ndeath); k++){
5290: if (k != i) {
5291: printf("%d%d ",i,k);
5292: fprintf(ficlog,"%d%d ",i,k);
5293: fprintf(ficres,"%1d%1d ",i,k);
5294: for(j=1; j <=ncovmodel; j++){
5295: printf("%f ",p[jk]);
5296: fprintf(ficlog,"%f ",p[jk]);
5297: fprintf(ficres,"%f ",p[jk]);
5298: jk++;
5299: }
5300: printf("\n");
5301: fprintf(ficlog,"\n");
5302: fprintf(ficres,"\n");
1.59 brouard 5303: }
5304: }
5305: }
1.98 brouard 5306: if(mle!=0){
5307: /* Computing hessian and covariance matrix */
5308: ftolhess=ftol; /* Usually correct */
5309: hesscov(matcov, p, npar, delti, ftolhess, func);
5310: }
5311: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
5312: printf("# Scales (for hessian or gradient estimation)\n");
5313: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
5314: for(i=1,jk=1; i <=nlstate; i++){
1.95 brouard 5315: for(j=1; j <=nlstate+ndeath; j++){
1.98 brouard 5316: if (j!=i) {
5317: fprintf(ficres,"%1d%1d",i,j);
5318: printf("%1d%1d",i,j);
5319: fprintf(ficlog,"%1d%1d",i,j);
5320: for(k=1; k<=ncovmodel;k++){
5321: printf(" %.5e",delti[jk]);
5322: fprintf(ficlog," %.5e",delti[jk]);
5323: fprintf(ficres," %.5e",delti[jk]);
5324: jk++;
1.95 brouard 5325: }
1.98 brouard 5326: printf("\n");
5327: fprintf(ficlog,"\n");
5328: fprintf(ficres,"\n");
5329: }
5330: }
5331: }
5332:
5333: 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");
5334: if(mle>=1)
5335: 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");
5336: 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");
5337: /* # 121 Var(a12)\n\ */
5338: /* # 122 Cov(b12,a12) Var(b12)\n\ */
5339: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
5340: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
5341: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
5342: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
5343: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
5344: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
5345:
5346:
5347: /* Just to have a covariance matrix which will be more understandable
5348: even is we still don't want to manage dictionary of variables
5349: */
5350: for(itimes=1;itimes<=2;itimes++){
5351: jj=0;
5352: for(i=1; i <=nlstate; i++){
5353: for(j=1; j <=nlstate+ndeath; j++){
5354: if(j==i) continue;
5355: for(k=1; k<=ncovmodel;k++){
5356: jj++;
5357: ca[0]= k+'a'-1;ca[1]='\0';
5358: if(itimes==1){
5359: if(mle>=1)
5360: printf("#%1d%1d%d",i,j,k);
5361: fprintf(ficlog,"#%1d%1d%d",i,j,k);
5362: fprintf(ficres,"#%1d%1d%d",i,j,k);
5363: }else{
5364: if(mle>=1)
5365: printf("%1d%1d%d",i,j,k);
5366: fprintf(ficlog,"%1d%1d%d",i,j,k);
5367: fprintf(ficres,"%1d%1d%d",i,j,k);
5368: }
5369: ll=0;
5370: for(li=1;li <=nlstate; li++){
5371: for(lj=1;lj <=nlstate+ndeath; lj++){
5372: if(lj==li) continue;
5373: for(lk=1;lk<=ncovmodel;lk++){
5374: ll++;
5375: if(ll<=jj){
5376: cb[0]= lk +'a'-1;cb[1]='\0';
5377: if(ll<jj){
5378: if(itimes==1){
5379: if(mle>=1)
5380: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
5381: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
5382: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
5383: }else{
5384: if(mle>=1)
5385: printf(" %.5e",matcov[jj][ll]);
5386: fprintf(ficlog," %.5e",matcov[jj][ll]);
5387: fprintf(ficres," %.5e",matcov[jj][ll]);
5388: }
1.95 brouard 5389: }else{
1.98 brouard 5390: if(itimes==1){
5391: if(mle>=1)
5392: printf(" Var(%s%1d%1d)",ca,i,j);
5393: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
5394: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
5395: }else{
5396: if(mle>=1)
5397: printf(" %.5e",matcov[jj][ll]);
5398: fprintf(ficlog," %.5e",matcov[jj][ll]);
5399: fprintf(ficres," %.5e",matcov[jj][ll]);
5400: }
1.95 brouard 5401: }
5402: }
1.98 brouard 5403: } /* end lk */
5404: } /* end lj */
5405: } /* end li */
5406: if(mle>=1)
5407: printf("\n");
5408: fprintf(ficlog,"\n");
5409: fprintf(ficres,"\n");
5410: numlinepar++;
5411: } /* end k*/
5412: } /*end j */
5413: } /* end i */
5414: } /* end itimes */
5415:
5416: fflush(ficlog);
5417: fflush(ficres);
5418:
5419: while((c=getc(ficpar))=='#' && c!= EOF){
5420: ungetc(c,ficpar);
5421: fgets(line, MAXLINE, ficpar);
5422: puts(line);
5423: fputs(line,ficparo);
5424: }
1.59 brouard 5425: ungetc(c,ficpar);
1.98 brouard 5426:
5427: estepm=0;
5428: fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm);
5429: if (estepm==0 || estepm < stepm) estepm=stepm;
5430: if (fage <= 2) {
5431: bage = ageminpar;
5432: fage = agemaxpar;
5433: }
5434:
5435: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
5436: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d\n",ageminpar,agemaxpar,bage,fage, estepm);
5437: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d\n",ageminpar,agemaxpar,bage,fage, estepm);
5438:
5439: while((c=getc(ficpar))=='#' && c!= EOF){
5440: ungetc(c,ficpar);
5441: fgets(line, MAXLINE, ficpar);
5442: puts(line);
5443: fputs(line,ficparo);
5444: }
1.59 brouard 5445: ungetc(c,ficpar);
1.98 brouard 5446:
5447: 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);
5448: 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);
5449: 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);
5450: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
5451: 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);
5452:
5453: while((c=getc(ficpar))=='#' && c!= EOF){
5454: ungetc(c,ficpar);
5455: fgets(line, MAXLINE, ficpar);
5456: puts(line);
5457: fputs(line,ficparo);
5458: }
1.59 brouard 5459: ungetc(c,ficpar);
1.98 brouard 5460:
5461:
5462: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
5463: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
5464:
5465: fscanf(ficpar,"pop_based=%d\n",&popbased);
5466: fprintf(ficparo,"pop_based=%d\n",popbased);
5467: fprintf(ficres,"pop_based=%d\n",popbased);
5468:
5469: while((c=getc(ficpar))=='#' && c!= EOF){
5470: ungetc(c,ficpar);
5471: fgets(line, MAXLINE, ficpar);
5472: puts(line);
5473: fputs(line,ficparo);
5474: }
1.53 brouard 5475: ungetc(c,ficpar);
1.98 brouard 5476:
5477: 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);
5478: 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);
5479: 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);
5480: 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);
5481: 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);
5482: /* day and month of proj2 are not used but only year anproj2.*/
5483:
5484:
5485:
5486: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint);*/
5487: /*,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
5488:
5489: replace_back_to_slash(pathc,path); /* Even gnuplot wants a / */
5490: printinggnuplot(fileres, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
5491:
5492: printinghtml(fileres,title,datafile, firstpass, lastpass, stepm, weightopt,\
5493: model,imx,jmin,jmax,jmean,rfileres,popforecast,estepm,\
5494: jprev1,mprev1,anprev1,jprev2,mprev2,anprev2);
5495:
5496: /*------------ free_vector -------------*/
5497: /* chdir(path); */
1.53 brouard 5498:
1.98 brouard 5499: free_ivector(wav,1,imx);
5500: free_imatrix(dh,1,lastpass-firstpass+1,1,imx);
5501: free_imatrix(bh,1,lastpass-firstpass+1,1,imx);
5502: free_imatrix(mw,1,lastpass-firstpass+1,1,imx);
5503: free_lvector(num,1,n);
5504: free_vector(agedc,1,n);
5505: /*free_matrix(covar,0,NCOVMAX,1,n);*/
5506: /*free_matrix(covar,1,NCOVMAX,1,n);*/
5507: fclose(ficparo);
5508: fclose(ficres);
5509:
5510:
1.117 ! brouard 5511: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.98 brouard 5512:
5513: strcpy(filerespl,"pl");
5514: strcat(filerespl,fileres);
5515: if((ficrespl=fopen(filerespl,"w"))==NULL) {
1.117 ! brouard 5516: printf("Problem with period (stable) prevalence resultfile: %s\n", filerespl);goto end;
! 5517: fprintf(ficlog,"Problem with period (stable) prevalence resultfile: %s\n", filerespl);goto end;
1.98 brouard 5518: }
1.117 ! brouard 5519: printf("Computing period (stable) prevalence: result on file '%s' \n", filerespl);
! 5520: fprintf(ficlog,"Computing period (stable) prevalence: result on file '%s' \n", filerespl);
! 5521: pstamp(ficrespl);
! 5522: fprintf(ficrespl,"# Period (stable) prevalence \n");
1.98 brouard 5523: fprintf(ficrespl,"#Age ");
5524: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
5525: fprintf(ficrespl,"\n");
5526:
5527: prlim=matrix(1,nlstate,1,nlstate);
5528:
5529: agebase=ageminpar;
5530: agelim=agemaxpar;
5531: ftolpl=1.e-10;
5532: i1=cptcoveff;
5533: if (cptcovn < 1){i1=1;}
5534:
5535: for(cptcov=1,k=0;cptcov<=i1;cptcov++){
5536: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
5537: k=k+1;
5538: /*printf("cptcov=%d cptcod=%d codtab=%d nbcode=%d\n",cptcov, cptcod,Tcode[cptcode],codtab[cptcod][cptcov]);*/
5539: fprintf(ficrespl,"\n#******");
5540: printf("\n#******");
5541: fprintf(ficlog,"\n#******");
5542: for(j=1;j<=cptcoveff;j++) {
5543: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
5544: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
5545: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
5546: }
5547: fprintf(ficrespl,"******\n");
5548: printf("******\n");
5549: fprintf(ficlog,"******\n");
1.53 brouard 5550:
1.98 brouard 5551: for (age=agebase; age<=agelim; age++){
5552: prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
5553: fprintf(ficrespl,"%.0f ",age );
5554: for(j=1;j<=cptcoveff;j++)
5555: fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
5556: for(i=1; i<=nlstate;i++)
5557: fprintf(ficrespl," %.5f", prlim[i][i]);
5558: fprintf(ficrespl,"\n");
5559: }
1.53 brouard 5560: }
5561: }
1.98 brouard 5562: fclose(ficrespl);
1.53 brouard 5563:
1.98 brouard 5564: /*------------- h Pij x at various ages ------------*/
1.53 brouard 5565:
1.98 brouard 5566: strcpy(filerespij,"pij"); strcat(filerespij,fileres);
5567: if((ficrespij=fopen(filerespij,"w"))==NULL) {
5568: printf("Problem with Pij resultfile: %s\n", filerespij);goto end;
5569: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij);goto end;
5570: }
5571: printf("Computing pij: result on file '%s' \n", filerespij);
5572: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
1.53 brouard 5573:
1.98 brouard 5574: stepsize=(int) (stepm+YEARM-1)/YEARM;
5575: /*if (stepm<=24) stepsize=2;*/
1.53 brouard 5576:
1.98 brouard 5577: agelim=AGESUP;
5578: hstepm=stepsize*YEARM; /* Every year of age */
5579: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
1.53 brouard 5580:
1.98 brouard 5581: /* hstepm=1; aff par mois*/
1.117 ! brouard 5582: pstamp(ficrespij);
1.98 brouard 5583: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
5584: for(cptcov=1,k=0;cptcov<=i1;cptcov++){
5585: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
5586: k=k+1;
5587: fprintf(ficrespij,"\n#****** ");
5588: for(j=1;j<=cptcoveff;j++)
5589: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
5590: fprintf(ficrespij,"******\n");
1.53 brouard 5591:
1.98 brouard 5592: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
5593: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5594: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
1.59 brouard 5595:
1.98 brouard 5596: /* nhstepm=nhstepm*YEARM; aff par mois*/
1.59 brouard 5597:
1.98 brouard 5598: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5599: oldm=oldms;savm=savms;
5600: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
5601: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
1.53 brouard 5602: for(i=1; i<=nlstate;i++)
5603: for(j=1; j<=nlstate+ndeath;j++)
1.98 brouard 5604: fprintf(ficrespij," %1d-%1d",i,j);
5605: fprintf(ficrespij,"\n");
5606: for (h=0; h<=nhstepm; h++){
5607: fprintf(ficrespij,"%d %3.f %3.f",k,agedeb, agedeb+ h*hstepm/YEARM*stepm );
5608: for(i=1; i<=nlstate;i++)
5609: for(j=1; j<=nlstate+ndeath;j++)
5610: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
5611: fprintf(ficrespij,"\n");
5612: }
5613: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1.53 brouard 5614: fprintf(ficrespij,"\n");
5615: }
1.59 brouard 5616: }
1.53 brouard 5617: }
5618:
1.105 lievre 5619: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
1.53 brouard 5620:
1.98 brouard 5621: fclose(ficrespij);
1.53 brouard 5622:
1.98 brouard 5623: probs= ma3x(1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
5624: for(i=1;i<=AGESUP;i++)
5625: for(j=1;j<=NCOVMAX;j++)
5626: for(k=1;k<=NCOVMAX;k++)
5627: probs[i][j][k]=0.;
5628:
5629: /*---------- Forecasting ------------------*/
5630: /*if((stepm == 1) && (strcmp(model,".")==0)){*/
5631: if(prevfcast==1){
5632: /* if(stepm ==1){*/
1.70 brouard 5633: prevforecast(fileres, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);
1.74 brouard 5634: /* (popforecast==1) populforecast(fileres, anpyram,mpyram,jpyram, agemin,agemax, dateprev1, dateprev2,mobilav, agedeb, fage, popforecast, popfile, anpyram1,p, i1);*/
1.98 brouard 5635: /* } */
5636: /* else{ */
5637: /* erreur=108; */
5638: /* 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); */
5639: /* 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); */
5640: /* } */
5641: }
1.53 brouard 5642:
5643:
1.98 brouard 5644: /*---------- Health expectancies and variances ------------*/
1.53 brouard 5645:
1.98 brouard 5646: strcpy(filerest,"t");
5647: strcat(filerest,fileres);
5648: if((ficrest=fopen(filerest,"w"))==NULL) {
5649: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
5650: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
5651: }
1.117 ! brouard 5652: printf("Computing Total Life expectancies with their standard errors: file '%s' \n", filerest);
! 5653: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' \n", filerest);
1.53 brouard 5654:
5655:
1.98 brouard 5656: strcpy(filerese,"e");
5657: strcat(filerese,fileres);
5658: if((ficreseij=fopen(filerese,"w"))==NULL) {
5659: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
5660: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
5661: }
5662: printf("Computing Health Expectancies: result on file '%s' \n", filerese);
5663: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' \n", filerese);
1.68 lievre 5664:
1.117 ! brouard 5665: strcpy(fileresstde,"stde");
! 5666: strcat(fileresstde,fileres);
! 5667: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
! 5668: printf("Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
! 5669: fprintf(ficlog,"Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
! 5670: }
! 5671: printf("Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
! 5672: fprintf(ficlog,"Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
! 5673:
! 5674: strcpy(filerescve,"cve");
! 5675: strcat(filerescve,fileres);
! 5676: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
! 5677: printf("Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
! 5678: fprintf(ficlog,"Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
! 5679: }
! 5680: printf("Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
! 5681: fprintf(ficlog,"Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
! 5682:
1.98 brouard 5683: strcpy(fileresv,"v");
5684: strcat(fileresv,fileres);
5685: if((ficresvij=fopen(fileresv,"w"))==NULL) {
5686: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
5687: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
5688: }
5689: printf("Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
5690: fprintf(ficlog,"Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
1.58 lievre 5691:
1.98 brouard 5692: /* Computes prevalence between agemin (i.e minimal age computed) and no more ageminpar */
5693: prevalence(probs, agemin, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
5694: /* 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",\
5695: ageminpar, agemax, s[lastpass][imx], agev[lastpass][imx], nlstate, imx, mint[lastpass][imx],anint[lastpass][imx], dateprev1, dateprev2, firstpass, lastpass);
5696: */
1.58 lievre 5697:
1.98 brouard 5698: if (mobilav!=0) {
5699: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
5700: if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){
5701: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
5702: printf(" Error in movingaverage mobilav=%d\n",mobilav);
5703: }
1.54 brouard 5704: }
1.53 brouard 5705:
1.98 brouard 5706: for(cptcov=1,k=0;cptcov<=i1;cptcov++){
5707: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
5708: k=k+1;
5709: fprintf(ficrest,"\n#****** ");
5710: for(j=1;j<=cptcoveff;j++)
5711: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
5712: fprintf(ficrest,"******\n");
5713:
5714: fprintf(ficreseij,"\n#****** ");
1.117 ! brouard 5715: fprintf(ficresstdeij,"\n#****** ");
! 5716: fprintf(ficrescveij,"\n#****** ");
! 5717: for(j=1;j<=cptcoveff;j++) {
1.98 brouard 5718: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
1.117 ! brouard 5719: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 5720: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
! 5721: }
1.98 brouard 5722: fprintf(ficreseij,"******\n");
1.117 ! brouard 5723: fprintf(ficresstdeij,"******\n");
! 5724: fprintf(ficrescveij,"******\n");
1.98 brouard 5725:
5726: fprintf(ficresvij,"\n#****** ");
5727: for(j=1;j<=cptcoveff;j++)
5728: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
5729: fprintf(ficresvij,"******\n");
5730:
5731: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
5732: oldm=oldms;savm=savms;
1.117 ! brouard 5733: evsij(fileres, eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart);
! 5734: cvevsij(fileres, eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart);
1.53 brouard 5735:
1.98 brouard 5736: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
5737: oldm=oldms;savm=savms;
1.105 lievre 5738: varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k, estepm, cptcov,cptcod,0, mobilav, strstart);
1.98 brouard 5739: if(popbased==1){
1.105 lievre 5740: varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k, estepm, cptcov,cptcod,popbased,mobilav, strstart);
1.98 brouard 5741: }
1.53 brouard 5742:
1.117 ! brouard 5743: pstamp(ficrest);
! 5744: fprintf(ficrest,"# Total life expectancy with std error and decomposition into time to be expected in each health state ( e.. (std) ");
1.98 brouard 5745: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
5746: fprintf(ficrest,"\n");
5747:
5748: epj=vector(1,nlstate+1);
5749: for(age=bage; age <=fage ;age++){
5750: prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
5751: if (popbased==1) {
5752: if(mobilav ==0){
5753: for(i=1; i<=nlstate;i++)
5754: prlim[i][i]=probs[(int)age][i][k];
5755: }else{ /* mobilav */
5756: for(i=1; i<=nlstate;i++)
5757: prlim[i][i]=mobaverage[(int)age][i][k];
5758: }
1.53 brouard 5759: }
5760:
1.98 brouard 5761: fprintf(ficrest," %4.0f",age);
5762: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
5763: for(i=1, epj[j]=0.;i <=nlstate;i++) {
5764: epj[j] += prlim[i][i]*eij[i][j][(int)age];
5765: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
5766: }
5767: epj[nlstate+1] +=epj[j];
1.53 brouard 5768: }
5769:
1.98 brouard 5770: for(i=1, vepp=0.;i <=nlstate;i++)
5771: for(j=1;j <=nlstate;j++)
5772: vepp += vareij[i][j][(int)age];
5773: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
5774: for(j=1;j <=nlstate;j++){
5775: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
5776: }
5777: fprintf(ficrest,"\n");
1.53 brouard 5778: }
1.98 brouard 5779: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
5780: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
5781: free_vector(epj,1,nlstate+1);
5782: }
5783: }
5784: free_vector(weight,1,n);
5785: free_imatrix(Tvard,1,15,1,2);
5786: free_imatrix(s,1,maxwav+1,1,n);
5787: free_matrix(anint,1,maxwav,1,n);
5788: free_matrix(mint,1,maxwav,1,n);
5789: free_ivector(cod,1,n);
5790: free_ivector(tab,1,NCOVMAX);
5791: fclose(ficreseij);
1.117 ! brouard 5792: fclose(ficresstdeij);
! 5793: fclose(ficrescveij);
1.98 brouard 5794: fclose(ficresvij);
5795: fclose(ficrest);
5796: fclose(ficpar);
5797:
1.117 ! brouard 5798: /*------- Variance of period (stable) prevalence------*/
1.98 brouard 5799:
5800: strcpy(fileresvpl,"vpl");
5801: strcat(fileresvpl,fileres);
5802: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
1.117 ! brouard 5803: printf("Problem with variance of period (stable) prevalence resultfile: %s\n", fileresvpl);
1.98 brouard 5804: exit(0);
5805: }
1.117 ! brouard 5806: printf("Computing Variance-covariance of period (stable) prevalence: file '%s' \n", fileresvpl);
1.98 brouard 5807:
5808: for(cptcov=1,k=0;cptcov<=i1;cptcov++){
5809: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
5810: k=k+1;
5811: fprintf(ficresvpl,"\n#****** ");
5812: for(j=1;j<=cptcoveff;j++)
5813: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
5814: fprintf(ficresvpl,"******\n");
5815:
5816: varpl=matrix(1,nlstate,(int) bage, (int) fage);
5817: oldm=oldms;savm=savms;
1.105 lievre 5818: varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k,strstart);
1.98 brouard 5819: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
1.53 brouard 5820: }
1.98 brouard 5821: }
1.53 brouard 5822:
1.98 brouard 5823: fclose(ficresvpl);
1.53 brouard 5824:
1.98 brouard 5825: /*---------- End : free ----------------*/
5826: if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
5827: free_ma3x(probs,1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
5828:
5829: } /* mle==-3 arrives here for freeing */
1.113 brouard 5830: free_matrix(prlim,1,nlstate,1,nlstate);
1.98 brouard 5831: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
5832: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
5833: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
5834: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
5835: free_matrix(covar,0,NCOVMAX,1,n);
5836: free_matrix(matcov,1,npar,1,npar);
5837: /*free_vector(delti,1,npar);*/
5838: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
5839: free_matrix(agev,1,maxwav,1,imx);
5840: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
5841:
5842: free_ivector(ncodemax,1,8);
5843: free_ivector(Tvar,1,15);
5844: free_ivector(Tprod,1,15);
5845: free_ivector(Tvaraff,1,15);
5846: free_ivector(Tage,1,15);
5847: free_ivector(Tcode,1,100);
1.74 brouard 5848:
1.113 brouard 5849: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
5850: free_imatrix(codtab,1,100,1,10);
1.88 brouard 5851: fflush(fichtm);
5852: fflush(ficgp);
1.53 brouard 5853:
5854:
1.91 brouard 5855: if((nberr >0) || (nbwarn>0)){
1.95 brouard 5856: printf("End of Imach with %d errors and/or %d warnings\n",nberr,nbwarn);
1.91 brouard 5857: fprintf(ficlog,"End of Imach with %d errors and/or warnings %d\n",nberr,nbwarn);
1.53 brouard 5858: }else{
1.91 brouard 5859: printf("End of Imach\n");
5860: fprintf(ficlog,"End of Imach\n");
1.53 brouard 5861: }
5862: printf("See log file on %s\n",filelog);
5863: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.85 brouard 5864: (void) gettimeofday(&end_time,&tzp);
5865: tm = *localtime(&end_time.tv_sec);
5866: tmg = *gmtime(&end_time.tv_sec);
1.88 brouard 5867: strcpy(strtend,asctime(&tm));
1.105 lievre 5868: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
1.94 brouard 5869: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.91 brouard 5870: printf("Total time used %s\n", asc_diff_time(end_time.tv_sec -start_time.tv_sec,tmpout));
1.85 brouard 5871:
1.91 brouard 5872: printf("Total time was %d Sec.\n", end_time.tv_sec -start_time.tv_sec);
5873: fprintf(ficlog,"Total time used %s\n", asc_diff_time(end_time.tv_sec -start_time.tv_sec,tmpout));
5874: fprintf(ficlog,"Total time was %d Sec.\n", end_time.tv_sec -start_time.tv_sec);
1.85 brouard 5875: /* printf("Total time was %d uSec.\n", total_usecs);*/
1.87 brouard 5876: /* if(fileappend(fichtm,optionfilehtm)){ */
1.88 brouard 5877: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>",strstart, strtend);
1.87 brouard 5878: fclose(fichtm);
1.91 brouard 5879: fclose(fichtmcov);
1.88 brouard 5880: fclose(ficgp);
1.91 brouard 5881: fclose(ficlog);
1.53 brouard 5882: /*------ End -----------*/
5883:
1.88 brouard 5884: chdir(path);
1.107 brouard 5885: /*strcat(plotcmd,CHARSEPARATOR);*/
1.111 brouard 5886: sprintf(plotcmd,"gnuplot");
1.107 brouard 5887: #ifndef UNIX
1.112 brouard 5888: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
1.107 brouard 5889: #endif
1.111 brouard 5890: if(!stat(plotcmd,&info)){
1.107 brouard 5891: printf("Error gnuplot program not found: %s\n",plotcmd);fflush(stdout);
1.111 brouard 5892: if(!stat(getenv("GNUPLOTBIN"),&info)){
5893: printf("Error gnuplot program not found: %s Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
5894: }else
5895: strcpy(pplotcmd,plotcmd);
5896: #ifdef UNIX
5897: strcpy(plotcmd,GNUPLOTPROGRAM);
5898: if(!stat(plotcmd,&info)){
5899: printf("Error gnuplot program not found: %s\n",plotcmd);fflush(stdout);
5900: }else
5901: strcpy(pplotcmd,plotcmd);
1.108 lievre 5902: #endif
1.111 brouard 5903: }else
5904: strcpy(pplotcmd,plotcmd);
5905:
5906: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
5907: printf("Starting graphs with: %s\n",plotcmd);fflush(stdout);
1.107 brouard 5908:
1.91 brouard 5909: if((outcmd=system(plotcmd)) != 0){
1.108 lievre 5910: printf("\n Problem with gnuplot\n");
1.91 brouard 5911: }
1.75 brouard 5912: printf(" Wait...");
1.53 brouard 5913: while (z[0] != 'q') {
5914: /* chdir(path); */
1.91 brouard 5915: printf("\nType e to edit output files, g to graph again and q for exiting: ");
1.53 brouard 5916: scanf("%s",z);
1.91 brouard 5917: /* if (z[0] == 'c') system("./imach"); */
1.99 brouard 5918: if (z[0] == 'e') {
5919: printf("Starting browser with: %s",optionfilehtm);fflush(stdout);
5920: system(optionfilehtm);
5921: }
1.53 brouard 5922: else if (z[0] == 'g') system(plotcmd);
5923: else if (z[0] == 'q') exit(0);
5924: }
1.91 brouard 5925: end:
5926: while (z[0] != 'q') {
5927: printf("\nType q for exiting: ");
5928: scanf("%s",z);
5929: }
1.53 brouard 5930: }
1.88 brouard 5931:
1.53 brouard 5932:
5933:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>