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