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