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