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