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