Annotation of imach/src/imach.c, revision 1.35
1.35 ! lievre 1: /* $Id: imach.c,v 1.34 2002/03/13 17:19:16 brouard Exp $
1.24 lievre 2: Interpolated Markov Chain
1.22 brouard 3:
4: Short summary of the programme:
5:
6: This program computes Healthy Life Expectancies from
7: cross-longitudinal data. Cross-longitudinal data consist in: -1- a
8: first survey ("cross") where individuals from different ages are
9: interviewed on their health status or degree of disability (in the
10: case of a health survey which is our main interest) -2- at least a
11: second wave of interviews ("longitudinal") which measure each change
12: (if any) in individual health status. Health expectancies are
13: computed from the time spent in each health state according to a
14: model. More health states you consider, more time is necessary to reach the
15: Maximum Likelihood of the parameters involved in the model. The
16: simplest model is the multinomial logistic model where pij is the
17: probabibility to be observed in state j at the second wave
18: conditional to be observed in state i at the first wave. Therefore
19: the model is: log(pij/pii)= aij + bij*age+ cij*sex + etc , where
20: 'age' is age and 'sex' is a covariate. If you want to have a more
21: complex model than "constant and age", you should modify the program
22: where the markup *Covariates have to be included here again* invites
23: you to do it. More covariates you add, slower the
24: convergence.
25:
26: The advantage of this computer programme, compared to a simple
27: multinomial logistic model, is clear when the delay between waves is not
28: identical for each individual. Also, if a individual missed an
29: intermediate interview, the information is lost, but taken into
30: account using an interpolation or extrapolation.
31:
32: hPijx is the probability to be observed in state i at age x+h
33: conditional to the observed state i at age x. The delay 'h' can be
34: split into an exact number (nh*stepm) of unobserved intermediate
35: states. This elementary transition (by month or quarter trimester,
36: semester or year) is model as a multinomial logistic. The hPx
37: matrix is simply the matrix product of nh*stepm elementary matrices
38: and the contribution of each individual to the likelihood is simply
39: hPijx.
1.2 lievre 40:
41: Also this programme outputs the covariance matrix of the parameters but also
42: of the life expectancies. It also computes the prevalence limits.
43:
44: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
45: Institut national d'études démographiques, Paris.
46: This software have been partly granted by Euro-REVES, a concerted action
47: from the European Union.
48: It is copyrighted identically to a GNU software product, ie programme and
49: software can be distributed freely for non commercial use. Latest version
50: can be accessed at http://euroreves.ined.fr/imach .
51: **********************************************************************/
52:
53: #include <math.h>
54: #include <stdio.h>
55: #include <stdlib.h>
56: #include <unistd.h>
57:
58: #define MAXLINE 256
1.35 ! lievre 59: #define GNUPLOTPROGRAM "wgnuplot"
! 60: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
1.2 lievre 61: #define FILENAMELENGTH 80
62: /*#define DEBUG*/
63: #define windows
1.5 lievre 64: #define GLOCK_ERROR_NOPATH -1 /* empty path */
65: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
66:
1.2 lievre 67: #define MAXPARM 30 /* Maximum number of parameters for the optimization */
68: #define NPARMAX 64 /* (nlstate+ndeath-1)*nlstate*ncovmodel */
69:
70: #define NINTERVMAX 8
71: #define NLSTATEMAX 8 /* Maximum number of live states (for func) */
72: #define NDEATHMAX 8 /* Maximum number of dead states (for func) */
73: #define NCOVMAX 8 /* Maximum number of covariates */
1.3 lievre 74: #define MAXN 20000
1.2 lievre 75: #define YEARM 12. /* Number of months per year */
76: #define AGESUP 130
77: #define AGEBASE 40
78:
79:
1.21 lievre 80: int erreur; /* Error number */
1.2 lievre 81: int nvar;
1.8 lievre 82: int cptcovn, cptcovage=0, cptcoveff=0,cptcov;
1.2 lievre 83: int npar=NPARMAX;
84: int nlstate=2; /* Number of live states */
85: int ndeath=1; /* Number of dead states */
1.34 brouard 86: int ncovmodel, ncovcol; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.15 lievre 87: int popbased=0;
1.2 lievre 88:
89: int *wav; /* Number of waves for this individuual 0 is possible */
90: int maxwav; /* Maxim number of waves */
1.8 lievre 91: int jmin, jmax; /* min, max spacing between 2 waves */
1.2 lievre 92: int mle, weightopt;
93: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
94: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
1.8 lievre 95: double jmean; /* Mean space between 2 waves */
1.2 lievre 96: double **oldm, **newm, **savm; /* Working pointers to matrices */
97: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.27 lievre 98: FILE *fic,*ficpar, *ficparo,*ficres, *ficrespl, *ficrespij, *ficrest,*ficresf,*ficrespop;
1.25 lievre 99: FILE *ficgp,*ficresprob,*ficpop;
1.2 lievre 100: FILE *ficreseij;
101: char filerese[FILENAMELENGTH];
102: FILE *ficresvij;
103: char fileresv[FILENAMELENGTH];
104: FILE *ficresvpl;
105: char fileresvpl[FILENAMELENGTH];
106:
107: #define NR_END 1
108: #define FREE_ARG char*
109: #define FTOL 1.0e-10
110:
111: #define NRANSI
112: #define ITMAX 200
113:
114: #define TOL 2.0e-4
115:
116: #define CGOLD 0.3819660
117: #define ZEPS 1.0e-10
118: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
119:
120: #define GOLD 1.618034
121: #define GLIMIT 100.0
122: #define TINY 1.0e-20
123:
124: static double maxarg1,maxarg2;
125: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
126: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1.25 lievre 127:
1.2 lievre 128: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
129: #define rint(a) floor(a+0.5)
130:
131: static double sqrarg;
132: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
133: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
134:
135: int imx;
136: int stepm;
137: /* Stepm, step in month: minimum step interpolation*/
138:
139: int m,nb;
1.6 lievre 140: int *num, firstpass=0, lastpass=4,*cod, *ncodemax, *Tage;
1.2 lievre 141: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.13 lievre 142: double **pmmij, ***probs, ***mobaverage;
1.19 lievre 143: double dateintmean=0;
1.2 lievre 144:
145: double *weight;
146: int **s; /* Status */
147: double *agedc, **covar, idx;
1.7 lievre 148: int **nbcode, *Tcode, *Tvar, **codtab, **Tvard, *Tprod, cptcovprod, *Tvaraff;
1.2 lievre 149:
150: double ftol=FTOL; /* Tolerance for computing Max Likelihood */
151: double ftolhess; /* Tolerance for computing hessian */
152:
1.7 lievre 153: /**************** split *************************/
1.22 brouard 154: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1.5 lievre 155: {
156: char *s; /* pointer */
157: int l1, l2; /* length counters */
158:
159: l1 = strlen( path ); /* length of path */
160: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1.22 brouard 161: #ifdef windows
1.5 lievre 162: s = strrchr( path, '\\' ); /* find last / */
1.22 brouard 163: #else
164: s = strrchr( path, '/' ); /* find last / */
165: #endif
1.5 lievre 166: if ( s == NULL ) { /* no directory, so use current */
167: #if defined(__bsd__) /* get current working directory */
168: extern char *getwd( );
169:
170: if ( getwd( dirc ) == NULL ) {
171: #else
172: extern char *getcwd( );
173:
174: if ( getcwd( dirc, FILENAME_MAX ) == NULL ) {
175: #endif
176: return( GLOCK_ERROR_GETCWD );
177: }
178: strcpy( name, path ); /* we've got it */
179: } else { /* strip direcotry from path */
180: s++; /* after this, the filename */
181: l2 = strlen( s ); /* length of filename */
182: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
183: strcpy( name, s ); /* save file name */
184: strncpy( dirc, path, l1 - l2 ); /* now the directory */
185: dirc[l1-l2] = 0; /* add zero */
186: }
187: l1 = strlen( dirc ); /* length of directory */
1.22 brouard 188: #ifdef windows
1.5 lievre 189: if ( dirc[l1-1] != '\\' ) { dirc[l1] = '\\'; dirc[l1+1] = 0; }
1.22 brouard 190: #else
191: if ( dirc[l1-1] != '/' ) { dirc[l1] = '/'; dirc[l1+1] = 0; }
192: #endif
193: s = strrchr( name, '.' ); /* find last / */
194: s++;
195: strcpy(ext,s); /* save extension */
196: l1= strlen( name);
197: l2= strlen( s)+1;
198: strncpy( finame, name, l1-l2);
199: finame[l1-l2]= 0;
1.5 lievre 200: return( 0 ); /* we're done */
201: }
202:
203:
1.2 lievre 204: /******************************************/
205:
206: void replace(char *s, char*t)
207: {
208: int i;
209: int lg=20;
210: i=0;
211: lg=strlen(t);
212: for(i=0; i<= lg; i++) {
213: (s[i] = t[i]);
214: if (t[i]== '\\') s[i]='/';
215: }
216: }
217:
218: int nbocc(char *s, char occ)
219: {
220: int i,j=0;
221: int lg=20;
222: i=0;
223: lg=strlen(s);
224: for(i=0; i<= lg; i++) {
225: if (s[i] == occ ) j++;
226: }
227: return j;
228: }
229:
230: void cutv(char *u,char *v, char*t, char occ)
231: {
1.6 lievre 232: int i,lg,j,p=0;
1.2 lievre 233: i=0;
234: for(j=0; j<=strlen(t)-1; j++) {
1.3 lievre 235: if((t[j]!= occ) && (t[j+1]== occ)) p=j+1;
1.2 lievre 236: }
237:
238: lg=strlen(t);
239: for(j=0; j<p; j++) {
240: (u[j] = t[j]);
241: }
1.6 lievre 242: u[p]='\0';
1.2 lievre 243:
244: for(j=0; j<= lg; j++) {
245: if (j>=(p+1))(v[j-p-1] = t[j]);
246: }
247: }
248:
249: /********************** nrerror ********************/
250:
251: void nrerror(char error_text[])
252: {
253: fprintf(stderr,"ERREUR ...\n");
254: fprintf(stderr,"%s\n",error_text);
255: exit(1);
256: }
257: /*********************** vector *******************/
258: double *vector(int nl, int nh)
259: {
260: double *v;
261: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
262: if (!v) nrerror("allocation failure in vector");
263: return v-nl+NR_END;
264: }
265:
266: /************************ free vector ******************/
267: void free_vector(double*v, int nl, int nh)
268: {
269: free((FREE_ARG)(v+nl-NR_END));
270: }
271:
272: /************************ivector *******************************/
273: int *ivector(long nl,long nh)
274: {
275: int *v;
276: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
277: if (!v) nrerror("allocation failure in ivector");
278: return v-nl+NR_END;
279: }
280:
281: /******************free ivector **************************/
282: void free_ivector(int *v, long nl, long nh)
283: {
284: free((FREE_ARG)(v+nl-NR_END));
285: }
286:
287: /******************* imatrix *******************************/
288: int **imatrix(long nrl, long nrh, long ncl, long nch)
289: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
290: {
291: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
292: int **m;
293:
294: /* allocate pointers to rows */
295: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
296: if (!m) nrerror("allocation failure 1 in matrix()");
297: m += NR_END;
298: m -= nrl;
299:
300:
301: /* allocate rows and set pointers to them */
302: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
303: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
304: m[nrl] += NR_END;
305: m[nrl] -= ncl;
306:
307: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
308:
309: /* return pointer to array of pointers to rows */
310: return m;
311: }
312:
313: /****************** free_imatrix *************************/
314: void free_imatrix(m,nrl,nrh,ncl,nch)
315: int **m;
316: long nch,ncl,nrh,nrl;
317: /* free an int matrix allocated by imatrix() */
318: {
319: free((FREE_ARG) (m[nrl]+ncl-NR_END));
320: free((FREE_ARG) (m+nrl-NR_END));
321: }
322:
323: /******************* matrix *******************************/
324: double **matrix(long nrl, long nrh, long ncl, long nch)
325: {
326: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
327: double **m;
328:
329: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
330: if (!m) nrerror("allocation failure 1 in matrix()");
331: m += NR_END;
332: m -= nrl;
333:
334: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
335: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
336: m[nrl] += NR_END;
337: m[nrl] -= ncl;
338:
339: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
340: return m;
341: }
342:
343: /*************************free matrix ************************/
344: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
345: {
346: free((FREE_ARG)(m[nrl]+ncl-NR_END));
347: free((FREE_ARG)(m+nrl-NR_END));
348: }
349:
350: /******************* ma3x *******************************/
351: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
352: {
353: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
354: double ***m;
355:
356: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
357: if (!m) nrerror("allocation failure 1 in matrix()");
358: m += NR_END;
359: m -= nrl;
360:
361: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
362: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
363: m[nrl] += NR_END;
364: m[nrl] -= ncl;
365:
366: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
367:
368: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
369: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
370: m[nrl][ncl] += NR_END;
371: m[nrl][ncl] -= nll;
372: for (j=ncl+1; j<=nch; j++)
373: m[nrl][j]=m[nrl][j-1]+nlay;
374:
375: for (i=nrl+1; i<=nrh; i++) {
376: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
377: for (j=ncl+1; j<=nch; j++)
378: m[i][j]=m[i][j-1]+nlay;
379: }
380: return m;
381: }
382:
383: /*************************free ma3x ************************/
384: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
385: {
386: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
387: free((FREE_ARG)(m[nrl]+ncl-NR_END));
388: free((FREE_ARG)(m+nrl-NR_END));
389: }
390:
391: /***************** f1dim *************************/
392: extern int ncom;
393: extern double *pcom,*xicom;
394: extern double (*nrfunc)(double []);
395:
396: double f1dim(double x)
397: {
398: int j;
399: double f;
400: double *xt;
401:
402: xt=vector(1,ncom);
403: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
404: f=(*nrfunc)(xt);
405: free_vector(xt,1,ncom);
406: return f;
407: }
408:
409: /*****************brent *************************/
410: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
411: {
412: int iter;
413: double a,b,d,etemp;
414: double fu,fv,fw,fx;
415: double ftemp;
416: double p,q,r,tol1,tol2,u,v,w,x,xm;
417: double e=0.0;
418:
419: a=(ax < cx ? ax : cx);
420: b=(ax > cx ? ax : cx);
421: x=w=v=bx;
422: fw=fv=fx=(*f)(x);
423: for (iter=1;iter<=ITMAX;iter++) {
424: xm=0.5*(a+b);
425: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
426: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
427: printf(".");fflush(stdout);
428: #ifdef DEBUG
429: 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);
430: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
431: #endif
432: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
433: *xmin=x;
434: return fx;
435: }
436: ftemp=fu;
437: if (fabs(e) > tol1) {
438: r=(x-w)*(fx-fv);
439: q=(x-v)*(fx-fw);
440: p=(x-v)*q-(x-w)*r;
441: q=2.0*(q-r);
442: if (q > 0.0) p = -p;
443: q=fabs(q);
444: etemp=e;
445: e=d;
446: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
447: d=CGOLD*(e=(x >= xm ? a-x : b-x));
448: else {
449: d=p/q;
450: u=x+d;
451: if (u-a < tol2 || b-u < tol2)
452: d=SIGN(tol1,xm-x);
453: }
454: } else {
455: d=CGOLD*(e=(x >= xm ? a-x : b-x));
456: }
457: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
458: fu=(*f)(u);
459: if (fu <= fx) {
460: if (u >= x) a=x; else b=x;
461: SHFT(v,w,x,u)
462: SHFT(fv,fw,fx,fu)
463: } else {
464: if (u < x) a=u; else b=u;
465: if (fu <= fw || w == x) {
466: v=w;
467: w=u;
468: fv=fw;
469: fw=fu;
470: } else if (fu <= fv || v == x || v == w) {
471: v=u;
472: fv=fu;
473: }
474: }
475: }
476: nrerror("Too many iterations in brent");
477: *xmin=x;
478: return fx;
479: }
480:
481: /****************** mnbrak ***********************/
482:
483: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
484: double (*func)(double))
485: {
486: double ulim,u,r,q, dum;
487: double fu;
488:
489: *fa=(*func)(*ax);
490: *fb=(*func)(*bx);
491: if (*fb > *fa) {
492: SHFT(dum,*ax,*bx,dum)
493: SHFT(dum,*fb,*fa,dum)
494: }
495: *cx=(*bx)+GOLD*(*bx-*ax);
496: *fc=(*func)(*cx);
497: while (*fb > *fc) {
498: r=(*bx-*ax)*(*fb-*fc);
499: q=(*bx-*cx)*(*fb-*fa);
500: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
501: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r));
502: ulim=(*bx)+GLIMIT*(*cx-*bx);
503: if ((*bx-u)*(u-*cx) > 0.0) {
504: fu=(*func)(u);
505: } else if ((*cx-u)*(u-ulim) > 0.0) {
506: fu=(*func)(u);
507: if (fu < *fc) {
508: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
509: SHFT(*fb,*fc,fu,(*func)(u))
510: }
511: } else if ((u-ulim)*(ulim-*cx) >= 0.0) {
512: u=ulim;
513: fu=(*func)(u);
514: } else {
515: u=(*cx)+GOLD*(*cx-*bx);
516: fu=(*func)(u);
517: }
518: SHFT(*ax,*bx,*cx,u)
519: SHFT(*fa,*fb,*fc,fu)
520: }
521: }
522:
523: /*************** linmin ************************/
524:
525: int ncom;
526: double *pcom,*xicom;
527: double (*nrfunc)(double []);
528:
529: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
530: {
531: double brent(double ax, double bx, double cx,
532: double (*f)(double), double tol, double *xmin);
533: double f1dim(double x);
534: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
535: double *fc, double (*func)(double));
536: int j;
537: double xx,xmin,bx,ax;
538: double fx,fb,fa;
539:
540: ncom=n;
541: pcom=vector(1,n);
542: xicom=vector(1,n);
543: nrfunc=func;
544: for (j=1;j<=n;j++) {
545: pcom[j]=p[j];
546: xicom[j]=xi[j];
547: }
548: ax=0.0;
549: xx=1.0;
550: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim);
551: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin);
552: #ifdef DEBUG
553: printf("retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
554: #endif
555: for (j=1;j<=n;j++) {
556: xi[j] *= xmin;
557: p[j] += xi[j];
558: }
559: free_vector(xicom,1,n);
560: free_vector(pcom,1,n);
561: }
562:
563: /*************** powell ************************/
564: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
565: double (*func)(double []))
566: {
567: void linmin(double p[], double xi[], int n, double *fret,
568: double (*func)(double []));
569: int i,ibig,j;
570: double del,t,*pt,*ptt,*xit;
571: double fp,fptt;
572: double *xits;
573: pt=vector(1,n);
574: ptt=vector(1,n);
575: xit=vector(1,n);
576: xits=vector(1,n);
577: *fret=(*func)(p);
578: for (j=1;j<=n;j++) pt[j]=p[j];
579: for (*iter=1;;++(*iter)) {
580: fp=(*fret);
581: ibig=0;
582: del=0.0;
583: printf("\nPowell iter=%d -2*LL=%.12f",*iter,*fret);
584: for (i=1;i<=n;i++)
585: printf(" %d %.12f",i, p[i]);
586: printf("\n");
587: for (i=1;i<=n;i++) {
588: for (j=1;j<=n;j++) xit[j]=xi[j][i];
589: fptt=(*fret);
590: #ifdef DEBUG
591: printf("fret=%lf \n",*fret);
592: #endif
593: printf("%d",i);fflush(stdout);
594: linmin(p,xit,n,fret,func);
595: if (fabs(fptt-(*fret)) > del) {
596: del=fabs(fptt-(*fret));
597: ibig=i;
598: }
599: #ifdef DEBUG
600: printf("%d %.12e",i,(*fret));
601: for (j=1;j<=n;j++) {
602: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
603: printf(" x(%d)=%.12e",j,xit[j]);
604: }
605: for(j=1;j<=n;j++)
606: printf(" p=%.12e",p[j]);
607: printf("\n");
608: #endif
609: }
610: if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) {
611: #ifdef DEBUG
612: int k[2],l;
613: k[0]=1;
614: k[1]=-1;
615: printf("Max: %.12e",(*func)(p));
616: for (j=1;j<=n;j++)
617: printf(" %.12e",p[j]);
618: printf("\n");
619: for(l=0;l<=1;l++) {
620: for (j=1;j<=n;j++) {
621: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
622: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
623: }
624: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
625: }
626: #endif
627:
628:
629: free_vector(xit,1,n);
630: free_vector(xits,1,n);
631: free_vector(ptt,1,n);
632: free_vector(pt,1,n);
633: return;
634: }
635: if (*iter == ITMAX) nrerror("powell exceeding maximum iterations.");
636: for (j=1;j<=n;j++) {
637: ptt[j]=2.0*p[j]-pt[j];
638: xit[j]=p[j]-pt[j];
639: pt[j]=p[j];
640: }
641: fptt=(*func)(ptt);
642: if (fptt < fp) {
643: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt);
644: if (t < 0.0) {
645: linmin(p,xit,n,fret,func);
646: for (j=1;j<=n;j++) {
647: xi[j][ibig]=xi[j][n];
648: xi[j][n]=xit[j];
649: }
650: #ifdef DEBUG
651: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
652: for(j=1;j<=n;j++)
653: printf(" %.12e",xit[j]);
654: printf("\n");
655: #endif
656: }
657: }
658: }
659: }
660:
661: /**** Prevalence limit ****************/
662:
663: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int ij)
664: {
665: /* Computes the prevalence limit in each live state at age x by left multiplying the unit
666: matrix by transitions matrix until convergence is reached */
667:
668: int i, ii,j,k;
669: double min, max, maxmin, maxmax,sumnew=0.;
670: double **matprod2();
671: double **out, cov[NCOVMAX], **pmij();
672: double **newm;
673: double agefin, delaymax=50 ; /* Max number of years to converge */
674:
675: for (ii=1;ii<=nlstate+ndeath;ii++)
676: for (j=1;j<=nlstate+ndeath;j++){
677: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
678: }
1.6 lievre 679:
680: cov[1]=1.;
681:
682: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.2 lievre 683: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
684: newm=savm;
685: /* Covariates have to be included here again */
1.6 lievre 686: cov[2]=agefin;
687:
688: for (k=1; k<=cptcovn;k++) {
1.7 lievre 689: cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];
1.35 ! lievre 690: /* 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]]);*/
1.6 lievre 691: }
1.35 ! lievre 692: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
1.7 lievre 693: for (k=1; k<=cptcovprod;k++)
694: cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];
695:
696: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
697: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
1.35 ! lievre 698: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.2 lievre 699: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
700:
701: savm=oldm;
702: oldm=newm;
703: maxmax=0.;
704: for(j=1;j<=nlstate;j++){
705: min=1.;
706: max=0.;
707: for(i=1; i<=nlstate; i++) {
708: sumnew=0;
709: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
710: prlim[i][j]= newm[i][j]/(1-sumnew);
711: max=FMAX(max,prlim[i][j]);
712: min=FMIN(min,prlim[i][j]);
713: }
714: maxmin=max-min;
715: maxmax=FMAX(maxmax,maxmin);
716: }
717: if(maxmax < ftolpl){
718: return prlim;
719: }
720: }
721: }
722:
1.12 lievre 723: /*************** transition probabilities ***************/
1.2 lievre 724:
725: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
726: {
727: double s1, s2;
728: /*double t34;*/
729: int i,j,j1, nc, ii, jj;
730:
731: for(i=1; i<= nlstate; i++){
732: for(j=1; j<i;j++){
733: for (nc=1, s2=0.;nc <=ncovmodel; nc++){
734: /*s2 += param[i][j][nc]*cov[nc];*/
735: s2 += x[(i-1)*nlstate*ncovmodel+(j-1)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];
736: /*printf("Int j<i s1=%.17e, s2=%.17e\n",s1,s2);*/
737: }
738: ps[i][j]=s2;
739: /*printf("s1=%.17e, s2=%.17e\n",s1,s2);*/
740: }
741: for(j=i+1; j<=nlstate+ndeath;j++){
742: for (nc=1, s2=0.;nc <=ncovmodel; nc++){
743: s2 += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];
744: /*printf("Int j>i s1=%.17e, s2=%.17e %lx %lx\n",s1,s2,s1,s2);*/
745: }
1.22 brouard 746: ps[i][j]=s2;
1.2 lievre 747: }
748: }
1.12 lievre 749: /*ps[3][2]=1;*/
750:
1.2 lievre 751: for(i=1; i<= nlstate; i++){
752: s1=0;
753: for(j=1; j<i; j++)
754: s1+=exp(ps[i][j]);
755: for(j=i+1; j<=nlstate+ndeath; j++)
756: s1+=exp(ps[i][j]);
757: ps[i][i]=1./(s1+1.);
758: for(j=1; j<i; j++)
759: ps[i][j]= exp(ps[i][j])*ps[i][i];
760: for(j=i+1; j<=nlstate+ndeath; j++)
761: ps[i][j]= exp(ps[i][j])*ps[i][i];
762: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
763: } /* end i */
764:
765: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
766: for(jj=1; jj<= nlstate+ndeath; jj++){
767: ps[ii][jj]=0;
768: ps[ii][ii]=1;
769: }
770: }
771:
1.12 lievre 772:
1.2 lievre 773: /* for(ii=1; ii<= nlstate+ndeath; ii++){
774: for(jj=1; jj<= nlstate+ndeath; jj++){
775: printf("%lf ",ps[ii][jj]);
776: }
777: printf("\n ");
778: }
779: printf("\n ");printf("%lf ",cov[2]);*/
780: /*
781: for(i=1; i<= npar; i++) printf("%f ",x[i]);
782: goto end;*/
783: return ps;
784: }
785:
786: /**************** Product of 2 matrices ******************/
787:
788: double **matprod2(double **out, double **in,long nrl, long nrh, long ncl, long nch, long ncolol, long ncoloh, double **b)
789: {
1.13 lievre 790: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
1.2 lievre 791: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
792: /* in, b, out are matrice of pointers which should have been initialized
793: before: only the contents of out is modified. The function returns
794: a pointer to pointers identical to out */
795: long i, j, k;
796: for(i=nrl; i<= nrh; i++)
797: for(k=ncolol; k<=ncoloh; k++)
798: for(j=ncl,out[i][k]=0.; j<=nch; j++)
799: out[i][k] +=in[i][j]*b[j][k];
800:
801: return out;
802: }
803:
804:
805: /************* Higher Matrix Product ***************/
806:
807: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij )
808: {
809: /* Computes the transition matrix starting at age 'age' over 'nhstepm*hstepm*stepm' month
810: duration (i.e. until
811: age (in years) age+nhstepm*stepm/12) by multiplying nhstepm*hstepm matrices.
812: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
813: (typically every 2 years instead of every month which is too big).
814: Model is determined by parameters x and covariates have to be
815: included manually here.
816:
817: */
818:
819: int i, j, d, h, k;
820: double **out, cov[NCOVMAX];
821: double **newm;
822:
823: /* Hstepm could be zero and should return the unit matrix */
824: for (i=1;i<=nlstate+ndeath;i++)
825: for (j=1;j<=nlstate+ndeath;j++){
826: oldm[i][j]=(i==j ? 1.0 : 0.0);
827: po[i][j][0]=(i==j ? 1.0 : 0.0);
828: }
829: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
830: for(h=1; h <=nhstepm; h++){
831: for(d=1; d <=hstepm; d++){
832: newm=savm;
833: /* Covariates have to be included here again */
834: cov[1]=1.;
835: cov[2]=age+((h-1)*hstepm + (d-1))*stepm/YEARM;
1.7 lievre 836: for (k=1; k<=cptcovn;k++) cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];
1.12 lievre 837: for (k=1; k<=cptcovage;k++)
1.7 lievre 838: cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
1.12 lievre 839: for (k=1; k<=cptcovprod;k++)
1.7 lievre 840: cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];
841:
842:
1.2 lievre 843: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
844: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
845: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
846: pmij(pmmij,cov,ncovmodel,x,nlstate));
847: savm=oldm;
848: oldm=newm;
849: }
850: for(i=1; i<=nlstate+ndeath; i++)
851: for(j=1;j<=nlstate+ndeath;j++) {
852: po[i][j][h]=newm[i][j];
853: /*printf("i=%d j=%d h=%d po[i][j][h]=%f ",i,j,h,po[i][j][h]);
854: */
855: }
856: } /* end h */
857: return po;
858: }
859:
860:
861: /*************** log-likelihood *************/
862: double func( double *x)
863: {
1.6 lievre 864: int i, ii, j, k, mi, d, kk;
1.2 lievre 865: double l, ll[NLSTATEMAX], cov[NCOVMAX];
866: double **out;
867: double sw; /* Sum of weights */
868: double lli; /* Individual log likelihood */
869: long ipmx;
870: /*extern weight */
871: /* We are differentiating ll according to initial status */
872: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
873: /*for(i=1;i<imx;i++)
1.8 lievre 874: printf(" %d\n",s[4][i]);
1.2 lievre 875: */
1.6 lievre 876: cov[1]=1.;
1.2 lievre 877:
878: for(k=1; k<=nlstate; k++) ll[k]=0.;
879: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.6 lievre 880: for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1.8 lievre 881: for(mi=1; mi<= wav[i]-1; mi++){
1.2 lievre 882: for (ii=1;ii<=nlstate+ndeath;ii++)
883: for (j=1;j<=nlstate+ndeath;j++) oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1.8 lievre 884: for(d=0; d<dh[mi][i]; d++){
885: newm=savm;
886: cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
887: for (kk=1; kk<=cptcovage;kk++) {
888: cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
889: }
890:
891: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
892: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
893: savm=oldm;
894: oldm=newm;
895:
896:
1.2 lievre 897: } /* end mult */
1.8 lievre 898:
1.2 lievre 899: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);
900: /* printf(" %f ",out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/
901: ipmx +=1;
902: sw += weight[i];
903: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
904: } /* end of wave */
905: } /* end of individual */
906:
907: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
908: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
909: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
910: return -l;
911: }
912:
913:
914: /*********** Maximum Likelihood Estimation ***************/
915:
916: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
917: {
918: int i,j, iter;
919: double **xi,*delti;
920: double fret;
921: xi=matrix(1,npar,1,npar);
922: for (i=1;i<=npar;i++)
923: for (j=1;j<=npar;j++)
924: xi[i][j]=(i==j ? 1.0 : 0.0);
925: printf("Powell\n");
926: powell(p,xi,npar,ftol,&iter,&fret,func);
927:
928: printf("\n#Number of iterations = %d, -2 Log likelihood = %.12f\n",iter,func(p));
1.21 lievre 929: fprintf(ficres,"#Number of iterations = %d, -2 Log likelihood = %.12f \n",iter,func(p));
1.2 lievre 930:
931: }
932:
933: /**** Computes Hessian and covariance matrix ***/
934: void hesscov(double **matcov, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
935: {
936: double **a,**y,*x,pd;
937: double **hess;
938: int i, j,jk;
939: int *indx;
940:
941: double hessii(double p[], double delta, int theta, double delti[]);
942: double hessij(double p[], double delti[], int i, int j);
943: void lubksb(double **a, int npar, int *indx, double b[]) ;
944: void ludcmp(double **a, int npar, int *indx, double *d) ;
945:
946: hess=matrix(1,npar,1,npar);
947:
948: printf("\nCalculation of the hessian matrix. Wait...\n");
949: for (i=1;i<=npar;i++){
950: printf("%d",i);fflush(stdout);
951: hess[i][i]=hessii(p,ftolhess,i,delti);
952: /*printf(" %f ",p[i]);*/
1.12 lievre 953: /*printf(" %lf ",hess[i][i]);*/
1.2 lievre 954: }
1.12 lievre 955:
1.2 lievre 956: for (i=1;i<=npar;i++) {
957: for (j=1;j<=npar;j++) {
958: if (j>i) {
959: printf(".%d%d",i,j);fflush(stdout);
960: hess[i][j]=hessij(p,delti,i,j);
1.12 lievre 961: hess[j][i]=hess[i][j];
962: /*printf(" %lf ",hess[i][j]);*/
1.2 lievre 963: }
964: }
965: }
966: printf("\n");
967:
968: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
969:
970: a=matrix(1,npar,1,npar);
971: y=matrix(1,npar,1,npar);
972: x=vector(1,npar);
973: indx=ivector(1,npar);
974: for (i=1;i<=npar;i++)
975: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
976: ludcmp(a,npar,indx,&pd);
977:
978: for (j=1;j<=npar;j++) {
979: for (i=1;i<=npar;i++) x[i]=0;
980: x[j]=1;
981: lubksb(a,npar,indx,x);
982: for (i=1;i<=npar;i++){
983: matcov[i][j]=x[i];
984: }
985: }
986:
987: printf("\n#Hessian matrix#\n");
988: for (i=1;i<=npar;i++) {
989: for (j=1;j<=npar;j++) {
990: printf("%.3e ",hess[i][j]);
991: }
992: printf("\n");
993: }
994:
995: /* Recompute Inverse */
996: for (i=1;i<=npar;i++)
997: for (j=1;j<=npar;j++) a[i][j]=matcov[i][j];
998: ludcmp(a,npar,indx,&pd);
999:
1000: /* printf("\n#Hessian matrix recomputed#\n");
1001:
1002: for (j=1;j<=npar;j++) {
1003: for (i=1;i<=npar;i++) x[i]=0;
1004: x[j]=1;
1005: lubksb(a,npar,indx,x);
1006: for (i=1;i<=npar;i++){
1007: y[i][j]=x[i];
1008: printf("%.3e ",y[i][j]);
1009: }
1010: printf("\n");
1011: }
1012: */
1013:
1014: free_matrix(a,1,npar,1,npar);
1015: free_matrix(y,1,npar,1,npar);
1016: free_vector(x,1,npar);
1017: free_ivector(indx,1,npar);
1018: free_matrix(hess,1,npar,1,npar);
1019:
1020:
1021: }
1022:
1023: /*************** hessian matrix ****************/
1024: double hessii( double x[], double delta, int theta, double delti[])
1025: {
1026: int i;
1027: int l=1, lmax=20;
1028: double k1,k2;
1029: double p2[NPARMAX+1];
1030: double res;
1031: double delt, delts, nkhi=10.,nkhif=1., khi=1.e-4;
1032: double fx;
1033: int k=0,kmax=10;
1034: double l1;
1035:
1036: fx=func(x);
1037: for (i=1;i<=npar;i++) p2[i]=x[i];
1038: for(l=0 ; l <=lmax; l++){
1039: l1=pow(10,l);
1040: delts=delt;
1041: for(k=1 ; k <kmax; k=k+1){
1042: delt = delta*(l1*k);
1043: p2[theta]=x[theta] +delt;
1044: k1=func(p2)-fx;
1045: p2[theta]=x[theta]-delt;
1046: k2=func(p2)-fx;
1047: /*res= (k1-2.0*fx+k2)/delt/delt; */
1048: res= (k1+k2)/delt/delt/2.; /* Divided by because L and not 2*L */
1049:
1050: #ifdef DEBUG
1051: 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);
1052: #endif
1053: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
1054: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
1055: k=kmax;
1056: }
1057: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1058: k=kmax; l=lmax*10.;
1059: }
1060: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
1061: delts=delt;
1062: }
1063: }
1064: }
1065: delti[theta]=delts;
1.12 lievre 1066: return res;
1.3 lievre 1067:
1.2 lievre 1068: }
1069:
1070: double hessij( double x[], double delti[], int thetai,int thetaj)
1071: {
1072: int i;
1073: int l=1, l1, lmax=20;
1074: double k1,k2,k3,k4,res,fx;
1075: double p2[NPARMAX+1];
1076: int k;
1077:
1078: fx=func(x);
1079: for (k=1; k<=2; k++) {
1080: for (i=1;i<=npar;i++) p2[i]=x[i];
1081: p2[thetai]=x[thetai]+delti[thetai]/k;
1082: p2[thetaj]=x[thetaj]+delti[thetaj]/k;
1083: k1=func(p2)-fx;
1084:
1085: p2[thetai]=x[thetai]+delti[thetai]/k;
1086: p2[thetaj]=x[thetaj]-delti[thetaj]/k;
1087: k2=func(p2)-fx;
1088:
1089: p2[thetai]=x[thetai]-delti[thetai]/k;
1090: p2[thetaj]=x[thetaj]+delti[thetaj]/k;
1091: k3=func(p2)-fx;
1092:
1093: p2[thetai]=x[thetai]-delti[thetai]/k;
1094: p2[thetaj]=x[thetaj]-delti[thetaj]/k;
1095: k4=func(p2)-fx;
1096: res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /* Because of L not 2*L */
1097: #ifdef DEBUG
1098: 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);
1099: #endif
1100: }
1101: return res;
1102: }
1103:
1104: /************** Inverse of matrix **************/
1105: void ludcmp(double **a, int n, int *indx, double *d)
1106: {
1107: int i,imax,j,k;
1108: double big,dum,sum,temp;
1109: double *vv;
1110:
1111: vv=vector(1,n);
1112: *d=1.0;
1113: for (i=1;i<=n;i++) {
1114: big=0.0;
1115: for (j=1;j<=n;j++)
1116: if ((temp=fabs(a[i][j])) > big) big=temp;
1117: if (big == 0.0) nrerror("Singular matrix in routine ludcmp");
1118: vv[i]=1.0/big;
1119: }
1120: for (j=1;j<=n;j++) {
1121: for (i=1;i<j;i++) {
1122: sum=a[i][j];
1123: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
1124: a[i][j]=sum;
1125: }
1126: big=0.0;
1127: for (i=j;i<=n;i++) {
1128: sum=a[i][j];
1129: for (k=1;k<j;k++)
1130: sum -= a[i][k]*a[k][j];
1131: a[i][j]=sum;
1132: if ( (dum=vv[i]*fabs(sum)) >= big) {
1133: big=dum;
1134: imax=i;
1135: }
1136: }
1137: if (j != imax) {
1138: for (k=1;k<=n;k++) {
1139: dum=a[imax][k];
1140: a[imax][k]=a[j][k];
1141: a[j][k]=dum;
1142: }
1143: *d = -(*d);
1144: vv[imax]=vv[j];
1145: }
1146: indx[j]=imax;
1147: if (a[j][j] == 0.0) a[j][j]=TINY;
1148: if (j != n) {
1149: dum=1.0/(a[j][j]);
1150: for (i=j+1;i<=n;i++) a[i][j] *= dum;
1151: }
1152: }
1153: free_vector(vv,1,n); /* Doesn't work */
1154: ;
1155: }
1156:
1157: void lubksb(double **a, int n, int *indx, double b[])
1158: {
1159: int i,ii=0,ip,j;
1160: double sum;
1161:
1162: for (i=1;i<=n;i++) {
1163: ip=indx[i];
1164: sum=b[ip];
1165: b[ip]=b[i];
1166: if (ii)
1167: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
1168: else if (sum) ii=i;
1169: b[i]=sum;
1170: }
1171: for (i=n;i>=1;i--) {
1172: sum=b[i];
1173: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
1174: b[i]=sum/a[i][i];
1175: }
1176: }
1177:
1178: /************ Frequencies ********************/
1.26 lievre 1179: void freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2,double jprev1, double mprev1,double anprev1,double jprev2, double mprev2,double anprev2)
1.2 lievre 1180: { /* Some frequencies */
1.35 ! lievre 1181:
1.18 lievre 1182: int i, m, jk, k1,i1, j1, bool, z1,z2,j;
1.2 lievre 1183: double ***freq; /* Frequencies */
1184: double *pp;
1.19 lievre 1185: double pos, k2, dateintsum=0,k2cpt=0;
1.2 lievre 1186: FILE *ficresp;
1187: char fileresp[FILENAMELENGTH];
1.35 ! lievre 1188:
1.2 lievre 1189: pp=vector(1,nlstate);
1.19 lievre 1190: probs= ma3x(1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
1.2 lievre 1191: strcpy(fileresp,"p");
1192: strcat(fileresp,fileres);
1193: if((ficresp=fopen(fileresp,"w"))==NULL) {
1194: printf("Problem with prevalence resultfile: %s\n", fileresp);
1195: exit(0);
1196: }
1197: freq= ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,agemin,agemax+3);
1198: j1=0;
1.35 ! lievre 1199:
1.7 lievre 1200: j=cptcoveff;
1.2 lievre 1201: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.35 ! lievre 1202:
1.2 lievre 1203: for(k1=1; k1<=j;k1++){
1.35 ! lievre 1204: for(i1=1; i1<=ncodemax[k1];i1++){
! 1205: j1++;
! 1206: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
! 1207: scanf("%d", i);*/
! 1208: for (i=-1; i<=nlstate+ndeath; i++)
! 1209: for (jk=-1; jk<=nlstate+ndeath; jk++)
! 1210: for(m=agemin; m <= agemax+3; m++)
! 1211: freq[i][jk][m]=0;
! 1212:
! 1213: dateintsum=0;
! 1214: k2cpt=0;
! 1215: for (i=1; i<=imx; i++) {
! 1216: bool=1;
! 1217: if (cptcovn>0) {
! 1218: for (z1=1; z1<=cptcoveff; z1++)
! 1219: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtab[j1][z1]])
! 1220: bool=0;
! 1221: }
! 1222: if (bool==1) {
! 1223: for(m=firstpass; m<=lastpass; m++){
! 1224: k2=anint[m][i]+(mint[m][i]/12.);
! 1225: if ((k2>=dateprev1) && (k2<=dateprev2)) {
! 1226: if(agev[m][i]==0) agev[m][i]=agemax+1;
! 1227: if(agev[m][i]==1) agev[m][i]=agemax+2;
! 1228: if (m<lastpass) {
! 1229: freq[s[m][i]][s[m+1][i]][(int)agev[m][i]] += weight[i];
! 1230: freq[s[m][i]][s[m+1][i]][(int) agemax+3] += weight[i];
! 1231: }
! 1232:
! 1233: if ((agev[m][i]>1) && (agev[m][i]< (agemax+3))) {
! 1234: dateintsum=dateintsum+k2;
! 1235: k2cpt++;
! 1236: }
! 1237: }
! 1238: }
! 1239: }
! 1240: }
1.26 lievre 1241:
1.35 ! lievre 1242: fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);
1.26 lievre 1243:
1.35 ! lievre 1244: if (cptcovn>0) {
! 1245: fprintf(ficresp, "\n#********** Variable ");
! 1246: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
! 1247: fprintf(ficresp, "**********\n#");
! 1248: }
! 1249: for(i=1; i<=nlstate;i++)
! 1250: fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
! 1251: fprintf(ficresp, "\n");
! 1252:
! 1253: for(i=(int)agemin; i <= (int)agemax+3; i++){
! 1254: if(i==(int)agemax+3)
! 1255: printf("Total");
! 1256: else
! 1257: printf("Age %d", i);
! 1258: for(jk=1; jk <=nlstate ; jk++){
! 1259: for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
! 1260: pp[jk] += freq[jk][m][i];
! 1261: }
! 1262: for(jk=1; jk <=nlstate ; jk++){
! 1263: for(m=-1, pos=0; m <=0 ; m++)
! 1264: pos += freq[jk][m][i];
! 1265: if(pp[jk]>=1.e-10)
! 1266: printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
! 1267: else
! 1268: printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
! 1269: }
1.14 lievre 1270:
1.35 ! lievre 1271: for(jk=1; jk <=nlstate ; jk++){
! 1272: for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)
! 1273: pp[jk] += freq[jk][m][i];
! 1274: }
1.14 lievre 1275:
1.35 ! lievre 1276: for(jk=1,pos=0; jk <=nlstate ; jk++)
! 1277: pos += pp[jk];
! 1278: for(jk=1; jk <=nlstate ; jk++){
! 1279: if(pos>=1.e-5)
! 1280: printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
! 1281: else
! 1282: printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
! 1283: if( i <= (int) agemax){
! 1284: if(pos>=1.e-5){
! 1285: fprintf(ficresp," %d %.5f %.0f %.0f",i,pp[jk]/pos, pp[jk],pos);
! 1286: probs[i][jk][j1]= pp[jk]/pos;
! 1287: /*printf("\ni=%d jk=%d j1=%d %.5f %.0f %.0f %f",i,jk,j1,pp[jk]/pos, pp[jk],pos,probs[i][jk][j1]);*/
! 1288: }
! 1289: else
! 1290: fprintf(ficresp," %d NaNq %.0f %.0f",i,pp[jk],pos);
! 1291: }
1.13 lievre 1292: }
1.35 ! lievre 1293:
! 1294: for(jk=-1; jk <=nlstate+ndeath; jk++)
! 1295: for(m=-1; m <=nlstate+ndeath; m++)
! 1296: if(freq[jk][m][i] !=0 ) printf(" %d%d=%.0f",jk,m,freq[jk][m][i]);
! 1297: if(i <= (int) agemax)
! 1298: fprintf(ficresp,"\n");
! 1299: printf("\n");
1.2 lievre 1300: }
1301: }
1.35 ! lievre 1302: }
1.19 lievre 1303: dateintmean=dateintsum/k2cpt;
1.2 lievre 1304:
1305: fclose(ficresp);
1306: free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath,(int) agemin,(int) agemax+3);
1307: free_vector(pp,1,nlstate);
1.35 ! lievre 1308:
1.19 lievre 1309: /* End of Freq */
1310: }
1.2 lievre 1311:
1.15 lievre 1312: /************ Prevalence ********************/
1.28 lievre 1313: void prevalence(int agemin, float agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2, double calagedate)
1.15 lievre 1314: { /* Some frequencies */
1315:
1316: int i, m, jk, k1, i1, j1, bool, z1,z2,j;
1317: double ***freq; /* Frequencies */
1318: double *pp;
1.18 lievre 1319: double pos, k2;
1.15 lievre 1320:
1321: pp=vector(1,nlstate);
1.19 lievre 1322: probs= ma3x(1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
1.15 lievre 1323:
1324: freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,agemin,agemax+3);
1325: j1=0;
1326:
1327: j=cptcoveff;
1328: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1329:
1330: for(k1=1; k1<=j;k1++){
1331: for(i1=1; i1<=ncodemax[k1];i1++){
1332: j1++;
1333:
1334: for (i=-1; i<=nlstate+ndeath; i++)
1335: for (jk=-1; jk<=nlstate+ndeath; jk++)
1336: for(m=agemin; m <= agemax+3; m++)
1.19 lievre 1337: freq[i][jk][m]=0;
1.28 lievre 1338:
1.15 lievre 1339: for (i=1; i<=imx; i++) {
1340: bool=1;
1341: if (cptcovn>0) {
1342: for (z1=1; z1<=cptcoveff; z1++)
1343: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtab[j1][z1]])
1344: bool=0;
1.28 lievre 1345: }
1.19 lievre 1346: if (bool==1) {
1347: for(m=firstpass; m<=lastpass; m++){
1348: k2=anint[m][i]+(mint[m][i]/12.);
1349: if ((k2>=dateprev1) && (k2<=dateprev2)) {
1.18 lievre 1350: if(agev[m][i]==0) agev[m][i]=agemax+1;
1351: if(agev[m][i]==1) agev[m][i]=agemax+2;
1.35 ! lievre 1352: if (m<lastpass) freq[s[m][i]][s[m+1][i]][(int)(agev[m][i]+1-((int)calagedate %12)/12.)] += weight[i];
1.28 lievre 1353: /* freq[s[m][i]][s[m+1][i]][(int)(agemax+3+1)] += weight[i]; */
1.18 lievre 1354: }
1.15 lievre 1355: }
1356: }
1357: }
1.18 lievre 1358: for(i=(int)agemin; i <= (int)agemax+3; i++){
1359: for(jk=1; jk <=nlstate ; jk++){
1360: for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
1361: pp[jk] += freq[jk][m][i];
1362: }
1363: for(jk=1; jk <=nlstate ; jk++){
1364: for(m=-1, pos=0; m <=0 ; m++)
1.15 lievre 1365: pos += freq[jk][m][i];
1366: }
1367:
1368: for(jk=1; jk <=nlstate ; jk++){
1369: for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)
1370: pp[jk] += freq[jk][m][i];
1371: }
1372:
1373: for(jk=1,pos=0; jk <=nlstate ; jk++) pos += pp[jk];
1374:
1375: for(jk=1; jk <=nlstate ; jk++){
1376: if( i <= (int) agemax){
1377: if(pos>=1.e-5){
1378: probs[i][jk][j1]= pp[jk]/pos;
1379: }
1380: }
1381: }
1382:
1.18 lievre 1383: }
1.15 lievre 1384: }
1385: }
1386:
1387:
1388: free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath,(int) agemin,(int) agemax+3);
1389: free_vector(pp,1,nlstate);
1390:
1391: } /* End of Freq */
1.19 lievre 1392:
1.2 lievre 1393: /************* Waves Concatenation ***************/
1394:
1395: void concatwav(int wav[], int **dh, int **mw, int **s, double *agedc, double **agev, int firstpass, int lastpass, int imx, int nlstate, int stepm)
1396: {
1397: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
1398: Death is a valid wave (if date is known).
1399: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
1400: dh[m][i] of dh[mw[mi][i][i] is the delay between two effective waves m=mw[mi][i]
1401: and mw[mi+1][i]. dh depends on stepm.
1402: */
1403:
1404: int i, mi, m;
1.8 lievre 1405: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
1406: double sum=0., jmean=0.;*/
1.2 lievre 1407:
1.11 lievre 1408: int j, k=0,jk, ju, jl;
1409: double sum=0.;
1410: jmin=1e+5;
1411: jmax=-1;
1412: jmean=0.;
1.2 lievre 1413: for(i=1; i<=imx; i++){
1414: mi=0;
1415: m=firstpass;
1416: while(s[m][i] <= nlstate){
1417: if(s[m][i]>=1)
1418: mw[++mi][i]=m;
1419: if(m >=lastpass)
1420: break;
1421: else
1422: m++;
1423: }/* end while */
1424: if (s[m][i] > nlstate){
1425: mi++; /* Death is another wave */
1426: /* if(mi==0) never been interviewed correctly before death */
1427: /* Only death is a correct wave */
1428: mw[mi][i]=m;
1429: }
1430:
1431: wav[i]=mi;
1432: if(mi==0)
1433: printf("Warning, no any valid information for:%d line=%d\n",num[i],i);
1434: }
1435:
1436: for(i=1; i<=imx; i++){
1437: for(mi=1; mi<wav[i];mi++){
1438: if (stepm <=0)
1439: dh[mi][i]=1;
1440: else{
1441: if (s[mw[mi+1][i]][i] > nlstate) {
1.10 lievre 1442: if (agedc[i] < 2*AGESUP) {
1.2 lievre 1443: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
1.8 lievre 1444: if(j==0) j=1; /* Survives at least one month after exam */
1445: k=k+1;
1446: if (j >= jmax) jmax=j;
1.11 lievre 1447: if (j <= jmin) jmin=j;
1.8 lievre 1448: sum=sum+j;
1.30 lievre 1449: /*if (j<0) printf("j=%d num=%d \n",j,i); */
1.10 lievre 1450: }
1.2 lievre 1451: }
1452: else{
1453: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1454: k=k+1;
1455: if (j >= jmax) jmax=j;
1456: else if (j <= jmin)jmin=j;
1.30 lievre 1457: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
1.2 lievre 1458: sum=sum+j;
1459: }
1460: jk= j/stepm;
1461: jl= j -jk*stepm;
1462: ju= j -(jk+1)*stepm;
1463: if(jl <= -ju)
1464: dh[mi][i]=jk;
1465: else
1466: dh[mi][i]=jk+1;
1467: if(dh[mi][i]==0)
1468: dh[mi][i]=1; /* At least one step */
1469: }
1470: }
1471: }
1.8 lievre 1472: jmean=sum/k;
1473: printf("Delay (in months) between two waves Min=%d Max=%d Mean=%f\n\n ",jmin, jmax,jmean);
1.12 lievre 1474: }
1.2 lievre 1475: /*********** Tricode ****************************/
1476: void tricode(int *Tvar, int **nbcode, int imx)
1477: {
1.7 lievre 1478: int Ndum[20],ij=1, k, j, i;
1.2 lievre 1479: int cptcode=0;
1.7 lievre 1480: cptcoveff=0;
1481:
1482: for (k=0; k<19; k++) Ndum[k]=0;
1.2 lievre 1483: for (k=1; k<=7; k++) ncodemax[k]=0;
1.6 lievre 1484:
1.7 lievre 1485: for (j=1; j<=(cptcovn+2*cptcovprod); j++) {
1.2 lievre 1486: for (i=1; i<=imx; i++) {
1487: ij=(int)(covar[Tvar[j]][i]);
1488: Ndum[ij]++;
1.8 lievre 1489: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
1.2 lievre 1490: if (ij > cptcode) cptcode=ij;
1491: }
1.7 lievre 1492:
1.2 lievre 1493: for (i=0; i<=cptcode; i++) {
1494: if(Ndum[i]!=0) ncodemax[j]++;
1495: }
1496: ij=1;
1.7 lievre 1497:
1.8 lievre 1498:
1.2 lievre 1499: for (i=1; i<=ncodemax[j]; i++) {
1.7 lievre 1500: for (k=0; k<=19; k++) {
1.2 lievre 1501: if (Ndum[k] != 0) {
1502: nbcode[Tvar[j]][ij]=k;
1.35 ! lievre 1503: /* printf("nbcodeaaaaaaaaaaa=%d Tvar[j]=%d ij=%d j=%d",nbcode[Tvar[j]][ij],Tvar[j],ij,j);*/
1.2 lievre 1504: ij++;
1505: }
1506: if (ij > ncodemax[j]) break;
1507: }
1508: }
1.7 lievre 1509: }
1.8 lievre 1510:
1511: for (k=0; k<19; k++) Ndum[k]=0;
1512:
1.12 lievre 1513: for (i=1; i<=ncovmodel-2; i++) {
1.7 lievre 1514: ij=Tvar[i];
1515: Ndum[ij]++;
1516: }
1.8 lievre 1517:
1.7 lievre 1518: ij=1;
1.8 lievre 1519: for (i=1; i<=10; i++) {
1.34 brouard 1520: if((Ndum[i]!=0) && (i<=ncovcol)){
1.8 lievre 1521: Tvaraff[ij]=i;
1522: ij++;
1.7 lievre 1523: }
1524: }
1525:
1.8 lievre 1526: cptcoveff=ij-1;
1.6 lievre 1527: }
1.2 lievre 1528:
1529: /*********** Health Expectancies ****************/
1530:
1531: void evsij(char fileres[], double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int ij)
1532: {
1533: /* Health expectancies */
1.31 brouard 1534: int i, j, nhstepm, hstepm, h, nstepm, k;
1.35 ! lievre 1535: double age, agelim, hf;
1.2 lievre 1536: double ***p3mat;
1537:
1538: fprintf(ficreseij,"# Health expectancies\n");
1539: fprintf(ficreseij,"# Age");
1540: for(i=1; i<=nlstate;i++)
1541: for(j=1; j<=nlstate;j++)
1542: fprintf(ficreseij," %1d-%1d",i,j);
1543: fprintf(ficreseij,"\n");
1544:
1.31 brouard 1545: k=1; /* For example stepm=6 months */
1546: hstepm=k*YEARM; /* (a) Every k years of age (in months), for example every k=2 years 24 m */
1.32 brouard 1547: hstepm=stepm; /* or (b) We decided to compute the life expectancy with the smallest unit */
1.31 brouard 1548: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
1549: nhstepm is the number of hstepm from age to agelim
1550: nstepm is the number of stepm from age to agelin.
1551: Look at hpijx to understand the reason of that which relies in memory size
1552: and note for a fixed period like k years */
1553: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
1.32 brouard 1554: survival function given by stepm (the optimization length). Unfortunately it
1.31 brouard 1555: means that if the survival funtion is printed only each two years of age and if
1556: you sum them up and add 1 year (area under the trapezoids) you won't get the same
1557: results. So we changed our mind and took the option of the best precision.
1558: */
1559: hstepm=hstepm/stepm; /* Typically in stepm units, if k= 2 years, = 2/6 months = 4 */
1.2 lievre 1560:
1561: agelim=AGESUP;
1562: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
1563: /* nhstepm age range expressed in number of stepm */
1.31 brouard 1564: nstepm=(int) rint((agelim-age)*YEARM/stepm);
1565: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
1.33 brouard 1566: /* if (stepm >= YEARM) hstepm=1;*/
1.31 brouard 1567: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
1.2 lievre 1568: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1569: /* Computed by stepm unit matrices, product of hstepm matrices, stored
1570: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
1571: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, ij);
1.32 brouard 1572: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.2 lievre 1573: for(i=1; i<=nlstate;i++)
1574: for(j=1; j<=nlstate;j++)
1.29 lievre 1575: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
1.31 brouard 1576: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
1577: /* 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]);*/
1.2 lievre 1578: }
1.29 lievre 1579: fprintf(ficreseij,"%3.0f",age );
1580: for(i=1; i<=nlstate;i++)
1581: for(j=1; j<=nlstate;j++){
1.30 lievre 1582: fprintf(ficreseij," %9.4f", eij[i][j][(int)age]);
1.2 lievre 1583: }
1584: fprintf(ficreseij,"\n");
1585: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1586: }
1587: }
1588:
1589: /************ Variance ******************/
1590: void varevsij(char fileres[], 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)
1591: {
1592: /* Variance of health expectancies */
1593: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
1594: double **newm;
1595: double **dnewm,**doldm;
1.35 ! lievre 1596: int i, j, nhstepm, hstepm, h, nstepm, kk;
1.2 lievre 1597: int k, cptcode;
1.12 lievre 1598: double *xp;
1.2 lievre 1599: double **gp, **gm;
1600: double ***gradg, ***trgradg;
1601: double ***p3mat;
1.35 ! lievre 1602: double age,agelim, hf;
1.2 lievre 1603: int theta;
1604:
1605: fprintf(ficresvij,"# Covariances of life expectancies\n");
1606: fprintf(ficresvij,"# Age");
1607: for(i=1; i<=nlstate;i++)
1608: for(j=1; j<=nlstate;j++)
1609: fprintf(ficresvij," Cov(e%1d, e%1d)",i,j);
1610: fprintf(ficresvij,"\n");
1611:
1612: xp=vector(1,npar);
1613: dnewm=matrix(1,nlstate,1,npar);
1614: doldm=matrix(1,nlstate,1,nlstate);
1615:
1.35 ! lievre 1616: kk=1; /* For example stepm=6 months */
! 1617: hstepm=kk*YEARM; /* (a) Every k years of age (in months), for example every k=2 years 24 m */
! 1618: hstepm=stepm; /* or (b) We decided to compute the life expectancy with the smallest unit */
! 1619: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
! 1620: nhstepm is the number of hstepm from age to agelim
! 1621: nstepm is the number of stepm from age to agelin.
! 1622: Look at hpijx to understand the reason of that which relies in memory size
! 1623: and note for a fixed period like k years */
! 1624: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
! 1625: survival function given by stepm (the optimization length). Unfortunately it
! 1626: means that if the survival funtion is printed only each two years of age and if
! 1627: you sum them up and add 1 year (area under the trapezoids) you won't get the same
! 1628: results. So we changed our mind and took the option of the best precision.
! 1629: */
! 1630: hstepm=hstepm/stepm; /* Typically in stepm units, if k= 2 years, = 2/6 months = 4 */
1.2 lievre 1631: agelim = AGESUP;
1632: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
1.35 ! lievre 1633: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
! 1634: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
1.2 lievre 1635: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1636: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
1637: gp=matrix(0,nhstepm,1,nlstate);
1638: gm=matrix(0,nhstepm,1,nlstate);
1639:
1640: for(theta=1; theta <=npar; theta++){
1641: for(i=1; i<=npar; i++){ /* Computes gradient */
1642: xp[i] = x[i] + (i==theta ?delti[theta]:0);
1643: }
1644: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
1645: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
1.14 lievre 1646:
1647: if (popbased==1) {
1648: for(i=1; i<=nlstate;i++)
1649: prlim[i][i]=probs[(int)age][i][ij];
1650: }
1.26 lievre 1651:
1.2 lievre 1652: for(j=1; j<= nlstate; j++){
1653: for(h=0; h<=nhstepm; h++){
1654: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
1655: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
1656: }
1657: }
1658:
1659: for(i=1; i<=npar; i++) /* Computes gradient */
1660: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1661: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
1662: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
1.26 lievre 1663:
1.14 lievre 1664: if (popbased==1) {
1665: for(i=1; i<=nlstate;i++)
1666: prlim[i][i]=probs[(int)age][i][ij];
1667: }
1668:
1.2 lievre 1669: for(j=1; j<= nlstate; j++){
1670: for(h=0; h<=nhstepm; h++){
1671: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
1672: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
1673: }
1674: }
1.14 lievre 1675:
1.2 lievre 1676: for(j=1; j<= nlstate; j++)
1677: for(h=0; h<=nhstepm; h++){
1678: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
1679: }
1680: } /* End theta */
1681:
1682: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar);
1683:
1684: for(h=0; h<=nhstepm; h++)
1685: for(j=1; j<=nlstate;j++)
1686: for(theta=1; theta <=npar; theta++)
1687: trgradg[h][j][theta]=gradg[h][theta][j];
1688:
1.35 ! lievre 1689: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.2 lievre 1690: for(i=1;i<=nlstate;i++)
1691: for(j=1;j<=nlstate;j++)
1692: vareij[i][j][(int)age] =0.;
1.35 ! lievre 1693:
1.2 lievre 1694: for(h=0;h<=nhstepm;h++){
1695: for(k=0;k<=nhstepm;k++){
1696: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
1697: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
1698: for(i=1;i<=nlstate;i++)
1699: for(j=1;j<=nlstate;j++)
1.35 ! lievre 1700: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
1.2 lievre 1701: }
1702: }
1.35 ! lievre 1703:
1.2 lievre 1704: fprintf(ficresvij,"%.0f ",age );
1705: for(i=1; i<=nlstate;i++)
1706: for(j=1; j<=nlstate;j++){
1.35 ! lievre 1707: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
1.2 lievre 1708: }
1709: fprintf(ficresvij,"\n");
1710: free_matrix(gp,0,nhstepm,1,nlstate);
1711: free_matrix(gm,0,nhstepm,1,nlstate);
1712: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
1713: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
1714: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1715: } /* End age */
1.26 lievre 1716:
1.2 lievre 1717: free_vector(xp,1,npar);
1718: free_matrix(doldm,1,nlstate,1,npar);
1719: free_matrix(dnewm,1,nlstate,1,nlstate);
1720:
1721: }
1722:
1723: /************ Variance of prevlim ******************/
1724: 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)
1725: {
1726: /* Variance of prevalence limit */
1727: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
1728: double **newm;
1729: double **dnewm,**doldm;
1730: int i, j, nhstepm, hstepm;
1731: int k, cptcode;
1732: double *xp;
1733: double *gp, *gm;
1734: double **gradg, **trgradg;
1735: double age,agelim;
1736: int theta;
1737:
1738: fprintf(ficresvpl,"# Standard deviation of prevalences limit\n");
1739: fprintf(ficresvpl,"# Age");
1740: for(i=1; i<=nlstate;i++)
1741: fprintf(ficresvpl," %1d-%1d",i,i);
1742: fprintf(ficresvpl,"\n");
1743:
1744: xp=vector(1,npar);
1745: dnewm=matrix(1,nlstate,1,npar);
1746: doldm=matrix(1,nlstate,1,nlstate);
1747:
1748: hstepm=1*YEARM; /* Every year of age */
1749: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
1750: agelim = AGESUP;
1751: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
1752: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
1753: if (stepm >= YEARM) hstepm=1;
1754: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
1755: gradg=matrix(1,npar,1,nlstate);
1756: gp=vector(1,nlstate);
1757: gm=vector(1,nlstate);
1758:
1759: for(theta=1; theta <=npar; theta++){
1760: for(i=1; i<=npar; i++){ /* Computes gradient */
1761: xp[i] = x[i] + (i==theta ?delti[theta]:0);
1762: }
1763: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
1764: for(i=1;i<=nlstate;i++)
1765: gp[i] = prlim[i][i];
1766:
1767: for(i=1; i<=npar; i++) /* Computes gradient */
1768: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1769: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
1770: for(i=1;i<=nlstate;i++)
1771: gm[i] = prlim[i][i];
1772:
1773: for(i=1;i<=nlstate;i++)
1774: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1775: } /* End theta */
1776:
1777: trgradg =matrix(1,nlstate,1,npar);
1778:
1779: for(j=1; j<=nlstate;j++)
1780: for(theta=1; theta <=npar; theta++)
1781: trgradg[j][theta]=gradg[theta][j];
1782:
1783: for(i=1;i<=nlstate;i++)
1784: varpl[i][(int)age] =0.;
1785: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
1786: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
1787: for(i=1;i<=nlstate;i++)
1788: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
1789:
1790: fprintf(ficresvpl,"%.0f ",age );
1791: for(i=1; i<=nlstate;i++)
1792: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
1793: fprintf(ficresvpl,"\n");
1794: free_vector(gp,1,nlstate);
1795: free_vector(gm,1,nlstate);
1796: free_matrix(gradg,1,npar,1,nlstate);
1797: free_matrix(trgradg,1,nlstate,1,npar);
1798: } /* End age */
1799:
1800: free_vector(xp,1,npar);
1801: free_matrix(doldm,1,nlstate,1,npar);
1802: free_matrix(dnewm,1,nlstate,1,nlstate);
1803:
1804: }
1805:
1.13 lievre 1806: /************ Variance of one-step probabilities ******************/
1807: void varprob(char fileres[], double **matcov, double x[], double delti[], int nlstate, double bage, double fage, int ij)
1808: {
1809: int i, j;
1810: int k=0, cptcode;
1811: double **dnewm,**doldm;
1812: double *xp;
1813: double *gp, *gm;
1814: double **gradg, **trgradg;
1815: double age,agelim, cov[NCOVMAX];
1816: int theta;
1817: char fileresprob[FILENAMELENGTH];
1818:
1819: strcpy(fileresprob,"prob");
1820: strcat(fileresprob,fileres);
1821: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
1822: printf("Problem with resultfile: %s\n", fileresprob);
1823: }
1824: printf("Computing variance of one-step probabilities: result on file '%s' \n",fileresprob);
1825:
1.2 lievre 1826:
1.13 lievre 1827: xp=vector(1,npar);
1828: dnewm=matrix(1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
1829: doldm=matrix(1,(nlstate+ndeath)*(nlstate+ndeath),1,(nlstate+ndeath)*(nlstate+ndeath));
1830:
1831: cov[1]=1;
1832: for (age=bage; age<=fage; age ++){
1833: cov[2]=age;
1834: gradg=matrix(1,npar,1,9);
1835: trgradg=matrix(1,9,1,npar);
1836: gp=vector(1,(nlstate+ndeath)*(nlstate+ndeath));
1837: gm=vector(1,(nlstate+ndeath)*(nlstate+ndeath));
1838:
1839: for(theta=1; theta <=npar; theta++){
1840: for(i=1; i<=npar; i++)
1841: xp[i] = x[i] + (i==theta ?delti[theta]:0);
1842:
1843: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1844:
1845: k=0;
1846: for(i=1; i<= (nlstate+ndeath); i++){
1847: for(j=1; j<=(nlstate+ndeath);j++){
1848: k=k+1;
1849: gp[k]=pmmij[i][j];
1850: }
1851: }
1852:
1853: for(i=1; i<=npar; i++)
1854: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1855:
1856:
1857: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1858: k=0;
1859: for(i=1; i<=(nlstate+ndeath); i++){
1860: for(j=1; j<=(nlstate+ndeath);j++){
1861: k=k+1;
1862: gm[k]=pmmij[i][j];
1863: }
1864: }
1865:
1866: for(i=1; i<= (nlstate+ndeath)*(nlstate+ndeath); i++)
1867: gradg[theta][i]=(gp[i]-gm[i])/2./delti[theta];
1868: }
1869:
1870: for(j=1; j<=(nlstate+ndeath)*(nlstate+ndeath);j++)
1871: for(theta=1; theta <=npar; theta++)
1872: trgradg[j][theta]=gradg[theta][j];
1873:
1874: matprod2(dnewm,trgradg,1,9,1,npar,1,npar,matcov);
1875: matprod2(doldm,dnewm,1,9,1,npar,1,9,gradg);
1876:
1877: pmij(pmmij,cov,ncovmodel,x,nlstate);
1878:
1879: k=0;
1880: for(i=1; i<=(nlstate+ndeath); i++){
1881: for(j=1; j<=(nlstate+ndeath);j++){
1882: k=k+1;
1883: gm[k]=pmmij[i][j];
1884: }
1885: }
1886:
1887: /*printf("\n%d ",(int)age);
1888: for (i=1; i<=(nlstate+ndeath)*(nlstate+ndeath-1);i++){
1889:
1890:
1891: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
1892: }*/
1893:
1894: fprintf(ficresprob,"\n%d ",(int)age);
1895:
1896: for (i=1; i<=(nlstate+ndeath)*(nlstate+ndeath-1);i++){
1897: if (i== 2) fprintf(ficresprob,"%.3e %.3e ",gm[i],doldm[i][i]);
1898: if (i== 4) fprintf(ficresprob,"%.3e %.3e ",gm[i],doldm[i][i]);
1899: }
1900:
1901: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
1902: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
1903: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
1904: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
1905: }
1906: free_vector(xp,1,npar);
1907: fclose(ficresprob);
1.26 lievre 1908:
1.13 lievre 1909: }
1.2 lievre 1910:
1.25 lievre 1911: /******************* Printing html file ***********/
1.35 ! lievre 1912: void printinghtml(char fileres[], char title[], char datafile[], int firstpass, \
! 1913: int lastpass, int stepm, int weightopt, char model[],\
! 1914: int imx,int jmin, int jmax, double jmeanint,char optionfile[], \
! 1915: char optionfilehtm[],char rfileres[], char optionfilegnuplot[],\
! 1916: char version[], int popforecast ){
1.25 lievre 1917: int jj1, k1, i1, cpt;
1918: FILE *fichtm;
1919: /*char optionfilehtm[FILENAMELENGTH];*/
1920:
1921: strcpy(optionfilehtm,optionfile);
1922: strcat(optionfilehtm,".htm");
1923: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1924: printf("Problem with %s \n",optionfilehtm), exit(0);
1925: }
1926:
1.35 ! lievre 1927: fprintf(fichtm,"<body> <font size=\"2\">Imach, Version %s </font> <hr size=\"2\" color=\"#EC5E5E\"> \n
! 1928: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>\n
! 1929: \n
! 1930: Total number of observations=%d <br>\n
! 1931: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n
1.25 lievre 1932: <hr size=\"2\" color=\"#EC5E5E\">
1.35 ! lievre 1933: <ul><li>Outputs files<br>\n
! 1934: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n
! 1935: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n
! 1936: - Observed prevalence in each state: <a href=\"p%s\">p%s</a> <br>\n
! 1937: - Stationary prevalence in each state: <a href=\"pl%s\">pl%s</a> <br>\n
! 1938: - Transition probabilities: <a href=\"pij%s\">pij%s</a><br>\n
! 1939: - Life expectancies by age and initial health status: <a href=\"e%s\">e%s</a> <br>\n",version,title,datafile,firstpass,lastpass,stepm, weightopt,model,imx,jmin,jmax,jmean,fileres,fileres,optionfilegnuplot,optionfilegnuplot,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres);
! 1940:
! 1941: fprintf(fichtm,"\n
! 1942: - Parameter file with estimated parameters and the covariance matrix: <a href=\"%s\">%s</a> <br>\n
! 1943: - Variances of life expectancies by age and initial health status: <a href=\"v%s\">v%s</a><br>\n
! 1944: - Health expectancies with their variances: <a href=\"t%s\">t%s</a> <br>\n
! 1945: - Standard deviation of stationary prevalences: <a href=\"vpl%s\">vpl%s</a> <br>\n",rfileres,rfileres,fileres,fileres,fileres,fileres,fileres,fileres);
! 1946:
! 1947: if(popforecast==1) fprintf(fichtm,"\n
! 1948: - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n
! 1949: - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n
! 1950: <br>",fileres,fileres,fileres,fileres);
! 1951: else
! 1952: 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.25 lievre 1953: fprintf(fichtm," <li>Graphs</li><p>");
1954:
1955: m=cptcoveff;
1956: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1957:
1958: jj1=0;
1959: for(k1=1; k1<=m;k1++){
1960: for(i1=1; i1<=ncodemax[k1];i1++){
1961: jj1++;
1962: if (cptcovn > 0) {
1963: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1964: for (cpt=1; cpt<=cptcoveff;cpt++)
1965: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtab[jj1][cpt]]);
1966: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
1967: }
1968: fprintf(fichtm,"<br>- Probabilities: pe%s%d.gif<br>
1969: <img src=\"pe%s%d.gif\">",strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1);
1970: for(cpt=1; cpt<nlstate;cpt++){
1971: fprintf(fichtm,"<br>- Prevalence of disability : p%s%d%d.gif<br>
1972: <img src=\"p%s%d%d.gif\">",strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1);
1973: }
1974: for(cpt=1; cpt<=nlstate;cpt++) {
1975: fprintf(fichtm,"<br>- Observed and stationary prevalence (with confident
1976: interval) in state (%d): v%s%d%d.gif <br>
1977: <img src=\"v%s%d%d.gif\">",cpt,strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1);
1978: }
1979: for(cpt=1; cpt<=nlstate;cpt++) {
1980: fprintf(fichtm,"\n<br>- Health life expectancies by age and initial health state (%d): exp%s%d%d.gif <br>
1981: <img src=\"exp%s%d%d.gif\">",cpt,strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1);
1982: }
1983: fprintf(fichtm,"\n<br>- Total life expectancy by age and
1984: health expectancies in states (1) and (2): e%s%d.gif<br>
1985: <img src=\"e%s%d.gif\">",strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1);
1986: fprintf(fichtm,"\n</body>");
1987: }
1988: }
1989: fclose(fichtm);
1990: }
1991:
1992: /******************* Gnuplot file **************/
1.35 ! lievre 1993: void printinggnuplot(char fileres[],char optionfilefiname[],char optionfile[],char optionfilegnuplot[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.25 lievre 1994:
1995: int m,cpt,k1,i,k,j,jk,k2,k3,ij,l;
1996:
1997: strcpy(optionfilegnuplot,optionfilefiname);
1.35 ! lievre 1998: strcat(optionfilegnuplot,".gp.txt");
1.25 lievre 1999: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
2000: printf("Problem with file %s",optionfilegnuplot);
2001: }
2002:
2003: #ifdef windows
2004: fprintf(ficgp,"cd \"%s\" \n",pathc);
2005: #endif
2006: m=pow(2,cptcoveff);
2007:
2008: /* 1eme*/
2009: for (cpt=1; cpt<= nlstate ; cpt ++) {
2010: for (k1=1; k1<= m ; k1 ++) {
2011:
2012: #ifdef windows
1.35 ! lievre 2013: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter gif small size 400,300\nplot [%.f:%.f] \"vpl%s\" every :::%d::%d u 1:2 \"\%%lf",ageminpar,fage,fileres,k1-1,k1-1);
1.25 lievre 2014: #endif
2015: #ifdef unix
1.35 ! lievre 2016: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nplot [%.f:%.f] \"vpl%s\" u 1:2 \"\%%lf",ageminpar,fage,fileres);
1.25 lievre 2017: #endif
2018:
2019: for (i=1; i<= nlstate ; i ++) {
2020: if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
2021: else fprintf(ficgp," \%%*lf (\%%*lf)");
2022: }
2023: fprintf(ficgp,"\" t\"Stationary prevalence\" w l 0,\"vpl%s\" every :::%d::%d u 1:($2+2*$3) \"\%%lf",fileres,k1-1,k1-1);
2024: for (i=1; i<= nlstate ; i ++) {
2025: if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
2026: else fprintf(ficgp," \%%*lf (\%%*lf)");
2027: }
2028: fprintf(ficgp,"\" t\"95\%% CI\" w l 1,\"vpl%s\" every :::%d::%d u 1:($2-2*$3) \"\%%lf",fileres,k1-1,k1-1);
2029: for (i=1; i<= nlstate ; i ++) {
2030: if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
2031: else fprintf(ficgp," \%%*lf (\%%*lf)");
2032: }
2033: 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));
2034: #ifdef unix
2035: fprintf(ficgp,"\nset ter gif small size 400,300");
2036: #endif
2037: fprintf(ficgp,"\nset out \"v%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
2038: }
2039: }
2040: /*2 eme*/
2041:
2042: for (k1=1; k1<= m ; k1 ++) {
1.35 ! lievre 2043: fprintf(ficgp,"set ylabel \"Years\" \nset ter gif small size 400,300\nplot [%.f:%.f] ",ageminpar,fage);
1.25 lievre 2044:
2045: for (i=1; i<= nlstate+1 ; i ++) {
2046: k=2*i;
2047: fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:2 \"\%%lf",fileres,k1-1,k1-1);
2048: for (j=1; j<= nlstate+1 ; j ++) {
2049: if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
2050: else fprintf(ficgp," \%%*lf (\%%*lf)");
2051: }
2052: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l ,");
2053: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l ,",i-1);
2054: fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2-$3*2) \"\%%lf",fileres,k1-1,k1-1);
2055: for (j=1; j<= nlstate+1 ; j ++) {
2056: if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
2057: else fprintf(ficgp," \%%*lf (\%%*lf)");
2058: }
2059: fprintf(ficgp,"\" t\"\" w l 0,");
2060: fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2+$3*2) \"\%%lf",fileres,k1-1,k1-1);
2061: for (j=1; j<= nlstate+1 ; j ++) {
2062: if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
2063: else fprintf(ficgp," \%%*lf (\%%*lf)");
2064: }
2065: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l 0");
2066: else fprintf(ficgp,"\" t\"\" w l 0,");
2067: }
2068: fprintf(ficgp,"\nset out \"e%s%d.gif\" \nreplot\n\n",strtok(optionfile, "."),k1);
2069: }
2070:
2071: /*3eme*/
2072:
2073: for (k1=1; k1<= m ; k1 ++) {
2074: for (cpt=1; cpt<= nlstate ; cpt ++) {
2075: k=2+nlstate*(cpt-1);
1.35 ! lievre 2076: fprintf(ficgp,"set ter gif small size 400,300\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);
1.25 lievre 2077: for (i=1; i< nlstate ; i ++) {
2078: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",fileres,k1-1,k1-1,k+i,cpt,i+1);
2079: }
2080: fprintf(ficgp,"\nset out \"exp%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
2081: }
2082: }
2083:
2084: /* CV preval stat */
2085: for (k1=1; k1<= m ; k1 ++) {
2086: for (cpt=1; cpt<nlstate ; cpt ++) {
2087: k=3;
1.35 ! lievre 2088: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter gif small size 400,300\nplot [%.f:%.f] \"pij%s\" u ($1==%d ? ($3):1/0):($%d/($%d",ageminpar,agemaxpar,fileres,k1,k+cpt+1,k+1);
1.25 lievre 2089:
2090: for (i=1; i< nlstate ; i ++)
2091: fprintf(ficgp,"+$%d",k+i+1);
2092: fprintf(ficgp,")) t\"prev(%d,%d)\" w l",cpt,cpt+1);
2093:
2094: l=3+(nlstate+ndeath)*cpt;
2095: fprintf(ficgp,",\"pij%s\" u ($1==%d ? ($3):1/0):($%d/($%d",fileres,k1,l+cpt+1,l+1);
2096: for (i=1; i< nlstate ; i ++) {
2097: l=3+(nlstate+ndeath)*cpt;
2098: fprintf(ficgp,"+$%d",l+i+1);
2099: }
2100: fprintf(ficgp,")) t\"prev(%d,%d)\" w l\n",cpt+1,cpt+1);
2101: fprintf(ficgp,"set out \"p%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
2102: }
2103: }
2104:
2105: /* proba elementaires */
2106: for(i=1,jk=1; i <=nlstate; i++){
2107: for(k=1; k <=(nlstate+ndeath); k++){
2108: if (k != i) {
2109: for(j=1; j <=ncovmodel; j++){
2110:
2111: fprintf(ficgp,"p%d=%f ",jk,p[jk]);
2112: jk++;
2113: fprintf(ficgp,"\n");
2114: }
2115: }
2116: }
2117: }
2118:
2119: for(jk=1; jk <=m; jk++) {
1.35 ! lievre 2120: fprintf(ficgp,"\nset ter gif small size 400,300\nset log y\nplot [%.f:%.f] ",ageminpar,agemaxpar);
1.25 lievre 2121: i=1;
2122: for(k2=1; k2<=nlstate; k2++) {
2123: k3=i;
2124: for(k=1; k<=(nlstate+ndeath); k++) {
2125: if (k != k2){
2126: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
2127: ij=1;
2128: for(j=3; j <=ncovmodel; j++) {
2129: if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) {
2130: fprintf(ficgp,"+p%d*%d*x",i+j-1,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);
2131: ij++;
2132: }
2133: else
2134: fprintf(ficgp,"+p%d*%d",i+j-1,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
2135: }
2136: fprintf(ficgp,")/(1");
2137:
2138: for(k1=1; k1 <=nlstate; k1++){
2139: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
2140: ij=1;
2141: for(j=3; j <=ncovmodel; j++){
2142: if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) {
2143: fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);
2144: ij++;
2145: }
2146: else
2147: fprintf(ficgp,"+p%d*%d",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
2148: }
2149: fprintf(ficgp,")");
2150: }
2151: fprintf(ficgp,") t \"p%d%d\" ", k2,k);
2152: if ((k+k2)!= (nlstate*2+ndeath)) fprintf(ficgp,",");
2153: i=i+ncovmodel;
2154: }
2155: }
2156: }
2157: fprintf(ficgp,"\nset out \"pe%s%d.gif\" \nreplot\n\n",strtok(optionfile, "."),jk);
2158: }
2159:
2160: fclose(ficgp);
2161: } /* end gnuplot */
2162:
2163:
2164: /*************** Moving average **************/
1.35 ! lievre 2165: void movingaverage(double agedeb, double fage,double ageminpar, double ***mobaverage){
1.25 lievre 2166:
2167: int i, cpt, cptcod;
1.35 ! lievre 2168: for (agedeb=ageminpar; agedeb<=fage; agedeb++)
1.25 lievre 2169: for (i=1; i<=nlstate;i++)
2170: for (cptcod=1;cptcod<=ncodemax[cptcov];cptcod++)
2171: mobaverage[(int)agedeb][i][cptcod]=0.;
2172:
1.35 ! lievre 2173: for (agedeb=ageminpar+4; agedeb<=fage; agedeb++){
1.25 lievre 2174: for (i=1; i<=nlstate;i++){
2175: for (cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
2176: for (cpt=0;cpt<=4;cpt++){
2177: mobaverage[(int)agedeb-2][i][cptcod]=mobaverage[(int)agedeb-2][i][cptcod]+probs[(int)agedeb-cpt][i][cptcod];
2178: }
2179: mobaverage[(int)agedeb-2][i][cptcod]=mobaverage[(int)agedeb-2][i][cptcod]/5;
2180: }
2181: }
2182: }
2183:
2184: }
2185:
1.27 lievre 2186:
2187: /************** Forecasting ******************/
1.35 ! lievre 2188: prevforecast(char fileres[], double anproj1,double mproj1,double jproj1,double ageminpar, double agemax,double dateprev1, double dateprev2, int mobilav, double agedeb, double fage, int popforecast, char popfile[], double anproj2,double p[], int i2){
1.27 lievre 2189:
2190: int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h;
2191: int *popage;
2192: double calagedate, agelim, kk1, kk2, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
2193: double *popeffectif,*popcount;
2194: double ***p3mat;
2195: char fileresf[FILENAMELENGTH];
2196:
2197: agelim=AGESUP;
2198: calagedate=(anproj1+mproj1/12.+jproj1/365.-dateintmean)*YEARM;
1.28 lievre 2199:
1.35 ! lievre 2200: prevalence(ageminpar, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax,mint,anint,dateprev1,dateprev2, calagedate);
1.28 lievre 2201:
1.27 lievre 2202:
2203: strcpy(fileresf,"f");
2204: strcat(fileresf,fileres);
2205: if((ficresf=fopen(fileresf,"w"))==NULL) {
2206: printf("Problem with forecast resultfile: %s\n", fileresf);
2207: }
2208: printf("Computing forecasting: result on file '%s' \n", fileresf);
2209:
2210: if (cptcoveff==0) ncodemax[cptcoveff]=1;
2211:
2212: if (mobilav==1) {
2213: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
1.35 ! lievre 2214: movingaverage(agedeb, fage, ageminpar, mobaverage);
1.27 lievre 2215: }
2216:
2217: stepsize=(int) (stepm+YEARM-1)/YEARM;
2218: if (stepm<=12) stepsize=1;
2219:
2220: agelim=AGESUP;
2221:
2222: hstepm=1;
2223: hstepm=hstepm/stepm;
2224: yp1=modf(dateintmean,&yp);
2225: anprojmean=yp;
2226: yp2=modf((yp1*12),&yp);
2227: mprojmean=yp;
2228: yp1=modf((yp2*30.5),&yp);
2229: jprojmean=yp;
2230: if(jprojmean==0) jprojmean=1;
2231: if(mprojmean==0) jprojmean=1;
2232:
2233: fprintf(ficresf,"# Estimated date of observed prevalence: %.lf/%.lf/%.lf ",jprojmean,mprojmean,anprojmean);
2234:
2235: for(cptcov=1;cptcov<=i2;cptcov++){
2236: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
2237: k=k+1;
2238: fprintf(ficresf,"\n#******");
2239: for(j=1;j<=cptcoveff;j++) {
2240: fprintf(ficresf," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
2241: }
2242: fprintf(ficresf,"******\n");
2243: fprintf(ficresf,"# StartingAge FinalAge");
2244: for(j=1; j<=nlstate+ndeath;j++) fprintf(ficresf," P.%d",j);
2245:
2246:
2247: for (cpt=0; cpt<=(anproj2-anproj1);cpt++) {
2248: fprintf(ficresf,"\n");
2249: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+cpt);
1.28 lievre 2250:
1.35 ! lievre 2251: for (agedeb=(fage-((int)calagedate %12/12.)); agedeb>=(ageminpar-((int)calagedate %12)/12.); agedeb--){
1.27 lievre 2252: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
2253: nhstepm = nhstepm/hstepm;
2254:
2255: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
2256: oldm=oldms;savm=savms;
2257: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
2258:
2259: for (h=0; h<=nhstepm; h++){
2260: if (h==(int) (calagedate+YEARM*cpt)) {
1.35 ! lievre 2261: fprintf(ficresf,"\n %.f %.f ",anproj1+cpt,agedeb+h*hstepm/YEARM*stepm);
1.27 lievre 2262: }
2263: for(j=1; j<=nlstate+ndeath;j++) {
2264: kk1=0.;kk2=0;
2265: for(i=1; i<=nlstate;i++) {
2266: if (mobilav==1)
2267: kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod];
2268: else {
2269: kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod];
2270: }
2271:
2272: }
2273: if (h==(int)(calagedate+12*cpt)){
2274: fprintf(ficresf," %.3f", kk1);
2275:
2276: }
2277: }
2278: }
2279: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
2280: }
2281: }
2282: }
2283: }
2284:
2285: if (mobilav==1) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
2286:
2287: fclose(ficresf);
2288: }
2289: /************** Forecasting ******************/
1.35 ! lievre 2290: 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){
1.27 lievre 2291:
2292: int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h;
2293: int *popage;
2294: double calagedate, agelim, kk1, kk2, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
2295: double *popeffectif,*popcount;
2296: double ***p3mat,***tabpop,***tabpopprev;
2297: char filerespop[FILENAMELENGTH];
2298:
1.28 lievre 2299: tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
2300: tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
2301: agelim=AGESUP;
2302: calagedate=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM;
1.27 lievre 2303:
1.35 ! lievre 2304: prevalence(ageminpar, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax,mint,anint,dateprev1,dateprev2, calagedate);
1.28 lievre 2305:
2306:
1.27 lievre 2307: strcpy(filerespop,"pop");
2308: strcat(filerespop,fileres);
2309: if((ficrespop=fopen(filerespop,"w"))==NULL) {
2310: printf("Problem with forecast resultfile: %s\n", filerespop);
2311: }
2312: printf("Computing forecasting: result on file '%s' \n", filerespop);
2313:
2314: if (cptcoveff==0) ncodemax[cptcoveff]=1;
2315:
2316: if (mobilav==1) {
2317: mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
1.35 ! lievre 2318: movingaverage(agedeb, fage, ageminpar, mobaverage);
1.27 lievre 2319: }
2320:
2321: stepsize=(int) (stepm+YEARM-1)/YEARM;
2322: if (stepm<=12) stepsize=1;
2323:
2324: agelim=AGESUP;
2325:
2326: hstepm=1;
2327: hstepm=hstepm/stepm;
2328:
2329: if (popforecast==1) {
2330: if((ficpop=fopen(popfile,"r"))==NULL) {
2331: printf("Problem with population file : %s\n",popfile);exit(0);
2332: }
2333: popage=ivector(0,AGESUP);
2334: popeffectif=vector(0,AGESUP);
2335: popcount=vector(0,AGESUP);
2336:
2337: i=1;
2338: while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1;
2339:
2340: imx=i;
2341: for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i];
2342: }
2343:
2344: for(cptcov=1;cptcov<=i2;cptcov++){
2345: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
2346: k=k+1;
2347: fprintf(ficrespop,"\n#******");
2348: for(j=1;j<=cptcoveff;j++) {
2349: fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
2350: }
2351: fprintf(ficrespop,"******\n");
1.28 lievre 2352: fprintf(ficrespop,"# Age");
1.27 lievre 2353: for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j);
2354: if (popforecast==1) fprintf(ficrespop," [Population]");
2355:
2356: for (cpt=0; cpt<=0;cpt++) {
1.28 lievre 2357: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
1.27 lievre 2358:
1.35 ! lievre 2359: for (agedeb=(fage-((int)calagedate %12/12.)); agedeb>=(ageminpar-((int)calagedate %12)/12.); agedeb--){
1.27 lievre 2360: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
2361: nhstepm = nhstepm/hstepm;
2362:
2363: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
2364: oldm=oldms;savm=savms;
2365: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
2366:
2367: for (h=0; h<=nhstepm; h++){
2368: if (h==(int) (calagedate+YEARM*cpt)) {
1.28 lievre 2369: fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
1.27 lievre 2370: }
2371: for(j=1; j<=nlstate+ndeath;j++) {
2372: kk1=0.;kk2=0;
2373: for(i=1; i<=nlstate;i++) {
2374: if (mobilav==1)
2375: kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod];
2376: else {
2377: kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod];
2378: }
2379: }
2380: if (h==(int)(calagedate+12*cpt)){
2381: tabpop[(int)(agedeb)][j][cptcod]=kk1;
2382: /*fprintf(ficrespop," %.3f", kk1);
2383: if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*/
2384: }
2385: }
2386: for(i=1; i<=nlstate;i++){
2387: kk1=0.;
2388: for(j=1; j<=nlstate;j++){
1.28 lievre 2389: kk1= kk1+tabpop[(int)(agedeb)][j][cptcod];
1.27 lievre 2390: }
2391: tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedate+12*cpt)*hstepm/YEARM*stepm-1)];
2392: }
2393:
1.28 lievre 2394: if (h==(int)(calagedate+12*cpt)) for(j=1; j<=nlstate;j++)
2395: fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]);
1.27 lievre 2396: }
2397: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
2398: }
2399: }
2400:
2401: /******/
2402:
1.28 lievre 2403: for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) {
2404: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
1.35 ! lievre 2405: for (agedeb=(fage-((int)calagedate %12/12.)); agedeb>=(ageminpar-((int)calagedate %12)/12.); agedeb--){
1.27 lievre 2406: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
2407: nhstepm = nhstepm/hstepm;
2408:
2409: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
2410: oldm=oldms;savm=savms;
2411: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
2412: for (h=0; h<=nhstepm; h++){
2413: if (h==(int) (calagedate+YEARM*cpt)) {
1.28 lievre 2414: fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
1.27 lievre 2415: }
2416: for(j=1; j<=nlstate+ndeath;j++) {
2417: kk1=0.;kk2=0;
2418: for(i=1; i<=nlstate;i++) {
2419: kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod];
2420: }
1.28 lievre 2421: if (h==(int)(calagedate+12*cpt)) fprintf(ficresf," %15.2f", kk1);
1.27 lievre 2422: }
2423: }
2424: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
2425: }
2426: }
2427: }
2428: }
2429:
2430: if (mobilav==1) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
2431:
2432: if (popforecast==1) {
2433: free_ivector(popage,0,AGESUP);
2434: free_vector(popeffectif,0,AGESUP);
2435: free_vector(popcount,0,AGESUP);
2436: }
2437: free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
2438: free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
2439: fclose(ficrespop);
2440: }
2441:
1.2 lievre 2442: /***********************************************/
2443: /**************** Main Program *****************/
2444: /***********************************************/
2445:
1.22 brouard 2446: int main(int argc, char *argv[])
1.2 lievre 2447: {
2448:
1.8 lievre 2449: int i,j, k, n=MAXN,iter,m,size,cptcode, cptcod;
1.2 lievre 2450: double agedeb, agefin,hf;
1.35 ! lievre 2451: double ageminpar=1.e20,agemin=1.e20, agemaxpar=-1.e20, agemax=-1.e20;
1.2 lievre 2452:
2453: double fret;
2454: double **xi,tmp,delta;
2455:
2456: double dum; /* Dummy variable */
2457: double ***p3mat;
2458: int *indx;
2459: char line[MAXLINE], linepar[MAXLINE];
2460: char title[MAXLINE];
1.25 lievre 2461: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH];
2462: char optionfilext[10], optionfilefiname[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilegnuplot[FILENAMELENGTH], plotcmd[FILENAMELENGTH];
1.22 brouard 2463:
1.29 lievre 2464: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.22 brouard 2465:
1.2 lievre 2466: char filerest[FILENAMELENGTH];
2467: char fileregp[FILENAMELENGTH];
1.16 lievre 2468: char popfile[FILENAMELENGTH];
1.2 lievre 2469: char path[80],pathc[80],pathcd[80],pathtot[80],model[20];
2470: int firstobs=1, lastobs=10;
2471: int sdeb, sfin; /* Status at beginning and end */
2472: int c, h , cpt,l;
2473: int ju,jl, mi;
1.7 lievre 2474: int i1,j1, k1,k2,k3,jk,aa,bb, stepsize, ij;
1.14 lievre 2475: int jnais,jdc,jint4,jint1,jint2,jint3,**outcome,**adl,*tab;
1.19 lievre 2476: int mobilav=0,popforecast=0;
1.2 lievre 2477: int hstepm, nhstepm;
1.28 lievre 2478: double jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,jpyram, mpyram,anpyram,jpyram1, mpyram1,anpyram1;
1.14 lievre 2479:
1.2 lievre 2480: double bage, fage, age, agelim, agebase;
2481: double ftolpl=FTOL;
2482: double **prlim;
2483: double *severity;
2484: double ***param; /* Matrix of parameters */
2485: double *p;
2486: double **matcov; /* Matrix of covariance */
2487: double ***delti3; /* Scale */
2488: double *delti; /* Scale */
2489: double ***eij, ***vareij;
2490: double **varpl; /* Variances of prevalence limits by age */
2491: double *epj, vepp;
1.16 lievre 2492: double kk1, kk2;
1.27 lievre 2493: double dateprev1, dateprev2,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2;
2494:
1.13 lievre 2495:
1.35 ! lievre 2496: char version[80]="Imach version 0.8a, March 2002, INED-EUROREVES ";
1.2 lievre 2497: char *alph[]={"a","a","b","c","d","e"}, str[4];
1.5 lievre 2498:
1.13 lievre 2499:
1.2 lievre 2500: char z[1]="c", occ;
2501: #include <sys/time.h>
2502: #include <time.h>
2503: char stra[80], strb[80], strc[80], strd[80],stre[80],modelsav[80];
1.19 lievre 2504:
1.2 lievre 2505: /* long total_usecs;
2506: struct timeval start_time, end_time;
2507:
2508: gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.35 ! lievre 2509: getcwd(pathcd, size);
1.2 lievre 2510:
1.22 brouard 2511: printf("\n%s",version);
2512: if(argc <=1){
2513: printf("\nEnter the parameter file name: ");
2514: scanf("%s",pathtot);
2515: }
2516: else{
2517: strcpy(pathtot,argv[1]);
2518: }
2519: /*if(getcwd(pathcd, 80)!= NULL)printf ("Error pathcd\n");*/
1.5 lievre 2520: /*cygwin_split_path(pathtot,path,optionfile);
2521: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
2522: /* cutv(path,optionfile,pathtot,'\\');*/
2523:
1.22 brouard 2524: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
2525: printf("pathtot=%s, path=%s, optionfile=%s optionfilext=%s optionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.2 lievre 2526: chdir(path);
2527: replace(pathc,path);
2528:
2529: /*-------- arguments in the command line --------*/
2530:
2531: strcpy(fileres,"r");
1.22 brouard 2532: strcat(fileres, optionfilefiname);
2533: strcat(fileres,".txt"); /* Other files have txt extension */
1.2 lievre 2534:
2535: /*---------arguments file --------*/
2536:
2537: if((ficpar=fopen(optionfile,"r"))==NULL) {
2538: printf("Problem with optionfile %s\n",optionfile);
2539: goto end;
2540: }
2541:
2542: strcpy(filereso,"o");
2543: strcat(filereso,fileres);
2544: if((ficparo=fopen(filereso,"w"))==NULL) {
2545: printf("Problem with Output resultfile: %s\n", filereso);goto end;
2546: }
2547:
2548: /* Reads comments: lines beginning with '#' */
2549: while((c=getc(ficpar))=='#' && c!= EOF){
2550: ungetc(c,ficpar);
2551: fgets(line, MAXLINE, ficpar);
2552: puts(line);
2553: fputs(line,ficparo);
2554: }
2555: ungetc(c,ficpar);
2556:
1.34 brouard 2557: 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\nmodel=%s\n",title, datafile, &lastobs, &firstpass,&lastpass,&ftol, &stepm, &ncovcol, &nlstate,&ndeath, &maxwav, &mle, &weightopt,model);
2558: 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);
2559: 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.14 lievre 2560: while((c=getc(ficpar))=='#' && c!= EOF){
2561: ungetc(c,ficpar);
2562: fgets(line, MAXLINE, ficpar);
2563: puts(line);
2564: fputs(line,ficparo);
2565: }
2566: ungetc(c,ficpar);
2567:
1.19 lievre 2568:
1.8 lievre 2569: covar=matrix(0,NCOVMAX,1,n);
2570: cptcovn=0;
2571: if (strlen(model)>1) cptcovn=nbocc(model,'+')+1;
1.2 lievre 2572:
2573: ncovmodel=2+cptcovn;
2574: nvar=ncovmodel-1; /* Suppressing age as a basic covariate */
2575:
2576: /* Read guess parameters */
2577: /* Reads comments: lines beginning with '#' */
2578: while((c=getc(ficpar))=='#' && c!= EOF){
2579: ungetc(c,ficpar);
2580: fgets(line, MAXLINE, ficpar);
2581: puts(line);
2582: fputs(line,ficparo);
2583: }
2584: ungetc(c,ficpar);
2585:
2586: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
2587: for(i=1; i <=nlstate; i++)
2588: for(j=1; j <=nlstate+ndeath-1; j++){
2589: fscanf(ficpar,"%1d%1d",&i1,&j1);
2590: fprintf(ficparo,"%1d%1d",i1,j1);
2591: printf("%1d%1d",i,j);
2592: for(k=1; k<=ncovmodel;k++){
2593: fscanf(ficpar," %lf",¶m[i][j][k]);
2594: printf(" %lf",param[i][j][k]);
2595: fprintf(ficparo," %lf",param[i][j][k]);
2596: }
2597: fscanf(ficpar,"\n");
2598: printf("\n");
2599: fprintf(ficparo,"\n");
2600: }
2601:
1.12 lievre 2602: npar= (nlstate+ndeath-1)*nlstate*ncovmodel;
2603:
1.2 lievre 2604: p=param[1][1];
2605:
2606: /* Reads comments: lines beginning with '#' */
2607: while((c=getc(ficpar))=='#' && c!= EOF){
2608: ungetc(c,ficpar);
2609: fgets(line, MAXLINE, ficpar);
2610: puts(line);
2611: fputs(line,ficparo);
2612: }
2613: ungetc(c,ficpar);
2614:
2615: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
2616: delti=vector(1,npar); /* Scale of each paramater (output from hesscov) */
2617: for(i=1; i <=nlstate; i++){
2618: for(j=1; j <=nlstate+ndeath-1; j++){
2619: fscanf(ficpar,"%1d%1d",&i1,&j1);
2620: printf("%1d%1d",i,j);
2621: fprintf(ficparo,"%1d%1d",i1,j1);
2622: for(k=1; k<=ncovmodel;k++){
2623: fscanf(ficpar,"%le",&delti3[i][j][k]);
2624: printf(" %le",delti3[i][j][k]);
2625: fprintf(ficparo," %le",delti3[i][j][k]);
2626: }
2627: fscanf(ficpar,"\n");
2628: printf("\n");
2629: fprintf(ficparo,"\n");
2630: }
2631: }
2632: delti=delti3[1][1];
2633:
2634: /* Reads comments: lines beginning with '#' */
2635: while((c=getc(ficpar))=='#' && c!= EOF){
2636: ungetc(c,ficpar);
2637: fgets(line, MAXLINE, ficpar);
2638: puts(line);
2639: fputs(line,ficparo);
2640: }
2641: ungetc(c,ficpar);
2642:
2643: matcov=matrix(1,npar,1,npar);
2644: for(i=1; i <=npar; i++){
2645: fscanf(ficpar,"%s",&str);
2646: printf("%s",str);
2647: fprintf(ficparo,"%s",str);
2648: for(j=1; j <=i; j++){
2649: fscanf(ficpar," %le",&matcov[i][j]);
2650: printf(" %.5le",matcov[i][j]);
2651: fprintf(ficparo," %.5le",matcov[i][j]);
2652: }
2653: fscanf(ficpar,"\n");
2654: printf("\n");
2655: fprintf(ficparo,"\n");
2656: }
2657: for(i=1; i <=npar; i++)
2658: for(j=i+1;j<=npar;j++)
2659: matcov[i][j]=matcov[j][i];
2660:
2661: printf("\n");
2662:
2663:
1.29 lievre 2664: /*-------- Rewriting paramater file ----------*/
2665: strcpy(rfileres,"r"); /* "Rparameterfile */
2666: strcat(rfileres,optionfilefiname); /* Parameter file first name*/
2667: strcat(rfileres,"."); /* */
2668: strcat(rfileres,optionfilext); /* Other files have txt extension */
2669: if((ficres =fopen(rfileres,"w"))==NULL) {
2670: printf("Problem writing new parameter file: %s\n", fileres);goto end;
1.2 lievre 2671: }
2672: fprintf(ficres,"#%s\n",version);
2673:
1.29 lievre 2674: /*-------- data file ----------*/
1.2 lievre 2675: if((fic=fopen(datafile,"r"))==NULL) {
2676: printf("Problem with datafile: %s\n", datafile);goto end;
2677: }
2678:
2679: n= lastobs;
2680: severity = vector(1,maxwav);
2681: outcome=imatrix(1,maxwav+1,1,n);
2682: num=ivector(1,n);
2683: moisnais=vector(1,n);
2684: annais=vector(1,n);
2685: moisdc=vector(1,n);
2686: andc=vector(1,n);
2687: agedc=vector(1,n);
2688: cod=ivector(1,n);
2689: weight=vector(1,n);
2690: for(i=1;i<=n;i++) weight[i]=1.0; /* Equal weights, 1 by default */
2691: mint=matrix(1,maxwav,1,n);
2692: anint=matrix(1,maxwav,1,n);
2693: s=imatrix(1,maxwav+1,1,n);
2694: adl=imatrix(1,maxwav+1,1,n);
2695: tab=ivector(1,NCOVMAX);
1.3 lievre 2696: ncodemax=ivector(1,8);
1.2 lievre 2697:
1.12 lievre 2698: i=1;
1.2 lievre 2699: while (fgets(line, MAXLINE, fic) != NULL) {
2700: if ((i >= firstobs) && (i <=lastobs)) {
2701:
2702: for (j=maxwav;j>=1;j--){
2703: cutv(stra, strb,line,' '); s[j][i]=atoi(strb);
2704: strcpy(line,stra);
2705: cutv(stra, strb,line,'/'); anint[j][i]=(double)(atoi(strb)); strcpy(line,stra);
2706: cutv(stra, strb,line,' '); mint[j][i]=(double)(atoi(strb)); strcpy(line,stra);
2707: }
2708:
2709: cutv(stra, strb,line,'/'); andc[i]=(double)(atoi(strb)); strcpy(line,stra);
2710: cutv(stra, strb,line,' '); moisdc[i]=(double)(atoi(strb)); strcpy(line,stra);
2711:
2712: cutv(stra, strb,line,'/'); annais[i]=(double)(atoi(strb)); strcpy(line,stra);
2713: cutv(stra, strb,line,' '); moisnais[i]=(double)(atoi(strb)); strcpy(line,stra);
2714:
2715: cutv(stra, strb,line,' '); weight[i]=(double)(atoi(strb)); strcpy(line,stra);
1.34 brouard 2716: for (j=ncovcol;j>=1;j--){
1.2 lievre 2717: cutv(stra, strb,line,' '); covar[j][i]=(double)(atoi(strb)); strcpy(line,stra);
2718: }
2719: num[i]=atol(stra);
1.12 lievre 2720:
2721: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
2722: printf("%d %.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.2 lievre 2723:
2724: i=i+1;
2725: }
2726: }
1.12 lievre 2727: /* printf("ii=%d", ij);
2728: scanf("%d",i);*/
2729: imx=i-1; /* Number of individuals */
1.3 lievre 2730:
1.12 lievre 2731: /* for (i=1; i<=imx; i++){
2732: if ((s[1][i]==3) && (s[2][i]==2)) s[2][i]=3;
2733: if ((s[2][i]==3) && (s[3][i]==2)) s[3][i]=3;
2734: if ((s[3][i]==3) && (s[4][i]==2)) s[4][i]=3;
1.35 ! lievre 2735: }*/
! 2736:
! 2737: /* for (i=1; i<=imx; i++){
! 2738: if (s[4][i]==9) s[4][i]=-1;
! 2739: printf("%d %.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]));}
! 2740: */
! 2741:
1.2 lievre 2742: /* Calculation of the number of parameter from char model*/
1.7 lievre 2743: Tvar=ivector(1,15);
2744: Tprod=ivector(1,15);
2745: Tvaraff=ivector(1,15);
2746: Tvard=imatrix(1,15,1,2);
1.6 lievre 2747: Tage=ivector(1,15);
1.2 lievre 2748:
2749: if (strlen(model) >1){
1.7 lievre 2750: j=0, j1=0, k1=1, k2=1;
1.2 lievre 2751: j=nbocc(model,'+');
1.6 lievre 2752: j1=nbocc(model,'*');
1.2 lievre 2753: cptcovn=j+1;
1.7 lievre 2754: cptcovprod=j1;
1.3 lievre 2755:
1.2 lievre 2756: strcpy(modelsav,model);
1.8 lievre 2757: if ((strcmp(model,"age")==0) || (strcmp(model,"age*age")==0)){
2758: printf("Error. Non available option model=%s ",model);
2759: goto end;
2760: }
2761:
2762: for(i=(j+1); i>=1;i--){
2763: cutv(stra,strb,modelsav,'+');
2764: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav);
2765: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
2766: /*scanf("%d",i);*/
2767: if (strchr(strb,'*')) {
2768: cutv(strd,strc,strb,'*');
2769: if (strcmp(strc,"age")==0) {
1.7 lievre 2770: cptcovprod--;
1.8 lievre 2771: cutv(strb,stre,strd,'V');
2772: Tvar[i]=atoi(stre);
2773: cptcovage++;
2774: Tage[cptcovage]=i;
2775: /*printf("stre=%s ", stre);*/
1.7 lievre 2776: }
1.8 lievre 2777: else if (strcmp(strd,"age")==0) {
1.7 lievre 2778: cptcovprod--;
1.8 lievre 2779: cutv(strb,stre,strc,'V');
2780: Tvar[i]=atoi(stre);
2781: cptcovage++;
2782: Tage[cptcovage]=i;
1.7 lievre 2783: }
2784: else {
1.8 lievre 2785: cutv(strb,stre,strc,'V');
1.34 brouard 2786: Tvar[i]=ncovcol+k1;
1.8 lievre 2787: cutv(strb,strc,strd,'V');
2788: Tprod[k1]=i;
2789: Tvard[k1][1]=atoi(strc);
2790: Tvard[k1][2]=atoi(stre);
2791: Tvar[cptcovn+k2]=Tvard[k1][1];
2792: Tvar[cptcovn+k2+1]=Tvard[k1][2];
1.7 lievre 2793: for (k=1; k<=lastobs;k++)
1.34 brouard 2794: covar[ncovcol+k1][k]=covar[atoi(stre)][k]*covar[atoi(strc)][k];
1.8 lievre 2795: k1++;
2796: k2=k2+2;
1.7 lievre 2797: }
1.2 lievre 2798: }
1.8 lievre 2799: else {
2800: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
2801: /* scanf("%d",i);*/
2802: cutv(strd,strc,strb,'V');
2803: Tvar[i]=atoi(strc);
2804: }
2805: strcpy(modelsav,stra);
2806: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
2807: scanf("%d",i);*/
1.2 lievre 2808: }
1.8 lievre 2809: }
2810:
1.35 ! lievre 2811: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
1.8 lievre 2812: printf("cptcovprod=%d ", cptcovprod);
2813: scanf("%d ",i);*/
1.2 lievre 2814: fclose(fic);
2815:
1.7 lievre 2816: /* if(mle==1){*/
1.2 lievre 2817: if (weightopt != 1) { /* Maximisation without weights*/
2818: for(i=1;i<=n;i++) weight[i]=1.0;
2819: }
2820: /*-calculation of age at interview from date of interview and age at death -*/
2821: agev=matrix(1,maxwav,1,imx);
1.12 lievre 2822:
1.35 ! lievre 2823: for (i=1; i<=imx; i++) {
! 2824: for(m=2; (m<= maxwav); m++) {
1.12 lievre 2825: if ((mint[m][i]== 99) && (s[m][i] <= nlstate)){
2826: anint[m][i]=9999;
2827: s[m][i]=-1;
2828: }
1.35 ! lievre 2829: if(moisdc[i]==99 && andc[i]==9999 & s[m][i]>nlstate) s[m][i]=-1;
! 2830: }
! 2831: }
! 2832:
1.2 lievre 2833: for (i=1; i<=imx; i++) {
2834: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
2835: for(m=1; (m<= maxwav); m++){
2836: if(s[m][i] >0){
1.35 ! lievre 2837: if (s[m][i] >= nlstate+1) {
1.2 lievre 2838: if(agedc[i]>0)
2839: if(moisdc[i]!=99 && andc[i]!=9999)
1.35 ! lievre 2840: agev[m][i]=agedc[i];
! 2841: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
! 2842: else {
1.8 lievre 2843: if (andc[i]!=9999){
1.2 lievre 2844: printf("Warning negative age at death: %d line:%d\n",num[i],i);
2845: agev[m][i]=-1;
1.8 lievre 2846: }
1.2 lievre 2847: }
2848: }
2849: else if(s[m][i] !=9){ /* Should no more exist */
2850: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
1.3 lievre 2851: if(mint[m][i]==99 || anint[m][i]==9999)
1.2 lievre 2852: agev[m][i]=1;
2853: else if(agev[m][i] <agemin){
2854: agemin=agev[m][i];
2855: /*printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], agemin);*/
2856: }
2857: else if(agev[m][i] >agemax){
2858: agemax=agev[m][i];
2859: /* printf(" anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.0f\n",m,i,anint[m][i], i,annais[i], agemax);*/
2860: }
2861: /*agev[m][i]=anint[m][i]-annais[i];*/
2862: /* agev[m][i] = age[i]+2*m;*/
2863: }
2864: else { /* =9 */
2865: agev[m][i]=1;
2866: s[m][i]=-1;
2867: }
2868: }
2869: else /*= 0 Unknown */
2870: agev[m][i]=1;
2871: }
2872:
2873: }
2874: for (i=1; i<=imx; i++) {
2875: for(m=1; (m<= maxwav); m++){
2876: if (s[m][i] > (nlstate+ndeath)) {
2877: printf("Error: Wrong value in nlstate or ndeath\n");
2878: goto end;
2879: }
2880: }
2881: }
2882:
2883: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, agemin, agemax);
2884:
2885: free_vector(severity,1,maxwav);
2886: free_imatrix(outcome,1,maxwav+1,1,n);
2887: free_vector(moisnais,1,n);
2888: free_vector(annais,1,n);
1.17 lievre 2889: /* free_matrix(mint,1,maxwav,1,n);
2890: free_matrix(anint,1,maxwav,1,n);*/
1.2 lievre 2891: free_vector(moisdc,1,n);
2892: free_vector(andc,1,n);
2893:
2894:
2895: wav=ivector(1,imx);
2896: dh=imatrix(1,lastpass-firstpass+1,1,imx);
2897: mw=imatrix(1,lastpass-firstpass+1,1,imx);
2898:
2899: /* Concatenates waves */
2900: concatwav(wav, dh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
2901:
2902:
1.6 lievre 2903: Tcode=ivector(1,100);
1.8 lievre 2904: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
1.7 lievre 2905: ncodemax[1]=1;
2906: if (cptcovn > 0) tricode(Tvar,nbcode,imx);
2907:
1.2 lievre 2908: codtab=imatrix(1,100,1,10);
2909: h=0;
1.7 lievre 2910: m=pow(2,cptcoveff);
1.2 lievre 2911:
1.7 lievre 2912: for(k=1;k<=cptcoveff; k++){
1.2 lievre 2913: for(i=1; i <=(m/pow(2,k));i++){
2914: for(j=1; j <= ncodemax[k]; j++){
1.7 lievre 2915: for(cpt=1; cpt <=(m/pow(2,cptcoveff+1-k)); cpt++){
1.2 lievre 2916: h++;
1.35 ! lievre 2917: if (h>m) h=1;codtab[h][k]=j;codtab[h][Tvar[k]]=j;
! 2918: /* printf("h=%d k=%d j=%d codtab[h][k]=%d tvar[k]=%d \n",h, k,j,codtab[h][k],Tvar[k]);*/
1.2 lievre 2919: }
2920: }
2921: }
2922: }
1.35 ! lievre 2923: /* printf("codtab[1][2]=%d codtab[2][2]=%d",codtab[1][2],codtab[2][2]);
! 2924: codtab[1][2]=1;codtab[2][2]=2; */
! 2925: /* for(i=1; i <=m ;i++){
! 2926: for(k=1; k <=cptcovn; k++){
! 2927: printf("i=%d k=%d %d %d ",i,k,codtab[i][k], cptcoveff);
! 2928: }
! 2929: printf("\n");
! 2930: }
! 2931: scanf("%d",i);*/
1.2 lievre 2932:
2933: /* Calculates basic frequencies. Computes observed prevalence at single age
2934: and prints on file fileres'p'. */
1.18 lievre 2935:
1.19 lievre 2936:
1.18 lievre 2937:
1.19 lievre 2938: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.2 lievre 2939: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
2940: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
2941: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
2942: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.12 lievre 2943:
1.2 lievre 2944: /* For Powell, parameters are in a vector p[] starting at p[1]
2945: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
2946: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
1.7 lievre 2947:
2948: if(mle==1){
1.2 lievre 2949: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
1.7 lievre 2950: }
1.2 lievre 2951:
2952: /*--------- results files --------------*/
1.34 brouard 2953: 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.19 lievre 2954:
1.16 lievre 2955:
1.2 lievre 2956: jk=1;
1.34 brouard 2957: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
2958: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
1.2 lievre 2959: for(i=1,jk=1; i <=nlstate; i++){
2960: for(k=1; k <=(nlstate+ndeath); k++){
2961: if (k != i)
2962: {
2963: printf("%d%d ",i,k);
2964: fprintf(ficres,"%1d%1d ",i,k);
2965: for(j=1; j <=ncovmodel; j++){
2966: printf("%f ",p[jk]);
2967: fprintf(ficres,"%f ",p[jk]);
2968: jk++;
2969: }
2970: printf("\n");
2971: fprintf(ficres,"\n");
2972: }
2973: }
2974: }
1.7 lievre 2975: if(mle==1){
1.2 lievre 2976: /* Computing hessian and covariance matrix */
2977: ftolhess=ftol; /* Usually correct */
2978: hesscov(matcov, p, npar, delti, ftolhess, func);
1.7 lievre 2979: }
1.34 brouard 2980: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
2981: printf("# Scales (for hessian or gradient estimation)\n");
1.2 lievre 2982: for(i=1,jk=1; i <=nlstate; i++){
2983: for(j=1; j <=nlstate+ndeath; j++){
2984: if (j!=i) {
2985: fprintf(ficres,"%1d%1d",i,j);
2986: printf("%1d%1d",i,j);
2987: for(k=1; k<=ncovmodel;k++){
2988: printf(" %.5e",delti[jk]);
2989: fprintf(ficres," %.5e",delti[jk]);
2990: jk++;
2991: }
2992: printf("\n");
2993: fprintf(ficres,"\n");
2994: }
2995: }
1.18 lievre 2996: }
1.2 lievre 2997:
2998: k=1;
1.34 brouard 2999: 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");
3000: 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");
1.2 lievre 3001: for(i=1;i<=npar;i++){
3002: /* if (k>nlstate) k=1;
3003: i1=(i-1)/(ncovmodel*nlstate)+1;
3004: fprintf(ficres,"%s%d%d",alph[k],i1,tab[i]);
3005: printf("%s%d%d",alph[k],i1,tab[i]);*/
3006: fprintf(ficres,"%3d",i);
3007: printf("%3d",i);
3008: for(j=1; j<=i;j++){
3009: fprintf(ficres," %.5e",matcov[i][j]);
3010: printf(" %.5e",matcov[i][j]);
3011: }
3012: fprintf(ficres,"\n");
3013: printf("\n");
3014: k++;
3015: }
3016:
3017: while((c=getc(ficpar))=='#' && c!= EOF){
3018: ungetc(c,ficpar);
3019: fgets(line, MAXLINE, ficpar);
3020: puts(line);
3021: fputs(line,ficparo);
3022: }
3023: ungetc(c,ficpar);
3024:
1.35 ! lievre 3025: fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf\n",&ageminpar,&agemaxpar, &bage, &fage);
1.2 lievre 3026:
3027: if (fage <= 2) {
1.35 ! lievre 3028: bage = ageminpar;
1.28 lievre 3029: fage = agemaxpar;
1.2 lievre 3030: }
1.22 brouard 3031:
3032: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.35 ! lievre 3033: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",ageminpar,agemaxpar,bage,fage);
! 3034: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",ageminpar,agemaxpar,bage,fage);
1.19 lievre 3035:
3036: while((c=getc(ficpar))=='#' && c!= EOF){
3037: ungetc(c,ficpar);
3038: fgets(line, MAXLINE, ficpar);
3039: puts(line);
3040: fputs(line,ficparo);
3041: }
3042: ungetc(c,ficpar);
3043:
1.25 lievre 3044: fscanf(ficpar,"begin-prev-date=%lf/%lf/%lf end-prev-date=%lf/%lf/%lf\n",&jprev1, &mprev1,&anprev1,&jprev2, &mprev2,&anprev2);
3045: fprintf(ficparo,"begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);
3046: fprintf(ficres,"begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);
1.19 lievre 3047:
3048: while((c=getc(ficpar))=='#' && c!= EOF){
3049: ungetc(c,ficpar);
3050: fgets(line, MAXLINE, ficpar);
3051: puts(line);
3052: fputs(line,ficparo);
3053: }
3054: ungetc(c,ficpar);
3055:
1.7 lievre 3056:
1.19 lievre 3057: dateprev1=anprev1+mprev1/12.+jprev1/365.;
3058: dateprev2=anprev2+mprev2/12.+jprev2/365.;
3059:
3060: fscanf(ficpar,"pop_based=%d\n",&popbased);
1.28 lievre 3061: fprintf(ficparo,"pop_based=%d\n",popbased);
3062: fprintf(ficres,"pop_based=%d\n",popbased);
3063:
3064: while((c=getc(ficpar))=='#' && c!= EOF){
3065: ungetc(c,ficpar);
3066: fgets(line, MAXLINE, ficpar);
3067: puts(line);
3068: fputs(line,ficparo);
3069: }
3070: ungetc(c,ficpar);
1.19 lievre 3071:
1.28 lievre 3072: fscanf(ficpar,"starting-proj-date=%lf/%lf/%lf final-proj-date=%lf/%lf/%lf mov_average=%d\n",&jproj1,&mproj1,&anproj1,&jproj2,&mproj2,&anproj2,&mobilav);
3073: fprintf(ficparo,"starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mov_average=%d\n",jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilav);
3074: fprintf(ficres,"starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mov_average=%d\n",jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilav);
3075:
3076:
3077: while((c=getc(ficpar))=='#' && c!= EOF){
1.19 lievre 3078: ungetc(c,ficpar);
3079: fgets(line, MAXLINE, ficpar);
3080: puts(line);
3081: fputs(line,ficparo);
3082: }
3083: ungetc(c,ficpar);
1.28 lievre 3084:
3085: fscanf(ficpar,"popforecast=%d popfile=%s popfiledate=%lf/%lf/%lf last-popfiledate=%lf/%lf/%lf\n",&popforecast,popfile,&jpyram,&mpyram,&anpyram,&jpyram1,&mpyram1,&anpyram1);
3086: fprintf(ficparo,"popforecast=%d popfile=%s popfiledate=%.lf/%.lf/%.lf last-popfiledate=%.lf/%.lf/%.lf\n",popforecast,popfile,jpyram,mpyram,anpyram,jpyram1,mpyram1,anpyram1);
3087: fprintf(ficres,"popforecast=%d popfile=%s popfiledate=%.lf/%.lf/%.lf last-popfiledate=%.lf/%.lf/%.lf\n",popforecast,popfile,jpyram,mpyram,anpyram,jpyram1,mpyram1,anpyram1);
1.19 lievre 3088:
1.26 lievre 3089: freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax,mint,anint,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);
1.19 lievre 3090:
1.25 lievre 3091: /*------------ gnuplot -------------*/
1.35 ! lievre 3092: printinggnuplot(fileres,optionfilefiname,optionfile,optionfilegnuplot, ageminpar,agemaxpar,fage, pathc,p);
1.25 lievre 3093:
3094: /*------------ free_vector -------------*/
3095: chdir(path);
1.2 lievre 3096:
1.25 lievre 3097: free_ivector(wav,1,imx);
3098: free_imatrix(dh,1,lastpass-firstpass+1,1,imx);
3099: free_imatrix(mw,1,lastpass-firstpass+1,1,imx);
3100: free_ivector(num,1,n);
3101: free_vector(agedc,1,n);
3102: /*free_matrix(covar,1,NCOVMAX,1,n);*/
3103: fclose(ficparo);
3104: fclose(ficres);
1.28 lievre 3105:
1.2 lievre 3106: /*--------- index.htm --------*/
3107:
1.35 ! lievre 3108: printinghtml(fileres,title,datafile, firstpass, lastpass, stepm, weightopt,model,imx,jmin,jmax,jmean,optionfile,optionfilehtm,rfileres,optionfilegnuplot,version,popforecast);
1.2 lievre 3109:
1.25 lievre 3110:
1.2 lievre 3111: /*--------------- Prevalence limit --------------*/
3112:
3113: strcpy(filerespl,"pl");
3114: strcat(filerespl,fileres);
3115: if((ficrespl=fopen(filerespl,"w"))==NULL) {
3116: printf("Problem with Prev limit resultfile: %s\n", filerespl);goto end;
3117: }
3118: printf("Computing prevalence limit: result on file '%s' \n", filerespl);
3119: fprintf(ficrespl,"#Prevalence limit\n");
3120: fprintf(ficrespl,"#Age ");
3121: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
3122: fprintf(ficrespl,"\n");
3123:
3124: prlim=matrix(1,nlstate,1,nlstate);
3125: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
3126: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
3127: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
3128: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
3129: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
3130: k=0;
1.35 ! lievre 3131: agebase=ageminpar;
1.28 lievre 3132: agelim=agemaxpar;
1.2 lievre 3133: ftolpl=1.e-10;
1.7 lievre 3134: i1=cptcoveff;
1.2 lievre 3135: if (cptcovn < 1){i1=1;}
3136:
3137: for(cptcov=1;cptcov<=i1;cptcov++){
3138: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
3139: k=k+1;
3140: /*printf("cptcov=%d cptcod=%d codtab=%d nbcode=%d\n",cptcov, cptcod,Tcode[cptcode],codtab[cptcod][cptcov]);*/
1.6 lievre 3141: fprintf(ficrespl,"\n#******");
1.7 lievre 3142: for(j=1;j<=cptcoveff;j++)
3143: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
1.2 lievre 3144: fprintf(ficrespl,"******\n");
3145:
3146: for (age=agebase; age<=agelim; age++){
3147: prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
3148: fprintf(ficrespl,"%.0f",age );
3149: for(i=1; i<=nlstate;i++)
3150: fprintf(ficrespl," %.5f", prlim[i][i]);
3151: fprintf(ficrespl,"\n");
3152: }
3153: }
3154: }
3155: fclose(ficrespl);
1.13 lievre 3156:
1.2 lievre 3157: /*------------- h Pij x at various ages ------------*/
3158:
3159: strcpy(filerespij,"pij"); strcat(filerespij,fileres);
3160: if((ficrespij=fopen(filerespij,"w"))==NULL) {
3161: printf("Problem with Pij resultfile: %s\n", filerespij);goto end;
3162: }
3163: printf("Computing pij: result on file '%s' \n", filerespij);
3164:
3165: stepsize=(int) (stepm+YEARM-1)/YEARM;
1.13 lievre 3166: /*if (stepm<=24) stepsize=2;*/
1.2 lievre 3167:
3168: agelim=AGESUP;
3169: hstepm=stepsize*YEARM; /* Every year of age */
3170: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
3171:
3172: k=0;
3173: for(cptcov=1;cptcov<=i1;cptcov++){
3174: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
3175: k=k+1;
3176: fprintf(ficrespij,"\n#****** ");
1.7 lievre 3177: for(j=1;j<=cptcoveff;j++)
3178: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
1.2 lievre 3179: fprintf(ficrespij,"******\n");
3180:
3181: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
3182: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
3183: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
3184: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3185: oldm=oldms;savm=savms;
3186: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
3187: fprintf(ficrespij,"# Age");
3188: for(i=1; i<=nlstate;i++)
3189: for(j=1; j<=nlstate+ndeath;j++)
3190: fprintf(ficrespij," %1d-%1d",i,j);
3191: fprintf(ficrespij,"\n");
3192: for (h=0; h<=nhstepm; h++){
3193: fprintf(ficrespij,"%d %.0f %.0f",k,agedeb, agedeb+ h*hstepm/YEARM*stepm );
3194: for(i=1; i<=nlstate;i++)
3195: for(j=1; j<=nlstate+ndeath;j++)
3196: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
3197: fprintf(ficrespij,"\n");
3198: }
3199: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
3200: fprintf(ficrespij,"\n");
3201: }
3202: }
3203: }
3204:
1.13 lievre 3205: /* varprob(fileres, matcov, p, delti, nlstate, (int) bage, (int) fage,k);*/
3206:
1.2 lievre 3207: fclose(ficrespij);
3208:
1.27 lievre 3209:
3210: /*---------- Forecasting ------------------*/
1.32 brouard 3211: if((stepm == 1) && (strcmp(model,".")==0)){
1.27 lievre 3212: prevforecast(fileres, anproj1,mproj1,jproj1, agemin,agemax, dateprev1, dateprev2,mobilav, agedeb, fage, popforecast, popfile, anproj2,p, i1);
1.32 brouard 3213: if (popforecast==1) populforecast(fileres, anpyram,mpyram,jpyram, agemin,agemax, dateprev1, dateprev2,mobilav, agedeb, fage, popforecast, popfile, anpyram1,p, i1);
1.27 lievre 3214: free_matrix(mint,1,maxwav,1,n);
3215: free_matrix(anint,1,maxwav,1,n); free_imatrix(s,1,maxwav+1,1,n);
3216: free_vector(weight,1,n);}
1.21 lievre 3217: else{
3218: erreur=108;
1.32 brouard 3219: 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);
1.21 lievre 3220: }
1.26 lievre 3221:
1.27 lievre 3222:
1.2 lievre 3223: /*---------- Health expectancies and variances ------------*/
3224:
3225: strcpy(filerest,"t");
3226: strcat(filerest,fileres);
3227: if((ficrest=fopen(filerest,"w"))==NULL) {
3228: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
3229: }
3230: printf("Computing Total LEs with variances: file '%s' \n", filerest);
3231:
3232:
3233: strcpy(filerese,"e");
3234: strcat(filerese,fileres);
3235: if((ficreseij=fopen(filerese,"w"))==NULL) {
3236: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
3237: }
3238: printf("Computing Health Expectancies: result on file '%s' \n", filerese);
3239:
3240: strcpy(fileresv,"v");
3241: strcat(fileresv,fileres);
3242: if((ficresvij=fopen(fileresv,"w"))==NULL) {
3243: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
3244: }
3245: printf("Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
3246:
3247: k=0;
3248: for(cptcov=1;cptcov<=i1;cptcov++){
3249: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
3250: k=k+1;
3251: fprintf(ficrest,"\n#****** ");
1.7 lievre 3252: for(j=1;j<=cptcoveff;j++)
3253: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
1.2 lievre 3254: fprintf(ficrest,"******\n");
3255:
3256: fprintf(ficreseij,"\n#****** ");
1.7 lievre 3257: for(j=1;j<=cptcoveff;j++)
1.35 ! lievre 3258: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
1.2 lievre 3259: fprintf(ficreseij,"******\n");
3260:
3261: fprintf(ficresvij,"\n#****** ");
1.7 lievre 3262: for(j=1;j<=cptcoveff;j++)
1.35 ! lievre 3263: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
1.2 lievre 3264: fprintf(ficresvij,"******\n");
3265:
3266: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
3267: oldm=oldms;savm=savms;
3268: evsij(fileres, eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k);
3269: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
3270: oldm=oldms;savm=savms;
1.26 lievre 3271: varevsij(fileres, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k);
3272:
3273:
3274:
1.2 lievre 3275: fprintf(ficrest,"#Total LEs with variances: e.. (std) ");
3276: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
3277: fprintf(ficrest,"\n");
1.26 lievre 3278:
1.2 lievre 3279: epj=vector(1,nlstate+1);
3280: for(age=bage; age <=fage ;age++){
3281: prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
1.14 lievre 3282: if (popbased==1) {
3283: for(i=1; i<=nlstate;i++)
3284: prlim[i][i]=probs[(int)age][i][k];
3285: }
3286:
1.33 brouard 3287: fprintf(ficrest," %4.0f",age);
1.2 lievre 3288: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
3289: for(i=1, epj[j]=0.;i <=nlstate;i++) {
1.33 brouard 3290: epj[j] += prlim[i][i]*eij[i][j][(int)age];
1.2 lievre 3291: }
3292: epj[nlstate+1] +=epj[j];
3293: }
3294: for(i=1, vepp=0.;i <=nlstate;i++)
3295: for(j=1;j <=nlstate;j++)
3296: vepp += vareij[i][j][(int)age];
1.33 brouard 3297: fprintf(ficrest," %7.2f (%7.2f)", epj[nlstate+1],sqrt(vepp));
1.2 lievre 3298: for(j=1;j <=nlstate;j++){
1.33 brouard 3299: fprintf(ficrest," %7.2f (%7.2f)", epj[j],sqrt(vareij[j][j][(int)age]));
1.2 lievre 3300: }
3301: fprintf(ficrest,"\n");
3302: }
3303: }
3304: }
1.13 lievre 3305:
1.27 lievre 3306: fclose(ficreseij);
3307: fclose(ficresvij);
1.2 lievre 3308: fclose(ficrest);
3309: fclose(ficpar);
3310: free_vector(epj,1,nlstate+1);
1.26 lievre 3311:
1.2 lievre 3312: /*------- Variance limit prevalence------*/
3313:
1.27 lievre 3314: strcpy(fileresvpl,"vpl");
1.2 lievre 3315: strcat(fileresvpl,fileres);
3316: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
3317: printf("Problem with variance prev lim resultfile: %s\n", fileresvpl);
3318: exit(0);
3319: }
3320: printf("Computing Variance-covariance of Prevalence limit: file '%s' \n", fileresvpl);
3321:
1.27 lievre 3322: k=0;
3323: for(cptcov=1;cptcov<=i1;cptcov++){
3324: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
3325: k=k+1;
3326: fprintf(ficresvpl,"\n#****** ");
3327: for(j=1;j<=cptcoveff;j++)
3328: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
3329: fprintf(ficresvpl,"******\n");
3330:
3331: varpl=matrix(1,nlstate,(int) bage, (int) fage);
3332: oldm=oldms;savm=savms;
1.2 lievre 3333: varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k);
1.27 lievre 3334: }
1.2 lievre 3335: }
3336:
3337: fclose(ficresvpl);
3338:
3339: /*---------- End : free ----------------*/
3340: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
3341:
3342: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
3343: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
3344:
3345:
3346: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
3347: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
3348: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
3349: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
1.13 lievre 3350:
1.2 lievre 3351: free_matrix(matcov,1,npar,1,npar);
3352: free_vector(delti,1,npar);
1.26 lievre 3353: free_matrix(agev,1,maxwav,1,imx);
1.2 lievre 3354: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
3355:
1.21 lievre 3356: if(erreur >0)
1.34 brouard 3357: printf("End of Imach with error or warning %d\n",erreur);
1.21 lievre 3358: else printf("End of Imach\n");
1.2 lievre 3359: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
3360:
3361: /* 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);*/
3362: /*printf("Total time was %d uSec.\n", total_usecs);*/
3363: /*------ End -----------*/
1.12 lievre 3364:
1.2 lievre 3365:
3366: end:
3367: #ifdef windows
1.22 brouard 3368: /* chdir(pathcd);*/
1.2 lievre 3369: #endif
1.22 brouard 3370: /*system("wgnuplot graph.plt");*/
3371: /*system("../gp37mgw/wgnuplot graph.plt");*/
3372: /*system("cd ../gp37mgw");*/
3373: /* system("..\\gp37mgw\\wgnuplot graph.plt");*/
3374: strcpy(plotcmd,GNUPLOTPROGRAM);
3375: strcat(plotcmd," ");
3376: strcat(plotcmd,optionfilegnuplot);
3377: system(plotcmd);
1.2 lievre 3378:
3379: #ifdef windows
3380: while (z[0] != 'q') {
1.35 ! lievre 3381: /* chdir(path); */
! 3382: printf("\nType e to edit output files, g to graph again, c to start again, and q for exiting: ");
1.2 lievre 3383: scanf("%s",z);
3384: if (z[0] == 'c') system("./imach");
1.35 ! lievre 3385: else if (z[0] == 'e') system(optionfilehtm);
! 3386: else if (z[0] == 'g') system(plotcmd);
1.2 lievre 3387: else if (z[0] == 'q') exit(0);
3388: }
3389: #endif
3390: }
3391:
3392:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>