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