--- imach/src/imach.c 2014/02/10 22:17:31 1.144 +++ imach/src/imach.c 2014/06/10 21:23:15 1.145 @@ -1,8 +1,18 @@ -/* $Id: imach.c,v 1.144 2014/02/10 22:17:31 brouard Exp $ +/* $Id: imach.c,v 1.145 2014/06/10 21:23:15 brouard Exp $ $State: Exp $ $Log: imach.c,v $ - Revision 1.144 2014/02/10 22:17:31 brouard - *** empty log message *** + Revision 1.145 2014/06/10 21:23:15 brouard + Summary: Debugging with valgrind + Author: Nicolas Brouard + + Lot of changes in order to output the results with some covariates + After the Edimburgh REVES conference 2014, it seems mandatory to + improve the code. + No more memory valgrind error but a lot has to be done in order to + continue the work of splitting the code into subroutines. + Also, decodemodel has been improved. Tricode is still not + optimal. nbcode should be improved. Documentation has been added in + the source code. Revision 1.143 2014/01/26 09:45:38 brouard Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising @@ -377,10 +387,22 @@ begin-prev-date,... open gnuplot file open html file - period (stable) prevalence - for age prevalim() - h Pij x - variance of p varprob + period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate + for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ****** + | 65 1 0 2 1 3 1 4 0 0.96326 0.03674 + freexexit2 possible for memory heap. + + h Pij x | pij_nom ficrestpij + # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3 + 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000 + 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907 + + 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340 + 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597 + variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in () + Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix + Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix + forecasting if prevfcast==1 prevforecast call prevalence() health expectancies Variance-covariance of DFLE @@ -439,6 +461,7 @@ extern int errno; #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */ #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */ #define NCOVMAX 20 /**< Maximum number of covariates, including generated covariates V1*V2 */ +#define codtabm(h,k) 1 & (h-1) >> (k-1) ; #define MAXN 20000 #define YEARM 12. /**< Number of months per year */ #define AGESUP 130 @@ -454,16 +477,23 @@ extern int errno; #define ODIRSEPARATOR '/' #endif -/* $Id: imach.c,v 1.144 2014/02/10 22:17:31 brouard Exp $ */ +/* $Id: imach.c,v 1.145 2014/06/10 21:23:15 brouard Exp $ */ /* $State: Exp $ */ char version[]="Imach version 0.98nR2, January 2014,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121)"; -char fullversion[]="$Revision: 1.144 $ $Date: 2014/02/10 22:17:31 $"; +char fullversion[]="$Revision: 1.145 $ $Date: 2014/06/10 21:23:15 $"; char strstart[80]; char optionfilext[10], optionfilefiname[FILENAMELENGTH]; int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */ int nvar=0, nforce=0; /* Number of variables, number of forces */ -int cptcovn=0, cptcovage=0, cptcoveff=0,cptcov=0; /* Number of covariates, of covariates with '*age' */ +/* Number of covariates model=V2+V1+ V3*age+V2*V4 */ +int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */ +int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */ +int cptcovs=0; /**< cptcovs number of simple covariates V2+V1 =2 */ +int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */ +int cptcovprodnoage=0; /**< Number of covariate products without age */ +int cptcoveff=0; /* Total number of covariates to vary for printing results */ +int cptcov=0; /* Working variable */ int npar=NPARMAX; int nlstate=2; /* Number of live states */ int ndeath=1; /* Number of dead states */ @@ -482,6 +512,7 @@ int **dh; /* dh[mi][i] is number of step int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between * wave mi and wave mi+1 is not an exact multiple of stepm. */ double jmean=1; /* Mean space between 2 waves */ +double **matprod2(); /* test */ double **oldm, **newm, **savm; /* Working pointers to matrices */ double **oldms, **newms, **savms; /* Fixed working pointers to matrices */ /*FILE *fic ; */ /* Used in readdata only */ @@ -582,11 +613,12 @@ double dateintmean=0; double *weight; int **s; /* Status */ double *agedc; -double **covar; /**< covar[i,j], value of jth covariate for individual i, +double **covar; /**< covar[j,i], value of jth covariate for individual i, * covar=matrix(0,NCOVMAX,1,n); * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2]; */ double idx; int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */ +int *Ndum; /** Freq of modality (tricode */ int **codtab; /**< codtab=imatrix(1,100,1,10); */ int **Tvard, *Tprod, cptcovprod, *Tvaraff; double *lsurv, *lpop, *tpop; @@ -675,6 +707,34 @@ char *trimbb(char *out, char *in) return s; } +char *cutl(char *blocc, char *alocc, char *in, char occ) +{ + /* cuts string in into blocc and alocc where blocc ends before first occurence of char 'occ' + and alocc starts after first 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 blocc + */ + char *s, *t, *bl; + t=in;s=in; + while ((*in != occ) && (*in != '\0')){ + *alocc++ = *in++; + } + if( *in == occ){ + *(alocc)='\0'; + s=++in; + } + + if (s == t) {/* occ not found */ + *(alocc-(in-s))='\0'; + in=s; + } + while ( *in != '\0'){ + *blocc++ = *in++; + } + + *blocc='\0'; + return t; +} 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' @@ -845,7 +905,9 @@ double **matrix(long nrl, long nrh, long for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol; return m; - /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) + /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0]) +m[i] = address of ith row of the table. &(m[i]) is its value which is another adress +that of m[i][0]. In order to get the value p m[i][0] but it is unitialized. */ } @@ -1280,7 +1342,7 @@ double **prevalim(double **prlim, int nl int i, ii,j,k; double min, max, maxmin, maxmax,sumnew=0.; - double **matprod2(); + /* double **matprod2(); */ /* test */ double **out, cov[NCOVMAX+1], **pmij(); double **newm; double agefin, delaymax=50 ; /* Max number of years to converge */ @@ -1300,15 +1362,17 @@ double **prevalim(double **prlim, int nl for (k=1; k<=cptcovn;k++) { cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]]; - /* printf("ij=%d k=%d Tvar[k]=%d nbcode=%d cov=%lf codtab[ij][Tvar[k]]=%d \n",ij,k, Tvar[k],nbcode[Tvar[k]][codtab[ij][Tvar[k]]],cov[2+k], codtab[ij][Tvar[k]]);*/ + /*printf("prevalim ij=%d k=%d Tvar[%d]=%d nbcode=%d cov=%lf codtab[%d][Tvar[%d]]=%d \n",ij,k, k, Tvar[k],nbcode[Tvar[k]][codtab[ij][Tvar[k]]],cov[2+k], ij, k, codtab[ij][Tvar[k]]);*/ } - for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; - for (k=1; k<=cptcovprod;k++) - cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]] * nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]]; + /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */ + /* for (k=1; k<=cptcovprod;k++) /\* Useless *\/ */ + /* cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]] * nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]]; */ /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/ /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/ /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/ + /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */ + /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */ out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */ savm=oldm; @@ -1321,6 +1385,7 @@ double **prevalim(double **prlim, int nl sumnew=0; for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k]; prlim[i][j]= newm[i][j]/(1-sumnew); + /*printf(" prevalim i=%d, j=%d, prmlim[%d][%d]=%f, agefin=%d \n", i, j, i, j, prlim[i][j],(int)agefin);*/ max=FMAX(max,prlim[i][j]); min=FMIN(min,prlim[i][j]); } @@ -1401,15 +1466,15 @@ double **pmij(double **ps, double *cov, } } - -/* for(ii=1; ii<= nlstate+ndeath; ii++){ */ -/* for(jj=1; jj<= nlstate+ndeath; jj++){ */ -/* printf("ddd %lf ",ps[ii][jj]); */ -/* } */ -/* printf("\n "); */ -/* } */ -/* printf("\n ");printf("%lf ",cov[2]); */ - /* + + /* for(ii=1; ii<= nlstate+ndeath; ii++){ */ + /* for(jj=1; jj<= nlstate+ndeath; jj++){ */ + /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */ + /* } */ + /* printf("\n "); */ + /* } */ + /* printf("\n ");printf("%lf ",cov[2]);*/ + /* for(i=1; i<= npar; i++) printf("%f ",x[i]); goto end;*/ return ps; @@ -1417,19 +1482,20 @@ double **pmij(double **ps, double *cov, /**************** Product of 2 matrices ******************/ -double **matprod2(double **out, double **in,long nrl, long nrh, long ncl, long nch, long ncolol, long ncoloh, double **b) +double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b) { /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */ /* in, b, out are matrice of pointers which should have been initialized before: only the contents of out is modified. The function returns a pointer to pointers identical to out */ - long i, j, k; + int i, j, k; for(i=nrl; i<= nrh; i++) - for(k=ncolol; k<=ncoloh; k++) - for(j=ncl,out[i][k]=0.; j<=nch; j++) - out[i][k] +=in[i][j]*b[j][k]; - + for(k=ncolol; k<=ncoloh; k++){ + out[i][k]=0.; + for(j=ncl; j<=nch; j++) + out[i][k] +=in[i][j]*b[j][k]; + } return out; } @@ -1471,7 +1537,7 @@ double ***hpxij(double ***po, int nhstep cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]]; for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; - for (k=1; k<=cptcovprod;k++) + for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */ cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]]; @@ -1522,7 +1588,9 @@ double func( double *x) 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. */ - for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i]; + for (k=1; k<=cptcovn;k++){ /* Simple and product covariates without age* products */ + 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 be computed because of age Tvar[4=V3*V2] has been calculated etc */ @@ -1790,8 +1858,11 @@ double funcone( double *x) for (kk=1; kk<=cptcovage;kk++) { cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2]; } + /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */ out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); + /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */ + /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */ savm=oldm; oldm=newm; } /* end mult */ @@ -2037,13 +2108,13 @@ double hessii(double x[], double delta, fx=func(x); for (i=1;i<=npar;i++) p2[i]=x[i]; - for(l=0 ; l <=lmax; l++){ + for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */ l1=pow(10,l); delts=delt; for(k=1 ; k 0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */ for (z1=1; z1<=cptcoveff; z1++) if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtab[j1][z1]]){ + /* Tests if the value of each of the covariates of i is equal to filter j1 */ bool=0; - printf("bool=%d i=%d, z1=%d, i1=%d, Tvaraff[%d]=%d, covar[Tvarff][%d]=%2f, codtab[%d][%d]=%d, nbcode[Tvaraff][codtab[%d][%d]=%d, j1=%d\n", - bool,i,z1, i1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtab[j1][z1], - j1,z1,nbcode[Tvaraff[z1]][codtab[j1][z1]],j1); + /* printf("bool=%d i=%d, z1=%d, Tvaraff[%d]=%d, covar[Tvarff][%d]=%2f, codtab[%d][%d]=%d, nbcode[Tvaraff][codtab[%d][%d]=%d, j1=%d\n", + bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtab[j1][z1], + j1,z1,nbcode[Tvaraff[z1]][codtab[j1][z1]],j1);*/ /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtab[7][3]=1 and nbcde[3][?]=1*/ } } @@ -2260,7 +2334,7 @@ void freqsummary(char fileres[], int ia /*}*/ } } - } + } /* end i */ /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/ pstamp(ficresp); @@ -2347,7 +2421,7 @@ void freqsummary(char fileres[], int ia printf("Others in log...\n"); fprintf(ficlog,"\n"); } - } + /*}*/ } dateintmean=dateintsum/k2cpt; @@ -2372,6 +2446,7 @@ void prevalence(double ***probs, double double pos,posprop; double y2; /* in fractional years */ int iagemin, iagemax; + int first; /** to stop verbosity which is redirected to log file */ iagemin= (int) agemin; iagemax= (int) agemax; @@ -2380,12 +2455,13 @@ void prevalence(double ***probs, double /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/ j1=0; - j=cptcoveff; + /*j=cptcoveff;*/ if (cptcovn<1) {j=1;ncodemax[1]=1;} - for(k1=1; k1<=j;k1++){ - for(i1=1; i1<=ncodemax[k1];i1++){ - j1++; + first=1; + for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ + /*for(i1=1; i1<=ncodemax[k1];i1++){ + j1++;*/ for (i=1; i<=nlstate; i++) for(m=iagemin; m <= iagemax+3; m++) @@ -2415,22 +2491,25 @@ void prevalence(double ***probs, double } } for(i=iagemin; i <= iagemax+3; i++){ - for(jk=1,posprop=0; jk <=nlstate ; jk++) { posprop += prop[jk][i]; } - + for(jk=1; jk <=nlstate ; jk++){ if( i <= iagemax){ if(posprop>=1.e-5){ probs[i][jk][j1]= prop[jk][i]/posprop; - } else - printf("Warning Observed prevalence probs[%d][%d][%d]=%lf because of lack of cases\n",jk,i,j1,probs[i][jk][j1]); + } else{ + if(first==1){ + first=0; + printf("Warning Observed prevalence probs[%d][%d][%d]=%lf because of lack of cases\nSee others on log file...\n",jk,i,j1,probs[i][jk][j1]); + } + } } }/* end jk */ }/* end i */ - } /* end i1 */ - } /* end k1 */ + /*} *//* end i1 */ + } /* end j1 */ /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/ /*free_vector(pp,1,nlstate);*/ @@ -2580,46 +2659,80 @@ void concatwav(int wav[], int **dh, int } /*********** Tricode ****************************/ -void tricode(int *Tvar, int **nbcode, int imx) +void tricode(int *Tvar, int **nbcode, int imx, int *Ndum) { /**< Uses cptcovn+2*cptcovprod as the number of covariates */ /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1 /* Boring subroutine which should only output nbcode[Tvar[j]][k] - /* nbcode[Tvar[j][1]= + * Tvar[5] in V2+V1+V3*age+V2*V4 is 2 (V2) + /* nbcode[Tvar[j]][1]= */ - int Ndum[20],ij=1, k=0, j=0, i=0, maxncov=NCOVMAX; + int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX; int modmaxcovj=0; /* Modality max of covariates j */ + int cptcode=0; /* Modality max of covariates j */ + int modmincovj=0; /* Modality min of covariates j */ + + cptcoveff=0; - for (k=0; k < maxncov; k++) Ndum[k]=0; + for (k=-1; k < maxncov; k++) Ndum[k]=0; for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */ - for (j=1; j<=(cptcovn+2*cptcovprod); j++) { /* For each covariate j */ - for (i=1; i<=imx; i++) { /*reads the data file to get the maximum value of the + /* Loop on covariates without age and products */ + for (j=1; j<=(cptcovs); j++) { /* model V1 + V2*age+ V3 + V3*V4 : V1 + V3 = 2 only */ + for (i=1; i<=imx; i++) { /* Lopp on individuals: reads the data file to get the maximum value of the modality of this covariate Vj*/ - ij=(int)(covar[Tvar[j]][i]); /* ij=0 or 1 or -1. Finds for covariate j, n=Tvar[j] of Vn . ij is the + ij=(int)(covar[Tvar[j]][i]); /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i + * If product of Vn*Vm, still boolean *: + * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables + * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */ + /* Finds for covariate j, n=Tvar[j] of Vn . ij is the modality of the nth covariate of individual i. */ + if (ij > modmaxcovj) + modmaxcovj=ij; + else if (ij < modmincovj) + modmincovj=ij; + if ((ij < -1) && (ij > NCOVMAX)){ + printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX ); + exit(1); + }else Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/ + /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */ /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/ - if (ij > modmaxcovj) modmaxcovj=ij; /* getting the maximum value of the modality of the covariate (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and female is 1, then modmaxcovj=1.*/ } + printf(" Minimal and maximal values of %d th covariate V%d: min=%d max=%d \n", j, Tvar[j], modmincovj, modmaxcovj); + cptcode=modmaxcovj; /* 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 each modality of model-cov j */ - if( Ndum[i] != 0 ) - ncodemax[j]++; - /* Number of modalities of the j th covariate. In fact - ncodemax[j]=2 (dichotom. variables only) but it could be more for - historical reasons */ + /*for (i=0; i<=cptcode; i++) {*/ + for (i=modmincovj; i<=modmaxcovj; i++) { /* i=-1 ? 0 and 1*//* For each value of the modality of model-cov j */ + printf("Frequencies of covariates %d V%d %d\n", j, Tvar[j], Ndum[i]); + if( Ndum[i] != 0 ){ /* Counts if nobody answered, empty modality */ + ncodemax[j]++; /* ncodemax[j]= Number of non-null modalities of the j th covariate. */ + } + /* In fact ncodemax[j]=2 (dichotom. variables only) but it could be more for + historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */ } /* Ndum[-1] number of undefined modalities */ /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */ - ij=1; - for (i=1; i<=ncodemax[j]; i++) { /* i= 1 to 2 for dichotomous */ - for (k=0; k<= modmaxcovj; k++) { /* k=-1 ? NCOVMAX*//* maxncov or modmaxcovj */ + /* For covariate j, modalities could be 1, 2, 3, 4. If Ndum[2]=0 ncodemax[j] is not 4 but 3 */ + /* If Ndum[3}= 635; Ndum[4]=0; Ndum[5]=0; Ndum[6]=27; Ndum[7]=125; + modmincovj=3; modmaxcovj = 7; + There are only 3 modalities non empty (or 2 if 27 is too few) : ncodemax[j]=3; + which will be coded 0, 1, 2 which in binary on 3-1 digits are 0=00 1=01, 2=10; defining two dummy + variables V1_1 and V1_2. + nbcode[Tvar[j]][ij]=k; + nbcode[Tvar[j]][1]=0; + nbcode[Tvar[j]][2]=1; + nbcode[Tvar[j]][3]=2; + */ + ij=1; /* ij is similar to i but can jumps over null modalities */ + for (i=modmincovj; i<=modmaxcovj; i++) { /* i= 1 to 2 for dichotomous, or from 1 to 3 */ + for (k=0; k<= cptcode; k++) { /* k=-1 ? k=0 to 1 *//* Could be 1 to 4 */ + /*recode from 0 */ 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. k is a modality. If we have model=V1+V1*sex @@ -2631,25 +2744,30 @@ void tricode(int *Tvar, int **nbcode, in } /* 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=-1; k< maxncov; k++) Ndum[k]=0; - 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.*/ - ij=Tvar[i]; /* Tvar might be -1 if status was unknown */ - Ndum[ij]++; - } + 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.*/ + ij=Tvar[i]; /* Tvar might be -1 if status was unknown */ + Ndum[ij]++; + } ij=1; - for (i=1; i<= maxncov; i++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */ + for (i=0; i<= maxncov-1; i++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */ + /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/ if((Ndum[i]!=0) && (i<=ncovcol)){ - Tvaraff[ij]=i; /*For printing */ + /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/ + Tvaraff[ij]=i; /*For printing (unclear) */ ij++; - } + }else + Tvaraff[ij]=0; } ij--; cptcoveff=ij; /*Number of total covariates*/ + } + /*********** Health Expectancies ****************/ void evsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,char strstart[] ) @@ -3245,15 +3363,15 @@ void varevsij(char optionfilefiname[], d free_vector(gmp,nlstate+1,nlstate+ndeath); free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath); free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/ - fprintf(ficgp,"\nunset parametric;unset label; set ter png small;set size 0.65, 0.65"); + fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */ fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";"); /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */ /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */ /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */ - fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 2 ",subdirf(fileresprobmorprev)); - fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95\%% interval\" w l lt 3 ",subdirf(fileresprobmorprev)); - fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 3 ",subdirf(fileresprobmorprev)); + fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev)); + fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95\%% interval\" w l lt 2 ",subdirf(fileresprobmorprev)); + fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev)); fprintf(fichtm,"\n
File (multiple files are possible if covariates are present): %s\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev)); fprintf(fichtm,"\n
Probability is computed over estepm=%d months.

\n", estepm,subdirf3(optionfilefiname,"varmuptjgr",digitp),digit); /* fprintf(fichtm,"\n
Probability is computed over estepm=%d months and then divided by estepm and multiplied by %.0f in order to have the probability to die over a year

\n", stepm,YEARM,digitp,digit); @@ -3363,20 +3481,19 @@ void varprob(char optionfilefiname[], do int i, j=0, i1, k1, l1, t, tj; int k2, l2, j1, z1; int k=0,l, cptcode; - int first=1, first1; + int first=1, first1, first2; double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp; double **dnewm,**doldm; double *xp; double *gp, *gm; double **gradg, **trgradg; double **mu; - double age,agelim, cov[NCOVMAX]; + double age,agelim, cov[NCOVMAX+1]; double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */ int theta; char fileresprob[FILENAMELENGTH]; char fileresprobcov[FILENAMELENGTH]; char fileresprobcor[FILENAMELENGTH]; - double ***varpij; strcpy(fileresprob,"prob"); @@ -3449,12 +3566,13 @@ standard deviations wide on each axis. < To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.
\n"); cov[1]=1; - tj=cptcoveff; + /* tj=cptcoveff; */ + tj = (int) pow(2,cptcoveff); if (cptcovn<1) {tj=1;ncodemax[1]=1;} j1=0; - for(t=1; t<=tj;t++){ - for(i1=1; i1<=ncodemax[t];i1++){ - j1++; + for(j1=1; j1<=tj;j1++){ + /*for(i1=1; i1<=ncodemax[t];i1++){ */ + /*j1++;*/ if (cptcovn>0) { fprintf(ficresprob, "\n#********** Variable "); for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]); @@ -3477,19 +3595,24 @@ To be simple, these graphs help to under fprintf(ficresprobcor, "**********\n#"); } + gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath)); + trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar); + gp=vector(1,(nlstate)*(nlstate+ndeath)); + gm=vector(1,(nlstate)*(nlstate+ndeath)); for (age=bage; age<=fage; age ++){ cov[2]=age; for (k=1; k<=cptcovn;k++) { - cov[2+k]=nbcode[Tvar[k]][codtab[j1][Tvar[k]]]; + cov[2+k]=nbcode[Tvar[k]][codtab[j1][Tvar[k]]];/* j1 1 2 3 4 + * 1 1 1 1 1 + * 2 2 1 1 1 + * 3 1 2 1 1 + */ + /* nbcode[1][1]=0 nbcode[1][2]=1;*/ } for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; for (k=1; k<=cptcovprod;k++) cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]]; - gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath)); - trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar); - gp=vector(1,(nlstate)*(nlstate+ndeath)); - gm=vector(1,(nlstate)*(nlstate+ndeath)); for(theta=1; theta <=npar; theta++){ for(i=1; i<=npar; i++) @@ -3527,10 +3650,6 @@ To be simple, these graphs help to under matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov); matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg); - free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath)); - free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath)); - free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar); - free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar); pmij(pmmij,cov,ncovmodel,x,nlstate); @@ -3564,17 +3683,22 @@ To be simple, these graphs help to under i=0; for (k=1; k<=(nlstate);k++){ for (l=1; l<=(nlstate+ndeath);l++){ - i=i++; + i++; fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l); fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l); for (j=1; j<=i;j++){ + /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */ fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]); fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age])); } } }/* end of loop for state */ } /* end of loop for age */ - + free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath)); + free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath)); + free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar); + free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar); + /* Confidence intervalle of pij */ /* fprintf(ficgp,"\nunset parametric;unset label"); @@ -3587,7 +3711,7 @@ To be simple, these graphs help to under */ /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/ - first1=1; + first1=1;first2=2; for (k2=1; k2<=(nlstate);k2++){ for (l2=1; l2<=(nlstate+ndeath);l2++){ if(l2==k2) continue; @@ -3609,10 +3733,13 @@ To be simple, these graphs help to under lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.; lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.; if ((lc2 <0) || (lc1 <0) ){ - printf("Error: One eigen value of 2x2 matrix of covariance is negative, lc1=%11.3e, lc2=%11.3e, v1=%11.3e, v2=%11.3e, cv12=%11.3e.\n It means that the matrix was not well estimated (varpij), for i=%2d, j=%2d, age=%4d .\n See files %s and %s. Continuing by making them positive: WRONG RESULTS.\n", lc1, lc2, v1, v2, cv12, i, j, (int)age,fileresprobcov, fileresprobcor); - fprintf(ficlog,"Error: One eigen value of 2x2 matrix of covariance is negative, lc1=%11.3e, lc2=%11.3e, v1=%11.3e, v2=%11.3e, cv12=%11.3e\n", lc1, lc2, v1, v2, cv12);fflush(ficlog); - lc1=fabs(lc1); - lc2=fabs(lc2); + if(first2==1){ + first1=0; + printf("Strange: j1=%d One eigen value of 2x2 matrix of covariance is negative, lc1=%11.3e, lc2=%11.3e, v1=%11.3e, v2=%11.3e, cv12=%11.3e.\n It means that the matrix was not well estimated (varpij), for i=%2d, j=%2d, age=%4d .\n See files %s and %s. Probably WRONG RESULTS. See log file for details...\n", j1, lc1, lc2, v1, v2, cv12, i, j, (int)age,fileresprobcov, fileresprobcor); + } + fprintf(ficlog,"Strange: j1=%d One eigen value of 2x2 matrix of covariance is negative, lc1=%11.3e, lc2=%11.3e, v1=%11.3e, v2=%11.3e, cv12=%11.3e.\n It means that the matrix was not well estimated (varpij), for i=%2d, j=%2d, age=%4d .\n See files %s and %s. Probably WRONG RESULTS.\n", j1, lc1, lc2, v1, v2, cv12, i, j, (int)age,fileresprobcov, fileresprobcor);fflush(ficlog); + /* lc1=fabs(lc1); */ /* If we want to have them positive */ + /* lc2=fabs(lc2); */ } /* Eigen vectors */ @@ -3634,7 +3761,7 @@ To be simple, these graphs help to under first=0; 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 ter png small\nset size 0.65,0.65"); + fprintf(ficgp,"\nset ter png small size 320, 240"); fprintf(fichtmcov,"\n
Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year-1\ :\ %s%d%1d%1d-%1d%1d.png, ",k1,l1,k2,l2,\ @@ -3665,7 +3792,7 @@ To be simple, these graphs help to under } /* k12 */ } /*l1 */ }/* k1 */ - } /* loop covariates */ + /* } /* loop covariates */ } free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage); free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage); @@ -3711,7 +3838,7 @@ void printinghtml(char fileres[], char t fprintf(fichtm," \n