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