Annotation of imach/src/imach.c, revision 1.12

1.2       lievre      1:     
                      2: /*********************** Imach **************************************        
                      3:   This program computes Healthy Life Expectancies from cross-longitudinal 
                      4:   data. Cross-longitudinal consist in a first survey ("cross") where 
                      5:   individuals from different ages are interviewed on their health status
                      6:   or degree of  disability. At least a second wave of interviews 
                      7:   ("longitudinal") should  measure each new individual health status. 
                      8:   Health expectancies are computed from the transistions observed between 
                      9:   waves and are computed for each degree of severity of disability (number
                     10:   of life states). More degrees you consider, more time is necessary to
1.11      lievre     11:   reach the Maximum Likelihood of the parameters involved in the model. 
1.2       lievre     12:   The simplest model is the multinomial logistic model where pij is
                     13:   the probabibility to be observed in state j at the second wave conditional
                     14:   to be observed in state i at the first wave. Therefore the model is:
                     15:   log(pij/pii)= aij + bij*age+ cij*sex + etc , where 'age' is age and 'sex' 
                     16:   is a covariate. If you want to have a more complex model than "constant and
                     17:   age", you should modify the program where the markup 
                     18:     *Covariates have to be included here again* invites you to do it.
                     19:   More covariates you add, less is the speed of the convergence.
                     20: 
                     21:   The advantage that this computer programme claims, comes from that if the 
                     22:   delay between waves is not identical for each individual, or if some 
                     23:   individual missed an interview, the information is not rounded or lost, but
                     24:   taken into account using an interpolation or extrapolation.
1.12    ! lievre     25:   hPijx is the probability to be 
1.2       lievre     26:   observed in state i at age x+h conditional to the observed state i at age 
                     27:   x. The delay 'h' can be split into an exact number (nh*stepm) of 
                     28:   unobserved intermediate  states. This elementary transition (by month or 
                     29:   quarter trimester, semester or year) is model as a multinomial logistic. 
                     30:   The hPx matrix is simply the matrix product of nh*stepm elementary matrices
                     31:   and the contribution of each individual to the likelihood is simply hPijx.
                     32: 
                     33:   Also this programme outputs the covariance matrix of the parameters but also
                     34:   of the life expectancies. It also computes the prevalence limits. 
                     35:   
                     36:   Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
                     37:            Institut national d'études démographiques, Paris.
                     38:   This software have been partly granted by Euro-REVES, a concerted action
                     39:   from the European Union.
                     40:   It is copyrighted identically to a GNU software product, ie programme and
                     41:   software can be distributed freely for non commercial use. Latest version
                     42:   can be accessed at http://euroreves.ined.fr/imach .
                     43:   **********************************************************************/
                     44:  
                     45: #include <math.h>
                     46: #include <stdio.h>
                     47: #include <stdlib.h>
                     48: #include <unistd.h>
                     49: 
                     50: #define MAXLINE 256
                     51: #define FILENAMELENGTH 80
                     52: /*#define DEBUG*/
                     53: #define windows
1.5       lievre     54: #define        GLOCK_ERROR_NOPATH              -1      /* empty path */
                     55: #define        GLOCK_ERROR_GETCWD              -2      /* cannot get cwd */
                     56: 
1.2       lievre     57: #define MAXPARM 30 /* Maximum number of parameters for the optimization */
                     58: #define NPARMAX 64 /* (nlstate+ndeath-1)*nlstate*ncovmodel */
                     59: 
                     60: #define NINTERVMAX 8
                     61: #define NLSTATEMAX 8 /* Maximum number of live states (for func) */
                     62: #define NDEATHMAX 8 /* Maximum number of dead states (for func) */
                     63: #define NCOVMAX 8 /* Maximum number of covariates */
1.3       lievre     64: #define MAXN 20000
1.2       lievre     65: #define YEARM 12. /* Number of months per year */
                     66: #define AGESUP 130
                     67: #define AGEBASE 40
                     68: 
                     69: 
                     70: int nvar;
1.8       lievre     71: int cptcovn, cptcovage=0, cptcoveff=0,cptcov;
1.2       lievre     72: int npar=NPARMAX;
                     73: int nlstate=2; /* Number of live states */
                     74: int ndeath=1; /* Number of dead states */
                     75: int ncovmodel, ncov;     /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
                     76: 
                     77: int *wav; /* Number of waves for this individuual 0 is possible */
                     78: int maxwav; /* Maxim number of waves */
1.8       lievre     79: int jmin, jmax; /* min, max spacing between 2 waves */
1.2       lievre     80: int mle, weightopt;
                     81: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
                     82: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
1.8       lievre     83: double jmean; /* Mean space between 2 waves */
1.2       lievre     84: double **oldm, **newm, **savm; /* Working pointers to matrices */
                     85: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
                     86: FILE *fic,*ficpar, *ficparo,*ficres,  *ficrespl, *ficrespij, *ficrest;
                     87: FILE *ficgp, *fichtm;
                     88: FILE *ficreseij;
                     89:   char filerese[FILENAMELENGTH];
                     90:  FILE  *ficresvij;
                     91:   char fileresv[FILENAMELENGTH];
                     92:  FILE  *ficresvpl;
                     93:   char fileresvpl[FILENAMELENGTH];
                     94: 
                     95: #define NR_END 1
                     96: #define FREE_ARG char*
                     97: #define FTOL 1.0e-10
                     98: 
                     99: #define NRANSI 
                    100: #define ITMAX 200 
                    101: 
                    102: #define TOL 2.0e-4 
                    103: 
                    104: #define CGOLD 0.3819660 
                    105: #define ZEPS 1.0e-10 
                    106: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d); 
                    107: 
                    108: #define GOLD 1.618034 
                    109: #define GLIMIT 100.0 
                    110: #define TINY 1.0e-20 
                    111: 
                    112: static double maxarg1,maxarg2;
                    113: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
                    114: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
                    115:  
                    116: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
                    117: #define rint(a) floor(a+0.5)
                    118: 
                    119: static double sqrarg;
                    120: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
                    121: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;} 
                    122: 
                    123: int imx; 
                    124: int stepm;
                    125: /* Stepm, step in month: minimum step interpolation*/
                    126: 
                    127: int m,nb;
1.6       lievre    128: int *num, firstpass=0, lastpass=4,*cod, *ncodemax, *Tage;
1.2       lievre    129: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
                    130: double **pmmij;
                    131: 
                    132: double *weight;
                    133: int **s; /* Status */
                    134: double *agedc, **covar, idx;
1.7       lievre    135: int **nbcode, *Tcode, *Tvar, **codtab, **Tvard, *Tprod, cptcovprod, *Tvaraff;
1.2       lievre    136: 
                    137: double ftol=FTOL; /* Tolerance for computing Max Likelihood */
                    138: double ftolhess; /* Tolerance for computing hessian */
                    139: 
1.7       lievre    140: /**************** split *************************/
1.5       lievre    141: static int split( char *path, char *dirc, char *name )
                    142: {
                    143:    char        *s;                             /* pointer */
                    144:    int l1, l2;                         /* length counters */
                    145: 
                    146:    l1 = strlen( path );                        /* length of path */
                    147:    if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
                    148:    s = strrchr( path, '\\' );          /* find last / */
                    149:    if ( s == NULL ) {                  /* no directory, so use current */
                    150: #if    defined(__bsd__)                /* get current working directory */
                    151:       extern char      *getwd( );
                    152: 
                    153:       if ( getwd( dirc ) == NULL ) {
                    154: #else
                    155:       extern char      *getcwd( );
                    156: 
                    157:       if ( getcwd( dirc, FILENAME_MAX ) == NULL ) {
                    158: #endif
                    159:          return( GLOCK_ERROR_GETCWD );
                    160:       }
                    161:       strcpy( name, path );            /* we've got it */
                    162:    } else {                            /* strip direcotry from path */
                    163:       s++;                             /* after this, the filename */
                    164:       l2 = strlen( s );                        /* length of filename */
                    165:       if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
                    166:       strcpy( name, s );               /* save file name */
                    167:       strncpy( dirc, path, l1 - l2 );  /* now the directory */
                    168:       dirc[l1-l2] = 0;                 /* add zero */
                    169:    }
                    170:    l1 = strlen( dirc );                        /* length of directory */
                    171:    if ( dirc[l1-1] != '\\' ) { dirc[l1] = '\\'; dirc[l1+1] = 0; }
                    172:    return( 0 );                                /* we're done */
                    173: }
                    174: 
                    175: 
1.2       lievre    176: /******************************************/
                    177: 
                    178: void replace(char *s, char*t)
                    179: {
                    180:   int i;
                    181:   int lg=20;
                    182:   i=0;
                    183:   lg=strlen(t);
                    184:   for(i=0; i<= lg; i++) {
                    185:     (s[i] = t[i]);
                    186:     if (t[i]== '\\') s[i]='/';
                    187:   }
                    188: }
                    189: 
                    190: int nbocc(char *s, char occ)
                    191: {
                    192:   int i,j=0;
                    193:   int lg=20;
                    194:   i=0;
                    195:   lg=strlen(s);
                    196:   for(i=0; i<= lg; i++) {
                    197:   if  (s[i] == occ ) j++;
                    198:   }
                    199:   return j;
                    200: }
                    201: 
                    202: void cutv(char *u,char *v, char*t, char occ)
                    203: {
1.6       lievre    204:   int i,lg,j,p=0;
1.2       lievre    205:   i=0;
                    206:   for(j=0; j<=strlen(t)-1; j++) {
1.3       lievre    207:     if((t[j]!= occ) && (t[j+1]== occ)) p=j+1;
1.2       lievre    208:   }
                    209: 
                    210:   lg=strlen(t);
                    211:   for(j=0; j<p; j++) {
                    212:     (u[j] = t[j]);
                    213:   }
1.6       lievre    214:      u[p]='\0';
1.2       lievre    215: 
                    216:    for(j=0; j<= lg; j++) {
                    217:     if (j>=(p+1))(v[j-p-1] = t[j]);
                    218:   }
                    219: }
                    220: 
                    221: /********************** nrerror ********************/
                    222: 
                    223: void nrerror(char error_text[])
                    224: {
                    225:   fprintf(stderr,"ERREUR ...\n");
                    226:   fprintf(stderr,"%s\n",error_text);
                    227:   exit(1);
                    228: }
                    229: /*********************** vector *******************/
                    230: double *vector(int nl, int nh)
                    231: {
                    232:   double *v;
                    233:   v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
                    234:   if (!v) nrerror("allocation failure in vector");
                    235:   return v-nl+NR_END;
                    236: }
                    237: 
                    238: /************************ free vector ******************/
                    239: void free_vector(double*v, int nl, int nh)
                    240: {
                    241:   free((FREE_ARG)(v+nl-NR_END));
                    242: }
                    243: 
                    244: /************************ivector *******************************/
                    245: int *ivector(long nl,long nh)
                    246: {
                    247:   int *v;
                    248:   v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
                    249:   if (!v) nrerror("allocation failure in ivector");
                    250:   return v-nl+NR_END;
                    251: }
                    252: 
                    253: /******************free ivector **************************/
                    254: void free_ivector(int *v, long nl, long nh)
                    255: {
                    256:   free((FREE_ARG)(v+nl-NR_END));
                    257: }
                    258: 
                    259: /******************* imatrix *******************************/
                    260: int **imatrix(long nrl, long nrh, long ncl, long nch) 
                    261:      /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */ 
                    262: { 
                    263:   long i, nrow=nrh-nrl+1,ncol=nch-ncl+1; 
                    264:   int **m; 
                    265:   
                    266:   /* allocate pointers to rows */ 
                    267:   m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*))); 
                    268:   if (!m) nrerror("allocation failure 1 in matrix()"); 
                    269:   m += NR_END; 
                    270:   m -= nrl; 
                    271:   
                    272:   
                    273:   /* allocate rows and set pointers to them */ 
                    274:   m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int))); 
                    275:   if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); 
                    276:   m[nrl] += NR_END; 
                    277:   m[nrl] -= ncl; 
                    278:   
                    279:   for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol; 
                    280:   
                    281:   /* return pointer to array of pointers to rows */ 
                    282:   return m; 
                    283: } 
                    284: 
                    285: /****************** free_imatrix *************************/
                    286: void free_imatrix(m,nrl,nrh,ncl,nch)
                    287:       int **m;
                    288:       long nch,ncl,nrh,nrl; 
                    289:      /* free an int matrix allocated by imatrix() */ 
                    290: { 
                    291:   free((FREE_ARG) (m[nrl]+ncl-NR_END)); 
                    292:   free((FREE_ARG) (m+nrl-NR_END)); 
                    293: } 
                    294: 
                    295: /******************* matrix *******************************/
                    296: double **matrix(long nrl, long nrh, long ncl, long nch)
                    297: {
                    298:   long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
                    299:   double **m;
                    300: 
                    301:   m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
                    302:   if (!m) nrerror("allocation failure 1 in matrix()");
                    303:   m += NR_END;
                    304:   m -= nrl;
                    305: 
                    306:   m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
                    307:   if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
                    308:   m[nrl] += NR_END;
                    309:   m[nrl] -= ncl;
                    310: 
                    311:   for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
                    312:   return m;
                    313: }
                    314: 
                    315: /*************************free matrix ************************/
                    316: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
                    317: {
                    318:   free((FREE_ARG)(m[nrl]+ncl-NR_END));
                    319:   free((FREE_ARG)(m+nrl-NR_END));
                    320: }
                    321: 
                    322: /******************* ma3x *******************************/
                    323: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
                    324: {
                    325:   long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
                    326:   double ***m;
                    327: 
                    328:   m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
                    329:   if (!m) nrerror("allocation failure 1 in matrix()");
                    330:   m += NR_END;
                    331:   m -= nrl;
                    332: 
                    333:   m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
                    334:   if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
                    335:   m[nrl] += NR_END;
                    336:   m[nrl] -= ncl;
                    337: 
                    338:   for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
                    339: 
                    340:   m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
                    341:   if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
                    342:   m[nrl][ncl] += NR_END;
                    343:   m[nrl][ncl] -= nll;
                    344:   for (j=ncl+1; j<=nch; j++) 
                    345:     m[nrl][j]=m[nrl][j-1]+nlay;
                    346:   
                    347:   for (i=nrl+1; i<=nrh; i++) {
                    348:     m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
                    349:     for (j=ncl+1; j<=nch; j++) 
                    350:       m[i][j]=m[i][j-1]+nlay;
                    351:   }
                    352:   return m;
                    353: }
                    354: 
                    355: /*************************free ma3x ************************/
                    356: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
                    357: {
                    358:   free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
                    359:   free((FREE_ARG)(m[nrl]+ncl-NR_END));
                    360:   free((FREE_ARG)(m+nrl-NR_END));
                    361: }
                    362: 
                    363: /***************** f1dim *************************/
                    364: extern int ncom; 
                    365: extern double *pcom,*xicom;
                    366: extern double (*nrfunc)(double []); 
                    367:  
                    368: double f1dim(double x) 
                    369: { 
                    370:   int j; 
                    371:   double f;
                    372:   double *xt; 
                    373:  
                    374:   xt=vector(1,ncom); 
                    375:   for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j]; 
                    376:   f=(*nrfunc)(xt); 
                    377:   free_vector(xt,1,ncom); 
                    378:   return f; 
                    379: } 
                    380: 
                    381: /*****************brent *************************/
                    382: double brent(double ax, double bx, double cx, double (*f)(double), double tol,         double *xmin) 
                    383: { 
                    384:   int iter; 
                    385:   double a,b,d,etemp;
                    386:   double fu,fv,fw,fx;
                    387:   double ftemp;
                    388:   double p,q,r,tol1,tol2,u,v,w,x,xm; 
                    389:   double e=0.0; 
                    390:  
                    391:   a=(ax < cx ? ax : cx); 
                    392:   b=(ax > cx ? ax : cx); 
                    393:   x=w=v=bx; 
                    394:   fw=fv=fx=(*f)(x); 
                    395:   for (iter=1;iter<=ITMAX;iter++) { 
                    396:     xm=0.5*(a+b); 
                    397:     tol2=2.0*(tol1=tol*fabs(x)+ZEPS); 
                    398:     /*         if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
                    399:     printf(".");fflush(stdout);
                    400: #ifdef DEBUG
                    401:     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);
                    402:     /*         if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
                    403: #endif
                    404:     if (fabs(x-xm) <= (tol2-0.5*(b-a))){ 
                    405:       *xmin=x; 
                    406:       return fx; 
                    407:     } 
                    408:     ftemp=fu;
                    409:     if (fabs(e) > tol1) { 
                    410:       r=(x-w)*(fx-fv); 
                    411:       q=(x-v)*(fx-fw); 
                    412:       p=(x-v)*q-(x-w)*r; 
                    413:       q=2.0*(q-r); 
                    414:       if (q > 0.0) p = -p; 
                    415:       q=fabs(q); 
                    416:       etemp=e; 
                    417:       e=d; 
                    418:       if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) 
                    419:        d=CGOLD*(e=(x >= xm ? a-x : b-x)); 
                    420:       else { 
                    421:        d=p/q; 
                    422:        u=x+d; 
                    423:        if (u-a < tol2 || b-u < tol2) 
                    424:          d=SIGN(tol1,xm-x); 
                    425:       } 
                    426:     } else { 
                    427:       d=CGOLD*(e=(x >= xm ? a-x : b-x)); 
                    428:     } 
                    429:     u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d)); 
                    430:     fu=(*f)(u); 
                    431:     if (fu <= fx) { 
                    432:       if (u >= x) a=x; else b=x; 
                    433:       SHFT(v,w,x,u) 
                    434:        SHFT(fv,fw,fx,fu) 
                    435:        } else { 
                    436:          if (u < x) a=u; else b=u; 
                    437:          if (fu <= fw || w == x) { 
                    438:            v=w; 
                    439:            w=u; 
                    440:            fv=fw; 
                    441:            fw=fu; 
                    442:          } else if (fu <= fv || v == x || v == w) { 
                    443:            v=u; 
                    444:            fv=fu; 
                    445:          } 
                    446:        } 
                    447:   } 
                    448:   nrerror("Too many iterations in brent"); 
                    449:   *xmin=x; 
                    450:   return fx; 
                    451: } 
                    452: 
                    453: /****************** mnbrak ***********************/
                    454: 
                    455: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc, 
                    456:            double (*func)(double)) 
                    457: { 
                    458:   double ulim,u,r,q, dum;
                    459:   double fu; 
                    460:  
                    461:   *fa=(*func)(*ax); 
                    462:   *fb=(*func)(*bx); 
                    463:   if (*fb > *fa) { 
                    464:     SHFT(dum,*ax,*bx,dum) 
                    465:       SHFT(dum,*fb,*fa,dum) 
                    466:       } 
                    467:   *cx=(*bx)+GOLD*(*bx-*ax); 
                    468:   *fc=(*func)(*cx); 
                    469:   while (*fb > *fc) { 
                    470:     r=(*bx-*ax)*(*fb-*fc); 
                    471:     q=(*bx-*cx)*(*fb-*fa); 
                    472:     u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/ 
                    473:       (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); 
                    474:     ulim=(*bx)+GLIMIT*(*cx-*bx); 
                    475:     if ((*bx-u)*(u-*cx) > 0.0) { 
                    476:       fu=(*func)(u); 
                    477:     } else if ((*cx-u)*(u-ulim) > 0.0) { 
                    478:       fu=(*func)(u); 
                    479:       if (fu < *fc) { 
                    480:        SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx)) 
                    481:          SHFT(*fb,*fc,fu,(*func)(u)) 
                    482:          } 
                    483:     } else if ((u-ulim)*(ulim-*cx) >= 0.0) { 
                    484:       u=ulim; 
                    485:       fu=(*func)(u); 
                    486:     } else { 
                    487:       u=(*cx)+GOLD*(*cx-*bx); 
                    488:       fu=(*func)(u); 
                    489:     } 
                    490:     SHFT(*ax,*bx,*cx,u) 
                    491:       SHFT(*fa,*fb,*fc,fu) 
                    492:       } 
                    493: } 
                    494: 
                    495: /*************** linmin ************************/
                    496: 
                    497: int ncom; 
                    498: double *pcom,*xicom;
                    499: double (*nrfunc)(double []); 
                    500:  
                    501: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double [])) 
                    502: { 
                    503:   double brent(double ax, double bx, double cx, 
                    504:               double (*f)(double), double tol, double *xmin); 
                    505:   double f1dim(double x); 
                    506:   void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, 
                    507:              double *fc, double (*func)(double)); 
                    508:   int j; 
                    509:   double xx,xmin,bx,ax; 
                    510:   double fx,fb,fa;
                    511:  
                    512:   ncom=n; 
                    513:   pcom=vector(1,n); 
                    514:   xicom=vector(1,n); 
                    515:   nrfunc=func; 
                    516:   for (j=1;j<=n;j++) { 
                    517:     pcom[j]=p[j]; 
                    518:     xicom[j]=xi[j]; 
                    519:   } 
                    520:   ax=0.0; 
                    521:   xx=1.0; 
                    522:   mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); 
                    523:   *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); 
                    524: #ifdef DEBUG
                    525:   printf("retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
                    526: #endif
                    527:   for (j=1;j<=n;j++) { 
                    528:     xi[j] *= xmin; 
                    529:     p[j] += xi[j]; 
                    530:   } 
                    531:   free_vector(xicom,1,n); 
                    532:   free_vector(pcom,1,n); 
                    533: } 
                    534: 
                    535: /*************** powell ************************/
                    536: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret, 
                    537:            double (*func)(double [])) 
                    538: { 
                    539:   void linmin(double p[], double xi[], int n, double *fret, 
                    540:              double (*func)(double [])); 
                    541:   int i,ibig,j; 
                    542:   double del,t,*pt,*ptt,*xit;
                    543:   double fp,fptt;
                    544:   double *xits;
                    545:   pt=vector(1,n); 
                    546:   ptt=vector(1,n); 
                    547:   xit=vector(1,n); 
                    548:   xits=vector(1,n); 
                    549:   *fret=(*func)(p); 
                    550:   for (j=1;j<=n;j++) pt[j]=p[j]; 
                    551:   for (*iter=1;;++(*iter)) { 
                    552:     fp=(*fret); 
                    553:     ibig=0; 
                    554:     del=0.0; 
                    555:     printf("\nPowell iter=%d -2*LL=%.12f",*iter,*fret);
                    556:     for (i=1;i<=n;i++) 
                    557:       printf(" %d %.12f",i, p[i]);
                    558:     printf("\n");
                    559:     for (i=1;i<=n;i++) { 
                    560:       for (j=1;j<=n;j++) xit[j]=xi[j][i]; 
                    561:       fptt=(*fret); 
                    562: #ifdef DEBUG
                    563:       printf("fret=%lf \n",*fret);
                    564: #endif
                    565:       printf("%d",i);fflush(stdout);
                    566:       linmin(p,xit,n,fret,func); 
                    567:       if (fabs(fptt-(*fret)) > del) { 
                    568:        del=fabs(fptt-(*fret)); 
                    569:        ibig=i; 
                    570:       } 
                    571: #ifdef DEBUG
                    572:       printf("%d %.12e",i,(*fret));
                    573:       for (j=1;j<=n;j++) {
                    574:        xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
                    575:        printf(" x(%d)=%.12e",j,xit[j]);
                    576:       }
                    577:       for(j=1;j<=n;j++) 
                    578:        printf(" p=%.12e",p[j]);
                    579:       printf("\n");
                    580: #endif
                    581:     } 
                    582:     if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) {
                    583: #ifdef DEBUG
                    584:       int k[2],l;
                    585:       k[0]=1;
                    586:       k[1]=-1;
                    587:       printf("Max: %.12e",(*func)(p));
                    588:       for (j=1;j<=n;j++) 
                    589:        printf(" %.12e",p[j]);
                    590:       printf("\n");
                    591:       for(l=0;l<=1;l++) {
                    592:        for (j=1;j<=n;j++) {
                    593:          ptt[j]=p[j]+(p[j]-pt[j])*k[l];
                    594:          printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
                    595:        }
                    596:        printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
                    597:       }
                    598: #endif
                    599: 
                    600: 
                    601:       free_vector(xit,1,n); 
                    602:       free_vector(xits,1,n); 
                    603:       free_vector(ptt,1,n); 
                    604:       free_vector(pt,1,n); 
                    605:       return; 
                    606:     } 
                    607:     if (*iter == ITMAX) nrerror("powell exceeding maximum iterations."); 
                    608:     for (j=1;j<=n;j++) { 
                    609:       ptt[j]=2.0*p[j]-pt[j]; 
                    610:       xit[j]=p[j]-pt[j]; 
                    611:       pt[j]=p[j]; 
                    612:     } 
                    613:     fptt=(*func)(ptt); 
                    614:     if (fptt < fp) { 
                    615:       t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); 
                    616:       if (t < 0.0) { 
                    617:        linmin(p,xit,n,fret,func); 
                    618:        for (j=1;j<=n;j++) { 
                    619:          xi[j][ibig]=xi[j][n]; 
                    620:          xi[j][n]=xit[j]; 
                    621:        }
                    622: #ifdef DEBUG
                    623:        printf("Direction changed  last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
                    624:        for(j=1;j<=n;j++)
                    625:          printf(" %.12e",xit[j]);
                    626:        printf("\n");
                    627: #endif
                    628:       } 
                    629:     } 
                    630:   } 
                    631: } 
                    632: 
                    633: /**** Prevalence limit ****************/
                    634: 
                    635: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int ij)
                    636: {
                    637:   /* Computes the prevalence limit in each live state at age x by left multiplying the unit
                    638:      matrix by transitions matrix until convergence is reached */
                    639: 
                    640:   int i, ii,j,k;
                    641:   double min, max, maxmin, maxmax,sumnew=0.;
                    642:   double **matprod2();
                    643:   double **out, cov[NCOVMAX], **pmij();
                    644:   double **newm;
                    645:   double agefin, delaymax=50 ; /* Max number of years to converge */
                    646: 
                    647:   for (ii=1;ii<=nlstate+ndeath;ii++)
                    648:     for (j=1;j<=nlstate+ndeath;j++){
                    649:       oldm[ii][j]=(ii==j ? 1.0 : 0.0);
                    650:     }
1.6       lievre    651: 
                    652:    cov[1]=1.;
                    653:  
                    654:  /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.2       lievre    655:   for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
                    656:     newm=savm;
                    657:     /* Covariates have to be included here again */
1.6       lievre    658:      cov[2]=agefin;
                    659:   
                    660:       for (k=1; k<=cptcovn;k++) {
1.7       lievre    661:        cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];
                    662:        /*printf("ij=%d Tvar[k]=%d nbcode=%d cov=%lf\n",ij, Tvar[k],nbcode[Tvar[k]][codtab[ij][Tvar[k]]],cov[2+k]);*/
1.6       lievre    663:       }
                    664:       for (k=1; k<=cptcovage;k++)
                    665:        cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
1.7       lievre    666:       for (k=1; k<=cptcovprod;k++)
                    667:        cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];
                    668: 
                    669:       /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
                    670:       /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
                    671: 
1.2       lievre    672:     out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
                    673: 
                    674:     savm=oldm;
                    675:     oldm=newm;
                    676:     maxmax=0.;
                    677:     for(j=1;j<=nlstate;j++){
                    678:       min=1.;
                    679:       max=0.;
                    680:       for(i=1; i<=nlstate; i++) {
                    681:        sumnew=0;
                    682:        for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
                    683:        prlim[i][j]= newm[i][j]/(1-sumnew);
                    684:        max=FMAX(max,prlim[i][j]);
                    685:        min=FMIN(min,prlim[i][j]);
                    686:       }
                    687:       maxmin=max-min;
                    688:       maxmax=FMAX(maxmax,maxmin);
                    689:     }
                    690:     if(maxmax < ftolpl){
                    691:       return prlim;
                    692:     }
                    693:   }
                    694: }
                    695: 
1.12    ! lievre    696: /*************** transition probabilities ***************/ 
1.2       lievre    697: 
                    698: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
                    699: {
                    700:   double s1, s2;
                    701:   /*double t34;*/
                    702:   int i,j,j1, nc, ii, jj;
                    703: 
                    704:     for(i=1; i<= nlstate; i++){
                    705:     for(j=1; j<i;j++){
                    706:       for (nc=1, s2=0.;nc <=ncovmodel; nc++){
                    707:        /*s2 += param[i][j][nc]*cov[nc];*/
                    708:        s2 += x[(i-1)*nlstate*ncovmodel+(j-1)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];
                    709:        /*printf("Int j<i s1=%.17e, s2=%.17e\n",s1,s2);*/
                    710:       }
                    711:       ps[i][j]=s2;
                    712:       /*printf("s1=%.17e, s2=%.17e\n",s1,s2);*/
                    713:     }
                    714:     for(j=i+1; j<=nlstate+ndeath;j++){
                    715:       for (nc=1, s2=0.;nc <=ncovmodel; nc++){
                    716:        s2 += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];
                    717:        /*printf("Int j>i s1=%.17e, s2=%.17e %lx %lx\n",s1,s2,s1,s2);*/
                    718:       }
1.12    ! lievre    719:       ps[i][j]=(s2);
1.2       lievre    720:     }
                    721:   }
1.12    ! lievre    722:     /*ps[3][2]=1;*/
        !           723: 
1.2       lievre    724:   for(i=1; i<= nlstate; i++){
                    725:      s1=0;
                    726:     for(j=1; j<i; j++)
                    727:       s1+=exp(ps[i][j]);
                    728:     for(j=i+1; j<=nlstate+ndeath; j++)
                    729:       s1+=exp(ps[i][j]);
                    730:     ps[i][i]=1./(s1+1.);
                    731:     for(j=1; j<i; j++)
                    732:       ps[i][j]= exp(ps[i][j])*ps[i][i];
                    733:     for(j=i+1; j<=nlstate+ndeath; j++)
                    734:       ps[i][j]= exp(ps[i][j])*ps[i][i];
                    735:     /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
                    736:   } /* end i */
                    737: 
                    738:   for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
                    739:     for(jj=1; jj<= nlstate+ndeath; jj++){
                    740:       ps[ii][jj]=0;
                    741:       ps[ii][ii]=1;
                    742:     }
                    743:   }
                    744: 
1.12    ! lievre    745: 
1.2       lievre    746:   /*   for(ii=1; ii<= nlstate+ndeath; ii++){
                    747:     for(jj=1; jj<= nlstate+ndeath; jj++){
                    748:      printf("%lf ",ps[ii][jj]);
                    749:    }
                    750:     printf("\n ");
                    751:     }
                    752:     printf("\n ");printf("%lf ",cov[2]);*/
                    753: /*
                    754:   for(i=1; i<= npar; i++) printf("%f ",x[i]);
                    755:   goto end;*/
                    756:     return ps;
                    757: }
                    758: 
                    759: /**************** Product of 2 matrices ******************/
                    760: 
                    761: double **matprod2(double **out, double **in,long nrl, long nrh, long ncl, long nch, long ncolol, long ncoloh, double **b)
                    762: {
                    763:   /* Computes the matric product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
                    764:      b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
                    765:   /* in, b, out are matrice of pointers which should have been initialized 
                    766:      before: only the contents of out is modified. The function returns
                    767:      a pointer to pointers identical to out */
                    768:   long i, j, k;
                    769:   for(i=nrl; i<= nrh; i++)
                    770:     for(k=ncolol; k<=ncoloh; k++)
                    771:       for(j=ncl,out[i][k]=0.; j<=nch; j++)
                    772:        out[i][k] +=in[i][j]*b[j][k];
                    773: 
                    774:   return out;
                    775: }
                    776: 
                    777: 
                    778: /************* Higher Matrix Product ***************/
                    779: 
                    780: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij )
                    781: {
                    782:   /* Computes the transition matrix starting at age 'age' over 'nhstepm*hstepm*stepm' month 
                    783:      duration (i.e. until
                    784:      age (in years)  age+nhstepm*stepm/12) by multiplying nhstepm*hstepm matrices. 
                    785:      Output is stored in matrix po[i][j][h] for h every 'hstepm' step 
                    786:      (typically every 2 years instead of every month which is too big).
                    787:      Model is determined by parameters x and covariates have to be 
                    788:      included manually here. 
                    789: 
                    790:      */
                    791: 
                    792:   int i, j, d, h, k;
                    793:   double **out, cov[NCOVMAX];
                    794:   double **newm;
                    795: 
                    796:   /* Hstepm could be zero and should return the unit matrix */
                    797:   for (i=1;i<=nlstate+ndeath;i++)
                    798:     for (j=1;j<=nlstate+ndeath;j++){
                    799:       oldm[i][j]=(i==j ? 1.0 : 0.0);
                    800:       po[i][j][0]=(i==j ? 1.0 : 0.0);
                    801:     }
                    802:   /* Even if hstepm = 1, at least one multiplication by the unit matrix */
                    803:   for(h=1; h <=nhstepm; h++){
                    804:     for(d=1; d <=hstepm; d++){
                    805:       newm=savm;
                    806:       /* Covariates have to be included here again */
                    807:       cov[1]=1.;
                    808:       cov[2]=age+((h-1)*hstepm + (d-1))*stepm/YEARM;
1.7       lievre    809:       for (k=1; k<=cptcovn;k++) cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];
1.12    ! lievre    810:       for (k=1; k<=cptcovage;k++)
1.7       lievre    811:        cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
1.12    ! lievre    812:       for (k=1; k<=cptcovprod;k++)
1.7       lievre    813:        cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];
                    814: 
                    815: 
1.2       lievre    816:       /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
                    817:       /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
                    818:       out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, 
                    819:                   pmij(pmmij,cov,ncovmodel,x,nlstate));
                    820:       savm=oldm;
                    821:       oldm=newm;
                    822:     }
                    823:     for(i=1; i<=nlstate+ndeath; i++)
                    824:       for(j=1;j<=nlstate+ndeath;j++) {
                    825:        po[i][j][h]=newm[i][j];
                    826:        /*printf("i=%d j=%d h=%d po[i][j][h]=%f ",i,j,h,po[i][j][h]);
                    827:         */
                    828:       }
                    829:   } /* end h */
                    830:   return po;
                    831: }
                    832: 
                    833: 
                    834: /*************** log-likelihood *************/
                    835: double func( double *x)
                    836: {
1.6       lievre    837:   int i, ii, j, k, mi, d, kk;
1.2       lievre    838:   double l, ll[NLSTATEMAX], cov[NCOVMAX];
                    839:   double **out;
                    840:   double sw; /* Sum of weights */
                    841:   double lli; /* Individual log likelihood */
                    842:   long ipmx;
                    843:   /*extern weight */
                    844:   /* We are differentiating ll according to initial status */
                    845:   /*  for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
                    846:   /*for(i=1;i<imx;i++) 
1.8       lievre    847:     printf(" %d\n",s[4][i]);
1.2       lievre    848:   */
1.6       lievre    849:   cov[1]=1.;
1.2       lievre    850: 
                    851:   for(k=1; k<=nlstate; k++) ll[k]=0.;
                    852:   for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.6       lievre    853:     for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
1.8       lievre    854:     for(mi=1; mi<= wav[i]-1; mi++){
1.2       lievre    855:       for (ii=1;ii<=nlstate+ndeath;ii++)
                    856:        for (j=1;j<=nlstate+ndeath;j++) oldm[ii][j]=(ii==j ? 1.0 : 0.0);
1.8       lievre    857:       for(d=0; d<dh[mi][i]; d++){
                    858:        newm=savm;
                    859:        cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
                    860:        for (kk=1; kk<=cptcovage;kk++) {
                    861:          cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
                    862:        }
                    863:        
                    864:        out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
                    865:                     1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
                    866:        savm=oldm;
                    867:        oldm=newm;
                    868:        
                    869:        
1.2       lievre    870:       } /* end mult */
1.8       lievre    871:       
1.2       lievre    872:       lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);
                    873:       /* printf(" %f ",out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/
                    874:       ipmx +=1;
                    875:       sw += weight[i];
                    876:       ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
                    877:     } /* end of wave */
                    878:   } /* end of individual */
                    879: 
                    880:   for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
                    881:   /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
                    882:   l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
                    883:   return -l;
                    884: }
                    885: 
                    886: 
                    887: /*********** Maximum Likelihood Estimation ***************/
                    888: 
                    889: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
                    890: {
                    891:   int i,j, iter;
                    892:   double **xi,*delti;
                    893:   double fret;
                    894:   xi=matrix(1,npar,1,npar);
                    895:   for (i=1;i<=npar;i++)
                    896:     for (j=1;j<=npar;j++)
                    897:       xi[i][j]=(i==j ? 1.0 : 0.0);
                    898:   printf("Powell\n");
                    899:   powell(p,xi,npar,ftol,&iter,&fret,func);
                    900: 
                    901:    printf("\n#Number of iterations = %d, -2 Log likelihood = %.12f\n",iter,func(p));
                    902:   fprintf(ficres,"#Number of iterations = %d, -2 Log likelihood = %.12f ",iter,func(p));
                    903: 
                    904: }
                    905: 
                    906: /**** Computes Hessian and covariance matrix ***/
                    907: void hesscov(double **matcov, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
                    908: {
                    909:   double  **a,**y,*x,pd;
                    910:   double **hess;
                    911:   int i, j,jk;
                    912:   int *indx;
                    913: 
                    914:   double hessii(double p[], double delta, int theta, double delti[]);
                    915:   double hessij(double p[], double delti[], int i, int j);
                    916:   void lubksb(double **a, int npar, int *indx, double b[]) ;
                    917:   void ludcmp(double **a, int npar, int *indx, double *d) ;
                    918: 
                    919:   hess=matrix(1,npar,1,npar);
                    920: 
                    921:   printf("\nCalculation of the hessian matrix. Wait...\n");
                    922:   for (i=1;i<=npar;i++){
                    923:     printf("%d",i);fflush(stdout);
                    924:     hess[i][i]=hessii(p,ftolhess,i,delti);
                    925:     /*printf(" %f ",p[i]);*/
1.12    ! lievre    926:     /*printf(" %lf ",hess[i][i]);*/
1.2       lievre    927:   }
1.12    ! lievre    928:   
1.2       lievre    929:   for (i=1;i<=npar;i++) {
                    930:     for (j=1;j<=npar;j++)  {
                    931:       if (j>i) { 
                    932:        printf(".%d%d",i,j);fflush(stdout);
                    933:        hess[i][j]=hessij(p,delti,i,j);
1.12    ! lievre    934:        hess[j][i]=hess[i][j];    
        !           935:        /*printf(" %lf ",hess[i][j]);*/
1.2       lievre    936:       }
                    937:     }
                    938:   }
                    939:   printf("\n");
                    940: 
                    941:   printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
                    942:   
                    943:   a=matrix(1,npar,1,npar);
                    944:   y=matrix(1,npar,1,npar);
                    945:   x=vector(1,npar);
                    946:   indx=ivector(1,npar);
                    947:   for (i=1;i<=npar;i++)
                    948:     for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
                    949:   ludcmp(a,npar,indx,&pd);
                    950: 
                    951:   for (j=1;j<=npar;j++) {
                    952:     for (i=1;i<=npar;i++) x[i]=0;
                    953:     x[j]=1;
                    954:     lubksb(a,npar,indx,x);
                    955:     for (i=1;i<=npar;i++){ 
                    956:       matcov[i][j]=x[i];
                    957:     }
                    958:   }
                    959: 
                    960:   printf("\n#Hessian matrix#\n");
                    961:   for (i=1;i<=npar;i++) { 
                    962:     for (j=1;j<=npar;j++) { 
                    963:       printf("%.3e ",hess[i][j]);
                    964:     }
                    965:     printf("\n");
                    966:   }
                    967: 
                    968:   /* Recompute Inverse */
                    969:   for (i=1;i<=npar;i++)
                    970:     for (j=1;j<=npar;j++) a[i][j]=matcov[i][j];
                    971:   ludcmp(a,npar,indx,&pd);
                    972: 
                    973:   /*  printf("\n#Hessian matrix recomputed#\n");
                    974: 
                    975:   for (j=1;j<=npar;j++) {
                    976:     for (i=1;i<=npar;i++) x[i]=0;
                    977:     x[j]=1;
                    978:     lubksb(a,npar,indx,x);
                    979:     for (i=1;i<=npar;i++){ 
                    980:       y[i][j]=x[i];
                    981:       printf("%.3e ",y[i][j]);
                    982:     }
                    983:     printf("\n");
                    984:   }
                    985:   */
                    986: 
                    987:   free_matrix(a,1,npar,1,npar);
                    988:   free_matrix(y,1,npar,1,npar);
                    989:   free_vector(x,1,npar);
                    990:   free_ivector(indx,1,npar);
                    991:   free_matrix(hess,1,npar,1,npar);
                    992: 
                    993: 
                    994: }
                    995: 
                    996: /*************** hessian matrix ****************/
                    997: double hessii( double x[], double delta, int theta, double delti[])
                    998: {
                    999:   int i;
                   1000:   int l=1, lmax=20;
                   1001:   double k1,k2;
                   1002:   double p2[NPARMAX+1];
                   1003:   double res;
                   1004:   double delt, delts, nkhi=10.,nkhif=1., khi=1.e-4;
                   1005:   double fx;
                   1006:   int k=0,kmax=10;
                   1007:   double l1;
                   1008: 
                   1009:   fx=func(x);
                   1010:   for (i=1;i<=npar;i++) p2[i]=x[i];
                   1011:   for(l=0 ; l <=lmax; l++){
                   1012:     l1=pow(10,l);
                   1013:     delts=delt;
                   1014:     for(k=1 ; k <kmax; k=k+1){
                   1015:       delt = delta*(l1*k);
                   1016:       p2[theta]=x[theta] +delt;
                   1017:       k1=func(p2)-fx;
                   1018:       p2[theta]=x[theta]-delt;
                   1019:       k2=func(p2)-fx;
                   1020:       /*res= (k1-2.0*fx+k2)/delt/delt; */
                   1021:       res= (k1+k2)/delt/delt/2.; /* Divided by because L and not 2*L */
                   1022:       
                   1023: #ifdef DEBUG
                   1024:       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);
                   1025: #endif
                   1026:       /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
                   1027:       if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
                   1028:        k=kmax;
                   1029:       }
                   1030:       else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
                   1031:        k=kmax; l=lmax*10.;
                   1032:       }
                   1033:       else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ 
                   1034:        delts=delt;
                   1035:       }
                   1036:     }
                   1037:   }
                   1038:   delti[theta]=delts;
1.12    ! lievre   1039:   return res; 
1.3       lievre   1040:   
1.2       lievre   1041: }
                   1042: 
                   1043: double hessij( double x[], double delti[], int thetai,int thetaj)
                   1044: {
                   1045:   int i;
                   1046:   int l=1, l1, lmax=20;
                   1047:   double k1,k2,k3,k4,res,fx;
                   1048:   double p2[NPARMAX+1];
                   1049:   int k;
                   1050: 
                   1051:   fx=func(x);
                   1052:   for (k=1; k<=2; k++) {
                   1053:     for (i=1;i<=npar;i++) p2[i]=x[i];
                   1054:     p2[thetai]=x[thetai]+delti[thetai]/k;
                   1055:     p2[thetaj]=x[thetaj]+delti[thetaj]/k;
                   1056:     k1=func(p2)-fx;
                   1057:   
                   1058:     p2[thetai]=x[thetai]+delti[thetai]/k;
                   1059:     p2[thetaj]=x[thetaj]-delti[thetaj]/k;
                   1060:     k2=func(p2)-fx;
                   1061:   
                   1062:     p2[thetai]=x[thetai]-delti[thetai]/k;
                   1063:     p2[thetaj]=x[thetaj]+delti[thetaj]/k;
                   1064:     k3=func(p2)-fx;
                   1065:   
                   1066:     p2[thetai]=x[thetai]-delti[thetai]/k;
                   1067:     p2[thetaj]=x[thetaj]-delti[thetaj]/k;
                   1068:     k4=func(p2)-fx;
                   1069:     res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /* Because of L not 2*L */
                   1070: #ifdef DEBUG
                   1071:     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);
                   1072: #endif
                   1073:   }
                   1074:   return res;
                   1075: }
                   1076: 
                   1077: /************** Inverse of matrix **************/
                   1078: void ludcmp(double **a, int n, int *indx, double *d) 
                   1079: { 
                   1080:   int i,imax,j,k; 
                   1081:   double big,dum,sum,temp; 
                   1082:   double *vv; 
                   1083:  
                   1084:   vv=vector(1,n); 
                   1085:   *d=1.0; 
                   1086:   for (i=1;i<=n;i++) { 
                   1087:     big=0.0; 
                   1088:     for (j=1;j<=n;j++) 
                   1089:       if ((temp=fabs(a[i][j])) > big) big=temp; 
                   1090:     if (big == 0.0) nrerror("Singular matrix in routine ludcmp"); 
                   1091:     vv[i]=1.0/big; 
                   1092:   } 
                   1093:   for (j=1;j<=n;j++) { 
                   1094:     for (i=1;i<j;i++) { 
                   1095:       sum=a[i][j]; 
                   1096:       for (k=1;k<i;k++) sum -= a[i][k]*a[k][j]; 
                   1097:       a[i][j]=sum; 
                   1098:     } 
                   1099:     big=0.0; 
                   1100:     for (i=j;i<=n;i++) { 
                   1101:       sum=a[i][j]; 
                   1102:       for (k=1;k<j;k++) 
                   1103:        sum -= a[i][k]*a[k][j]; 
                   1104:       a[i][j]=sum; 
                   1105:       if ( (dum=vv[i]*fabs(sum)) >= big) { 
                   1106:        big=dum; 
                   1107:        imax=i; 
                   1108:       } 
                   1109:     } 
                   1110:     if (j != imax) { 
                   1111:       for (k=1;k<=n;k++) { 
                   1112:        dum=a[imax][k]; 
                   1113:        a[imax][k]=a[j][k]; 
                   1114:        a[j][k]=dum; 
                   1115:       } 
                   1116:       *d = -(*d); 
                   1117:       vv[imax]=vv[j]; 
                   1118:     } 
                   1119:     indx[j]=imax; 
                   1120:     if (a[j][j] == 0.0) a[j][j]=TINY; 
                   1121:     if (j != n) { 
                   1122:       dum=1.0/(a[j][j]); 
                   1123:       for (i=j+1;i<=n;i++) a[i][j] *= dum; 
                   1124:     } 
                   1125:   } 
                   1126:   free_vector(vv,1,n);  /* Doesn't work */
                   1127: ;
                   1128: } 
                   1129: 
                   1130: void lubksb(double **a, int n, int *indx, double b[]) 
                   1131: { 
                   1132:   int i,ii=0,ip,j; 
                   1133:   double sum; 
                   1134:  
                   1135:   for (i=1;i<=n;i++) { 
                   1136:     ip=indx[i]; 
                   1137:     sum=b[ip]; 
                   1138:     b[ip]=b[i]; 
                   1139:     if (ii) 
                   1140:       for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j]; 
                   1141:     else if (sum) ii=i; 
                   1142:     b[i]=sum; 
                   1143:   } 
                   1144:   for (i=n;i>=1;i--) { 
                   1145:     sum=b[i]; 
                   1146:     for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j]; 
                   1147:     b[i]=sum/a[i][i]; 
                   1148:   } 
                   1149: } 
                   1150: 
                   1151: /************ Frequencies ********************/
                   1152: void  freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax)
                   1153: {  /* Some frequencies */
                   1154:  
                   1155:   int i, m, jk, k1, i1, j1, bool, z1,z2,j;
                   1156:   double ***freq; /* Frequencies */
                   1157:   double *pp;
                   1158:   double pos;
                   1159:   FILE *ficresp;
                   1160:   char fileresp[FILENAMELENGTH];
                   1161: 
                   1162:   pp=vector(1,nlstate);
                   1163: 
                   1164:   strcpy(fileresp,"p");
                   1165:   strcat(fileresp,fileres);
                   1166:   if((ficresp=fopen(fileresp,"w"))==NULL) {
                   1167:     printf("Problem with prevalence resultfile: %s\n", fileresp);
                   1168:     exit(0);
                   1169:   }
                   1170:   freq= ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,agemin,agemax+3);
                   1171:   j1=0;
                   1172: 
1.7       lievre   1173:   j=cptcoveff;
1.2       lievre   1174:   if (cptcovn<1) {j=1;ncodemax[1]=1;}
                   1175: 
                   1176:   for(k1=1; k1<=j;k1++){
                   1177:    for(i1=1; i1<=ncodemax[k1];i1++){
                   1178:        j1++;
1.8       lievre   1179:        /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
                   1180:         scanf("%d", i);*/
1.2       lievre   1181:         for (i=-1; i<=nlstate+ndeath; i++)  
                   1182:         for (jk=-1; jk<=nlstate+ndeath; jk++)  
                   1183:           for(m=agemin; m <= agemax+3; m++)
                   1184:             freq[i][jk][m]=0;
                   1185:        
                   1186:        for (i=1; i<=imx; i++) {
                   1187:         bool=1;
                   1188:         if  (cptcovn>0) {
1.7       lievre   1189:           for (z1=1; z1<=cptcoveff; z1++) 
1.8       lievre   1190:             if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtab[j1][z1]]) 
                   1191:               bool=0;
1.2       lievre   1192:         }
                   1193:          if (bool==1) {
                   1194:           for(m=firstpass; m<=lastpass-1; m++){
                   1195:             if(agev[m][i]==0) agev[m][i]=agemax+1;
                   1196:             if(agev[m][i]==1) agev[m][i]=agemax+2;
                   1197:             freq[s[m][i]][s[m+1][i]][(int)agev[m][i]] += weight[i];
                   1198:             freq[s[m][i]][s[m+1][i]][(int) agemax+3] += weight[i];
                   1199:           }
                   1200:         }
                   1201:        }
                   1202:         if  (cptcovn>0) {
1.7       lievre   1203:         fprintf(ficresp, "\n#********** Variable "); 
                   1204:         for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
                   1205:        fprintf(ficresp, "**********\n#");
1.8       lievre   1206:        }
1.2       lievre   1207:        for(i=1; i<=nlstate;i++) 
                   1208:         fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
                   1209:        fprintf(ficresp, "\n");
                   1210:        
                   1211:   for(i=(int)agemin; i <= (int)agemax+3; i++){
                   1212:     if(i==(int)agemax+3)
                   1213:       printf("Total");
                   1214:     else
                   1215:       printf("Age %d", i);
                   1216:     for(jk=1; jk <=nlstate ; jk++){
                   1217:       for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
                   1218:        pp[jk] += freq[jk][m][i];
                   1219:     }
                   1220:     for(jk=1; jk <=nlstate ; jk++){
                   1221:       for(m=-1, pos=0; m <=0 ; m++)
                   1222:        pos += freq[jk][m][i];
                   1223:       if(pp[jk]>=1.e-10)
                   1224:        printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
                   1225:       else
                   1226:         printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
                   1227:     }
                   1228:     for(jk=1; jk <=nlstate ; jk++){
                   1229:       for(m=1, pp[jk]=0; m <=nlstate+ndeath; m++)
                   1230:        pp[jk] += freq[jk][m][i];
                   1231:     }
                   1232:     for(jk=1,pos=0; jk <=nlstate ; jk++)
                   1233:       pos += pp[jk];
                   1234:     for(jk=1; jk <=nlstate ; jk++){
                   1235:       if(pos>=1.e-5)
                   1236:        printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
                   1237:       else
                   1238:        printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
                   1239:       if( i <= (int) agemax){
                   1240:        if(pos>=1.e-5)
                   1241:          fprintf(ficresp," %d %.5f %.0f %.0f",i,pp[jk]/pos, pp[jk],pos);
                   1242:       else
                   1243:          fprintf(ficresp," %d NaNq %.0f %.0f",i,pp[jk],pos);
                   1244:       }
                   1245:     }
                   1246:     for(jk=-1; jk <=nlstate+ndeath; jk++)
                   1247:       for(m=-1; m <=nlstate+ndeath; m++)
                   1248:        if(freq[jk][m][i] !=0 ) printf(" %d%d=%.0f",jk,m,freq[jk][m][i]);
                   1249:     if(i <= (int) agemax)
                   1250:       fprintf(ficresp,"\n");
                   1251:     printf("\n");
                   1252:     }
                   1253:     }
                   1254:  }
                   1255:  
                   1256:   fclose(ficresp);
                   1257:   free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath,(int) agemin,(int) agemax+3);
                   1258:   free_vector(pp,1,nlstate);
                   1259: 
                   1260: }  /* End of Freq */
                   1261: 
                   1262: /************* Waves Concatenation ***************/
                   1263: 
                   1264: void  concatwav(int wav[], int **dh, int **mw, int **s, double *agedc, double **agev, int  firstpass, int lastpass, int imx, int nlstate, int stepm)
                   1265: {
                   1266:   /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
                   1267:      Death is a valid wave (if date is known).
                   1268:      mw[mi][i] is the mi (mi=1 to wav[i])  effective wave of individual i
                   1269:      dh[m][i] of dh[mw[mi][i][i] is the delay between two effective waves m=mw[mi][i]
                   1270:      and mw[mi+1][i]. dh depends on stepm.
                   1271:      */
                   1272: 
                   1273:   int i, mi, m;
1.8       lievre   1274:   /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
                   1275:      double sum=0., jmean=0.;*/
1.2       lievre   1276: 
1.11      lievre   1277:   int j, k=0,jk, ju, jl;
                   1278:   double sum=0.;
                   1279:   jmin=1e+5;
                   1280:   jmax=-1;
                   1281:   jmean=0.;
1.2       lievre   1282:   for(i=1; i<=imx; i++){
                   1283:     mi=0;
                   1284:     m=firstpass;
                   1285:     while(s[m][i] <= nlstate){
                   1286:       if(s[m][i]>=1)
                   1287:        mw[++mi][i]=m;
                   1288:       if(m >=lastpass)
                   1289:        break;
                   1290:       else
                   1291:        m++;
                   1292:     }/* end while */
                   1293:     if (s[m][i] > nlstate){
                   1294:       mi++;    /* Death is another wave */
                   1295:       /* if(mi==0)  never been interviewed correctly before death */
                   1296:         /* Only death is a correct wave */
                   1297:       mw[mi][i]=m;
                   1298:     }
                   1299: 
                   1300:     wav[i]=mi;
                   1301:     if(mi==0)
                   1302:       printf("Warning, no any valid information for:%d line=%d\n",num[i],i);
                   1303:   }
                   1304: 
                   1305:   for(i=1; i<=imx; i++){
                   1306:     for(mi=1; mi<wav[i];mi++){
                   1307:       if (stepm <=0)
                   1308:        dh[mi][i]=1;
                   1309:       else{
                   1310:        if (s[mw[mi+1][i]][i] > nlstate) {
1.10      lievre   1311:          if (agedc[i] < 2*AGESUP) {
1.2       lievre   1312:          j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12); 
1.8       lievre   1313:          if(j==0) j=1;  /* Survives at least one month after exam */
                   1314:          k=k+1;
                   1315:          if (j >= jmax) jmax=j;
1.11      lievre   1316:          if (j <= jmin) jmin=j;
1.8       lievre   1317:          sum=sum+j;
1.12    ! lievre   1318:          /* if (j<10) printf("j=%d num=%d ",j,i); */
1.10      lievre   1319:          }
1.2       lievre   1320:        }
                   1321:        else{
                   1322:          j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
                   1323:          k=k+1;
                   1324:          if (j >= jmax) jmax=j;
                   1325:          else if (j <= jmin)jmin=j;
1.12    ! lievre   1326:          /*   if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
1.2       lievre   1327:          sum=sum+j;
                   1328:        }
                   1329:        jk= j/stepm;
                   1330:        jl= j -jk*stepm;
                   1331:        ju= j -(jk+1)*stepm;
                   1332:        if(jl <= -ju)
                   1333:          dh[mi][i]=jk;
                   1334:        else
                   1335:          dh[mi][i]=jk+1;
                   1336:        if(dh[mi][i]==0)
                   1337:          dh[mi][i]=1; /* At least one step */
                   1338:       }
                   1339:     }
                   1340:   }
1.8       lievre   1341:   jmean=sum/k;
                   1342:   printf("Delay (in months) between two waves Min=%d Max=%d Mean=%f\n\n ",jmin, jmax,jmean);
1.12    ! lievre   1343:  }
1.2       lievre   1344: /*********** Tricode ****************************/
                   1345: void tricode(int *Tvar, int **nbcode, int imx)
                   1346: {
1.7       lievre   1347:   int Ndum[20],ij=1, k, j, i;
1.2       lievre   1348:   int cptcode=0;
1.7       lievre   1349:   cptcoveff=0; 
                   1350:  
                   1351:   for (k=0; k<19; k++) Ndum[k]=0;
1.2       lievre   1352:   for (k=1; k<=7; k++) ncodemax[k]=0;
1.6       lievre   1353: 
1.7       lievre   1354:   for (j=1; j<=(cptcovn+2*cptcovprod); j++) {
1.2       lievre   1355:     for (i=1; i<=imx; i++) {
                   1356:       ij=(int)(covar[Tvar[j]][i]);
                   1357:       Ndum[ij]++; 
1.8       lievre   1358:       /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
1.2       lievre   1359:       if (ij > cptcode) cptcode=ij; 
                   1360:     }
1.7       lievre   1361: 
1.2       lievre   1362:     for (i=0; i<=cptcode; i++) {
                   1363:       if(Ndum[i]!=0) ncodemax[j]++;
                   1364:     }
                   1365:     ij=1; 
1.7       lievre   1366: 
1.8       lievre   1367: 
1.2       lievre   1368:     for (i=1; i<=ncodemax[j]; i++) {
1.7       lievre   1369:       for (k=0; k<=19; k++) {
1.2       lievre   1370:        if (Ndum[k] != 0) {
                   1371:          nbcode[Tvar[j]][ij]=k; 
                   1372:          ij++;
                   1373:        }
                   1374:        if (ij > ncodemax[j]) break; 
                   1375:       }  
                   1376:     } 
1.7       lievre   1377:   }  
1.8       lievre   1378: 
                   1379:  for (k=0; k<19; k++) Ndum[k]=0;
                   1380: 
1.12    ! lievre   1381:  for (i=1; i<=ncovmodel-2; i++) {
1.7       lievre   1382:       ij=Tvar[i];
                   1383:       Ndum[ij]++; 
                   1384:     }
1.8       lievre   1385: 
1.7       lievre   1386:  ij=1;
1.8       lievre   1387:  for (i=1; i<=10; i++) {
1.7       lievre   1388:    if((Ndum[i]!=0) && (i<=ncov)){
1.8       lievre   1389:      Tvaraff[ij]=i; 
                   1390:      ij++;
1.7       lievre   1391:    }
                   1392:  }
                   1393:  
1.8       lievre   1394:     cptcoveff=ij-1;
1.6       lievre   1395: }
1.2       lievre   1396: 
                   1397: /*********** Health Expectancies ****************/
                   1398: 
                   1399: void evsij(char fileres[], double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int ij)
                   1400: {
                   1401:   /* Health expectancies */
                   1402:   int i, j, nhstepm, hstepm, h;
                   1403:   double age, agelim,hf;
                   1404:   double ***p3mat;
                   1405:   
                   1406:   fprintf(ficreseij,"# Health expectancies\n");
                   1407:   fprintf(ficreseij,"# Age");
                   1408:   for(i=1; i<=nlstate;i++)
                   1409:     for(j=1; j<=nlstate;j++)
                   1410:       fprintf(ficreseij," %1d-%1d",i,j);
                   1411:   fprintf(ficreseij,"\n");
                   1412: 
                   1413:   hstepm=1*YEARM; /*  Every j years of age (in month) */
                   1414:   hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */ 
                   1415: 
                   1416:   agelim=AGESUP;
                   1417:   for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
                   1418:     /* nhstepm age range expressed in number of stepm */
                   1419:     nhstepm=(int) rint((agelim-age)*YEARM/stepm); 
                   1420:     /* Typically if 20 years = 20*12/6=40 stepm */ 
                   1421:     if (stepm >= YEARM) hstepm=1;
                   1422:     nhstepm = nhstepm/hstepm;/* Expressed in hstepm, typically 40/4=10 */
                   1423:     p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
                   1424:     /* Computed by stepm unit matrices, product of hstepm matrices, stored
                   1425:        in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
                   1426:     hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, ij);  
                   1427: 
                   1428: 
                   1429:     for(i=1; i<=nlstate;i++)
                   1430:       for(j=1; j<=nlstate;j++)
                   1431:        for (h=0, eij[i][j][(int)age]=0; h<=nhstepm; h++){
                   1432:          eij[i][j][(int)age] +=p3mat[i][j][h];
                   1433:        }
                   1434:     
                   1435:     hf=1;
                   1436:     if (stepm >= YEARM) hf=stepm/YEARM;
                   1437:     fprintf(ficreseij,"%.0f",age );
                   1438:     for(i=1; i<=nlstate;i++)
                   1439:       for(j=1; j<=nlstate;j++){
                   1440:        fprintf(ficreseij," %.4f", hf*eij[i][j][(int)age]);
                   1441:       }
                   1442:     fprintf(ficreseij,"\n");
                   1443:     free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
                   1444:   }
                   1445: }
                   1446: 
                   1447: /************ Variance ******************/
                   1448: void varevsij(char fileres[], 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)
                   1449: {
                   1450:   /* Variance of health expectancies */
                   1451:   /*  double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
                   1452:   double **newm;
                   1453:   double **dnewm,**doldm;
                   1454:   int i, j, nhstepm, hstepm, h;
                   1455:   int k, cptcode;
1.12    ! lievre   1456:   double *xp;
1.2       lievre   1457:   double **gp, **gm;
                   1458:   double ***gradg, ***trgradg;
                   1459:   double ***p3mat;
                   1460:   double age,agelim;
                   1461:   int theta;
                   1462: 
                   1463:    fprintf(ficresvij,"# Covariances of life expectancies\n");
                   1464:   fprintf(ficresvij,"# Age");
                   1465:   for(i=1; i<=nlstate;i++)
                   1466:     for(j=1; j<=nlstate;j++)
                   1467:       fprintf(ficresvij," Cov(e%1d, e%1d)",i,j);
                   1468:   fprintf(ficresvij,"\n");
                   1469: 
                   1470:   xp=vector(1,npar);
                   1471:   dnewm=matrix(1,nlstate,1,npar);
                   1472:   doldm=matrix(1,nlstate,1,nlstate);
                   1473:   
                   1474:   hstepm=1*YEARM; /* Every year of age */
                   1475:   hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */ 
                   1476:   agelim = AGESUP;
                   1477:   for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
                   1478:     nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */ 
                   1479:     if (stepm >= YEARM) hstepm=1;
                   1480:     nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
                   1481:     p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
                   1482:     gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
                   1483:     gp=matrix(0,nhstepm,1,nlstate);
                   1484:     gm=matrix(0,nhstepm,1,nlstate);
                   1485: 
                   1486:     for(theta=1; theta <=npar; theta++){
                   1487:       for(i=1; i<=npar; i++){ /* Computes gradient */
                   1488:        xp[i] = x[i] + (i==theta ?delti[theta]:0);
                   1489:       }
                   1490:       hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);  
                   1491:       prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
                   1492:       for(j=1; j<= nlstate; j++){
                   1493:        for(h=0; h<=nhstepm; h++){
                   1494:          for(i=1, gp[h][j]=0.;i<=nlstate;i++)
                   1495:            gp[h][j] += prlim[i][i]*p3mat[i][j][h];
                   1496:        }
                   1497:       }
                   1498:     
                   1499:       for(i=1; i<=npar; i++) /* Computes gradient */
                   1500:        xp[i] = x[i] - (i==theta ?delti[theta]:0);
                   1501:       hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);  
                   1502:       prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
                   1503:       for(j=1; j<= nlstate; j++){
                   1504:        for(h=0; h<=nhstepm; h++){
                   1505:          for(i=1, gm[h][j]=0.;i<=nlstate;i++)
                   1506:            gm[h][j] += prlim[i][i]*p3mat[i][j][h];
                   1507:        }
                   1508:       }
                   1509:       for(j=1; j<= nlstate; j++)
                   1510:        for(h=0; h<=nhstepm; h++){
                   1511:          gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
                   1512:        }
                   1513:     } /* End theta */
                   1514: 
                   1515:     trgradg =ma3x(0,nhstepm,1,nlstate,1,npar);
                   1516: 
                   1517:     for(h=0; h<=nhstepm; h++)
                   1518:       for(j=1; j<=nlstate;j++)
                   1519:        for(theta=1; theta <=npar; theta++)
                   1520:          trgradg[h][j][theta]=gradg[h][theta][j];
                   1521: 
                   1522:     for(i=1;i<=nlstate;i++)
                   1523:       for(j=1;j<=nlstate;j++)
                   1524:        vareij[i][j][(int)age] =0.;
                   1525:     for(h=0;h<=nhstepm;h++){
                   1526:       for(k=0;k<=nhstepm;k++){
                   1527:        matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
                   1528:        matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
                   1529:        for(i=1;i<=nlstate;i++)
                   1530:          for(j=1;j<=nlstate;j++)
                   1531:            vareij[i][j][(int)age] += doldm[i][j];
                   1532:       }
                   1533:     }
                   1534:     h=1;
                   1535:     if (stepm >= YEARM) h=stepm/YEARM;
                   1536:     fprintf(ficresvij,"%.0f ",age );
                   1537:     for(i=1; i<=nlstate;i++)
                   1538:       for(j=1; j<=nlstate;j++){
                   1539:        fprintf(ficresvij," %.4f", h*vareij[i][j][(int)age]);
                   1540:       }
                   1541:     fprintf(ficresvij,"\n");
                   1542:     free_matrix(gp,0,nhstepm,1,nlstate);
                   1543:     free_matrix(gm,0,nhstepm,1,nlstate);
                   1544:     free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
                   1545:     free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
                   1546:     free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
                   1547:   } /* End age */
                   1548:  
                   1549:   free_vector(xp,1,npar);
                   1550:   free_matrix(doldm,1,nlstate,1,npar);
                   1551:   free_matrix(dnewm,1,nlstate,1,nlstate);
                   1552: 
                   1553: }
                   1554: 
                   1555: /************ Variance of prevlim ******************/
                   1556: 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)
                   1557: {
                   1558:   /* Variance of prevalence limit */
                   1559:   /*  double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
                   1560:   double **newm;
                   1561:   double **dnewm,**doldm;
                   1562:   int i, j, nhstepm, hstepm;
                   1563:   int k, cptcode;
                   1564:   double *xp;
                   1565:   double *gp, *gm;
                   1566:   double **gradg, **trgradg;
                   1567:   double age,agelim;
                   1568:   int theta;
                   1569:    
                   1570:   fprintf(ficresvpl,"# Standard deviation of prevalences limit\n");
                   1571:   fprintf(ficresvpl,"# Age");
                   1572:   for(i=1; i<=nlstate;i++)
                   1573:       fprintf(ficresvpl," %1d-%1d",i,i);
                   1574:   fprintf(ficresvpl,"\n");
                   1575: 
                   1576:   xp=vector(1,npar);
                   1577:   dnewm=matrix(1,nlstate,1,npar);
                   1578:   doldm=matrix(1,nlstate,1,nlstate);
                   1579:   
                   1580:   hstepm=1*YEARM; /* Every year of age */
                   1581:   hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */ 
                   1582:   agelim = AGESUP;
                   1583:   for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
                   1584:     nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */ 
                   1585:     if (stepm >= YEARM) hstepm=1;
                   1586:     nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
                   1587:     gradg=matrix(1,npar,1,nlstate);
                   1588:     gp=vector(1,nlstate);
                   1589:     gm=vector(1,nlstate);
                   1590: 
                   1591:     for(theta=1; theta <=npar; theta++){
                   1592:       for(i=1; i<=npar; i++){ /* Computes gradient */
                   1593:        xp[i] = x[i] + (i==theta ?delti[theta]:0);
                   1594:       }
                   1595:       prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
                   1596:       for(i=1;i<=nlstate;i++)
                   1597:        gp[i] = prlim[i][i];
                   1598:     
                   1599:       for(i=1; i<=npar; i++) /* Computes gradient */
                   1600:        xp[i] = x[i] - (i==theta ?delti[theta]:0);
                   1601:       prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
                   1602:       for(i=1;i<=nlstate;i++)
                   1603:        gm[i] = prlim[i][i];
                   1604: 
                   1605:       for(i=1;i<=nlstate;i++)
                   1606:        gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
                   1607:     } /* End theta */
                   1608: 
                   1609:     trgradg =matrix(1,nlstate,1,npar);
                   1610: 
                   1611:     for(j=1; j<=nlstate;j++)
                   1612:       for(theta=1; theta <=npar; theta++)
                   1613:        trgradg[j][theta]=gradg[theta][j];
                   1614: 
                   1615:     for(i=1;i<=nlstate;i++)
                   1616:       varpl[i][(int)age] =0.;
                   1617:     matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
                   1618:     matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
                   1619:     for(i=1;i<=nlstate;i++)
                   1620:       varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
                   1621: 
                   1622:     fprintf(ficresvpl,"%.0f ",age );
                   1623:     for(i=1; i<=nlstate;i++)
                   1624:       fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
                   1625:     fprintf(ficresvpl,"\n");
                   1626:     free_vector(gp,1,nlstate);
                   1627:     free_vector(gm,1,nlstate);
                   1628:     free_matrix(gradg,1,npar,1,nlstate);
                   1629:     free_matrix(trgradg,1,nlstate,1,npar);
                   1630:   } /* End age */
                   1631: 
                   1632:   free_vector(xp,1,npar);
                   1633:   free_matrix(doldm,1,nlstate,1,npar);
                   1634:   free_matrix(dnewm,1,nlstate,1,nlstate);
                   1635: 
                   1636: }
                   1637: 
                   1638: 
                   1639: 
                   1640: /***********************************************/
                   1641: /**************** Main Program *****************/
                   1642: /***********************************************/
                   1643: 
                   1644: /*int main(int argc, char *argv[])*/
                   1645: int main()
                   1646: {
                   1647: 
1.8       lievre   1648:   int i,j, k, n=MAXN,iter,m,size,cptcode, cptcod;
1.2       lievre   1649:   double agedeb, agefin,hf;
                   1650:   double agemin=1.e20, agemax=-1.e20;
                   1651: 
                   1652:   double fret;
                   1653:   double **xi,tmp,delta;
                   1654: 
                   1655:   double dum; /* Dummy variable */
                   1656:   double ***p3mat;
                   1657:   int *indx;
                   1658:   char line[MAXLINE], linepar[MAXLINE];
                   1659:   char title[MAXLINE];
1.9       lievre   1660:   char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH],  filerespl[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH];
1.2       lievre   1661:   char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH];
                   1662:   char filerest[FILENAMELENGTH];
                   1663:   char fileregp[FILENAMELENGTH];
                   1664:   char path[80],pathc[80],pathcd[80],pathtot[80],model[20];
                   1665:   int firstobs=1, lastobs=10;
                   1666:   int sdeb, sfin; /* Status at beginning and end */
                   1667:   int c,  h , cpt,l;
                   1668:   int ju,jl, mi;
1.7       lievre   1669:   int i1,j1, k1,k2,k3,jk,aa,bb, stepsize, ij;
1.2       lievre   1670:   int jnais,jdc,jint4,jint1,jint2,jint3,**outcome,**adl,*tab;
1.12    ! lievre   1671:  
1.2       lievre   1672:   int hstepm, nhstepm;
                   1673:   double bage, fage, age, agelim, agebase;
                   1674:   double ftolpl=FTOL;
                   1675:   double **prlim;
                   1676:   double *severity;
                   1677:   double ***param; /* Matrix of parameters */
                   1678:   double  *p;
                   1679:   double **matcov; /* Matrix of covariance */
                   1680:   double ***delti3; /* Scale */
                   1681:   double *delti; /* Scale */
                   1682:   double ***eij, ***vareij;
                   1683:   double **varpl; /* Variances of prevalence limits by age */
                   1684:   double *epj, vepp;
1.10      lievre   1685:   char version[80]="Imach version 64b, May 2001, INED-EUROREVES ";
1.2       lievre   1686:   char *alph[]={"a","a","b","c","d","e"}, str[4];
1.5       lievre   1687: 
1.2       lievre   1688:   char z[1]="c", occ;
                   1689: #include <sys/time.h>
                   1690: #include <time.h>
                   1691:   char stra[80], strb[80], strc[80], strd[80],stre[80],modelsav[80];
                   1692:   /* long total_usecs;
                   1693:   struct timeval start_time, end_time;
                   1694:   
                   1695:   gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
                   1696: 
                   1697: 
1.9       lievre   1698:   printf("\nIMACH, Version 0.64b");
1.2       lievre   1699:   printf("\nEnter the parameter file name: ");
                   1700: 
                   1701: #ifdef windows
                   1702:   scanf("%s",pathtot);
1.5       lievre   1703:   getcwd(pathcd, size);
                   1704:   /*cygwin_split_path(pathtot,path,optionfile);
                   1705:     printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
                   1706:   /* cutv(path,optionfile,pathtot,'\\');*/
                   1707: 
                   1708: split(pathtot, path,optionfile);
1.2       lievre   1709:   chdir(path);
                   1710:   replace(pathc,path);
                   1711: #endif
                   1712: #ifdef unix
                   1713:   scanf("%s",optionfile);
                   1714: #endif
                   1715: 
                   1716: /*-------- arguments in the command line --------*/
                   1717: 
                   1718:   strcpy(fileres,"r");
                   1719:   strcat(fileres, optionfile);
                   1720: 
                   1721:   /*---------arguments file --------*/
                   1722: 
                   1723:   if((ficpar=fopen(optionfile,"r"))==NULL)    {
                   1724:     printf("Problem with optionfile %s\n",optionfile);
                   1725:     goto end;
                   1726:   }
                   1727: 
                   1728:   strcpy(filereso,"o");
                   1729:   strcat(filereso,fileres);
                   1730:   if((ficparo=fopen(filereso,"w"))==NULL) {
                   1731:     printf("Problem with Output resultfile: %s\n", filereso);goto end;
                   1732:   }
                   1733: 
                   1734:   /* Reads comments: lines beginning with '#' */
                   1735:   while((c=getc(ficpar))=='#' && c!= EOF){
                   1736:     ungetc(c,ficpar);
                   1737:     fgets(line, MAXLINE, ficpar);
                   1738:     puts(line);
                   1739:     fputs(line,ficparo);
                   1740:   }
                   1741:   ungetc(c,ficpar);
                   1742: 
                   1743:   fscanf(ficpar,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%lf stepm=%d ncov=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n",title, datafile, &lastobs, &firstpass,&lastpass,&ftol, &stepm, &ncov, &nlstate,&ndeath, &maxwav, &mle, &weightopt,model);
                   1744:   printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncov=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncov, nlstate,ndeath, maxwav, mle, weightopt,model);
                   1745:   fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncov=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncov,nlstate,ndeath,maxwav, mle, weightopt,model);
                   1746: 
1.8       lievre   1747:   covar=matrix(0,NCOVMAX,1,n); 
                   1748:   cptcovn=0; 
                   1749:   if (strlen(model)>1) cptcovn=nbocc(model,'+')+1;
1.2       lievre   1750: 
                   1751:   ncovmodel=2+cptcovn;
                   1752:   nvar=ncovmodel-1; /* Suppressing age as a basic covariate */
                   1753:   
                   1754:   /* Read guess parameters */
                   1755:   /* Reads comments: lines beginning with '#' */
                   1756:   while((c=getc(ficpar))=='#' && c!= EOF){
                   1757:     ungetc(c,ficpar);
                   1758:     fgets(line, MAXLINE, ficpar);
                   1759:     puts(line);
                   1760:     fputs(line,ficparo);
                   1761:   }
                   1762:   ungetc(c,ficpar);
                   1763:   
                   1764:   param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
                   1765:     for(i=1; i <=nlstate; i++)
                   1766:     for(j=1; j <=nlstate+ndeath-1; j++){
                   1767:       fscanf(ficpar,"%1d%1d",&i1,&j1);
                   1768:       fprintf(ficparo,"%1d%1d",i1,j1);
                   1769:       printf("%1d%1d",i,j);
                   1770:       for(k=1; k<=ncovmodel;k++){
                   1771:        fscanf(ficpar," %lf",&param[i][j][k]);
                   1772:        printf(" %lf",param[i][j][k]);
                   1773:        fprintf(ficparo," %lf",param[i][j][k]);
                   1774:       }
                   1775:       fscanf(ficpar,"\n");
                   1776:       printf("\n");
                   1777:       fprintf(ficparo,"\n");
                   1778:     }
                   1779:   
1.12    ! lievre   1780:     npar= (nlstate+ndeath-1)*nlstate*ncovmodel;
        !          1781: 
1.2       lievre   1782:   p=param[1][1];
                   1783:   
                   1784:   /* Reads comments: lines beginning with '#' */
                   1785:   while((c=getc(ficpar))=='#' && c!= EOF){
                   1786:     ungetc(c,ficpar);
                   1787:     fgets(line, MAXLINE, ficpar);
                   1788:     puts(line);
                   1789:     fputs(line,ficparo);
                   1790:   }
                   1791:   ungetc(c,ficpar);
                   1792: 
                   1793:   delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
                   1794:   delti=vector(1,npar); /* Scale of each paramater (output from hesscov) */
                   1795:   for(i=1; i <=nlstate; i++){
                   1796:     for(j=1; j <=nlstate+ndeath-1; j++){
                   1797:       fscanf(ficpar,"%1d%1d",&i1,&j1);
                   1798:       printf("%1d%1d",i,j);
                   1799:       fprintf(ficparo,"%1d%1d",i1,j1);
                   1800:       for(k=1; k<=ncovmodel;k++){
                   1801:        fscanf(ficpar,"%le",&delti3[i][j][k]);
                   1802:        printf(" %le",delti3[i][j][k]);
                   1803:        fprintf(ficparo," %le",delti3[i][j][k]);
                   1804:       }
                   1805:       fscanf(ficpar,"\n");
                   1806:       printf("\n");
                   1807:       fprintf(ficparo,"\n");
                   1808:     }
                   1809:   }
                   1810:   delti=delti3[1][1];
                   1811:   
                   1812:   /* Reads comments: lines beginning with '#' */
                   1813:   while((c=getc(ficpar))=='#' && c!= EOF){
                   1814:     ungetc(c,ficpar);
                   1815:     fgets(line, MAXLINE, ficpar);
                   1816:     puts(line);
                   1817:     fputs(line,ficparo);
                   1818:   }
                   1819:   ungetc(c,ficpar);
                   1820:   
                   1821:   matcov=matrix(1,npar,1,npar);
                   1822:   for(i=1; i <=npar; i++){
                   1823:     fscanf(ficpar,"%s",&str);
                   1824:     printf("%s",str);
                   1825:     fprintf(ficparo,"%s",str);
                   1826:     for(j=1; j <=i; j++){
                   1827:       fscanf(ficpar," %le",&matcov[i][j]);
                   1828:       printf(" %.5le",matcov[i][j]);
                   1829:       fprintf(ficparo," %.5le",matcov[i][j]);
                   1830:     }
                   1831:     fscanf(ficpar,"\n");
                   1832:     printf("\n");
                   1833:     fprintf(ficparo,"\n");
                   1834:   }
                   1835:   for(i=1; i <=npar; i++)
                   1836:     for(j=i+1;j<=npar;j++)
                   1837:       matcov[i][j]=matcov[j][i];
                   1838:    
                   1839:   printf("\n");
                   1840: 
                   1841: 
                   1842:     /*-------- data file ----------*/
                   1843:     if((ficres =fopen(fileres,"w"))==NULL) {
                   1844:       printf("Problem with resultfile: %s\n", fileres);goto end;
                   1845:     }
                   1846:     fprintf(ficres,"#%s\n",version);
                   1847:     
                   1848:     if((fic=fopen(datafile,"r"))==NULL)    {
                   1849:       printf("Problem with datafile: %s\n", datafile);goto end;
                   1850:     }
                   1851: 
                   1852:     n= lastobs;
                   1853:     severity = vector(1,maxwav);
                   1854:     outcome=imatrix(1,maxwav+1,1,n);
                   1855:     num=ivector(1,n);
                   1856:     moisnais=vector(1,n);
                   1857:     annais=vector(1,n);
                   1858:     moisdc=vector(1,n);
                   1859:     andc=vector(1,n);
                   1860:     agedc=vector(1,n);
                   1861:     cod=ivector(1,n);
                   1862:     weight=vector(1,n);
                   1863:     for(i=1;i<=n;i++) weight[i]=1.0; /* Equal weights, 1 by default */
                   1864:     mint=matrix(1,maxwav,1,n);
                   1865:     anint=matrix(1,maxwav,1,n);
                   1866:     s=imatrix(1,maxwav+1,1,n);
                   1867:     adl=imatrix(1,maxwav+1,1,n);    
                   1868:     tab=ivector(1,NCOVMAX);
1.3       lievre   1869:     ncodemax=ivector(1,8);
1.2       lievre   1870: 
1.12    ! lievre   1871:     i=1;
1.2       lievre   1872:     while (fgets(line, MAXLINE, fic) != NULL)    {
                   1873:       if ((i >= firstobs) && (i <=lastobs)) {
                   1874:        
                   1875:        for (j=maxwav;j>=1;j--){
                   1876:          cutv(stra, strb,line,' '); s[j][i]=atoi(strb); 
                   1877:          strcpy(line,stra);
                   1878:          cutv(stra, strb,line,'/'); anint[j][i]=(double)(atoi(strb)); strcpy(line,stra);
                   1879:          cutv(stra, strb,line,' '); mint[j][i]=(double)(atoi(strb)); strcpy(line,stra);
                   1880:        }
                   1881:        
                   1882:        cutv(stra, strb,line,'/'); andc[i]=(double)(atoi(strb)); strcpy(line,stra);
                   1883:        cutv(stra, strb,line,' '); moisdc[i]=(double)(atoi(strb)); strcpy(line,stra);
                   1884: 
                   1885:        cutv(stra, strb,line,'/'); annais[i]=(double)(atoi(strb)); strcpy(line,stra);
                   1886:        cutv(stra, strb,line,' '); moisnais[i]=(double)(atoi(strb)); strcpy(line,stra);
                   1887: 
                   1888:        cutv(stra, strb,line,' '); weight[i]=(double)(atoi(strb)); strcpy(line,stra);
                   1889:        for (j=ncov;j>=1;j--){
                   1890:          cutv(stra, strb,line,' '); covar[j][i]=(double)(atoi(strb)); strcpy(line,stra);
                   1891:        } 
                   1892:        num[i]=atol(stra);
1.12    ! lievre   1893:        
        !          1894:        /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
        !          1895:          printf("%d %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]),weight[i], (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]),  (mint[2][i]), (anint[2][i]), (s[2][i]),  (mint[3][i]), (anint[3][i]), (s[3][i]),  (mint[4][i]), (anint[4][i]), (s[4][i])); ij=ij+1;}*/
1.2       lievre   1896: 
                   1897:        i=i+1;
                   1898:       }
                   1899:     } 
1.12    ! lievre   1900:     /* printf("ii=%d", ij);
        !          1901:        scanf("%d",i);*/
        !          1902:   imx=i-1; /* Number of individuals */
1.3       lievre   1903: 
1.12    ! lievre   1904:   /* for (i=1; i<=imx; i++){
        !          1905:     if ((s[1][i]==3) && (s[2][i]==2)) s[2][i]=3;
        !          1906:     if ((s[2][i]==3) && (s[3][i]==2)) s[3][i]=3;
        !          1907:     if ((s[3][i]==3) && (s[4][i]==2)) s[4][i]=3;
        !          1908:   }
        !          1909:   for (i=1; i<=imx; i++) printf("%d %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]), (weight[i]), (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]),  (mint[2][i]), (anint[2][i]), (s[2][i]),  (mint[3][i]), (anint[3][i]), (s[3][i]),  (mint[4][i]), (anint[4][i]), (s[4][i]));*/
1.2       lievre   1910: 
                   1911:   /* Calculation of the number of parameter from char model*/
1.7       lievre   1912:   Tvar=ivector(1,15); 
                   1913:   Tprod=ivector(1,15); 
                   1914:   Tvaraff=ivector(1,15); 
                   1915:   Tvard=imatrix(1,15,1,2);
1.6       lievre   1916:   Tage=ivector(1,15);      
1.2       lievre   1917:    
                   1918:   if (strlen(model) >1){
1.7       lievre   1919:     j=0, j1=0, k1=1, k2=1;
1.2       lievre   1920:     j=nbocc(model,'+');
1.6       lievre   1921:     j1=nbocc(model,'*');
1.2       lievre   1922:     cptcovn=j+1;
1.7       lievre   1923:     cptcovprod=j1;
1.3       lievre   1924:     
1.8       lievre   1925:     
1.2       lievre   1926:     strcpy(modelsav,model); 
1.8       lievre   1927:     if ((strcmp(model,"age")==0) || (strcmp(model,"age*age")==0)){
                   1928:       printf("Error. Non available option model=%s ",model);
                   1929:       goto end;
                   1930:     }
                   1931:     
                   1932:     for(i=(j+1); i>=1;i--){
                   1933:       cutv(stra,strb,modelsav,'+');
                   1934:       if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); 
                   1935:       /*      printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
                   1936:       /*scanf("%d",i);*/
                   1937:       if (strchr(strb,'*')) {
                   1938:        cutv(strd,strc,strb,'*');
                   1939:        if (strcmp(strc,"age")==0) {
1.7       lievre   1940:          cptcovprod--;
1.8       lievre   1941:          cutv(strb,stre,strd,'V');
                   1942:          Tvar[i]=atoi(stre);
                   1943:          cptcovage++;
                   1944:            Tage[cptcovage]=i;
                   1945:            /*printf("stre=%s ", stre);*/
1.7       lievre   1946:        }
1.8       lievre   1947:        else if (strcmp(strd,"age")==0) {
1.7       lievre   1948:          cptcovprod--;
1.8       lievre   1949:          cutv(strb,stre,strc,'V');
                   1950:          Tvar[i]=atoi(stre);
                   1951:          cptcovage++;
                   1952:          Tage[cptcovage]=i;
1.7       lievre   1953:        }
                   1954:        else {
1.8       lievre   1955:          cutv(strb,stre,strc,'V');
                   1956:          Tvar[i]=ncov+k1;
                   1957:          cutv(strb,strc,strd,'V'); 
                   1958:          Tprod[k1]=i;
                   1959:          Tvard[k1][1]=atoi(strc);
                   1960:          Tvard[k1][2]=atoi(stre);
                   1961:          Tvar[cptcovn+k2]=Tvard[k1][1];
                   1962:          Tvar[cptcovn+k2+1]=Tvard[k1][2]; 
1.7       lievre   1963:          for (k=1; k<=lastobs;k++) 
1.8       lievre   1964:            covar[ncov+k1][k]=covar[atoi(stre)][k]*covar[atoi(strc)][k];
                   1965:          k1++;
                   1966:          k2=k2+2;
1.7       lievre   1967:        }
1.2       lievre   1968:       }
1.8       lievre   1969:       else {
                   1970:        /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
                   1971:        /*  scanf("%d",i);*/
                   1972:       cutv(strd,strc,strb,'V');
                   1973:       Tvar[i]=atoi(strc);
                   1974:       }
                   1975:       strcpy(modelsav,stra);  
                   1976:       /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
                   1977:        scanf("%d",i);*/
1.2       lievre   1978:     }
1.8       lievre   1979: }
                   1980:   
                   1981:   /*printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
                   1982:   printf("cptcovprod=%d ", cptcovprod);
                   1983:   scanf("%d ",i);*/
1.2       lievre   1984:     fclose(fic);
                   1985: 
1.7       lievre   1986:     /*  if(mle==1){*/
1.2       lievre   1987:     if (weightopt != 1) { /* Maximisation without weights*/
                   1988:       for(i=1;i<=n;i++) weight[i]=1.0;
                   1989:     }
                   1990:     /*-calculation of age at interview from date of interview and age at death -*/
                   1991:     agev=matrix(1,maxwav,1,imx);
1.12    ! lievre   1992: 
        !          1993:    for (i=1; i<=imx; i++) 
        !          1994:      for(m=2; (m<= maxwav); m++)
        !          1995:        if ((mint[m][i]== 99) && (s[m][i] <= nlstate)){
        !          1996:         anint[m][i]=9999;
        !          1997:         s[m][i]=-1;
        !          1998:        }
1.2       lievre   1999:     
                   2000:     for (i=1; i<=imx; i++)  {
                   2001:       agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
                   2002:       for(m=1; (m<= maxwav); m++){
                   2003:        if(s[m][i] >0){
                   2004:          if (s[m][i] == nlstate+1) {
                   2005:            if(agedc[i]>0)
                   2006:              if(moisdc[i]!=99 && andc[i]!=9999)
                   2007:              agev[m][i]=agedc[i];
1.8       lievre   2008:            else {
                   2009:              if (andc[i]!=9999){
1.2       lievre   2010:              printf("Warning negative age at death: %d line:%d\n",num[i],i);
                   2011:              agev[m][i]=-1;
1.8       lievre   2012:              }
1.2       lievre   2013:            }
                   2014:          }
                   2015:          else if(s[m][i] !=9){ /* Should no more exist */
                   2016:            agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
1.3       lievre   2017:            if(mint[m][i]==99 || anint[m][i]==9999)
1.2       lievre   2018:              agev[m][i]=1;
                   2019:            else if(agev[m][i] <agemin){ 
                   2020:              agemin=agev[m][i];
                   2021:              /*printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], agemin);*/
                   2022:            }
                   2023:            else if(agev[m][i] >agemax){
                   2024:              agemax=agev[m][i];
                   2025:             /* printf(" anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.0f\n",m,i,anint[m][i], i,annais[i], agemax);*/
                   2026:            }
                   2027:            /*agev[m][i]=anint[m][i]-annais[i];*/
                   2028:            /*   agev[m][i] = age[i]+2*m;*/
                   2029:          }
                   2030:          else { /* =9 */
                   2031:            agev[m][i]=1;
                   2032:            s[m][i]=-1;
                   2033:          }
                   2034:        }
                   2035:        else /*= 0 Unknown */
                   2036:          agev[m][i]=1;
                   2037:       }
                   2038:     
                   2039:     }
                   2040:     for (i=1; i<=imx; i++)  {
                   2041:       for(m=1; (m<= maxwav); m++){
                   2042:        if (s[m][i] > (nlstate+ndeath)) {
                   2043:          printf("Error: Wrong value in nlstate or ndeath\n");  
                   2044:          goto end;
                   2045:        }
                   2046:       }
                   2047:     }
                   2048: 
                   2049: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, agemin, agemax);
                   2050: 
                   2051:     free_vector(severity,1,maxwav);
                   2052:     free_imatrix(outcome,1,maxwav+1,1,n);
                   2053:     free_vector(moisnais,1,n);
                   2054:     free_vector(annais,1,n);
                   2055:     free_matrix(mint,1,maxwav,1,n);
                   2056:     free_matrix(anint,1,maxwav,1,n);
                   2057:     free_vector(moisdc,1,n);
                   2058:     free_vector(andc,1,n);
                   2059: 
                   2060:    
                   2061:     wav=ivector(1,imx);
                   2062:     dh=imatrix(1,lastpass-firstpass+1,1,imx);
                   2063:     mw=imatrix(1,lastpass-firstpass+1,1,imx);
                   2064:    
                   2065:     /* Concatenates waves */
                   2066:       concatwav(wav, dh, mw, s, agedc, agev,  firstpass, lastpass, imx, nlstate, stepm);
                   2067: 
                   2068: 
1.6       lievre   2069:       Tcode=ivector(1,100);
1.8       lievre   2070:       nbcode=imatrix(0,NCOVMAX,0,NCOVMAX); 
1.7       lievre   2071:       ncodemax[1]=1;
                   2072:       if (cptcovn > 0) tricode(Tvar,nbcode,imx);
                   2073:       
1.2       lievre   2074:    codtab=imatrix(1,100,1,10);
                   2075:    h=0;
1.7       lievre   2076:    m=pow(2,cptcoveff);
1.2       lievre   2077:  
1.7       lievre   2078:    for(k=1;k<=cptcoveff; k++){
1.2       lievre   2079:      for(i=1; i <=(m/pow(2,k));i++){
                   2080:        for(j=1; j <= ncodemax[k]; j++){
1.7       lievre   2081:         for(cpt=1; cpt <=(m/pow(2,cptcoveff+1-k)); cpt++){
1.2       lievre   2082:           h++;
                   2083:           if (h>m) h=1;codtab[h][k]=j;
                   2084:         } 
                   2085:        }
                   2086:      }
                   2087:    } 
                   2088: 
1.7       lievre   2089: 
                   2090:    /*for(i=1; i <=m ;i++){ 
1.2       lievre   2091:      for(k=1; k <=cptcovn; k++){
1.7       lievre   2092:        printf("i=%d k=%d %d %d",i,k,codtab[i][k], cptcoveff);
1.2       lievre   2093:      }
                   2094:      printf("\n");
1.6       lievre   2095:    }
                   2096:    scanf("%d",i);*/
1.2       lievre   2097:     
                   2098:    /* Calculates basic frequencies. Computes observed prevalence at single age
                   2099:        and prints on file fileres'p'. */
1.7       lievre   2100:   freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax);
1.2       lievre   2101: 
                   2102:     pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
                   2103:     oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
                   2104:     newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
                   2105:     savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
                   2106:     oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.12    ! lievre   2107:      
1.2       lievre   2108:     /* For Powell, parameters are in a vector p[] starting at p[1]
                   2109:        so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
                   2110:     p=param[1][1]; /* *(*(*(param +1)+1)+0) */
1.7       lievre   2111: 
                   2112:     if(mle==1){
1.2       lievre   2113:     mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
1.7       lievre   2114:     }
1.2       lievre   2115:     
                   2116:     /*--------- results files --------------*/
                   2117:     fprintf(ficres,"\ntitle=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncov=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncov, nlstate, ndeath, maxwav, mle,weightopt,model);
                   2118:     
                   2119:    jk=1;
                   2120:    fprintf(ficres,"# Parameters\n");
                   2121:    printf("# Parameters\n");
                   2122:    for(i=1,jk=1; i <=nlstate; i++){
                   2123:      for(k=1; k <=(nlstate+ndeath); k++){
                   2124:        if (k != i) 
                   2125:         {
                   2126:           printf("%d%d ",i,k);
                   2127:           fprintf(ficres,"%1d%1d ",i,k);
                   2128:           for(j=1; j <=ncovmodel; j++){
                   2129:             printf("%f ",p[jk]);
                   2130:             fprintf(ficres,"%f ",p[jk]);
                   2131:             jk++; 
                   2132:           }
                   2133:           printf("\n");
                   2134:           fprintf(ficres,"\n");
                   2135:         }
                   2136:      }
                   2137:    }
1.7       lievre   2138:  if(mle==1){
1.2       lievre   2139:     /* Computing hessian and covariance matrix */
                   2140:     ftolhess=ftol; /* Usually correct */
                   2141:     hesscov(matcov, p, npar, delti, ftolhess, func);
1.7       lievre   2142:  }
1.2       lievre   2143:     fprintf(ficres,"# Scales\n");
                   2144:     printf("# Scales\n");
                   2145:      for(i=1,jk=1; i <=nlstate; i++){
                   2146:       for(j=1; j <=nlstate+ndeath; j++){
                   2147:        if (j!=i) {
                   2148:          fprintf(ficres,"%1d%1d",i,j);
                   2149:          printf("%1d%1d",i,j);
                   2150:          for(k=1; k<=ncovmodel;k++){
                   2151:            printf(" %.5e",delti[jk]);
                   2152:            fprintf(ficres," %.5e",delti[jk]);
                   2153:            jk++;
                   2154:          }
                   2155:          printf("\n");
                   2156:          fprintf(ficres,"\n");
                   2157:        }
                   2158:       }
                   2159:       }
                   2160:     
                   2161:     k=1;
                   2162:     fprintf(ficres,"# Covariance\n");
                   2163:     printf("# Covariance\n");
                   2164:     for(i=1;i<=npar;i++){
                   2165:       /*  if (k>nlstate) k=1;
                   2166:       i1=(i-1)/(ncovmodel*nlstate)+1; 
                   2167:       fprintf(ficres,"%s%d%d",alph[k],i1,tab[i]);
                   2168:       printf("%s%d%d",alph[k],i1,tab[i]);*/
                   2169:       fprintf(ficres,"%3d",i);
                   2170:       printf("%3d",i);
                   2171:       for(j=1; j<=i;j++){
                   2172:        fprintf(ficres," %.5e",matcov[i][j]);
                   2173:        printf(" %.5e",matcov[i][j]);
                   2174:       }
                   2175:       fprintf(ficres,"\n");
                   2176:       printf("\n");
                   2177:       k++;
                   2178:     }
                   2179:     
                   2180:     while((c=getc(ficpar))=='#' && c!= EOF){
                   2181:       ungetc(c,ficpar);
                   2182:       fgets(line, MAXLINE, ficpar);
                   2183:       puts(line);
                   2184:       fputs(line,ficparo);
                   2185:     }
                   2186:     ungetc(c,ficpar);
                   2187:   
                   2188:     fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf\n",&agemin,&agemax, &bage, &fage);
                   2189:     
                   2190:     if (fage <= 2) {
                   2191:       bage = agemin;
                   2192:       fage = agemax;
                   2193:     }
                   2194: 
                   2195:     fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
                   2196:     fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax,bage,fage);
1.7       lievre   2197: 
                   2198:    
1.2       lievre   2199: /*------------ gnuplot -------------*/
                   2200: chdir(pathcd);
                   2201:   if((ficgp=fopen("graph.plt","w"))==NULL) {
1.5       lievre   2202:     printf("Problem with file graph.gp");goto end;
1.2       lievre   2203:   }
                   2204: #ifdef windows
                   2205:   fprintf(ficgp,"cd \"%s\" \n",pathc);
                   2206: #endif
1.7       lievre   2207: m=pow(2,cptcoveff);
1.2       lievre   2208:   
                   2209:  /* 1eme*/
                   2210:   for (cpt=1; cpt<= nlstate ; cpt ++) {
                   2211:    for (k1=1; k1<= m ; k1 ++) {
                   2212: 
                   2213: #ifdef windows
                   2214:     fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter gif small size 400,300\nplot [%.f:%.f] \"vpl%s\" every :::%d::%d u 1:2 \"\%%lf",agemin,fage,fileres,k1-1,k1-1);
                   2215: #endif
                   2216: #ifdef unix
                   2217: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nplot [%.f:%.f] \"vpl%s\" u 1:2 \"\%%lf",agemin,fage,fileres);
                   2218: #endif
                   2219: 
                   2220: for (i=1; i<= nlstate ; i ++) {
                   2221:   if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
                   2222:   else fprintf(ficgp," \%%*lf (\%%*lf)");
                   2223: }
                   2224:     fprintf(ficgp,"\" t\"Stationary prevalence\" w l 0,\"vpl%s\" every :::%d::%d u 1:($2+2*$3) \"\%%lf",fileres,k1-1,k1-1);
                   2225:     for (i=1; i<= nlstate ; i ++) {
                   2226:   if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
                   2227:   else fprintf(ficgp," \%%*lf (\%%*lf)");
                   2228: } 
                   2229:   fprintf(ficgp,"\" t\"95\%% CI\" w l 1,\"vpl%s\" every :::%d::%d u 1:($2-2*$3) \"\%%lf",fileres,k1-1,k1-1); 
                   2230:      for (i=1; i<= nlstate ; i ++) {
                   2231:   if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
                   2232:   else fprintf(ficgp," \%%*lf (\%%*lf)");
                   2233: }  
                   2234:      fprintf(ficgp,"\" t\"\" w l 1,\"p%s\" every :::%d::%d u 1:($%d) t\"Observed prevalence \" w l 2",fileres,k1-1,k1-1,2+4*(cpt-1));
                   2235: #ifdef unix
                   2236: fprintf(ficgp,"\nset ter gif small size 400,300");
                   2237: #endif
                   2238: fprintf(ficgp,"\nset out \"v%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
                   2239:    }
                   2240:   }
                   2241:   /*2 eme*/
                   2242: 
                   2243:   for (k1=1; k1<= m ; k1 ++) { 
                   2244:     fprintf(ficgp,"set ylabel \"Years\" \nset ter gif small size 400,300\nplot [%.f:%.f] ",agemin,fage);
                   2245:     
                   2246:     for (i=1; i<= nlstate+1 ; i ++) {
                   2247:       k=2*i;
                   2248:       fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:2 \"\%%lf",fileres,k1-1,k1-1);
                   2249:       for (j=1; j<= nlstate+1 ; j ++) {
                   2250:   if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
                   2251:   else fprintf(ficgp," \%%*lf (\%%*lf)");
                   2252: }   
                   2253:       if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l ,");
                   2254:       else fprintf(ficgp,"\" t\"LE in state (%d)\" w l ,",i-1);
                   2255:     fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2-$3*2) \"\%%lf",fileres,k1-1,k1-1);
                   2256:       for (j=1; j<= nlstate+1 ; j ++) {
                   2257:        if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
                   2258:        else fprintf(ficgp," \%%*lf (\%%*lf)");
                   2259: }   
                   2260:       fprintf(ficgp,"\" t\"\" w l 0,");
                   2261:      fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2+$3*2) \"\%%lf",fileres,k1-1,k1-1);
                   2262:       for (j=1; j<= nlstate+1 ; j ++) {
                   2263:   if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
                   2264:   else fprintf(ficgp," \%%*lf (\%%*lf)");
                   2265: }   
                   2266:       if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l 0");
                   2267:       else fprintf(ficgp,"\" t\"\" w l 0,");
                   2268:     }
                   2269:     fprintf(ficgp,"\nset out \"e%s%d.gif\" \nreplot\n\n",strtok(optionfile, "."),k1);
                   2270:   }
                   2271:  
                   2272:   /*3eme*/
                   2273: 
1.5       lievre   2274:   for (k1=1; k1<= m ; k1 ++) { 
1.2       lievre   2275:     for (cpt=1; cpt<= nlstate ; cpt ++) {
                   2276:       k=2+nlstate*(cpt-1);
                   2277:       fprintf(ficgp,"set ter gif small size 400,300\nplot [%.f:%.f] \"e%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",agemin,fage,fileres,k1-1,k1-1,k,cpt);
                   2278:       for (i=1; i< nlstate ; i ++) {
                   2279:        fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",fileres,k1-1,k1-1,k+i,cpt,i+1);
                   2280:       } 
                   2281:       fprintf(ficgp,"\nset out \"exp%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
                   2282:     }
1.5       lievre   2283:   }
1.2       lievre   2284:  
                   2285:   /* CV preval stat */
1.5       lievre   2286:   for (k1=1; k1<= m ; k1 ++) { 
1.2       lievre   2287:     for (cpt=1; cpt<nlstate ; cpt ++) {
                   2288:       k=3;
                   2289:       fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter gif small size 400,300\nplot [%.f:%.f] \"pij%s\" u ($1==%d ? ($3):1/0):($%d/($%d",agemin,agemax,fileres,k1,k+cpt+1,k+1);
                   2290:       for (i=1; i< nlstate ; i ++)
                   2291:        fprintf(ficgp,"+$%d",k+i+1);
                   2292:       fprintf(ficgp,")) t\"prev(%d,%d)\" w l",cpt,cpt+1);
                   2293:       
                   2294:       l=3+(nlstate+ndeath)*cpt;
                   2295:       fprintf(ficgp,",\"pij%s\" u ($1==%d ? ($3):1/0):($%d/($%d",fileres,k1,l+cpt+1,l+1);
                   2296:       for (i=1; i< nlstate ; i ++) {
                   2297:        l=3+(nlstate+ndeath)*cpt;
                   2298:        fprintf(ficgp,"+$%d",l+i+1);
                   2299:       }
                   2300:       fprintf(ficgp,")) t\"prev(%d,%d)\" w l\n",cpt+1,cpt+1);   
                   2301:       fprintf(ficgp,"set out \"p%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
                   2302:     } 
                   2303:   }
1.5       lievre   2304: 
1.2       lievre   2305:   /* proba elementaires */
1.5       lievre   2306:    for(i=1,jk=1; i <=nlstate; i++){
1.2       lievre   2307:     for(k=1; k <=(nlstate+ndeath); k++){
                   2308:       if (k != i) {
                   2309:        for(j=1; j <=ncovmodel; j++){
1.5       lievre   2310:          /*fprintf(ficgp,"%s%1d%1d=%f ",alph[j],i,k,p[jk]);*/
                   2311:          /*fprintf(ficgp,"%s",alph[1]);*/
                   2312:          fprintf(ficgp,"p%d=%f ",jk,p[jk]);
1.2       lievre   2313:          jk++; 
                   2314:          fprintf(ficgp,"\n");
                   2315:        }
                   2316:       }
                   2317:     }
1.5       lievre   2318:     }
                   2319: 
1.2       lievre   2320:   for(jk=1; jk <=m; jk++) {
                   2321:   fprintf(ficgp,"\nset ter gif small size 400,300\nset log y\nplot  [%.f:%.f] ",agemin,agemax);
1.5       lievre   2322:    i=1;
                   2323:    for(k2=1; k2<=nlstate; k2++) {
                   2324:      k3=i;
                   2325:      for(k=1; k<=(nlstate+ndeath); k++) {
                   2326:        if (k != k2){
                   2327:        fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
1.7       lievre   2328: ij=1;
                   2329:        for(j=3; j <=ncovmodel; j++) {
                   2330:          if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) {
                   2331:            fprintf(ficgp,"+p%d*%d*x",i+j-1,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);
                   2332:            ij++;
                   2333:          }
                   2334:          else
1.6       lievre   2335:          fprintf(ficgp,"+p%d*%d",i+j-1,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
1.7       lievre   2336:        }
                   2337:          fprintf(ficgp,")/(1");
1.6       lievre   2338:        
                   2339:        for(k1=1; k1 <=nlstate; k1++){   
                   2340:          fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
1.7       lievre   2341: ij=1;
                   2342:          for(j=3; j <=ncovmodel; j++){
                   2343:          if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) {
                   2344:            fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);
                   2345:            ij++;
                   2346:          }
                   2347:          else
1.6       lievre   2348:            fprintf(ficgp,"+p%d*%d",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
1.7       lievre   2349:          }
1.6       lievre   2350:          fprintf(ficgp,")");
1.5       lievre   2351:        }
                   2352:        fprintf(ficgp,") t \"p%d%d\" ", k2,k);
                   2353:        if ((k+k2)!= (nlstate*2+ndeath)) fprintf(ficgp,",");
1.6       lievre   2354:        i=i+ncovmodel;
1.5       lievre   2355:        }
                   2356:      }
                   2357:    }
1.6       lievre   2358:    fprintf(ficgp,"\nset out \"pe%s%d.gif\" \nreplot\n\n",strtok(optionfile, "."),jk); 
                   2359:   }
1.5       lievre   2360:    
                   2361:   fclose(ficgp);
                   2362:    
                   2363: chdir(path);
1.2       lievre   2364:     free_matrix(agev,1,maxwav,1,imx);
                   2365:     free_ivector(wav,1,imx);
                   2366:     free_imatrix(dh,1,lastpass-firstpass+1,1,imx);
                   2367:     free_imatrix(mw,1,lastpass-firstpass+1,1,imx);
                   2368:     
                   2369:     free_imatrix(s,1,maxwav+1,1,n);
                   2370:     
                   2371:     
                   2372:     free_ivector(num,1,n);
                   2373:     free_vector(agedc,1,n);
                   2374:     free_vector(weight,1,n);
                   2375:     /*free_matrix(covar,1,NCOVMAX,1,n);*/
                   2376:     fclose(ficparo);
                   2377:     fclose(ficres);
1.7       lievre   2378:     /*  }*/
1.2       lievre   2379:    
                   2380:    /*________fin mle=1_________*/
                   2381:    
                   2382: 
                   2383:   
                   2384:     /* No more information from the sample is required now */
                   2385:   /* Reads comments: lines beginning with '#' */
                   2386:   while((c=getc(ficpar))=='#' && c!= EOF){
                   2387:     ungetc(c,ficpar);
                   2388:     fgets(line, MAXLINE, ficpar);
                   2389:     puts(line);
                   2390:     fputs(line,ficparo);
                   2391:   }
                   2392:   ungetc(c,ficpar);
                   2393:   
                   2394:   fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf\n",&agemin,&agemax, &bage, &fage);
                   2395:   printf("agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax, bage, fage);
                   2396:   fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax,bage,fage);
                   2397: /*--------- index.htm --------*/
                   2398: 
1.9       lievre   2399:   strcpy(optionfilehtm,optionfile);
                   2400:   strcat(optionfilehtm,".htm");
                   2401:   if((fichtm=fopen(optionfilehtm,"w"))==NULL)    {
                   2402:     printf("Problem with %s \n",optionfilehtm);goto end;
1.2       lievre   2403:   }
                   2404: 
1.9       lievre   2405:  fprintf(fichtm,"<body><ul> <font size=\"6\">Imach, Version 0.64b </font> <hr size=\"2\" color=\"#EC5E5E\"> 
1.8       lievre   2406: Titre=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>
                   2407: Total number of observations=%d <br>
                   2408: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>
                   2409: <hr  size=\"2\" color=\"#EC5E5E\"> 
                   2410: <li>Outputs files<br><br>\n
1.2       lievre   2411:         - Observed prevalence in each state: <a href=\"p%s\">p%s</a> <br>\n
                   2412: - Estimated parameters and the covariance matrix: <a href=\"%s\">%s</a> <br>
                   2413:         - Stationary prevalence in each state: <a href=\"pl%s\">pl%s</a> <br>
                   2414:         - Transition probabilities: <a href=\"pij%s\">pij%s</a><br>
                   2415:         - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>
                   2416:         - Life expectancies by age and initial health status: <a href=\"e%s\">e%s</a> <br>
                   2417:         - Variances of life expectancies by age and initial health status: <a href=\"v%s\">v%s</a><br>
                   2418:         - Health expectancies with their variances: <a href=\"t%s\">t%s</a> <br>
1.8       lievre   2419:         - Standard deviation of stationary prevalences: <a href=\"vpl%s\">vpl%s</a> <br><br>",title,datafile,firstpass,lastpass,stepm, weightopt,model,imx,jmin,jmax,jmean,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres);
1.2       lievre   2420: 
1.8       lievre   2421:  fprintf(fichtm," <li>Graphs</li><p>");
1.2       lievre   2422: 
1.7       lievre   2423:  m=cptcoveff;
1.2       lievre   2424:  if (cptcovn < 1) {m=1;ncodemax[1]=1;}
                   2425: 
                   2426:  j1=0;
                   2427:  for(k1=1; k1<=m;k1++){
                   2428:    for(i1=1; i1<=ncodemax[k1];i1++){
                   2429:        j1++;
                   2430:        if (cptcovn > 0) {
1.8       lievre   2431:         fprintf(fichtm,"<hr  size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.7       lievre   2432:         for (cpt=1; cpt<=cptcoveff;cpt++) 
                   2433:           fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtab[j1][cpt]]);
1.8       lievre   2434:         fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
1.2       lievre   2435:        }
                   2436:        fprintf(fichtm,"<br>- Probabilities: pe%s%d.gif<br>
                   2437: <img src=\"pe%s%d.gif\">",strtok(optionfile, "."),j1,strtok(optionfile, "."),j1);     
                   2438:        for(cpt=1; cpt<nlstate;cpt++){
                   2439:         fprintf(fichtm,"<br>- Prevalence of disability : p%s%d%d.gif<br>
                   2440: <img src=\"p%s%d%d.gif\">",strtok(optionfile, "."),cpt,j1,strtok(optionfile, "."),cpt,j1);
                   2441:        }
                   2442:     for(cpt=1; cpt<=nlstate;cpt++) {
                   2443:        fprintf(fichtm,"<br>- Observed and stationary prevalence (with confident
                   2444: interval) in state (%d): v%s%d%d.gif <br>
                   2445: <img src=\"v%s%d%d.gif\">",cpt,strtok(optionfile, "."),cpt,j1,strtok(optionfile, "."),cpt,j1);  
                   2446:      }
                   2447:      for(cpt=1; cpt<=nlstate;cpt++) {
                   2448:         fprintf(fichtm,"\n<br>- Health life expectancies by age and initial health state (%d): exp%s%d%d.gif <br>
1.5       lievre   2449: <img src=\"exp%s%d%d.gif\">",cpt,strtok(optionfile, "."),cpt,j1,strtok(optionfile, "."),cpt,j1);
1.2       lievre   2450:      }
                   2451:      fprintf(fichtm,"\n<br>- Total life expectancy by age and
                   2452: health expectancies in states (1) and (2): e%s%d.gif<br>
                   2453: <img src=\"e%s%d.gif\">",strtok(optionfile, "."),j1,strtok(optionfile, "."),j1);
                   2454: fprintf(fichtm,"\n</body>");
                   2455:    }
                   2456:  }
                   2457: fclose(fichtm);
                   2458: 
                   2459:   /*--------------- Prevalence limit --------------*/
                   2460:   
                   2461:   strcpy(filerespl,"pl");
                   2462:   strcat(filerespl,fileres);
                   2463:   if((ficrespl=fopen(filerespl,"w"))==NULL) {
                   2464:     printf("Problem with Prev limit resultfile: %s\n", filerespl);goto end;
                   2465:   }
                   2466:   printf("Computing prevalence limit: result on file '%s' \n", filerespl);
                   2467:   fprintf(ficrespl,"#Prevalence limit\n");
                   2468:   fprintf(ficrespl,"#Age ");
                   2469:   for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
                   2470:   fprintf(ficrespl,"\n");
                   2471:   
                   2472:   prlim=matrix(1,nlstate,1,nlstate);
                   2473:   pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
                   2474:   oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
                   2475:   newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
                   2476:   savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
                   2477:   oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
                   2478:   k=0;
                   2479:   agebase=agemin;
                   2480:   agelim=agemax;
                   2481:   ftolpl=1.e-10;
1.7       lievre   2482:   i1=cptcoveff;
1.2       lievre   2483:   if (cptcovn < 1){i1=1;}
                   2484: 
                   2485:   for(cptcov=1;cptcov<=i1;cptcov++){
                   2486:     for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
                   2487:        k=k+1;
                   2488:        /*printf("cptcov=%d cptcod=%d codtab=%d nbcode=%d\n",cptcov, cptcod,Tcode[cptcode],codtab[cptcod][cptcov]);*/
1.6       lievre   2489:        fprintf(ficrespl,"\n#******");
1.7       lievre   2490:        for(j=1;j<=cptcoveff;j++) 
                   2491:          fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
1.2       lievre   2492:        fprintf(ficrespl,"******\n");
                   2493:        
                   2494:        for (age=agebase; age<=agelim; age++){
                   2495:          prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
                   2496:          fprintf(ficrespl,"%.0f",age );
                   2497:          for(i=1; i<=nlstate;i++)
                   2498:          fprintf(ficrespl," %.5f", prlim[i][i]);
                   2499:          fprintf(ficrespl,"\n");
                   2500:        }
                   2501:       }
                   2502:     }
                   2503:   fclose(ficrespl);
                   2504:   /*------------- h Pij x at various ages ------------*/
                   2505:   
                   2506:   strcpy(filerespij,"pij");  strcat(filerespij,fileres);
                   2507:   if((ficrespij=fopen(filerespij,"w"))==NULL) {
                   2508:     printf("Problem with Pij resultfile: %s\n", filerespij);goto end;
                   2509:   }
                   2510:   printf("Computing pij: result on file '%s' \n", filerespij);
                   2511:   
                   2512:   stepsize=(int) (stepm+YEARM-1)/YEARM;
                   2513:   if (stepm<=24) stepsize=2;
                   2514: 
                   2515:   agelim=AGESUP;
                   2516:   hstepm=stepsize*YEARM; /* Every year of age */
                   2517:   hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */ 
                   2518:   
                   2519:   k=0;
                   2520:   for(cptcov=1;cptcov<=i1;cptcov++){
                   2521:     for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
                   2522:       k=k+1;
                   2523:        fprintf(ficrespij,"\n#****** ");
1.7       lievre   2524:        for(j=1;j<=cptcoveff;j++) 
                   2525:          fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
1.2       lievre   2526:        fprintf(ficrespij,"******\n");
                   2527:        
                   2528:        for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
                   2529:          nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */ 
                   2530:          nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
                   2531:          p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
                   2532:          oldm=oldms;savm=savms;
                   2533:          hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);  
                   2534:          fprintf(ficrespij,"# Age");
                   2535:          for(i=1; i<=nlstate;i++)
                   2536:            for(j=1; j<=nlstate+ndeath;j++)
                   2537:              fprintf(ficrespij," %1d-%1d",i,j);
                   2538:          fprintf(ficrespij,"\n");
                   2539:          for (h=0; h<=nhstepm; h++){
                   2540:            fprintf(ficrespij,"%d %.0f %.0f",k,agedeb, agedeb+ h*hstepm/YEARM*stepm );
                   2541:            for(i=1; i<=nlstate;i++)
                   2542:              for(j=1; j<=nlstate+ndeath;j++)
                   2543:                fprintf(ficrespij," %.5f", p3mat[i][j][h]);
                   2544:            fprintf(ficrespij,"\n");
                   2545:          }
                   2546:          free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
                   2547:          fprintf(ficrespij,"\n");
                   2548:        }
                   2549:     }
                   2550:   }
                   2551: 
                   2552:   fclose(ficrespij);
                   2553: 
                   2554:   /*---------- Health expectancies and variances ------------*/
                   2555: 
                   2556:   strcpy(filerest,"t");
                   2557:   strcat(filerest,fileres);
                   2558:   if((ficrest=fopen(filerest,"w"))==NULL) {
                   2559:     printf("Problem with total LE resultfile: %s\n", filerest);goto end;
                   2560:   }
                   2561:   printf("Computing Total LEs with variances: file '%s' \n", filerest); 
                   2562: 
                   2563: 
                   2564:   strcpy(filerese,"e");
                   2565:   strcat(filerese,fileres);
                   2566:   if((ficreseij=fopen(filerese,"w"))==NULL) {
                   2567:     printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
                   2568:   }
                   2569:   printf("Computing Health Expectancies: result on file '%s' \n", filerese);
                   2570: 
                   2571:  strcpy(fileresv,"v");
                   2572:   strcat(fileresv,fileres);
                   2573:   if((ficresvij=fopen(fileresv,"w"))==NULL) {
                   2574:     printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
                   2575:   }
                   2576:   printf("Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
                   2577: 
                   2578:   k=0;
                   2579:   for(cptcov=1;cptcov<=i1;cptcov++){
                   2580:     for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
                   2581:       k=k+1;
                   2582:       fprintf(ficrest,"\n#****** ");
1.7       lievre   2583:       for(j=1;j<=cptcoveff;j++) 
                   2584:        fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
1.2       lievre   2585:       fprintf(ficrest,"******\n");
                   2586: 
                   2587:       fprintf(ficreseij,"\n#****** ");
1.7       lievre   2588:       for(j=1;j<=cptcoveff;j++) 
1.2       lievre   2589:        fprintf(ficreseij,"V%d=%d ",j,nbcode[j][codtab[k][j]]);
                   2590:       fprintf(ficreseij,"******\n");
                   2591: 
                   2592:       fprintf(ficresvij,"\n#****** ");
1.7       lievre   2593:       for(j=1;j<=cptcoveff;j++) 
1.2       lievre   2594:        fprintf(ficresvij,"V%d=%d ",j,nbcode[j][codtab[k][j]]);
                   2595:       fprintf(ficresvij,"******\n");
                   2596: 
                   2597:       eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
                   2598:       oldm=oldms;savm=savms;
                   2599:       evsij(fileres, eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k);  
                   2600:       vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
                   2601:       oldm=oldms;savm=savms;
                   2602:       varevsij(fileres, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k);
                   2603:       
                   2604:       fprintf(ficrest,"#Total LEs with variances: e.. (std) ");
                   2605:       for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
                   2606:       fprintf(ficrest,"\n");
                   2607:        
                   2608:       hf=1;
                   2609:       if (stepm >= YEARM) hf=stepm/YEARM;
                   2610:       epj=vector(1,nlstate+1);
                   2611:       for(age=bage; age <=fage ;age++){
                   2612:        prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
                   2613:        fprintf(ficrest," %.0f",age);
                   2614:        for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
                   2615:          for(i=1, epj[j]=0.;i <=nlstate;i++) {
                   2616:            epj[j] += prlim[i][i]*hf*eij[i][j][(int)age];
                   2617:          }
                   2618:          epj[nlstate+1] +=epj[j];
                   2619:        }
                   2620:        for(i=1, vepp=0.;i <=nlstate;i++)
                   2621:          for(j=1;j <=nlstate;j++)
                   2622:            vepp += vareij[i][j][(int)age];
                   2623:        fprintf(ficrest," %.2f (%.2f)", epj[nlstate+1],hf*sqrt(vepp));
                   2624:        for(j=1;j <=nlstate;j++){
                   2625:          fprintf(ficrest," %.2f (%.2f)", epj[j],hf*sqrt(vareij[j][j][(int)age]));
                   2626:        }
                   2627:        fprintf(ficrest,"\n");
                   2628:       }
                   2629:     }
                   2630:   }
                   2631:        
                   2632:  fclose(ficreseij);
                   2633:  fclose(ficresvij);
                   2634:   fclose(ficrest);
                   2635:   fclose(ficpar);
                   2636:   free_vector(epj,1,nlstate+1);
1.5       lievre   2637:   /*  scanf("%d ",i); */
1.2       lievre   2638: 
                   2639:   /*------- Variance limit prevalence------*/   
                   2640: 
                   2641: strcpy(fileresvpl,"vpl");
                   2642:   strcat(fileresvpl,fileres);
                   2643:   if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
                   2644:     printf("Problem with variance prev lim resultfile: %s\n", fileresvpl);
                   2645:     exit(0);
                   2646:   }
                   2647:   printf("Computing Variance-covariance of Prevalence limit: file '%s' \n", fileresvpl);
                   2648: 
                   2649:  k=0;
                   2650:  for(cptcov=1;cptcov<=i1;cptcov++){
                   2651:    for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
                   2652:      k=k+1;
                   2653:      fprintf(ficresvpl,"\n#****** ");
1.7       lievre   2654:      for(j=1;j<=cptcoveff;j++) 
                   2655:        fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
1.2       lievre   2656:      fprintf(ficresvpl,"******\n");
                   2657:      
                   2658:      varpl=matrix(1,nlstate,(int) bage, (int) fage);
                   2659:      oldm=oldms;savm=savms;
                   2660:      varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k);
                   2661:    }
                   2662:  }
                   2663: 
                   2664:   fclose(ficresvpl);
                   2665: 
                   2666:   /*---------- End : free ----------------*/
                   2667:   free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
                   2668:   
                   2669:   free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
                   2670:   free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
                   2671:   
                   2672:   
                   2673:   free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
                   2674:   free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
                   2675:   free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
                   2676:   free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
                   2677:   
                   2678:   free_matrix(matcov,1,npar,1,npar);
                   2679:   free_vector(delti,1,npar);
                   2680:   
                   2681:   free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
                   2682: 
                   2683:   printf("End of Imach\n");
                   2684:   /*  gettimeofday(&end_time, (struct timezone*)0);*/  /* after time */
                   2685:   
                   2686:   /* printf("Total time was %d Sec. %d uSec.\n", end_time.tv_sec -start_time.tv_sec, end_time.tv_usec -start_time.tv_usec);*/
                   2687:   /*printf("Total time was %d uSec.\n", total_usecs);*/
                   2688:   /*------ End -----------*/
1.12    ! lievre   2689: 
1.2       lievre   2690: 
                   2691:  end:
                   2692: #ifdef windows
                   2693:  chdir(pathcd);
                   2694: #endif 
1.8       lievre   2695:  /*system("wgnuplot graph.plt");*/
1.11      lievre   2696:  /*system("../gp37mgw/wgnuplot graph.plt");*/
                   2697:  /*system("cd ../gp37mgw");*/
                   2698:  system("..\\gp37mgw\\wgnuplot graph.plt");
1.2       lievre   2699: 
                   2700: #ifdef windows
                   2701:   while (z[0] != 'q') {
                   2702:     chdir(pathcd); 
                   2703:     printf("\nType e to edit output files, c to start again, and q for exiting: ");
                   2704:     scanf("%s",z);
                   2705:     if (z[0] == 'c') system("./imach");
                   2706:     else if (z[0] == 'e') {
                   2707:       chdir(path);
1.10      lievre   2708:       system(optionfilehtm);
1.2       lievre   2709:     }
                   2710:     else if (z[0] == 'q') exit(0);
                   2711:   }
                   2712: #endif 
                   2713: }
                   2714: 
                   2715: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>