--- imach/src/imach.c 2006/04/28 18:11:50 1.127 +++ imach/src/imach.c 2009/10/29 15:33:14 1.135 @@ -1,6 +1,32 @@ -/* $Id: imach.c,v 1.127 2006/04/28 18:11:50 brouard Exp $ +/* $Id: imach.c,v 1.135 2009/10/29 15:33:14 brouard Exp $ $State: Exp $ $Log: imach.c,v $ + Revision 1.135 2009/10/29 15:33:14 brouard + (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code. + + Revision 1.134 2009/10/29 13:18:53 brouard + (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code. + + Revision 1.133 2009/07/06 10:21:25 brouard + just nforces + + Revision 1.132 2009/07/06 08:22:05 brouard + Many tings + + Revision 1.131 2009/06/20 16:22:47 brouard + Some dimensions resccaled + + Revision 1.130 2009/05/26 06:44:34 brouard + (Module): Max Covariate is now set to 20 instead of 8. A + lot of cleaning with variables initialized to 0. Trying to make + V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better. + + Revision 1.129 2007/08/31 13:49:27 lievre + Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting + + Revision 1.128 2006/06/30 13:02:05 brouard + (Module): Clarifications on computing e.j + Revision 1.127 2006/04/28 18:11:50 brouard (Module): Yes the sum of survivors was wrong since imach-114 because nhstepm was no more computed in the age @@ -154,7 +180,7 @@ The same imach parameter file can be used but the option for mle should be -3. - Agnès, who wrote this part of the code, tried to keep most of the + Agnès, who wrote this part of the code, tried to keep most of the former routines in order to include the new code within the former code. The output is very simple: only an estimate of the intercept and of @@ -287,8 +313,8 @@ Also this programme outputs the covariance matrix of the parameters but also of the life expectancies. It also computes the period (stable) prevalence. - Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr). - Institut national d'études démographiques, Paris. + Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr). + Institut national d'études démographiques, Paris. This software have been partly granted by Euro-REVES, a concerted action from the European Union. It is copyrighted identically to a GNU software product, ie programme and @@ -361,13 +387,13 @@ extern int errno; #define GLOCK_ERROR_NOPATH -1 /* empty path */ #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */ -#define MAXPARM 30 /* Maximum number of parameters for the optimization */ +#define MAXPARM 128 /* Maximum number of parameters for the optimization */ #define NPARMAX 64 /* (nlstate+ndeath-1)*nlstate*ncovmodel */ #define NINTERVMAX 8 #define NLSTATEMAX 8 /* Maximum number of live states (for func) */ #define NDEATHMAX 8 /* Maximum number of dead states (for func) */ -#define NCOVMAX 8 /* Maximum number of covariates */ +#define NCOVMAX 20 /* Maximum number of covariates */ #define MAXN 20000 #define YEARM 12. /* Number of months per year */ #define AGESUP 130 @@ -383,41 +409,41 @@ extern int errno; #define ODIRSEPARATOR '/' #endif -/* $Id: imach.c,v 1.127 2006/04/28 18:11:50 brouard Exp $ */ +/* $Id: imach.c,v 1.135 2009/10/29 15:33:14 brouard Exp $ */ /* $State: Exp $ */ -char version[]="Imach version 0.98h, April 2006, INED-EUROREVES-Institut de longevite "; -char fullversion[]="$Revision: 1.127 $ $Date: 2006/04/28 18:11:50 $"; +char version[]="Imach version 0.98l, October 2009, INED-EUROREVES-Institut de longevite "; +char fullversion[]="$Revision: 1.135 $ $Date: 2009/10/29 15:33:14 $"; char strstart[80]; char optionfilext[10], optionfilefiname[FILENAMELENGTH]; -int erreur, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */ -int nvar; -int cptcovn=0, cptcovage=0, cptcoveff=0,cptcov; +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' */ int npar=NPARMAX; int nlstate=2; /* Number of live states */ int ndeath=1; /* Number of dead states */ -int ncovmodel, ncovcol; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */ +int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */ int popbased=0; int *wav; /* Number of waves for this individuual 0 is possible */ -int maxwav; /* Maxim number of waves */ -int jmin, jmax; /* min, max spacing between 2 waves */ -int ijmin, ijmax; /* Individuals having jmin and jmax */ -int gipmx, gsw; /* Global variables on the number of contributions +int maxwav=0; /* Maxim number of waves */ +int jmin=0, jmax=0; /* min, max spacing between 2 waves */ +int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */ +int gipmx=0, gsw=0; /* Global variables on the number of contributions to the likelihood and the sum of weights (done by funcone)*/ -int mle, weightopt; +int mle=1, weightopt=0; int **mw; /* mw[mi][i] is number of the mi wave for this individual */ int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */ 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; /* Mean space between 2 waves */ +double jmean=1; /* Mean space between 2 waves */ double **oldm, **newm, **savm; /* Working pointers to matrices */ double **oldms, **newms, **savms; /* Fixed working pointers to matrices */ FILE *fic,*ficpar, *ficparo,*ficres, *ficresp, *ficrespl, *ficrespij, *ficrest,*ficresf,*ficrespop; FILE *ficlog, *ficrespow; -int globpr; /* Global variable for printing or not */ +int globpr=0; /* Global variable for printing or not */ double fretone; /* Only one call to likelihood */ -long ipmx; /* Number of contributions */ +long ipmx=0; /* Number of contributions */ double sw; /* Sum of weights */ char filerespow[FILENAMELENGTH]; char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */ @@ -510,7 +536,7 @@ double dateintmean=0; double *weight; int **s; /* Status */ double *agedc, **covar, idx; -int **nbcode, *Tcode, *Tvar, **codtab, **Tvard, *Tprod, cptcovprod, *Tvaraff; +int **nbcode, *Tvar, **codtab, **Tvard, *Tprod, cptcovprod, *Tvaraff; double *lsurv, *lpop, *tpop; double ftol=FTOL; /* Tolerance for computing Max Likelihood */ @@ -583,6 +609,20 @@ void replace_back_to_slash(char *s, char } } +char *trimbb(char *out, char *in) +{ /* Trim multiple blanks in line */ + char *s; + s=out; + while (*in != '\0'){ + while( *in == ' ' && *(in+1) == ' ' && *(in+1) != '\0'){ + in++; + } + *out++ = *in++; + } + *out='\0'; + return s; +} + int nbocc(char *s, char occ) { int i,j=0; @@ -1160,7 +1200,7 @@ double **prevalim(double **prlim, int nl int i, ii,j,k; double min, max, maxmin, maxmax,sumnew=0.; double **matprod2(); - double **out, cov[NCOVMAX], **pmij(); + double **out, cov[NCOVMAX+1], **pmij(); double **newm; double agefin, delaymax=50 ; /* Max number of years to converge */ @@ -1242,10 +1282,14 @@ double **pmij(double **ps, double *cov, for(i=1; i<= nlstate; i++){ s1=0; - for(j=1; j=1.e-10){ if(first==1){ - printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]); + printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]); } fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]); }else{ @@ -2206,7 +2252,7 @@ void prevalence(double ***probs, double We still use firstpass and lastpass as another selection. */ - int i, m, jk, k1, i1, j1, bool, z1,z2,j; + int i, m, jk, k1, i1, j1, bool, z1,j; double ***freq; /* Frequencies */ double *pp, **prop; double pos,posprop; @@ -2264,7 +2310,8 @@ void prevalence(double ***probs, double 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]); } }/* end jk */ }/* end i */ @@ -2422,35 +2469,41 @@ void concatwav(int wav[], int **dh, int void tricode(int *Tvar, int **nbcode, int imx) { - int Ndum[20],ij=1, k, j, i, maxncov=19; + /* Tvar[i]=atoi(stre); /* find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1 */ + + int Ndum[20],ij=1, k=0, j=0, i=0, maxncov=NCOVMAX; int cptcode=0; cptcoveff=0; for (k=0; k cptcode) cptcode=ij; /* getting the maximum of covariable + if (ij > cptcode) cptcode=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 0 and female is 1, then cptcode=1.*/ } - for (i=0; i<=cptcode; i++) { - if(Ndum[i]!=0) ncodemax[j]++; /* Nomber of modalities of the j th covariates. In fact ncodemax[j]=2 (dichotom. variables) but it can be more */ - } + for (i=0; i<=cptcode; i++) { /* i=-1 ?*/ + if(Ndum[i]!=0) ncodemax[j]++; /* Nomber of modalities of the j + th covariate. In fact + ncodemax[j]=2 + (dichotom. variables only) but + it can be more */ + } /* Ndum[-1] number of undefined modalities */ ij=1; - for (i=1; i<=ncodemax[j]; i++) { - for (k=0; k<= maxncov; k++) { - if (Ndum[k] != 0) { - nbcode[Tvar[j]][ij]=k; - /* store the modality in an array. k is a modality. If we have model=V1+V1*sex then: nbcode[1][1]=0 ; nbcode[1][2]=1; nbcode[2][1]=0 ; nbcode[2][2]=1; */ - + for (i=1; i<=ncodemax[j]; i++) { /* i= 1 to 2 */ + for (k=0; k<= maxncov; k++) { /* k=-1 ?*/ + 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 + then: nbcode[1][1]=0 ; nbcode[1][2]=1; nbcode[2][1]=0 ; nbcode[2][2]=1; */ ij++; } if (ij > ncodemax[j]) break; @@ -2460,9 +2513,9 @@ void tricode(int *Tvar, int **nbcode, in for (k=0; k< maxncov; k++) Ndum[k]=0; - for (i=1; i<=ncovmodel-2; i++) { + 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]; + ij=Tvar[i]; /* Tvar might be -1 if status was unknown */ Ndum[ij]++; } @@ -2473,8 +2526,8 @@ void tricode(int *Tvar, int **nbcode, in ij++; } } - - cptcoveff=ij-1; /*Number of simple covariates*/ + ij--; + cptcoveff=ij; /*Number of simple covariates*/ } /*********** Health Expectancies ****************/ @@ -2868,7 +2921,7 @@ void varevsij(char optionfilefiname[], d pstamp(ficresvij); fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are "); if(popbased==1) - fprintf(ficresvij,"the age specific prevalence observed in the population i.e cross-sectionally\n in each health state (popbased=1)"); + fprintf(ficresvij,"the age specific prevalence observed (cross-sectionally) in the population i.e cross-sectionally\n in each health state (popbased=1) (mobilav=%d\n",mobilav); else fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n"); fprintf(ficresvij,"# Age"); @@ -2896,8 +2949,7 @@ void varevsij(char optionfilefiname[], d /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm. nhstepm is the number of hstepm from age to agelim nstepm is the number of stepm from age to agelin. - Look at hpijx to understand the reason of that which relies in memory size - and note for a fixed period like k years */ + Look at function hpijx to understand why (it is linked to memory size questions) */ /* We decided (b) to get a life expectancy respecting the most precise curvature of the survival function given by stepm (the optimization length). Unfortunately it means that if the survival funtion is printed every two years of age and if @@ -2963,7 +3015,7 @@ void varevsij(char optionfilefiname[], d } } - for(j=1; j<= nlstate; j++){ + for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */ for(h=0; h<=nhstepm; h++){ for(i=1, gm[h][j]=0.;i<=nlstate;i++) gm[h][j] += prlim[i][i]*p3mat[i][j][h]; @@ -3073,9 +3125,9 @@ 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,"\nset noparametric;set nolabel; set ter png small;set size 0.65, 0.65"); + fprintf(ficgp,"\nunset parametric;unset label; set ter png small;set size 0.65, 0.65"); /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */ - fprintf(ficgp,"\n set log y; set nolog x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";"); + 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); */ @@ -3252,7 +3304,7 @@ void varprob(char optionfilefiname[], do fprintf(ficresprobcov,"\n"); fprintf(ficresprobcor,"\n"); */ - xp=vector(1,npar); + xp=vector(1,npar); dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar); doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath)); mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage); @@ -3405,7 +3457,7 @@ To be simple, these graphs help to under /* Confidence intervalle of pij */ /* - fprintf(ficgp,"\nset noparametric;unset label"); + fprintf(ficgp,"\nunset parametric;unset label"); fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\""); fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65"); fprintf(fichtm,"\n
Probability with confidence intervals expressed in year-1 :pijgr%s.png, ",optionfilefiname,optionfilefiname); @@ -3436,6 +3488,13 @@ To be simple, these graphs help to under /* Computing eigen value of matrix of covariance */ 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); + } + /* Eigen vectors */ v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12)); /*v21=sqrt(1.-v11*v11); *//* error */ @@ -3523,7 +3582,7 @@ void printinghtml(char fileres[], char t - Period (stable) prevalence in each health state: %s
\n", subdirf2(fileres,"pl"),subdirf2(fileres,"pl")); fprintf(fichtm,"\ - - (a) Life expectancies by health status at initial age, (b) health expectancies by health status at initial age: ei., eij . If one or more covariate are included, specific tables for each value of the covariate are output in sequences within the same file (estepm=%2d months): \ + - (a) Life expectancies by health status at initial age, ei. (b) health expectancies by health status at initial age, eij . If one or more covariates are included, specific tables for each value of the covariate are output in sequences within the same file (estepm=%2d months): \ %s
\n", estepm,subdirf2(fileres,"e"),subdirf2(fileres,"e")); fprintf(fichtm,"\ @@ -3588,11 +3647,11 @@ fprintf(fichtm," \n