Diff for /imach/src/imach.c between versions 1.136 and 1.137

version 1.136, 2010/04/26 20:30:53 version 1.137, 2010/04/29 18:11:38
Line 1 Line 1
 /* $Id$  /* $Id$
   $State$    $State$
   $Log$    $Log$
     Revision 1.137  2010/04/29 18:11:38  brouard
     (Module): Checking covariates for more complex models
     than V1+V2. A lot of change to be done. Unstable.
   
   Revision 1.136  2010/04/26 20:30:53  brouard    Revision 1.136  2010/04/26 20:30:53  brouard
   (Module): merging some libgsl code. Fixing computation    (Module): merging some libgsl code. Fixing computation
   of likelione (using inter/intrapolation if mle = 0) in order to    of likelione (using inter/intrapolation if mle = 0) in order to
Line 622  void replace_back_to_slash(char *s, char Line 626  void replace_back_to_slash(char *s, char
 }  }
   
 char *trimbb(char *out, char *in)  char *trimbb(char *out, char *in)
 { /* Trim multiple blanks in line */  { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
   char *s;    char *s;
   s=out;    s=out;
   while (*in != '\0'){    while (*in != '\0'){
     while( *in == ' ' && *(in+1) == ' ' && *(in+1) != '\0'){      while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
       in++;        in++;
     }      }
     *out++ = *in++;      *out++ = *in++;
Line 635  char *trimbb(char *out, char *in) Line 639  char *trimbb(char *out, char *in)
   return s;    return s;
 }  }
   
   char *cutv(char *blocc, char *alocc, char *in, char occ)
   {
     /* cuts string in into blocc and alocc where blocc ends before last occurence of char 'occ' 
        and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
        gives blocc="abcdef2ghi" and alocc="j".
        If occ is not found blocc is null and alocc is equal to in. Returns alocc
     */
     char *s, *t;
     t=in;s=in;
     while (*in != '\0'){
       while( *in == occ){
         *blocc++ = *in++;
         s=in;
       }
       *blocc++ = *in++;
     }
     if (s == t) /* occ not found */
       *(blocc-(in-s))='\0';
     else
       *(blocc-(in-s)-1)='\0';
     in=s;
     while ( *in != '\0'){
       *alocc++ = *in++;
     }
   
     *alocc='\0';
     return s;
   }
   
 int nbocc(char *s, char occ)  int nbocc(char *s, char occ)
 {  {
   int i,j=0;    int i,j=0;
Line 647  int nbocc(char *s, char occ) Line 680  int nbocc(char *s, char occ)
   return j;    return j;
 }  }
   
 void cutv(char *u,char *v, char*t, char occ)  /* void cutv(char *u,char *v, char*t, char occ) */
 {  /* { */
   /* cuts string t into u and v where u ends before first occurence of char 'occ'   /*   /\* cuts string t into u and v where u ends before last occurence of char 'occ'  */
      and v starts after first occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2')  /*      and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
      gives u="abcedf" and v="ghi2j" */  /*      gives u="abcdef2ghi" and v="j" *\/ */
   int i,lg,j,p=0;  /*   int i,lg,j,p=0; */
   i=0;  /*   i=0; */
   for(j=0; j<=strlen(t)-1; j++) {  /*   lg=strlen(t); */
     if((t[j]!= occ) && (t[j+1]== occ)) p=j+1;  /*   for(j=0; j<=lg-1; j++) { */
   }  /*     if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
   /*   } */
   
   lg=strlen(t);  /*   for(j=0; j<p; j++) { */
   for(j=0; j<p; j++) {  /*     (u[j] = t[j]); */
     (u[j] = t[j]);  /*   } */
   }  /*      u[p]='\0'; */
      u[p]='\0';  
   
    for(j=0; j<= lg; j++) {  /*    for(j=0; j<= lg; j++) { */
     if (j>=(p+1))(v[j-p-1] = t[j]);  /*     if (j>=(p+1))(v[j-p-1] = t[j]); */
   }  /*   } */
 }  /* } */
   
 /********************** nrerror ********************/  /********************** nrerror ********************/
   
Line 1434  double func( double *x) Line 1467  double func( double *x)
   if(mle==1){    if(mle==1){
     for (i=1,ipmx=0, sw=0.; i<=imx; i++){      for (i=1,ipmx=0, sw=0.; i<=imx; i++){
       for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];        for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];
         /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4] 
            is 6, Tvar[3=age*V3] should not been computed because of age Tvar[4=V3*V2] 
            has been calculated etc */
       for(mi=1; mi<= wav[i]-1; mi++){        for(mi=1; mi<= wav[i]-1; mi++){
         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++){
Line 1444  double func( double *x) Line 1480  double func( double *x)
           newm=savm;            newm=savm;
           cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;            cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
           for (kk=1; kk<=cptcovage;kk++) {            for (kk=1; kk<=cptcovage;kk++) {
             cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];              cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2]; /* Tage[kk] gives the data-covariate associated with age */
           }            }
           out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,            out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
                        1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));                         1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
Line 2503  void tricode(int *Tvar, int **nbcode, in Line 2539  void tricode(int *Tvar, int **nbcode, in
          (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and           (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
          female is 1, then modmaxcovj=1.*/           female is 1, then modmaxcovj=1.*/
     }      }
       /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
     for (i=0; i<=modmaxcovj; i++) { /* i=-1 ? 0 and 1*/      for (i=0; i<=modmaxcovj; i++) { /* i=-1 ? 0 and 1*//* For each modality of model-cov j */
       if( Ndum[i] != 0 )        if( Ndum[i] != 0 )
         ncodemax[j]++;           ncodemax[j]++; 
       /* Number of modalities of the j th covariate. In fact        /* Number of modalities of the j th covariate. In fact
Line 2515  void tricode(int *Tvar, int **nbcode, in Line 2551  void tricode(int *Tvar, int **nbcode, in
     /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */      /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
     ij=1;       ij=1; 
     for (i=1; i<=ncodemax[j]; i++) { /* i= 1 to 2 for dichotomous */      for (i=1; i<=ncodemax[j]; i++) { /* i= 1 to 2 for dichotomous */
       for (k=0; k<= maxncov; k++) { /* k=-1 ? NCOVMAX*/        for (k=0; k<= modmaxcovj; k++) { /* k=-1 ? NCOVMAX*//* maxncov or modmaxcovj */
         if (Ndum[k] != 0) { /* If at least one individual responded to this modality k */          if (Ndum[k] != 0) { /* If at least one individual responded to this modality k */
           nbcode[Tvar[j]][ij]=k;  /* stores the modality in an array nbcode.             nbcode[Tvar[j]][ij]=k;  /* stores the modality in an array nbcode. 
                                      k is a modality. If we have model=V1+V1*sex                                        k is a modality. If we have model=V1+V1*sex 
Line 2523  void tricode(int *Tvar, int **nbcode, in Line 2559  void tricode(int *Tvar, int **nbcode, in
           ij++;            ij++;
         }          }
         if (ij > ncodemax[j]) break;           if (ij > ncodemax[j]) break; 
       }          }  /* end of loop on */
     }       } /* end of loop on modality */ 
   }      } /* end of loop on model-covariate j. nbcode[Tvarj][1]=0 and nbcode[Tvarj][2]=1 sets the value of covariate j*/  
     
  for (k=0; k< maxncov; k++) Ndum[k]=0;    for (k=0; k< maxncov; k++) Ndum[k]=0;
     
  for (i=1; i<=ncovmodel-2; i++) { /* -2, cste and age */    for (i=1; i<=ncovmodel-2; i++) { /* -2, cste and age */
    /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/     /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
    ij=Tvar[i]; /* Tvar might be -1 if status was unknown */     ij=Tvar[i]; /* Tvar might be -1 if status was unknown */
    Ndum[ij]++;     Ndum[ij]++;
  }   }
   
  ij=1;   ij=1;
  for (i=1; i<= maxncov; i++) {   for (i=1; i<= maxncov; i++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
    if((Ndum[i]!=0) && (i<=ncovcol)){     if((Ndum[i]!=0) && (i<=ncovcol)){
      Tvaraff[ij]=i; /*For printing */       Tvaraff[ij]=i; /*For printing */
      ij++;       ij++;
Line 4533  int readdata(char datafile[], int firsto Line 4569  int readdata(char datafile[], int firsto
       
   
     for (j=maxwav;j>=1;j--){      for (j=maxwav;j>=1;j--){
       cutv(stra, strb,line,' ');         cutv(stra, strb, line, ' '); 
       if(strb[0]=='.') { /* Missing status */        if(strb[0]=='.') { /* Missing status */
         lval=-1;          lval=-1;
       }else{        }else{
Line 4693  int decodemodel ( char model[], int last Line 4729  int decodemodel ( char model[], int last
     cptcovprod=j1; /*Number of products  V1*V2 +v3*age = 2 */      cptcovprod=j1; /*Number of products  V1*V2 +v3*age = 2 */
           
     strcpy(modelsav,model);       strcpy(modelsav,model); 
     if ((strcmp(model,"age")==0) || (strcmp(model,"age*age")==0)){      if (strstr(model,"AGE") !=0){
       printf("Error. Non available option model=%s ",model);        printf("Error. AGE must be in lower case 'age' model=%s ",model);
       fprintf(ficlog,"Error. Non available option model=%s ",model);fflush(ficlog);        fprintf(ficlog,"Error. AGE must be in lower case model=%s ",model);fflush(ficlog);
       return 1;        return 1;
     }      }
           
     /* This loop fills the array Tvar from the string 'model'.*/      /* This loop fills the array Tvar from the string 'model'.*/
     /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */      /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
     /*    modelsav=V3*age+V2+V1+V4 strb=V3*age stra=V2+V1+V4       /*   modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4  */
         i=1 Tvar[1]=3 Tage[1]=1        /*  k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
         i=2 Tvar[2]=2      /*  k=3 V4 Tvar[k=3]= 4 (from V4) */
         i=3 Tvar[3]=1      /*  k=2 V1 Tvar[k=2]= 1 (from V1) */
         i=4 Tvar[4]= 4      /*  k=1 Tvar[1]=2 (from V2) */
         i=5 Tvar[5]      /*  k=5 Tvar[5] */
       for (k=1; k<=cptcovn;k++) {      /* for (k=1; k<=cptcovn;k++) { */
         cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];      /*  cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]]; */
      */      /*  } */
     for(k=1; k<=(j+1);k++){      /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
       cutv(strb,stra,modelsav,'+'); /* keeps in strb after the first '+'       for(k=cptcovn; k>=1;k--){
                                      modelsav=V3*age+V2+V1+V4 strb=V3*age stra=V2+V1+V4         cutv(stra,strb,modelsav,'+'); /* keeps in strb after the first '+' 
                                        modelsav==V2+V1+V4+V3*age strb=V3*age stra=V2+V1+V4 
                                     */                                       */ 
       /* if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav);*/ /* and analyzes it */        if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
       /*      printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/        /*      printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
       /*scanf("%d",i);*/        /*scanf("%d",i);*/
       if (strchr(strb,'*')) {  /* Model includes a product V3*age+V2+V1+V4 strb=V3*age */        if (strchr(strb,'*')) {  /* Model includes a product V2+V1+V4+V3*age strb=V3*age */
         cutv(strd,strc,strb,'*'); /* strd*strc  Vm*Vn: strb=V3*age strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */          cutv(strd,strc,strb,'*'); /* strd*strc  Vm*Vn: strb=V3*age strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
         if (strcmp(strc,"age")==0) { /* Vn*age */          if (strcmp(strc,"age")==0) { /* Vn*age */
           cptcovprod--;            cptcovprod--;
           cutv(strb,stre,strd,'V'); /* stre="V3" */            cutv(strb,stre,strd,'V'); /* stre="V3" */
           Tvar[k]=atoi(stre);  /* V1+V3*age+V2 Tvar[2]=3, and Tvar[3]=2 */            Tvar[k]=atoi(stre);  /* V2+V1+V4+V3*age Tvar[4]=2 ; V1+V2*age Tvar[2]=2 */
           cptcovage++; /* Sums the number of covariates which include age as a product */            cptcovage++; /* Sums the number of covariates which include age as a product */
           Tage[cptcovage]=k;  /* Tage[1] =2 */            Tage[cptcovage]=k;  /* Tage[1] = 4 */
           /*printf("stre=%s ", stre);*/            /*printf("stre=%s ", stre);*/
         }          } else if (strcmp(strd,"age")==0) { /* or age*Vn */
         else if (strcmp(strd,"age")==0) { /* or age*Vn */  
           cptcovprod--;            cptcovprod--;
           cutv(strb,stre,strc,'V');            cutv(strb,stre,strc,'V');
           Tvar[k]=atoi(stre);            Tvar[k]=atoi(stre);
           cptcovage++;            cptcovage++;
           Tage[cptcovage]=k;            Tage[cptcovage]=k;
         }          } else {  /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2  strb=V3*V2*/
         else {  /* Age is not in the model V1+V3*V2+V2  strb=V3*V2*/            /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
           cutv(strb,stre,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/            cutv(strb,stre,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
           Tvar[k]=ncovcol+k1;  /* find 'n' in Vn and stores in Tvar.             Tvar[k]=ncovcol+k1;  /* For model-covariate k tells which data-covariate to use but
                                   If already ncovcol=2 and model=V2*V1 Tvar[1]=2+1 and Tvar[2]=2+2 etc */                                    because this model-covariate is a construction we invent a new column
                                     ncovcol + k1
                                     If already ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2
                                     Tvar[3=V1*V4]=4+1 Tvar[5=V3*V2]=4 + 2= 6, etc */
           cutv(strb,strc,strd,'V'); /* strd was Vm, strc is m */            cutv(strb,strc,strd,'V'); /* strd was Vm, strc is m */
           Tprod[k1]=k;  /* Tprod[1]  */            Tprod[k1]=k;  /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2  */
           Tvard[k1][1]=atoi(strc); /* m*/            Tvard[k1][1]=atoi(strc); /* m 1 for V1*/
           Tvard[k1][2]=atoi(stre); /* n */            Tvard[k1][2]=atoi(stre); /* n 4 for V4*/
           Tvar[cptcovn+k2]=Tvard[k1][1];            Tvar[cptcovn+k2]=Tvard[k1][1]; /* Tvar[(cptcovn=4+k2=1)=5]= 1 (V1) */
           Tvar[cptcovn+k2+1]=Tvard[k1][2];             Tvar[cptcovn+k2+1]=Tvard[k1][2];  /* Tvar[(cptcovn=4+(k2=1)+1)=6]= 4 (V4) */
           for (i=1; i<=lastobs;i++) /* Computes the new covariate which is a product of covar[n][i]* covar[m][i]            for (i=1; i<=lastobs;i++){
                                      and is stored at ncovol+k1 */              /* Computes the new covariate which is a product of
                  covar[n][i]* covar[m][i] and stores it at ncovol+k1 */
             covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];              covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
             }
           k1++;            k1++;
           k2=k2+2;            k2=k2+2;
         }          } /* End age is not in the model */
       }        } /* End if model includes a product */
       else { /* no more sum */        else { /* no more sum */
         /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/          /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
        /*  scanf("%d",i);*/         /*  scanf("%d",i);*/
         cutv(strd,strc,strb,'V');          cutv(strd,strc,strb,'V');
         Tvar[i]=atoi(strc);          Tvar[k]=atoi(strc);
       }        }
       strcpy(modelsav,stra);  /* modelsav=V2+V3*age+V1+V4 strb=V3*age+V1+V4 */         strcpy(modelsav,stra);  /* modelsav=V2+V1+V4 stra=V2+V1+V4 */ 
       /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);        /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
         scanf("%d",i);*/          scanf("%d",i);*/
     } /* end of loop + */      } /* end of loop + */
Line 4773  int decodemodel ( char model[], int last Line 4814  int decodemodel ( char model[], int last
   scanf("%d ",i);*/    scanf("%d ",i);*/
   
   
   return (0);    return (0); /* with covar[new additional covariate if product] and Tage if age */ 
   endread:    endread:
     printf("Exiting decodemodel: ");      printf("Exiting decodemodel: ");
     return (1);      return (1);
Line 5317  run imach with mle=-1 to get a correct t Line 5358  run imach with mle=-1 to get a correct t
   anint=matrix(1,maxwav,1,n);    anint=matrix(1,maxwav,1,n);
   s=imatrix(1,maxwav+1,1,n); /* s[i][j] health state for wave i and individual j */     s=imatrix(1,maxwav+1,1,n); /* s[i][j] health state for wave i and individual j */ 
   tab=ivector(1,NCOVMAX);    tab=ivector(1,NCOVMAX);
   ncodemax=ivector(1,8);    ncodemax=ivector(1,8); /* hard coded ? */
   
   /* Reads data from file datafile */    /* Reads data from file datafile */
   if (readdata(datafile, firstobs, lastobs, &imx)==1)    if (readdata(datafile, firstobs, lastobs, &imx)==1)
     goto end;      goto end;
   
   /* Calculation of the number of parameters from char model */    /* Calculation of the number of parameters from char model */
   Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. Stores the number n of the covariates in Vm+Vn at 1 and m at 2 */      /*    modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 
           k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
           k=3 V4 Tvar[k=3]= 4 (from V4)
           k=2 V1 Tvar[k=2]= 1 (from V1)
           k=1 Tvar[1]=2 (from V2)
       */
     Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
     /*  V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs). 
         For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4, 
         Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
     */
     /* For model-covariate k tells which data-covariate to use but
       because this model-covariate is a construction we invent a new column
       ncovcol + k1
       If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
       Tvar[3=V1*V4]=4+1 etc */
   Tprod=ivector(1,15);     Tprod=ivector(1,15); 
     /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
        if  V2+V1+V1*V4+age*V3+V3*V2   TProd[k1=2]=5 (V3*V2)
     */
   Tvaraff=ivector(1,15);     Tvaraff=ivector(1,15); 
   Tvard=imatrix(1,15,1,2);    Tvard=imatrix(1,15,1,2); /* For V3*V2 Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
   Tage=ivector(1,15);          Tage=ivector(1,15); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
                            4 covariates (3 plus signs)
                            Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
                         */  
   
   if(decodemodel(model, lastobs) == 1)    if(decodemodel(model, lastobs) == 1)
     goto end;      goto end;
   
     if((double)(lastobs-imx)/(double)imx > 1.10){
       nbwarn++;
       printf("Warning: The value of parameter lastobs=%d is big compared to the \n  effective number of cases imx=%d, please adjust, \n  otherwise you are allocating more memory than necessary.\n",lastobs, imx); 
       fprintf(ficlog,"Warning: The value of parameter lastobs=%d is big compared to the \n  effective number of cases imx=%d, please adjust, \n  otherwise you are allocating more memory than necessary.\n",lastobs, imx); 
     }
     /*  if(mle==1){*/      /*  if(mle==1){*/
   if (weightopt != 1) { /* Maximisation without weights*/    if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
     for(i=1;i<=n;i++) weight[i]=1.0;      for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
   }    }
   
     /*-calculation of age at interview from date of interview and age at death -*/      /*-calculation of age at interview from date of interview and age at death -*/

Removed from v.1.136  
changed lines
  Added in v.1.137


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