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

version 1.70, 2003/02/05 12:40:38 version 1.77, 2003/05/16 16:49:47
Line 48 Line 48
   It is copyrighted identically to a GNU software product, ie programme and    It is copyrighted identically to a GNU software product, ie programme and
   software can be distributed freely for non commercial use. Latest version    software can be distributed freely for non commercial use. Latest version
   can be accessed at http://euroreves.ined.fr/imach .    can be accessed at http://euroreves.ined.fr/imach .
   
     Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
     or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
     
   **********************************************************************/    **********************************************************************/
   /*
     main
     read parameterfile
     read datafile
     concatwav
     if (mle >= 1)
       mlikeli
     print results files
     if mle==1 
        computes hessian
     read end of parameter file: agemin, agemax, bage, fage, estepm
         begin-prev-date,...
     open gnuplot file
     open html file
     stable prevalence
      for age prevalim()
     h Pij x
     variance of p varprob
     forecasting if prevfcast==1 prevforecast call prevalence()
     health expectancies
     Variance-covariance of DFLE
     prevalence()
      movingaverage()
     varevsij() 
     if popbased==1 varevsij(,popbased)
     total life expectancies
     Variance of stable prevalence
    end
   */
   
   
   
     
 #include <math.h>  #include <math.h>
 #include <stdio.h>  #include <stdio.h>
Line 83 Line 119
 #define ODIRSEPARATOR '\\'  #define ODIRSEPARATOR '\\'
 #endif  #endif
   
 char version[80]="Imach version 0.92, February 2003, INED-EUROREVES ";  char version[80]="Imach version 0.95a, May 2003, INED-EUROREVES ";
 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 105  double jmean; /* Mean space between 2 wa Line 141  double jmean; /* Mean space between 2 wa
 double **oldm, **newm, **savm; /* Working pointers to matrices */  double **oldm, **newm, **savm; /* Working pointers to matrices */
 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;  FILE *ficlog, *ficrespow;
 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 188  static int split( char *path, char *dirc Line 224  static int split( char *path, char *dirc
   if ( ss == NULL ) {                   /* no directory, so use current */    if ( ss == NULL ) {                   /* no directory, so use current */
     /*if(strrchr(path, ODIRSEPARATOR )==NULL)      /*if(strrchr(path, ODIRSEPARATOR )==NULL)
       printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/        printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
 #if     defined(__bsd__)                /* get current working directory */      /* get current working directory */
     extern char *getwd( );      /*    extern  char* getcwd ( char *buf , int len);*/
   
     if ( getwd( dirc ) == NULL ) {  
 #else  
     extern char *getcwd( );  
   
     if ( getcwd( dirc, FILENAME_MAX ) == NULL ) {      if ( getcwd( dirc, FILENAME_MAX ) == NULL ) {
 #endif  
       return( GLOCK_ERROR_GETCWD );        return( GLOCK_ERROR_GETCWD );
     }      }
     strcpy( name, path );               /* we've got it */      strcpy( name, path );               /* we've got it */
Line 297  void free_vector(double*v, int nl, int n Line 327  void free_vector(double*v, int nl, int n
 }  }
   
 /************************ivector *******************************/  /************************ivector *******************************/
   char *cvector(long nl,long nh)
   {
     char *v;
     v=(char *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(char)));
     if (!v) nrerror("allocation failure in cvector");
     return v-nl+NR_END;
   }
   
   /******************free ivector **************************/
   void free_cvector(char *v, long nl, long nh)
   {
     free((FREE_ARG)(v+nl-NR_END));
   }
   
   /************************ivector *******************************/
 int *ivector(long nl,long nh)  int *ivector(long nl,long nh)
 {  {
   int *v;    int *v;
Line 365  double **matrix(long nrl, long nrh, long Line 410  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]) 
      */
 }  }
   
 /*************************free matrix ************************/  /*************************free matrix ************************/
Line 404  double ***ma3x(long nrl, long nrh, long Line 451  double ***ma3x(long nrl, long nrh, long
     for (j=ncl+1; j<=nch; j++)       for (j=ncl+1; j<=nch; j++) 
       m[i][j]=m[i][j-1]+nlay;        m[i][j]=m[i][j-1]+nlay;
   }    }
   return m;    return m; 
     /*  gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
              &(m[i][j][k]) <=> *((*(m+i) + j)+k)
     */
 }  }
   
 /*************************free ma3x ************************/  /*************************free ma3x ************************/
Line 612  void powell(double p[], double **xi, int Line 662  void powell(double p[], double **xi, int
     del=0.0;       del=0.0; 
     printf("\nPowell iter=%d -2*LL=%.12f",*iter,*fret);      printf("\nPowell iter=%d -2*LL=%.12f",*iter,*fret);
     fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f",*iter,*fret);      fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f",*iter,*fret);
     for (i=1;i<=n;i++)       fprintf(ficrespow,"%d %.12f",*iter,*fret);
       for (i=1;i<=n;i++) {
       printf(" %d %.12f",i, p[i]);        printf(" %d %.12f",i, p[i]);
     fprintf(ficlog," %d %.12f",i, p[i]);        fprintf(ficlog," %d %.12lf",i, p[i]);
         fprintf(ficrespow," %.12lf", p[i]);
       }
     printf("\n");      printf("\n");
     fprintf(ficlog,"\n");      fprintf(ficlog,"\n");
       fprintf(ficrespow,"\n");
     for (i=1;i<=n;i++) {       for (i=1;i<=n;i++) { 
       for (j=1;j<=n;j++) xit[j]=xi[j][i];         for (j=1;j<=n;j++) xit[j]=xi[j][i]; 
       fptt=(*fret);         fptt=(*fret); 
Line 972  double func( double *x) Line 1026  double func( double *x)
          * is higher than the multiple of stepm and negative otherwise.           * is higher than the multiple of stepm and negative otherwise.
          */           */
         /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/          /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
         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 */          if( s2 > nlstate){ 
             /* i.e. if s2 is a death state and if the date of death is known then the contribution
                to the likelihood is the probability to die between last step unit time and current 
                step unit time, which is also the differences between probability to die before dh 
                and probability to die before dh-stepm . 
                In version up to 0.92 likelihood was computed
           as if date of death was unknown. Death was treated as any other
           health state: the date of the interview describes the actual state
           and not the date of a change in health state. The former idea was
           to consider that at each interview the state was recorded
           (healthy, disable or death) and IMaCh was corrected; but when we
           introduced the exact date of death then we should have modified
           the contribution of an exact death to the likelihood. This new
           contribution is smaller and very dependent of the step unit
           stepm. It is no more the probability to die between last interview
           and month of death but the probability to survive from last
           interview up to one month before death multiplied by the
           probability to die within a month. Thanks to Chris
           Jackson for correcting this bug.  Former versions increased
           mortality artificially. The bad side is that we add another loop
           which slows down the processing. The difference can be up to 10%
           lower mortality.
             */
             lli=log(out[s1][s2] - savm[s1][s2]);
           }else{
             lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
             /*  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 */
           } 
         /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/          /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
         /*if(lli ==000.0)*/          /*if(lli ==000.0)*/
         /*printf("bbh= %f lli=%f savm=%f out=%f %d\n",bbh,lli,savm[s1][s2], out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]],i); */          /*printf("bbh= %f lli=%f savm=%f out=%f %d\n",bbh,lli,savm[s1][s2], out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]],i); */
         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;
       } /* end of wave */        } /* end of wave */
Line 1123  double func( double *x) Line 1204  double func( double *x)
 void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))  void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
 {  {
   int i,j, iter;    int i,j, iter;
   double **xi,*delti;    double **xi;
   double fret;    double fret;
     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++)
     for (j=1;j<=npar;j++)      for (j=1;j<=npar;j++)
       xi[i][j]=(i==j ? 1.0 : 0.0);        xi[i][j]=(i==j ? 1.0 : 0.0);
   printf("Powell\n");  fprintf(ficlog,"Powell\n");    printf("Powell\n");  fprintf(ficlog,"Powell\n");
     strcpy(filerespow,"pow"); 
     strcat(filerespow,fileres);
     if((ficrespow=fopen(filerespow,"w"))==NULL) {
       printf("Problem with resultfile: %s\n", filerespow);
       fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
     }
     fprintf(ficrespow,"# Powell\n# iter -2*LL");
     for (i=1;i<=nlstate;i++)
       for(j=1;j<=nlstate+ndeath;j++)
         if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
     fprintf(ficrespow,"\n");
   powell(p,xi,npar,ftol,&iter,&fret,func);    powell(p,xi,npar,ftol,&iter,&fret,func);
   
    printf("\n#Number of iterations = %d, -2 Log likelihood = %.12f\n",iter,func(p));    fclose(ficrespow);
     printf("\n#Number of iterations = %d, -2 Log likelihood = %.12f\n",iter,func(p));
   fprintf(ficlog,"\n#Number of iterations = %d, -2 Log likelihood = %.12f \n",iter,func(p));    fprintf(ficlog,"\n#Number of iterations = %d, -2 Log likelihood = %.12f \n",iter,func(p));
   fprintf(ficres,"#Number of iterations = %d, -2 Log likelihood = %.12f \n",iter,func(p));    fprintf(ficres,"#Number of iterations = %d, -2 Log likelihood = %.12f \n",iter,func(p));
   
Line 1396  void lubksb(double **a, int n, int *indx Line 1490  void lubksb(double **a, int n, int *indx
 }   } 
   
 /************ Frequencies ********************/  /************ Frequencies ********************/
 void  freqsummary(char fileres[], int agemin, int agemax, 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, double dateprev1,double dateprev2,double jprev1, double mprev1,double anprev1,double jprev2, double mprev2,double anprev2)
 {  /* 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;
   int first;    int first;
   double ***freq; /* Frequencies */    double ***freq; /* Frequencies */
   double *pp;    double *pp, **prop;
   double pos, k2, dateintsum=0,k2cpt=0;    double pos,posprop, k2, dateintsum=0,k2cpt=0;
   FILE *ficresp;    FILE *ficresp;
   char fileresp[FILENAMELENGTH];    char fileresp[FILENAMELENGTH];
       
   pp=vector(1,nlstate);    pp=vector(1,nlstate);
   probs= ma3x(1,AGESUP,1,NCOVMAX, 1,NCOVMAX);    prop=matrix(1,nlstate,iagemin,iagemax+3);
   strcpy(fileresp,"p");    strcpy(fileresp,"p");
   strcat(fileresp,fileres);    strcat(fileresp,fileres);
   if((ficresp=fopen(fileresp,"w"))==NULL) {    if((ficresp=fopen(fileresp,"w"))==NULL) {
Line 1416  void  freqsummary(char fileres[], int ag Line 1510  void  freqsummary(char fileres[], int ag
     fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);      fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
     exit(0);      exit(0);
   }    }
   freq= ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,agemin,agemax+3);    freq= ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);
   j1=0;    j1=0;
       
   j=cptcoveff;    j=cptcoveff;
Line 1431  void  freqsummary(char fileres[], int ag Line 1525  void  freqsummary(char fileres[], int ag
         scanf("%d", i);*/          scanf("%d", i);*/
       for (i=-1; i<=nlstate+ndeath; i++)          for (i=-1; i<=nlstate+ndeath; i++)  
         for (jk=-1; jk<=nlstate+ndeath; jk++)            for (jk=-1; jk<=nlstate+ndeath; jk++)  
           for(m=agemin; m <= agemax+3; m++)            for(m=iagemin; m <= iagemax+3; m++)
             freq[i][jk][m]=0;              freq[i][jk][m]=0;
   
       for (i=1; i<=nlstate; i++)  
         for(m=iagemin; m <= iagemax+3; m++)
           prop[i][m]=0;
               
       dateintsum=0;        dateintsum=0;
       k2cpt=0;        k2cpt=0;
Line 1447  void  freqsummary(char fileres[], int ag Line 1545  void  freqsummary(char fileres[], int ag
           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]=agemax+1;                if(agev[m][i]==0) agev[m][i]=iagemax+1;
               if(agev[m][i]==1) agev[m][i]=agemax+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 (m<lastpass) {                if (m<lastpass) {
                 freq[s[m][i]][s[m+1][i]][(int)agev[m][i]] += weight[i];                  freq[s[m][i]][s[m+1][i]][(int)agev[m][i]] += weight[i];
                 freq[s[m][i]][s[m+1][i]][(int) agemax+3] += weight[i];                  freq[s[m][i]][s[m+1][i]][iagemax+3] += weight[i];
               }                }
                               
               if ((agev[m][i]>1) && (agev[m][i]< (agemax+3))) {                if ((agev[m][i]>1) && (agev[m][i]< (iagemax+3))) {
                 dateintsum=dateintsum+k2;                  dateintsum=dateintsum+k2;
                 k2cpt++;                  k2cpt++;
               }                }
Line 1474  void  freqsummary(char fileres[], int ag Line 1573  void  freqsummary(char fileres[], int ag
         fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);          fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
       fprintf(ficresp, "\n");        fprintf(ficresp, "\n");
               
       for(i=(int)agemin; i <= (int)agemax+3; i++){        for(i=iagemin; i <= iagemax+3; i++){
         if(i==(int)agemax+3){          if(i==iagemax+3){
           fprintf(ficlog,"Total");            fprintf(ficlog,"Total");
         }else{          }else{
           if(first==1){            if(first==1){
Line 1506  void  freqsummary(char fileres[], int ag Line 1605  void  freqsummary(char fileres[], int ag
         for(jk=1; jk <=nlstate ; jk++){          for(jk=1; jk <=nlstate ; jk++){
           for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)            for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)
             pp[jk] += freq[jk][m][i];              pp[jk] += freq[jk][m][i];
         }          }       
           for(jk=1,pos=0,posprop=0; jk <=nlstate ; jk++){
         for(jk=1,pos=0; jk <=nlstate ; jk++)  
           pos += pp[jk];            pos += pp[jk];
             posprop += prop[jk][i];
           }
         for(jk=1; jk <=nlstate ; jk++){          for(jk=1; jk <=nlstate ; jk++){
           if(pos>=1.e-5){            if(pos>=1.e-5){
             if(first==1)              if(first==1)
Line 1520  void  freqsummary(char fileres[], int ag Line 1620  void  freqsummary(char fileres[], int ag
               printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);                printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
             fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);              fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
           }            }
           if( i <= (int) agemax){            if( i <= iagemax){
             if(pos>=1.e-5){              if(pos>=1.e-5){
               fprintf(ficresp," %d %.5f %.0f %.0f",i,pp[jk]/pos, pp[jk],pos);                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
               fprintf(ficresp," %d NaNq %.0f %.0f",i,pp[jk],pos);                fprintf(ficresp," %d NaNq %.0f %.0f",i,prop[jk][i],posprop);
           }            }
         }          }
                   
Line 1538  void  freqsummary(char fileres[], int ag Line 1638  void  freqsummary(char fileres[], int ag
               printf(" %d%d=%.0f",jk,m,freq[jk][m][i]);                printf(" %d%d=%.0f",jk,m,freq[jk][m][i]);
               fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][i]);                fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][i]);
             }              }
         if(i <= (int) agemax)          if(i <= iagemax)
           fprintf(ficresp,"\n");            fprintf(ficresp,"\n");
         if(first==1)          if(first==1)
           printf("Others in log...\n");            printf("Others in log...\n");
Line 1549  void  freqsummary(char fileres[], int ag Line 1649  void  freqsummary(char fileres[], int ag
   dateintmean=dateintsum/k2cpt;     dateintmean=dateintsum/k2cpt; 
     
   fclose(ficresp);    fclose(ficresp);
   free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath,(int) agemin,(int) agemax+3);    free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);
   free_vector(pp,1,nlstate);    free_vector(pp,1,nlstate);
       free_matrix(prop,1,nlstate,iagemin, iagemax+3);
   /* End of Freq */    /* End of Freq */
 }  }
   
 /************ Prevalence ********************/  /************ Prevalence ********************/
 void prevalence(int agemin, float 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 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 1565  void prevalence(int agemin, float agemax Line 1665  void prevalence(int agemin, float agemax
     
   int i, m, jk, k1, i1, j1, bool, z1,z2,j;    int i, m, jk, k1, i1, j1, bool, z1,z2,j;
   double ***freq; /* Frequencies */    double ***freq; /* Frequencies */
   double *pp;    double *pp, **prop;
   double pos;     double pos,posprop; 
   double  y2; /* in fractional years */    double  y2; /* in fractional years */
     int iagemin, iagemax;
   
   pp=vector(1,nlstate);    iagemin= (int) agemin;
       iagemax= (int) agemax;
   freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,agemin,agemax+3);    /*pp=vector(1,nlstate);*/
     prop=matrix(1,nlstate,iagemin,iagemax+3); 
     /*  freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
   j1=0;    j1=0;
       
   j=cptcoveff;    j=cptcoveff;
Line 1581  void prevalence(int agemin, float agemax Line 1684  void prevalence(int agemin, float agemax
     for(i1=1; i1<=ncodemax[k1];i1++){      for(i1=1; i1<=ncodemax[k1];i1++){
       j1++;        j1++;
               
       for (i=-1; i<=nlstate+ndeath; i++)          for (i=1; i<=nlstate; i++)  
         for (jk=-1; jk<=nlstate+ndeath; jk++)            for(m=iagemin; m <= iagemax+3; m++)
           for(m=agemin; m <= agemax+3; m++)            prop[i][m]=0.0;
             freq[i][jk][m]=0;  
             
       for (i=1; i<=imx; i++) { /* Each individual */        for (i=1; i<=imx; i++) { /* Each individual */
         bool=1;          bool=1;
Line 1597  void prevalence(int agemin, float agemax Line 1699  void prevalence(int agemin, float agemax
           for(m=firstpass; m<=lastpass; m++){/* Other selection (we can limit to certain interviews*/            for(m=firstpass; m<=lastpass; m++){/* Other selection (we can limit to certain interviews*/
             y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */              y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
             if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */              if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
               if(agev[m][i]==0) agev[m][i]=agemax+1;                if(agev[m][i]==0) agev[m][i]=iagemax+1;
               if(agev[m][i]==1) agev[m][i]=agemax+2;                if(agev[m][i]==1) agev[m][i]=iagemax+2;
               if (m<lastpass) {                if((int)agev[m][i] <iagemin || (int)agev[m][i] >iagemax+3) printf("Error on individual =%d agev[m][i]=%f m=%d\n",i, agev[m][i],m); 
                 freq[s[m][i]][s[m+1][i]][(int)agev[m][i]] += weight[i];                if (s[m][i]>0 && s[m][i]<=nlstate) { 
                 freq[s[m][i]][s[m+1][i]][(int)(agemax+3)] += weight[i];                   /*if(i>4620) printf(" i=%d m=%d s[m][i]=%d (int)agev[m][i]=%d weight[i]=%f prop=%f\n",i,m,s[m][i],(int)agev[m][m],weight[i],prop[s[m][i]][(int)agev[m][i]]);*/
               }                  prop[s[m][i]][(int)agev[m][i]] += weight[i];
                   prop[s[m][i]][iagemax+3] += weight[i]; 
                 } 
             }              }
           } /* end selection of waves */            } /* end selection of waves */
         }          }
       }        }
       for(i=(int)agemin; i <= (int)agemax+3; i++){         for(i=iagemin; i <= iagemax+3; i++){  
         for(jk=1; jk <=nlstate ; jk++){  
           for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)  
             pp[jk] += freq[jk][m][i];   
         }  
         for(jk=1; jk <=nlstate ; jk++){  
           for(m=-1, pos=0; m <=0 ; m++)  
             pos += freq[jk][m][i];  
         }  
           
         for(jk=1; jk <=nlstate ; jk++){  
           for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)  
             pp[jk] += freq[jk][m][i];  
         }  
           
         for(jk=1,pos=0; jk <=nlstate ; jk++) pos += pp[jk];  
                   
         for(jk=1; jk <=nlstate ; jk++){              for(jk=1,posprop=0; jk <=nlstate ; jk++) { 
           if( i <= (int) agemax){            posprop += prop[jk][i]; 
             if(pos>=1.e-5){          } 
               probs[i][jk][j1]= pp[jk]/pos;  
             }          for(jk=1; jk <=nlstate ; jk++){     
           }            if( i <=  iagemax){ 
         }/* end jk */              if(posprop>=1.e-5){ 
       }/* end i */                probs[i][jk][j1]= prop[jk][i]/posprop;
               } 
             } 
           }/* end jk */ 
         }/* end i */ 
     } /* end i1 */      } /* end i1 */
   } /* end k1 */    } /* end k1 */
   
     
   free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath,(int) agemin,(int) agemax+3);  
   free_vector(pp,1,nlstate);  
       
 }  /* End of Freq */    /*  free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
     /*free_vector(pp,1,nlstate);*/
     free_matrix(prop,1,nlstate, iagemin,iagemax+3);
   }  /* End of prevalence */
   
 /************* Waves Concatenation ***************/  /************* Waves Concatenation ***************/
   
Line 1683  void  concatwav(int wav[], int **dh, int Line 1775  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, no any valid information for:%d line=%d and may be others, see log file\n",num[i],i);          printf("Warning! None valid information for:%d 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, no any valid information for:%d line=%d\n",num[i],i);          fprintf(ficlog,"Warning! None valid information for:%d line=%d (skipped)\n",num[i],i);
       }        }
     } /* end mi==0 */      } /* end mi==0 */
   }    } /* End individuals */
   
   for(i=1; i<=imx; i++){    for(i=1; i<=imx; i++){
     for(mi=1; mi<wav[i];mi++){      for(mi=1; mi<wav[i];mi++){
       if (stepm <=0)        if (stepm <=0)
         dh[mi][i]=1;          dh[mi][i]=1;
       else{        else{
         if (s[mw[mi+1][i]][i] > nlstate) {          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 */
Line 1705  void  concatwav(int wav[], int **dh, int Line 1797  void  concatwav(int wav[], int **dh, int
           if (j >= jmax) jmax=j;            if (j >= jmax) jmax=j;
           if (j <= jmin) jmin=j;            if (j <= jmin) jmin=j;
           sum=sum+j;            sum=sum+j;
           /*if (j<0) printf("j=%d num=%d \n",j,i); */            /*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);*/            /*      printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
             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]);
           }            }
         }          }
         else{          else{
Line 1716  void  concatwav(int wav[], int **dh, int Line 1809  void  concatwav(int wav[], int **dh, int
           if (j >= jmax) jmax=j;            if (j >= jmax) jmax=j;
           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]);*/
             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]);
           sum=sum+j;            sum=sum+j;
         }          }
         jk= j/stepm;          jk= j/stepm;
Line 1744  void  concatwav(int wav[], int **dh, int Line 1839  void  concatwav(int wav[], int **dh, int
           if(dh[mi][i]==0){            if(dh[mi][i]==0){
             dh[mi][i]=1; /* At least one step */              dh[mi][i]=1; /* At least one step */
             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 */
Line 1829  void evsij(char fileres[], double ***eij Line 1924  void evsij(char fileres[], double ***eij
   double ***gradg, ***trgradg;    double ***gradg, ***trgradg;
   int theta;    int theta;
   
   varhe=ma3x(1,nlstate*2,1,nlstate*2,(int) bage, (int) fage);    varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
   xp=vector(1,npar);    xp=vector(1,npar);
   dnewm=matrix(1,nlstate*2,1,npar);    dnewm=matrix(1,nlstate*nlstate,1,npar);
   doldm=matrix(1,nlstate*2,1,nlstate*2);    doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
       
   fprintf(ficreseij,"# Health expectancies\n");    fprintf(ficreseij,"# Health expectancies\n");
   fprintf(ficreseij,"# Age");    fprintf(ficreseij,"# Age");
Line 1878  void evsij(char fileres[], double ***eij Line 1973  void evsij(char fileres[], double ***eij
     /* if (stepm >= YEARM) hstepm=1;*/      /* if (stepm >= YEARM) hstepm=1;*/
     nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */      nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
     p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);      p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
     gradg=ma3x(0,nhstepm,1,npar,1,nlstate*2);      gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
     gp=matrix(0,nhstepm,1,nlstate*2);      gp=matrix(0,nhstepm,1,nlstate*nlstate);
     gm=matrix(0,nhstepm,1,nlstate*2);      gm=matrix(0,nhstepm,1,nlstate*nlstate);
   
     /* Computed by stepm unit matrices, product of hstepm matrices, stored      /* Computed by stepm unit matrices, product of hstepm matrices, stored
        in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */         in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
Line 1917  void evsij(char fileres[], double ***eij Line 2012  void evsij(char fileres[], double ***eij
         for(i=1;i<=nlstate;i++){          for(i=1;i<=nlstate;i++){
           cptj=cptj+1;            cptj=cptj+1;
           for(h=0, gm[h][cptj]=0.; h<=nhstepm-1; h++){            for(h=0, gm[h][cptj]=0.; h<=nhstepm-1; h++){
   
             gm[h][cptj] = (p3mat[i][j][h]+p3mat[i][j][h+1])/2.;              gm[h][cptj] = (p3mat[i][j][h]+p3mat[i][j][h+1])/2.;
           }            }
         }          }
       }        }
       for(j=1; j<= nlstate*2; j++)        for(j=1; j<= nlstate*nlstate; j++)
         for(h=0; h<=nhstepm-1; h++){          for(h=0; h<=nhstepm-1; h++){
           gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];            gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
         }          }
Line 1929  void evsij(char fileres[], double ***eij Line 2025  void evsij(char fileres[], double ***eij
         
 /* End theta */  /* End theta */
   
      trgradg =ma3x(0,nhstepm,1,nlstate*2,1,npar);       trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
   
      for(h=0; h<=nhstepm-1; h++)       for(h=0; h<=nhstepm-1; h++)
       for(j=1; j<=nlstate*2;j++)        for(j=1; j<=nlstate*nlstate;j++)
         for(theta=1; theta <=npar; theta++)          for(theta=1; theta <=npar; theta++)
           trgradg[h][j][theta]=gradg[h][theta][j];            trgradg[h][j][theta]=gradg[h][theta][j];
             
   
      for(i=1;i<=nlstate*2;i++)       for(i=1;i<=nlstate*nlstate;i++)
       for(j=1;j<=nlstate*2;j++)        for(j=1;j<=nlstate*nlstate;j++)
         varhe[i][j][(int)age] =0.;          varhe[i][j][(int)age] =0.;
   
      printf("%d|",(int)age);fflush(stdout);       printf("%d|",(int)age);fflush(stdout);
      fprintf(ficlog,"%d|",(int)age);fflush(ficlog);       fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
      for(h=0;h<=nhstepm-1;h++){       for(h=0;h<=nhstepm-1;h++){
       for(k=0;k<=nhstepm-1;k++){        for(k=0;k<=nhstepm-1;k++){
         matprod2(dnewm,trgradg[h],1,nlstate*2,1,npar,1,npar,matcov);          matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
         matprod2(doldm,dnewm,1,nlstate*2,1,npar,1,nlstate*2,gradg[k]);          matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
         for(i=1;i<=nlstate*2;i++)          for(i=1;i<=nlstate*nlstate;i++)
           for(j=1;j<=nlstate*2;j++)            for(j=1;j<=nlstate*nlstate;j++)
             varhe[i][j][(int)age] += doldm[i][j]*hf*hf;              varhe[i][j][(int)age] += doldm[i][j]*hf*hf;
       }        }
     }      }
Line 1971  void evsij(char fileres[], double ***eij Line 2067  void evsij(char fileres[], double ***eij
       }        }
     fprintf(ficreseij,"\n");      fprintf(ficreseij,"\n");
         
     free_matrix(gm,0,nhstepm,1,nlstate*2);      free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
     free_matrix(gp,0,nhstepm,1,nlstate*2);      free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
     free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*2);      free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
     free_ma3x(trgradg,0,nhstepm,1,nlstate*2,1,npar);      free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
     free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);      free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
   }    }
   printf("\n");    printf("\n");
   fprintf(ficlog,"\n");    fprintf(ficlog,"\n");
   
   free_vector(xp,1,npar);    free_vector(xp,1,npar);
   free_matrix(dnewm,1,nlstate*2,1,npar);    free_matrix(dnewm,1,nlstate*nlstate,1,npar);
   free_matrix(doldm,1,nlstate*2,1,nlstate*2);    free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
   free_ma3x(varhe,1,nlstate*2,1,nlstate*2,(int) bage, (int)fage);    free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
 }  }
   
 /************ Variance ******************/  /************ Variance ******************/
Line 2279  void varevsij(char optionfilefiname[], d Line 2375  void varevsij(char optionfilefiname[], d
   fprintf(ficgp,"\n replot \"%s\"  u 1:(($3+1.96*$4)) t \"95\%% interval\" w l 2 ",fileresprobmorprev);    fprintf(ficgp,"\n replot \"%s\"  u 1:(($3+1.96*$4)) t \"95\%% interval\" w l 2 ",fileresprobmorprev);
   fprintf(ficgp,"\n replot \"%s\"  u 1:(($3-1.96*$4)) not w l 2 ",fileresprobmorprev);    fprintf(ficgp,"\n replot \"%s\"  u 1:(($3-1.96*$4)) not w l 2 ",fileresprobmorprev);
   fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",fileresprobmorprev,fileresprobmorprev);    fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",fileresprobmorprev,fileresprobmorprev);
   fprintf(fichtm,"\n<br> Probability is computed over estepm=%d months. <br> <img src=\"varmuptjgr%s%s.png\"> <br>\n", estepm,digitp,digit);    fprintf(fichtm,"\n<br> Probability is computed over estepm=%d months. <br> <img src=\"varmuptjgr%s%s%s.png\"> <br>\n", estepm,digitp,optionfilefiname,digit);
   /*  fprintf(fichtm,"\n<br> Probability is computed over estepm=%d months and then divided by estepm and multiplied by %.0f in order to have the probability to die over a year <br> <img src=\"varmuptjgr%s%s.png\"> <br>\n", stepm,YEARM,digitp,digit);    /*  fprintf(fichtm,"\n<br> Probability is computed over estepm=%d months and then divided by estepm and multiplied by %.0f in order to have the probability to die over a year <br> <img src=\"varmuptjgr%s%s.png\"> <br>\n", stepm,YEARM,digitp,digit);
 */  */
   fprintf(ficgp,"\nset out \"varmuptjgr%s%s.png\";replot;",digitp,digit);    fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.png\";replot;",digitp,optionfilefiname,digit);
   
   free_vector(xp,1,npar);    free_vector(xp,1,npar);
   free_matrix(doldm,1,nlstate,1,nlstate);    free_matrix(doldm,1,nlstate,1,nlstate);
Line 2518  void varprob(char optionfilefiname[], do Line 2614  void varprob(char optionfilefiname[], do
           
         for(theta=1; theta <=npar; theta++){          for(theta=1; theta <=npar; theta++){
           for(i=1; i<=npar; i++)            for(i=1; i<=npar; i++)
             xp[i] = x[i] + (i==theta ?delti[theta]:0);              xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
                       
           pmij(pmmij,cov,ncovmodel,xp,nlstate);            pmij(pmmij,cov,ncovmodel,xp,nlstate);
                       
Line 2531  void varprob(char optionfilefiname[], do Line 2627  void varprob(char optionfilefiname[], do
           }            }
                       
           for(i=1; i<=npar; i++)            for(i=1; i<=npar; i++)
             xp[i] = x[i] - (i==theta ?delti[theta]:0);              xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
           
           pmij(pmmij,cov,ncovmodel,xp,nlstate);            pmij(pmmij,cov,ncovmodel,xp,nlstate);
           k=0;            k=0;
Line 2543  void varprob(char optionfilefiname[], do Line 2639  void varprob(char optionfilefiname[], do
           }            }
             
           for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)             for(i=1; i<= (nlstate)*(nlstate+ndeath); i++) 
             gradg[theta][i]=(gp[i]-gm[i])/2./delti[theta];                gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];  
         }          }
   
         for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)          for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
Line 2730  fprintf(fichtm," \n<ul><li><b>Graphs</b> Line 2826  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>
Line 2761  health expectancies in states (1) and (2 Line 2857  health expectancies in states (1) and (2
  - 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 */
  - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n  /*  - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
  - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n  /*  - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
         <br>",fileres,fileres,fileres,fileres);  /*      <br>",fileres,fileres,fileres,fileres); */
  else   /*  else  */
    fprintf(fichtm,"\n No population forecast: popforecast = %d (instead of 1) or stepm = %d (instead of 1) or model=%s (instead of .)<br><br></li>\n",popforecast, stepm, model);  /*    fprintf(fichtm,"\n No population forecast: popforecast = %d (instead of 1) or stepm = %d (instead of 1) or model=%s (instead of .)<br><br></li>\n",popforecast, stepm, model); */
 fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");  fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
   
  m=cptcoveff;   m=cptcoveff;
Line 2783  fprintf(fichtm," <ul><li><b>Graphs</b></ Line 2879  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 stationary 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);  
      }       }
Line 2884  m=pow(2,cptcoveff); Line 2980  m=pow(2,cptcoveff);
     }      }
   }    }
       
   /* CV preval stat */    /* CV preval stable (period) */
   for (k1=1; k1<= m ; k1 ++) {     for (k1=1; k1<= m ; k1 ++) { 
     for (cpt=1; cpt<nlstate ; cpt ++) {      for (cpt=1; cpt<=nlstate ; cpt ++) {
       k=3;        k=3;
       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 3020  prevforecast(char fileres[], double anpr Line 3116  prevforecast(char fileres[], double anpr
      dateprev1 dateprev2 range of dates during which prevalence is computed       dateprev1 dateprev2 range of dates during which prevalence is computed
      anproj2 year of en of projection (same day and month as proj1).       anproj2 year of en of projection (same day and month as proj1).
   */    */
   int yearp, stepsize, hstepm, nhstepm, j, k, c, cptcod, i, h;    int yearp, stepsize, hstepm, nhstepm, j, k, c, cptcod, i, h, i1;
   int *popage;    int *popage;
   double agec; /* generic age */    double agec; /* generic age */
   double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;    double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
Line 3053  prevforecast(char fileres[], double anpr Line 3149  prevforecast(char fileres[], double anpr
   
   stepsize=(int) (stepm+YEARM-1)/YEARM;    stepsize=(int) (stepm+YEARM-1)/YEARM;
   if (stepm<=12) stepsize=1;    if (stepm<=12) stepsize=1;
       if(estepm < stepm){
   hstepm=1;      printf ("Problem %d lower than %d\n",estepm, stepm);
     }
     else  hstepm=estepm;   
   
   hstepm=hstepm/stepm;     hstepm=hstepm/stepm; 
   yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp  and    yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp  and
                                fractional in yp1 */                                 fractional in yp1 */
Line 3065  prevforecast(char fileres[], double anpr Line 3164  prevforecast(char fileres[], double anpr
   jprojmean=yp;    jprojmean=yp;
   if(jprojmean==0) jprojmean=1;    if(jprojmean==0) jprojmean=1;
   if(mprojmean==0) jprojmean=1;    if(mprojmean==0) jprojmean=1;
   
     i1=cptcoveff;
     if (cptcovn < 1){i1=1;}
       
   fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2);     fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2); 
       
   fprintf(ficresf,"#****** Routine prevforecast **\n");    fprintf(ficresf,"#****** Routine prevforecast **\n");
   for(cptcov=1, k=0;cptcov<=cptcoveff;cptcov++){  
   /*            if (h==(int)(YEARM*yearp)){ */
     for(cptcov=1, k=0;cptcov<=i1;cptcov++){
     for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){      for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
       k=k+1;        k=k+1;
       fprintf(ficresf,"\n#******");        fprintf(ficresf,"\n#******");
Line 3083  prevforecast(char fileres[], double anpr Line 3187  prevforecast(char fileres[], double anpr
           fprintf(ficresf," p%d%d",i,j);            fprintf(ficresf," p%d%d",i,j);
         fprintf(ficresf," p.%d",j);          fprintf(ficresf," p.%d",j);
       }        }
       for (yearp=0; yearp<=(anproj2-anproj1);yearp++) {         for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { 
         fprintf(ficresf,"\n");          fprintf(ficresf,"\n");
         fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);             fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);   
   
         for (agec=fage; agec>=ageminpar; agec--){           for (agec=fage; agec>=(ageminpar-1); agec--){ 
           nhstepm=(int) rint((agelim-agec)*YEARM/stepm);             nhstepm=(int) rint((agelim-agec)*YEARM/stepm); 
           nhstepm = nhstepm/hstepm;             nhstepm = nhstepm/hstepm; 
           p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);            p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
Line 3095  prevforecast(char fileres[], double anpr Line 3199  prevforecast(char fileres[], double anpr
           hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k);              hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k);  
                   
           for (h=0; h<=nhstepm; h++){            for (h=0; h<=nhstepm; h++){
             if (h==(int) (YEARM*yearp)) {              if (h*hstepm/YEARM*stepm ==yearp) {
               fprintf(ficresf,"\n");                fprintf(ficresf,"\n");
               for(j=1;j<=cptcoveff;j++)                 for(j=1;j<=cptcoveff;j++) 
                 fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);                  fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
Line 3103  prevforecast(char fileres[], double anpr Line 3207  prevforecast(char fileres[], double anpr
             }               } 
             for(j=1; j<=nlstate+ndeath;j++) {              for(j=1; j<=nlstate+ndeath;j++) {
               ppij=0.;                ppij=0.;
               for(i=1; i<=nlstate;i++) {                              for(i=1; i<=nlstate;i++) {
                 if (mobilav==1)                   if (mobilav==1) 
                   ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec+1][i][cptcod];                    ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod];
                 else {                  else {
                   ppij=ppij+p3mat[i][j][h]*probs[(int)(agec+1)][i][cptcod];                    ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod];
                 }                  }
                 if (h==(int)(YEARM*yearp))                  if (h*hstepm/YEARM*stepm== yearp) {
                   fprintf(ficresf," %.3f", p3mat[i][j][h]);                    fprintf(ficresf," %.3f", p3mat[i][j][h]);
               }                  }
               if (h==(int)(YEARM*yearp)){                } /* end i */
                 if (h*hstepm/YEARM*stepm==yearp) {
                 fprintf(ficresf," %.3f", ppij);                  fprintf(ficresf," %.3f", ppij);
               }                }
             }              }/* end j */
           }            } /* end h */
           free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);            free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
         }          } /* end agec */
       }        } /* end yearp */
     }      } /* end cptcod */
   }    } /* end  cptcov */
                 
   if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);    if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
   
Line 3295  populforecast(char fileres[], double anp Line 3400  populforecast(char fileres[], double anp
 int main(int argc, char *argv[])  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,cptcode, cptcod;    int i,j, k, n=MAXN,iter,m,size=100,cptcode, cptcod;
   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 3317  int main(int argc, char *argv[]) Line 3422  int main(int argc, char *argv[])
   int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */    int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
   int mobilav=0,popforecast=0;    int mobilav=0,popforecast=0;
   int hstepm, nhstepm;    int hstepm, nhstepm;
   double jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,jpyram, mpyram,anpyram,jpyram1, mpyram1,anpyram1;    double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
     double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
   
   double bage, fage, age, agelim, agebase;    double bage, fage, age, agelim, agebase;
   double ftolpl=FTOL;    double ftolpl=FTOL;
Line 3332  int main(int argc, char *argv[]) Line 3438  int main(int argc, char *argv[])
   double **varpl; /* Variances of prevalence limits by age */    double **varpl; /* Variances of prevalence limits by age */
   double *epj, vepp;    double *epj, vepp;
   double kk1, kk2;    double kk1, kk2;
   double dateprev1, dateprev2,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2;    double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
   
   char *alph[]={"a","a","b","c","d","e"}, str[4];    char *alph[]={"a","a","b","c","d","e"}, str[4];
   
Line 3479  int main(int argc, char *argv[]) Line 3585  int main(int argc, char *argv[])
   ungetc(c,ficpar);    ungetc(c,ficpar);
   
   delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);    delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
   delti=vector(1,npar); /* Scale of each paramater (output from hesscov) */    /* delti=vector(1,npar); *//* Scale of each paramater (output from hesscov) */
   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);
Line 3496  int main(int argc, char *argv[]) Line 3602  int main(int argc, char *argv[])
     }      }
   }    }
   delti=delti3[1][1];    delti=delti3[1][1];
   
   
     /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
       
   /* Reads comments: lines beginning with '#' */    /* Reads comments: lines beginning with '#' */
   while((c=getc(ficpar))=='#' && c!= EOF){    while((c=getc(ficpar))=='#' && c!= EOF){
Line 3615  int main(int argc, char *argv[]) Line 3724  int main(int argc, char *argv[])
      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("%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]));}*/
       
    for (i=1; i<=imx; i++)
     
      /*if ((s[3][i]==3) ||  (s[4][i]==3)) weight[i]=0.08;
        else weight[i]=1;*/
   
   /* Calculation of the number of parameter from char model*/    /* Calculation of the number of parameter from char model*/
   Tvar=ivector(1,15); /* stores the number n of the covariates in Vm+Vn at 1 and m at 2 */    Tvar=ivector(1,15); /* stores the number n of the covariates in Vm+Vn at 1 and m at 2 */
   Tprod=ivector(1,15);     Tprod=ivector(1,15); 
Line 3707  int main(int argc, char *argv[]) Line 3820  int main(int argc, char *argv[])
   
   for (i=1; i<=imx; i++) {    for (i=1; i<=imx; i++) {
     for(m=2; (m<= maxwav); m++) {      for(m=2; (m<= maxwav); m++) {
       if ((mint[m][i]== 99) && (s[m][i] <= nlstate)){        if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
         anint[m][i]=9999;          anint[m][i]=9999;
         s[m][i]=-1;          s[m][i]=-1;
       }        }
       if(moisdc[i]==99 && andc[i]==9999 & s[m][i]>nlstate) s[m][i]=-1;        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);
           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);
           s[m][i]=-1;
         }
         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]); 
           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]); 
           s[m][i]=-1;
         }
     }      }
   }    }
   
   for (i=1; i<=imx; i++)  {    for (i=1; i<=imx; i++)  {
     agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);      agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
     for(m=1; (m<= maxwav); m++){      for(m=firstpass; (m<= lastpass); m++){
       if(s[m][i] >0){        if(s[m][i] >0){
         if (s[m][i] >= nlstate+1) {          if (s[m][i] >= nlstate+1) {
           if(agedc[i]>0)            if(agedc[i]>0)
             if(moisdc[i]!=99 && andc[i]!=9999)              if((int)moisdc[i]!=99 && (int)andc[i]!=9999)
               agev[m][i]=agedc[i];                agev[m][i]=agedc[i];
           /*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 (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: %d 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: %d line:%d\n",num[i],i);
                 agev[m][i]=-1;                  agev[m][i]=-1;
Line 3736  int main(int argc, char *argv[]) Line 3858  int main(int argc, char *argv[])
                                  years but with the precision of a                                   years but with the precision of a
                                  month */                                   month */
           agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);            agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
           if(mint[m][i]==99 || anint[m][i]==9999)            if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
             agev[m][i]=1;              agev[m][i]=1;
           else if(agev[m][i] <agemin){             else if(agev[m][i] <agemin){ 
             agemin=agev[m][i];              agemin=agev[m][i];
Line 3760  int main(int argc, char *argv[]) Line 3882  int main(int argc, char *argv[])
           
   }    }
   for (i=1; i<=imx; i++)  {    for (i=1; i<=imx; i++)  {
     for(m=1; (m<= maxwav); m++){      for(m=firstpass; (m<=lastpass); m++){
       if (s[m][i] > (nlstate+ndeath)) {        if (s[m][i] > (nlstate+ndeath)) {
         printf("Error: on wave %d of individual %d status %d > (nlstate+ndeath)=(%d+%d)=%d\n",m,i,s[m][i],nlstate, ndeath, nlstate+ndeath);               printf("Error: on wave %d of individual %d status %d > (nlstate+ndeath)=(%d+%d)=%d\n",m,i,s[m][i],nlstate, ndeath, nlstate+ndeath);     
         fprintf(ficlog,"Error: on wave %d of individual %d status %d > (nlstate+ndeath)=(%d+%d)=%d\n",m,i,s[m][i],nlstate, ndeath, nlstate+ndeath);               fprintf(ficlog,"Error: on wave %d of individual %d status %d > (nlstate+ndeath)=(%d+%d)=%d\n",m,i,s[m][i],nlstate, ndeath, nlstate+ndeath);     
Line 3769  int main(int argc, char *argv[]) Line 3891  int main(int argc, char *argv[])
     }      }
   }    }
   
     /*for (i=1; i<=imx; i++){
     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("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, agemin, agemax);    printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, agemin, agemax);
   fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, agemin, agemax);     fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, agemin, agemax); 
   
Line 3982  int main(int argc, char *argv[]) Line 4111  int main(int argc, char *argv[])
   
   fscanf(ficpar,"prevforecast=%d starting-proj-date=%lf/%lf/%lf final-proj-date=%lf/%lf/%lf mobil_average=%d\n",&prevfcast,&jproj1,&mproj1,&anproj1,&jproj2,&mproj2,&anproj2,&mobilavproj);    fscanf(ficpar,"prevforecast=%d starting-proj-date=%lf/%lf/%lf final-proj-date=%lf/%lf/%lf mobil_average=%d\n",&prevfcast,&jproj1,&mproj1,&anproj1,&jproj2,&mproj2,&anproj2,&mobilavproj);
   fprintf(ficparo,"prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);    fprintf(ficparo,"prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
   printf("prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobilaverage=%d\n",&prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);    printf("prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
   fprintf(ficlog,"prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobilaverage=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);    fprintf(ficlog,"prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
   fprintf(ficres,"prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobilaverage=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);    fprintf(ficres,"prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
   /* day and month of proj2 are not used but only year anproj2.*/    /* day and month of proj2 are not used but only year anproj2.*/
   
   while((c=getc(ficpar))=='#' && c!= EOF){    while((c=getc(ficpar))=='#' && c!= EOF){
Line 3999  int main(int argc, char *argv[]) Line 4128  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,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);    freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);
   
   /*------------ gnuplot -------------*/    /*------------ gnuplot -------------*/
Line 4026  int main(int argc, char *argv[]) Line 4156  int main(int argc, char *argv[])
 Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>\n  Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>\n
 \n  \n
 Total number of observations=%d <br>\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  Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n
 <hr  size=\"2\" color=\"#EC5E5E\">  <hr  size=\"2\" color=\"#EC5E5E\">
  <ul><li><h4>Parameter files</h4>\n   <ul><li><h4>Parameter files</h4>\n
  - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\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   - 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,jmin,jmax,jmean,fileres,fileres,filelog,filelog,optionfilegnuplot,optionfilegnuplot);   - 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);     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);
     
Line 4158  Interval (in months) between two waves: Line 4289  Interval (in months) between two waves:
     }      }
   }    }
   
   varprob(optionfilefiname, matcov, p, delti, nlstate, (int) bage, (int) fage,k,Tvar,nbcode, ncodemax);    varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax);
   
   fclose(ficrespij);    fclose(ficrespij);
   
Line 4166  Interval (in months) between two waves: Line 4297  Interval (in months) between two waves:
   /*---------- Forecasting ------------------*/    /*---------- Forecasting ------------------*/
   /*if((stepm == 1) && (strcmp(model,".")==0)){*/    /*if((stepm == 1) && (strcmp(model,".")==0)){*/
   if(prevfcast==1){    if(prevfcast==1){
     if(stepm ==1){      /*    if(stepm ==1){*/
       prevforecast(fileres, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);        prevforecast(fileres, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);
       if (popforecast==1) populforecast(fileres, anpyram,mpyram,jpyram, agemin,agemax, dateprev1, dateprev2,mobilav, agedeb, fage, popforecast, popfile, anpyram1,p, i1);        /* (popforecast==1) populforecast(fileres, anpyram,mpyram,jpyram, agemin,agemax, dateprev1, dateprev2,mobilav, agedeb, fage, popforecast, popfile, anpyram1,p, i1);*/
     }   /*      }  */
     else{  /*      else{ */
       erreur=108;  /*        erreur=108; */
       printf("Warning %d!! You can only forecast the prevalences if the optimization\n  has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model);  /*        printf("Warning %d!! You can only forecast the prevalences if the optimization\n  has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model); */
       fprintf(ficlog,"Warning %d!! You can only forecast the prevalences if the optimization\n  has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model);  /*        fprintf(ficlog,"Warning %d!! You can only forecast the prevalences if the optimization\n  has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model); */
     }  /*      } */
   }    }
       
   
Line 4208  Interval (in months) between two waves: Line 4339  Interval (in months) between two waves:
   printf("Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);    printf("Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
   fprintf(ficlog,"Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);    fprintf(ficlog,"Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
   
   prevalence(ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);    /* 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);
     /*  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);
     */
   
   if (mobilav!=0) {    if (mobilav!=0) {
     mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);      mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
Line 4335  Interval (in months) between two waves: Line 4470  Interval (in months) between two waves:
       
   free_matrix(covar,0,NCOVMAX,1,n);    free_matrix(covar,0,NCOVMAX,1,n);
   free_matrix(matcov,1,npar,1,npar);    free_matrix(matcov,1,npar,1,npar);
   free_vector(delti,1,npar);    /*free_vector(delti,1,npar);*/
     free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel); 
   free_matrix(agev,1,maxwav,1,imx);    free_matrix(agev,1,maxwav,1,imx);
   free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);    free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
   if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);    if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
     free_ma3x(probs,1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
   
   free_ivector(ncodemax,1,8);    free_ivector(ncodemax,1,8);
   free_ivector(Tvar,1,15);    free_ivector(Tvar,1,15);
   free_ivector(Tprod,1,15);    free_ivector(Tprod,1,15);
Line 4346  Interval (in months) between two waves: Line 4484  Interval (in months) between two waves:
   free_ivector(Tage,1,15);    free_ivector(Tage,1,15);
   free_ivector(Tcode,1,100);    free_ivector(Tcode,1,100);
   
   fprintf(fichtm,"\n</body>");    /*  fclose(fichtm);*/
   fclose(fichtm);    /*  fclose(ficgp);*/ /* ALready done */
   fclose(ficgp);  
       
   
   if(erreur >0){    if(erreur >0){
Line 4377  Interval (in months) between two waves: Line 4514  Interval (in months) between two waves:
   strcpy(plotcmd,GNUPLOTPROGRAM);    strcpy(plotcmd,GNUPLOTPROGRAM);
   strcat(plotcmd," ");    strcat(plotcmd," ");
   strcat(plotcmd,optionfilegnuplot);    strcat(plotcmd,optionfilegnuplot);
   printf("Starting: %s\n",plotcmd);fflush(stdout);    printf("Starting graphs with: %s",plotcmd);fflush(stdout);
   system(plotcmd);    system(plotcmd);
     printf(" Wait...");
   
  /*#ifdef windows*/   /*#ifdef windows*/
   while (z[0] != 'q') {    while (z[0] != 'q') {

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


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