Diff for /imach/src/imach.c between versions 1.335 and 1.336

version 1.335, 2022/08/31 08:23:16 version 1.336, 2022/08/31 09:52:36
Line 1 Line 1
 /* $Id$  /* $Id$
   $State$    $State$
   $Log$    $Log$
     Revision 1.336  2022/08/31 09:52:36  brouard
     *** empty log message ***
   
   Revision 1.335  2022/08/31 08:23:16  brouard    Revision 1.335  2022/08/31 08:23:16  brouard
   Summary: improvements...    Summary: improvements...
   
Line 3508  double **matprod2(double **out, double * Line 3511  double **matprod2(double **out, double *
   
 double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij, int nres )  double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij, int nres )
 {  {
   /* Computes the transition matrix starting at age 'age' and dummies values in each resultline (loop on ij to find the corresponding combination) to over     /* Already optimized with precov.
        Computes the transition matrix starting at age 'age' and dummies values in each resultline (loop on ij to find the corresponding combination) to 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 3839  double ***hbxij(double ***po, int nhstep Line 3843  double ***hbxij(double ***po, int nhstep
 /*************** log-likelihood *************/  /*************** log-likelihood *************/
 double func( double *x)  double func( double *x)
 {  {
   int i, ii, j, k, mi, d, kk;    int i, ii, j, k, mi, d, kk, kf=0;
   int ioffset=0;    int ioffset=0;
   double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];    double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
   double **out;    double **out;
   double lli; /* Individual log likelihood */    double lli; /* Individual log likelihood */
   int s1, s2;    int s1, s2;
   int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */    int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
   
   double bbh, survp;    double bbh, survp;
   long ipmx;  
   double agexact;    double agexact;
     double agebegin, ageend;
   /*extern weight */    /*extern weight */
   /* We are differentiating ll according to initial status */    /* We are differentiating ll according to initial status */
   /*  for (i=1;i<=npar;i++) printf("%f ", x[i]);*/    /*  for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
Line 3871  double func( double *x) Line 3876  double func( double *x)
       */        */
       ioffset=2+nagesqr ;        ioffset=2+nagesqr ;
    /* Fixed */     /* Fixed */
       for (k=1; k<=ncovf;k++){ /* For each fixed covariate dummu or quant or prod */        for (kf=1; kf<=ncovf;kf++){ /* For each fixed covariate dummu or quant or prod */
         /* # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi */          /* # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi */
         /*             V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */          /*             V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
         /*  TvarF[1]=Tvar[6]=2,  TvarF[2]=Tvar[7]=7, TvarF[3]=Tvar[9]=1  ID of fixed covariates or product V2, V1*V2, V1 */          /*  TvarF[1]=Tvar[6]=2,  TvarF[2]=Tvar[7]=7, TvarF[3]=Tvar[9]=1  ID of fixed covariates or product V2, V1*V2, V1 */
         /* TvarFind;  TvarFind[1]=6,  TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod)  */          /* TvarFind;  TvarFind[1]=6,  TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod)  */
         cov[ioffset+TvarFind[k]]=covar[Tvar[TvarFind[k]]][i];/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V1 is fixed (TvarFind[1]=6)*/          cov[ioffset+TvarFind[kf]]=covar[Tvar[TvarFind[kf]]][i];/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V1 is fixed (TvarFind[1]=6)*/
         /* V1*V2 (7)  TvarFind[2]=7, TvarFind[3]=9 */          /* V1*V2 (7)  TvarFind[2]=7, TvarFind[3]=9 */
       }        }
       /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]         /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4] 
Line 3891  double func( double *x) Line 3896  double func( double *x)
          But if the variable is not in the model TTvar[iv] is the real variable effective in the model:           But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
          meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]           meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
       */        */
       for(mi=1; mi<= wav[i]-1; mi++){        for(mi=1; mi<= wav[i]-1; mi++){  /* Varying with waves */
         /* Wave varying (but not age varying) */
         for(k=1; k <= ncovv ; k++){ /* Varying  covariates in the model (single and product but no age )"V5+V4+V3+V4*V3+V5*age+V1*age+V1" +TvarVind 1,2,3,4(V4*V3)  Tvar[1]@7{5, 4, 3, 6, 5, 1, 1 ; 6 because the created covar is after V5 and is 6, minus 1+1, 3,2,1,4 positions in cotvar*/          for(k=1; k <= ncovv ; k++){ /* Varying  covariates in the model (single and product but no age )"V5+V4+V3+V4*V3+V5*age+V1*age+V1" +TvarVind 1,2,3,4(V4*V3)  Tvar[1]@7{5, 4, 3, 6, 5, 1, 1 ; 6 because the created covar is after V5 and is 6, minus 1+1, 3,2,1,4 positions in cotvar*/
           /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; but where is the crossproduct? */            /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; but where is the crossproduct? */
           cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];            cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];
Line 3901  double func( double *x) Line 3907  double func( double *x)
             oldm[ii][j]=(ii==j ? 1.0 : 0.0);              oldm[ii][j]=(ii==j ? 1.0 : 0.0);
             savm[ii][j]=(ii==j ? 1.0 : 0.0);              savm[ii][j]=(ii==j ? 1.0 : 0.0);
           }            }
   
           agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
           ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
         for(d=0; d<dh[mi][i]; d++){          for(d=0; d<dh[mi][i]; d++){
           newm=savm;            newm=savm;
           agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;            agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
Line 3991  double func( double *x) Line 4000  double func( double *x)
           /*survp += out[s1][j]; */            /*survp += out[s1][j]; */
           lli= log(survp);            lli= log(survp);
         }          }
         else if  (s2==-4) {           /* else if  (s2==-4) {  */
           for (j=3,survp=0. ; j<=nlstate; j++)            /*   for (j=3,survp=0. ; j<=nlstate; j++)   */
             survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];          /*     survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
           lli= log(survp);           /*   lli= log(survp);  */
         }           /* }  */
         else if  (s2==-5) {           /* else if  (s2==-5) {  */
           for (j=1,survp=0. ; j<=2; j++)            /*   for (j=1,survp=0. ; j<=2; j++)   */
             survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];          /*     survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
           lli= log(survp);           /*   lli= log(survp);  */
         }           /* }  */
         else{          else{
           lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */            lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
           /*  lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*(savm[s1][s2])):log((1.+bbh)*out[s1][s2]));*/ /* linear interpolation */            /*  lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*(savm[s1][s2])):log((1.+bbh)*out[s1][s2]));*/ /* linear interpolation */
Line 4198  double funcone( double *x) Line 4207  double funcone( double *x)
   for(k=1; k<=nlstate; k++) ll[k]=0.;    for(k=1; k<=nlstate; k++) ll[k]=0.;
   ioffset=0;    ioffset=0;
   for (i=1,ipmx=0, sw=0.; i<=imx; i++){    for (i=1,ipmx=0, sw=0.; i<=imx; i++){
       /* Computes the values of the ncovmodel covariates of the model
          depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
          Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
          to be observed in j being in i according to the model.
       */
     /* ioffset=2+nagesqr+cptcovage; */      /* ioffset=2+nagesqr+cptcovage; */
     ioffset=2+nagesqr;      ioffset=2+nagesqr;
     /* Fixed */      /* Fixed */
Line 4215  double funcone( double *x) Line 4229  double funcone( double *x)
 /*    cov[2+9]=covar[Tvar[9]][i];  */  /*    cov[2+9]=covar[Tvar[9]][i];  */
 /*    cov[2+9]=covar[1][i]; V1  */  /*    cov[2+9]=covar[1][i]; V1  */
     }      }
         /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4] 
            is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6 
            has been calculated etc */
         /* For an individual i, wav[i] gives the number of effective waves */
         /* We compute the contribution to Likelihood of each effective transition
            mw[mi][i] is real wave of the mi th effectve wave */
         /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
            s2=s[mw[mi+1][i]][i];
            And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i]
            But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
            meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
         */
       /* This part may be useless now because everythin should be in covar */
     /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */      /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
     /*   cov[++ioffset]=coqvar[TvarFQ[k]][i];/\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V2 and V1*V2 is fixed (k=6 and 7?)*\/ */      /*   cov[++ioffset]=coqvar[TvarFQ[k]][i];/\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V2 and V1*V2 is fixed (k=6 and 7?)*\/ */
     /* } */      /* } */
Line 4272  double funcone( double *x) Line 4299  double funcone( double *x)
         savm=oldm;          savm=oldm;
         oldm=newm;          oldm=newm;
       } /* end mult */        } /* end mult */
                 /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
           /* But now since version 0.9 we anticipate for bias at large stepm.
            * If stepm is larger than one month (smallest stepm) and if the exact delay 
            * (in months) between two waves is not a multiple of stepm, we rounded to 
            * the nearest (and in case of equal distance, to the lowest) interval but now
            * we keep into memory the bias bh[mi][i] and also the previous matrix product
            * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
            * probability in order to take into account the bias as a fraction of the way
                                    * from savm to out if bh is negative or even beyond if bh is positive. bh varies
                                    * -stepm/2 to stepm/2 .
                                    * For stepm=1 the results are the same as for previous versions of Imach.
                                    * For stepm > 1 the results are less biased than in previous versions. 
                                    */
       s1=s[mw[mi][i]][i];        s1=s[mw[mi][i]][i];
       s2=s[mw[mi+1][i]][i];        s2=s[mw[mi+1][i]][i];
       /* if(s2==-1){ */        /* if(s2==-1){ */
Line 6234  void  concatwav(int wav[], int **dh, int Line 6273  void  concatwav(int wav[], int **dh, int
   /* Covariances of health expectancies eij and of total life expectancies according    /* Covariances of health expectancies eij and of total life expectancies according
      to initial status i, ei. .       to initial status i, ei. .
   */    */
     /* Very time consuming function, but already optimized with precov */
   int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;    int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
   int nhstepma, nstepma; /* Decreasing with age */    int nhstepma, nstepma; /* Decreasing with age */
   double age, agelim, hf;    double age, agelim, hf;
Line 11608  int back_prevalence_limit(double *p, dou Line 11648  int back_prevalence_limit(double *p, dou
     
 int hPijx(double *p, int bage, int fage){  int hPijx(double *p, int bage, int fage){
     /*------------- h Pij x at various ages ------------*/      /*------------- h Pij x at various ages ------------*/
     /* to be optimized with precov */
   int stepsize;    int stepsize;
   int agelim;    int agelim;
   int hstepm;    int hstepm;
Line 11685  int hPijx(double *p, int bage, int fage) Line 11725  int hPijx(double *p, int bage, int fage)
     
  int hBijx(double *p, int bage, int fage, double ***prevacurrent){   int hBijx(double *p, int bage, int fage, double ***prevacurrent){
     /*------------- h Bij x at various ages ------------*/      /*------------- h Bij x at various ages ------------*/
       /* To be optimized with precov */
   int stepsize;    int stepsize;
   /* int agelim; */    /* int agelim; */
         int ageminl;          int ageminl;
Line 12447  Please run with mle=-1 to get a correct Line 12487  Please run with mle=-1 to get a correct
   mint=matrix(1,maxwav,firstobs,lastobs);    mint=matrix(1,maxwav,firstobs,lastobs);
   anint=matrix(1,maxwav,firstobs,lastobs);    anint=matrix(1,maxwav,firstobs,lastobs);
   s=imatrix(1,maxwav+1,firstobs,lastobs); /* s[i][j] health state for wave i and individual j */    s=imatrix(1,maxwav+1,firstobs,lastobs); /* s[i][j] health state for wave i and individual j */
   printf("BUG ncovmodel=%d NCOVMAX=%d 2**ncovmodel=%f BUG\n",ncovmodel,NCOVMAX,pow(2,ncovmodel));    /* printf("BUG ncovmodel=%d NCOVMAX=%d 2**ncovmodel=%f BUG\n",ncovmodel,NCOVMAX,pow(2,ncovmodel)); */
   tab=ivector(1,NCOVMAX);    tab=ivector(1,NCOVMAX);
   ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */    ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
   ncodemaxwundef=ivector(1,NCOVMAX); /* Number of code per covariate; if - 1 O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */    ncodemaxwundef=ivector(1,NCOVMAX); /* Number of code per covariate; if - 1 O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
Line 13761  Please run with mle=-1 to get a correct Line 13801  Please run with mle=-1 to get a correct
   
                                   
     /*---------- State-specific expectancies and variances ------------*/      /*---------- State-specific expectancies and variances ------------*/
                       /* Should be moved in a function */         
     strcpy(filerest,"T_");      strcpy(filerest,"T_");
     strcat(filerest,fileresu);      strcat(filerest,fileresu);
     if((ficrest=fopen(filerest,"w"))==NULL) {      if((ficrest=fopen(filerest,"w"))==NULL) {

Removed from v.1.335  
changed lines
  Added in v.1.336


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