--- imach/src/imach.c 2001/03/13 18:10:26 1.2 +++ imach/src/imach.c 2006/04/04 15:20:31 1.125 @@ -1,37 +1,273 @@ - -/*********************** Imach ************************************** - This program computes Healthy Life Expectancies from cross-longitudinal - data. Cross-longitudinal consist in a first survey ("cross") where - individuals from different ages are interviewed on their health status - or degree of disability. At least a second wave of interviews - ("longitudinal") should measure each new individual health status. - Health expectancies are computed from the transistions observed between - waves and are computed for each degree of severity of disability (number - of life states). More degrees you consider, more time is necessary to - reach the Maximum Likelihood of the parameters involved in the model. - The simplest model is the multinomial logistic model where pij is - the probabibility to be observed in state j at the second wave conditional - to be observed in state i at the first wave. Therefore the model is: - log(pij/pii)= aij + bij*age+ cij*sex + etc , where 'age' is age and 'sex' - is a covariate. If you want to have a more complex model than "constant and - age", you should modify the program where the markup - *Covariates have to be included here again* invites you to do it. - More covariates you add, less is the speed of the convergence. - - The advantage that this computer programme claims, comes from that if the - delay between waves is not identical for each individual, or if some - individual missed an interview, the information is not rounded or lost, but - taken into account using an interpolation or extrapolation. - hPijx is the probability to be - observed in state i at age x+h conditional to the observed state i at age - x. The delay 'h' can be split into an exact number (nh*stepm) of - unobserved intermediate states. This elementary transition (by month or - quarter trimester, semester or year) is model as a multinomial logistic. - The hPx matrix is simply the matrix product of nh*stepm elementary matrices - and the contribution of each individual to the likelihood is simply hPijx. +/* $Id: imach.c,v 1.125 2006/04/04 15:20:31 lievre Exp $ + $State: Exp $ + $Log: imach.c,v $ + 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]);
}
}
-
/********************** nrerror ********************/
void nrerror(char error_text[])
{
fprintf(stderr,"ERREUR ...\n");
fprintf(stderr,"%s\n",error_text);
- exit(1);
+ exit(EXIT_FAILURE);
}
/*********************** vector *******************/
double *vector(int nl, int nh)
@@ -223,6 +637,21 @@ void free_ivector(int *v, long nl, long
free((FREE_ARG)(v+nl-NR_END));
}
+/************************lvector *******************************/
+long *lvector(long nl,long nh)
+{
+ long *v;
+ v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
+ if (!v) nrerror("allocation failure in ivector");
+ return v-nl+NR_END;
+}
+
+/******************free lvector **************************/
+void free_lvector(long *v, long nl, long nh)
+{
+ free((FREE_ARG)(v+nl-NR_END));
+}
+
/******************* imatrix *******************************/
int **imatrix(long nrl, long nrh, long ncl, long nch)
/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
@@ -277,6 +706,8 @@ double **matrix(long nrl, long nrh, long
for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
return m;
+ /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1])
+ */
}
/*************************free matrix ************************/
@@ -316,7 +747,10 @@ double ***ma3x(long nrl, long nrh, long
for (j=ncl+1; j<=nch; j++)
m[i][j]=m[i][j-1]+nlay;
}
- return m;
+ return m;
+ /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
+ &(m[i][j][k]) <=> *((*(m+i) + j)+k)
+ */
}
/*************************free ma3x ************************/
@@ -327,6 +761,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;
@@ -364,8 +833,10 @@ double brent(double ax, double bx, doubl
tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
/* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
printf(".");fflush(stdout);
+ fprintf(ficlog,".");fflush(ficlog);
#ifdef DEBUG
printf("br %d,x=%.10e xm=%.10e b=%.10e a=%.10e tol=%.10e tol1=%.10e tol2=%.10e x-xm=%.10e fx=%.12e fu=%.12e,fw=%.12e,ftemp=%.12e,ftol=%.12e\n",iter,x,xm,b,a,tol,tol1,tol2,(x-xm),fx,fu,fw,ftemp,ftol);
+ fprintf(ficlog,"br %d,x=%.10e xm=%.10e b=%.10e a=%.10e tol=%.10e tol1=%.10e tol2=%.10e x-xm=%.10e fx=%.12e fu=%.12e,fw=%.12e,ftemp=%.12e,ftol=%.12e\n",iter,x,xm,b,a,tol,tol1,tol2,(x-xm),fx,fu,fw,ftemp,ftol);
/* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
#endif
if (fabs(x-xm) <= (tol2-0.5*(b-a))){
@@ -490,6 +961,7 @@ void linmin(double p[], double xi[], int
*fret=brent(ax,xx,bx,f1dim,TOL,&xmin);
#ifdef DEBUG
printf("retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
+ fprintf(ficlog,"retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
#endif
for (j=1;j<=n;j++) {
xi[j] *= xmin;
@@ -499,6 +971,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 []))
@@ -509,6 +994,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);
@@ -519,17 +1006,50 @@ void powell(double p[], double **xi, int
fp=(*fret);
ibig=0;
del=0.0;
- printf("\nPowell iter=%d -2*LL=%.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");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);
#ifdef DEBUG
printf("fret=%lf \n",*fret);
+ fprintf(ficlog,"fret=%lf \n",*fret);
#endif
printf("%d",i);fflush(stdout);
+ fprintf(ficlog,"%d",i);fflush(ficlog);
linmin(p,xit,n,fret,func);
if (fabs(fptt-(*fret)) > del) {
del=fabs(fptt-(*fret));
@@ -537,13 +1057,18 @@ void powell(double p[], double **xi, int
}
#ifdef DEBUG
printf("%d %.12e",i,(*fret));
+ fprintf(ficlog,"%d %.12e",i,(*fret));
for (j=1;j<=n;j++) {
xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
printf(" x(%d)=%.12e",j,xit[j]);
+ fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
}
- for(j=1;j<=n;j++)
+ for(j=1;j<=n;j++) {
printf(" p=%.12e",p[j]);
+ fprintf(ficlog," p=%.12e",p[j]);
+ }
printf("\n");
+ fprintf(ficlog,"\n");
#endif
}
if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) {
@@ -552,15 +1077,21 @@ void powell(double p[], double **xi, int
k[0]=1;
k[1]=-1;
printf("Max: %.12e",(*func)(p));
- for (j=1;j<=n;j++)
+ fprintf(ficlog,"Max: %.12e",(*func)(p));
+ for (j=1;j<=n;j++) {
printf(" %.12e",p[j]);
+ fprintf(ficlog," %.12e",p[j]);
+ }
printf("\n");
+ fprintf(ficlog,"\n");
for(l=0;l<=1;l++) {
for (j=1;j<=n;j++) {
ptt[j]=p[j]+(p[j]-pt[j])*k[l];
printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
+ fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
}
printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
+ fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
}
#endif
@@ -588,16 +1119,20 @@ void powell(double p[], double **xi, int
}
#ifdef DEBUG
printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
- for(j=1;j<=n;j++)
+ fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
+ for(j=1;j<=n;j++){
printf(" %.12e",xit[j]);
+ fprintf(ficlog," %.12e",xit[j]);
+ }
printf("\n");
+ fprintf(ficlog,"\n");
#endif
- }
+ }
}
}
}
-/**** Prevalence limit ****************/
+/**** Prevalence limit (stable or period prevalence) ****************/
double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int ij)
{
@@ -615,15 +1150,26 @@ double **prevalim(double **prlim, int nl
for (j=1;j<=nlstate+ndeath;j++){
oldm[ii][j]=(ii==j ? 1.0 : 0.0);
}
- /* Even if hstepm = 1, at least one multiplication by the unit matrix */
+
+ cov[1]=1.;
+
+ /* Even if hstepm = 1, at least one multiplication by the unit matrix */
for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
newm=savm;
/* Covariates have to be included here again */
- cov[1]=1.;
- cov[2]=agefin;
- if (cptcovn>0){
- for (k=1; k<=cptcovn;k++) {cov[2+k]=nbcode[Tvar[k]][codtab[ij][k]];/*printf("Tcode[ij]=%d nbcode=%d\n",Tcode[ij],nbcode[k][Tcode[ij]]);*/}
- }
+ 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;
@@ -648,7 +1194,7 @@ double **prevalim(double **prlim, int nl
}
}
-/*************** transition probabilities **********/
+/*************** transition probabilities ***************/
double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
{
@@ -657,54 +1203,57 @@ double **pmij(double **ps, double *cov,
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;
- }
- }
- for(i=1; i<= nlstate; i++){
- s1=0;
- for(j=1; ji s1=%.17e, s2=%.17e %lx %lx\n",s1,s2,s1,s2); */
+ }
+ ps[i][j]=s2;
+ }
}
- }
-
- /* for(ii=1; ii<= nlstate+ndeath; ii++){
- for(jj=1; jj<= nlstate+ndeath; jj++){
- printf("%lf ",ps[ii][jj]);
- }
- printf("\n ");
+ /*ps[3][2]=1;*/
+
+ for(i=1; i<= nlstate; i++){
+ s1=0;
+ for(j=1; j0){
- for (k=1; k<=cptcovn;k++) cov[2+k]=nbcode[Tvar[k]][codtab[ij][k]];
- }
+ 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=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("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
/*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
@@ -782,72 +1337,416 @@ double ***hpxij(double ***po, int nhstep
/*************** log-likelihood *************/
double func( double *x)
{
- int i, ii, j, k, mi, d;
+ int i, ii, j, k, mi, d, kk;
double l, ll[NLSTATEMAX], cov[NCOVMAX];
double **out;
double sw; /* Sum of weights */
double lli; /* Individual log likelihood */
+ int s1, s2;
+ double bbh, survp;
long ipmx;
/*extern weight */
/* We are differentiating ll according to initial status */
/* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
/*for(i=1;i ");
+
+ m=cptcoveff;
+ if (cptcovn < 1) {m=1;ncodemax[1]=1;}
+
+ jj1=0;
+ for(k1=1; k1<=m;k1++){
+ for(i1=1; i1<=ncodemax[k1];i1++){
+ jj1++;
+ if (cptcovn > 0) {
+ fprintf(fichtm," ");
+
+ m=cptcoveff;
+ if (cptcovn < 1) {m=1;ncodemax[1]=1;}
+
+ jj1=0;
+ for(k1=1; k1<=m;k1++){
+ for(i1=1; i1<=ncodemax[k1];i1++){
+ jj1++;
+ if (cptcovn > 0) {
+ fprintf(fichtm," ");
-
- m=cptcovn;
- if (cptcovn < 1) {m=1;ncodemax[1]=1;}
- j1=0;
- for(k1=1; k1<=m;k1++){
- for(i1=1; i1<=ncodemax[k1];i1++){
- j1++;
- if (cptcovn > 0) {
- fprintf(fichtm,"
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 []))
{
int i,j, iter;
- double **xi,*delti;
+ double **xi;
double fret;
+ double fretone; /* Only one call to likelihood */
+ /* char filerespow[FILENAMELENGTH];*/
xi=matrix(1,npar,1,npar);
for (i=1;i<=npar;i++)
for (j=1;j<=npar;j++)
xi[i][j]=(i==j ? 1.0 : 0.0);
- printf("Powell\n");
+ printf("Powell\n"); fprintf(ficlog,"Powell\n");
+ strcpy(filerespow,"pow");
+ strcat(filerespow,fileres);
+ if((ficrespow=fopen(filerespow,"w"))==NULL) {
+ printf("Problem with resultfile: %s\n", filerespow);
+ fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
+ }
+ fprintf(ficrespow,"# Powell\n# iter -2*LL");
+ 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");
+
powell(p,xi,npar,ftol,&iter,&fret,func);
- printf("\n#Number of iterations = %d, -2 Log likelihood = %.12f\n",iter,func(p));
- fprintf(ficres,"#Number of iterations = %d, -2 Log likelihood = %.12f ",iter,func(p));
+ 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));
+ fprintf(ficres,"#Number of iterations = %d, -2 Log likelihood = %.12f \n",iter,func(p));
}
@@ -859,33 +1758,42 @@ 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");
+ fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
for (i=1;i<=npar;i++){
printf("%d",i);fflush(stdout);
- hess[i][i]=hessii(p,ftolhess,i,delti);
- /*printf(" %f ",p[i]);*/
+ fprintf(ficlog,"%d",i);fflush(ficlog);
+
+ 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++) {
for (j=1;j<=npar;j++) {
if (j>i) {
printf(".%d%d",i,j);fflush(stdout);
- hess[i][j]=hessij(p,delti,i,j);
- hess[j][i]=hess[i][j];
+ fprintf(ficlog,".%d%d",i,j);fflush(ficlog);
+ hess[i][j]=hessij(p,delti,i,j,func,npar);
+
+ hess[j][i]=hess[i][j];
+ /*printf(" %lf ",hess[i][j]);*/
}
}
}
printf("\n");
+ fprintf(ficlog,"\n");
printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
+ fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
a=matrix(1,npar,1,npar);
y=matrix(1,npar,1,npar);
@@ -905,11 +1813,14 @@ void hesscov(double **matcov, double p[]
}
printf("\n#Hessian matrix#\n");
+ fprintf(ficlog,"\n#Hessian matrix#\n");
for (i=1;i<=npar;i++) {
for (j=1;j<=npar;j++) {
printf("%.3e ",hess[i][j]);
+ fprintf(ficlog,"%.3e ",hess[i][j]);
}
printf("\n");
+ fprintf(ficlog,"\n");
}
/* Recompute Inverse */
@@ -926,8 +1837,10 @@ void hesscov(double **matcov, double p[]
for (i=1;i<=npar;i++){
y[i][j]=x[i];
printf("%.3e ",y[i][j]);
+ fprintf(ficlog,"%.3e ",y[i][j]);
}
printf("\n");
+ fprintf(ficlog,"\n");
}
*/
@@ -941,14 +1854,14 @@ void hesscov(double **matcov, double p[]
}
/*************** hessian matrix ****************/
-double hessii( double x[], double delta, int theta, double delti[])
+double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
{
int i;
int l=1, lmax=20;
double k1,k2;
double p2[NPARMAX+1];
double res;
- double delt, delts, nkhi=10.,nkhif=1., khi=1.e-4;
+ double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
double fx;
int k=0,kmax=10;
double l1;
@@ -969,6 +1882,7 @@ double hessii( double x[], double delta,
#ifdef DEBUG
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
/*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
if((k1 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);
+ pstamp(ficresvij);
+ fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
+ if(popbased==1)
+ fprintf(ficresvij,"the age specific prevalence observed in the population i.e cross-sectionally\n in each health state (popbased=1)");
+ 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);
dnewm=matrix(1,nlstate,1,npar);
doldm=matrix(1,nlstate,1,nlstate);
+ dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
+ doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
+
+ gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
+ gpp=vector(nlstate+1,nlstate+ndeath);
+ gmp=vector(nlstate+1,nlstate+ndeath);
+ trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
- hstepm=1*YEARM; /* Every year of age */
- hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
+ if(estepm < stepm){
+ printf ("Problem %d lower than %d\n",estepm, stepm);
+ }
+ else hstepm=estepm;
+ /* For example we decided to compute the life expectancy with the smallest unit */
+ /* 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 */
+ /* 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
+ you sum them up and add 1 year (area under the trapezoids) you won't get the same
+ results. So we changed our mind and took the option of the best precision.
+ */
+ hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
agelim = AGESUP;
for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
- nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
- if (stepm >= YEARM) hstepm=1;
- nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
+ nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
+ nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
gp=matrix(0,nhstepm,1,nlstate);
gm=matrix(0,nhstepm,1,nlstate);
+
for(theta=1; theta <=npar; theta++){
- for(i=1; i<=npar; i++){ /* Computes gradient */
+ for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
xp[i] = x[i] + (i==theta ?delti[theta]:0);
}
hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
+
+ if (popbased==1) {
+ if(mobilav ==0){
+ for(i=1; i<=nlstate;i++)
+ prlim[i][i]=probs[(int)age][i][ij];
+ }else{ /* mobilav */
+ for(i=1; i<=nlstate;i++)
+ prlim[i][i]=mobaverage[(int)age][i][ij];
+ }
+ }
+
for(j=1; j<= nlstate; j++){
for(h=0; h<=nhstepm; h++){
for(i=1, gp[h][j]=0.;i<=nlstate;i++)
gp[h][j] += prlim[i][i]*p3mat[i][j][h];
}
}
-
- for(i=1; i<=npar; i++) /* Computes gradient */
+ /* This for computing probability of death (h=1 means
+ computed over hstepm matrices product = hstepm*stepm months)
+ as a weighted average of prlim.
+ */
+ for(j=nlstate+1;j<=nlstate+ndeath;j++){
+ for(i=1,gpp[j]=0.; i<= nlstate; i++)
+ gpp[j] += prlim[i][i]*p3mat[i][j][1];
+ }
+ /* end probability of death */
+
+ for(i=1; i<=npar; i++) /* Computes gradient x - delta */
xp[i] = x[i] - (i==theta ?delti[theta]:0);
hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
+
+ if (popbased==1) {
+ if(mobilav ==0){
+ for(i=1; i<=nlstate;i++)
+ prlim[i][i]=probs[(int)age][i][ij];
+ }else{ /* mobilav */
+ for(i=1; i<=nlstate;i++)
+ prlim[i][i]=mobaverage[(int)age][i][ij];
+ }
+ }
+
for(j=1; j<= nlstate; 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];
}
}
- for(j=1; j<= nlstate; j++)
+ /* This for computing probability of death (h=1 means
+ computed over hstepm matrices product = hstepm*stepm months)
+ as a weighted average of prlim.
+ */
+ for(j=nlstate+1;j<=nlstate+ndeath;j++){
+ for(i=1,gmp[j]=0.; i<= nlstate; i++)
+ gmp[j] += prlim[i][i]*p3mat[i][j][1];
+ }
+ /* end probability of death */
+
+ for(j=1; j<= nlstate; j++) /* vareij */
for(h=0; h<=nhstepm; h++){
gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
}
+
+ for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu */
+ gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
+ }
+
} /* End theta */
- trgradg =ma3x(0,nhstepm,1,nlstate,1,npar);
+ trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
- for(h=0; h<=nhstepm; h++)
+ for(h=0; h<=nhstepm; h++) /* veij */
for(j=1; j<=nlstate;j++)
for(theta=1; theta <=npar; theta++)
trgradg[h][j][theta]=gradg[h][theta][j];
+ for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
+ for(theta=1; theta <=npar; theta++)
+ trgradgp[j][theta]=gradgp[theta][j];
+
+
+ hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
for(i=1;i<=nlstate;i++)
for(j=1;j<=nlstate;j++)
vareij[i][j][(int)age] =0.;
+
for(h=0;h<=nhstepm;h++){
for(k=0;k<=nhstepm;k++){
matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
for(i=1;i<=nlstate;i++)
for(j=1;j<=nlstate;j++)
- vareij[i][j][(int)age] += doldm[i][j];
+ vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
+ }
+ }
+
+ /* pptj */
+ matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
+ matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
+ for(j=nlstate+1;j<=nlstate+ndeath;j++)
+ for(i=nlstate+1;i<=nlstate+ndeath;i++)
+ varppt[j][i]=doldmp[j][i];
+ /* end ppptj */
+ /* x centered again */
+ hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij);
+ prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ij);
+
+ if (popbased==1) {
+ if(mobilav ==0){
+ for(i=1; i<=nlstate;i++)
+ prlim[i][i]=probs[(int)age][i][ij];
+ }else{ /* mobilav */
+ for(i=1; i<=nlstate;i++)
+ prlim[i][i]=mobaverage[(int)age][i][ij];
}
}
- h=1;
- if (stepm >= YEARM) h=stepm/YEARM;
+
+ /* This for computing probability of death (h=1 means
+ computed over hstepm (estepm) matrices product = hstepm*stepm months)
+ as a weighted average of prlim.
+ */
+ for(j=nlstate+1;j<=nlstate+ndeath;j++){
+ for(i=1,gmp[j]=0.;i<= nlstate; i++)
+ gmp[j] += prlim[i][i]*p3mat[i][j][1];
+ }
+ /* end probability of death */
+
+ fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
+ for(j=nlstate+1; j<=(nlstate+ndeath);j++){
+ fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
+ for(i=1; i<=nlstate;i++){
+ fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
+ }
+ }
+ fprintf(ficresprobmorprev,"\n");
+
fprintf(ficresvij,"%.0f ",age );
for(i=1; i<=nlstate;i++)
for(j=1; j<=nlstate;j++){
- fprintf(ficresvij," %.4f", h*vareij[i][j][(int)age]);
+ fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
}
fprintf(ficresvij,"\n");
free_matrix(gp,0,nhstepm,1,nlstate);
@@ -1457,18 +3038,43 @@ void varevsij(char fileres[], double ***
free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
} /* End age */
-
- free_vector(xp,1,npar);
- free_matrix(doldm,1,nlstate,1,npar);
- free_matrix(dnewm,1,nlstate,1,nlstate);
+ free_vector(gpp,nlstate+1,nlstate+ndeath);
+ 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");
+ /* 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 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 ",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 \"%s%s.png\";replot;\n",subdirf3(optionfilefiname,"varmuptjgr",digitp),digit);
-}
+ free_vector(xp,1,npar);
+ free_matrix(doldm,1,nlstate,1,nlstate);
+ free_matrix(dnewm,1,nlstate,1,npar);
+ free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
+ free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
+ 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);
+ 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);*/
+ /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
double **newm;
double **dnewm,**doldm;
int i, j, nhstepm, hstepm;
@@ -1478,8 +3084,9 @@ void varprevlim(char fileres[], double *
double **gradg, **trgradg;
double age,agelim;
int theta;
-
- fprintf(ficresvpl,"# Standard deviation of prevalences limit\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);
@@ -1547,41 +3154,1243 @@ void varprevlim(char fileres[], double *
}
-
-
-/***********************************************/
-/**************** Main Program *****************/
-/***********************************************/
-
-/*int main(int argc, char *argv[])*/
-int main()
+/************ 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, char strstart[])
{
+ int i, j=0, i1, k1, l1, t, tj;
+ int k2, l2, j1, z1;
+ int k=0,l, cptcode;
+ int first=1, first1;
+ double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
+ double **dnewm,**doldm;
+ double *xp;
+ double *gp, *gm;
+ double **gradg, **trgradg;
+ double **mu;
+ double age,agelim, cov[NCOVMAX];
+ double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
+ int theta;
+ char fileresprob[FILENAMELENGTH];
+ char fileresprobcov[FILENAMELENGTH];
+ char fileresprobcor[FILENAMELENGTH];
+
+ double ***varpij;
+
+ strcpy(fileresprob,"prob");
+ strcat(fileresprob,fileres);
+ if((ficresprob=fopen(fileresprob,"w"))==NULL) {
+ printf("Problem with resultfile: %s\n", fileresprob);
+ fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
+ }
+ strcpy(fileresprobcov,"probcov");
+ strcat(fileresprobcov,fileres);
+ if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
+ printf("Problem with resultfile: %s\n", fileresprobcov);
+ fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
+ }
+ strcpy(fileresprobcor,"probcor");
+ strcat(fileresprobcor,fileres);
+ if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
+ printf("Problem with resultfile: %s\n", fileresprobcor);
+ fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
+ }
+ printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
+ fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
+ printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
+ 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(ficresprobcor,"# Age");
- int i,j, k, n=MAXN,iter,m,size,cptcode, aaa, cptcod;
- double agedeb, agefin,hf;
- double agemin=1.e20, agemax=-1.e20;
- double fret;
- double **xi,tmp,delta;
+ for(i=1; i<=nlstate;i++)
+ for(j=1; j<=(nlstate+ndeath);j++){
+ fprintf(ficresprob," p%1d-%1d (SE)",i,j);
+ fprintf(ficresprobcov," p%1d-%1d ",i,j);
+ fprintf(ficresprobcor," p%1d-%1d ",i,j);
+ }
+ /* fprintf(ficresprob,"\n");
+ fprintf(ficresprobcov,"\n");
+ fprintf(ficresprobcor,"\n");
+ */
+ 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;
+ 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;
+ if (cptcovn<1) {tj=1;ncodemax[1]=1;}
+ j1=0;
+ for(t=1; t<=tj;t++){
+ for(i1=1; i1<=ncodemax[t];i1++){
+ j1++;
+ if (cptcovn>0) {
+ fprintf(ficresprob, "\n#********** Variable ");
+ for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
+ fprintf(ficresprob, "**********\n#\n");
+ fprintf(ficresprobcov, "\n#********** Variable ");
+ for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
+ fprintf(ficresprobcov, "**********\n#\n");
+
+ fprintf(ficgp, "\n#********** Variable ");
+ for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
+ fprintf(ficgp, "**********\n#\n");
+
+
+ fprintf(fichtmcov, "\n
********** Variable ");
+ for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
+ 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]]);
+ fprintf(ficresprobcor, "**********\n#");
+ }
+
+ for (age=bage; age<=fage; age ++){
+ cov[2]=age;
+ for (k=1; k<=cptcovn;k++) {
+ cov[2+k]=nbcode[Tvar[k]][codtab[j1][Tvar[k]]];
+ }
+ for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];
+ for (k=1; k<=cptcovprod;k++)
+ cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];
+
+ gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
+ trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
+ gp=vector(1,(nlstate)*(nlstate+ndeath));
+ gm=vector(1,(nlstate)*(nlstate+ndeath));
+
+ for(theta=1; theta <=npar; theta++){
+ for(i=1; i<=npar; i++)
+ xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
+
+ pmij(pmmij,cov,ncovmodel,xp,nlstate);
+
+ k=0;
+ for(i=1; i<= (nlstate); i++){
+ for(j=1; j<=(nlstate+ndeath);j++){
+ k=k+1;
+ gp[k]=pmmij[i][j];
+ }
+ }
+
+ for(i=1; i<=npar; i++)
+ xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
+
+ pmij(pmmij,cov,ncovmodel,xp,nlstate);
+ k=0;
+ for(i=1; i<=(nlstate); i++){
+ for(j=1; j<=(nlstate+ndeath);j++){
+ k=k+1;
+ gm[k]=pmmij[i][j];
+ }
+ }
+
+ for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
+ gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
+ }
- double dum; /* Dummy variable */
- double ***p3mat;
+ for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
+ for(theta=1; theta <=npar; theta++)
+ trgradg[j][theta]=gradg[theta][j];
+
+ matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
+ matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
+ free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
+ free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
+ free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
+ free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
+
+ pmij(pmmij,cov,ncovmodel,x,nlstate);
+
+ k=0;
+ for(i=1; i<=(nlstate); i++){
+ for(j=1; j<=(nlstate+ndeath);j++){
+ k=k+1;
+ mu[k][(int) age]=pmmij[i][j];
+ }
+ }
+ for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
+ for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
+ varpij[i][j][(int)age] = doldm[i][j];
+
+ /*printf("\n%d ",(int)age);
+ for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
+ printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
+ fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
+ }*/
+
+ fprintf(ficresprob,"\n%d ",(int)age);
+ fprintf(ficresprobcov,"\n%d ",(int)age);
+ fprintf(ficresprobcor,"\n%d ",(int)age);
+
+ for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
+ fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
+ for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
+ fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
+ fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
+ }
+ i=0;
+ for (k=1; k<=(nlstate);k++){
+ for (l=1; l<=(nlstate+ndeath);l++){
+ i=i++;
+ fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
+ fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
+ for (j=1; j<=i;j++){
+ fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
+ fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
+ }
+ }
+ }/* end of loop for state */
+ } /* end of loop for age */
+
+ /* Confidence intervalle of pij */
+ /*
+ fprintf(ficgp,"\nset noparametric;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);
+ fprintf(fichtm,"\n
",optionfilefiname);
+ fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
+ fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
+ */
+
+ /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
+ first1=1;
+ for (k2=1; k2<=(nlstate);k2++){
+ for (l2=1; l2<=(nlstate+ndeath);l2++){
+ if(l2==k2) continue;
+ j=(k2-1)*(nlstate+ndeath)+l2;
+ for (k1=1; k1<=(nlstate);k1++){
+ for (l1=1; l1<=(nlstate+ndeath);l1++){
+ if(l1==k1) continue;
+ i=(k1-1)*(nlstate+ndeath)+l1;
+ if(i<=j) continue;
+ for (age=bage; age<=fage; age ++){
+ if ((int)age %5==0){
+ v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
+ v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
+ cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
+ mu1=mu[i][(int) age]/stepm*YEARM ;
+ mu2=mu[j][(int) age]/stepm*YEARM;
+ c12=cv12/sqrt(v1*v2);
+ /* 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.;
+ /* Eigen vectors */
+ v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
+ /*v21=sqrt(1.-v11*v11); *//* error */
+ v21=(lc1-v1)/cv12*v11;
+ v12=-v21;
+ v22=v11;
+ tnalp=v21/v11;
+ if(first1==1){
+ first1=0;
+ printf("%d %d%d-%d%d mu %.4e %.4e Var %.4e %.4e cor %.3f cov %.4e Eig %.3e %.3e 1stv %.3f %.3f tang %.3f\nOthers in log...\n",(int) age,k1,l1,k2,l2,mu1,mu2,v1,v2,c12,cv12,lc1,lc2,v11,v21,tnalp);
+ }
+ fprintf(ficlog,"%d %d%d-%d%d mu %.4e %.4e Var %.4e %.4e cor %.3f cov %.4e Eig %.3e %.3e 1stv %.3f %.3f tan %.3f\n",(int) age,k1,l1,k2,l2,mu1,mu2,v1,v2,c12,cv12,lc1,lc2,v11,v21,tnalp);
+ /*printf(fignu*/
+ /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
+ /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
+ if(first==1){
+ first=0;
+ fprintf(ficgp,"\nset parametric;unset label");
+ fprintf(ficgp,"\nset log y;set log x; set xlabel \"p%1d%1d (year-1)\";set ylabel \"p%1d%1d (year-1)\"",k1,l1,k2,l2);
+ fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
+ fprintf(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",\
+ mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),\
+ mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
+ }else{
+ first=0;
+ 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",\
+ mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),\
+ mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
+ }/* if first */
+ } /* age mod 5 */
+ } /* end loop age */
+ fprintf(ficgp,"\nset out \"%s%d%1d%1d-%1d%1d.png\";replot;",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2);
+ first=1;
+ } /*l12 */
+ } /* k12 */
+ } /*l1 */
+ }/* k1 */
+ } /* loop covariates */
+ }
+ free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
+ free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
+ 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);
+ fflush(ficgp);
+ fflush(fichtmcov);
+}
+
+
+/******************* Printing html file ***********/
+void printinghtml(char fileres[], char title[], char datafile[], int firstpass, \
+ int lastpass, int stepm, int weightopt, char model[],\
+ int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
+ int popforecast, int estepm ,\
+ double jprev1, double mprev1,double anprev1, \
+ double jprev2, double mprev2,double anprev2){
+ int jj1, k1, i1, cpt;
+
+ fprintf(fichtm,"");
+ 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): %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, (b) health expectancies by health status at initial age: ei., eij . If one or more covariate are included, specific tables for each value of the covariate are output in sequences within the same file (estepm=%2d months): \
+ %s
\n",
+ estepm,subdirf2(fileres,"e"),subdirf2(fileres,"e"));
+ fprintf(fichtm,"\
+ - Population projections by age and states: \
+ %s
\n
");
+
+
+ fprintf(fichtm,"\
+\n
************ Results for covariates");
+ for (cpt=1; cpt<=cptcoveff;cpt++)
+ fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtab[jj1][cpt]]);
+ fprintf(fichtm," ************\n
");
+ }
+ /* Pij */
+ 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: %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
\
+",subdirf2(optionfilefiname,"p"),cpt,jj1,subdirf2(optionfilefiname,"p"),cpt,jj1,subdirf2(optionfilefiname,"p"),cpt,jj1);
+ }
+ for(cpt=1; cpt<=nlstate;cpt++) {
+ 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);
+ }
+ } /* end i1 */
+ }/* End k1 */
+ fprintf(fichtm," 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: %s
\n",
+ 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 */
+/* - Population forecasting (if popforecast=1): pop%s
\n */
+/*
",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); */
+ fflush(fichtm);
+ fprintf(fichtm,"
");
+ fflush(fichtm);
+}
+
+/******************* Gnuplot file **************/
+void printinggnuplot(char fileres[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
+
+ char dirfileres[132],optfileres[132];
+ 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); */
+/* } */
+
+ /*#ifdef windows */
+ fprintf(ficgp,"cd \"%s\" \n",pathc);
+ /*#endif */
+ 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 \"%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)");
+ }
+ 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,\"%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,\"%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 \"%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,"\"%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,"\"%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,"\"%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)");
+ }
+ if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l 0");
+ else fprintf(ficgp,"\" t\"\" w l 0,");
+ }
+ }
+
+ /*3eme*/
+
+ for (k1=1; k1<= m ; k1 ++) {
+ for (cpt=1; cpt<= nlstate ; 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);
+ 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);
+
+ */
+ for (i=1; i< nlstate ; i ++) {
+ 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);
+ }
+ }
+
+ /* CV preval stable (period) */
+ for (k1=1; k1<= m ; k1 ++) {
+ for (cpt=1; cpt<=nlstate ; cpt ++) {
+ k=3;
+ 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,",\"%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);
+ }
+ fprintf(ficgp,")) t\"prev(%d,%d)\" w l\n",cpt+1,cpt+1);
+ }
+ }
+
+ /* proba elementaires */
+ for(i=1,jk=1; i <=nlstate; i++){
+ for(k=1; k <=(nlstate+ndeath); k++){
+ if (k != i) {
+ for(j=1; j <=ncovmodel; j++){
+ fprintf(ficgp,"p%d=%f ",jk,p[jk]);
+ jk++;
+ fprintf(ficgp,"\n");
+ }
+ }
+ }
+ }
+
+ 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 \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"pe"),jk,ng);
+ if (ng==2)
+ fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
+ else
+ fprintf(ficgp,"\nset title \"Probability\"\n");
+ fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65\nset log y\nplot [%.f:%.f] ",ageminpar,agemaxpar);
+ i=1;
+ for(k2=1; k2<=nlstate; k2++) {
+ k3=i;
+ for(k=1; k<=(nlstate+ndeath); k++) {
+ if (k != k2){
+ if(ng==2)
+ fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
+ else
+ fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
+ ij=1;
+ for(j=3; j <=ncovmodel; j++) {
+ if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) {
+ fprintf(ficgp,"+p%d*%d*x",i+j-1,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);
+ ij++;
+ }
+ else
+ fprintf(ficgp,"+p%d*%d",i+j-1,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
+ }
+ fprintf(ficgp,")/(1");
+
+ for(k1=1; k1 <=nlstate; k1++){
+ fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
+ ij=1;
+ for(j=3; j <=ncovmodel; j++){
+ if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) {
+ fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);
+ ij++;
+ }
+ else
+ fprintf(ficgp,"+p%d*%d",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
+ }
+ fprintf(ficgp,")");
+ }
+ fprintf(ficgp,") t \"p%d%d\" ", k2,k);
+ if ((k+k2)!= (nlstate*2+ndeath)) fprintf(ficgp,",");
+ i=i+ncovmodel;
+ }
+ } /* end k */
+ } /* end k2 */
+ } /* end jk */
+ } /* end ng */
+ fflush(ficgp);
+} /* end gnuplot */
+
+
+/*************** Moving average **************/
+int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav){
+
+ int i, cpt, cptcod;
+ int modcovmax =1;
+ int mobilavrange, mob;
+ double age;
+
+ modcovmax=2*cptcoveff;/* Max number of modalities. We suppose
+ a covariate has 2 modalities */
+ if (cptcovn<1) modcovmax=1; /* At least 1 pass */
+
+ if(mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
+ if(mobilav==1) mobilavrange=5; /* default */
+ else mobilavrange=mobilav;
+ for (age=bage; age<=fage; age++)
+ for (i=1; i<=nlstate;i++)
+ for (cptcod=1;cptcod<=modcovmax;cptcod++)
+ mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
+ /* We keep the original values on the extreme ages bage, fage and for
+ fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
+ we use a 5 terms etc. until the borders are no more concerned.
+ */
+ for (mob=3;mob <=mobilavrange;mob=mob+2){
+ for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
+ for (i=1; i<=nlstate;i++){
+ for (cptcod=1;cptcod<=modcovmax;cptcod++){
+ mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
+ for (cpt=1;cpt<=(mob-1)/2;cpt++){
+ mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
+ mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
+ }
+ mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
+ }
+ }
+ }/* end age */
+ }/* end mob */
+ }else return -1;
+ return 0;
+}/* End movingaverage */
+
+
+/************** Forecasting ******************/
+prevforecast(char fileres[], double anproj1, double mproj1, double jproj1, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double bage, double fage, int firstpass, int lastpass, double anproj2, double p[], int cptcoveff){
+ /* proj1, year, month, day of starting projection
+ agemin, agemax range of age
+ dateprev1 dateprev2 range of dates during which prevalence is computed
+ anproj2 year of en of projection (same day and month as proj1).
+ */
+ int yearp, stepsize, hstepm, nhstepm, j, k, c, cptcod, i, h, i1;
+ int *popage;
+ double agec; /* generic age */
+ double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
+ double *popeffectif,*popcount;
+ double ***p3mat;
+ double ***mobaverage;
+ char fileresf[FILENAMELENGTH];
+
+ agelim=AGESUP;
+ prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
+
+ strcpy(fileresf,"f");
+ strcat(fileresf,fileres);
+ if((ficresf=fopen(fileresf,"w"))==NULL) {
+ printf("Problem with forecast resultfile: %s\n", fileresf);
+ fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
+ }
+ printf("Computing forecasting: result on file '%s' \n", fileresf);
+ fprintf(ficlog,"Computing forecasting: result on file '%s' \n", fileresf);
+
+ if (cptcoveff==0) ncodemax[cptcoveff]=1;
+
+ if (mobilav!=0) {
+ mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
+ if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){
+ fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
+ printf(" Error in movingaverage mobilav=%d\n",mobilav);
+ }
+ }
+
+ stepsize=(int) (stepm+YEARM-1)/YEARM;
+ if (stepm<=12) stepsize=1;
+ if(estepm < stepm){
+ printf ("Problem %d lower than %d\n",estepm, stepm);
+ }
+ else hstepm=estepm;
+
+ hstepm=hstepm/stepm;
+ yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp and
+ fractional in yp1 */
+ anprojmean=yp;
+ yp2=modf((yp1*12),&yp);
+ mprojmean=yp;
+ yp1=modf((yp2*30.5),&yp);
+ jprojmean=yp;
+ if(jprojmean==0) jprojmean=1;
+ if(mprojmean==0) jprojmean=1;
+
+ i1=cptcoveff;
+ if (cptcovn < 1){i1=1;}
+
+ fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2);
+
+ fprintf(ficresf,"#****** Routine prevforecast **\n");
+
+/* if (h==(int)(YEARM*yearp)){ */
+ for(cptcov=1, k=0;cptcov<=i1;cptcov++){
+ for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
+ k=k+1;
+ fprintf(ficresf,"\n#******");
+ for(j=1;j<=cptcoveff;j++) {
+ fprintf(ficresf," V%d=%d, hpijx=probability over h years, hp.jx is weighted by observed prev ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
+ }
+ fprintf(ficresf,"******\n");
+ fprintf(ficresf,"# Covariate valuofcovar yearproj age");
+ for(j=1; j<=nlstate+ndeath;j++){
+ for(i=1; i<=nlstate;i++)
+ fprintf(ficresf," p%d%d",i,j);
+ fprintf(ficresf," p.%d",j);
+ }
+ for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) {
+ fprintf(ficresf,"\n");
+ fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);
+
+ for (agec=fage; agec>=(ageminpar-1); agec--){
+ nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
+ nhstepm = nhstepm/hstepm;
+ p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
+ oldm=oldms;savm=savms;
+ hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k);
+
+ for (h=0; h<=nhstepm; h++){
+ if (h*hstepm/YEARM*stepm ==yearp) {
+ fprintf(ficresf,"\n");
+ for(j=1;j<=cptcoveff;j++)
+ fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
+ fprintf(ficresf,"%.f %.f ",anproj1+yearp,agec+h*hstepm/YEARM*stepm);
+ }
+ for(j=1; j<=nlstate+ndeath;j++) {
+ ppij=0.;
+ for(i=1; i<=nlstate;i++) {
+ if (mobilav==1)
+ ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod];
+ else {
+ ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod];
+ }
+ if (h*hstepm/YEARM*stepm== yearp) {
+ fprintf(ficresf," %.3f", p3mat[i][j][h]);
+ }
+ } /* end i */
+ if (h*hstepm/YEARM*stepm==yearp) {
+ fprintf(ficresf," %.3f", ppij);
+ }
+ }/* end j */
+ } /* end h */
+ free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
+ } /* end agec */
+ } /* end yearp */
+ } /* end cptcod */
+ } /* end cptcov */
+
+ if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
+
+ fclose(ficresf);
+}
+
+/************** Forecasting *****not tested NB*************/
+populforecast(char fileres[], double anpyram,double mpyram,double jpyram,double ageminpar, double agemax,double dateprev1, double dateprev2, int mobilav, double agedeb, double fage, int popforecast, char popfile[], double anpyram1,double p[], int i2){
+
+ int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h;
+ int *popage;
+ double calagedatem, agelim, kk1, kk2;
+ double *popeffectif,*popcount;
+ double ***p3mat,***tabpop,***tabpopprev;
+ double ***mobaverage;
+ char filerespop[FILENAMELENGTH];
+
+ tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
+ tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
+ agelim=AGESUP;
+ calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM;
+
+ prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
+
+
+ strcpy(filerespop,"pop");
+ strcat(filerespop,fileres);
+ if((ficrespop=fopen(filerespop,"w"))==NULL) {
+ printf("Problem with forecast resultfile: %s\n", filerespop);
+ fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop);
+ }
+ printf("Computing forecasting: result on file '%s' \n", filerespop);
+ fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop);
+
+ if (cptcoveff==0) ncodemax[cptcoveff]=1;
+
+ if (mobilav!=0) {
+ mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
+ if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){
+ fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
+ printf(" Error in movingaverage mobilav=%d\n",mobilav);
+ }
+ }
+
+ stepsize=(int) (stepm+YEARM-1)/YEARM;
+ if (stepm<=12) stepsize=1;
+
+ agelim=AGESUP;
+
+ hstepm=1;
+ hstepm=hstepm/stepm;
+
+ if (popforecast==1) {
+ if((ficpop=fopen(popfile,"r"))==NULL) {
+ printf("Problem with population file : %s\n",popfile);exit(0);
+ fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0);
+ }
+ popage=ivector(0,AGESUP);
+ popeffectif=vector(0,AGESUP);
+ popcount=vector(0,AGESUP);
+
+ i=1;
+ while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1;
+
+ imx=i;
+ for (i=1; i
************ Results for covariates");
+ for (cpt=1; cpt<=cptcoveff;cpt++)
+ fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtab[jj1][cpt]]);
+ fprintf(fichtm," ************\n
");
+ }
+ for(cpt=1; cpt<=nlstate;cpt++) {
+ 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): %s%d.png
\
+",subdirf2(optionfilefiname,"e"),jj1,subdirf2(optionfilefiname,"e"),jj1);
+ } /* end i1 */
+ }/* End k1 */
+ fprintf(fichtm,"
");
+
+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
");
+
+ fprintf(fichtm,"\nAge l
");
+
+ 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]);
+
+
+ fflush(fichtm);
+}
+
+/******************* Gnuplot file **************/
+void printinggnuplotmort(char fileres[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
+
+ char dirfileres[132],optfileres[132];
+ int m,cpt,k1,i,k,j,jk,k2,k3,ij,l;
+ int ng;
+
+
+ /*#ifdef windows */
+ fprintf(ficgp,"cd \"%s\" \n",pathc);
+ /*#endif */
+
+
+ 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);
+
+}
+
+
+
+
+
+/***********************************************/
+/**************** Main Program *****************/
+/***********************************************/
+
+int main(int argc, char *argv[])
+{
+ 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 linei, month, year,iout;
+ int jj, ll, li, lj, lk, imk;
+ int numlinepar=0; /* Current linenumber of parameter file */
+ int itimes;
+ int NDIM=2;
+
+ char ca[32], cb[32], cc[32];
+ char dummy[]=" ";
+ /* FILE *fichtm; *//* Html File */
+ /* FILE *ficgp;*/ /*Gnuplot File */
+ struct stat info;
+ double agedeb, agefin,hf;
+ double ageminpar=1.e20,agemin=1.e20, agemaxpar=-1.e20, agemax=-1.e20;
+
+ double fret;
+ double **xi,tmp,delta;
+
+ double dum; /* Dummy variable */
+ double ***p3mat;
+ double ***mobaverage;
int *indx;
char line[MAXLINE], linepar[MAXLINE];
- char title[MAXLINE];
- char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH];
- char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH];
- char filerest[FILENAMELENGTH];
- char fileregp[FILENAMELENGTH];
- char path[80],pathc[80],pathcd[80],pathtot[80],model[20];
+ char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE],model[MAXLINE];
+ char pathr[MAXLINE], pathimach[MAXLINE];
+ char **bp, *tok, *val; /* pathtot */
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,jk,aa,bb, stepsize;
- int jnais,jdc,jint4,jint1,jint2,jint3,**outcome,**adl,*tab;
-
+ 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;
+ int agemortsup;
+ float sumlpop=0.;
+ 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;
+
double bage, fage, age, agelim, agebase;
double ftolpl=FTOL;
double **prlim;
@@ -1594,445 +4403,1098 @@ int main()
double ***eij, ***vareij;
double **varpl; /* Variances of prevalence limits by age */
double *epj, vepp;
- char version[80]="Imach version 62c, May 1999, INED-EUROREVES ";
+ double kk1, kk2;
+ double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
+ double **ximort;
char *alph[]={"a","a","b","c","d","e"}, str[4];
+ int *dcwave;
+
char z[1]="c", occ;
-#include
%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,"\n
%s \
+
\n\
+Title=%s
Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s
\n\
+\n\
+
\
+
\n",\
+ 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,strstart);
+
+ fprintf(fichtm,"\n");
+ 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\
+ - Date and time at start: %s
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);
+ 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) */
+
+ globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
+
+ if (mle==-3){
+ ximort=matrix(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]);*/
+
+
+ printf("Powell\n"); fprintf(ficlog,"Powell\n");
+ 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);
+ }
+ fprintf(ficrespow,"# Powell\n# iter -2*LL");
+ /* 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");
+
+ powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
+ fclose(ficrespow);
+
+ hesscov(matcov, p, NDIM, delti, 1e-4, gompertz);
+
+ for(i=1; i <=NDIM; i++)
+ for(j=i+1;j<=NDIM;j++)
+ matcov[i][j]=matcov[j][i];
+
+ printf("\nCovariance matrix\n ");
+ for(i=1; i <=NDIM; i++) {
+ for(j=1;j<=NDIM;j++){
+ printf("%f ",matcov[i][j]);
+ }
+ printf("\n ");
+ }
+
+ printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
+ for (i=1;i<=NDIM;i++)
+ printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
+
+ lsurv=vector(1,AGESUP);
+ lpop=vector(1,AGESUP);
+ tpop=vector(1,AGESUP);
+ lsurv[agegomp]=100000;
+
+ for (k=agegomp;k<=AGESUP;k++) {
+ agemortsup=k;
+ if (p[1]*exp(p[2]*(k-agegomp))>1) break;
+ }
- /* Calculates basic frequencies. Computes observed prevalence at single age
- and prints on file fileres'p'. */
- freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax);
-
- 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 (k=agegomp;k Imach, Version 0.63
\n
- - Observed prevalence in each state: p%s
\n
-- Estimated parameters and the covariance matrix: %s
- - Stationary prevalence in each state: pl%s
- - Transition probabilities: pij%s
- - Copy of the parameter file: o%s
- - Life expectancies by age and initial health status: e%s
- - Variances of life expectancies by age and initial health status: v%s
- - Health expectancies with their variances: t%s
- - Standard deviation of stationary prevalences: vpl%s
",fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres);
-
- fprintf(fichtm,"
************ Results for covariates");
- for (cpt=1; cpt<=cptcovn;cpt++)
- fprintf(fichtm," V%d=%d ",Tvar[cpt],nbcode[Tvar[cpt]][codtab[j1][cpt]]);
- fprintf(fichtm," ************\n
");
- }
- fprintf(fichtm,"
- Probabilities: pe%s%d.gif
-",strtok(optionfile, "."),j1,strtok(optionfile, "."),j1);
- for(cpt=1; cpt
-",strtok(optionfile, "."),cpt,j1,strtok(optionfile, "."),cpt,j1);
- }
- for(cpt=1; cpt<=nlstate;cpt++) {
- fprintf(fichtm,"
- Observed and stationary prevalence (with confident
-interval) in state (%d): v%s%d%d.gif
-",cpt,strtok(optionfile, "."),cpt,j1,strtok(optionfile, "."),cpt,j1);
- }
- for(cpt=1; cpt<=nlstate;cpt++) {
- fprintf(fichtm,"\n
- Health life expectancies by age and initial health state (%d): exp%s%d%d.gif
-",cpt,strtok(optionfile, "."),cpt,j1,strtok(optionfile, "."),cpt,j1);
- }
- fprintf(fichtm,"\n
- Total life expectancy by age and
-health expectancies in states (1) and (2): e%s%d.gif
-",strtok(optionfile, "."),j1,strtok(optionfile, "."),j1);
-fprintf(fichtm,"\n");
- }
- }
-fclose(fichtm);
- /*--------------- Prevalence limit --------------*/
+ /*--------------- Prevalence limit (period or stable prevalence) --------------*/
- strcpy(filerespl,"pl");
- strcat(filerespl,fileres);
- if((ficrespl=fopen(filerespl,"w"))==NULL) {
- printf("Problem with Prev limit resultfile: %s\n", filerespl);goto end;
- }
- printf("Computing prevalence limit: result on file '%s' \n", filerespl);
- fprintf(ficrespl,"#Prevalence limit\n");
- fprintf(ficrespl,"#Age ");
- for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
- fprintf(ficrespl,"\n");
+ strcpy(filerespl,"pl");
+ strcat(filerespl,fileres);
+ if((ficrespl=fopen(filerespl,"w"))==NULL) {
+ printf("Problem with period (stable) prevalence resultfile: %s\n", filerespl);goto end;
+ fprintf(ficlog,"Problem with period (stable) prevalence resultfile: %s\n", filerespl);goto end;
+ }
+ printf("Computing period (stable) prevalence: result on file '%s' \n", filerespl);
+ fprintf(ficlog,"Computing period (stable) prevalence: result on file '%s' \n", filerespl);
+ pstamp(ficrespl);
+ fprintf(ficrespl,"# Period (stable) prevalence \n");
+ fprintf(ficrespl,"#Age ");
+ for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
+ fprintf(ficrespl,"\n");
- prlim=matrix(1,nlstate,1,nlstate);
- 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 */
- k=0;
- agebase=agemin;
- agelim=agemax;
- ftolpl=1.e-10;
- i1=cptcovn;
- if (cptcovn < 1){i1=1;}
+ prlim=matrix(1,nlstate,1,nlstate);
- for(cptcov=1;cptcov<=i1;cptcov++){
- for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
+ agebase=ageminpar;
+ agelim=agemaxpar;
+ ftolpl=1.e-10;
+ i1=cptcoveff;
+ if (cptcovn < 1){i1=1;}
+
+ for(cptcov=1,k=0;cptcov<=i1;cptcov++){
+ for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
k=k+1;
/*printf("cptcov=%d cptcod=%d codtab=%d nbcode=%d\n",cptcov, cptcod,Tcode[cptcode],codtab[cptcod][cptcov]);*/
- fprintf(ficrespl,"\n#****** ");
- for(j=1;j<=cptcovn;j++)
- fprintf(ficrespl,"V%d=%d ",Tvar[j],nbcode[Tvar[j]][codtab[k][j]]);
+ fprintf(ficrespl,"\n#******");
+ printf("\n#******");
+ fprintf(ficlog,"\n#******");
+ for(j=1;j<=cptcoveff;j++) {
+ fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
+ printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
+ fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
+ }
fprintf(ficrespl,"******\n");
+ printf("******\n");
+ fprintf(ficlog,"******\n");
for (age=agebase; age<=agelim; age++){
prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
- fprintf(ficrespl,"%.0f",age );
+ fprintf(ficrespl,"%.0f ",age );
+ for(j=1;j<=cptcoveff;j++)
+ fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
for(i=1; i<=nlstate;i++)
- fprintf(ficrespl," %.5f", prlim[i][i]);
+ fprintf(ficrespl," %.5f", prlim[i][i]);
fprintf(ficrespl,"\n");
}
}
}
- fclose(ficrespl);
- /*------------- h Pij x at various ages ------------*/
+ fclose(ficrespl);
+
+ /*------------- h Pij x at various ages ------------*/
- strcpy(filerespij,"pij"); strcat(filerespij,fileres);
- if((ficrespij=fopen(filerespij,"w"))==NULL) {
- printf("Problem with Pij resultfile: %s\n", filerespij);goto end;
- }
- printf("Computing pij: result on file '%s' \n", filerespij);
+ strcpy(filerespij,"pij"); strcat(filerespij,fileres);
+ if((ficrespij=fopen(filerespij,"w"))==NULL) {
+ printf("Problem with Pij resultfile: %s\n", filerespij);goto end;
+ fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij);goto end;
+ }
+ printf("Computing pij: result on file '%s' \n", filerespij);
+ fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
- stepsize=(int) (stepm+YEARM-1)/YEARM;
- if (stepm<=24) stepsize=2;
+ stepsize=(int) (stepm+YEARM-1)/YEARM;
+ /*if (stepm<=24) stepsize=2;*/
- agelim=AGESUP;
- hstepm=stepsize*YEARM; /* Every year of age */
- hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
-
- k=0;
- for(cptcov=1;cptcov<=i1;cptcov++){
- for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
- k=k+1;
+ agelim=AGESUP;
+ hstepm=stepsize*YEARM; /* Every year of age */
+ hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
+
+ /* hstepm=1; aff par mois*/
+ pstamp(ficrespij);
+ fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
+ for(cptcov=1,k=0;cptcov<=i1;cptcov++){
+ for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
+ k=k+1;
fprintf(ficrespij,"\n#****** ");
- for(j=1;j<=cptcovn;j++)
- fprintf(ficrespij,"V%d=%d ",Tvar[j],nbcode[Tvar[j]][codtab[k][j]]);
+ for(j=1;j<=cptcoveff;j++)
+ fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
fprintf(ficrespij,"******\n");
for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
+
+ /* nhstepm=nhstepm*YEARM; aff par mois*/
+
p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
oldm=oldms;savm=savms;
hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
- fprintf(ficrespij,"# Age");
+ fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
for(i=1; i<=nlstate;i++)
for(j=1; j<=nlstate+ndeath;j++)
fprintf(ficrespij," %1d-%1d",i,j);
fprintf(ficrespij,"\n");
for (h=0; h<=nhstepm; h++){
- fprintf(ficrespij,"%d %.0f %.0f",k,agedeb, agedeb+ h*hstepm/YEARM*stepm );
+ fprintf(ficrespij,"%d %3.f %3.f",k,agedeb, agedeb+ h*hstepm/YEARM*stepm );
for(i=1; i<=nlstate;i++)
for(j=1; j<=nlstate+ndeath;j++)
fprintf(ficrespij," %.5f", p3mat[i][j][h]);
@@ -2373,166 +5692,327 @@ fclose(fichtm);
free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
fprintf(ficrespij,"\n");
}
+ }
}
- }
- fclose(ficrespij);
+ varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
- /*---------- Health expectancies and variances ------------*/
-
- strcpy(filerest,"t");
- strcat(filerest,fileres);
- if((ficrest=fopen(filerest,"w"))==NULL) {
- printf("Problem with total LE resultfile: %s\n", filerest);goto end;
- }
- printf("Computing Total LEs with variances: file '%s' \n", filerest);
+ fclose(ficrespij);
+ probs= ma3x(1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
+ for(i=1;i<=AGESUP;i++)
+ for(j=1;j<=NCOVMAX;j++)
+ for(k=1;k<=NCOVMAX;k++)
+ probs[i][j][k]=0.;
+
+ /*---------- Forecasting ------------------*/
+ /*if((stepm == 1) && (strcmp(model,".")==0)){*/
+ if(prevfcast==1){
+ /* if(stepm ==1){*/
+ prevforecast(fileres, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);
+ /* (popforecast==1) populforecast(fileres, anpyram,mpyram,jpyram, agemin,agemax, dateprev1, dateprev2,mobilav, agedeb, fage, popforecast, popfile, anpyram1,p, i1);*/
+ /* } */
+ /* else{ */
+ /* erreur=108; */
+ /* printf("Warning %d!! You can only forecast the prevalences if the optimization\n has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model); */
+ /* fprintf(ficlog,"Warning %d!! You can only forecast the prevalences if the optimization\n has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model); */
+ /* } */
+ }
+
- strcpy(filerese,"e");
- strcat(filerese,fileres);
- if((ficreseij=fopen(filerese,"w"))==NULL) {
- printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
- }
- printf("Computing Health Expectancies: result on file '%s' \n", filerese);
+ /*---------- Health expectancies and variances ------------*/
- strcpy(fileresv,"v");
- strcat(fileresv,fileres);
- if((ficresvij=fopen(fileresv,"w"))==NULL) {
- printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
- }
- printf("Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
+ strcpy(filerest,"t");
+ strcat(filerest,fileres);
+ if((ficrest=fopen(filerest,"w"))==NULL) {
+ printf("Problem with total LE resultfile: %s\n", filerest);goto end;
+ fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
+ }
+ printf("Computing Total Life expectancies with their standard errors: file '%s' \n", filerest);
+ fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' \n", filerest);
+
+
+ strcpy(filerese,"e");
+ strcat(filerese,fileres);
+ if((ficreseij=fopen(filerese,"w"))==NULL) {
+ printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
+ fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
+ }
+ printf("Computing Health Expectancies: result on file '%s' \n", filerese);
+ fprintf(ficlog,"Computing Health Expectancies: result on file '%s' \n", filerese);
+
+ strcpy(fileresstde,"stde");
+ strcat(fileresstde,fileres);
+ if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
+ printf("Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
+ fprintf(ficlog,"Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
+ }
+ printf("Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
+ fprintf(ficlog,"Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
+
+ strcpy(filerescve,"cve");
+ strcat(filerescve,fileres);
+ if((ficrescveij=fopen(filerescve,"w"))==NULL) {
+ printf("Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
+ fprintf(ficlog,"Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
+ }
+ printf("Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
+ fprintf(ficlog,"Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
+
+ strcpy(fileresv,"v");
+ strcat(fileresv,fileres);
+ if((ficresvij=fopen(fileresv,"w"))==NULL) {
+ printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
+ fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
+ }
+ printf("Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
+ fprintf(ficlog,"Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
+
+ /* Computes prevalence between agemin (i.e minimal age computed) and no more ageminpar */
+ prevalence(probs, agemin, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
+ /* printf("ageminpar=%f, agemax=%f, s[lastpass][imx]=%d, agev[lastpass][imx]=%f, nlstate=%d, imx=%d, mint[lastpass][imx]=%f, anint[lastpass][imx]=%f,dateprev1=%f, dateprev2=%f, firstpass=%d, lastpass=%d\n",\
+ ageminpar, agemax, s[lastpass][imx], agev[lastpass][imx], nlstate, imx, mint[lastpass][imx],anint[lastpass][imx], dateprev1, dateprev2, firstpass, lastpass);
+ */
+
+ if (mobilav!=0) {
+ mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
+ if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){
+ fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
+ printf(" Error in movingaverage mobilav=%d\n",mobilav);
+ }
+ }
- k=0;
- for(cptcov=1;cptcov<=i1;cptcov++){
- for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
- k=k+1;
- fprintf(ficrest,"\n#****** ");
- for(j=1;j<=cptcovn;j++)
- fprintf(ficrest,"V%d=%d ",Tvar[j],nbcode[Tvar[j]][codtab[k][j]]);
- fprintf(ficrest,"******\n");
-
- fprintf(ficreseij,"\n#****** ");
- for(j=1;j<=cptcovn;j++)
- fprintf(ficreseij,"V%d=%d ",j,nbcode[j][codtab[k][j]]);
- fprintf(ficreseij,"******\n");
-
- fprintf(ficresvij,"\n#****** ");
- for(j=1;j<=cptcovn;j++)
- fprintf(ficresvij,"V%d=%d ",j,nbcode[j][codtab[k][j]]);
- fprintf(ficresvij,"******\n");
-
- eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
- oldm=oldms;savm=savms;
- evsij(fileres, eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k);
- vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
- oldm=oldms;savm=savms;
- varevsij(fileres, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k);
-
- fprintf(ficrest,"#Total LEs with variances: e.. (std) ");
- for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
- fprintf(ficrest,"\n");
-
- hf=1;
- if (stepm >= YEARM) hf=stepm/YEARM;
- epj=vector(1,nlstate+1);
- for(age=bage; age <=fage ;age++){
- prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
- fprintf(ficrest," %.0f",age);
- for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
- for(i=1, epj[j]=0.;i <=nlstate;i++) {
- epj[j] += prlim[i][i]*hf*eij[i][j][(int)age];
- }
- epj[nlstate+1] +=epj[j];
- }
- for(i=1, vepp=0.;i <=nlstate;i++)
- for(j=1;j <=nlstate;j++)
- vepp += vareij[i][j][(int)age];
- fprintf(ficrest," %.2f (%.2f)", epj[nlstate+1],hf*sqrt(vepp));
- for(j=1;j <=nlstate;j++){
- fprintf(ficrest," %.2f (%.2f)", epj[j],hf*sqrt(vareij[j][j][(int)age]));
+ for(cptcov=1,k=0;cptcov<=i1;cptcov++){
+ for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
+ k=k+1;
+ fprintf(ficrest,"\n#****** ");
+ for(j=1;j<=cptcoveff;j++)
+ fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
+ fprintf(ficrest,"******\n");
+
+ fprintf(ficreseij,"\n#****** ");
+ fprintf(ficresstdeij,"\n#****** ");
+ fprintf(ficrescveij,"\n#****** ");
+ for(j=1;j<=cptcoveff;j++) {
+ fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
+ fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
+ fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
}
+ fprintf(ficreseij,"******\n");
+ fprintf(ficresstdeij,"******\n");
+ fprintf(ficrescveij,"******\n");
+
+ fprintf(ficresvij,"\n#****** ");
+ for(j=1;j<=cptcoveff;j++)
+ fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
+ fprintf(ficresvij,"******\n");
+
+ eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
+ oldm=oldms;savm=savms;
+ evsij(fileres, eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart);
+ cvevsij(fileres, eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart);
+
+ vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
+ oldm=oldms;savm=savms;
+ varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k, estepm, cptcov,cptcod,0, mobilav, strstart);
+ if(popbased==1){
+ varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k, estepm, cptcov,cptcod,popbased,mobilav, strstart);
+ }
+
+ pstamp(ficrest);
+ fprintf(ficrest,"# Total life expectancy with std error and decomposition into time to be expected in each health state\n# Age ( e.. (std) ");
+ for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
fprintf(ficrest,"\n");
+
+ epj=vector(1,nlstate+1);
+ for(age=bage; age <=fage ;age++){
+ prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
+ if (popbased==1) {
+ if(mobilav ==0){
+ for(i=1; i<=nlstate;i++)
+ prlim[i][i]=probs[(int)age][i][k];
+ }else{ /* mobilav */
+ for(i=1; i<=nlstate;i++)
+ prlim[i][i]=mobaverage[(int)age][i][k];
+ }
+ }
+
+ fprintf(ficrest," %4.0f",age);
+ for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
+ for(i=1, epj[j]=0.;i <=nlstate;i++) {
+ epj[j] += prlim[i][i]*eij[i][j][(int)age];
+ /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
+ }
+ epj[nlstate+1] +=epj[j];
+ }
+
+ for(i=1, vepp=0.;i <=nlstate;i++)
+ for(j=1;j <=nlstate;j++)
+ vepp += vareij[i][j][(int)age];
+ fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
+ for(j=1;j <=nlstate;j++){
+ fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
+ }
+ fprintf(ficrest,"\n");
+ }
+ free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
+ free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
+ free_vector(epj,1,nlstate+1);
}
}
- }
-
- fclose(ficreseij);
- fclose(ficresvij);
- fclose(ficrest);
- fclose(ficpar);
- free_vector(epj,1,nlstate+1);
- /* scanf("%d ",i); */
-
- /*------- Variance limit prevalence------*/
-
-strcpy(fileresvpl,"vpl");
- strcat(fileresvpl,fileres);
- if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
- printf("Problem with variance prev lim resultfile: %s\n", fileresvpl);
- exit(0);
- }
- printf("Computing Variance-covariance of Prevalence limit: file '%s' \n", fileresvpl);
+ free_vector(weight,1,n);
+ free_imatrix(Tvard,1,15,1,2);
+ free_imatrix(s,1,maxwav+1,1,n);
+ free_matrix(anint,1,maxwav,1,n);
+ free_matrix(mint,1,maxwav,1,n);
+ free_ivector(cod,1,n);
+ free_ivector(tab,1,NCOVMAX);
+ fclose(ficreseij);
+ fclose(ficresstdeij);
+ fclose(ficrescveij);
+ fclose(ficresvij);
+ fclose(ficrest);
+ fclose(ficpar);
+
+ /*------- Variance of period (stable) prevalence------*/
- k=0;
- for(cptcov=1;cptcov<=i1;cptcov++){
- for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
- k=k+1;
- fprintf(ficresvpl,"\n#****** ");
- for(j=1;j<=cptcovn;j++)
- fprintf(ficresvpl,"V%d=%d ",Tvar[j],nbcode[Tvar[j]][codtab[k][j]]);
- fprintf(ficresvpl,"******\n");
-
- varpl=matrix(1,nlstate,(int) bage, (int) fage);
- oldm=oldms;savm=savms;
- varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k);
- }
- }
+ strcpy(fileresvpl,"vpl");
+ strcat(fileresvpl,fileres);
+ if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
+ printf("Problem with variance of period (stable) prevalence resultfile: %s\n", fileresvpl);
+ exit(0);
+ }
+ printf("Computing Variance-covariance of period (stable) prevalence: file '%s' \n", fileresvpl);
- fclose(ficresvpl);
+ for(cptcov=1,k=0;cptcov<=i1;cptcov++){
+ for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
+ k=k+1;
+ fprintf(ficresvpl,"\n#****** ");
+ for(j=1;j<=cptcoveff;j++)
+ fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);
+ fprintf(ficresvpl,"******\n");
+
+ varpl=matrix(1,nlstate,(int) bage, (int) fage);
+ oldm=oldms;savm=savms;
+ varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k,strstart);
+ free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
+ }
+ }
- /*---------- End : free ----------------*/
- free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
-
- free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
- free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
-
-
- free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
- free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
- free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
- free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
-
- free_matrix(matcov,1,npar,1,npar);
- free_vector(delti,1,npar);
+ fclose(ficresvpl);
+
+ /*---------- End : free ----------------*/
+ if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
+ free_ma3x(probs,1,AGESUP,1,NCOVMAX, 1,NCOVMAX);
+
+ } /* mle==-3 arrives here for freeing */
+ free_matrix(prlim,1,nlstate,1,nlstate);
+ free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
+ free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
+ free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
+ free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
+ free_matrix(covar,0,NCOVMAX,1,n);
+ free_matrix(matcov,1,npar,1,npar);
+ /*free_vector(delti,1,npar);*/
+ free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
+ free_matrix(agev,1,maxwav,1,imx);
+ free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
+
+ free_ivector(ncodemax,1,8);
+ free_ivector(Tvar,1,15);
+ free_ivector(Tprod,1,15);
+ free_ivector(Tvaraff,1,15);
+ free_ivector(Tage,1,15);
+ free_ivector(Tcode,1,100);
+
+ free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
+ free_imatrix(codtab,1,100,1,10);
+ fflush(fichtm);
+ fflush(ficgp);
- free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
- printf("End of Imach\n");
+ if((nberr >0) || (nbwarn>0)){
+ printf("End of Imach with %d errors and/or %d warnings\n",nberr,nbwarn);
+ fprintf(ficlog,"End of Imach with %d errors and/or warnings %d\n",nberr,nbwarn);
+ }else{
+ printf("End of Imach\n");
+ fprintf(ficlog,"End of Imach\n");
+ }
+ printf("See log file on %s\n",filelog);
/* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
-
- /* printf("Total time was %d Sec. %d uSec.\n", end_time.tv_sec -start_time.tv_sec, end_time.tv_usec -start_time.tv_usec);*/
- /*printf("Total time was %d uSec.\n", total_usecs);*/
+ (void) gettimeofday(&end_time,&tzp);
+ tm = *localtime(&end_time.tv_sec);
+ tmg = *gmtime(&end_time.tv_sec);
+ strcpy(strtend,asctime(&tm));
+ printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
+ fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
+ printf("Total time used %s\n", asc_diff_time(end_time.tv_sec -start_time.tv_sec,tmpout));
+
+ printf("Total time was %d Sec.\n", end_time.tv_sec -start_time.tv_sec);
+ fprintf(ficlog,"Total time used %s\n", asc_diff_time(end_time.tv_sec -start_time.tv_sec,tmpout));
+ fprintf(ficlog,"Total time was %d Sec.\n", end_time.tv_sec -start_time.tv_sec);
+ /* printf("Total time was %d uSec.\n", total_usecs);*/
+/* 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 ../gp37mgw/graph.plt");
-#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(pathcd);
- printf("\nType e to edit output files, c to start again, and q for exiting: ");
+ /* chdir(path); */
+ 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') {
- chdir(path);
- system("index.htm");
+/* 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);
+ }
}
+