--- imach/src/imach.c 2003/06/17 20:04:08 1.86 +++ imach/src/imach.c 2010/04/30 18:19:40 1.138 @@ -1,6 +1,257 @@ -/* $Id: imach.c,v 1.86 2003/06/17 20:04:08 brouard Exp $ +/* $Id: imach.c,v 1.138 2010/04/30 18:19:40 brouard Exp $ $State: Exp $ $Log: imach.c,v $ + Revision 1.138 2010/04/30 18:19:40 brouard + *** empty log message *** + + 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 + (Module): merging some libgsl code. Fixing computation + of likelione (using inter/intrapolation if mle = 0) in order to + get same likelihood as if mle=1. + Some cleaning of code and comments added. + + 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 + loop. Now we define nhstepma in the age loop. + (Module): In order to speed up (in case of numerous covariates) we + compute health expectancies (without variances) in a first step + and then all the health expectancies with variances or standard + deviation (needs data from the Hessian matrices) which slows the + computation. + In the future we should be able to stop the program is only health + expectancies and graph are needed without standard deviations. + + Revision 1.126 2006/04/28 17:23:28 brouard + (Module): Yes the sum of survivors was wrong since + imach-114 because nhstepm was no more computed in the age + loop. Now we define nhstepma in the age loop. + Version 0.98h + + Revision 1.125 2006/04/04 15:20:31 lievre + Errors in calculation of health expectancies. Age was not initialized. + Forecasting file added. + + Revision 1.124 2006/03/22 17:13:53 lievre + Parameters are printed with %lf instead of %f (more numbers after the comma). + The log-likelihood is printed in the log file + + Revision 1.123 2006/03/20 10:52:43 brouard + * imach.c (Module):
=(p+1))(v[j-p-1] = t[j]); - } -} +/* void cutv(char *u,char *v, char*t, char occ) */ +/* { */ +/* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */ +/* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */ +/* gives u="abcdef2ghi" and v="j" *\/ */ +/* int i,lg,j,p=0; */ +/* i=0; */ +/* lg=strlen(t); */ +/* for(j=0; j<=lg-1; j++) { */ +/* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */ +/* } */ + +/* for(j=0; j
=(p+1))(v[j-p-1] = t[j]); */
+/* } */
+/* } */
/********************** nrerror ********************/
@@ -516,6 +867,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;
@@ -691,6 +1077,19 @@ void linmin(double p[], double xi[], int
free_vector(pcom,1,n);
}
+char *asc_diff_time(long time_sec, char ascdiff[])
+{
+ long sec_left, days, hours, minutes;
+ days = (time_sec) / (60*60*24);
+ sec_left = (time_sec) % (60*60*24);
+ hours = (sec_left) / (60*60) ;
+ sec_left = (sec_left) %(60*60);
+ minutes = (sec_left) /60;
+ sec_left = (sec_left) % (60);
+ sprintf(ascdiff,"%d day(s) %d hour(s) %d minute(s) %d second(s)",days, hours, minutes, sec_left);
+ return ascdiff;
+}
+
/*************** powell ************************/
void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
double (*func)(double []))
@@ -701,6 +1100,8 @@ void powell(double p[], double **xi, int
double del,t,*pt,*ptt,*xit;
double fp,fptt;
double *xits;
+ int niterf, itmp;
+
pt=vector(1,n);
ptt=vector(1,n);
xit=vector(1,n);
@@ -711,17 +1112,41 @@ void powell(double p[], double **xi, int
fp=(*fret);
ibig=0;
del=0.0;
- printf("\nPowell iter=%d -2*LL=%.12f",*iter,*fret);
- fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f",*iter,*fret);
- fprintf(ficrespow,"%d %.12f",*iter,*fret);
- for (i=1;i<=n;i++) {
+ 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); fflush(ficlog);
+/* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tv_sec-start_time.tv_sec); */
+ for (i=1;i<=n;i++) {
printf(" %d %.12f",i, p[i]);
fprintf(ficlog," %d %.12lf",i, p[i]);
fprintf(ficrespow," %.12lf", p[i]);
}
printf("\n");
fprintf(ficlog,"\n");
- fprintf(ficrespow,"\n");
+ fprintf(ficrespow,"\n");fflush(ficrespow);
+ if(*iter <=3){
+ tm = *localtime(&curr_time.tv_sec);
+ strcpy(strcurr,asctime(&tm));
+/* asctime_r(&tm,strcurr); */
+ forecast_time=curr_time;
+ itmp = strlen(strcurr);
+ if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
+ strcurr[itmp-1]='\0';
+ printf("\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,curr_time.tv_sec-last_time.tv_sec);
+ fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,curr_time.tv_sec-last_time.tv_sec);
+ for(niterf=10;niterf<=30;niterf+=10){
+ forecast_time.tv_sec=curr_time.tv_sec+(niterf-*iter)*(curr_time.tv_sec-last_time.tv_sec);
+ tmf = *localtime(&forecast_time.tv_sec);
+/* asctime_r(&tmf,strfor); */
+ strcpy(strfor,asctime(&tmf));
+ itmp = strlen(strfor);
+ if(strfor[itmp-1]=='\n')
+ strfor[itmp-1]='\0';
+ printf(" - if your program needs %d iterations to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",niterf, asc_diff_time(forecast_time.tv_sec-curr_time.tv_sec,tmpout),strfor,strcurr);
+ fprintf(ficlog," - if your program needs %d iterations to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",niterf, asc_diff_time(forecast_time.tv_sec-curr_time.tv_sec,tmpout),strfor,strcurr);
+ }
+ }
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) xit[j]=xi[j][i];
fptt=(*fret);
@@ -813,7 +1238,7 @@ void powell(double p[], double **xi, int
}
}
-/**** Prevalence limit (stable prevalence) ****************/
+/**** Prevalence limit (stable or period prevalence) ****************/
double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int ij)
{
@@ -823,7 +1248,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 */
@@ -838,21 +1263,21 @@ double **prevalim(double **prlim, int nl
for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
newm=savm;
/* Covariates have to be included here again */
- cov[2]=agefin;
-
- 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]]);*/
- }
- 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]]];
-
- /*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]);*/
+ cov[2]=agefin;
+
+ 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]]);*/
+ }
+ 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]]];
+
+ /*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]);*/
out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
-
+
savm=oldm;
oldm=newm;
maxmax=0.;
@@ -879,62 +1304,81 @@ double **prevalim(double **prlim, int nl
double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
{
- double s1, s2;
+ /* According to parameters values stored in x and the covariate's values stored in cov,
+ computes the probability to be observed in state j being in state i by appying the
+ model to the ncovmodel covariates (including constant and age).
+ lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
+ and, according on how parameters are entered, the position of the coefficient xij(nc) of the
+ ncth covariate in the global vector x is given by the formula:
+ j=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
+ Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
+ sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
+ Outputs ps[i][j] the probability to be observed in j being in j according to
+ the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
+ */
+ double s1, lnpijopii;
/*double t34;*/
int i,j,j1, nc, ii, jj;
for(i=1; i<= nlstate; i++){
- for(j=1; ji s1=%.17e, s2=%.17e %lx %lx\n",s1,s2,s1,s2);*/
- }
- ps[i][j]=s2;
- }
- }
- /*ps[3][2]=1;*/
-
- for(i=1; i<= nlstate; i++){
- s1=0;
- for(j=1; ji s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
+ }
+ ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
+ }
+ }
+
+ for(i=1; i<= nlstate; i++){
+ s1=0;
+ for(j=1; ji} pij/pii=(1-pii)/pii and thus pii is known from s1 */
+ ps[i][i]=1./(s1+1.);
+ /* Computing other pijs */
+ for(j=1; j 1 the results are less biased than in previous versions.
@@ -1073,15 +1527,16 @@ double func( double *x)
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
+ /* bias bh is positive if real duration
* is higher than the multiple of stepm and negative otherwise.
*/
/* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
if( s2 > nlstate){
- /* i.e. if s2 is a death state and if the date of death is known then the contribution
- to the likelihood is the probability to die between last step unit time and current
- step unit time, which is also the differences between probability to die before dh
- and probability to die before dh-stepm .
+ /* i.e. if s2 is a death state and if the date of death is known
+ then the contribution to the likelihood is the probability to
+ die between last step unit time and current step unit time,
+ which is also equal to probability to die before dh
+ minus probability to die before dh-stepm .
In version up to 0.92 likelihood was computed
as if date of death was unknown. Death was treated as any other
health state: the date of the interview describes the actual state
@@ -1101,7 +1556,28 @@ double func( double *x)
lower mortality.
*/
lli=log(out[s1][s2] - savm[s1][s2]);
- }else{
+
+
+ } else if (s2==-2) {
+ for (j=1,survp=0. ; j<=nlstate; j++)
+ survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
+ /*survp += out[s1][j]; */
+ lli= log(survp);
+ }
+
+ else if (s2==-4) {
+ for (j=3,survp=0. ; j<=nlstate; j++)
+ survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
+ lli= log(survp);
+ }
+
+ else if (s2==-5) {
+ for (j=1,survp=0. ; j<=2; j++)
+ survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
+ lli= log(survp);
+ }
+
+ else{
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 */
}
@@ -1134,31 +1610,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;
@@ -1185,30 +1640,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;
@@ -1290,10 +1725,12 @@ double func( double *x)
/*************** log-likelihood *************/
double funcone( double *x)
{
+ /* Same as likeli but slower because of a lot of printf and if */
int i, ii, j, k, mi, d, kk;
- double l, ll[NLSTATEMAX], cov[NCOVMAX];
+ double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
double **out;
double lli; /* Individual log likelihood */
+ double llt;
int s1, s2;
double bbh, survp;
/*extern weight */
@@ -1334,7 +1771,11 @@ double funcone( double *x)
*/
if( s2 > nlstate && (mle <5) ){ /* Jackson */
lli=log(out[s1][s2] - savm[s1][s2]);
- } else if (mle==1){
+ } else if (s2==-2) {
+ for (j=1,survp=0. ; j<=nlstate; j++)
+ survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
+ lli= log(survp);
+ }else if (mle==1){
lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
} else if(mle==2){
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 */
@@ -1342,69 +1783,73 @@ double funcone( double *x)
lli= (savm[s1][s2]>(double)1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* exponential inter-extrapolation */
} else if (mle==4){ /* mle=4 no inter-extrapolation */
lli=log(out[s1][s2]); /* Original formula */
- } else{ /* ml>=5 no inter-extrapolation no jackson =0.8a */
- lli=log(out[s1][s2]); /* Original formula */
+ } else{ /* mle=0 back to 1 */
+ lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
+ /*lli=log(out[s1][s2]); */ /* Original formula */
} /* End of if */
ipmx +=1;
sw += weight[i];
ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
-/* printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */
+ /*printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */
if(globpr){
- fprintf(ficresilk,"%ld %6d %1d %1d %1d %1d %3d %10.6f %6.4f\
- %10.6f %10.6f %10.6f ", \
+ fprintf(ficresilk,"%9d %6d %2d %2d %1d %1d %3d %11.6f %8.4f\
+ %11.6f %11.6f %11.6f ", \
num[i],i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],
2*weight[i]*lli,out[s1][s2],savm[s1][s2]);
- for(k=1,l=0.; k<=nlstate; k++)
- fprintf(ficresilk," %10.6f",ll[k]);
- fprintf(ficresilk,"\n");
+ for(k=1,llt=0.,l=0.; k<=nlstate; k++){
+ llt +=ll[k]*gipmx/gsw;
+ fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
+ }
+ fprintf(ficresilk," %10.6f\n", -llt);
}
} /* end of wave */
} /* end of individual */
for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
/* printf("l1=%f l2=%f ",ll[1],ll[2]); */
l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
+ if(globpr==0){ /* First time we count the contributions and weights */
+ gipmx=ipmx;
+ gsw=sw;
+ }
return -l;
}
-void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpr, long *ipmx, double *sw, double *fretone, double (*funcone)(double []))
+/*************** 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 the selection of individuals/waves and
+ /* This routine should help understanding what is done with
+ the selection of individuals/waves and
to check the exact contribution to the likelihood.
Plotting could be done.
*/
int k;
- if(globpr !=0){ /* Just counts and sums no printings */
+
+ if(*globpri !=0){ /* Just counts and sums, no printings */
strcpy(fileresilk,"ilk");
strcat(fileresilk,fileres);
if((ficresilk=fopen(fileresilk,"w"))==NULL) {
printf("Problem with resultfile: %s\n", fileresilk);
fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
}
- fprintf(ficresilk, "#individual(line's record) s1 s2 wave# effective_wave# number_of_product_matrix pij weight 2ln(pij)*weight 0pij_x 0pij_(x-stepm) cumulating_loglikeli_by_health_state\n");
- fprintf(ficresilk, "#num_i i s1 s2 mi mw dh likeli weight out sav ");
+ fprintf(ficresilk, "#individual(line's_record) s1 s2 wave# effective_wave# number_of_matrices_product pij weight -2ln(pij)*weight 0pij_x 0pij_(x-stepm) cumulating_loglikeli_by_health_state(reweighted=-2ll*weightXnumber_of_contribs/sum_of_weights) and_total\n");
+ fprintf(ficresilk, "#num_i i s1 s2 mi mw dh likeli weight 2wlli out sav ");
/* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
for(k=1; k<=nlstate; k++)
- fprintf(ficresilk," ll[%d]",k);
- fprintf(ficresilk,"\n");
+ fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
+ fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
}
*fretone=(*funcone)(p);
- if(globpr !=0){
+ if(*globpri !=0){
fclose(ficresilk);
- if((fichtm=fopen(optionfilehtm,"a"))==NULL) {
- printf("Problem with html file: %s\n", optionfilehtm);
- fprintf(ficlog,"Problem with html file: %s\n", optionfilehtm);
- exit(0);
- }
- else{
- fprintf(fichtm,"\n ");
@@ -3043,37 +3680,56 @@ fprintf(fichtm," \n ");
+ fflush(fichtm);
+ fprintf(fichtm," ");
m=cptcoveff;
if (cptcovn < 1) {m=1;ncodemax[1]=1;}
@@ -3097,76 +3754,90 @@ fprintf(fichtm,"
File of contributions to the likelihood: %s
\n",fileresilk);
- fclose(fichtm);
- }
- }
+ fprintf(fichtm,"\n
File of contributions to the likelihood: %s
\n",subdirf(fileresilk),subdirf(fileresilk));
+ fflush(fichtm);
+ }
return;
}
+
/*********** Maximum Likelihood Estimation ***************/
void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
@@ -1413,7 +1858,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++)
@@ -1433,6 +1878,7 @@ void mlikeli(FILE *ficres,double p[], in
powell(p,xi,npar,ftol,&iter,&fret,func);
+ free_matrix(xi,1,npar,1,npar);
fclose(ficrespow);
printf("\n#Number of iterations = %d, -2 Log likelihood = %.12f\n",iter,func(p));
fprintf(ficlog,"\n#Number of iterations = %d, -2 Log likelihood = %.12f \n",iter,func(p));
@@ -1448,11 +1894,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");
@@ -1460,9 +1906,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++) {
@@ -1470,7 +1918,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]);*/
}
@@ -1541,14 +1990,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 p2[MAXPARM+1]; /* identical to x */
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;
@@ -1567,7 +2016,7 @@ double hessii( double x[], double delta,
/*res= (k1-2.0*fx+k2)/delt/delt; */
res= (k1+k2)/delt/delt/2.; /* Divided by because L and not 2*L */
-#ifdef DEBUG
+#ifdef DEBUGHESS
printf("%d %d k1=%.12e k2=%.12e xk1=%.12e xk2=%.12e delt=%.12e res=%.12e l=%d k=%d,fx=%.12e\n",theta,theta,k1,k2,x[theta]+delt,x[theta]-delt,delt,res, l, k,fx);
fprintf(ficlog,"%d %d k1=%.12e k2=%.12e xk1=%.12e xk2=%.12e delt=%.12e res=%.12e l=%d k=%d,fx=%.12e\n",theta,theta,k1,k2,x[theta]+delt,x[theta]-delt,delt,res, l, k,fx);
#endif
@@ -1588,12 +2037,12 @@ 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;
double k1,k2,k3,k4,res,fx;
- double p2[NPARMAX+1];
+ double p2[MAXPARM+1];
int k;
fx=func(x);
@@ -1697,16 +2146,20 @@ void lubksb(double **a, int n, int *indx
}
}
+void pstamp(FILE *fichier)
+{
+ fprintf(fichier,"# %s.%s\n#%s\n#%s\n# %s", optionfilefiname,optionfilext,version,fullversion,strstart);
+}
+
/************ Frequencies ********************/
-void freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, int *Tvaraff, int **nbcode, int *ncodemax,double **mint,double **anint)
+void freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, int *Tvaraff, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[])
{ /* Some frequencies */
- int i, m, jk, k1,i1, j1, bool, z1,z2,j;
+ int i, m, jk, k1,i1, j1, bool, z1,j;
int first;
double ***freq; /* Frequencies */
double *pp, **prop;
double pos,posprop, k2, dateintsum=0,k2cpt=0;
- FILE *ficresp;
char fileresp[FILENAMELENGTH];
pp=vector(1,nlstate);
@@ -1718,7 +2171,7 @@ void freqsummary(char fileres[], int ia
fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
exit(0);
}
- freq= ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);
+ freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin,iagemax+3);
j1=0;
j=cptcoveff;
@@ -1731,8 +2184,8 @@ void freqsummary(char fileres[], int ia
j1++;
/*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
scanf("%d", i);*/
- for (i=-1; i<=nlstate+ndeath; i++)
- for (jk=-1; jk<=nlstate+ndeath; jk++)
+ for (i=-5; i<=nlstate+ndeath; i++)
+ for (jk=-5; jk<=nlstate+ndeath; jk++)
for(m=iagemin; m <= iagemax+3; m++)
freq[i][jk][m]=0;
@@ -1771,7 +2224,7 @@ void freqsummary(char fileres[], int ia
}
/* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
-
+ pstamp(ficresp);
if (cptcovn>0) {
fprintf(ficresp, "\n#********** Variable ");
for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
@@ -1800,7 +2253,7 @@ void freqsummary(char fileres[], int ia
pos += freq[jk][m][i];
if(pp[jk]>=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{
@@ -1857,7 +2310,7 @@ void freqsummary(char fileres[], int ia
dateintmean=dateintsum/k2cpt;
fclose(ficresp);
- free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);
+ free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin, iagemax+3);
free_vector(pp,1,nlstate);
free_matrix(prop,1,nlstate,iagemin, iagemax+3);
/* End of Freq */
@@ -1871,7 +2324,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;
@@ -1929,7 +2382,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 */
@@ -1966,7 +2420,7 @@ void concatwav(int wav[], int **dh, int
mi=0;
m=firstpass;
while(s[m][i] <= nlstate){
- if(s[m][i]>=1)
+ if(s[m][i]>=1 || s[m][i]==-2 || s[m][i]==-4 || s[m][i]==-5)
mw[++mi][i]=m;
if(m >=lastpass)
break;
@@ -1982,12 +2436,13 @@ void concatwav(int wav[], int **dh, int
wav[i]=mi;
if(mi==0){
+ nbwarn++;
if(first==0){
- printf("Warning! None valid information for:%ld line=%d (skipped) and may be others, see log file\n",num[i],i);
+ printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
first=1;
}
if(first==1){
- fprintf(ficlog,"Warning! None valid information for:%ld line=%d (skipped)\n",num[i],i);
+ fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
}
} /* end mi==0 */
} /* End individuals */
@@ -2002,15 +2457,22 @@ void concatwav(int wav[], int **dh, int
j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
if(j==0) j=1; /* Survives at least one month after exam */
else if(j<0){
+ nberr++;
printf("Error! Negative delay (%d to death) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
- j=1; /* Careful Patch */
- printf(" We assumed that the date of interview was correct (and not the date of death) and postponed the death %d month(s) (one stepm) after the interview.\n You MUST fix the contradiction between dates.\n",stepm);
- printf("Error! Negative delay (%d to death) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
- fprintf(ficlog," We assumed that the date of interview was correct (and not the date of death) and postponed the death %d month(s) (one stepm) after the interview.\n You MUST fix the contradiction between dates.\n",stepm);
+ j=1; /* Temporary Dangerous patch */
+ printf(" We assumed that the date of interview was correct (and not the date of death) and postponed the death %d month(s) (one stepm) after the interview. You MUST fix the contradiction between dates.\n",stepm);
+ fprintf(ficlog,"Error! Negative delay (%d to death) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
+ fprintf(ficlog," We assumed that the date of interview was correct (and not the date of death) and postponed the death %d month(s) (one stepm) after the interview. You MUST fix the contradiction between dates.\n",stepm);
}
k=k+1;
- if (j >= jmax) jmax=j;
- if (j <= jmin) jmin=j;
+ if (j >= jmax){
+ jmax=j;
+ ijmax=i;
+ }
+ if (j <= jmin){
+ jmin=j;
+ ijmin=i;
+ }
sum=sum+j;
/*if (j<0) printf("j=%d num=%d \n",j,i);*/
/* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
@@ -2018,13 +2480,21 @@ void concatwav(int wav[], int **dh, int
}
else{
j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
- /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
+/* if (j<0) printf("%d %lf %lf %d %d %d\n", i,agev[mw[mi+1][i]][i], agev[mw[mi][i]][i],j,s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]); */
+
k=k+1;
- if (j >= jmax) jmax=j;
- else if (j <= jmin)jmin=j;
+ if (j >= jmax) {
+ jmax=j;
+ ijmax=i;
+ }
+ else if (j <= jmin){
+ jmin=j;
+ ijmin=i;
+ }
/* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
/*printf("%d %lf %d %d %d\n", i,agev[mw[mi][i]][i],j,s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);*/
if(j<0){
+ nberr++;
printf("Error! Negative delay (%d) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
fprintf(ficlog,"Error! Negative delay (%d) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
}
@@ -2038,7 +2508,7 @@ void concatwav(int wav[], int **dh, int
dh[mi][i]=jk;
bh[mi][i]=0;
}else{ /* We want a negative bias in order to only have interpolation ie
- * at the price of an extra matrix product in likelihood */
+ * to avoid the price of an extra matrix product in likelihood */
dh[mi][i]=jk+1;
bh[mi][i]=ju;
}
@@ -2063,96 +2533,102 @@ void concatwav(int wav[], int **dh, int
} /* end wave */
}
jmean=sum/k;
- printf("Delay (in months) between two waves Min=%d Max=%d Mean=%f\n\n ",jmin, jmax,jmean);
- fprintf(ficlog,"Delay (in months) between two waves Min=%d Max=%d Mean=%f\n\n ",jmin, jmax,jmean);
+ printf("Delay (in months) between two waves Min=%d (for indiviudal %ld) Max=%d (%ld) Mean=%f\n\n ",jmin, num[ijmin], jmax, num[ijmax], jmean);
+ fprintf(ficlog,"Delay (in months) between two waves Min=%d (for indiviudal %ld) Max=%d (%ld) Mean=%f\n\n ",jmin, ijmin, jmax, ijmax, jmean);
}
/*********** Tricode ****************************/
void tricode(int *Tvar, int **nbcode, int imx)
{
-
- int Ndum[20],ij=1, k, j, i, maxncov=19;
- int cptcode=0;
+ /* 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 */
+
+ int Ndum[20],ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
+ int modmaxcovj=0; /* Modality max of covariates j */
cptcoveff=0;
for (k=0; k Computing probabilities of dying over estepm months as a weighted average (i.e global mortality independent of initial healh state)
%s
\n",digitp);
- }
+ fprintf(ficgp,"\n# Routine varevsij");
+ /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
+ fprintf(fichtm,"\n Computing probabilities of dying over estepm months as a weighted average (i.e global mortality independent of initial healh state)
%s
\n",digitp);
+/* } */
varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
-
- fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are the stable prevalence in health states i\n");
+ 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 (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");
for(i=1; i<=nlstate;i++)
for(j=1; j<=nlstate;j++)
- fprintf(ficresvij," Cov(e%1d, e%1d)",i,j);
+ fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
fprintf(ficresvij,"\n");
xp=vector(1,npar);
@@ -2405,8 +3024,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
@@ -2472,7 +3090,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];
@@ -2582,20 +3200,21 @@ 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); */
- fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l 1 ",fileresprobmorprev);
- fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95\%% interval\" w l 2 ",fileresprobmorprev);
- fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l 2 ",fileresprobmorprev);
- fprintf(fichtm,"\n
File (multiple files are possible if covariates are present): %s\n",fileresprobmorprev,fileresprobmorprev);
- fprintf(fichtm,"\n
Probability is computed over estepm=%d months.
\n", estepm,digitp,optionfilefiname,digit);
+ fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l 1 ",subdirf(fileresprobmorprev));
+ fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95\%% interval\" w l 2 ",subdirf(fileresprobmorprev));
+ fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l 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);
*/
- fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.png\";replot;",digitp,optionfilefiname,digit);
+/* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.png\";replot;",digitp,optionfilefiname,digit); */
+ fprintf(ficgp,"\nset out \"%s%s.png\";replot;\n",subdirf3(optionfilefiname,"varmuptjgr",digitp),digit);
free_vector(xp,1,npar);
free_matrix(doldm,1,nlstate,1,nlstate);
@@ -2605,12 +3224,12 @@ void varevsij(char optionfilefiname[], d
free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
fclose(ficresprobmorprev);
- fclose(ficgp);
- fclose(fichtm);
+ fflush(ficgp);
+ fflush(fichtm);
} /* end varevsij */
/************ Variance of prevlim ******************/
-void varprevlim(char fileres[], double **varpl, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int ij)
+void varprevlim(char fileres[], double **varpl, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int ij, char strstart[])
{
/* Variance of prevalence limit */
/* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
@@ -2623,8 +3242,9 @@ void varprevlim(char fileres[], double *
double **gradg, **trgradg;
double age,agelim;
int theta;
-
- fprintf(ficresvpl,"# Standard deviation of stable prevalences \n");
+
+ pstamp(ficresvpl);
+ fprintf(ficresvpl,"# Standard deviation of period (stable) prevalences \n");
fprintf(ficresvpl,"# Age");
for(i=1; i<=nlstate;i++)
fprintf(ficresvpl," %1d-%1d",i,i);
@@ -2693,7 +3313,7 @@ void varprevlim(char fileres[], double *
}
/************ Variance of one-step probabilities ******************/
-void varprob(char optionfilefiname[], double **matcov, double x[], double delti[], int nlstate, double bage, double fage, int ij, int *Tvar, int **nbcode, int *ncodemax)
+void varprob(char optionfilefiname[], double **matcov, double x[], double delti[], int nlstate, double bage, double fage, int ij, int *Tvar, int **nbcode, int *ncodemax, char strstart[])
{
int i, j=0, i1, k1, l1, t, tj;
int k2, l2, j1, z1;
@@ -2738,13 +3358,15 @@ void varprob(char optionfilefiname[], do
fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
-
+ pstamp(ficresprob);
fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
fprintf(ficresprob,"# Age");
+ pstamp(ficresprobcov);
fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
fprintf(ficresprobcov,"# Age");
+ pstamp(ficresprobcor);
fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
- fprintf(ficresprobcov,"# Age");
+ fprintf(ficresprobcor,"# Age");
for(i=1; i<=nlstate;i++)
@@ -2757,34 +3379,29 @@ 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);
varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
first=1;
- if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) {
- printf("Problem with gnuplot file: %s\n", optionfilegnuplot);
- fprintf(ficlog,"Problem with gnuplot file: %s\n", optionfilegnuplot);
- exit(0);
- }
- else{
- fprintf(ficgp,"\n# Routine varprob");
- }
- if((fichtm=fopen(optionfilehtm,"a"))==NULL) {
- printf("Problem with html file: %s\n", optionfilehtm);
- fprintf(ficlog,"Problem with html file: %s\n", optionfilehtm);
- exit(0);
- }
- else{
- fprintf(fichtm,"\n Computing and drawing one step probabilities with their confidence intervals
Computing matrix of variance-covariance of step probabilities
\n");
- fprintf(fichtm,"\n
We have drawn x'cov-1x = 4 where x is the column vector (pij,pkl). It means that if pij and pkl where uncorrelated the (2X2) matrix would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 standard deviations wide on each axis.
When both incidences are correlated we diagonalised the inverse of the covariance matrix and made the appropriate rotation.
\n");
-
- }
+ fprintf(ficgp,"\n# Routine varprob");
+ fprintf(fichtm,"\n Computing and drawing one step probabilities with their confidence intervals
Matrix of variance-covariance of pairs of step probabilities (drawings)
Matrix of variance-covariance of pairs of step probabilities
\n\
+ file %s
\n",optionfilehtmcov);
+ fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p
\n");
+ fprintf(fichtmcov,"\n
Contour plot corresponding to x'cov-1x = 4 (where x is the column vector (pij,pkl)) are drawn. \
+It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
+would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
+standard deviations wide on each axis.
\
+ Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
+ and made the appropriate rotation to look at the uncorrelated principal directions.
\
+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;
@@ -2806,9 +3423,9 @@ void varprob(char optionfilefiname[], do
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]]);
@@ -2915,7 +3532,7 @@ void varprob(char optionfilefiname[], do
/* 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);
@@ -2946,6 +3563,13 @@ void varprob(char optionfilefiname[], do
/* 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 */
@@ -2966,10 +3590,14 @@ void varprob(char optionfilefiname[], do
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(fichtm,"\n
Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year-1 :varpijgr%s%d%1d%1d-%1d%1d.png, ",k1,l1,k2,l2,optionfilefiname, j1,k1,l1,k2,l2,optionfilefiname, j1,k1,l1,k2,l2);
- fprintf(fichtm,"\n
",optionfilefiname, j1,k1,l1,k2,l2);
- fprintf(fichtm,"\n
Correlation at age %d (%.3f),",(int) age, c12);
- fprintf(ficgp,"\nset out \"varpijgr%s%d%1d%1d-%1d%1d.png\"",optionfilefiname, j1,k1,l1,k2,l2);
+ 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,\
+ subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2,\
+ subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
+ fprintf(fichtmcov,"\n
",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
+ fprintf(fichtmcov,"\n
Correlation at age %d (%.3f),",(int) age, c12);
+ fprintf(ficgp,"\nset out \"%s%d%1d%1d-%1d%1d.png\"",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
fprintf(ficgp,"\nplot [-pi:pi] %11.3e+ %.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)), %11.3e +%.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)) not",\
@@ -2977,7 +3605,7 @@ void varprob(char optionfilefiname[], do
mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
}else{
first=0;
- fprintf(fichtm," %d (%.3f),",(int) age, c12);
+ fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
fprintf(ficgp,"\nreplot %11.3e+ %.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)), %11.3e +%.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)) not",\
@@ -2986,7 +3614,7 @@ void varprob(char optionfilefiname[], do
}/* if first */
} /* age mod 5 */
} /* end loop age */
- fprintf(ficgp,"\nset out \"varpijgr%s%d%1d%1d-%1d%1d.png\";replot;",optionfilefiname, j1,k1,l1,k2,l2);
+ fprintf(ficgp,"\nset out \"%s%d%1d%1d-%1d%1d.png\";replot;",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
first=1;
} /*l12 */
} /* k12 */
@@ -2996,12 +3624,14 @@ void varprob(char optionfilefiname[], do
}
free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
+ free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
+ free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
free_vector(xp,1,npar);
fclose(ficresprob);
fclose(ficresprobcov);
fclose(ficresprobcor);
- fclose(ficgp);
- fclose(fichtm);
+ fflush(ficgp);
+ fflush(fichtmcov);
}
@@ -3013,19 +3643,26 @@ 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,"Result files (first order: no variance)
\n \
- - Observed prevalence in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): p%s
\n \
- - Estimated transition probabilities over %d (stepm) months: pij%s
\n \
- - Stable prevalence in each health state: pl%s
\n \
- - Life expectancies by age and initial health status (estepm=%2d months): \
- e%s
\nResult files (first order: no variance)
\n \
+ - Observed prevalence in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): %s
\n ",
+ jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirf2(fileres,"p"),subdirf2(fileres,"p"));
+ fprintf(fichtm,"\
+ - Estimated transition probabilities over %d (stepm) months: %s
\n ",
+ stepm,subdirf2(fileres,"pij"),subdirf2(fileres,"pij"));
+ fprintf(fichtm,"\
+ - 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, 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,"\
+ - Population projections by age and states: \
+ %s
\n
");
- fprintf(fichtm,"\n
");
}
/* Pij */
- fprintf(fichtm,"
- Pij or Conditional probabilities to be observed in state j being in state i, %d (stepm) months before: pe%s%d1.png
\
-",stepm,strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1);
+ fprintf(fichtm,"
- Pij or Conditional probabilities to be observed in state j being in state i, %d (stepm) months before: %s%d1.png
\
+",stepm,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1);
/* Quasi-incidences */
fprintf(fichtm,"
- Pij or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
- before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too: pe%s%d2.png
\
-",stepm,strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1);
- /* Stable prevalence in each health state */
+ before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too: %s%d2.png
\
+",stepm,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1);
+ /* Period (stable) prevalence in each health state */
for(cpt=1; cpt
\
-",strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1);
+ fprintf(fichtm,"
- Period (stable) prevalence in each health state : %s%d%d.png
\
+",subdirf2(optionfilefiname,"p"),cpt,jj1,subdirf2(optionfilefiname,"p"),cpt,jj1,subdirf2(optionfilefiname,"p"),cpt,jj1);
}
for(cpt=1; cpt<=nlstate;cpt++) {
- fprintf(fichtm,"\n
- Health life expectancies by age and initial health state (%d): exp%s%d%d.png
\
-",cpt,strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1);
+ fprintf(fichtm,"\n
- Life expectancy by health state (%d) at initial age and its decomposition into health expectancies : %s%d%d.png
\
+",cpt,subdirf2(optionfilefiname,"exp"),cpt,jj1,subdirf2(optionfilefiname,"exp"),cpt,jj1,subdirf2(optionfilefiname,"exp"),cpt,jj1);
}
- fprintf(fichtm,"\n
- Total life expectancy by age and \
-health expectancies in states (1) and (2): e%s%d.png
\
-",strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1);
} /* end i1 */
}/* End k1 */
fprintf(fichtm," Result files (second order: variances)
\n\
- - Parameter file with estimated parameters and covariance matrix: %s
\n\
- - Variance of one-step probabilities: prob%s
\n\
- - Variance-covariance of one-step probabilities: probcov%s
\n\
- - Correlation matrix of one-step probabilities: probcor%s
\n\
- - Variances and covariances of life expectancies by age and initial health status (estepm=%d months): v%s
\n\
- - Health expectancies with their variances (no covariance): t%s
\n\
- - Standard deviation of stable prevalences: vpl%s
\n",rfileres,rfileres,fileres,fileres,fileres,fileres,fileres,fileres, estepm, fileres,fileres,fileres,fileres,fileres,fileres);
+ fprintf(fichtm,"\
+\n Result files (second order: variances)
\n\
+ - Parameter file with estimated parameters and covariance matrix: %s
\n", rfileres,rfileres);
+
+ fprintf(fichtm," - Variance of one-step probabilities: %s
\n",
+ subdirf2(fileres,"prob"),subdirf2(fileres,"prob"));
+ fprintf(fichtm,"\
+ - Variance-covariance of one-step probabilities: %s
\n",
+ subdirf2(fileres,"probcov"),subdirf2(fileres,"probcov"));
+
+ fprintf(fichtm,"\
+ - Correlation matrix of one-step probabilities: %s
\n",
+ subdirf2(fileres,"probcor"),subdirf2(fileres,"probcor"));
+ fprintf(fichtm,"\
+ - Variances and covariances of health expectancies by age and initial health status (cov(eij,ekl)(estepm=%2d months): \
+ %s
\n
\n",
+ estepm,subdirf2(fileres,"stde"),subdirf2(fileres,"stde"));
+ fprintf(fichtm,"\
+ - Variances and covariances of health expectancies by age. Status (i) based health expectancies (in state j), eij are weighted by the period prevalences in each state i (if popbased=1, an additional computation is done using the cross-sectional prevalences, i.e population based) (estepm=%d months): %s
\n",
+ estepm, subdirf2(fileres,"v"),subdirf2(fileres,"v"));
+ fprintf(fichtm,"\
+ - Total life expectancy and total health expectancies to be spent in each health state e.j with their standard errors (if popbased=1, an additional computation is done using the cross-sectional prevalences, i.e population based) (estepm=%d months): %s
\n",
+ estepm, subdirf2(fileres,"t"),subdirf2(fileres,"t"));
+ fprintf(fichtm,"\
+ - Standard deviation of period (stable) prevalences: %s
\n",\
+ subdirf2(fileres,"vpl"),subdirf2(fileres,"vpl"));
/* if(popforecast==1) fprintf(fichtm,"\n */
/* - Prevalences forecasting: f%s
\n */
@@ -3081,7 +3737,8 @@ health expectancies in states (1) and (2
/*
",fileres,fileres,fileres,fileres); */
/* else */
/* fprintf(fichtm,"\n No population forecast: popforecast = %d (instead of 1) or stepm = %d (instead of 1) or model=%s (instead of .)
\n",popforecast, stepm, model); */
-fprintf(fichtm,"
");
-fclose(fichtm);
+ fflush(fichtm);
}
/******************* Gnuplot file **************/
-void printinggnuplot(char fileres[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
+void printinggnuplot(char fileres[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
- int m,cpt,k1,i,k,j,jk,k2,k3,ij,l;
- int ng;
- if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) {
- printf("Problem with file %s",optionfilegnuplot);
- fprintf(ficlog,"Problem with file %s",optionfilegnuplot);
- }
+ char dirfileres[132],optfileres[132];
+ int m0,cpt=0,k1=0,i=0,k=0,j=0,jk=0,k2=0,k3=0,ij=0,l=0;
+ int ng=0;
+/* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
+/* printf("Problem with file %s",optionfilegnuplot); */
+/* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
+/* } */
/*#ifdef windows */
- fprintf(ficgp,"cd \"%s\" \n",pathc);
+ fprintf(ficgp,"cd \"%s\" \n",pathc);
/*#endif */
-m=pow(2,cptcoveff);
-
+ m=pow(2,cptcoveff);
+
+ strcpy(dirfileres,optionfilefiname);
+ strcpy(optfileres,"vpl");
/* 1eme*/
for (cpt=1; cpt<= nlstate ; cpt ++) {
for (k1=1; k1<= m ; k1 ++) {
- fprintf(ficgp,"\nset out \"v%s%d%d.png\" \n",strtok(optionfile, "."),cpt,k1);
- fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter png small\nset size 0.65,0.65\nplot [%.f:%.f] \"vpl%s\" every :::%d::%d u 1:2 \"\%%lf",ageminpar,fage,fileres,k1-1,k1-1);
+ fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"v"),cpt,k1);
+ fprintf(ficgp,"\n#set out \"v%s%d%d.png\" \n",optionfilefiname,cpt,k1);
+ fprintf(ficgp,"set xlabel \"Age\" \n\
+set ylabel \"Probability\" \n\
+set ter png small\n\
+set size 0.65,0.65\n\
+plot [%.f:%.f] \"%s\" every :::%d::%d u 1:2 \"\%%lf",ageminpar,fage,subdirf2(fileres,"vpl"),k1-1,k1-1);
for (i=1; i<= nlstate ; i ++) {
if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
- else fprintf(ficgp," \%%*lf (\%%*lf)");
+ else fprintf(ficgp," \%%*lf (\%%*lf)");
}
- fprintf(ficgp,"\" t\"Stable prevalence\" w l 0,\"vpl%s\" every :::%d::%d u 1:($2+1.96*$3) \"\%%lf",fileres,k1-1,k1-1);
+ fprintf(ficgp,"\" t\"Period (stable) prevalence\" w l 0,\"%s\" every :::%d::%d u 1:($2+1.96*$3) \"\%%lf",subdirf2(fileres,"vpl"),k1-1,k1-1);
for (i=1; i<= nlstate ; i ++) {
if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
else fprintf(ficgp," \%%*lf (\%%*lf)");
}
- fprintf(ficgp,"\" t\"95\%% CI\" w l 1,\"vpl%s\" every :::%d::%d u 1:($2-1.96*$3) \"\%%lf",fileres,k1-1,k1-1);
+ fprintf(ficgp,"\" t\"95\%% CI\" w l 1,\"%s\" every :::%d::%d u 1:($2-1.96*$3) \"\%%lf",subdirf2(fileres,"vpl"),k1-1,k1-1);
for (i=1; i<= nlstate ; i ++) {
if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
else fprintf(ficgp," \%%*lf (\%%*lf)");
}
- fprintf(ficgp,"\" t\"\" w l 1,\"p%s\" every :::%d::%d u 1:($%d) t\"Observed prevalence \" w l 2",fileres,k1-1,k1-1,2+4*(cpt-1));
+ fprintf(ficgp,"\" t\"\" w l 1,\"%s\" every :::%d::%d u 1:($%d) t\"Observed prevalence \" w l 2",subdirf2(fileres,"p"),k1-1,k1-1,2+4*(cpt-1));
}
}
/*2 eme*/
for (k1=1; k1<= m ; k1 ++) {
- fprintf(ficgp,"\nset out \"e%s%d.png\" \n",strtok(optionfile, "."),k1);
+ fprintf(ficgp,"\nset out \"%s%d.png\" \n",subdirf2(optionfilefiname,"e"),k1);
fprintf(ficgp,"set ylabel \"Years\" \nset ter png small\nset size 0.65,0.65\nplot [%.f:%.f] ",ageminpar,fage);
for (i=1; i<= nlstate+1 ; i ++) {
k=2*i;
- fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:2 \"\%%lf",fileres,k1-1,k1-1);
+ fprintf(ficgp,"\"%s\" every :::%d::%d u 1:2 \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1);
for (j=1; j<= nlstate+1 ; j ++) {
if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
else fprintf(ficgp," \%%*lf (\%%*lf)");
}
if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l ,");
else fprintf(ficgp,"\" t\"LE in state (%d)\" w l ,",i-1);
- fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2-$3*2) \"\%%lf",fileres,k1-1,k1-1);
+ fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2-$3*2) \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1);
for (j=1; j<= nlstate+1 ; j ++) {
if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
else fprintf(ficgp," \%%*lf (\%%*lf)");
}
fprintf(ficgp,"\" t\"\" w l 0,");
- fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2+$3*2) \"\%%lf",fileres,k1-1,k1-1);
+ fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2+$3*2) \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1);
for (j=1; j<= nlstate+1 ; j ++) {
if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
else fprintf(ficgp," \%%*lf (\%%*lf)");
@@ -3180,9 +3851,12 @@ m=pow(2,cptcoveff);
for (k1=1; k1<= m ; k1 ++) {
for (cpt=1; cpt<= nlstate ; cpt ++) {
- k=2+nlstate*(2*cpt-2);
- fprintf(ficgp,"\nset out \"exp%s%d%d.png\" \n",strtok(optionfile, "."),cpt,k1);
- fprintf(ficgp,"set ter png small\nset size 0.65,0.65\nplot [%.f:%.f] \"e%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",ageminpar,fage,fileres,k1-1,k1-1,k,cpt);
+ /* k=2+nlstate*(2*cpt-2); */
+ k=2+(nlstate+1)*(cpt-1);
+ fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"exp"),cpt,k1);
+ fprintf(ficgp,"set ter png small\n\
+set size 0.65,0.65\n\
+plot [%.f:%.f] \"%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",ageminpar,fage,subdirf2(fileres,"e"),k1-1,k1-1,k,cpt);
/*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
@@ -3192,9 +3866,11 @@ m=pow(2,cptcoveff);
*/
for (i=1; i< nlstate ; i ++) {
- fprintf(ficgp," ,\"e%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",fileres,k1-1,k1-1,k+2*i,cpt,i+1);
+ fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+i,cpt,i+1);
+ /* fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+2*i,cpt,i+1);*/
}
+ fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+nlstate,cpt);
}
}
@@ -3202,15 +3878,18 @@ m=pow(2,cptcoveff);
for (k1=1; k1<= m ; k1 ++) {
for (cpt=1; cpt<=nlstate ; cpt ++) {
k=3;
- fprintf(ficgp,"\nset out \"p%s%d%d.png\" \n",strtok(optionfile, "."),cpt,k1);
- fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter png small\nset size 0.65,0.65\nplot [%.f:%.f] \"pij%s\" u ($1==%d ? ($3):1/0):($%d/($%d",ageminpar,agemaxpar,fileres,k1,k+cpt+1,k+1);
+ fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"p"),cpt,k1);
+ fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
+set ter png small\nset size 0.65,0.65\n\
+unset log y\n\
+plot [%.f:%.f] \"%s\" u ($1==%d ? ($3):1/0):($%d/($%d",ageminpar,agemaxpar,subdirf2(fileres,"pij"),k1,k+cpt+1,k+1);
for (i=1; i< nlstate ; i ++)
fprintf(ficgp,"+$%d",k+i+1);
fprintf(ficgp,")) t\"prev(%d,%d)\" w l",cpt,cpt+1);
l=3+(nlstate+ndeath)*cpt;
- fprintf(ficgp,",\"pij%s\" u ($1==%d ? ($3):1/0):($%d/($%d",fileres,k1,l+cpt+1,l+1);
+ fprintf(ficgp,",\"%s\" u ($1==%d ? ($3):1/0):($%d/($%d",subdirf2(fileres,"pij"),k1,l+cpt+1,l+1);
for (i=1; i< nlstate ; i ++) {
l=3+(nlstate+ndeath)*cpt;
fprintf(ficgp,"+$%d",l+i+1);
@@ -3234,7 +3913,7 @@ m=pow(2,cptcoveff);
for(ng=1; ng<=2;ng++){ /* Number of graphics: first is probabilities second is incidence per year*/
for(jk=1; jk <=m; jk++) {
- fprintf(ficgp,"\nset out \"pe%s%d%d.png\" \n",strtok(optionfile, "."),jk,ng);
+ fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"pe"),jk,ng);
if (ng==2)
fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
else
@@ -3281,7 +3960,7 @@ m=pow(2,cptcoveff);
} /* end k2 */
} /* end jk */
} /* end ng */
- fclose(ficgp);
+ fflush(ficgp);
} /* end gnuplot */
@@ -3611,493 +4290,538 @@ populforecast(char fileres[], double anp
fclose(ficrespop);
} /* End of popforecast */
-int fileappend(FILE *fichier, char *optionfile)
+int fileappend(FILE *fichier, char *optionfich)
{
- if((fichier=fopen(optionfile,"a"))==NULL) {
- printf("Problem with file: %s\n", optionfile);
- fprintf(ficlog,"Problem with file: %s\n", optionfile);
- return (1);
+ if((fichier=fopen(optionfich,"a"))==NULL) {
+ printf("Problem with file: %s\n", optionfich);
+ fprintf(ficlog,"Problem with file: %s\n", optionfich);
+ return (0);
}
-
+ fflush(fichier);
+ return (1);
}
-/***********************************************/
-/**************** Main Program *****************/
-/***********************************************/
-int main(int argc, char *argv[])
+
+/**************** function prwizard **********************/
+void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
{
- int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
- int i,j, k, n=MAXN,iter,m,size=100,cptcode, cptcod;
- int jj;
- int numlinepar=0; /* Current linenumber of parameter file */
- double agedeb, agefin,hf;
- double ageminpar=1.e20,agemin=1.e20, agemaxpar=-1.e20, agemax=-1.e20;
- double fret;
- double **xi,tmp,delta;
+ /* Wizard to print covariance matrix template */
- double dum; /* Dummy variable */
- double ***p3mat;
- double ***mobaverage;
- int *indx;
- char line[MAXLINE], linepar[MAXLINE];
- char path[132],pathc[132],pathcd[132],pathtot[132],model[132];
- int firstobs=1, lastobs=10;
- int sdeb, sfin; /* Status at beginning and end */
- int c, h , cpt,l;
- int ju,jl, mi;
- int i1,j1, k1,k2,k3,jk,aa,bb, stepsize, ij;
- int jnais,jdc,jint4,jint1,jint2,jint3,**outcome,*tab;
- int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
- int mobilav=0,popforecast=0;
- int hstepm, nhstepm;
- double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
- double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
+ char ca[32], cb[32], cc[32];
+ int i,j, k, l, li, lj, lk, ll, jj, npar, itimes;
+ int numlinepar;
- double bage, fage, age, agelim, agebase;
- double ftolpl=FTOL;
- double **prlim;
- double *severity;
- double ***param; /* Matrix of parameters */
- double *p;
- double **matcov; /* Matrix of covariance */
- double ***delti3; /* Scale */
- double *delti; /* Scale */
- double ***eij, ***vareij;
- double **varpl; /* Variances of prevalence limits by age */
- double *epj, vepp;
- double kk1, kk2;
- double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
+ printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
+ fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
+ for(i=1; i <=nlstate; i++){
+ jj=0;
+ for(j=1; j <=nlstate+ndeath; j++){
+ if(j==i) continue;
+ jj++;
+ /*ca[0]= k+'a'-1;ca[1]='\0';*/
+ printf("%1d%1d",i,j);
+ fprintf(ficparo,"%1d%1d",i,j);
+ for(k=1; k<=ncovmodel;k++){
+ /* printf(" %lf",param[i][j][k]); */
+ /* fprintf(ficparo," %lf",param[i][j][k]); */
+ printf(" 0.");
+ fprintf(ficparo," 0.");
+ }
+ printf("\n");
+ fprintf(ficparo,"\n");
+ }
+ }
+ printf("# Scales (for hessian or gradient estimation)\n");
+ fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
+ npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
+ for(i=1; i <=nlstate; i++){
+ jj=0;
+ for(j=1; j <=nlstate+ndeath; j++){
+ if(j==i) continue;
+ jj++;
+ fprintf(ficparo,"%1d%1d",i,j);
+ printf("%1d%1d",i,j);
+ fflush(stdout);
+ for(k=1; k<=ncovmodel;k++){
+ /* printf(" %le",delti3[i][j][k]); */
+ /* fprintf(ficparo," %le",delti3[i][j][k]); */
+ printf(" 0.");
+ fprintf(ficparo," 0.");
+ }
+ numlinepar++;
+ printf("\n");
+ fprintf(ficparo,"\n");
+ }
+ }
+ printf("# Covariance matrix\n");
+/* # 121 Var(a12)\n\ */
+/* # 122 Cov(b12,a12) Var(b12)\n\ */
+/* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
+/* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
+/* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
+/* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
+/* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
+/* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
+ fflush(stdout);
+ fprintf(ficparo,"# Covariance matrix\n");
+ /* # 121 Var(a12)\n\ */
+ /* # 122 Cov(b12,a12) Var(b12)\n\ */
+ /* # ...\n\ */
+ /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
+
+ for(itimes=1;itimes<=2;itimes++){
+ jj=0;
+ for(i=1; i <=nlstate; i++){
+ for(j=1; j <=nlstate+ndeath; j++){
+ if(j==i) continue;
+ for(k=1; k<=ncovmodel;k++){
+ jj++;
+ ca[0]= k+'a'-1;ca[1]='\0';
+ if(itimes==1){
+ printf("#%1d%1d%d",i,j,k);
+ fprintf(ficparo,"#%1d%1d%d",i,j,k);
+ }else{
+ printf("%1d%1d%d",i,j,k);
+ fprintf(ficparo,"%1d%1d%d",i,j,k);
+ /* printf(" %.5le",matcov[i][j]); */
+ }
+ ll=0;
+ for(li=1;li <=nlstate; li++){
+ for(lj=1;lj <=nlstate+ndeath; lj++){
+ if(lj==li) continue;
+ for(lk=1;lk<=ncovmodel;lk++){
+ ll++;
+ if(ll<=jj){
+ cb[0]= lk +'a'-1;cb[1]='\0';
+ if(ll
");
}
for(cpt=1; cpt<=nlstate;cpt++) {
- fprintf(fichtm,"
- Observed and period prevalence (with confident\
-interval) in state (%d): v%s%d%d.png
\
-",cpt,strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1);
+ fprintf(fichtm,"
- Observed (cross-sectional) and period (incidence based) \
+prevalence (with 95%% confidence interval) in state (%d): %s%d%d.png
\
+",cpt,subdirf2(optionfilefiname,"v"),cpt,jj1,subdirf2(optionfilefiname,"v"),cpt,jj1);
}
+ fprintf(fichtm,"\n
- Total life expectancy by age and \
+health expectancies in states (1) and (2). If popbased=1 the smooth (due to the model) \
+true period expectancies (those weighted with period prevalences are also\
+ drawn in addition to the population based expectancies computed using\
+ observed and cahotic prevalences: %s%d.png
\
+",subdirf2(optionfilefiname,"e"),jj1,subdirf2(optionfilefiname,"e"),jj1);
} /* end i1 */
}/* End k1 */
fprintf(fichtm,"
");
- split(pathtot,path,optionfile,optionfilext,optionfilefiname);
- printf("pathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
- chdir(path);
- replace(pathc,path);
+fprintf(fichtm,"Result files
\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):
");
+ fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year
",p[1],p[2],agegomp);
+ for (i=1;i<=2;i++)
+ fprintf(fichtm," p[%d] = %lf [%f ; %f]
\n",i,p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
+ fprintf(fichtm,"
");
+ fprintf(fichtm,"Life table
\n
");
- /*-------- arguments in the command line --------*/
+ fprintf(fichtm,"\nAge l
");
- /* Log file */
- strcat(filelog, optionfilefiname);
- strcat(filelog,".log"); /* */
- if((ficlog=fopen(filelog,"w"))==NULL) {
- printf("Problem with logfile %s\n",filelog);
- goto end;
- }
- fprintf(ficlog,"Log filename:%s\n",filelog);
- fprintf(ficlog,"\n%s\n%s",version,fullversion);
- fprintf(ficlog,"\nEnter the parameter file name: ");
- fprintf(ficlog,"pathtot=%s, path=%s, optionfile=%s optionfilext=%s optionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
+ for (k=agegomp;k<(agemortsup-2);k++)
+ fprintf(fichtm,"%d %.0lf %lf %.0lf %.0lf %.0lf %lf
\n",k,lsurv[k],p[1]*exp(p[2]*(k-agegomp)),(p[1]*exp(p[2]*(k-agegomp)))*lsurv[k],lpop[k],tpop[k],tpop[k]/lsurv[k]);
- printf("Localtime (at start)=%s",strt);
- fprintf(ficlog,"Localtime (at start)=%s",strt);
- fflush(ficlog);
+
+ fflush(fichtm);
+}
- /* */
- strcpy(fileres,"r");
- strcat(fileres, optionfilefiname);
- strcat(fileres,".txt"); /* Other files have txt extension */
+/******************* Gnuplot file **************/
+void printinggnuplotmort(char fileres[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
- /*---------arguments file --------*/
+ char dirfileres[132],optfileres[132];
+ int m,cpt,k1,i,k,j,jk,k2,k3,ij,l;
+ int ng;
- if((ficpar=fopen(optionfile,"r"))==NULL) {
- printf("Problem with optionfile %s\n",optionfile);
- fprintf(ficlog,"Problem with optionfile %s\n",optionfile);
- fflush(ficlog);
- goto end;
- }
- strcpy(filereso,"o");
- strcat(filereso,fileres);
- if((ficparo=fopen(filereso,"w"))==NULL) {
- printf("Problem with Output resultfile: %s\n", filereso);
- fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
- fflush(ficlog);
- goto end;
- }
+ /*#ifdef windows */
+ fprintf(ficgp,"cd \"%s\" \n",pathc);
+ /*#endif */
- /* Reads comments: lines beginning with '#' */
- numlinepar=0;
- while((c=getc(ficpar))=='#' && c!= EOF){
- ungetc(c,ficpar);
- fgets(line, MAXLINE, ficpar);
- numlinepar++;
- puts(line);
- fputs(line,ficparo);
- fputs(line,ficlog);
- }
- ungetc(c,ficpar);
- fscanf(ficpar,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%lf stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d model=%s\n",title, datafile, &lastobs, &firstpass,&lastpass,&ftol, &stepm, &ncovcol, &nlstate,&ndeath, &maxwav, &mle, &weightopt,model);
- numlinepar++;
- printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate,ndeath, maxwav, mle, weightopt,model);
- fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol,nlstate,ndeath,maxwav, mle, weightopt,model);
- fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol,nlstate,ndeath,maxwav, mle, weightopt,model);
- fflush(ficlog);
- while((c=getc(ficpar))=='#' && c!= EOF){
- ungetc(c,ficpar);
- fgets(line, MAXLINE, ficpar);
- numlinepar++;
- puts(line);
- fputs(line,ficparo);
- fputs(line,ficlog);
- }
- ungetc(c,ficpar);
+ strcpy(dirfileres,optionfilefiname);
+ strcpy(optfileres,"vpl");
+ fprintf(ficgp,"set out \"graphmort.png\"\n ");
+ fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
+ fprintf(ficgp, "set ter png small\n set log y\n");
+ fprintf(ficgp, "set size 0.65,0.65\n");
+ fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
-
- covar=matrix(0,NCOVMAX,1,n);
- cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement*/
- if (strlen(model)>1) cptcovn=nbocc(model,'+')+1;
+}
- ncovmodel=2+cptcovn; /*Number of variables = cptcovn + intercept + age */
- nvar=ncovmodel-1; /* Suppressing age as a basic covariate */
-
- /* Read guess parameters */
- /* Reads comments: lines beginning with '#' */
- while((c=getc(ficpar))=='#' && c!= EOF){
- ungetc(c,ficpar);
- fgets(line, MAXLINE, ficpar);
- numlinepar++;
- puts(line);
- fputs(line,ficparo);
- fputs(line,ficlog);
- }
- ungetc(c,ficpar);
+int readdata(char datafile[], int firstobs, int lastobs, int *imax)
+{
- param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
- for(i=1; i <=nlstate; i++){
- j=0;
- for(jj=1; jj <=nlstate+ndeath; jj++){
- if(jj==i) continue;
- j++;
- fscanf(ficpar,"%1d%1d",&i1,&j1);
- if ((i1 != i) && (j1 != j)){
- printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
- exit(1);
- }
- fprintf(ficparo,"%1d%1d",i1,j1);
- if(mle==1)
- printf("%1d%1d",i,j);
- fprintf(ficlog,"%1d%1d",i,j);
- for(k=1; k<=ncovmodel;k++){
- fscanf(ficpar," %lf",¶m[i][j][k]);
- if(mle==1){
- printf(" %lf",param[i][j][k]);
- fprintf(ficlog," %lf",param[i][j][k]);
- }
- else
- fprintf(ficlog," %lf",param[i][j][k]);
- fprintf(ficparo," %lf",param[i][j][k]);
- }
- fscanf(ficpar,"\n");
- numlinepar++;
- if(mle==1)
- printf("\n");
- fprintf(ficlog,"\n");
- fprintf(ficparo,"\n");
- }
- }
- fflush(ficlog);
+ /*-------- data file ----------*/
+ FILE *fic;
+ char dummy[]=" ";
+ int i, j, n;
+ int linei, month, year,iout;
+ char line[MAXLINE], linetmp[MAXLINE];
+ char stra[80], strb[80];
+ char *stratrunc;
+ int lstra;
- npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
- p=param[1][1];
-
- /* Reads comments: lines beginning with '#' */
- while((c=getc(ficpar))=='#' && c!= EOF){
- ungetc(c,ficpar);
- fgets(line, MAXLINE, ficpar);
- numlinepar++;
- puts(line);
- fputs(line,ficparo);
- fputs(line,ficlog);
+ if((fic=fopen(datafile,"r"))==NULL) {
+ printf("Problem while opening datafile: %s\n", datafile);return 1;
+ fprintf(ficlog,"Problem while opening datafile: %s\n", datafile);return 1;
}
- ungetc(c,ficpar);
- delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
- /* delti=vector(1,npar); *//* Scale of each paramater (output from hesscov) */
- for(i=1; i <=nlstate; i++){
- for(j=1; j <=nlstate+ndeath-1; j++){
- fscanf(ficpar,"%1d%1d",&i1,&j1);
- if ((i1-i)*(j1-j)!=0){
- printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
- exit(1);
- }
- printf("%1d%1d",i,j);
- fprintf(ficparo,"%1d%1d",i1,j1);
- fprintf(ficlog,"%1d%1d",i1,j1);
- for(k=1; k<=ncovmodel;k++){
- fscanf(ficpar,"%le",&delti3[i][j][k]);
- printf(" %le",delti3[i][j][k]);
- fprintf(ficparo," %le",delti3[i][j][k]);
- fprintf(ficlog," %le",delti3[i][j][k]);
- }
- fscanf(ficpar,"\n");
- numlinepar++;
- printf("\n");
- fprintf(ficparo,"\n");
- fprintf(ficlog,"\n");
+ i=1;
+ linei=0;
+ while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
+ linei=linei+1;
+ for(j=strlen(line); j>=0;j--){ /* Untabifies line */
+ if(line[j] == '\t')
+ line[j] = ' ';
+ }
+ for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
+ ;
+ };
+ line[j+1]=0; /* Trims blanks at end of line */
+ if(line[0]=='#'){
+ fprintf(ficlog,"Comment line\n%s\n",line);
+ printf("Comment line\n%s\n",line);
+ continue;
+ }
+ trimbb(linetmp,line); /* Trims multiple blanks in line */
+ for (j=0; line[j]!='\0';j++){
+ line[j]=linetmp[j];
}
- }
- fflush(ficlog);
-
- delti=delti3[1][1];
-
-
- /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
-
- /* Reads comments: lines beginning with '#' */
- while((c=getc(ficpar))=='#' && c!= EOF){
- ungetc(c,ficpar);
- fgets(line, MAXLINE, ficpar);
- numlinepar++;
- puts(line);
- fputs(line,ficparo);
- fputs(line,ficlog);
- }
- ungetc(c,ficpar);
- matcov=matrix(1,npar,1,npar);
- for(i=1; i <=npar; i++){
- fscanf(ficpar,"%s",&str);
- if(mle==1)
- printf("%s",str);
- fprintf(ficlog,"%s",str);
- fprintf(ficparo,"%s",str);
- for(j=1; j <=i; j++){
- fscanf(ficpar," %le",&matcov[i][j]);
- if(mle==1){
- printf(" %.5le",matcov[i][j]);
+
+ for (j=maxwav;j>=1;j--){
+ cutv(stra, strb, line, ' ');
+ if(strb[0]=='.') { /* Missing status */
+ lval=-1;
+ }else{
+ errno=0;
+ lval=strtol(strb,&endptr,10);
+ /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
+ if( strb[0]=='\0' || (*endptr != '\0')){
+ printf("Error reading data around '%s' at line number %ld for individual %d, '%s'\nShould be a status of wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,j,maxwav);
+ fprintf(ficlog,"Error reading data around '%s' at line number %ld for individual %d, '%s'\nShould be a status of wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,j,maxwav);fflush(ficlog);
+ return 1;
+ }
}
- fprintf(ficlog," %.5le",matcov[i][j]);
- fprintf(ficparo," %.5le",matcov[i][j]);
+ s[j][i]=lval;
+
+ strcpy(line,stra);
+ cutv(stra, strb,line,' ');
+ if(iout=sscanf(strb,"%d/%d",&month, &year) != 0){
+ }
+ else if(iout=sscanf(strb,"%s.") != 0){
+ month=99;
+ year=9999;
+ }else{
+ printf("Error reading data around '%s' at line number %ld for individual %d, '%s'\nShould be a date of interview (mm/yyyy or .) at wave %d. Exiting.\n",strb, linei,i, line,j);
+ fprintf(ficlog,"Error reading data around '%s' at line number %ld for individual %d, '%s'\nShould be a date of interview (mm/yyyy or .) at wave %d. Exiting.\n",strb, linei,i, line,j);fflush(ficlog);
+ return 1;
+ }
+ anint[j][i]= (double) year;
+ mint[j][i]= (double)month;
+ strcpy(line,stra);
+ } /* ENd Waves */
+
+ cutv(stra, strb,line,' ');
+ if(iout=sscanf(strb,"%d/%d",&month, &year) != 0){
}
- fscanf(ficpar,"\n");
- numlinepar++;
- if(mle==1)
- printf("\n");
- fprintf(ficlog,"\n");
- fprintf(ficparo,"\n");
- }
- for(i=1; i <=npar; i++)
- for(j=i+1;j<=npar;j++)
- matcov[i][j]=matcov[j][i];
-
- if(mle==1)
- printf("\n");
- fprintf(ficlog,"\n");
-
- fflush(ficlog);
-
- /*-------- Rewriting paramater file ----------*/
- strcpy(rfileres,"r"); /* "Rparameterfile */
- strcat(rfileres,optionfilefiname); /* Parameter file first name*/
- strcat(rfileres,"."); /* */
- strcat(rfileres,optionfilext); /* Other files have txt extension */
- if((ficres =fopen(rfileres,"w"))==NULL) {
- printf("Problem writing new parameter file: %s\n", fileres);goto end;
- fprintf(ficlog,"Problem writing new parameter file: %s\n", fileres);goto end;
- }
- fprintf(ficres,"#%s\n",version);
+ else if(iout=sscanf(strb,"%s.",dummy) != 0){
+ month=99;
+ year=9999;
+ }else{
+ printf("Error reading data around '%s' at line number %ld for individual %d, '%s'\nShould be a date of death (mm/yyyy or .). Exiting.\n",strb, linei,i,line);
+ fprintf(ficlog,"Error reading data around '%s' at line number %ld for individual %d, '%s'\nShould be a date of death (mm/yyyy or .). Exiting.\n",strb, linei,i,line);fflush(ficlog);
+ return 1;
+ }
+ andc[i]=(double) year;
+ moisdc[i]=(double) month;
+ strcpy(line,stra);
- /*-------- data file ----------*/
- if((fic=fopen(datafile,"r"))==NULL) {
- printf("Problem with datafile: %s\n", datafile);goto end;
- fprintf(ficlog,"Problem with datafile: %s\n", datafile);goto end;
- }
-
- n= lastobs;
- severity = vector(1,maxwav);
- outcome=imatrix(1,maxwav+1,1,n);
- num=lvector(1,n);
- moisnais=vector(1,n);
- annais=vector(1,n);
- moisdc=vector(1,n);
- andc=vector(1,n);
- agedc=vector(1,n);
- cod=ivector(1,n);
- weight=vector(1,n);
- for(i=1;i<=n;i++) weight[i]=1.0; /* Equal weights, 1 by default */
- mint=matrix(1,maxwav,1,n);
- anint=matrix(1,maxwav,1,n);
- s=imatrix(1,maxwav+1,1,n);
- tab=ivector(1,NCOVMAX);
- ncodemax=ivector(1,8);
-
- i=1;
- while (fgets(line, MAXLINE, fic) != NULL) {
- if ((i >= firstobs) && (i <=lastobs)) {
-
- for (j=maxwav;j>=1;j--){
- cutv(stra, strb,line,' '); s[j][i]=atoi(strb);
- strcpy(line,stra);
- cutv(stra, strb,line,'/'); anint[j][i]=(double)(atoi(strb)); strcpy(line,stra);
- cutv(stra, strb,line,' '); mint[j][i]=(double)(atoi(strb)); strcpy(line,stra);
+ cutv(stra, strb,line,' ');
+ if(iout=sscanf(strb,"%d/%d",&month, &year) != 0){
+ }
+ else if(iout=sscanf(strb,"%s.") != 0){
+ month=99;
+ year=9999;
+ }else{
+ printf("Error reading data around '%s' at line number %ld for individual %d, '%s'\nShould be a date of birth (mm/yyyy or .). Exiting.\n",strb, linei,i,line);
+ fprintf(ficlog,"Error reading data around '%s' at line number %ld for individual %d, '%s'\nShould be a date of birth (mm/yyyy or .). Exiting.\n",strb, linei,i,line);fflush(ficlog);
+ return 1;
+ }
+ if (year==9999) {
+ printf("Error reading data around '%s' at line number %ld for individual %d, '%s'\nShould be a date of birth (mm/yyyy) but at least the year of birth should be given. Exiting.\n",strb, linei,i,line);
+ fprintf(ficlog,"Error reading data around '%s' at line number %ld for individual %d, '%s'\nShould be a date of birth (mm/yyyy) but at least the year of birth should be given. Exiting.\n",strb, linei,i,line);fflush(ficlog);
+ return 1;
+
+ }
+ annais[i]=(double)(year);
+ moisnais[i]=(double)(month);
+ strcpy(line,stra);
+
+ cutv(stra, strb,line,' ');
+ errno=0;
+ dval=strtod(strb,&endptr);
+ if( strb[0]=='\0' || (*endptr != '\0')){
+ printf("Error reading data around '%f' at line number %ld, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
+ fprintf(ficlog,"Error reading data around '%f' at line number %ld, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
+ fflush(ficlog);
+ return 1;
+ }
+ weight[i]=dval;
+ strcpy(line,stra);
+
+ for (j=ncovcol;j>=1;j--){
+ cutv(stra, strb,line,' ');
+ if(strb[0]=='.') { /* Missing status */
+ lval=-1;
+ }else{
+ errno=0;
+ lval=strtol(strb,&endptr,10);
+ if( strb[0]=='\0' || (*endptr != '\0')){
+ printf("Error reading data around '%d' at line number %ld for individual %d, '%s'\nShould be a covariate value (=0 for the reference or 1 for alternative). Exiting.\n",lval, linei,i, line);
+ fprintf(ficlog,"Error reading data around '%d' at line number %ld for individual %d, '%s'\nShould be a covariate value (=0 for the reference or 1 for alternative). Exiting.\n",lval, linei,i, line);fflush(ficlog);
+ return 1;
+ }
+ }
+ if(lval <-1 || lval >1){
+ printf("Error reading data around '%d' at line number %ld for individual %d, '%s'\n \
+ Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
+ for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
+ For example, for multinomial values like 1, 2 and 3,\n \
+ build V1=0 V2=0 for the reference value (1),\n \
+ V1=1 V2=0 for (2) \n \
+ and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
+ output of IMaCh is often meaningless.\n \
+ Exiting.\n",lval,linei, i,line,j);
+ fprintf(ficlog,"Error reading data around '%d' at line number %ld for individual %d, '%s'\n \
+ Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
+ for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
+ For example, for multinomial values like 1, 2 and 3,\n \
+ build V1=0 V2=0 for the reference value (1),\n \
+ V1=1 V2=0 for (2) \n \
+ and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
+ output of IMaCh is often meaningless.\n \
+ Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
+ return 1;
}
-
- cutv(stra, strb,line,'/'); andc[i]=(double)(atoi(strb)); strcpy(line,stra);
- cutv(stra, strb,line,' '); moisdc[i]=(double)(atoi(strb)); strcpy(line,stra);
+ covar[j][i]=(double)(lval);
+ strcpy(line,stra);
+ }
+ lstra=strlen(stra);
+
+ if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
+ stratrunc = &(stra[lstra-9]);
+ num[i]=atol(stratrunc);
+ }
+ else
+ num[i]=atol(stra);
+ /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
+ printf("%ld %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]),weight[i], (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]), (mint[2][i]), (anint[2][i]), (s[2][i]), (mint[3][i]), (anint[3][i]), (s[3][i]), (mint[4][i]), (anint[4][i]), (s[4][i])); ij=ij+1;}*/
+
+ i=i+1;
+ } /* End loop reading data */
+
+ *imax=i-1; /* Number of individuals */
+ fclose(fic);
+
+ return (0);
+ endread:
+ printf("Exiting readdata: ");
+ fclose(fic);
+ return (1);
- cutv(stra, strb,line,'/'); annais[i]=(double)(atoi(strb)); strcpy(line,stra);
- cutv(stra, strb,line,' '); moisnais[i]=(double)(atoi(strb)); strcpy(line,stra);
- cutv(stra, strb,line,' '); weight[i]=(double)(atoi(strb)); strcpy(line,stra);
- for (j=ncovcol;j>=1;j--){
- cutv(stra, strb,line,' '); covar[j][i]=(double)(atoi(strb)); strcpy(line,stra);
- }
- lstra=strlen(stra);
- if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
- stratrunc = &(stra[lstra-9]);
- num[i]=atol(stratrunc);
- }
- else
- num[i]=atol(stra);
-
- /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
- printf("%ld %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]),weight[i], (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]), (mint[2][i]), (anint[2][i]), (s[2][i]), (mint[3][i]), (anint[3][i]), (s[3][i]), (mint[4][i]), (anint[4][i]), (s[4][i])); ij=ij+1;}*/
- i=i+1;
- }
- }
- /* printf("ii=%d", ij);
- scanf("%d",i);*/
- imx=i-1; /* Number of individuals */
+}
- /* for (i=1; i<=imx; i++){
- if ((s[1][i]==3) && (s[2][i]==2)) s[2][i]=3;
- if ((s[2][i]==3) && (s[3][i]==2)) s[3][i]=3;
- if ((s[3][i]==3) && (s[4][i]==2)) s[4][i]=3;
- }*/
- /* for (i=1; i<=imx; i++){
- if (s[4][i]==9) s[4][i]=-1;
- printf("%ld %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]), (weight[i]), (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]), (mint[2][i]), (anint[2][i]), (s[2][i]), (mint[3][i]), (anint[3][i]), (s[3][i]), (mint[4][i]), (anint[4][i]), (s[4][i]));}*/
-
- for (i=1; i<=imx; i++)
-
- /*if ((s[3][i]==3) || (s[4][i]==3)) weight[i]=0.08;
- else weight[i]=1;*/
+int decodemodel ( char model[], int lastobs)
+{
+ int i, j, k;
+ int i1, j1, k1, k2;
+ char modelsav[80];
+ char stra[80], strb[80], strc[80], strd[80],stre[80];
- /* Calculation of the number of parameter from char model*/
- Tvar=ivector(1,15); /* stores the number n of the covariates in Vm+Vn at 1 and m at 2 */
- Tprod=ivector(1,15);
- Tvaraff=ivector(1,15);
- Tvard=imatrix(1,15,1,2);
- Tage=ivector(1,15);
-
if (strlen(model) >1){ /* If there is at least 1 covariate */
j=0, j1=0, k1=1, k2=1;
j=nbocc(model,'+'); /* j=Number of '+' */
j1=nbocc(model,'*'); /* j1=Number of '*' */
- cptcovn=j+1;
- cptcovprod=j1; /*Number of products */
+ cptcovn=j+1; /* Number of covariates V1+V2*age+V3 =>(2 plus signs) + 1=3
+ but the covariates which are product must be computed and stored. */
+ cptcovprod=j1; /*Number of products V1*V2 +v3*age = 2 */
strcpy(modelsav,model);
- if ((strcmp(model,"age")==0) || (strcmp(model,"age*age")==0)){
- printf("Error. Non available option model=%s ",model);
- fprintf(ficlog,"Error. Non available option model=%s ",model);
- goto end;
+ if (strstr(model,"AGE") !=0){
+ printf("Error. AGE must be in lower case 'age' model=%s ",model);
+ fprintf(ficlog,"Error. AGE must be in lower case model=%s ",model);fflush(ficlog);
+ return 1;
}
/* This loop fills the array Tvar from the string 'model'.*/
-
- for(i=(j+1); i>=1;i--){
- cutv(stra,strb,modelsav,'+'); /* keeps in strb after the last + */
+ /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
+ /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
+ /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[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) */
+ /* k=5 Tvar[5] */
+ /* for (k=1; k<=cptcovn;k++) { */
+ /* 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=cptcovn; k>=1;k--){
+ 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 */
/* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
/*scanf("%d",i);*/
- if (strchr(strb,'*')) { /* Model includes a product */
- cutv(strd,strc,strb,'*'); /* strd*strc Vm*Vn (if not *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 */
if (strcmp(strc,"age")==0) { /* Vn*age */
cptcovprod--;
- cutv(strb,stre,strd,'V');
- Tvar[i]=atoi(stre); /* computes n in Vn and stores in Tvar*/
- cptcovage++;
- Tage[cptcovage]=i;
- /*printf("stre=%s ", stre);*/
- }
- else if (strcmp(strd,"age")==0) { /* or age*Vn */
+ cutv(strb,stre,strd,'V'); /* stre="V3" */
+ 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 */
+ Tage[cptcovage]=k; /* Tage[1] = 4 */
+ /*printf("stre=%s ", stre);*/
+ } else if (strcmp(strd,"age")==0) { /* or age*Vn */
cptcovprod--;
cutv(strb,stre,strc,'V');
- Tvar[i]=atoi(stre);
+ Tvar[k]=atoi(stre);
cptcovage++;
- Tage[cptcovage]=i;
- }
- else { /* Age is not in the model */
- cutv(strb,stre,strc,'V'); /* strc= Vn, stre is n*/
- Tvar[i]=ncovcol+k1;
+ Tage[cptcovage]=k;
+ } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*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=*/
+ Tvar[k]=ncovcol+k1; /* 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+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 */
- Tprod[k1]=i;
- Tvard[k1][1]=atoi(strc); /* m*/
- Tvard[k1][2]=atoi(stre); /* n */
- Tvar[cptcovn+k2]=Tvard[k1][1];
- Tvar[cptcovn+k2+1]=Tvard[k1][2];
- for (k=1; k<=lastobs;k++)
- covar[ncovcol+k1][k]=covar[atoi(stre)][k]*covar[atoi(strc)][k];
+ Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
+ Tvard[k1][1]=atoi(strc); /* m 1 for V1*/
+ Tvard[k1][2]=atoi(stre); /* n 4 for V4*/
+ Tvar[cptcovn+k2]=Tvard[k1][1]; /* Tvar[(cptcovn=4+k2=1)=5]= 1 (V1) */
+ 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] and stores it at ncovol+k1 */
+ covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
+ }
k1++;
k2=k2+2;
- }
- }
+ } /* End age is not in the model */
+ } /* End if model includes a product */
else { /* no more sum */
/*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
/* scanf("%d",i);*/
- cutv(strd,strc,strb,'V');
- Tvar[i]=atoi(strc);
+ cutv(strd,strc,strb,'V');
+ Tvar[k]=atoi(strc);
}
- strcpy(modelsav,stra);
+ strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
/*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
scanf("%d",i);*/
} /* end of loop + */
@@ -4110,15 +4834,18 @@ int main(int argc, char *argv[])
printf("cptcovprod=%d ", cptcovprod);
fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
- scanf("%d ",i);
- fclose(fic);*/
+ scanf("%d ",i);*/
- /* if(mle==1){*/
- if (weightopt != 1) { /* Maximisation without weights*/
- for(i=1;i<=n;i++) weight[i]=1.0;
- }
- /*-calculation of age at interview from date of interview and age at death -*/
- agev=matrix(1,maxwav,1,imx);
+
+ return (0); /* with covar[new additional covariate if product] and Tage if age */
+ endread:
+ printf("Exiting decodemodel: ");
+ return (1);
+}
+
+calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
+{
+ int i, m;
for (i=1; i<=imx; i++) {
for(m=2; (m<= maxwav); m++) {
@@ -4127,11 +4854,13 @@ int main(int argc, char *argv[])
s[m][i]=-1;
}
if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
+ *nberr++;
printf("Error! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results are biased\n",(int)moisdc[i],(int)andc[i],num[i],i);
fprintf(ficlog,"Error! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results are biased\n",(int)moisdc[i],(int)andc[i],num[i],i);
s[m][i]=-1;
}
if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
+ *nberr++;
printf("Error! Month of death of individual %ld on line %d was unknown %2d, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,(int)moisdc[i]);
fprintf(ficlog,"Error! Month of death of individual %ld on line %d was unknown %f, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,moisdc[i]);
s[m][i]=-1; /* We prefer to skip it (and to skip it in version 0.8a1 too */
@@ -4142,7 +4871,7 @@ int main(int argc, char *argv[])
for (i=1; i<=imx; i++) {
agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
for(m=firstpass; (m<= lastpass); m++){
- if(s[m][i] >0){
+ if(s[m][i] >0 || s[m][i]==-2 || s[m][i]==-4 || s[m][i]==-5){
if (s[m][i] >= nlstate+1) {
if(agedc[i]>0)
if((int)moisdc[i]!=99 && (int)andc[i]!=9999)
@@ -4150,6 +4879,7 @@ int main(int argc, char *argv[])
/*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
else {
if ((int)andc[i]!=9999){
+ nbwarn++;
printf("Warning negative age at death: %ld line:%d\n",num[i],i);
fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
agev[m][i]=-1;
@@ -4157,17 +4887,16 @@ int main(int argc, char *argv[])
}
}
else if(s[m][i] !=9){ /* Standard case, age in fractional
- years but with the precision of a
- month */
+ years but with the precision of a month */
agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
agev[m][i]=1;
- else if(agev[m][i]
%s \
+
\n\
+Title=%s
Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s
\n",\
+ optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
}
- fprintf(fichtm," %s
%s \
+ fprintf(fichtm,"\n
%s \
\n\
Title=%s
Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s
\n\
\n\
\
\n",\
- version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt,\
- model,fileres,fileres,\
- filelog,filelog,optionfilegnuplot,optionfilegnuplot,strt);
- fclose(fichtm);
-
+ optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
+ optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
+ fileres,fileres,\
+ filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
+ fflush(fichtm);
+
+ strcpy(pathr,path);
+ strcat(pathr,optionfilefiname);
+ chdir(optionfilefiname); /* Move to directory named optionfile */
+
/* Calculates basic frequencies. Computes observed prevalence at single age
and prints on file fileres'p'. */
- freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint);
+ freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart);
- if(fileappend(fichtm, optionfilehtm)){
- fprintf(fichtm,"Parameter files
\n\
+ - Parameter file: %s.%s
\n\
- Copy of the parameter file: o%s
\n\
- Log file of the run: %s
\n\
- - Gnuplot file name: %s\n\
+ - Gnuplot file name: %s
\n\
- Date and time at start: %s
Total number of observations=%d
\n\
+ fprintf(fichtm,"\n");
+ fprintf(fichtm,"
Total number of observations=%d
\n\
Youngest age at first (selected) pass %.2f, oldest age %.2f
\n\
Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf
\n",\
- imx,agemin,agemax,jmin,jmax,jmean);
- fclose(fichtm);
- }
- pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
+ imx,agemin,agemax,jmin,jmax,jmean);
+ pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
-
- /* For Powell, parameters are in a vector p[] starting at p[1]
- so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
- p=param[1][1]; /* *(*(*(param +1)+1)+0) */
+
+ /* For Powell, parameters are in a vector p[] starting at p[1]
+ so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
+ p=param[1][1]; /* *(*(*(param +1)+1)+0) */
+
+ globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
+
+ if (mle==-3){
+ ximort=matrix(1,NDIM,1,NDIM);
+/* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
+ cens=ivector(1,n);
+ ageexmed=vector(1,n);
+ agecens=vector(1,n);
+ dcwave=ivector(1,n);
+
+ for (i=1; i<=imx; i++){
+ dcwave[i]=-1;
+ for (m=firstpass; m<=lastpass; m++)
+ if (s[m][i]>nlstate) {
+ dcwave[i]=m;
+ /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
+ break;
+ }
+ }
+
+ for (i=1; i<=imx; i++) {
+ if (wav[i]>0){
+ ageexmed[i]=agev[mw[1][i]][i];
+ j=wav[i];
+ agecens[i]=1.;
+
+ if (ageexmed[i]> 1 && wav[i] > 0){
+ agecens[i]=agev[mw[j][i]][i];
+ cens[i]= 1;
+ }else if (ageexmed[i]< 1)
+ cens[i]= -1;
+ if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
+ cens[i]=0 ;
+ }
+ else cens[i]=-1;
+ }
+
+ for (i=1;i<=NDIM;i++) {
+ for (j=1;j<=NDIM;j++)
+ ximort[i][j]=(i == j ? 1.0 : 0.0);
+ }
+
+ p[1]=0.0268; p[NDIM]=0.083;
+ /*printf("%lf %lf", p[1], p[2]);*/
+
+
+#ifdef GSL
+ printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
+#elsedef
+ printf("Powell\n"); fprintf(ficlog,"Powell\n");
+#endif
+ strcpy(filerespow,"pow-mort");
+ strcat(filerespow,fileres);
+ if((ficrespow=fopen(filerespow,"w"))==NULL) {
+ printf("Problem with resultfile: %s\n", filerespow);
+ fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
+ }
+#ifdef GSL
+ fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
+#elsedef
+ fprintf(ficrespow,"# Powell\n# iter -2*LL");
+#endif
+ /* for (i=1;i<=nlstate;i++)
+ for(j=1;j<=nlstate+ndeath;j++)
+ if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
+ */
+ fprintf(ficrespow,"\n");
+#ifdef GSL
+ /* gsl starts here */
+ T = gsl_multimin_fminimizer_nmsimplex;
+ gsl_multimin_fminimizer *sfm = NULL;
+ gsl_vector *ss, *x;
+ gsl_multimin_function minex_func;
+
+ /* Initial vertex size vector */
+ ss = gsl_vector_alloc (NDIM);
+
+ if (ss == NULL){
+ GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
+ }
+ /* Set all step sizes to 1 */
+ gsl_vector_set_all (ss, 0.001);
- globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
- likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
- printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
- for (k=1; k<=npar;k++)
- printf(" %d %8.5f",k,p[k]);
- printf("\n");
- globpr=1; /* to print the contributions */
- likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
- printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
- for (k=1; k<=npar;k++)
- printf(" %d %8.5f",k,p[k]);
- printf("\n");
- if(mle>=1){ /* Could be 1 or 2 */
- mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
- }
+ /* Starting point */
+
+ x = gsl_vector_alloc (NDIM);
- /*--------- results files --------------*/
- fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle= 0 weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate, ndeath, maxwav, weightopt,model);
+ if (x == NULL){
+ gsl_vector_free(ss);
+ GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
+ }
+ /* Initialize method and iterate */
+ /* p[1]=0.0268; p[NDIM]=0.083; */
+/* gsl_vector_set(x, 0, 0.0268); */
+/* gsl_vector_set(x, 1, 0.083); */
+ gsl_vector_set(x, 0, p[1]);
+ gsl_vector_set(x, 1, p[2]);
+
+ minex_func.f = &gompertz_f;
+ minex_func.n = NDIM;
+ minex_func.params = (void *)&p; /* ??? */
+
+ sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
+ gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
+
+ printf("Iterations beginning .....\n\n");
+ printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
- jk=1;
- fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
- printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
- fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
- for(i=1,jk=1; i <=nlstate; i++){
- for(k=1; k <=(nlstate+ndeath); k++){
- if (k != i)
- {
+ iteri=0;
+ while (rval == GSL_CONTINUE){
+ iteri++;
+ status = gsl_multimin_fminimizer_iterate(sfm);
+
+ if (status) printf("error: %s\n", gsl_strerror (status));
+ fflush(0);
+
+ if (status)
+ break;
+
+ rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
+ ssval = gsl_multimin_fminimizer_size (sfm);
+
+ if (rval == GSL_SUCCESS)
+ printf ("converged to a local maximum at\n");
+
+ printf("%5d ", iteri);
+ for (it = 0; it < NDIM; it++){
+ printf ("%10.5f ", gsl_vector_get (sfm->x, it));
+ }
+ printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
+ }
+
+ printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
+
+ gsl_vector_free(x); /* initial values */
+ gsl_vector_free(ss); /* inital step size */
+ for (it=0; it
Localtime at start %s and at end=%s
",strt, strtend);
- fclose(fichtm);
- }
+/* if(fileappend(fichtm,optionfilehtm)){ */
+ fprintf(fichtm,"
Local time at start %s
Local time at end %s
\n",strstart, strtend);
+ fclose(fichtm);
+ fprintf(fichtmcov,"
Local time at start %s
Local time at end %s
\n",strstart, strtend);
+ fclose(fichtmcov);
+ fclose(ficgp);
+ fclose(ficlog);
/*------ End -----------*/
- end:
-#ifdef windows
- /* chdir(pathcd);*/
-#endif
- /*system("wgnuplot graph.plt");*/
- /*system("../gp37mgw/wgnuplot graph.plt");*/
- /*system("cd ../gp37mgw");*/
- /* system("..\\gp37mgw\\wgnuplot graph.plt");*/
- strcpy(plotcmd,GNUPLOTPROGRAM);
- strcat(plotcmd," ");
- strcat(plotcmd,optionfilegnuplot);
- printf("Starting graphs with: %s",plotcmd);fflush(stdout);
- system(plotcmd);
- printf(" Wait...");
- /*#ifdef windows*/
+ printf("Before Current directory %s!\n",pathcd);
+ if(chdir(pathcd) != 0)
+ printf("Can't move to directory %s!\n",path);
+ if(getcwd(pathcd,MAXLINE) > 0)
+ printf("Current directory %s!\n",pathcd);
+ /*strcat(plotcmd,CHARSEPARATOR);*/
+ sprintf(plotcmd,"gnuplot");
+#ifndef UNIX
+ sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
+#endif
+ if(!stat(plotcmd,&info)){
+ printf("Error gnuplot program not found: %s\n",plotcmd);fflush(stdout);
+ if(!stat(getenv("GNUPLOTBIN"),&info)){
+ printf("Error gnuplot program not found: %s Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
+ }else
+ strcpy(pplotcmd,plotcmd);
+#ifdef UNIX
+ strcpy(plotcmd,GNUPLOTPROGRAM);
+ if(!stat(plotcmd,&info)){
+ printf("Error gnuplot program not found: %s\n",plotcmd);fflush(stdout);
+ }else
+ strcpy(pplotcmd,plotcmd);
+#endif
+ }else
+ strcpy(pplotcmd,plotcmd);
+
+ sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
+ printf("Starting graphs with: %s\n",plotcmd);fflush(stdout);
+
+ if((outcmd=system(plotcmd)) != 0){
+ printf("\n Problem with gnuplot\n");
+ }
+ printf(" Wait...");
while (z[0] != 'q') {
/* chdir(path); */
- printf("\nType e to edit output files, g to graph again, c to start again, and q for exiting: ");
+ printf("\nType e to edit output files, g to graph again and q for exiting: ");
scanf("%s",z);
- if (z[0] == 'c') system("./imach");
- else if (z[0] == 'e') system(optionfilehtm);
+/* if (z[0] == 'c') system("./imach"); */
+ if (z[0] == 'e') {
+ printf("Starting browser with: %s",optionfilehtm);fflush(stdout);
+ system(optionfilehtm);
+ }
else if (z[0] == 'g') system(plotcmd);
else if (z[0] == 'q') exit(0);
}
- /*#endif */
+ end:
+ while (z[0] != 'q') {
+ printf("\nType q for exiting: ");
+ scanf("%s",z);
+ }
}
+