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