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