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