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