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