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