--- imach/src/imach.c	2017/05/24 05:45:29	1.270
+++ imach/src/imach.c	2017/06/27 11:06:02	1.273
@@ -1,6 +1,15 @@
-/* $Id: imach.c,v 1.270 2017/05/24 05:45:29 brouard Exp $
+/* $Id: imach.c,v 1.273 2017/06/27 11:06:02 brouard Exp $
   $State: Exp $
   $Log: imach.c,v $
+  Revision 1.273  2017/06/27 11:06:02  brouard
+  Summary: More documentation on projections
+
+  Revision 1.272  2017/06/27 10:22:40  brouard
+  Summary: Color of backprojection changed from 6 to 5(yellow)
+
+  Revision 1.271  2017/06/27 10:17:50  brouard
+  Summary: Some bug with rint
+
   Revision 1.270  2017/05/24 05:45:29  brouard
   *** empty log message ***
 
@@ -1013,12 +1022,12 @@ typedef struct {
 #define ODIRSEPARATOR '\\'
 #endif
 
-/* $Id: imach.c,v 1.270 2017/05/24 05:45:29 brouard Exp $ */
+/* $Id: imach.c,v 1.273 2017/06/27 11:06:02 brouard Exp $ */
 /* $State: Exp $ */
 #include "version.h"
 char version[]=__IMACH_VERSION__;
 char copyright[]="February 2016,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121), Intel Software 2015-2018";
-char fullversion[]="$Revision: 1.270 $ $Date: 2017/05/24 05:45:29 $"; 
+char fullversion[]="$Revision: 1.273 $ $Date: 2017/06/27 11:06:02 $"; 
 char strstart[80];
 char optionfilext[10], optionfilefiname[FILENAMELENGTH];
 int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings  */
@@ -3243,7 +3252,7 @@ double ***hbxij(double ***po, int nhstep
       newm=savm;
       /* Covariates have to be included here again */
       cov[1]=1.;
-      agexact=age-((h-1)*hstepm + (d))*stepm/YEARM; /* age just before transition, d or d-1? */
+      agexact=age-( (h-1)*hstepm + (d)  )*stepm/YEARM; /* age just before transition, d or d-1? */
       /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
       cov[2]=agexact;
       if(nagesqr==1)
@@ -6658,8 +6667,8 @@ void printinghtml(char fileresu[], char
 		  int lastpass, int stepm, int weightopt, char model[],\
 		  int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
 		  int popforecast, int mobilav, int prevfcast, int mobilavproj, int backcast, int estepm , \
-		  double jprev1, double mprev1,double anprev1, double dateprev1, \
-		  double jprev2, double mprev2,double anprev2, double dateprev2){
+		  double jprev1, double mprev1,double anprev1, double dateprev1, double dateproj1, double dateback1, \
+		  double jprev2, double mprev2,double anprev2, double dateprev2, double dateproj2, double dateback2){
   int jj1, k1, i1, cpt, k4, nres;
 
    fprintf(fichtm,"
- Result files (first order: no variance)\n \
@@ -6813,15 +6822,18 @@ divided by h: hPij
      if(prevfcast==1){
        /* Projection of prevalence up to period (stable) prevalence in each health state */
        for(cpt=1; cpt<=nlstate;cpt++){
-	 fprintf(fichtm,"
 \n- Projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d) up to period (stable) prevalence in state %d. Or probability to be in state %d being in an observed weighted state (from 1 to %d). %s_%d-%d-%d.svg
 \
- ", dateprev1, dateprev2, mobilavproj, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
+	 fprintf(fichtm," ", dateprev1, dateprev2, mobilavproj, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
+	 fprintf(fichtm,"
 \n- Projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), from year %.1f up to year %.1f tending to period (stable) prevalence in state %d. Or probability to be in state %d being in an observed weighted state (from 1 to %d). %s_%d-%d-%d.svg
 \
+ ", dateprev1, dateprev2, mobilavproj, dateproj1, dateproj2, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
        }
      }
      if(backcast==1){
       /* Back projection of prevalence up to stable (mixed) back-prevalence in each health state */
        for(cpt=1; cpt<=nlstate;cpt++){
-	 fprintf(fichtm," ", dateprev1, dateprev2, mobilavproj, dateproj1, dateproj2, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
        }
      }
      if(backcast==1){
       /* Back projection of prevalence up to stable (mixed) back-prevalence in each health state */
        for(cpt=1; cpt<=nlstate;cpt++){
-	 fprintf(fichtm,"
 \n- Back projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d) up to stable (mixed) back prevalence in state %d. Or probability to have been in an state %d, knowing that the person was in either state (1 or %d) with weights corresponding to observed prevalence  at different ages. %s_%d-%d-%d.svg
 \
- ", dateprev1, dateprev2, mobilavproj, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
+	 fprintf(fichtm," ", dateprev1, dateprev2, mobilavproj, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
+	 fprintf(fichtm,"
 \n- Back projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), \
+ from year %.1f up to year %.1f (probably close to stable [mixed] back prevalence in state %d (randomness in cross-sectional prevalence is not taken into \
+ account but can visually be appreciated). Or probability to have been in an state %d, knowing that the person was in either state (1 or %d) \
+with weights corresponding to observed prevalence at different ages. %s_%d-%d-%d.svg
 \
+ ", dateprev1, dateprev2, mobilavproj, dateback1, dateback2, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
        }
      }
 	 
@@ -7042,7 +7054,7 @@ void printinggnuplot(char fileresu[], ch
 	
 	fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" u 1:((",subdirf2(fileresu,"P_"));
         if(cptcoveff ==0){
-	  fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3",	 2+(cpt-1),  cpt );
+	  fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3",	 2+3*(cpt-1),  cpt );
 	}else{
 	  kl=0;
 	  for (k=1; k<=cptcoveff; k++){    /* For each combination of covariate  */
@@ -7100,17 +7112,17 @@ void printinggnuplot(char fileresu[], ch
 	      if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
 	      else        fprintf(ficgp," %%*lf (%%*lf)");
 	    }
-	    fprintf(ficgp,"\" t\"Backward (stable) prevalence\" w l lt 0,\"%s\" every :::%d::%d u 1:($2==%d ? $3+1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
+	    fprintf(ficgp,"\" t\"Backward (stable) prevalence\" w l lt 6 dt 3,\"%s\" every :::%d::%d u 1:($2==%d ? $3+1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
 	    for (i=1; i<= nlstate ; i ++) {
 	      if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
 	      else fprintf(ficgp," %%*lf (%%*lf)");
 	    } 
-	    fprintf(ficgp,"\" t\"95%% CI\" w l lt 1,\"%s\" every :::%d::%d u 1:($2==%d ? $3-1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres); 
+	    fprintf(ficgp,"\" t\"95%% CI\" w l lt 5,\"%s\" every :::%d::%d u 1:($2==%d ? $3-1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres); 
 	    for (i=1; i<= nlstate ; i ++) {
 	      if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
 	      else fprintf(ficgp," %%*lf (%%*lf)");
 	    } 
-	    fprintf(ficgp,"\" t\"\" w l lt 1");
+	    fprintf(ficgp,"\" t\"\" w l lt 5");
 	  } /* end if backprojcast */
 	} /* end if backcast */
 	fprintf(ficgp,"\nset out ;unset label;\n");
@@ -8349,11 +8361,12 @@ set ter svg size 640, 480\nunset log y\n
       /* for (agec=bage; agec<=agemax-1; agec++){  /\* testing *\/ */
       for (agec=bage; agec<=fage; agec++){  /* testing */
 	/* We compute bij at age agec over nhstepm, nhstepm decreases when agec increases because of agemax;*/
-	nhstepm=(int) rint((agec-agelim)*YEARM/stepm);
+	nhstepm=(int) (agec-agelim) *YEARM/stepm;/*	nhstepm=(int) rint((agec-agelim)*YEARM/stepm);*/
 	nhstepm = nhstepm/hstepm;
 	p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
 	oldm=oldms;savm=savms;
 	/* computes hbxij at age agec over 1 to nhstepm */
+	/* printf("####prevbackforecast debug  agec=%.2f nhstepm=%d\n",agec, nhstepm);fflush(stdout); */
 	hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
 	/* hpxij(p3mat,nhstepm,agec,hstepm,p,             nlstate,stepm,oldm,savm, k,nres); */
 	/* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
@@ -10627,8 +10640,9 @@ int main(int argc, char *argv[])
 
   double *epj, vepp;
 
-  double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
-  double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000;
+  double dateprev1, dateprev2;
+  double jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000, dateproj1=0, dateproj2=0;
+  double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000, dateback1=0, dateback2=0;
 
   double **ximort;
   char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
@@ -12019,6 +12033,9 @@ Please run with mle=-1 to get a correct
 	  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 mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
 	  /* day and month of proj2 are not used but only year anproj2.*/
+	  dateproj1=anproj1+(mproj1-1)/12.+(jproj1-1)/365.;
+	  dateproj2=anproj2+(mproj2-1)/12.+(jproj2-1)/365.;
+
 	}
 	break;
       case 12:
@@ -12034,6 +12051,8 @@ Please run with mle=-1 to get a correct
 	  fprintf(ficlog,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
 	  fprintf(ficres,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
 	  /* day and month of proj2 are not used but only year anproj2.*/
+	  dateback1=anback1+(mback1-1)/12.+(jback1-1)/365.;
+	  dateback2=anback2+(mback2-1)/12.+(jback2-1)/365.;
 	}
 	break;
       case 13:
@@ -12089,7 +12108,7 @@ Please run with mle=-1 to get a correct
     }
     printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
 		 model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,backcast, estepm, \
-		 jprev1,mprev1,anprev1,dateprev1,jprev2,mprev2,anprev2,dateprev2);
+		 jprev1,mprev1,anprev1,dateprev1, dateproj1, dateback1,jprev2,mprev2,anprev2,dateprev2,dateproj2, dateback2);
 		
     /*------------ free_vector  -------------*/
     /*  chdir(path); */ ", dateprev1, dateprev2, mobilavproj, dateback1, dateback2, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
        }
      }
 	 
@@ -7042,7 +7054,7 @@ void printinggnuplot(char fileresu[], ch
 	
 	fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" u 1:((",subdirf2(fileresu,"P_"));
         if(cptcoveff ==0){
-	  fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3",	 2+(cpt-1),  cpt );
+	  fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3",	 2+3*(cpt-1),  cpt );
 	}else{
 	  kl=0;
 	  for (k=1; k<=cptcoveff; k++){    /* For each combination of covariate  */
@@ -7100,17 +7112,17 @@ void printinggnuplot(char fileresu[], ch
 	      if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
 	      else        fprintf(ficgp," %%*lf (%%*lf)");
 	    }
-	    fprintf(ficgp,"\" t\"Backward (stable) prevalence\" w l lt 0,\"%s\" every :::%d::%d u 1:($2==%d ? $3+1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
+	    fprintf(ficgp,"\" t\"Backward (stable) prevalence\" w l lt 6 dt 3,\"%s\" every :::%d::%d u 1:($2==%d ? $3+1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
 	    for (i=1; i<= nlstate ; i ++) {
 	      if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
 	      else fprintf(ficgp," %%*lf (%%*lf)");
 	    } 
-	    fprintf(ficgp,"\" t\"95%% CI\" w l lt 1,\"%s\" every :::%d::%d u 1:($2==%d ? $3-1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres); 
+	    fprintf(ficgp,"\" t\"95%% CI\" w l lt 5,\"%s\" every :::%d::%d u 1:($2==%d ? $3-1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres); 
 	    for (i=1; i<= nlstate ; i ++) {
 	      if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
 	      else fprintf(ficgp," %%*lf (%%*lf)");
 	    } 
-	    fprintf(ficgp,"\" t\"\" w l lt 1");
+	    fprintf(ficgp,"\" t\"\" w l lt 5");
 	  } /* end if backprojcast */
 	} /* end if backcast */
 	fprintf(ficgp,"\nset out ;unset label;\n");
@@ -8349,11 +8361,12 @@ set ter svg size 640, 480\nunset log y\n
       /* for (agec=bage; agec<=agemax-1; agec++){  /\* testing *\/ */
       for (agec=bage; agec<=fage; agec++){  /* testing */
 	/* We compute bij at age agec over nhstepm, nhstepm decreases when agec increases because of agemax;*/
-	nhstepm=(int) rint((agec-agelim)*YEARM/stepm);
+	nhstepm=(int) (agec-agelim) *YEARM/stepm;/*	nhstepm=(int) rint((agec-agelim)*YEARM/stepm);*/
 	nhstepm = nhstepm/hstepm;
 	p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
 	oldm=oldms;savm=savms;
 	/* computes hbxij at age agec over 1 to nhstepm */
+	/* printf("####prevbackforecast debug  agec=%.2f nhstepm=%d\n",agec, nhstepm);fflush(stdout); */
 	hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
 	/* hpxij(p3mat,nhstepm,agec,hstepm,p,             nlstate,stepm,oldm,savm, k,nres); */
 	/* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
@@ -10627,8 +10640,9 @@ int main(int argc, char *argv[])
 
   double *epj, vepp;
 
-  double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
-  double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000;
+  double dateprev1, dateprev2;
+  double jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000, dateproj1=0, dateproj2=0;
+  double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000, dateback1=0, dateback2=0;
 
   double **ximort;
   char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
@@ -12019,6 +12033,9 @@ Please run with mle=-1 to get a correct
 	  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 mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
 	  /* day and month of proj2 are not used but only year anproj2.*/
+	  dateproj1=anproj1+(mproj1-1)/12.+(jproj1-1)/365.;
+	  dateproj2=anproj2+(mproj2-1)/12.+(jproj2-1)/365.;
+
 	}
 	break;
       case 12:
@@ -12034,6 +12051,8 @@ Please run with mle=-1 to get a correct
 	  fprintf(ficlog,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
 	  fprintf(ficres,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
 	  /* day and month of proj2 are not used but only year anproj2.*/
+	  dateback1=anback1+(mback1-1)/12.+(jback1-1)/365.;
+	  dateback2=anback2+(mback2-1)/12.+(jback2-1)/365.;
 	}
 	break;
       case 13:
@@ -12089,7 +12108,7 @@ Please run with mle=-1 to get a correct
     }
     printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
 		 model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,backcast, estepm, \
-		 jprev1,mprev1,anprev1,dateprev1,jprev2,mprev2,anprev2,dateprev2);
+		 jprev1,mprev1,anprev1,dateprev1, dateproj1, dateback1,jprev2,mprev2,anprev2,dateprev2,dateproj2, dateback2);
 		
     /*------------ free_vector  -------------*/
     /*  chdir(path); */