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