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

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

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