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