]> henry.ined.fr Git - .git/commitdiff
Many changes:
authorN. Brouard <brouard@ined.fr>
Mon, 10 Jun 2002 13:12:01 +0000 (13:12 +0000)
committerN. Brouard <brouard@ined.fr>
Mon, 10 Jun 2002 13:12:01 +0000 (13:12 +0000)
(a) We look at covariance between step probabilities by drawing
confidence interval ellipsoids of two step probabilities pij versus
pkl.
It is done by diagonalizing the inverse of the convariance matrix.

(b) In order to write to the gnuplot file or even to the html file
without using to much memory we are now reopening ficgp and fichtm
with an "append" option each time we need them into the main program.
It is better than keeping matrices in memory until the end.

src/imach.c

index d1c74555e36515da874fc8cac64dd290ffc47f16..810ca7346cbd973989f29941ba7d11ba35553a22 100644 (file)
 #define YEARM 12. /* Number of months per year */\r
 #define AGESUP 130\r
 #define AGEBASE 40\r
+#ifdef windows\r
+#define DIRSEPARATOR '\\'\r
+#else\r
+#define DIRSEPARATOR '/'\r
+#endif\r
 \r
-\r
+char version[80]="Imach version 0.8g, May 2002, INED-EUROREVES ";\r
 int erreur; /* Error number */\r
 int nvar;\r
 int cptcovn, cptcovage=0, cptcoveff=0,cptcov;\r
@@ -97,12 +102,24 @@ double **oldm, **newm, **savm; /* Working pointers to matrices */
 double **oldms, **newms, **savms; /* Fixed working pointers to matrices */\r
 FILE *fic,*ficpar, *ficparo,*ficres,  *ficrespl, *ficrespij, *ficrest,*ficresf,*ficrespop;\r
 FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;\r
+FILE *fichtm; /* Html File */\r
 FILE *ficreseij;\r
-  char filerese[FILENAMELENGTH];\r
- FILE  *ficresvij;\r
-  char fileresv[FILENAMELENGTH];\r
- FILE  *ficresvpl;\r
-  char fileresvpl[FILENAMELENGTH];\r
+char filerese[FILENAMELENGTH];\r
+FILE  *ficresvij;\r
+char fileresv[FILENAMELENGTH];\r
+FILE  *ficresvpl;\r
+char fileresvpl[FILENAMELENGTH];\r
+char title[MAXLINE];\r
+char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH],  filerespl[FILENAMELENGTH];\r
+char optionfilext[10], optionfilefiname[FILENAMELENGTH], plotcmd[FILENAMELENGTH];\r
+\r
+char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];\r
+\r
+char filerest[FILENAMELENGTH];\r
+char fileregp[FILENAMELENGTH];\r
+char popfile[FILENAMELENGTH];\r
+\r
+char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH];\r
 \r
 #define NR_END 1\r
 #define FREE_ARG char*\r
@@ -161,11 +178,7 @@ static     int split( char *path, char *dirc, char *name, char *ext, char *finame )
 \r
    l1 = strlen( path );                        /* length of path */\r
    if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );\r
-#ifdef windows\r
-   s = strrchr( path, '\\' );          /* find last / */\r
-#else\r
-   s = strrchr( path, '/' );           /* find last / */\r
-#endif\r
+   s = strrchr( path,  DIRSEPARATOR );         /* find last / */\r
    if ( s == NULL ) {                  /* no directory, so use current */\r
 #if    defined(__bsd__)                /* get current working directory */\r
       extern char      *getwd( );\r
@@ -1671,8 +1684,6 @@ void evsij(char fileres[], double ***eij, double x[], int nlstate, int stepm, in
            varhe[i][j][(int)age] += doldm[i][j]*hf*hf;\r
       }\r
     }\r
-\r
-     \r
     /* Computing expectancies */\r
     for(i=1; i<=nlstate;i++)\r
       for(j=1; j<=nlstate;j++)\r
@@ -1698,6 +1709,8 @@ void evsij(char fileres[], double ***eij, double x[], int nlstate, int stepm, in
     free_ma3x(trgradg,0,nhstepm,1,nlstate*2,1,npar);\r
     free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);\r
   }\r
+  printf("\n");\r
+\r
   free_vector(xp,1,npar);\r
   free_matrix(dnewm,1,nlstate*2,1,npar);\r
   free_matrix(doldm,1,nlstate*2,1,nlstate*2);\r
@@ -1924,20 +1937,27 @@ void varprevlim(char fileres[], double **varpl, double **matcov, double x[], dou
 }\r
 \r
 /************ Variance of one-step probabilities  ******************/\r
-void varprob(char fileres[], double **matcov, double x[], double delti[], int nlstate, double bage, double fage, int ij, int *Tvar, int **nbcode, int *ncodemax)\r
+void varprob(char optionfilefiname[], double **matcov, double x[], double delti[], int nlstate, double bage, double fage, int ij, int *Tvar, int **nbcode, int *ncodemax)\r
 {\r
-  int i, j, i1, k1, j1, z1;\r
+  int i, j,  i1, k1, l1;\r
+  int k2, l2, j1,  z1;\r
   int k=0,l, cptcode;\r
+  int first=1;\r
+  double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2;\r
   double **dnewm,**doldm;\r
   double *xp;\r
   double *gp, *gm;\r
   double **gradg, **trgradg;\r
+  double **mu;\r
   double age,agelim, cov[NCOVMAX];\r
+  double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */\r
   int theta;\r
   char fileresprob[FILENAMELENGTH];\r
   char fileresprobcov[FILENAMELENGTH];\r
   char fileresprobcor[FILENAMELENGTH];\r
 \r
+  double ***varpij;\r
+\r
   strcpy(fileresprob,"prob"); \r
   strcat(fileresprob,fileres);\r
   if((ficresprob=fopen(fileresprob,"w"))==NULL) {\r
@@ -1963,6 +1983,8 @@ void varprob(char fileres[], double **matcov, double x[], double delti[], int nl
   fprintf(ficresprobcov,"# Age");\r
   fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");\r
   fprintf(ficresprobcov,"# Age");\r
+\r
+\r
   for(i=1; i<=nlstate;i++)\r
     for(j=1; j<=(nlstate+ndeath);j++){\r
       fprintf(ficresprob," p%1d-%1d (SE)",i,j);\r
@@ -1973,9 +1995,28 @@ void varprob(char fileres[], double **matcov, double x[], double delti[], int nl
   fprintf(ficresprobcov,"\n");\r
   fprintf(ficresprobcor,"\n");\r
   xp=vector(1,npar);\r
-  dnewm=matrix(1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);\r
-  doldm=matrix(1,(nlstate+ndeath)*(nlstate+ndeath),1,(nlstate+ndeath)*(nlstate+ndeath));\r
-  \r
+  dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);\r
+  doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));\r
+  mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);\r
+  varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);\r
+  first=1;\r
+  if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) {\r
+    printf("Problem with gnuplot file: %s\n", optionfilegnuplot);\r
+    exit(0);\r
+  }\r
+  else{\r
+    fprintf(ficgp,"\n# Routine varprob");\r
+  }\r
+  if((fichtm=fopen(optionfilehtm,"a"))==NULL) {\r
+    printf("Problem with html file: %s\n", optionfilehtm);\r
+    exit(0);\r
+  }\r
+  else{\r
+    fprintf(fichtm,"\n<H2> Computing matrix of variance-covariance of step probabilities</h2>\n");\r
+    fprintf(fichtm,"\n<br> We 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");\r
+    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");\r
+\r
+  }\r
   cov[1]=1;\r
   j=cptcoveff;\r
   if (cptcovn<1) {j=1;ncodemax[1]=1;}\r
@@ -1987,13 +2028,19 @@ void varprob(char fileres[], double **matcov, double x[], double delti[], int nl
     if  (cptcovn>0) {\r
       fprintf(ficresprob, "\n#********** Variable "); \r
       fprintf(ficresprobcov, "\n#********** Variable "); \r
+      fprintf(ficgp, "\n#********** Variable "); \r
+      fprintf(fichtm, "\n<h4>********** Variable</h4>\n "); \r
       fprintf(ficresprobcor, "\n#********** Variable "); \r
       for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);\r
       fprintf(ficresprob, "**********\n#");\r
       for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);\r
       fprintf(ficresprobcov, "**********\n#");\r
       for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);\r
-      fprintf(ficresprobcor, "**********\n#");\r
+      fprintf(ficgp, "**********\n#");\r
+      for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, "# V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);\r
+      fprintf(ficgp, "**********\n#");\r
+      for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);\r
+      fprintf(fichtm, "**********\n#");\r
     }\r
     \r
       for (age=bage; age<=fage; age ++){ \r
@@ -2005,10 +2052,10 @@ void varprob(char fileres[], double **matcov, double x[], double delti[], int nl
        for (k=1; k<=cptcovprod;k++)\r
          cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];\r
        \r
-       gradg=matrix(1,npar,1,9);\r
-       trgradg=matrix(1,9,1,npar);\r
-       gp=vector(1,(nlstate+ndeath)*(nlstate+ndeath));\r
-       gm=vector(1,(nlstate+ndeath)*(nlstate+ndeath));\r
+       gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));\r
+       trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);\r
+       gp=vector(1,(nlstate)*(nlstate+ndeath));\r
+       gm=vector(1,(nlstate)*(nlstate+ndeath));\r
     \r
        for(theta=1; theta <=npar; theta++){\r
          for(i=1; i<=npar; i++)\r
@@ -2017,7 +2064,7 @@ void varprob(char fileres[], double **matcov, double x[], double delti[], int nl
          pmij(pmmij,cov,ncovmodel,xp,nlstate);\r
          \r
          k=0;\r
-         for(i=1; i<= (nlstate+ndeath); i++){\r
+         for(i=1; i<= (nlstate); i++){\r
            for(j=1; j<=(nlstate+ndeath);j++){\r
              k=k+1;\r
              gp[k]=pmmij[i][j];\r
@@ -2029,36 +2076,39 @@ void varprob(char fileres[], double **matcov, double x[], double delti[], int nl
     \r
          pmij(pmmij,cov,ncovmodel,xp,nlstate);\r
          k=0;\r
-         for(i=1; i<=(nlstate+ndeath); i++){\r
+         for(i=1; i<=(nlstate); i++){\r
            for(j=1; j<=(nlstate+ndeath);j++){\r
              k=k+1;\r
              gm[k]=pmmij[i][j];\r
            }\r
          }\r
      \r
-         for(i=1; i<= (nlstate+ndeath)*(nlstate+ndeath); i++) \r
+         for(i=1; i<= (nlstate)*(nlstate+ndeath); i++) \r
            gradg[theta][i]=(gp[i]-gm[i])/2./delti[theta];  \r
        }\r
 \r
-       for(j=1; j<=(nlstate+ndeath)*(nlstate+ndeath);j++)\r
+       for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)\r
          for(theta=1; theta <=npar; theta++)\r
            trgradg[j][theta]=gradg[theta][j];\r
        \r
-       matprod2(dnewm,trgradg,1,9,1,npar,1,npar,matcov);\r
-       matprod2(doldm,dnewm,1,9,1,npar,1,9,gradg);\r
+       matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov); \r
+       matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);\r
        \r
        pmij(pmmij,cov,ncovmodel,x,nlstate);\r
        \r
        k=0;\r
-       for(i=1; i<=(nlstate+ndeath); i++){\r
+       for(i=1; i<=(nlstate); i++){\r
          for(j=1; j<=(nlstate+ndeath);j++){\r
            k=k+1;\r
-           gm[k]=pmmij[i][j];\r
+           mu[k][(int) age]=pmmij[i][j];\r
          }\r
        }\r
-     \r
+       for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)\r
+         for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)\r
+           varpij[i][j][(int)age] = doldm[i][j];\r
+\r
        /*printf("\n%d ",(int)age);\r
-     for (i=1; i<=(nlstate+ndeath)*(nlstate+ndeath-1);i++){\r
+     for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){\r
        printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));\r
      }*/\r
 \r
@@ -2066,11 +2116,11 @@ void varprob(char fileres[], double **matcov, double x[], double delti[], int nl
        fprintf(ficresprobcov,"\n%d ",(int)age);\r
        fprintf(ficresprobcor,"\n%d ",(int)age);\r
 \r
-       for (i=1; i<=(nlstate+ndeath)*(nlstate+ndeath-1);i++)\r
-         fprintf(ficresprob,"%12.3e (%12.3e) ",gm[i],sqrt(doldm[i][j]));\r
+       for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)\r
+         fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));\r
        for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){\r
-         fprintf(ficresprobcov,"%12.3e ",gm[i]);\r
-         fprintf(ficresprobcor,"%12.3e ",gm[i]);\r
+         fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);\r
+         fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);\r
        }\r
        i=0;\r
        for (k=1; k<=(nlstate);k++){\r
@@ -2079,15 +2129,75 @@ void varprob(char fileres[], double **matcov, double x[], double delti[], int nl
            fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);\r
            fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);\r
            for (j=1; j<=i;j++){\r
-             fprintf(ficresprobcov," %12.3e",doldm[i][j]);\r
-             fprintf(ficresprobcor," %12.3e",doldm[i][j]/sqrt(doldm[i][i])/sqrt(doldm[j][j]));\r
+             fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);\r
+             fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));\r
            }\r
          }\r
-       }\r
-      }\r
-    }\r
+       }/* end of loop for state */\r
+      } /* end of loop for age */\r
+       /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/\r
+      for (k1=1; k1<=(nlstate);k1++){\r
+       for (l1=1; l1<=(nlstate+ndeath);l1++){ \r
+         if(l1==k1) continue;\r
+         i=(k1-1)*(nlstate+ndeath)+l1;\r
+         for (k2=1; k2<=(nlstate);k2++){\r
+           for (l2=1; l2<=(nlstate+ndeath);l2++){ \r
+             if(l2==k2) continue;\r
+             j=(k2-1)*(nlstate+ndeath)+l2;\r
+             if(j<=i) continue;\r
+             for (age=bage; age<=fage; age ++){ \r
+               if ((int)age %5==0){\r
+                 v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;\r
+                 v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;\r
+                 cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;\r
+                 mu1=mu[i][(int) age]/stepm*YEARM ;\r
+                 mu2=mu[j][(int) age]/stepm*YEARM;\r
+                 /* Computing eigen value of matrix of covariance */\r
+                 lc1=(v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12));\r
+                 lc2=(v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12));\r
+                 printf("Var %.4e %.4e cov %.4e Eigen %.3e %.3e\n",v1,v2,cv12,lc1,lc2);\r
+                 /* Eigen vectors */\r
+                 v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));\r
+                 v21=sqrt(1.-v11*v11);\r
+                 v12=-v21;\r
+                 v22=v11;\r
+                 /*printf(fignu*/\r
+                 /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */\r
+                 /* mu2+ v21*lc1*cost + v21*lc2*sin(t) */\r
+                 if(first==1){\r
+                   first=0;\r
+                   fprintf(ficgp,"\nset parametric;set nolabel");\r
+                   fprintf(ficgp,"\nset log y;set log x; set xlabel \"p%1d%1d (year-1)\";set ylabel \"p%1d%1d (year-1)\"",k2,l2,k1,l1);\r
+                   fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");\r
+                   fprintf(fichtm,"\n<br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup> :<a href=\"varpijgr%s%1d%1d-%1d%1d.png\">varpijgr%s%1d%1d-%1d%1d.png</A>, ",k2,l2,k1,l1,optionfilefiname,k2,l2,k1,l1,optionfilefiname,k2,l2,k1,l1);\r
+                   fprintf(fichtm,"\n<br><img src=\"varpijgr%s%1d%1d-%1d%1d.png\">, ",optionfilefiname,k2,l2,k1,l1);\r
+                   fprintf(ficgp,"\nset out \"varpijgr%s%1d%1d-%1d%1d.png\"",optionfilefiname,k2,l2,k1,l1);\r
+                   fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu2,mu1);\r
+                   fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k2,l2,k1,l1);\r
+                   fprintf(ficgp,"\nplot [-pi:pi] %11.3e+ %.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)), %11.3e +%.3f*(-%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)) t \"%d\"",\\r
+                           mu2,std,v21,sqrt(lc1),v21,sqrt(lc2), \\r
+                           mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),(int) age);\r
+                 }else{\r
+                   first=0;\r
+                   fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k2,l2,k1,l1);\r
+                   fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu2,mu1);\r
+                   fprintf(ficgp,"\nreplot %11.3e+ %.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)), %11.3e +%.3f*(-%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)) t \"%d\"",\\r
+                           mu2,std,v21,sqrt(lc1),v21,sqrt(lc2), \\r
+                           mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),(int) age);\r
+                 }/* if first */\r
+               } /* age mod 5 */\r
+             } /* end loop age */\r
+             fprintf(ficgp,"\nset out \"varpijgr%s%1d%1d-%1d%1d.png\";replot;",optionfilefiname,k2,l2,k1,l1);\r
+             first=1;\r
+           } /*l12 */\r
+         } /* k12 */\r
+       } /*l1 */\r
+      }/* k1 */\r
+    } /* loop covariates */\r
+    free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);\r
     free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));\r
     free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));\r
+    free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);\r
     free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);\r
     free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);\r
   }\r
@@ -2095,37 +2205,24 @@ void varprob(char fileres[], double **matcov, double x[], double delti[], int nl
   fclose(ficresprob);\r
   fclose(ficresprobcov);\r
   fclose(ficresprobcor);\r
+  fclose(ficgp);\r
+  fclose(fichtm);\r
 }\r
 \r
 \r
 /******************* Printing html file ***********/\r
 void printinghtml(char fileres[], char title[], char datafile[], int firstpass, \\r
                  int lastpass, int stepm, int weightopt, char model[],\\r
-                 int imx,int jmin, int jmax, double jmeanint,char optionfile[], \\r
-                 char optionfilehtm[],char rfileres[], char optionfilegnuplot[],\\r
-                 char version[], int popforecast, int estepm ,\\r
+                 int imx,int jmin, int jmax, double jmeanint,char rfileres[],\\r
+                 int popforecast, int estepm ,\\r
                  double jprev1, double mprev1,double anprev1, \\r
                  double jprev2, double mprev2,double anprev2){\r
   int jj1, k1, i1, cpt;\r
-  FILE *fichtm;\r
   /*char optionfilehtm[FILENAMELENGTH];*/\r
-\r
-  strcpy(optionfilehtm,optionfile);\r
-  strcat(optionfilehtm,".htm");\r
-  if((fichtm=fopen(optionfilehtm,"w"))==NULL)    {\r
+  if((fichtm=fopen(optionfilehtm,"a"))==NULL)    {\r
     printf("Problem with %s \n",optionfilehtm), exit(0);\r
   }\r
 \r
-  fprintf(fichtm,"<body> <font size=\"2\">%s </font> <hr size=\"2\" color=\"#EC5E5E\"> \n\r
-Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>\n\r
-\n\r
-Total number of observations=%d <br>\n\r
-Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n\r
-<hr  size=\"2\" color=\"#EC5E5E\">\r
- <ul><li>Parameter files<br>\n\r
- - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\r
- - Gnuplot file name: <a href=\"%s\">%s</a><br></ul>\n",version,title,datafile,firstpass,lastpass,stepm, weightopt,model,imx,jmin,jmax,jmean,fileres,fileres,optionfilegnuplot,optionfilegnuplot);\r
-\r
    fprintf(fichtm,"<ul><li>Result files (first order: no variance)<br>\n\r
  - 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\r
  - Estimated transition probabilities over %d (stepm) months: <a href=\"pij%s\">pij%s</a><br>\n\r
@@ -2187,20 +2284,17 @@ interval) in state (%d): v%s%d%d.png <br>
      fprintf(fichtm,"\n<br>- Total life expectancy by age and\r
 health expectancies in states (1) and (2): e%s%d.png<br>\r
 <img src=\"e%s%d.png\">",strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1);\r
-fprintf(fichtm,"\n</body>");\r
    }\r
  }\r
 fclose(fichtm);\r
 }\r
 \r
 /******************* Gnuplot file **************/\r
-void printinggnuplot(char fileres[],char optionfilefiname[],char optionfile[],char optionfilegnuplot[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){\r
+void printinggnuplot(char fileres[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){\r
 \r
   int m,cpt,k1,i,k,j,jk,k2,k3,ij,l;\r
   int ng;\r
-  strcpy(optionfilegnuplot,optionfilefiname);\r
-  strcat(optionfilegnuplot,".gp");\r
-  if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {\r
+  if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) {\r
     printf("Problem with file %s",optionfilegnuplot);\r
   }\r
 \r
@@ -2379,7 +2473,7 @@ fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
        }\r
      }\r
    }\r
-   fclose(ficgp);\r
+   fclose(ficgp); \r
 }  /* end gnuplot */\r
 \r
 \r
@@ -2679,15 +2773,6 @@ int main(int argc, char *argv[])
   double ***p3mat;\r
   int *indx;\r
   char line[MAXLINE], linepar[MAXLINE];\r
-  char title[MAXLINE];\r
-  char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH],  filerespl[FILENAMELENGTH];\r
-  char optionfilext[10], optionfilefiname[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilegnuplot[FILENAMELENGTH], plotcmd[FILENAMELENGTH];\r
-  \r
-  char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];\r
-\r
-  char filerest[FILENAMELENGTH];\r
-  char fileregp[FILENAMELENGTH];\r
-  char popfile[FILENAMELENGTH];\r
   char path[80],pathc[80],pathcd[80],pathtot[80],model[20];\r
   int firstobs=1, lastobs=10;\r
   int sdeb, sfin; /* Status at beginning and end */\r
@@ -2715,7 +2800,6 @@ int main(int argc, char *argv[])
   double dateprev1, dateprev2,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2;\r
   \r
 \r
-  char version[80]="Imach version 0.8g, May 2002, INED-EUROREVES ";\r
   char *alph[]={"a","a","b","c","d","e"}, str[4];\r
 \r
 \r
@@ -3310,7 +3394,33 @@ while((c=getc(ficpar))=='#' && c!= EOF){
  freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax,mint,anint,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);\r
 \r
 /*------------ gnuplot -------------*/\r
- printinggnuplot(fileres,optionfilefiname,optionfile,optionfilegnuplot, ageminpar,agemaxpar,fage, pathc,p);\r
+  strcpy(optionfilegnuplot,optionfilefiname);\r
+  strcat(optionfilegnuplot,".gp");\r
+  if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {\r
+    printf("Problem with file %s",optionfilegnuplot);\r
+  }\r
+  fclose(ficgp);\r
+ printinggnuplot(fileres, ageminpar,agemaxpar,fage, pathc,p);\r
+/*--------- index.htm --------*/\r
+\r
+  strcpy(optionfilehtm,optionfile);\r
+  strcat(optionfilehtm,".htm");\r
+  if((fichtm=fopen(optionfilehtm,"w"))==NULL)    {\r
+    printf("Problem with %s \n",optionfilehtm), exit(0);\r
+  }\r
+\r
+  fprintf(fichtm,"<body> <font size=\"2\">%s </font> <hr size=\"2\" color=\"#EC5E5E\"> \n\r
+Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s<br>\n\r
+\n\r
+Total number of observations=%d <br>\n\r
+Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n\r
+<hr  size=\"2\" color=\"#EC5E5E\">\r
+ <ul><li>Parameter files<br>\n\r
+ - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\r
+ - Gnuplot file name: <a href=\"%s\">%s</a><br></ul>\n",version,title,datafile,firstpass,lastpass,stepm, weightopt,model,imx,jmin,jmax,jmean,fileres,fileres,optionfilegnuplot,optionfilegnuplot);\r
+  fclose(fichtm);\r
+\r
+ printinghtml(fileres,title,datafile, firstpass, lastpass, stepm, weightopt,model,imx,jmin,jmax,jmean,rfileres,popforecast,estepm,jprev1,mprev1,anprev1,jprev2,mprev2,anprev2);\r
  \r
 /*------------ free_vector  -------------*/\r
  chdir(path);\r
@@ -3324,11 +3434,7 @@ while((c=getc(ficpar))=='#' && c!= EOF){
  fclose(ficparo);\r
  fclose(ficres);\r
 \r
-/*--------- index.htm --------*/\r
 \r
- printinghtml(fileres,title,datafile, firstpass, lastpass, stepm, weightopt,model,imx,jmin,jmax,jmean,optionfile,optionfilehtm,rfileres,optionfilegnuplot,version,popforecast,estepm,jprev1,mprev1,anprev1,jprev2,mprev2,anprev2);\r
-\r
-  \r
   /*--------------- Prevalence limit --------------*/\r
   \r
   strcpy(filerespl,"pl");\r
@@ -3423,7 +3529,7 @@ while((c=getc(ficpar))=='#' && c!= EOF){
     }\r
   }\r
 \r
-  varprob(fileres, matcov, p, delti, nlstate, (int) bage, (int) fage,k,Tvar,nbcode, ncodemax);\r
+  varprob(optionfilefiname, matcov, p, delti, nlstate, (int) bage, (int) fage,k,Tvar,nbcode, ncodemax);\r
 \r
   fclose(ficrespij);\r
 \r
@@ -3579,6 +3685,11 @@ free_matrix(mint,1,maxwav,1,n);
   free_matrix(agev,1,maxwav,1,imx);\r
   free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);\r
 \r
+  fprintf(fichtm,"\n</body>");\r
+  fclose(fichtm);\r
+  fclose(ficgp);\r
+  \r
+\r
   if(erreur >0)\r
     printf("End of Imach with error or warning %d\n",erreur);\r
   else   printf("End of Imach\n");\r