Diff for /imach/src/imach.c between versions 1.77 and 1.87

version 1.77, 2003/05/16 16:49:47 version 1.87, 2003/06/18 12:26:01
Line 1 Line 1
 /* $Id$  /* $Id$
     $State$
     $Log$
     Revision 1.87  2003/06/18 12:26:01  brouard
     Version 0.96
   
     Revision 1.86  2003/06/17 20:04:08  brouard
     (Module): Change position of html and gnuplot routines and added
     routine fileappend.
   
     Revision 1.85  2003/06/17 13:12:43  brouard
     * imach.c (Repository): Check when date of death was earlier that
     current date of interview. It may happen when the death was just
     prior to the death. In this case, dh was negative and likelihood
     was wrong (infinity). We still send an "Error" but patch by
     assuming that the date of death was just one stepm after the
     interview.
     (Repository): Because some people have very long ID (first column)
     we changed int to long in num[] and we added a new lvector for
     memory allocation. But we also truncated to 8 characters (left
     truncation)
     (Repository): No more line truncation errors.
   
     Revision 1.84  2003/06/13 21:44:43  brouard
     * imach.c (Repository): Replace "freqsummary" at a correct
     place. It differs from routine "prevalence" which may be called
     many times. Probs is memory consuming and must be used with
     parcimony.
     Version 0.95a3 (should output exactly the same maximization than 0.8a2)
   
     Revision 1.83  2003/06/10 13:39:11  lievre
     *** empty log message ***
   
     Revision 1.82  2003/06/05 15:57:20  brouard
     Add log in  imach.c and  fullversion number is now printed.
   
   */
   /*
    Interpolated Markov Chain     Interpolated Markov Chain
   
   Short summary of the programme:    Short summary of the programme:
Line 58 Line 95
   read parameterfile    read parameterfile
   read datafile    read datafile
   concatwav    concatwav
     freqsummary
   if (mle >= 1)    if (mle >= 1)
     mlikeli      mlikeli
   print results files    print results files
Line 91 Line 129
 #include <stdlib.h>  #include <stdlib.h>
 #include <unistd.h>  #include <unistd.h>
   
   #include <sys/time.h>
   #include <time.h>
   #include "timeval.h"
   
 #define MAXLINE 256  #define MAXLINE 256
 #define GNUPLOTPROGRAM "gnuplot"  #define GNUPLOTPROGRAM "gnuplot"
 /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/  /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
 #define FILENAMELENGTH 80  #define FILENAMELENGTH 132
 /*#define DEBUG*/  /*#define DEBUG*/
 #define windows  /*#define windows*/
 #define GLOCK_ERROR_NOPATH              -1      /* empty path */  #define GLOCK_ERROR_NOPATH              -1      /* empty path */
 #define GLOCK_ERROR_GETCWD              -2      /* cannot get cwd */  #define GLOCK_ERROR_GETCWD              -2      /* cannot get cwd */
   
Line 111 Line 153
 #define YEARM 12. /* Number of months per year */  #define YEARM 12. /* Number of months per year */
 #define AGESUP 130  #define AGESUP 130
 #define AGEBASE 40  #define AGEBASE 40
 #ifdef windows  #ifdef unix
 #define DIRSEPARATOR '\\'  
 #define ODIRSEPARATOR '/'  
 #else  
 #define DIRSEPARATOR '/'  #define DIRSEPARATOR '/'
 #define ODIRSEPARATOR '\\'  #define ODIRSEPARATOR '\\'
   #else
   #define DIRSEPARATOR '\\'
   #define ODIRSEPARATOR '/'
 #endif  #endif
   
 char version[80]="Imach version 0.95a, May 2003, INED-EUROREVES ";  /* $Id$ */
   /* $State$ */
   
   char version[]="Imach version 0.96, June 2003, INED-EUROREVES ";
   char fullversion[]="$Revision$ $Date$"; 
 int erreur; /* Error number */  int erreur; /* Error number */
 int nvar;  int nvar;
 int cptcovn=0, cptcovage=0, cptcoveff=0,cptcov;  int cptcovn=0, cptcovage=0, cptcoveff=0,cptcov;
Line 132  int popbased=0; Line 178  int popbased=0;
 int *wav; /* Number of waves for this individuual 0 is possible */  int *wav; /* Number of waves for this individuual 0 is possible */
 int maxwav; /* Maxim number of waves */  int maxwav; /* Maxim number of waves */
 int jmin, jmax; /* min, max spacing between 2 waves */  int jmin, jmax; /* min, max spacing between 2 waves */
   int gipmx, gsw; /* Global variables on the number of contributions 
                      to the likelihood and the sum of weights (done by funcone)*/
 int mle, weightopt;  int mle, weightopt;
 int **mw; /* mw[mi][i] is number of the mi wave for this individual */  int **mw; /* mw[mi][i] is number of the mi wave for this individual */
 int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */  int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
Line 142  double **oldm, **newm, **savm; /* Workin Line 190  double **oldm, **newm, **savm; /* Workin
 double **oldms, **newms, **savms; /* Fixed working pointers to matrices */  double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
 FILE *fic,*ficpar, *ficparo,*ficres,  *ficrespl, *ficrespij, *ficrest,*ficresf,*ficrespop;  FILE *fic,*ficpar, *ficparo,*ficres,  *ficrespl, *ficrespij, *ficrest,*ficresf,*ficrespop;
 FILE *ficlog, *ficrespow;  FILE *ficlog, *ficrespow;
   int globpr; /* Global variable for printing or not */
   double fretone; /* Only one call to likelihood */
   long ipmx; /* Number of contributions */
   double sw; /* Sum of weights */
   char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
   FILE *ficresilk;
 FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;  FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
 FILE *ficresprobmorprev;  FILE *ficresprobmorprev;
 FILE *fichtm; /* Html File */  FILE *fichtm; /* Html File */
Line 199  int estepm; Line 253  int estepm;
 /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/  /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
   
 int m,nb;  int m,nb;
 int *num, firstpass=0, lastpass=4,*cod, *ncodemax, *Tage;  long *num;
   int firstpass=0, lastpass=4,*cod, *ncodemax, *Tage;
 double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;  double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
 double **pmmij, ***probs;  double **pmmij, ***probs;
 double dateintmean=0;  double dateintmean=0;
Line 239  static int split( char *path, char *dirc Line 294  static int split( char *path, char *dirc
     dirc[l1-l2] = 0;                    /* add zero */      dirc[l1-l2] = 0;                    /* add zero */
   }    }
   l1 = strlen( dirc );                  /* length of directory */    l1 = strlen( dirc );                  /* length of directory */
 #ifdef windows    /*#ifdef windows
   if ( dirc[l1-1] != '\\' ) { dirc[l1] = '\\'; dirc[l1+1] = 0; }    if ( dirc[l1-1] != '\\' ) { dirc[l1] = '\\'; dirc[l1+1] = 0; }
 #else  #else
   if ( dirc[l1-1] != '/' ) { dirc[l1] = '/'; dirc[l1+1] = 0; }    if ( dirc[l1-1] != '/' ) { dirc[l1] = '/'; dirc[l1+1] = 0; }
 #endif  #endif
     */
   ss = strrchr( name, '.' );            /* find last / */    ss = strrchr( name, '.' );            /* find last / */
   ss++;    ss++;
   strcpy(ext,ss);                       /* save extension */    strcpy(ext,ss);                       /* save extension */
Line 327  void free_vector(double*v, int nl, int n Line 383  void free_vector(double*v, int nl, int n
 }  }
   
 /************************ivector *******************************/  /************************ivector *******************************/
 char *cvector(long nl,long nh)  int *ivector(long nl,long nh)
 {  {
   char *v;    int *v;
   v=(char *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(char)));    v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
   if (!v) nrerror("allocation failure in cvector");    if (!v) nrerror("allocation failure in ivector");
   return v-nl+NR_END;    return v-nl+NR_END;
 }  }
   
 /******************free ivector **************************/  /******************free ivector **************************/
 void free_cvector(char *v, long nl, long nh)  void free_ivector(int *v, long nl, long nh)
 {  {
   free((FREE_ARG)(v+nl-NR_END));    free((FREE_ARG)(v+nl-NR_END));
 }  }
   
 /************************ivector *******************************/  /************************lvector *******************************/
 int *ivector(long nl,long nh)  long *lvector(long nl,long nh)
 {  {
   int *v;    long *v;
   v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));    v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
   if (!v) nrerror("allocation failure in ivector");    if (!v) nrerror("allocation failure in ivector");
   return v-nl+NR_END;    return v-nl+NR_END;
 }  }
   
 /******************free ivector **************************/  /******************free lvector **************************/
 void free_ivector(int *v, long nl, long nh)  void free_lvector(long *v, long nl, long nh)
 {  {
   free((FREE_ARG)(v+nl-NR_END));    free((FREE_ARG)(v+nl-NR_END));
 }  }
Line 410  double **matrix(long nrl, long nrh, long Line 466  double **matrix(long nrl, long nrh, long
   
   for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;    for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
   return m;    return m;
   /* print *(*(m+1)+70) ou print m[1][70]; print m+1 or print &(m[1])     /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) 
    */     */
 }  }
   
Line 1163  double func( double *x) Line 1219  double func( double *x)
         ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;          ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
       } /* end of wave */        } /* end of wave */
     } /* end of individual */      } /* end of individual */
   }else{  /* ml=4 no inter-extrapolation */    }else if (mle==4){  /* ml=4 no inter-extrapolation */
     for (i=1,ipmx=0, sw=0.; i<=imx; i++){      for (i=1,ipmx=0, sw=0.; i<=imx; i++){
       for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];        for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
       for(mi=1; mi<= wav[i]-1; mi++){        for(mi=1; mi<= wav[i]-1; mi++){
Line 1185  double func( double *x) Line 1241  double func( double *x)
           oldm=newm;            oldm=newm;
         } /* end mult */          } /* end mult */
               
           s1=s[mw[mi][i]][i];
           s2=s[mw[mi+1][i]][i];
           if( s2 > nlstate){ 
             lli=log(out[s1][s2] - savm[s1][s2]);
           }else{
             lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
           }
           ipmx +=1;
           sw += weight[i];
           ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
   /*      printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */
         } /* end of wave */
       } /* end of individual */
     }else{  /* ml=5 no inter-extrapolation no jackson =0.8a */
       for (i=1,ipmx=0, sw=0.; i<=imx; i++){
         for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
         for(mi=1; mi<= wav[i]-1; mi++){
           for (ii=1;ii<=nlstate+ndeath;ii++)
             for (j=1;j<=nlstate+ndeath;j++){
               oldm[ii][j]=(ii==j ? 1.0 : 0.0);
               savm[ii][j]=(ii==j ? 1.0 : 0.0);
             }
           for(d=0; d<dh[mi][i]; d++){
             newm=savm;
             cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
             for (kk=1; kk<=cptcovage;kk++) {
               cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
             }
           
             out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
                          1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
             savm=oldm;
             oldm=newm;
           } /* end mult */
         
           s1=s[mw[mi][i]][i];
           s2=s[mw[mi+1][i]][i];
         lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */          lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
         ipmx +=1;          ipmx +=1;
         sw += weight[i];          sw += weight[i];
         ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;          ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
           /*printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]);*/
       } /* end of wave */        } /* end of wave */
     } /* end of individual */      } /* end of individual */
   } /* End of if */    } /* End of if */
Line 1198  double func( double *x) Line 1292  double func( double *x)
   return -l;    return -l;
 }  }
   
   /*************** log-likelihood *************/
   double funcone( double *x)
   {
     /* Same as likeli but slower because of a lot of printf and if */
     int i, ii, j, k, mi, d, kk;
     double l, ll[NLSTATEMAX], cov[NCOVMAX];
     double **out;
     double lli; /* Individual log likelihood */
     double llt;
     int s1, s2;
     double bbh, survp;
     /*extern weight */
     /* We are differentiating ll according to initial status */
     /*  for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
     /*for(i=1;i<imx;i++) 
       printf(" %d\n",s[4][i]);
     */
     cov[1]=1.;
   
     for(k=1; k<=nlstate; k++) ll[k]=0.;
   
     for (i=1,ipmx=0, sw=0.; i<=imx; i++){
       for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
       for(mi=1; mi<= wav[i]-1; mi++){
         for (ii=1;ii<=nlstate+ndeath;ii++)
           for (j=1;j<=nlstate+ndeath;j++){
             oldm[ii][j]=(ii==j ? 1.0 : 0.0);
             savm[ii][j]=(ii==j ? 1.0 : 0.0);
           }
         for(d=0; d<dh[mi][i]; d++){
           newm=savm;
           cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
           for (kk=1; kk<=cptcovage;kk++) {
             cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
           }
           out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
                        1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
           savm=oldm;
           oldm=newm;
         } /* end mult */
         
         s1=s[mw[mi][i]][i];
         s2=s[mw[mi+1][i]][i];
         bbh=(double)bh[mi][i]/(double)stepm; 
         /* bias is positive if real duration
          * is higher than the multiple of stepm and negative otherwise.
          */
         if( s2 > nlstate && (mle <5) ){  /* Jackson */
           lli=log(out[s1][s2] - savm[s1][s2]);
         } else if (mle==1){
           lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
         } else if(mle==2){
           lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* linear interpolation */
         } else if(mle==3){  /* exponential inter-extrapolation */
           lli= (savm[s1][s2]>(double)1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* exponential inter-extrapolation */
         } else if (mle==4){  /* mle=4 no inter-extrapolation */
           lli=log(out[s1][s2]); /* Original formula */
         } else{  /* ml>=5 no inter-extrapolation no jackson =0.8a */
           lli=log(out[s1][s2]); /* Original formula */
         } /* End of if */
         ipmx +=1;
         sw += weight[i];
         ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
   /*       printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */
         if(globpr){
           fprintf(ficresilk,"%ld %6d %1d %1d %1d %1d %3d %10.6f %6.4f\
    %10.6f %10.6f %10.6f ", \
                   num[i],i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],
                   2*weight[i]*lli,out[s1][s2],savm[s1][s2]);
           for(k=1,llt=0.,l=0.; k<=nlstate; k++){
             llt +=ll[k]*gipmx/gsw;
             fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
           }
           fprintf(ficresilk," %10.6f\n", -llt);
         }
       } /* end of wave */
     } /* end of individual */
     for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
     /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
     l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
     if(globpr==0){ /* First time we count the contributions and weights */
       gipmx=ipmx;
       gsw=sw;
     }
     return -l;
   }
   
   
   void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*funcone)(double []))
   {
     /* This routine should help understanding what is done with 
        the selection of individuals/waves and
        to check the exact contribution to the likelihood.
        Plotting could be done.
      */
     int k;
   
     if(*globpri !=0){ /* Just counts and sums no printings */
       strcpy(fileresilk,"ilk"); 
       strcat(fileresilk,fileres);
       if((ficresilk=fopen(fileresilk,"w"))==NULL) {
         printf("Problem with resultfile: %s\n", fileresilk);
         fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
       }
       fprintf(ficresilk, "#individual(line's_record) s1 s2 wave# effective_wave# number_of_matrices_product pij weight -2ln(pij)*weight 0pij_x 0pij_(x-stepm) cumulating_loglikeli_by_health_state(reweighted=-2ll*weightXnumber_of_contribs/sum_of_weights) and_total\n");
       fprintf(ficresilk, "#num_i i s1 s2 mi mw dh likeli weight out sav ");
       /*  i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
       for(k=1; k<=nlstate; k++) 
         fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
       fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
     }
   
     *fretone=(*funcone)(p);
     if(*globpri !=0){
       fclose(ficresilk);
       fprintf(fichtm,"\n<br>File of contributions to the likelihood: <a href=\"%s\">%s</a><br>\n",fileresilk,fileresilk);
       fflush(fichtm); 
     } 
     return;
   }
   
 /*********** Maximum Likelihood Estimation ***************/  /*********** Maximum Likelihood Estimation ***************/
   
Line 1206  void mlikeli(FILE *ficres,double p[], in Line 1420  void mlikeli(FILE *ficres,double p[], in
   int i,j, iter;    int i,j, iter;
   double **xi;    double **xi;
   double fret;    double fret;
     double fretone; /* Only one call to likelihood */
   char filerespow[FILENAMELENGTH];    char filerespow[FILENAMELENGTH];
   xi=matrix(1,npar,1,npar);    xi=matrix(1,npar,1,npar);
   for (i=1;i<=npar;i++)    for (i=1;i<=npar;i++)
Line 1223  void mlikeli(FILE *ficres,double p[], in Line 1438  void mlikeli(FILE *ficres,double p[], in
     for(j=1;j<=nlstate+ndeath;j++)      for(j=1;j<=nlstate+ndeath;j++)
       if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);        if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
   fprintf(ficrespow,"\n");    fprintf(ficrespow,"\n");
   
   powell(p,xi,npar,ftol,&iter,&fret,func);    powell(p,xi,npar,ftol,&iter,&fret,func);
   
   fclose(ficrespow);    fclose(ficrespow);
Line 1490  void lubksb(double **a, int n, int *indx Line 1706  void lubksb(double **a, int n, int *indx
 }   } 
   
 /************ Frequencies ********************/  /************ Frequencies ********************/
 void  freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, int *Tvaraff, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2,double jprev1, double mprev1,double anprev1,double jprev2, double mprev2,double anprev2)  void  freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, int *Tvaraff, int **nbcode, int *ncodemax,double **mint,double **anint)
 {  /* Some frequencies */  {  /* Some frequencies */
       
   int i, m, jk, k1,i1, j1, bool, z1,z2,j;    int i, m, jk, k1,i1, j1, bool, z1,z2,j;
Line 1544  void  freqsummary(char fileres[], int ia Line 1760  void  freqsummary(char fileres[], int ia
         if (bool==1){          if (bool==1){
           for(m=firstpass; m<=lastpass; m++){            for(m=firstpass; m<=lastpass; m++){
             k2=anint[m][i]+(mint[m][i]/12.);              k2=anint[m][i]+(mint[m][i]/12.);
             if ((k2>=dateprev1) && (k2<=dateprev2)) {              /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
               if(agev[m][i]==0) agev[m][i]=iagemax+1;                if(agev[m][i]==0) agev[m][i]=iagemax+1;
               if(agev[m][i]==1) agev[m][i]=iagemax+2;                if(agev[m][i]==1) agev[m][i]=iagemax+2;
               if (s[m][i]>0 && s[m][i]<=nlstate) prop[s[m][i]][(int)agev[m][i]] += weight[i];                if (s[m][i]>0 && s[m][i]<=nlstate) prop[s[m][i]][(int)agev[m][i]] += weight[i];
Line 1557  void  freqsummary(char fileres[], int ia Line 1773  void  freqsummary(char fileres[], int ia
                 dateintsum=dateintsum+k2;                  dateintsum=dateintsum+k2;
                 k2cpt++;                  k2cpt++;
               }                }
             }                /*}*/
           }            }
         }          }
       }        }
                 
       fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);        /*      fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
   
       if  (cptcovn>0) {        if  (cptcovn>0) {
         fprintf(ficresp, "\n#********** Variable ");           fprintf(ficresp, "\n#********** Variable "); 
Line 1623  void  freqsummary(char fileres[], int ia Line 1839  void  freqsummary(char fileres[], int ia
           if( i <= iagemax){            if( i <= iagemax){
             if(pos>=1.e-5){              if(pos>=1.e-5){
               fprintf(ficresp," %d %.5f %.0f %.0f",i,prop[jk][i]/posprop, prop[jk][i],posprop);                fprintf(ficresp," %d %.5f %.0f %.0f",i,prop[jk][i]/posprop, prop[jk][i],posprop);
               probs[i][jk][j1]= pp[jk]/pos;                /*probs[i][jk][j1]= pp[jk]/pos;*/
               /*printf("\ni=%d jk=%d j1=%d %.5f %.0f %.0f %f",i,jk,j1,pp[jk]/pos, pp[jk],pos,probs[i][jk][j1]);*/                /*printf("\ni=%d jk=%d j1=%d %.5f %.0f %.0f %f",i,jk,j1,pp[jk]/pos, pp[jk],pos,probs[i][jk][j1]);*/
             }              }
             else              else
Line 1656  void  freqsummary(char fileres[], int ia Line 1872  void  freqsummary(char fileres[], int ia
 }  }
   
 /************ Prevalence ********************/  /************ Prevalence ********************/
 void prevalence(double agemin, double agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2, int firstpass, int lastpass)  void prevalence(double ***probs, double agemin, double agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2, int firstpass, int lastpass)
 {    {  
   /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people    /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
      in each health status at the date of interview (if between dateprev1 and dateprev2).       in each health status at the date of interview (if between dateprev1 and dateprev2).
Line 1775  void  concatwav(int wav[], int **dh, int Line 1991  void  concatwav(int wav[], int **dh, int
     wav[i]=mi;      wav[i]=mi;
     if(mi==0){      if(mi==0){
       if(first==0){        if(first==0){
         printf("Warning! None valid information for:%d line=%d (skipped) and may be others, see log file\n",num[i],i);          printf("Warning! None valid information for:%ld line=%d (skipped) and may be others, see log file\n",num[i],i);
         first=1;          first=1;
       }        }
       if(first==1){        if(first==1){
         fprintf(ficlog,"Warning! None valid information for:%d line=%d (skipped)\n",num[i],i);          fprintf(ficlog,"Warning! None valid information for:%ld line=%d (skipped)\n",num[i],i);
       }        }
     } /* end mi==0 */      } /* end mi==0 */
   } /* End individuals */    } /* End individuals */
Line 1791  void  concatwav(int wav[], int **dh, int Line 2007  void  concatwav(int wav[], int **dh, int
       else{        else{
         if (s[mw[mi+1][i]][i] > nlstate) { /* A death */          if (s[mw[mi+1][i]][i] > nlstate) { /* A death */
           if (agedc[i] < 2*AGESUP) {            if (agedc[i] < 2*AGESUP) {
           j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);               j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12); 
           if(j==0) j=1;  /* Survives at least one month after exam */              if(j==0) j=1;  /* Survives at least one month after exam */
           k=k+1;              else if(j<0){
           if (j >= jmax) jmax=j;                printf("Error! Negative delay (%d to death) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
           if (j <= jmin) jmin=j;                j=1; /* Careful Patch */
           sum=sum+j;                printf("   We assumed that the date of interview was correct (and not the date of death) and postponed the death %d month(s) (one stepm) after the interview.\n  You MUST fix the contradiction between dates.\n",stepm);
           /*if (j<0) printf("j=%d num=%d \n",j,i);*/                printf("Error! Negative delay (%d to death) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
           /*      printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/                fprintf(ficlog,"   We assumed that the date of interview was correct (and not the date of death) and postponed the death %d month(s) (one stepm) after the interview.\n  You MUST fix the contradiction between dates.\n",stepm);
           if(j<0)printf("Warning! Negative delay (%d) between waves %d and %d of individual at line %d who is aged %.1f with statuses %d %d\n ",j,mw[mi][i],mw[mi+1][i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);              }
               k=k+1;
               if (j >= jmax) jmax=j;
               if (j <= jmin) jmin=j;
               sum=sum+j;
               /*if (j<0) printf("j=%d num=%d \n",j,i);*/
               /*    printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
           }            }
         }          }
         else{          else{
Line 1810  void  concatwav(int wav[], int **dh, int Line 2032  void  concatwav(int wav[], int **dh, int
           else if (j <= jmin)jmin=j;            else if (j <= jmin)jmin=j;
           /*        if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */            /*        if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
           /*printf("%d %lf %d %d %d\n", i,agev[mw[mi][i]][i],j,s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);*/            /*printf("%d %lf %d %d %d\n", i,agev[mw[mi][i]][i],j,s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);*/
           if(j<0)printf("Warning! Negative delay (%d to death) between waves %d and %d of individual at line %d who is aged %.1f with statuses %d %d\n ",j,mw[mi][i],mw[mi+1][i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);            if(j<0){
               printf("Error! Negative delay (%d) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
               fprintf(ficlog,"Error! Negative delay (%d) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
             }
           sum=sum+j;            sum=sum+j;
         }          }
         jk= j/stepm;          jk= j/stepm;
         jl= j -jk*stepm;          jl= j -jk*stepm;
         ju= j -(jk+1)*stepm;          ju= j -(jk+1)*stepm;
         if(mle <=1){           if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
           if(jl==0){            if(jl==0){
             dh[mi][i]=jk;              dh[mi][i]=jk;
             bh[mi][i]=0;              bh[mi][i]=0;
Line 1841  void  concatwav(int wav[], int **dh, int Line 2066  void  concatwav(int wav[], int **dh, int
             bh[mi][i]=ju; /* At least one step */              bh[mi][i]=ju; /* At least one step */
             /*  printf(" bh=%d ju=%d jl=%d dh=%d jk=%d stepm=%d %d\n",bh[mi][i],ju,jl,dh[mi][i],jk,stepm,i);*/              /*  printf(" bh=%d ju=%d jl=%d dh=%d jk=%d stepm=%d %d\n",bh[mi][i],ju,jl,dh[mi][i],jk,stepm,i);*/
           }            }
         }          } /* end if mle */
       } /* end if mle */        }
     } /* end wave */      } /* end wave */
   }    }
   jmean=sum/k;    jmean=sum/k;
Line 2151  void varevsij(char optionfilefiname[], d Line 2376  void varevsij(char optionfilefiname[], d
   else{    else{
     fprintf(ficgp,"\n# Routine varevsij");      fprintf(ficgp,"\n# Routine varevsij");
   }    }
   if((fichtm=fopen(optionfilehtm,"a"))==NULL) {  /*   if((fichtm=fopen(optionfilehtm,"a"))==NULL) { */
     printf("Problem with html file: %s\n", optionfilehtm);  /*     printf("Problem with html file: %s\n", optionfilehtm); */
     fprintf(ficlog,"Problem with html file: %s\n", optionfilehtm);  /*     fprintf(ficlog,"Problem with html file: %s\n", optionfilehtm); */
     exit(0);  /*     exit(0); */
   }  /*   } */
   else{  /*   else{ */
     fprintf(fichtm,"\n<li><h4> Computing probabilities of dying over estepm months as a weighted average (i.e global mortality independent of initial healh state)</h4></li>\n");    fprintf(fichtm,"\n<li><h4> Computing probabilities of dying over estepm months as a weighted average (i.e global mortality independent of initial healh state)</h4></li>\n");
     fprintf(fichtm,"\n<br>%s  <br>\n",digitp);    fprintf(fichtm,"\n<br>%s  <br>\n",digitp);
   }  /*   } */
   varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);    varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
   
   fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n#  (weighted average of eij where weights are the stable prevalence in health states i\n");    fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n#  (weighted average of eij where weights are the stable prevalence in health states i\n");
Line 2389  void varevsij(char optionfilefiname[], d Line 2614  void varevsij(char optionfilefiname[], d
   if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);    if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
   fclose(ficresprobmorprev);    fclose(ficresprobmorprev);
   fclose(ficgp);    fclose(ficgp);
   fclose(fichtm);  /*   fclose(fichtm); */
 }    }  /* end varevsij */
   
 /************ Variance of prevlim ******************/  /************ Variance of prevlim ******************/
 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)  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)
Line 2554  void varprob(char optionfilefiname[], do Line 2779  void varprob(char optionfilefiname[], do
   else{    else{
     fprintf(ficgp,"\n# Routine varprob");      fprintf(ficgp,"\n# Routine varprob");
   }    }
   if((fichtm=fopen(optionfilehtm,"a"))==NULL) {  /*   if((fichtm=fopen(optionfilehtm,"a"))==NULL) { */
     printf("Problem with html file: %s\n", optionfilehtm);  /*     printf("Problem with html file: %s\n", optionfilehtm); */
     fprintf(ficlog,"Problem with html file: %s\n", optionfilehtm);  /*     fprintf(ficlog,"Problem with html file: %s\n", optionfilehtm); */
     exit(0);  /*     exit(0); */
   }  /*   } */
   else{  /*   else{ */
     fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");      fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
     fprintf(fichtm,"\n");      fprintf(fichtm,"\n");
   
Line 2567  void varprob(char optionfilefiname[], do Line 2792  void varprob(char optionfilefiname[], do
     fprintf(fichtm,"\nWe have drawn ellipsoids of confidence around the p<inf>ij</inf>, p<inf>kl</inf> to understand the covariance between two incidences. They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");      fprintf(fichtm,"\nWe have drawn ellipsoids of confidence around the p<inf>ij</inf>, p<inf>kl</inf> to understand the covariance between two incidences. They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
     fprintf(fichtm,"\n<br> We have drawn x'cov<sup>-1</sup>x = 4 where x is the column vector (pij,pkl). It means that if pij and pkl where uncorrelated the (2X2) matrix would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 standard deviations wide on each axis. <br> When both incidences are correlated we diagonalised the inverse of the covariance matrix and made the appropriate rotation.<br> \n");      fprintf(fichtm,"\n<br> We have drawn x'cov<sup>-1</sup>x = 4 where x is the column vector (pij,pkl). It means that if pij and pkl where uncorrelated the (2X2) matrix would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 standard deviations wide on each axis. <br> When both incidences are correlated we diagonalised the inverse of the covariance matrix and made the appropriate rotation.<br> \n");
   
   }  /*   } */
   
   cov[1]=1;    cov[1]=1;
   tj=cptcoveff;    tj=cptcoveff;
Line 2784  void varprob(char optionfilefiname[], do Line 3009  void varprob(char optionfilefiname[], do
   fclose(ficresprobcov);    fclose(ficresprobcov);
   fclose(ficresprobcor);    fclose(ficresprobcor);
   fclose(ficgp);    fclose(ficgp);
   fclose(fichtm);  
 }  }
   
   
Line 2797  void printinghtml(char fileres[], char t Line 3021  void printinghtml(char fileres[], char t
                   double jprev2, double mprev2,double anprev2){                    double jprev2, double mprev2,double anprev2){
   int jj1, k1, i1, cpt;    int jj1, k1, i1, cpt;
   /*char optionfilehtm[FILENAMELENGTH];*/    /*char optionfilehtm[FILENAMELENGTH];*/
   if((fichtm=fopen(optionfilehtm,"a"))==NULL)    {  /*   if((fichtm=fopen(optionfilehtm,"a"))==NULL)    { */
     printf("Problem with %s \n",optionfilehtm), exit(0);  /*     printf("Problem with %s \n",optionfilehtm), exit(0); */
     fprintf(ficlog,"Problem with %s \n",optionfilehtm), exit(0);  /*     fprintf(ficlog,"Problem with %s \n",optionfilehtm), exit(0); */
   }  /*   } */
   
    fprintf(fichtm,"<ul><li><h4>Result files (first order: no variance)</h4>\n     fprintf(fichtm,"<ul><li><h4>Result files (first order: no variance)</h4>\n \
  - Observed prevalence in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): <a href=\"p%s\">p%s</a> <br>\n   - Observed prevalence in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): <a href=\"p%s\">p%s</a> <br>\n \
  - Estimated transition probabilities over %d (stepm) months: <a href=\"pij%s\">pij%s</a><br>\n   - Estimated transition probabilities over %d (stepm) months: <a href=\"pij%s\">pij%s</a><br>\n \
  - Stable prevalence in each health state: <a href=\"pl%s\">pl%s</a> <br>\n   - Stable prevalence in each health state: <a href=\"pl%s\">pl%s</a> <br>\n \
  - Life expectancies by age and initial health status (estepm=%2d months):    - Life expectancies by age and initial health status (estepm=%2d months): \
    <a href=\"e%s\">e%s</a> <br>\n</li>", \     <a href=\"e%s\">e%s</a> <br>\n</li>", \
   jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,fileres,fileres,stepm,fileres,fileres,fileres,fileres,estepm,fileres,fileres);    jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,fileres,fileres,stepm,fileres,fileres,fileres,fileres,estepm,fileres,fileres);
   
Line 2826  fprintf(fichtm," \n<ul><li><b>Graphs</b> Line 3050  fprintf(fichtm," \n<ul><li><b>Graphs</b>
        fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");         fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
      }       }
      /* Pij */       /* Pij */
      fprintf(fichtm,"<br>- Pij or Conditional probabilities to be observed in state j being in state i, %d (stepm) months before: pe%s%d1.png<br>       fprintf(fichtm,"<br>- Pij or Conditional probabilities to be observed in state j being in state i, %d (stepm) months before: pe%s%d1.png<br> \
 <img src=\"pe%s%d1.png\">",stepm,strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1);       <img src=\"pe%s%d1.png\">",stepm,strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1);     
      /* Quasi-incidences */       /* Quasi-incidences */
      fprintf(fichtm,"<br>- Pij or Conditional probabilities to be observed in state j being in state i %d (stepm) months before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too: pe%s%d2.png<br>       fprintf(fichtm,"<br>- Pij or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
    before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too: pe%s%d2.png<br> \
 <img src=\"pe%s%d2.png\">",stepm,strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1);   <img src=\"pe%s%d2.png\">",stepm,strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1); 
        /* Stable prevalence in each health state */         /* Stable prevalence in each health state */
        for(cpt=1; cpt<nlstate;cpt++){         for(cpt=1; cpt<nlstate;cpt++){
          fprintf(fichtm,"<br>- Stable prevalence in each health state : p%s%d%d.png<br>           fprintf(fichtm,"<br>- Stable prevalence in each health state : p%s%d%d.png<br> \
 <img src=\"p%s%d%d.png\">",strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1);  <img src=\"p%s%d%d.png\">",strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1);
        }         }
      for(cpt=1; cpt<=nlstate;cpt++) {       for(cpt=1; cpt<=nlstate;cpt++) {
         fprintf(fichtm,"\n<br>- Health life expectancies by age and initial health state (%d): exp%s%d%d.png <br>          fprintf(fichtm,"\n<br>- Health life expectancies by age and initial health state (%d): exp%s%d%d.png <br> \
 <img src=\"exp%s%d%d.png\">",cpt,strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1);  <img src=\"exp%s%d%d.png\">",cpt,strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1);
      }       }
      fprintf(fichtm,"\n<br>- Total life expectancy by age and       fprintf(fichtm,"\n<br>- Total life expectancy by age and \
 health expectancies in states (1) and (2): e%s%d.png<br>  health expectancies in states (1) and (2): e%s%d.png<br>\
 <img src=\"e%s%d.png\">",strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1);  <img src=\"e%s%d.png\">",strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1);
    } /* end i1 */     } /* end i1 */
  }/* End k1 */   }/* End k1 */
  fprintf(fichtm,"</ul>");   fprintf(fichtm,"</ul>");
   
   
  fprintf(fichtm,"\n<br><li><h4> Result files (second order: variances)</h4>\n   fprintf(fichtm,"\n<br><li><h4> Result files (second order: variances)</h4>\n\
  - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br>\n   - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br>\n\
  - Variance of one-step probabilities: <a href=\"prob%s\">prob%s</a> <br>\n   - Variance of one-step probabilities: <a href=\"prob%s\">prob%s</a> <br>\n\
  - Variance-covariance of one-step probabilities: <a href=\"probcov%s\">probcov%s</a> <br>\n   - Variance-covariance of one-step probabilities: <a href=\"probcov%s\">probcov%s</a> <br>\n\
  - Correlation matrix of one-step probabilities: <a href=\"probcor%s\">probcor%s</a> <br>\n   - Correlation matrix of one-step probabilities: <a href=\"probcor%s\">probcor%s</a> <br>\n\
  - Variances and covariances of life expectancies by age and initial health status (estepm=%d months): <a href=\"v%s\">v%s</a><br>\n    - Variances and covariances of life expectancies by age and initial health status (estepm=%d months): <a href=\"v%s\">v%s</a><br>\n\
  - Health expectancies with their variances (no covariance): <a href=\"t%s\">t%s</a> <br>\n   - Health expectancies with their variances (no covariance): <a href=\"t%s\">t%s</a> <br>\n\
  - Standard deviation of stable prevalences: <a href=\"vpl%s\">vpl%s</a> <br>\n",rfileres,rfileres,fileres,fileres,fileres,fileres,fileres,fileres, estepm, fileres,fileres,fileres,fileres,fileres,fileres);   - Standard deviation of stable prevalences: <a href=\"vpl%s\">vpl%s</a> <br>\n",rfileres,rfileres,fileres,fileres,fileres,fileres,fileres,fileres, estepm, fileres,fileres,fileres,fileres,fileres,fileres);
   
 /*  if(popforecast==1) fprintf(fichtm,"\n */  /*  if(popforecast==1) fprintf(fichtm,"\n */
Line 2879  fprintf(fichtm," <ul><li><b>Graphs</b></ Line 3104  fprintf(fichtm," <ul><li><b>Graphs</b></
        fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");         fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
      }       }
      for(cpt=1; cpt<=nlstate;cpt++) {       for(cpt=1; cpt<=nlstate;cpt++) {
        fprintf(fichtm,"<br>- Observed and period prevalence (with confident         fprintf(fichtm,"<br>- Observed and period prevalence (with confident\
 interval) in state (%d): v%s%d%d.png <br>  interval) in state (%d): v%s%d%d.png <br>\
 <img src=\"v%s%d%d.png\">",cpt,strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1);    <img src=\"v%s%d%d.png\">",cpt,strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1);  
      }       }
    } /* end i1 */     } /* end i1 */
  }/* End k1 */   }/* End k1 */
  fprintf(fichtm,"</ul>");   fprintf(fichtm,"</ul>");
 fclose(fichtm);   fflush(fichtm);
 }  }
   
 /******************* Gnuplot file **************/  /******************* Gnuplot file **************/
Line 2987  m=pow(2,cptcoveff); Line 3212  m=pow(2,cptcoveff);
       fprintf(ficgp,"\nset out \"p%s%d%d.png\" \n",strtok(optionfile, "."),cpt,k1);        fprintf(ficgp,"\nset out \"p%s%d%d.png\" \n",strtok(optionfile, "."),cpt,k1);
       fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter png small\nset size 0.65,0.65\nplot [%.f:%.f] \"pij%s\" u ($1==%d ? ($3):1/0):($%d/($%d",ageminpar,agemaxpar,fileres,k1,k+cpt+1,k+1);        fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter png small\nset size 0.65,0.65\nplot [%.f:%.f] \"pij%s\" u ($1==%d ? ($3):1/0):($%d/($%d",ageminpar,agemaxpar,fileres,k1,k+cpt+1,k+1);
               
       for (i=1; i<= nlstate ; i ++)        for (i=1; i< nlstate ; i ++)
         fprintf(ficgp,"+$%d",k+i+1);          fprintf(ficgp,"+$%d",k+i+1);
       fprintf(ficgp,")) t\"prev(%d,%d)\" w l",cpt,cpt+1);        fprintf(ficgp,")) t\"prev(%d,%d)\" w l",cpt,cpt+1);
               
Line 3126  prevforecast(char fileres[], double anpr Line 3351  prevforecast(char fileres[], double anpr
   char fileresf[FILENAMELENGTH];    char fileresf[FILENAMELENGTH];
   
   agelim=AGESUP;    agelim=AGESUP;
   prevalence(ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);    prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
     
   strcpy(fileresf,"f");     strcpy(fileresf,"f"); 
   strcat(fileresf,fileres);    strcat(fileresf,fileres);
Line 3249  populforecast(char fileres[], double anp Line 3474  populforecast(char fileres[], double anp
   agelim=AGESUP;    agelim=AGESUP;
   calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM;    calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM;
       
   prevalence(ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);    prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
       
       
   strcpy(filerespop,"pop");     strcpy(filerespop,"pop"); 
Line 3391  populforecast(char fileres[], double anp Line 3616  populforecast(char fileres[], double anp
   free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);    free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
   free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);    free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
   fclose(ficrespop);    fclose(ficrespop);
 }  } /* End of popforecast */
   
   int fileappend(FILE *fichier, char *optionfich)
   {
     if((fichier=fopen(optionfich,"a"))==NULL) {
       printf("Problem with file: %s\n", optionfich);
       fprintf(ficlog,"Problem with file: %s\n", optionfich);
       return (0);
     }
     fflush(fichier);
     return (1);
   }
 /***********************************************/  /***********************************************/
 /**************** Main Program *****************/  /**************** Main Program *****************/
 /***********************************************/  /***********************************************/
Line 3401  int main(int argc, char *argv[]) Line 3636  int main(int argc, char *argv[])
 {  {
   int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);    int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
   int i,j, k, n=MAXN,iter,m,size=100,cptcode, cptcod;    int i,j, k, n=MAXN,iter,m,size=100,cptcode, cptcod;
     int jj;
     int numlinepar=0; /* Current linenumber of parameter file */
     /*  FILE *fichtm; *//* Html File */
     /* FILE *ficgp;*/ /*Gnuplot File */
   double agedeb, agefin,hf;    double agedeb, agefin,hf;
   double ageminpar=1.e20,agemin=1.e20, agemaxpar=-1.e20, agemax=-1.e20;    double ageminpar=1.e20,agemin=1.e20, agemaxpar=-1.e20, agemax=-1.e20;
   
Line 3412  int main(int argc, char *argv[]) Line 3651  int main(int argc, char *argv[])
   double ***mobaverage;    double ***mobaverage;
   int *indx;    int *indx;
   char line[MAXLINE], linepar[MAXLINE];    char line[MAXLINE], linepar[MAXLINE];
   char path[80],pathc[80],pathcd[80],pathtot[80],model[80];    char path[132],pathc[132],pathcd[132],pathtot[132],model[132];
   int firstobs=1, lastobs=10;    int firstobs=1, lastobs=10;
   int sdeb, sfin; /* Status at beginning and end */    int sdeb, sfin; /* Status at beginning and end */
   int c,  h , cpt,l;    int c,  h , cpt,l;
Line 3444  int main(int argc, char *argv[]) Line 3683  int main(int argc, char *argv[])
   
   
   char z[1]="c", occ;    char z[1]="c", occ;
 #include <sys/time.h>  
 #include <time.h>  
   char stra[80], strb[80], strc[80], strd[80],stre[80],modelsav[80];    char stra[80], strb[80], strc[80], strd[80],stre[80],modelsav[80];
     char *strt, *strtend;
     char *stratrunc;
     int lstra;
   
     long total_usecs;
     struct timeval start_time, end_time, curr_time;
     struct timezone tzp;
     extern int gettimeofday();
     struct tm tmg, tm, *gmtime(), *localtime();
     long time_value;
     extern long time();
     
   /* long total_usecs;    /*   gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
      struct timeval start_time, end_time;    (void) gettimeofday(&start_time,&tzp);
       tm = *localtime(&start_time.tv_sec);
      gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */    tmg = *gmtime(&start_time.tv_sec);
     strt=asctime(&tm);
   
   /*  printf("Localtime (at start)=%s",strt); */
   /*  tp.tv_sec = tp.tv_sec +86400; */
   /*  tm = *localtime(&start_time.tv_sec); */
   /*   tmg.tm_year=tmg.tm_year +dsign*dyear; */
   /*   tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
   /*   tmg.tm_hour=tmg.tm_hour + 1; */
   /*   tp.tv_sec = mktime(&tmg); */
   /*   strt=asctime(&tmg); */
   /*   printf("Time(after) =%s",strt);  */
   /*  (void) time (&time_value);
   *  printf("time=%d,t-=%d\n",time_value,time_value-86400);
   *  tm = *localtime(&time_value);
   *  strt=asctime(&tm);
   *  printf("tim_value=%d,asctime=%s\n",time_value,strt); 
   */
   
   getcwd(pathcd, size);    getcwd(pathcd, size);
   
   printf("\n%s",version);    printf("\n%s\n%s",version,fullversion);
   if(argc <=1){    if(argc <=1){
     printf("\nEnter the parameter file name: ");      printf("\nEnter the parameter file name: ");
     scanf("%s",pathtot);      scanf("%s",pathtot);
Line 3462  int main(int argc, char *argv[]) Line 3729  int main(int argc, char *argv[])
   else{    else{
     strcpy(pathtot,argv[1]);      strcpy(pathtot,argv[1]);
   }    }
   /*if(getcwd(pathcd, 80)!= NULL)printf ("Error pathcd\n");*/    /*if(getcwd(pathcd, 132)!= NULL)printf ("Error pathcd\n");*/
   /*cygwin_split_path(pathtot,path,optionfile);    /*cygwin_split_path(pathtot,path,optionfile);
     printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/      printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
   /* cutv(path,optionfile,pathtot,'\\');*/    /* cutv(path,optionfile,pathtot,'\\');*/
   
   split(pathtot,path,optionfile,optionfilext,optionfilefiname);    split(pathtot,path,optionfile,optionfilext,optionfilefiname);
   printf("pathtot=%s, path=%s, optionfile=%s optionfilext=%s optionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);    printf("pathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
   chdir(path);    chdir(path);
   replace(pathc,path);    replace(pathc,path);
   
Line 3482  int main(int argc, char *argv[]) Line 3749  int main(int argc, char *argv[])
     goto end;      goto end;
   }    }
   fprintf(ficlog,"Log filename:%s\n",filelog);    fprintf(ficlog,"Log filename:%s\n",filelog);
   fprintf(ficlog,"\n%s",version);    fprintf(ficlog,"\n%s\n%s",version,fullversion);
   fprintf(ficlog,"\nEnter the parameter file name: ");    fprintf(ficlog,"\nEnter the parameter file name: ");
   fprintf(ficlog,"pathtot=%s, path=%s, optionfile=%s optionfilext=%s optionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);    fprintf(ficlog,"pathtot=%s, path=%s, optionfile=%s optionfilext=%s optionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
   
     printf("Localtime (at start)=%s",strt);
     fprintf(ficlog,"Localtime (at start)=%s",strt);
   fflush(ficlog);    fflush(ficlog);
   
   /* */    /* */
Line 3497  int main(int argc, char *argv[]) Line 3767  int main(int argc, char *argv[])
   if((ficpar=fopen(optionfile,"r"))==NULL)    {    if((ficpar=fopen(optionfile,"r"))==NULL)    {
     printf("Problem with optionfile %s\n",optionfile);      printf("Problem with optionfile %s\n",optionfile);
     fprintf(ficlog,"Problem with optionfile %s\n",optionfile);      fprintf(ficlog,"Problem with optionfile %s\n",optionfile);
       fflush(ficlog);
     goto end;      goto end;
   }    }
   
Line 3505  int main(int argc, char *argv[]) Line 3776  int main(int argc, char *argv[])
   if((ficparo=fopen(filereso,"w"))==NULL) {    if((ficparo=fopen(filereso,"w"))==NULL) {
     printf("Problem with Output resultfile: %s\n", filereso);      printf("Problem with Output resultfile: %s\n", filereso);
     fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);      fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
       fflush(ficlog);
     goto end;      goto end;
   }    }
   
   /* Reads comments: lines beginning with '#' */    /* Reads comments: lines beginning with '#' */
     numlinepar=0;
   while((c=getc(ficpar))=='#' && c!= EOF){    while((c=getc(ficpar))=='#' && c!= EOF){
     ungetc(c,ficpar);      ungetc(c,ficpar);
     fgets(line, MAXLINE, ficpar);      fgets(line, MAXLINE, ficpar);
       numlinepar++;
     puts(line);      puts(line);
     fputs(line,ficparo);      fputs(line,ficparo);
       fputs(line,ficlog);
   }    }
   ungetc(c,ficpar);    ungetc(c,ficpar);
   
   fscanf(ficpar,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%lf stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d model=%s\n",title, datafile, &lastobs, &firstpass,&lastpass,&ftol, &stepm, &ncovcol, &nlstate,&ndeath, &maxwav, &mle, &weightopt,model);    fscanf(ficpar,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%lf stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d model=%s\n",title, datafile, &lastobs, &firstpass,&lastpass,&ftol, &stepm, &ncovcol, &nlstate,&ndeath, &maxwav, &mle, &weightopt,model);
     numlinepar++;
   printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate,ndeath, maxwav, mle, weightopt,model);    printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate,ndeath, maxwav, mle, weightopt,model);
   fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol,nlstate,ndeath,maxwav, mle, weightopt,model);    fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol,nlstate,ndeath,maxwav, mle, weightopt,model);
     fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol,nlstate,ndeath,maxwav, mle, weightopt,model);
     fflush(ficlog);
   while((c=getc(ficpar))=='#' && c!= EOF){    while((c=getc(ficpar))=='#' && c!= EOF){
     ungetc(c,ficpar);      ungetc(c,ficpar);
     fgets(line, MAXLINE, ficpar);      fgets(line, MAXLINE, ficpar);
       numlinepar++;
     puts(line);      puts(line);
     fputs(line,ficparo);      fputs(line,ficparo);
       fputs(line,ficlog);
   }    }
   ungetc(c,ficpar);    ungetc(c,ficpar);
     
         
   covar=matrix(0,NCOVMAX,1,n);     covar=matrix(0,NCOVMAX,1,n); 
   cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement*/    cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement*/
Line 3541  int main(int argc, char *argv[]) Line 3821  int main(int argc, char *argv[])
   while((c=getc(ficpar))=='#' && c!= EOF){    while((c=getc(ficpar))=='#' && c!= EOF){
     ungetc(c,ficpar);      ungetc(c,ficpar);
     fgets(line, MAXLINE, ficpar);      fgets(line, MAXLINE, ficpar);
       numlinepar++;
     puts(line);      puts(line);
     fputs(line,ficparo);      fputs(line,ficparo);
       fputs(line,ficlog);
   }    }
   ungetc(c,ficpar);    ungetc(c,ficpar);
     
   param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);    param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
   for(i=1; i <=nlstate; i++)    for(i=1; i <=nlstate; i++){
     for(j=1; j <=nlstate+ndeath-1; j++){      j=0;
       for(jj=1; jj <=nlstate+ndeath; jj++){
         if(jj==i) continue;
         j++;
       fscanf(ficpar,"%1d%1d",&i1,&j1);        fscanf(ficpar,"%1d%1d",&i1,&j1);
         if ((i1 != i) && (j1 != j)){
           printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
           exit(1);
         }
       fprintf(ficparo,"%1d%1d",i1,j1);        fprintf(ficparo,"%1d%1d",i1,j1);
       if(mle==1)        if(mle==1)
         printf("%1d%1d",i,j);          printf("%1d%1d",i,j);
Line 3565  int main(int argc, char *argv[]) Line 3854  int main(int argc, char *argv[])
         fprintf(ficparo," %lf",param[i][j][k]);          fprintf(ficparo," %lf",param[i][j][k]);
       }        }
       fscanf(ficpar,"\n");        fscanf(ficpar,"\n");
         numlinepar++;
       if(mle==1)        if(mle==1)
         printf("\n");          printf("\n");
       fprintf(ficlog,"\n");        fprintf(ficlog,"\n");
       fprintf(ficparo,"\n");        fprintf(ficparo,"\n");
     }      }
       }  
     fflush(ficlog);
   
   npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/    npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
   
   p=param[1][1];    p=param[1][1];
Line 3579  int main(int argc, char *argv[]) Line 3871  int main(int argc, char *argv[])
   while((c=getc(ficpar))=='#' && c!= EOF){    while((c=getc(ficpar))=='#' && c!= EOF){
     ungetc(c,ficpar);      ungetc(c,ficpar);
     fgets(line, MAXLINE, ficpar);      fgets(line, MAXLINE, ficpar);
       numlinepar++;
     puts(line);      puts(line);
     fputs(line,ficparo);      fputs(line,ficparo);
       fputs(line,ficlog);
   }    }
   ungetc(c,ficpar);    ungetc(c,ficpar);
   
Line 3589  int main(int argc, char *argv[]) Line 3883  int main(int argc, char *argv[])
   for(i=1; i <=nlstate; i++){    for(i=1; i <=nlstate; i++){
     for(j=1; j <=nlstate+ndeath-1; j++){      for(j=1; j <=nlstate+ndeath-1; j++){
       fscanf(ficpar,"%1d%1d",&i1,&j1);        fscanf(ficpar,"%1d%1d",&i1,&j1);
         if ((i1-i)*(j1-j)!=0){
           printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
           exit(1);
         }
       printf("%1d%1d",i,j);        printf("%1d%1d",i,j);
       fprintf(ficparo,"%1d%1d",i1,j1);        fprintf(ficparo,"%1d%1d",i1,j1);
         fprintf(ficlog,"%1d%1d",i1,j1);
       for(k=1; k<=ncovmodel;k++){        for(k=1; k<=ncovmodel;k++){
         fscanf(ficpar,"%le",&delti3[i][j][k]);          fscanf(ficpar,"%le",&delti3[i][j][k]);
         printf(" %le",delti3[i][j][k]);          printf(" %le",delti3[i][j][k]);
         fprintf(ficparo," %le",delti3[i][j][k]);          fprintf(ficparo," %le",delti3[i][j][k]);
           fprintf(ficlog," %le",delti3[i][j][k]);
       }        }
       fscanf(ficpar,"\n");        fscanf(ficpar,"\n");
         numlinepar++;
       printf("\n");        printf("\n");
       fprintf(ficparo,"\n");        fprintf(ficparo,"\n");
         fprintf(ficlog,"\n");
     }      }
   }    }
     fflush(ficlog);
   
   delti=delti3[1][1];    delti=delti3[1][1];
   
   
Line 3610  int main(int argc, char *argv[]) Line 3914  int main(int argc, char *argv[])
   while((c=getc(ficpar))=='#' && c!= EOF){    while((c=getc(ficpar))=='#' && c!= EOF){
     ungetc(c,ficpar);      ungetc(c,ficpar);
     fgets(line, MAXLINE, ficpar);      fgets(line, MAXLINE, ficpar);
       numlinepar++;
     puts(line);      puts(line);
     fputs(line,ficparo);      fputs(line,ficparo);
       fputs(line,ficlog);
   }    }
   ungetc(c,ficpar);    ungetc(c,ficpar);
       
Line 3626  int main(int argc, char *argv[]) Line 3932  int main(int argc, char *argv[])
       fscanf(ficpar," %le",&matcov[i][j]);        fscanf(ficpar," %le",&matcov[i][j]);
       if(mle==1){        if(mle==1){
         printf(" %.5le",matcov[i][j]);          printf(" %.5le",matcov[i][j]);
         fprintf(ficlog," %.5le",matcov[i][j]);  
       }        }
       else        fprintf(ficlog," %.5le",matcov[i][j]);
         fprintf(ficlog," %.5le",matcov[i][j]);  
       fprintf(ficparo," %.5le",matcov[i][j]);        fprintf(ficparo," %.5le",matcov[i][j]);
     }      }
     fscanf(ficpar,"\n");      fscanf(ficpar,"\n");
       numlinepar++;
     if(mle==1)      if(mle==1)
       printf("\n");        printf("\n");
     fprintf(ficlog,"\n");      fprintf(ficlog,"\n");
Line 3646  int main(int argc, char *argv[]) Line 3951  int main(int argc, char *argv[])
     printf("\n");      printf("\n");
   fprintf(ficlog,"\n");    fprintf(ficlog,"\n");
   
     fflush(ficlog);
   
   /*-------- Rewriting paramater file ----------*/    /*-------- Rewriting paramater file ----------*/
   strcpy(rfileres,"r");    /* "Rparameterfile */    strcpy(rfileres,"r");    /* "Rparameterfile */
Line 3667  int main(int argc, char *argv[]) Line 3973  int main(int argc, char *argv[])
   n= lastobs;    n= lastobs;
   severity = vector(1,maxwav);    severity = vector(1,maxwav);
   outcome=imatrix(1,maxwav+1,1,n);    outcome=imatrix(1,maxwav+1,1,n);
   num=ivector(1,n);    num=lvector(1,n);
   moisnais=vector(1,n);    moisnais=vector(1,n);
   annais=vector(1,n);    annais=vector(1,n);
   moisdc=vector(1,n);    moisdc=vector(1,n);
Line 3703  int main(int argc, char *argv[]) Line 4009  int main(int argc, char *argv[])
       for (j=ncovcol;j>=1;j--){        for (j=ncovcol;j>=1;j--){
         cutv(stra, strb,line,' '); covar[j][i]=(double)(atoi(strb)); strcpy(line,stra);          cutv(stra, strb,line,' '); covar[j][i]=(double)(atoi(strb)); strcpy(line,stra);
       }         } 
       num[i]=atol(stra);        lstra=strlen(stra);
         if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
           stratrunc = &(stra[lstra-9]);
           num[i]=atol(stratrunc);
         }
         else
           num[i]=atol(stra);
                   
       /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){        /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
         printf("%d %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]),weight[i], (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]),  (mint[2][i]), (anint[2][i]), (s[2][i]),  (mint[3][i]), (anint[3][i]), (s[3][i]),  (mint[4][i]), (anint[4][i]), (s[4][i])); ij=ij+1;}*/          printf("%ld %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]),weight[i], (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]),  (mint[2][i]), (anint[2][i]), (s[2][i]),  (mint[3][i]), (anint[3][i]), (s[3][i]),  (mint[4][i]), (anint[4][i]), (s[4][i])); ij=ij+1;}*/
   
       i=i+1;        i=i+1;
     }      }
Line 3722  int main(int argc, char *argv[]) Line 4034  int main(int argc, char *argv[])
     }*/      }*/
    /*  for (i=1; i<=imx; i++){     /*  for (i=1; i<=imx; i++){
      if (s[4][i]==9)  s[4][i]=-1;        if (s[4][i]==9)  s[4][i]=-1; 
      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]));}*/       printf("%ld %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]), (weight[i]), (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]),  (mint[2][i]), (anint[2][i]), (s[2][i]),  (mint[3][i]), (anint[3][i]), (s[3][i]),  (mint[4][i]), (anint[4][i]), (s[4][i]));}*/
       
  for (i=1; i<=imx; i++)   for (i=1; i<=imx; i++)
     
Line 3825  int main(int argc, char *argv[]) Line 4137  int main(int argc, char *argv[])
         s[m][i]=-1;          s[m][i]=-1;
       }        }
       if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){        if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
         printf("Error! Date of death (month %2d and year %4d) of individual %d on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results are biased\n",(int)moisdc[i],(int)andc[i],num[i],i);          printf("Error! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results are biased\n",(int)moisdc[i],(int)andc[i],num[i],i);
         fprintf(ficlog,"Error! Date of death (month %2d and year %4d) of individual %d on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results are biased\n",(int)moisdc[i],(int)andc[i],num[i],i);          fprintf(ficlog,"Error! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results are biased\n",(int)moisdc[i],(int)andc[i],num[i],i);
         s[m][i]=-1;          s[m][i]=-1;
       }        }
       if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){        if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
         printf("Error! Month of death of individual %d on line %d was unknown %2d, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,(int)moisdc[i]);           printf("Error! Month of death of individual %ld on line %d was unknown %2d, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,(int)moisdc[i]); 
         fprintf(ficlog,"Error! Month of death of individual %d on line %d was unknown %f, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,moisdc[i]);           fprintf(ficlog,"Error! Month of death of individual %ld on line %d was unknown %f, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,moisdc[i]); 
         s[m][i]=-1;          s[m][i]=-1; /* We prefer to skip it (and to skip it in version 0.8a1 too */
       }        }
     }      }
   }    }
Line 3848  int main(int argc, char *argv[]) Line 4160  int main(int argc, char *argv[])
           /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/            /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
             else {              else {
               if ((int)andc[i]!=9999){                if ((int)andc[i]!=9999){
                 printf("Warning negative age at death: %d line:%d\n",num[i],i);                  printf("Warning negative age at death: %ld line:%d\n",num[i],i);
                 fprintf(ficlog,"Warning negative age at death: %d line:%d\n",num[i],i);                  fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
                 agev[m][i]=-1;                  agev[m][i]=-1;
               }                }
             }              }
Line 3893  int main(int argc, char *argv[]) Line 4205  int main(int argc, char *argv[])
   
   /*for (i=1; i<=imx; i++){    /*for (i=1; i<=imx; i++){
   for (m=firstpass; (m<lastpass); m++){    for (m=firstpass; (m<lastpass); m++){
      printf("%d %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);       printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
 }  }
   
 }*/  }*/
Line 3952  int main(int argc, char *argv[]) Line 4264  int main(int argc, char *argv[])
      }       }
      scanf("%d",i);*/       scanf("%d",i);*/
           
     /*------------ gnuplot -------------*/
     strcpy(optionfilegnuplot,optionfilefiname);
     strcat(optionfilegnuplot,".gp");
     if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
       printf("Problem with file %s",optionfilegnuplot);
     }
     else{
       fprintf(ficgp,"\n# %s\n", version); 
       fprintf(ficgp,"# %s\n", optionfilegnuplot); 
       fprintf(ficgp,"set missing 'NaNq'\n");
     }
     fclose(ficgp);
     /*--------- index.htm --------*/
   
     strcpy(optionfilehtm,optionfilefiname);
     strcat(optionfilehtm,".htm");
     if((fichtm=fopen(optionfilehtm,"w"))==NULL)    {
       printf("Problem with %s \n",optionfilehtm), exit(0);
     }
   
     fprintf(fichtm,"<body>\n<title>IMaCh %s</title>\n <font size=\"2\">%s <br> %s</font> \
   <hr size=\"2\" color=\"#EC5E5E\"> \n\
   Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>\n\
   \n\
   <hr  size=\"2\" color=\"#EC5E5E\">\
    <ul><li><h4>Parameter files</h4>\n\
    - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
    - Log file of the run: <a href=\"%s\">%s</a><br>\n\
    - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
    - Date and time at start: %s</ul>\n",\
             fileres,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt,\
             model,fileres,fileres,\
             filelog,filelog,optionfilegnuplot,optionfilegnuplot,strt);
     /*fclose(fichtm);*/
     fflush(fichtm);
   
   /* Calculates basic frequencies. Computes observed prevalence at single age    /* Calculates basic frequencies. Computes observed prevalence at single age
      and prints on file fileres'p'. */       and prints on file fileres'p'. */
     freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint);
   
   /*   if((fichtm=fopen(optionfilehtm,"a"))==NULL) { */
   /*     printf("Problem with file: %s\n", optionfilehtm); */
   /*     fprintf(ficlog,"Problem with file: %s\n", optionfilehtm); */
   /*   } */
   
   
   /*   if(fileappend(fichtm, optionfilehtm)){ */
       fprintf(fichtm,"\n");
       fprintf(fichtm,"<br>Total number of observations=%d <br>\n\
   Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
   Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
           imx,agemin,agemax,jmin,jmax,jmean);
   /*     fclose(fichtm); */
   /*   } */
   
     pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */      pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
     oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */      oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
Line 3966  int main(int argc, char *argv[]) Line 4330  int main(int argc, char *argv[])
      so we point p on param[1][1] so that p[1] maps on param[1][1][1] */       so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
   p=param[1][1]; /* *(*(*(param +1)+1)+0) */    p=param[1][1]; /* *(*(*(param +1)+1)+0) */
   
     globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
     likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
     printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
     for (k=1; k<=npar;k++)
       printf(" %d %8.5f",k,p[k]);
     printf("\n");
     globpr=1; /* to print the contributions */
     likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
     printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
     for (k=1; k<=npar;k++)
       printf(" %d %8.5f",k,p[k]);
     printf("\n");
   if(mle>=1){ /* Could be 1 or 2 */    if(mle>=1){ /* Could be 1 or 2 */
     mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);      mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
   }    }
Line 3997  int main(int argc, char *argv[]) Line 4373  int main(int argc, char *argv[])
         }          }
     }      }
   }    }
   if(mle==1){    if(mle!=0){
     /* Computing hessian and covariance matrix */      /* Computing hessian and covariance matrix */
     ftolhess=ftol; /* Usually correct */      ftolhess=ftol; /* Usually correct */
     hesscov(matcov, p, npar, delti, ftolhess, func);      hesscov(matcov, p, npar, delti, ftolhess, func);
Line 4128  int main(int argc, char *argv[]) Line 4504  int main(int argc, char *argv[])
   fprintf(ficparo,"popforecast=%d popfile=%s popfiledate=%.lf/%.lf/%.lf last-popfiledate=%.lf/%.lf/%.lf\n",popforecast,popfile,jpyram,mpyram,anpyram,jpyram1,mpyram1,anpyram1);    fprintf(ficparo,"popforecast=%d popfile=%s popfiledate=%.lf/%.lf/%.lf last-popfiledate=%.lf/%.lf/%.lf\n",popforecast,popfile,jpyram,mpyram,anpyram,jpyram1,mpyram1,anpyram1);
   fprintf(ficres,"popforecast=%d popfile=%s popfiledate=%.lf/%.lf/%.lf last-popfiledate=%.lf/%.lf/%.lf\n",popforecast,popfile,jpyram,mpyram,anpyram,jpyram1,mpyram1,anpyram1);    fprintf(ficres,"popforecast=%d popfile=%s popfiledate=%.lf/%.lf/%.lf last-popfiledate=%.lf/%.lf/%.lf\n",popforecast,popfile,jpyram,mpyram,anpyram,jpyram1,mpyram1,anpyram1);
   
   probs= ma3x(1,AGESUP,1,NCOVMAX, 1,NCOVMAX);    /*  freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint);*/
   freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);    /*,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
   
   /*------------ gnuplot -------------*/  
   strcpy(optionfilegnuplot,optionfilefiname);  
   strcat(optionfilegnuplot,".gp");  
   if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {  
     printf("Problem with file %s",optionfilegnuplot);  
   }  
   else{  
     fprintf(ficgp,"\n# %s\n", version);   
     fprintf(ficgp,"# %s\n", optionfilegnuplot);   
     fprintf(ficgp,"set missing 'NaNq'\n");  
   }  
   fclose(ficgp);  
   printinggnuplot(fileres, ageminpar,agemaxpar,fage, pathc,p);    printinggnuplot(fileres, ageminpar,agemaxpar,fage, pathc,p);
   /*--------- index.htm --------*/  
   
   strcpy(optionfilehtm,optionfile);  
   strcat(optionfilehtm,".htm");  
   if((fichtm=fopen(optionfilehtm,"w"))==NULL)    {  
     printf("Problem with %s \n",optionfilehtm), exit(0);  
   }  
   
   fprintf(fichtm,"<body> <font size=\"2\">%s </font> <hr size=\"2\" color=\"#EC5E5E\"> \n  
 Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>\n  
 \n  
 Total number of observations=%d <br>\n  
 Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n  
 Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n  
 <hr  size=\"2\" color=\"#EC5E5E\">  
  <ul><li><h4>Parameter files</h4>\n  
  - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n  
  - Log file of the run: <a href=\"%s\">%s</a><br>\n  
  - Gnuplot file name: <a href=\"%s\">%s</a></ul>\n",version,title,datafile,firstpass,lastpass,stepm, weightopt,model,imx,agemin,agemax,jmin,jmax,jmean,fileres,fileres,filelog,filelog,optionfilegnuplot,optionfilegnuplot);  
    fclose(fichtm);  
   
   printinghtml(fileres,title,datafile, firstpass, lastpass, stepm, weightopt,model,imx,jmin,jmax,jmean,rfileres,popforecast,estepm,jprev1,mprev1,anprev1,jprev2,mprev2,anprev2);    printinghtml(fileres,title,datafile, firstpass, lastpass, stepm, weightopt,\
                  model,imx,jmin,jmax,jmean,rfileres,popforecast,estepm,\
                  jprev1,mprev1,anprev1,jprev2,mprev2,anprev2);
     
   /*------------ free_vector  -------------*/    /*------------ free_vector  -------------*/
   chdir(path);    chdir(path);
Line 4174  Interval (in months) between two waves: Line 4520  Interval (in months) between two waves:
   free_imatrix(dh,1,lastpass-firstpass+1,1,imx);    free_imatrix(dh,1,lastpass-firstpass+1,1,imx);
   free_imatrix(bh,1,lastpass-firstpass+1,1,imx);    free_imatrix(bh,1,lastpass-firstpass+1,1,imx);
   free_imatrix(mw,1,lastpass-firstpass+1,1,imx);       free_imatrix(mw,1,lastpass-firstpass+1,1,imx);   
   free_ivector(num,1,n);    free_lvector(num,1,n);
   free_vector(agedc,1,n);    free_vector(agedc,1,n);
   /*free_matrix(covar,0,NCOVMAX,1,n);*/    /*free_matrix(covar,0,NCOVMAX,1,n);*/
   /*free_matrix(covar,1,NCOVMAX,1,n);*/    /*free_matrix(covar,1,NCOVMAX,1,n);*/
Line 4293  Interval (in months) between two waves: Line 4639  Interval (in months) between two waves:
   
   fclose(ficrespij);    fclose(ficrespij);
   
     probs= ma3x(1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
   
   /*---------- Forecasting ------------------*/    /*---------- Forecasting ------------------*/
   /*if((stepm == 1) && (strcmp(model,".")==0)){*/    /*if((stepm == 1) && (strcmp(model,".")==0)){*/
Line 4340  Interval (in months) between two waves: Line 4687  Interval (in months) between two waves:
   fprintf(ficlog,"Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);    fprintf(ficlog,"Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
   
   /* Computes prevalence between agemin (i.e minimal age computed) and no more ageminpar */    /* Computes prevalence between agemin (i.e minimal age computed) and no more ageminpar */
   prevalence(agemin, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);    prevalence(probs, agemin, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
   /*  printf("ageminpar=%f, agemax=%f, s[lastpass][imx]=%d, agev[lastpass][imx]=%f, nlstate=%d, imx=%d,  mint[lastpass][imx]=%f, anint[lastpass][imx]=%f,dateprev1=%f, dateprev2=%f, firstpass=%d, lastpass=%d\n",\    /*  printf("ageminpar=%f, agemax=%f, s[lastpass][imx]=%d, agev[lastpass][imx]=%f, nlstate=%d, imx=%d,  mint[lastpass][imx]=%f, anint[lastpass][imx]=%f,dateprev1=%f, dateprev2=%f, firstpass=%d, lastpass=%d\n",\
 ageminpar, agemax, s[lastpass][imx], agev[lastpass][imx], nlstate, imx, mint[lastpass][imx],anint[lastpass][imx], dateprev1, dateprev2, firstpass, lastpass);  ageminpar, agemax, s[lastpass][imx], agev[lastpass][imx], nlstate, imx, mint[lastpass][imx],anint[lastpass][imx], dateprev1, dateprev2, firstpass, lastpass);
   */    */
Line 4498  ageminpar, agemax, s[lastpass][imx], age Line 4845  ageminpar, agemax, s[lastpass][imx], age
   printf("See log file on %s\n",filelog);    printf("See log file on %s\n",filelog);
   fclose(ficlog);    fclose(ficlog);
   /*  gettimeofday(&end_time, (struct timezone*)0);*/  /* after time */    /*  gettimeofday(&end_time, (struct timezone*)0);*/  /* after time */
       (void) gettimeofday(&end_time,&tzp);
   /* 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);*/    tm = *localtime(&end_time.tv_sec);
   /*printf("Total time was %d uSec.\n", total_usecs);*/    tmg = *gmtime(&end_time.tv_sec);
     strtend=asctime(&tm);
     printf("Localtime at start %s and at end=%s",strt, strtend); 
     fprintf(ficlog,"Localtime at start %s and at end=%s",strt, strtend); 
     /*  printf("Total time used %d Sec\n", asc_time(end_time.tv_sec -start_time.tv_sec);*/
   
     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);
     fprintf(ficlog,"Total time was %d Sec. %d uSec.\n", end_time.tv_sec -start_time.tv_sec, end_time.tv_usec -start_time.tv_usec);
     /*  printf("Total time was %d uSec.\n", total_usecs);*/
   /*   if(fileappend(fichtm,optionfilehtm)){ */
     fprintf(fichtm,"<br>Localtime at start %s and at end=%s<br>",strt, strtend);
     fclose(fichtm);
   /*------ End -----------*/    /*------ End -----------*/
   
   end:    end:

Removed from v.1.77  
changed lines
  Added in v.1.87


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