--- imach/src/imach.c 2003/06/25 16:33:55 1.93 +++ imach/src/imach.c 2004/05/16 15:05:56 1.98 @@ -1,6 +1,44 @@ -/* $Id: imach.c,v 1.93 2003/06/25 16:33:55 brouard Exp $ +/* $Id: imach.c,v 1.98 2004/05/16 15:05:56 brouard Exp $ $State: Exp $ $Log: imach.c,v $ + Revision 1.98 2004/05/16 15:05:56 brouard + New version 0.97 . First attempt to estimate force of mortality + directly from the data i.e. without the need of knowing the health + state at each age, but using a Gompertz model: log u =a + b*age . + This is the basic analysis of mortality and should be done before any + other analysis, in order to test if the mortality estimated from the + cross-longitudinal survey is different from the mortality estimated + from other sources like vital statistic data. + + 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 + 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 + the slope with 95% confident intervals. + + Current limitations: + A) Even if you enter covariates, i.e. with the + model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates. + B) There is no computation of Life Expectancy nor Life Table. + + Revision 1.97 2004/02/20 13:25:42 lievre + Version 0.96d. Population forecasting command line is (temporarily) + suppressed. + + Revision 1.96 2003/07/15 15:38:55 brouard + * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is + rewritten within the same printf. Workaround: many printfs. + + Revision 1.95 2003/07/08 07:54:34 brouard + * imach.c (Repository): + (Repository): Using imachwizard code to output a more meaningful covariance + matrix (cov(a12,c31) instead of numbers. + + Revision 1.94 2003/06/27 13:00:02 brouard + Just cleaning + Revision 1.93 2003/06/25 16:33:55 brouard (Module): On windows (cygwin) function asctime_r doesn't exist so I changed back to asctime which exists. @@ -162,6 +200,9 @@ #include #include "timeval.h" +/* #include */ +/* #define _(String) gettext (String) */ + #define MAXLINE 256 #define GNUPLOTPROGRAM "gnuplot" /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/ @@ -182,6 +223,7 @@ #define YEARM 12. /* Number of months per year */ #define AGESUP 130 #define AGEBASE 40 +#define AGEGOMP 10. /* Minimal age for Gompertz adjustment */ #ifdef unix #define DIRSEPARATOR '/' #define ODIRSEPARATOR '\\' @@ -190,11 +232,11 @@ #define ODIRSEPARATOR '/' #endif -/* $Id: imach.c,v 1.93 2003/06/25 16:33:55 brouard Exp $ */ +/* $Id: imach.c,v 1.98 2004/05/16 15:05:56 brouard Exp $ */ /* $State: Exp $ */ -char version[]="Imach version 0.96b, June 2003, INED-EUROREVES "; -char fullversion[]="$Revision: 1.93 $ $Date: 2003/06/25 16:33:55 $"; +char version[]="Imach version 0.70, May 2004, INED-EUROREVES "; +char fullversion[]="$Revision: 1.98 $ $Date: 2004/05/16 15:05:56 $"; int erreur, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */ int nvar; int cptcovn=0, cptcovage=0, cptcoveff=0,cptcov; @@ -223,6 +265,7 @@ int globpr; /* Global variable for print double fretone; /* Only one call to likelihood */ long ipmx; /* Number of contributions */ double sw; /* Sum of weights */ +char filerespow[FILENAMELENGTH]; char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */ FILE *ficresilk; FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor; @@ -237,12 +280,12 @@ char fileresvpl[FILENAMELENGTH]; char title[MAXLINE]; char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH]; char optionfilext[10], optionfilefiname[FILENAMELENGTH], plotcmd[FILENAMELENGTH]; -char tmpout[FILENAMELENGTH]; +char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH]; char command[FILENAMELENGTH]; int outcmd=0; char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH]; -char lfileres[FILENAMELENGTH]; + char filelog[FILENAMELENGTH]; /* Log file */ char filerest[FILENAMELENGTH]; char fileregp[FILENAMELENGTH]; @@ -285,9 +328,10 @@ static double maxarg1,maxarg2; static double sqrarg; #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg) #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;} +int agegomp= AGEGOMP; int imx; -int stepm; +int stepm=1; /* Stepm, step in month: minimum step interpolation*/ int estepm; @@ -295,9 +339,10 @@ int estepm; int m,nb; long *num; -int firstpass=0, lastpass=4,*cod, *ncodemax, *Tage; +int firstpass=0, lastpass=4,*cod, *ncodemax, *Tage,*cens; double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint; double **pmmij, ***probs; +double *ageexmed,*agecens; double dateintmean=0; double *weight; @@ -562,6 +607,41 @@ void free_ma3x(double ***m, long nrl, lo free((FREE_ARG)(m+nrl-NR_END)); } +/*************** function subdirf ***********/ +char *subdirf(char fileres[]) +{ + /* Caution optionfilefiname is hidden */ + strcpy(tmpout,optionfilefiname); + strcat(tmpout,"/"); /* Add to the right */ + strcat(tmpout,fileres); + return tmpout; +} + +/*************** function subdirf2 ***********/ +char *subdirf2(char fileres[], char *preop) +{ + + /* Caution optionfilefiname is hidden */ + strcpy(tmpout,optionfilefiname); + strcat(tmpout,"/"); + strcat(tmpout,preop); + strcat(tmpout,fileres); + return tmpout; +} + +/*************** function subdirf3 ***********/ +char *subdirf3(char fileres[], char *preop, char *preop2) +{ + + /* Caution optionfilefiname is hidden */ + strcpy(tmpout,optionfilefiname); + strcat(tmpout,"/"); + strcat(tmpout,preop); + strcat(tmpout,preop2); + strcat(tmpout,fileres); + return tmpout; +} + /***************** f1dim *************************/ extern int ncom; extern double *pcom,*xicom; @@ -775,9 +855,10 @@ void powell(double p[], double **xi, int last_time=curr_time; (void) gettimeofday(&curr_time,&tzp); printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, curr_time.tv_sec-last_time.tv_sec, curr_time.tv_sec-start_time.tv_sec);fflush(stdout); - fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, curr_time.tv_sec-last_time.tv_sec, curr_time.tv_sec-start_time.tv_sec); + /* fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, curr_time.tv_sec-last_time.tv_sec, curr_time.tv_sec-start_time.tv_sec); fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tv_sec-start_time.tv_sec); - for (i=1;i<=n;i++) { + */ + for (i=1;i<=n;i++) { printf(" %d %.12f",i, p[i]); fprintf(ficlog," %d %.12lf",i, p[i]); fprintf(ficrespow," %.12lf", p[i]); @@ -1219,31 +1300,10 @@ double func( double *x) oldm=newm; } /* 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 and 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'. The 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 neagtive 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]; s2=s[mw[mi+1][i]][i]; bbh=(double)bh[mi][i]/(double)stepm; - /* bias is positive if real duration - * is higher than the multiple of stepm and negative otherwise. - */ 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]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/ - /*lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.-+bh)*out[s1][s2])); */ /* exponential interpolation */ - /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/ - /*if(lli ==000.0)*/ - /*printf("bbh= %f lli=%f savm=%f out=%f %d\n",bbh,lli,savm[s1][s2], out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]],i); */ ipmx +=1; sw += weight[i]; ll[s[mw[mi][i]][i]] += 2*weight[i]*lli; @@ -1270,30 +1330,10 @@ double func( double *x) oldm=newm; } /* 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 and 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'. The 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 neagtive 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]; s2=s[mw[mi+1][i]][i]; bbh=(double)bh[mi][i]/(double)stepm; - /* bias is positive if real duration - * is higher than the multiple of stepm and negative otherwise. - */ - /* 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]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* exponential inter-extrapolation */ - /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/ - /*if(lli ==000.0)*/ - /*printf("bbh= %f lli=%f savm=%f out=%f %d\n",bbh,lli,savm[s1][s2], out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]],i); */ ipmx +=1; sw += weight[i]; ll[s[mw[mi][i]][i]] += 2*weight[i]*lli; @@ -1459,35 +1499,8 @@ double funcone( double *x) return -l; } -char *subdirf(char fileres[]) -{ - /* Caution optionfilefiname is hidden */ - strcpy(tmpout,optionfilefiname); - strcat(tmpout,"/"); /* Add to the right */ - strcat(tmpout,fileres); - return tmpout; -} - -char *subdirf2(char fileres[], char *preop) -{ - - strcpy(tmpout,optionfilefiname); - strcat(tmpout,"/"); - strcat(tmpout,preop); - strcat(tmpout,fileres); - return tmpout; -} -char *subdirf3(char fileres[], char *preop, char *preop2) -{ - - strcpy(tmpout,optionfilefiname); - strcat(tmpout,"/"); - strcat(tmpout,preop); - strcat(tmpout,preop2); - strcat(tmpout,fileres); - return tmpout; -} +/*************** function likelione ***********/ void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*funcone)(double [])) { /* This routine should help understanding what is done with @@ -1530,7 +1543,7 @@ void mlikeli(FILE *ficres,double p[], in double **xi; double fret; double fretone; /* Only one call to likelihood */ - char filerespow[FILENAMELENGTH]; + /* char filerespow[FILENAMELENGTH];*/ xi=matrix(1,npar,1,npar); for (i=1;i<=npar;i++) for (j=1;j<=npar;j++) @@ -1565,11 +1578,11 @@ void hesscov(double **matcov, double p[] int i, j,jk; int *indx; - double hessii(double p[], double delta, int theta, double delti[]); - double hessij(double p[], double delti[], int i, int j); + double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar); + double hessij(double p[], double delti[], int i, int j,double (*func)(double []),int npar); void lubksb(double **a, int npar, int *indx, double b[]) ; void ludcmp(double **a, int npar, int *indx, double *d) ; - + double gompertz(double p[]); hess=matrix(1,npar,1,npar); printf("\nCalculation of the hessian matrix. Wait...\n"); @@ -1577,9 +1590,11 @@ void hesscov(double **matcov, double p[] for (i=1;i<=npar;i++){ printf("%d",i);fflush(stdout); fprintf(ficlog,"%d",i);fflush(ficlog); - hess[i][i]=hessii(p,ftolhess,i,delti); - /*printf(" %f ",p[i]);*/ - /*printf(" %lf ",hess[i][i]);*/ + + hess[i][i]=hessii(p,ftolhess,i,delti,func,npar); + + /* printf(" %f ",p[i]); + printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/ } for (i=1;i<=npar;i++) { @@ -1587,7 +1602,8 @@ void hesscov(double **matcov, double p[] if (j>i) { printf(".%d%d",i,j);fflush(stdout); fprintf(ficlog,".%d%d",i,j);fflush(ficlog); - hess[i][j]=hessij(p,delti,i,j); + hess[i][j]=hessij(p,delti,i,j,func,npar); + hess[j][i]=hess[i][j]; /*printf(" %lf ",hess[i][j]);*/ } @@ -1658,14 +1674,14 @@ void hesscov(double **matcov, double p[] } /*************** hessian matrix ****************/ -double hessii( double x[], double delta, int theta, double delti[]) +double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar) { int i; int l=1, lmax=20; double k1,k2; double p2[NPARMAX+1]; double res; - double delt, delts, nkhi=10.,nkhif=1., khi=1.e-4; + double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; double fx; int k=0,kmax=10; double l1; @@ -1705,7 +1721,7 @@ double hessii( double x[], double delta, } -double hessij( double x[], double delti[], int thetai,int thetaj) +double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) { int i; int l=1, l1, lmax=20; @@ -2321,7 +2337,7 @@ void evsij(char fileres[], double ***eij hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */ - /* Computing Variances of health expectancies */ + /* Computing Variances of health expectancies */ for(theta=1; theta <=npar; theta++){ for(i=1; i<=npar; i++){ @@ -2875,8 +2891,8 @@ void varprob(char optionfilefiname[], do fprintf(fichtm,"\n
  • Computing and drawing one step probabilities with their confidence intervals

  • \n"); fprintf(fichtm,"\n"); - fprintf(fichtm,"\n
  • Computing matrix of variance-covariance of step probabilities

  • \n",optionfilehtmcov); - fprintf(fichtmcov,"\n

    Computing matrix of variance-covariance of step probabilities

    \n\ + fprintf(fichtm,"\n
  • Matrix of variance-covariance of pairs of step probabilities (drawings)

  • \n",optionfilehtmcov); + fprintf(fichtmcov,"\n

    Matrix of variance-covariance of pairs of step probabilities

    \n\ file %s
    \n",optionfilehtmcov); fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (pij, pkl) are estimated\ and drawn. It helps understanding how is the covariance between two incidences.\ @@ -2909,9 +2925,9 @@ To be simple, these graphs help to under fprintf(ficgp, "**********\n#\n"); - fprintf(fichtm, "\n
    ********** Variable "); + fprintf(fichtmcov, "\n
    ********** Variable "); for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]); - fprintf(fichtm, "**********\n
    "); + fprintf(fichtmcov, "**********\n
    "); fprintf(ficresprobcor, "\n#********** Variable "); for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]); @@ -3120,21 +3136,19 @@ void printinghtml(char fileres[], char t double jprev1, double mprev1,double anprev1, \ double jprev2, double mprev2,double anprev2){ int jj1, k1, i1, cpt; - /*char optionfilehtm[FILENAMELENGTH];*/ -/* if((fichtm=fopen(optionfilehtm,"a"))==NULL) { */ -/* printf("Problem with %s \n",optionfilehtm), exit(0); */ -/* fprintf(ficlog,"Problem with %s \n",optionfilehtm), exit(0); */ -/* } */ fprintf(fichtm,"