Diff for /imach/src/imach.c between versions 1.265 and 1.266

version 1.265, 2017/04/26 16:22:11 version 1.266, 2017/05/13 07:26:12
Line 1 Line 1
 /* $Id$  /* $Id$
   $State$    $State$
   $Log$    $Log$
     Revision 1.266  2017/05/13 07:26:12  brouard
     Summary: Version 0.99r13 (improvements and bugs fixed)
   
   Revision 1.265  2017/04/26 16:22:11  brouard    Revision 1.265  2017/04/26 16:22:11  brouard
   Summary: imach 0.99r13 Some bugs fixed    Summary: imach 0.99r13 Some bugs fixed
   
Line 2657  Earliest age to start was %d-%d=%d, ncvl Line 2660  Earliest age to start was %d-%d=%d, ncvl
   max=vector(1,nlstate);    max=vector(1,nlstate);
   meandiff=vector(1,nlstate);    meandiff=vector(1,nlstate);
   
         dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;    dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
         oldm=oldms; savm=savms;    oldm=oldms; savm=savms;
     
         /* Starting with matrix unity */    /* Starting with matrix unity */
         for (ii=1;ii<=nlstate+ndeath;ii++)    for (ii=1;ii<=nlstate+ndeath;ii++)
                 for (j=1;j<=nlstate+ndeath;j++){      for (j=1;j<=nlstate+ndeath;j++){
       oldm[ii][j]=(ii==j ? 1.0 : 0.0);        oldm[ii][j]=(ii==j ? 1.0 : 0.0);
     }      }
       
Line 2736  Earliest age to start was %d-%d=%d, ncvl Line 2739  Earliest age to start was %d-%d=%d, ncvl
     /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, ageminpar, agemaxpar, dnewm, doldm, dsavm,ij)); /\* Bug Valgrind *\/ */      /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, ageminpar, agemaxpar, dnewm, doldm, dsavm,ij)); /\* Bug Valgrind *\/ */
     /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij)); /\* Bug Valgrind *\/ */      /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij)); /\* Bug Valgrind *\/ */
     out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij)); /* Bug Valgrind */      out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij)); /* Bug Valgrind */
       /* if((int)age == 70){ */
       /*   printf(" Backward prevalim age=%d agefin=%d \n", (int) age, (int) agefin); */
       /*   for(i=1; i<=nlstate+ndeath; i++) { */
       /*  printf("%d newm= ",i); */
       /*  for(j=1;j<=nlstate+ndeath;j++) { */
       /*    printf("%f ",newm[i][j]); */
       /*  } */
       /*  printf("oldm * "); */
       /*  for(j=1;j<=nlstate+ndeath;j++) { */
       /*    printf("%f ",oldm[i][j]); */
       /*  } */
       /*  printf(" pmmij "); */
       /*  for(j=1;j<=nlstate+ndeath;j++) { */
       /*    printf("%f ",pmmij[i][j]); */
       /*  } */
       /*  printf("\n"); */
       /*   } */
       /* } */
     savm=oldm;      savm=oldm;
     oldm=newm;      oldm=newm;
   
     for(j=1; j<=nlstate; j++){      for(j=1; j<=nlstate; j++){
       max[j]=0.;        max[j]=0.;
       min[j]=1.;        min[j]=1.;
Line 2788  Oldest age to start was %d-%d=%d, ncvloo Line 2810  Oldest age to start was %d-%d=%d, ncvloo
 double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )  double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
 {  {
   /* According to parameters values stored in x and the covariate's values stored in cov,    /* According to parameters values stored in x and the covariate's values stored in cov,
      computes the probability to be observed in state j being in state i by appying the       computes the probability to be observed in state j (after stepm years) being in state i by appying the
      model to the ncovmodel covariates (including constant and age).       model to the ncovmodel covariates (including constant and age).
      lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]       lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
      and, according on how parameters are entered, the position of the coefficient xij(nc) of the       and, according on how parameters are entered, the position of the coefficient xij(nc) of the
Line 2797  double **pmij(double **ps, double *cov, Line 2819  double **pmij(double **ps, double *cov,
      j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel       j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
      Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,       Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
      sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.       sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
      Outputs ps[i][j] the probability to be observed in j being in j according to       Outputs ps[i][j] or probability to be observed in j being in i according to
      the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]       the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
        Sum on j ps[i][j] should equal to 1.
   */    */
   double s1, lnpijopii;    double s1, lnpijopii;
   /*double t34;*/    /*double t34;*/
Line 2862  double **pmij(double **ps, double *cov, Line 2885  double **pmij(double **ps, double *cov,
   /*    /*
     for(i=1; i<= npar; i++) printf("%f ",x[i]);      for(i=1; i<= npar; i++) printf("%f ",x[i]);
                 goto end;*/                  goto end;*/
   return ps;    return ps; /* Pointer is unchanged since its call */
 }  }
   
 /*************** backward transition probabilities ***************/   /*************** backward transition probabilities ***************/ 
Line 2871  double **pmij(double **ps, double *cov, Line 2894  double **pmij(double **ps, double *cov,
 /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate,  double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */  /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate,  double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
  double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate,  double ***prevacurrent, int ij )   double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate,  double ***prevacurrent, int ij )
 {  {
   /* Computes the backward probability at age agefin and covariate ij    /* Computes the backward probability at age agefin and covariate combination ij. In fact cov is already filled and x too.
    * and returns in **ps as well as **bmij.     * Call to pmij(cov and x), call to cross prevalence, sums and inverses, left multiply, and returns in **ps as well as **bmij.
    */     */
   int i, ii, j,k;    int i, ii, j,k;
       
Line 2889  double **pmij(double **ps, double *cov, Line 2912  double **pmij(double **ps, double *cov,
       
   agefin=cov[2];    agefin=cov[2];
   /* bmij *//* age is cov[2], ij is included in cov, but we need for    /* bmij *//* age is cov[2], ij is included in cov, but we need for
      the observed prevalence (with this covariate ij) */       the observed prevalence (with this covariate ij) at beginning of transition */
   dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate);    /* dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
     pmmij=pmij(pmmij,cov,ncovmodel,x,nlstate); /*This is forward probability from agefin to agefin + stepm */
     /* outputs pmmij which is a stochastic matrix */
   /* We do have the matrix Px in savm  and we need pij */    /* We do have the matrix Px in savm  and we need pij */
   for (j=1;j<=nlstate+ndeath;j++){    for (j=1;j<=nlstate+ndeath;j++){
     sumnew=0.; /* w1 p11 + w2 p21 only on live states */      sumnew=0.; /* w1 p11 + w2 p21 only on live states N1./N..*N11/N1. + N2./N..*N21/N2.=(N11+N21)/N..=N.1/N.. */
     for (ii=1;ii<=nlstate;ii++){      for (ii=1;ii<=nlstate;ii++){
       sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij];        /* sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij]; */
         sumnew+=pmmij[ii][j]*prevacurrent[(int)agefin][ii][ij]; /* Yes prevalence at beginning of transition */
     } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */      } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
     for (ii=1;ii<=nlstate+ndeath;ii++){      if(sumnew >= 1.e-10){
       if(sumnew >= 1.e-10){        for (ii=1;ii<=nlstate+ndeath;ii++){
         /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */          /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
         /*      doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */          /*      doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
         /* }else if(agefin >= agemaxpar+stepm/YEARM){ */          /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
         /*      doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */          /*      doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
         /* }else */          /* }else */
         doldm[ii][j]=(ii==j ? 1./sumnew : 0.0);          doldm[ii][j]=(ii==j ? 1./sumnew : 0.0);
       }else{        } /*End ii */
         ;      }else{ /* We put the identity matrix */
         /* printf("ii=%d, i=%d, doldm=%lf dsavm=%lf, probs=%lf, sumnew=%lf,agefin=%d\n",ii,j,doldm[ii][j],dsavm[ii][j],prevacurrent[(int)agefin][ii][ij],sumnew, (int)agefin); */        for (ii=1;ii<=nlstate+ndeath;ii++){
       }          doldm[ii][j]=(ii==j ? 1. : 0.0);
     } /*End ii */        } /*End ii */
   } /* End j, At the end doldm is diag[1/(w_1p1i+w_2 p2i)] */        /* printf("Problem internal bmij A: sum_i w_i*p_ij=N.j/N.. <1.e-10 i=%d, j=%d, sumnew=%lf,agefin=%d\n",ii,j,sumnew, (int)agefin); */
   /* left Product of this diag matrix by dsavm=Px (newm=dsavm*doldm) */      }
   bbmij=matprod2(dnewm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, doldm); /* Bug Valgrind */    } /* End j, At the end doldm is diag[1/(w_1p1i+w_2 p2i)] or identity*/
     /* left Product of this diag matrix by dsavm=Px (dnewm=dsavm*doldm) */
     /* bbmij=matprod2(dnewm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, doldm); /\* Bug Valgrind *\/ */
     bbmij=matprod2(dnewm, pmmij,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, doldm); /* Bug Valgrind */
   /* dsavm=doldm; /\* dsavm is now diag [1/(w_1p1i+w_2 p2i)] but can be overwritten*\/ */    /* dsavm=doldm; /\* dsavm is now diag [1/(w_1p1i+w_2 p2i)] but can be overwritten*\/ */
   /* doldm=dnewm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */    /* doldm=dnewm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
   /* dnewm=dsavm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */    /* dnewm=dsavm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
   /* left Product of this matrix by diag matrix of prevalences (savm) */    /* left Product of this matrix by diag matrix of prevalences (savm) */
   for (j=1;j<=nlstate+ndeath;j++){    for (j=1;j<=nlstate+ndeath;j++){
       sumnew=0.;
     for (ii=1;ii<=nlstate+ndeath;ii++){      for (ii=1;ii<=nlstate+ndeath;ii++){
         sumnew+=prevacurrent[(int)agefin][ii][ij];
       dsavm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij] : 0.0);        dsavm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij] : 0.0);
     }      }
   } /* End j, At the end oldm is diag[1/(w_1p1i+w_2 p2i)] */      /* if(sumnew <0.9){ */
   ps=matprod2(doldm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dnewm); /* Bug Valgrind */      /*   printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */
       /* } */
     } /* End j, At the end dsavm is diag[(w_i)] */
     /* What if dsavm doesn't sum ii to 1? */
     /* ps=matprod2(doldm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dnewm); /\* Bug Valgrind *\/ */
     ps=matprod2(ps, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dnewm); /* Bug Valgrind */
   /* newm or out is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */    /* newm or out is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
   /* end bmij */    /* end bmij */
   return ps;     return ps; /*pointer is unchanged */
 }  }
 /*************** transition probabilities ***************/   /*************** transition probabilities ***************/ 
   
Line 3151  double ***hpxij(double ***po, int nhstep Line 3187  double ***hpxij(double ***po, int nhstep
 /* double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, double **oldm, double **savm, double **dnewm, double **doldm, double **dsavm, int ij ) */  /* double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, double **oldm, double **savm, double **dnewm, double **doldm, double **dsavm, int ij ) */
 double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij )  double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij )
 {  {
   /* Computes the transition matrix starting at age 'age' over    /* For a combination of dummy covariate ij, computes the transition matrix starting at age 'age' over
      'nhstepm*hstepm*stepm' months (i.e. until       'nhstepm*hstepm*stepm' months (i.e. until
      age (in years)  age+nhstepm*hstepm*stepm/12) by multiplying       age (in years)  age+nhstepm*hstepm*stepm/12) by multiplying
      nhstepm*hstepm matrices.       nhstepm*hstepm matrices.
Line 3159  double ***hbxij(double ***po, int nhstep Line 3195  double ***hbxij(double ***po, int nhstep
      (typically every 2 years instead of every month which is too big       (typically every 2 years instead of every month which is too big
      for the memory).       for the memory).
      Model is determined by parameters x and covariates have to be       Model is determined by parameters x and covariates have to be
      included manually here.       included manually here. Then we use a call to bmij(x and cov)
        The addresss of po (p3mat allocated to the dimension of nhstepm) should be stored for output
   */    */
   
   int i, j, d, h, k;    int i, j, d, h, k;
   double **out, cov[NCOVMAX+1];    double **out, cov[NCOVMAX+1], **bmij();
   double **newm;    double **newm, ***newmm;
   double agexact;    double agexact;
   double agebegin, ageend;    double agebegin, ageend;
   double **oldm, **savm;    double **oldm, **savm;
   
   oldm=oldms;savm=savms;    newmm=po; /* To be saved */
     oldm=oldms;savm=savms; /* Global pointers */
   /* Hstepm could be zero and should return the unit matrix */    /* Hstepm could be zero and should return the unit matrix */
   for (i=1;i<=nlstate+ndeath;i++)    for (i=1;i<=nlstate+ndeath;i++)
     for (j=1;j<=nlstate+ndeath;j++){      for (j=1;j<=nlstate+ndeath;j++){
Line 3183  double ***hbxij(double ***po, int nhstep Line 3220  double ***hbxij(double ***po, int nhstep
       newm=savm;        newm=savm;
       /* Covariates have to be included here again */        /* Covariates have to be included here again */
       cov[1]=1.;        cov[1]=1.;
       agexact=age-((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */        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 *\/ */        /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
       cov[2]=agexact;        cov[2]=agexact;
       if(nagesqr==1)        if(nagesqr==1)
         cov[3]= agexact*agexact;          cov[3]= agexact*agexact;
       for (k=1; k<=cptcovn;k++)        for (k=1; k<=cptcovn;k++){
         cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];        /*        cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
       /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */        /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\/ */
           cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
           /* printf("hbxij Dummy agexact=%.0f combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov[%d]=%lf codtabm(%d,Tvar[%d])=%d \n",agexact,ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,k)],2+nagesqr+TvarsDind[k],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,k)); */
   
         }
       for (k=1; k<=cptcovage;k++) /* Should start at cptcovn+1 */        for (k=1; k<=cptcovage;k++) /* Should start at cptcovn+1 */
         /* cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */          /* cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
         cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];          cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
Line 3199  double ***hbxij(double ***po, int nhstep Line 3240  double ***hbxij(double ***po, int nhstep
         cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];          cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
       /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */        /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
                                                   
                           
       /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/        /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
       /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/        /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
       /* Careful transposed matrix */        /* Careful transposed matrix */
       /* age is in cov[2] */        /* age is in cov[2], prevacurrent at beginning of transition. */
       /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */        /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
       /*                                                 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */        /*                                                 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
       out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\        out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
Line 4947  void prevalence(double ***probs, double Line 4987  void prevalence(double ***probs, double
           } else{            } else{
             if(first==1){              if(first==1){
               first=0;                first=0;
               printf("Warning Observed prevalence probs[%d][%d][%d]=%lf because of lack of cases\nSee others in log file...\n",jk,i,j1,probs[i][jk][j1]);                printf("Warning Observed prevalence doesn't sum to 1 for state %d: probs[%d][%d][%d]=%lf because of lack of cases\nSee others in log file...\n",jk,i,jk, j1,probs[i][jk][j1]);
                 fprintf(ficlog,"Warning Observed prevalence doesn't sum to 1 for state %d: probs[%d][%d][%d]=%lf because of lack of cases\nSee others in log file...\n",jk,i,jk, j1,probs[i][jk][j1]);
               }else{
                 fprintf(ficlog,"Warning Observed prevalence doesn't sum to 1 for state %d: probs[%d][%d][%d]=%lf because of lack of cases\nSee others in log file...\n",jk,i,jk, j1,probs[i][jk][j1]);
             }              }
           }            }
         }           } 
Line 6185  void varprob(char optionfilefiname[], do Line 6228  void varprob(char optionfilefiname[], do
    fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");     fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
    fprintf(fichtm,"\n");     fprintf(fichtm,"\n");
   
    fprintf(fichtm,"\n<li><h4> <a href=\"%s\">Matrix of variance-covariance of one-step probabilities (drawings)</a></h4> this page is important in order to visualize confidence intervals and especially correlation between disability and recovery, or more generally, way in and way back.</li>\n",optionfilehtmcov);     fprintf(fichtm,"\n<li><h4> <a href=\"%s\">Matrix of variance-covariance of one-step probabilities (drawings)</a></h4> this page is important in order to visualize confidence intervals and especially correlation between disability and recovery, or more generally, way in and way back. %s</li>\n",optionfilehtmcov,optionfilehtmcov);
    fprintf(fichtmcov,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Matrix of variance-covariance of pairs of step probabilities</h4>\n",optionfilehtmcov, optionfilehtmcov);     fprintf(fichtmcov,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Matrix of variance-covariance of pairs of step probabilities</h4>\n",optionfilehtmcov, optionfilehtmcov);
    fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \     fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
 and drawn. It helps understanding how is the covariance between two incidences.\  and drawn. It helps understanding how is the covariance between two incidences.\
Line 6402  To be simple, these graphs help to under Line 6445  To be simple, these graphs help to under
                    fprintf(ficgp,"\nset parametric;unset label");                     fprintf(ficgp,"\nset parametric;unset label");
                    fprintf(ficgp,"\nset log y;set log x; set xlabel \"p%1d%1d (year-1)\";set ylabel \"p%1d%1d (year-1)\"",k1,l1,k2,l2);                     fprintf(ficgp,"\nset log y;set log x; set xlabel \"p%1d%1d (year-1)\";set ylabel \"p%1d%1d (year-1)\"",k1,l1,k2,l2);
                    fprintf(ficgp,"\nset ter svg size 640, 480");                     fprintf(ficgp,"\nset ter svg size 640, 480");
                    fprintf(fichtmcov,"\n<br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\                     fprintf(fichtmcov,"\n<p><br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
  :<a href=\"%s_%d%1d%1d-%1d%1d.svg\">                                                                                                                                           \   :<a href=\"%s_%d%1d%1d-%1d%1d.svg\">                                                                                                                                           \
 %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\  %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
                            subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2,      \                             subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2,      \
Line 6413  To be simple, these graphs help to under Line 6456  To be simple, these graphs help to under
                    fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);                     fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
                    fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);                     fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
                    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)) not",      \                     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)) not",      \
                            mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),                                                                         \                             mu1,std,v11,sqrt(lc1),v12,sqrt(fabs(lc2)),   \
                            mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));                             mu2,std,v21,sqrt(lc1),v22,sqrt(fabs(lc2))); /* For gnuplot only */
                  }else{                   }else{
                    first=0;                     first=0;
                    fprintf(fichtmcov," %d (%.3f),",(int) age, c12);                     fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
                    fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);                     fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
                    fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);                     fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
                    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)) not", \                     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)) not", \
                            mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),                                 \                             mu1,std,v11,sqrt(lc1),v12,sqrt(fabs(lc2)),   \
                            mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));                             mu2,std,v21,sqrt(lc1),v22,sqrt(fabs(lc2)));
                  }/* if first */                   }/* if first */
                } /* age mod 5 */                 } /* age mod 5 */
              } /* end loop age */               } /* end loop age */
Line 6713  true period expectancies (those weighted Line 6756  true period expectancies (those weighted
 }  }
   
 /******************* Gnuplot file **************/  /******************* Gnuplot file **************/
 void printinggnuplot(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , int prevfcast, int backcast, char pathc[], double p[]){      void printinggnuplot(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , int prevfcast, int backcast, char pathc[], double p[], int offyear){
   
   char dirfileres[132],optfileres[132];    char dirfileres[132],optfileres[132];
   char gplotcondition[132], gplotlabel[132];    char gplotcondition[132], gplotlabel[132];
Line 6723  void printinggnuplot(char fileresu[], ch Line 6766  void printinggnuplot(char fileresu[], ch
   int vpopbased;    int vpopbased;
   int ioffset; /* variable offset for columns */    int ioffset; /* variable offset for columns */
   int nres=0; /* Index of resultline */    int nres=0; /* Index of resultline */
     int istart=1; /* For starting graphs in projections */
   
 /*   if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */  /*   if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
 /*     printf("Problem with file %s",optionfilegnuplot); */  /*     printf("Problem with file %s",optionfilegnuplot); */
Line 7253  set ter svg size 640, 480\nunset log y\n Line 7297  set ter svg size 640, 480\nunset log y\n
         fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);          fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
         fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\          fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
 set ter svg size 640, 480\nunset log y\nplot [%.f:%.f]  ", ageminpar, agemaxpar);  set ter svg size 640, 480\nunset log y\nplot [%.f:%.f]  ", ageminpar, agemaxpar);
         for (i=1; i<= nlstate+1 ; i ++){  /* nlstate +1 p11 p21 p.1 */  
           /* for (i=1; i<= nlstate+1 ; i ++){  /\* nlstate +1 p11 p21 p.1 *\/ */
           istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
           /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
           for (i=istart; i<= nlstate+1 ; i ++){  /* nlstate +1 p11 p21 p.1 */
           /*#  V1  = 1  V2 =  0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/            /*#  V1  = 1  V2 =  0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
           /*#   1    2   3    4    5      6  7   8   9   10   11 12  13   14  15 */               /*#   1    2   3    4    5      6  7   8   9   10   11 12  13   14  15 */   
           /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/            /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
           /*#   1       2   3    4    5      6  7   8   9   10   11 12  13   14  15 */               /*#   1       2   3    4    5      6  7   8   9   10   11 12  13   14  15 */   
           if(i==1){            if(i==istart){
             fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));              fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
           }else{            }else{
             fprintf(ficgp,",\\\n '' ");              fprintf(ficgp,",\\\n '' ");
Line 7270  set ter svg size 640, 480\nunset log y\n Line 7318  set ter svg size 640, 480\nunset log y\n
             /*# V1  = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/              /*# V1  = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
             /*#  1    2        3   4   5  6    7  8   9   10  11  12  13  14  15  16  17  18 */              /*#  1    2        3   4   5  6    7  8   9   10  11  12  13  14  15  16  17  18 */
             fprintf(ficgp," u %d:(", ioffset);               fprintf(ficgp," u %d:(", ioffset); 
             if(i==nlstate+1)              if(i==nlstate+1){
               fprintf(ficgp," $%d/(1.-$%d)) t 'pw.%d' with line ",      \                fprintf(ficgp," $%d/(1.-$%d)):5 t 'pw.%d' with line lc variable ",        \
                         ioffset+(cpt-1)*(nlstate+1)+1+(i-1),  ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
                 fprintf(ficgp,",\\\n '' ");
                 fprintf(ficgp," u %d:(",ioffset); 
                 fprintf(ficgp," (($5-$6) == %d ) ? $%d/(1.-$%d) : 1/0):5 with labels center not ", \
                        offyear,                           \
                       ioffset+(cpt-1)*(nlstate+1)+1+(i-1),  ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );                        ioffset+(cpt-1)*(nlstate+1)+1+(i-1),  ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
             else              }else
               fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ",      \                fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ",      \
                       ioffset+(cpt-1)*(nlstate+1)+1+(i-1),  ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );                        ioffset+(cpt-1)*(nlstate+1)+1+(i-1),  ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
           }else{ /* more than 2 covariates */            }else{ /* more than 2 covariates */
Line 7305  set ter svg size 640, 480\nunset log y\n Line 7358  set ter svg size 640, 480\nunset log y\n
             /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */               /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */ 
             /* ''  u 6:(($1==1 && $2==0 && $3==2 && $4==0)? $9/(1.-$15) : 1/0):($5==2000? 3:2) t 'p.1' with line lc variable*/              /* ''  u 6:(($1==1 && $2==0 && $3==2 && $4==0)? $9/(1.-$15) : 1/0):($5==2000? 3:2) t 'p.1' with line lc variable*/
             if(i==nlstate+1){              if(i==nlstate+1){
               fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p.%d' with line ", gplotcondition, \                fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0):5 t 'p.%d' with line lc variable", gplotcondition, \
                         ioffset+(cpt-1)*(nlstate+1)+1+(i-1),  ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
                 fprintf(ficgp,",\\\n '' ");
                 fprintf(ficgp," u %d:(",ioffset); 
                 fprintf(ficgp,"%s && (($5-$6) == %d ) ? $%d/(1.-$%d) : 1/0):5 with labels center not ", gplotcondition, \
                        offyear,                           \
                       ioffset+(cpt-1)*(nlstate+1)+1+(i-1),  ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );                        ioffset+(cpt-1)*(nlstate+1)+1+(i-1),  ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
   /*  '' u 6:(($1==1 && $2==0  && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
             }else{              }else{
               fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \                fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
                       ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );                        ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
Line 7526  set ter svg size 640, 480\nunset log y\n Line 7585  set ter svg size 640, 480\nunset log y\n
    int mobilavrange, mob;     int mobilavrange, mob;
    int iage=0;     int iage=0;
   
    double sum=0.;     double sum=0., sumr=0.;
    double age;     double age;
    double *sumnewp, *sumnewm;     double *sumnewp, *sumnewm, *sumnewmr;
    double *agemingood, *agemaxgood; /* Currently identical for all covariates */     double *agemingood, *agemaxgood; 
      double *agemingoodr, *agemaxgoodr; 
       
       
    /* modcovmax=2*cptcoveff;/\* Max number of modalities. We suppose  */     /* modcovmax=2*cptcoveff;/\* Max number of modalities. We suppose  */
Line 7537  set ter svg size 640, 480\nunset log y\n Line 7597  set ter svg size 640, 480\nunset log y\n
   
    sumnewp = vector(1,ncovcombmax);     sumnewp = vector(1,ncovcombmax);
    sumnewm = vector(1,ncovcombmax);     sumnewm = vector(1,ncovcombmax);
      sumnewmr = vector(1,ncovcombmax);
    agemingood = vector(1,ncovcombmax);       agemingood = vector(1,ncovcombmax);  
      agemingoodr = vector(1,ncovcombmax); 
    agemaxgood = vector(1,ncovcombmax);     agemaxgood = vector(1,ncovcombmax);
      agemaxgoodr = vector(1,ncovcombmax);
   
    for (cptcod=1;cptcod<=ncovcombmax;cptcod++){     for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
      sumnewm[cptcod]=0.;       sumnewm[cptcod]=0.; sumnewmr[cptcod]=0.;
      sumnewp[cptcod]=0.;       sumnewp[cptcod]=0.;
      agemingood[cptcod]=0;       agemingood[cptcod]=0, agemingoodr[cptcod]=0;
      agemaxgood[cptcod]=0;       agemaxgood[cptcod]=0, agemaxgoodr[cptcod]=0;
    }     }
    if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */     if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
       
    if(mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){     if(mobilav==-1 || mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
      if(mobilav==1) mobilavrange=5; /* default */       if(mobilav==1 || mobilav==-1) mobilavrange=5; /* default */
      else mobilavrange=mobilav;       else mobilavrange=mobilav;
      for (age=bage; age<=fage; age++)       for (age=bage; age<=fage; age++)
        for (i=1; i<=nlstate;i++)         for (i=1; i<=nlstate;i++)
Line 7561  set ter svg size 640, 480\nunset log y\n Line 7624  set ter svg size 640, 480\nunset log y\n
      */        */ 
      for (mob=3;mob <=mobilavrange;mob=mob+2){       for (mob=3;mob <=mobilavrange;mob=mob+2){
        for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){         for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
          for (i=1; i<=nlstate;i++){           for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
            for (cptcod=1;cptcod<=ncovcombmax;cptcod++){             sumnewm[cptcod]=0.;
              for (i=1; i<=nlstate;i++){
              mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];               mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
              for (cpt=1;cpt<=(mob-1)/2;cpt++){               for (cpt=1;cpt<=(mob-1)/2;cpt++){
                mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];                 mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
                mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];                 mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
              }               }
              mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;               mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
            }               sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
          }             } /* end i */
              if(sumnewm[cptcod] >1.e-3) mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/sumnewm[cptcod]; /* Rescaling to sum one */
            } /* end cptcod */
        }/* end age */         }/* end age */
      }/* end mob */       }/* end mob */
    }else     }else{
        printf("Error internal in movingaverage, mobilav=%d.\n",mobilav);
      return -1;       return -1;
    for (cptcod=1;cptcod<=ncovcombmax;cptcod++){     }
   
      for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ /* for each combination */
      /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */       /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
      if(invalidvarcomb[cptcod]){       if(invalidvarcomb[cptcod]){
        printf("\nCombination (%d) ignored because no cases \n",cptcod);          printf("\nCombination (%d) ignored because no cases \n",cptcod); 
        continue;         continue;
      }       }
   
      agemingood[cptcod]=fage-(mob-1)/2;       for (age=fage-(mob-1)/2; age>=bage+(mob-1)/2; age--){ /*looking for the youngest and oldest good age */
      for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, finding the youngest wrong */         sumnewm[cptcod]=0.;
          sumnewmr[cptcod]=0.;
          for (i=1; i<=nlstate;i++){
            sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
            sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
          }
          if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
            agemingoodr[cptcod]=age;
          }
          if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
              agemingood[cptcod]=age;
          }
        } /* age */
        for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ /*looking for the youngest and oldest good age */
        sumnewm[cptcod]=0.;         sumnewm[cptcod]=0.;
          sumnewmr[cptcod]=0.;
        for (i=1; i<=nlstate;i++){         for (i=1; i<=nlstate;i++){
          sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];           sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
            sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
          }
          if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
            agemaxgoodr[cptcod]=age;
        }         }
        if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */         if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
          agemingood[cptcod]=age;           agemaxgood[cptcod]=age;
        }else{ /* bad */         }
          for (i=1; i<=nlstate;i++){       } /* age */
            mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];       /* Thus we have agemingood and agemaxgood as well as goodr for raw (preobs) */
          } /* i */       /* but they will change */
        for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, filling up to the youngest */
          sumnewm[cptcod]=0.;
          sumnewmr[cptcod]=0.;
          for (i=1; i<=nlstate;i++){
            sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
            sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
          }
          if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
            if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
              agemaxgoodr[cptcod]=age;  /* age min */
              for (i=1; i<=nlstate;i++)
                mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
            }else{ /* bad we change the value with the values of good ages */
              for (i=1; i<=nlstate;i++){
                mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgoodr[cptcod]][i][cptcod];
              } /* i */
            } /* end bad */
          }else{
            if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
              agemaxgood[cptcod]=age;
            }else{ /* bad we change the value with the values of good ages */
              for (i=1; i<=nlstate;i++){
                mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
              } /* i */
            } /* end bad */
          }/* end else */
          sum=0.;sumr=0.;
          for (i=1; i<=nlstate;i++){
            sum+=mobaverage[(int)age][i][cptcod];
            sumr+=probs[(int)age][i][cptcod];
          }
          if(fabs(sum - 1.) > 1.e-3) { /* bad */
            printf("Moving average A1: For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one (%f) at any descending age! age=%d, could you increase bage=%d\n",cptcod,sumr, (int)age, bage);
          } /* end bad */
          /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
          if(fabs(sumr - 1.) > 1.e-3) { /* bad */
            printf("Moving average A2: For this combination of covariate cptcod=%d, the raw prevalence doesn't sums to one (%f) even with smoothed values at young ages! age=%d, could you increase bage=%d\n",cptcod,sumr, (int)age, bage);
        } /* end bad */         } /* end bad */
      }/* age */       }/* age */
      sum=0.;  
      for (i=1; i<=nlstate;i++){       for (age=bage+(mob-1)/2; age<=fage; age++){/* From youngest, finding the oldest wrong */
        sum+=mobaverage[(int)agemingood[cptcod]][i][cptcod];  
      }  
      if(fabs(sum - 1.) > 1.e-3) { /* bad */  
        printf("For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one at any descending age!\n",cptcod);  
        /* for (i=1; i<=nlstate;i++){ */  
        /*   mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */  
        /* } /\* i *\/ */  
      } /* end bad */  
      /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */  
      /* From youngest, finding the oldest wrong */  
      agemaxgood[cptcod]=bage+(mob-1)/2;  
      for (age=bage+(mob-1)/2; age<=fage; age++){  
        sumnewm[cptcod]=0.;         sumnewm[cptcod]=0.;
          sumnewmr[cptcod]=0.;
        for (i=1; i<=nlstate;i++){         for (i=1; i<=nlstate;i++){
          sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];           sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
            sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
          } 
          if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
            if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good */
              agemingoodr[cptcod]=age;
              for (i=1; i<=nlstate;i++)
                mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
            }else{ /* bad we change the value with the values of good ages */
              for (i=1; i<=nlstate;i++){
                mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingoodr[cptcod]][i][cptcod];
              } /* i */
            } /* end bad */
          }else{
            if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
              agemingood[cptcod]=age;
            }else{ /* bad */
              for (i=1; i<=nlstate;i++){
                mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
              } /* i */
            } /* end bad */
          }/* end else */
          sum=0.;sumr=0.;
          for (i=1; i<=nlstate;i++){
            sum+=mobaverage[(int)age][i][cptcod];
            sumr+=mobaverage[(int)age][i][cptcod];
        }         }
        if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */         if(fabs(sum - 1.) > 1.e-3) { /* bad */
          agemaxgood[cptcod]=age;           printf("Moving average B1: For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one (%f) at any descending age! age=%d, could you decrease fage=%d?\n",cptcod, sum, (int) age, fage);
        }else{ /* bad */         } /* end bad */
          for (i=1; i<=nlstate;i++){         /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
            mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];         if(fabs(sumr - 1.) > 1.e-3) { /* bad */
          } /* i */           printf("Moving average B2: For this combination of covariate cptcod=%d, the raw prevalence doesn't sums to one (%f) even with smoothed values at young ages! age=%d, could you increase fage=%d\n",cptcod,sumr, (int)age, fage);
        } /* end bad */         } /* end bad */
      }/* age */       }/* age */
      sum=0.;  
      for (i=1; i<=nlstate;i++){  
        sum+=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];  
      }  
      if(fabs(sum - 1.) > 1.e-3) { /* bad */  
        printf("For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one at any ascending age!\n",cptcod);  
        /* for (i=1; i<=nlstate;i++){ */  
        /*   mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */  
        /* } /\* i *\/ */  
      } /* end bad */  
                                   
      for (age=bage; age<=fage; age++){       for (age=bage; age<=fage; age++){
        /* printf("%d %d ", cptcod, (int)age); */         /* printf("%d %d ", cptcod, (int)age); */
Line 7646  set ter svg size 640, 480\nunset log y\n Line 7775  set ter svg size 640, 480\nunset log y\n
      }       }
      /* printf("\n"); */       /* printf("\n"); */
      /* } */       /* } */
   
      /* brutal averaging */       /* brutal averaging */
      for (i=1; i<=nlstate;i++){       /* for (i=1; i<=nlstate;i++){ */
        for (age=1; age<=bage; age++){       /*   for (age=1; age<=bage; age++){ */
          mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];       /*          mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
          /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */       /*          /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
        }               /*   }      */
        for (age=fage; age<=AGESUP; age++){       /*   for (age=fage; age<=AGESUP; age++){ */
          mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];       /*          mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod]; */
          /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */       /*          /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
        }       /*   } */
      } /* end i status */       /* } /\* end i status *\/ */
      for (i=nlstate+1; i<=nlstate+ndeath;i++){       /* for (i=nlstate+1; i<=nlstate+ndeath;i++){ */
        for (age=1; age<=AGESUP; age++){       /*   for (age=1; age<=AGESUP; age++){ */
          /*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*/       /*          /\*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*\/ */
          mobaverage[(int)age][i][cptcod]=0.;       /*          mobaverage[(int)age][i][cptcod]=0.; */
        }       /*   } */
      }       /* } */
    }/* end cptcod */     }/* end cptcod */
    free_vector(sumnewm,1, ncovcombmax);     free_vector(agemaxgoodr,1, ncovcombmax);
    free_vector(sumnewp,1, ncovcombmax);  
    free_vector(agemaxgood,1, ncovcombmax);     free_vector(agemaxgood,1, ncovcombmax);
    free_vector(agemingood,1, ncovcombmax);     free_vector(agemingood,1, ncovcombmax);
      free_vector(agemingoodr,1, ncovcombmax);
      free_vector(sumnewmr,1, ncovcombmax);
      free_vector(sumnewm,1, ncovcombmax);
      free_vector(sumnewp,1, ncovcombmax);
    return 0;     return 0;
  }/* End movingaverage */   }/* End movingaverage */
     
Line 7774  set ter svg size 640, 480\nunset log y\n Line 7907  set ter svg size 640, 480\nunset log y\n
           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][i][k];                  ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][k];
               else {                  /* else { */ /* even if mobilav==-1 we use mobaverage */
                 ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];                /*        ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k]; */
               }                /* } */
               if (h*hstepm/YEARM*stepm== yearp) {                if (h*hstepm/YEARM*stepm== yearp) {
                 fprintf(ficresf," %.3f", p3mat[i][j][h]);                  fprintf(ficresf," %.3f", p3mat[i][j][h]);
               }                }
Line 7790  set ter svg size 640, 480\nunset log y\n Line 7923  set ter svg size 640, 480\nunset log y\n
         } /* end h */          } /* 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 agec */
         /* diffyear=(int) anproj1+yearp-ageminpar-1; */
         /*printf("Prevforecast %d+%d-%d=diffyear=%d\n",(int) anproj1, (int)yearp,(int)ageminpar,(int) anproj1-(int)ageminpar);*/
     } /* end yearp */      } /* end yearp */
   } /* end  k */    } /* end  k */
                   
Line 9771  int back_prevalence_limit(double *p, dou Line 9906  int back_prevalence_limit(double *p, dou
         }else{          }else{
           /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */            /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
           bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres);            bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres);
             /* printf("TOTOT\n"); */
             /* exit(1); */
         }          }
         fprintf(ficresplb,"%.0f ",age );          fprintf(ficresplb,"%.0f ",age );
         for(j=1;j<=cptcoveff;j++)          for(j=1;j<=cptcoveff;j++)
Line 9929  int hPijx(double *p, int bage, int fage) Line 10066  int hPijx(double *p, int bage, int fage)
                   
         /*        nhstepm=nhstepm*YEARM; aff par mois*/          /*        nhstepm=nhstepm*YEARM; aff par mois*/
                   
         p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);          p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); /* We can't have it at an upper level because of nhstepm */
           /* and memory limitations if stepm is small */
   
         /* oldm=oldms;savm=savms; */          /* oldm=oldms;savm=savms; */
         /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);   */          /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);   */
         hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k);          hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k);
Line 11492  Please run with mle=-1 to get a correct Line 11631  Please run with mle=-1 to get a correct
 This is probably because your parameter file doesn't \n  contain the exact number of lines (or columns) corresponding to your model line.\n\  This is probably because your parameter file doesn't \n  contain the exact number of lines (or columns) corresponding to your model line.\n\
 Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);  Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
     }else{      }else{
       printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p);        printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p, (int)anproj1-(int)ageminpar);
     }      }
     printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \      printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
                  model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,backcast, estepm, \                   model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,backcast, estepm, \
Line 11554  Please run with mle=-1 to get a correct Line 11693  Please run with mle=-1 to get a correct
           printf(" Error in movingaverage mobilav=%d\n",mobilav);            printf(" Error in movingaverage mobilav=%d\n",mobilav);
         }          }
       }        }
       /* /\* Prevalence for each covariates in probs[age][status][cov] *\/ */        /* else if(mobilavproj==-1){ /\* Forcing raw observed prevalences *\/ */
       /* prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */        /*        for(i=1;i<=AGESUP;i++) */
         /*          for(j=1;j<=nlstate;j++) */
         /*            for(k=1;k<=ncovcombmax;k++) */
         /*              mobaverages[i][j][k]=probs[i][j][k]; */
         /*        /\* /\\* Prevalence for each covariates in probs[age][status][cov] *\\/ *\/ */
         /*        /\* prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); *\/ */
         /* } */
       else if (mobilavproj !=0) {        else if (mobilavproj !=0) {
         printf("Movingaveraging projected observed prevalence\n");          printf("Movingaveraging projected observed prevalence\n");
         fprintf(ficlog,"Movingaveraging projected observed prevalence\n");          fprintf(ficlog,"Movingaveraging projected observed prevalence\n");

Removed from v.1.265  
changed lines
  Added in v.1.266


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