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