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