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