Annotation of imach096d/src/imach.c, revision 1.10

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

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