--- imach/src/imach.c 2002/06/20 14:03:39 1.49 +++ imach/src/imach.c 2002/06/26 23:25:02 1.50 @@ -1,4 +1,4 @@ -/* $Id: imach.c,v 1.49 2002/06/20 14:03:39 lievre Exp $ +/* $Id: imach.c,v 1.50 2002/06/26 23:25:02 brouard Exp $ Interpolated Markov Chain Short summary of the programme: @@ -77,11 +77,13 @@ #define AGEBASE 40 #ifdef windows #define DIRSEPARATOR '\\' +#define ODIRSEPARATOR '/' #else #define DIRSEPARATOR '/' +#define ODIRSEPARATOR '\\' #endif -char version[80]="Imach version 0.8h, May 2002, INED-EUROREVES "; +char version[80]="Imach version 0.8i, June 2002, INED-EUROREVES "; int erreur; /* Error number */ int nvar; int cptcovn=0, cptcovage=0, cptcoveff=0,cptcov; @@ -101,7 +103,9 @@ double jmean; /* Mean space between 2 wa double **oldm, **newm, **savm; /* Working pointers to matrices */ double **oldms, **newms, **savms; /* Fixed working pointers to matrices */ FILE *fic,*ficpar, *ficparo,*ficres, *ficrespl, *ficrespij, *ficrest,*ficresf,*ficrespop; +FILE *ficlog; FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor; +FILE *ficresprobmorprev; FILE *fichtm; /* Html File */ FILE *ficreseij; char filerese[FILENAMELENGTH]; @@ -114,7 +118,7 @@ char optionfile[FILENAMELENGTH], datafil char optionfilext[10], optionfilefiname[FILENAMELENGTH], plotcmd[FILENAMELENGTH]; char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH]; - +char filelog[FILENAMELENGTH]; /* Log file */ char filerest[FILENAMELENGTH]; char fileregp[FILENAMELENGTH]; char popfile[FILENAMELENGTH]; @@ -178,8 +182,10 @@ static int split( char *path, char *dirc l1 = strlen( path ); /* length of path */ if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH ); - s = strrchr( path, DIRSEPARATOR ); /* find last / */ + s= strrchr( path, DIRSEPARATOR ); /* find last / */ if ( s == NULL ) { /* no directory, so use current */ + /*if(strrchr(path, ODIRSEPARATOR )==NULL) + printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/ #if defined(__bsd__) /* get current working directory */ extern char *getwd( ); @@ -245,6 +251,9 @@ int nbocc(char *s, char occ) void cutv(char *u,char *v, char*t, char occ) { + /* cuts string t into u and v where u is ended by char occ excluding it + and v is after occ excluding it too : ex cutv(u,v,"abcdef2ghi2j",2) + gives u="abcedf" and v="ghi2j" */ int i,lg,j,p=0; i=0; for(j=0; j<=strlen(t)-1; j++) { @@ -441,8 +450,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))){ @@ -567,6 +578,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; @@ -597,16 +609,21 @@ void powell(double p[], double **xi, int ibig=0; del=0.0; printf("\nPowell iter=%d -2*LL=%.12f",*iter,*fret); + fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f",*iter,*fret); for (i=1;i<=n;i++) printf(" %d %.12f",i, p[i]); + fprintf(ficlog," %d %.12f",i, p[i]); printf("\n"); + fprintf(ficlog,"\n"); 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)); @@ -614,13 +631,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))) { @@ -629,15 +651,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 @@ -665,9 +693,13 @@ 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 } } @@ -938,10 +970,11 @@ void mlikeli(FILE *ficres,double p[], in 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"); powell(p,xi,npar,ftol,&iter,&fret,func); printf("\n#Number of iterations = %d, -2 Log likelihood = %.12f\n",iter,func(p)); + fprintf(ficlog,"#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)); } @@ -962,8 +995,10 @@ void hesscov(double **matcov, 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); + fprintf(ficlog,"%d",i);fflush(ficlog); hess[i][i]=hessii(p,ftolhess,i,delti); /*printf(" %f ",p[i]);*/ /*printf(" %lf ",hess[i][i]);*/ @@ -973,6 +1008,7 @@ void hesscov(double **matcov, double p[] for (j=1;j<=npar;j++) { if (j>i) { printf(".%d%d",i,j);fflush(stdout); + fprintf(ficlog,".%d%d",i,j);fflush(ficlog); hess[i][j]=hessij(p,delti,i,j); hess[j][i]=hess[i][j]; /*printf(" %lf ",hess[i][j]);*/ @@ -980,8 +1016,10 @@ void hesscov(double **matcov, double p[] } } 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); @@ -1001,11 +1039,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 */ @@ -1022,8 +1063,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"); } */ @@ -1065,6 +1108,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 =1.e-10) + if(pp[jk]>=1.e-10){ + if(first==1){ printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]); - else - printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk); + } + fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]); + }else{ + if(first==1) + printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk); + fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk); + } } for(jk=1; jk <=nlstate ; jk++){ @@ -1292,10 +1352,15 @@ void freqsummary(char fileres[], int ag for(jk=1,pos=0; jk <=nlstate ; jk++) pos += pp[jk]; for(jk=1; jk <=nlstate ; jk++){ - if(pos>=1.e-5) - printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos); - else - printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk); + if(pos>=1.e-5){ + if(first==1) + printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos); + fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos); + }else{ + if(first==1) + printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk); + fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk); + } if( i <= (int) agemax){ if(pos>=1.e-5){ fprintf(ficresp," %d %.5f %.0f %.0f",i,pp[jk]/pos, pp[jk],pos); @@ -1309,10 +1374,16 @@ void freqsummary(char fileres[], int ag for(jk=-1; jk <=nlstate+ndeath; jk++) for(m=-1; m <=nlstate+ndeath; m++) - if(freq[jk][m][i] !=0 ) printf(" %d%d=%.0f",jk,m,freq[jk][m][i]); + if(freq[jk][m][i] !=0 ) { + if(first==1) + printf(" %d%d=%.0f",jk,m,freq[jk][m][i]); + fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][i]); + } if(i <= (int) agemax) fprintf(ficresp,"\n"); - printf("\n"); + if(first==1) + printf("Others in log...\n"); + fprintf(ficlog,"\n"); } } } @@ -1399,11 +1470,10 @@ void prevalence(int agemin, float agemax probs[i][jk][j1]= pp[jk]/pos; } } - } - - } - } - } + }/* end jk */ + }/* end i */ + } /* end i1 */ + } /* end k1 */ free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath,(int) agemin,(int) agemax+3); @@ -1425,9 +1495,10 @@ void concatwav(int wav[], int **dh, int int i, mi, m; /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1; double sum=0., jmean=0.;*/ - + int first; int j, k=0,jk, ju, jl; double sum=0.; + first=0; jmin=1e+5; jmax=-1; jmean=0.; @@ -1450,8 +1521,15 @@ void concatwav(int wav[], int **dh, int } wav[i]=mi; - if(mi==0) - printf("Warning, no any valid information for:%d line=%d\n",num[i],i); + if(mi==0){ + if(first==0){ + printf("Warning, no any valid information for:%d line=%d and may be others, see log file\n",num[i],i); + first=1; + } + if(first==1){ + fprintf(ficlog,"Warning, no any valid information for:%d line=%d\n",num[i],i); + } + } /* end mi==0 */ } for(i=1; i<=imx; i++){ @@ -1492,7 +1570,9 @@ void concatwav(int wav[], int **dh, int } jmean=sum/k; printf("Delay (in months) between two waves Min=%d Max=%d Mean=%f\n\n ",jmin, jmax,jmean); + fprintf(ficlog,"Delay (in months) between two waves Min=%d Max=%d Mean=%f\n\n ",jmin, jmax,jmean); } + /*********** Tricode ****************************/ void tricode(int *Tvar, int **nbcode, int imx) { @@ -1532,9 +1612,9 @@ void tricode(int *Tvar, int **nbcode, in for (k=0; k<19; k++) Ndum[k]=0; for (i=1; i<=ncovmodel-2; i++) { - ij=Tvar[i]; - Ndum[ij]++; - } + ij=Tvar[i]; + Ndum[ij]++; + } ij=1; for (i=1; i<=10; i++) { @@ -1544,7 +1624,7 @@ void tricode(int *Tvar, int **nbcode, in } } - cptcoveff=ij-1; + cptcoveff=ij-1; } /*********** Health Expectancies ****************/ @@ -1675,6 +1755,7 @@ void evsij(char fileres[], double ***eij varhe[i][j][(int)age] =0.; printf("%d|",(int)age);fflush(stdout); + fprintf(ficlog,"%d|",(int)age);fflush(ficlog); for(h=0;h<=nhstepm-1;h++){ for(k=0;k<=nhstepm-1;k++){ matprod2(dnewm,trgradg[h],1,nlstate*2,1,npar,1,npar,matcov); @@ -1710,6 +1791,7 @@ void evsij(char fileres[], double ***eij free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); } printf("\n"); + fprintf(ficlog,"\n"); free_vector(xp,1,npar); free_matrix(dnewm,1,nlstate*2,1,npar); @@ -1718,20 +1800,71 @@ void evsij(char fileres[], double ***eij } /************ Variance ******************/ -void varevsij(char fileres[], double ***vareij, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int ij, int estepm) +void varevsij(char optionfilefiname[], double ***vareij, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int ij, int estepm, int cptcov, int cptcod, int popbased) { /* Variance of health expectancies */ /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/ - double **newm; + /* double **newm;*/ double **dnewm,**doldm; + double **dnewmp,**doldmp; int i, j, nhstepm, hstepm, h, nstepm ; int k, cptcode; double *xp; - double **gp, **gm; - double ***gradg, ***trgradg; + double **gp, **gm; /* for var eij */ + double ***gradg, ***trgradg; /*for var eij */ + double **gradgp, **trgradgp; /* for var p point j */ + double *gpp, *gmp; /* for var p point j */ + double **varppt; /* for var p point j nlstate to nlstate+ndeath */ double ***p3mat; double age,agelim, hf; int theta; + char digit[4]; + char digitp[16]; + + char fileresprobmorprev[FILENAMELENGTH]; + + if(popbased==1) + strcpy(digitp,"-populbased-"); + else + strcpy(digitp,"-stablbased-"); + + strcpy(fileresprobmorprev,"prmorprev"); + sprintf(digit,"%-d",ij); + /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/ + strcat(fileresprobmorprev,digit); /* Tvar to be done */ + strcat(fileresprobmorprev,digitp); /* Popbased or not */ + strcat(fileresprobmorprev,fileres); + if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) { + printf("Problem with resultfile: %s\n", fileresprobmorprev); + fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev); + } + printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev); + fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev); + fprintf(ficresprobmorprev,"# probabilities of dying during a year and weighted mean w1*p1j+w2*p2j+... stand dev in()\n"); + fprintf(ficresprobmorprev,"# Age cov=%-d",ij); + for(j=nlstate+1; j<=(nlstate+ndeath);j++){ + fprintf(ficresprobmorprev," p.%-d SE",j); + for(i=1; i<=nlstate;i++) + fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j); + } + fprintf(ficresprobmorprev,"\n"); + if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { + printf("Problem with gnuplot file: %s\n", optionfilegnuplot); + fprintf(ficlog,"Problem with gnuplot file: %s\n", optionfilegnuplot); + exit(0); + } + else{ + fprintf(ficgp,"\n# Routine varevsij"); + } + if((fichtm=fopen(optionfilehtm,"a"))==NULL) { + printf("Problem with html file: %s\n", optionfilehtm); + fprintf(ficlog,"Problem with html file: %s\n", optionfilehtm); + exit(0); + } + else{ + fprintf(fichtm,"\n
  • Computing step probabilities of dying and weighted average (i.e global mortality independent of initial healh state)

  • \n"); + } + varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath); fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are the stable prevalence in health states i\n"); fprintf(ficresvij,"# Age"); @@ -1743,6 +1876,13 @@ void varevsij(char fileres[], double *** 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*/ if(estepm < stepm){ printf ("Problem %d lower than %d\n",estepm, stepm); @@ -1770,6 +1910,7 @@ void varevsij(char fileres[], double *** 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 */ xp[i] = x[i] + (i==theta ?delti[theta]:0); @@ -1788,7 +1929,13 @@ void varevsij(char fileres[], double *** gp[h][j] += prlim[i][i]*p3mat[i][j][h]; } } - + /* This for computing forces of mortality (h=1)as a weighted average */ + for(j=nlstate+1,gpp[j]=0.;j<=nlstate+ndeath;j++){ + for(i=1; i<= nlstate; i++) + gpp[j] += prlim[i][i]*p3mat[i][j][1]; + } + /* end force of mortality */ + for(i=1; i<=npar; i++) /* Computes gradient */ xp[i] = x[i] - (i==theta ?delti[theta]:0); hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij); @@ -1805,20 +1952,34 @@ void varevsij(char fileres[], double *** gm[h][j] += prlim[i][i]*p3mat[i][j][h]; } } + /* This for computing force of mortality (h=1)as a weighted average */ + for(j=nlstate+1,gmp[j]=0.;j<=nlstate+ndeath;j++){ + for(i=1; i<= nlstate; i++) + gmp[j] += prlim[i][i]*p3mat[i][j][1]; + } + /* end force of mortality */ - for(j=1; j<= nlstate; j++) + 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++) @@ -1834,6 +1995,37 @@ void varevsij(char fileres[], double *** } } + /* 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 */ + hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij); + prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ij); + + if (popbased==1) { + for(i=1; i<=nlstate;i++) + prlim[i][i]=probs[(int)age][i][ij]; + } + + /* This for computing force of mortality (h=1)as a weighted average */ + for(j=nlstate+1,gmp[j]=0.;j<=nlstate+ndeath;j++){ + for(i=1; i<= nlstate; i++) + gmp[j] += prlim[i][i]*p3mat[i][j][1]; + } + /* end force of mortality */ + + 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++){ @@ -1846,10 +2038,29 @@ 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(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(fichtm,"\n
    File (multiple files are possible if covariates are present): %s\n",fileresprobmorprev,fileresprobmorprev); + fprintf(fichtm,"\n
    Probability is computed over estepm=%d months 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.png\";replot;",digitp,digit); + free_vector(xp,1,npar); - free_matrix(doldm,1,nlstate,1,npar); - free_matrix(dnewm,1,nlstate,1,nlstate); + 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); + fclose(ficresprobmorprev); + fclose(ficgp); + fclose(fichtm); } @@ -1942,7 +2153,7 @@ void varprob(char optionfilefiname[], do int i, j=0, i1, k1, l1, t, tj; int k2, l2, j1, z1; int k=0,l, cptcode; - int first=1; + int first=1, first1; double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2; double **dnewm,**doldm; double *xp; @@ -1962,20 +2173,26 @@ void varprob(char optionfilefiname[], do 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); fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n"); fprintf(ficresprob,"# Age"); @@ -2002,6 +2219,7 @@ void varprob(char optionfilefiname[], do first=1; if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { printf("Problem with gnuplot file: %s\n", optionfilegnuplot); + fprintf(ficlog,"Problem with gnuplot file: %s\n", optionfilegnuplot); exit(0); } else{ @@ -2009,9 +2227,13 @@ void varprob(char optionfilefiname[], do } if((fichtm=fopen(optionfilehtm,"a"))==NULL) { printf("Problem with html file: %s\n", optionfilehtm); + fprintf(ficlog,"Problem with html file: %s\n", optionfilehtm); exit(0); } else{ + fprintf(fichtm,"\n
  • Computing and drawing one step probabilities with their confidence intervals

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

  • \n"); fprintf(fichtm,"\nWe have drawn ellipsoids of confidence around the pij, pkl to understand the covariance between two incidences. They are expressed in year-1 in order to be less dependent of stepm.
    \n"); fprintf(fichtm,"\n
    We have drawn x'cov-1x = 4 where x is the column vector (pij,pkl). It means that if pij and pkl where uncorrelated the (2X2) matrix would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 standard deviations wide on each axis.
    When both incidences are correlated we diagonalised the inverse of the covariance matrix and made the appropriate rotation.
    \n"); @@ -2116,6 +2338,7 @@ void varprob(char optionfilefiname[], do /*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); @@ -2141,7 +2364,20 @@ void varprob(char optionfilefiname[], do } }/* end of loop for state */ } /* end of loop for age */ - /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/ + + /* 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 (k1=1; k1<=(nlstate);k1++){ for (l1=1; l1<=(nlstate+ndeath);l1++){ if(l1==k1) continue; @@ -2161,7 +2397,11 @@ void varprob(char optionfilefiname[], do /* Computing eigen value of matrix of covariance */ lc1=(v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)); lc2=(v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)); - printf("Var %.4e %.4e cov %.4e Eigen %.3e %.3e\n",v1,v2,cv12,lc1,lc2); + if(first1==1){ + first1=0; + printf("Var %.4e %.4e cov %.4e Eigen %.3e %.3e\nOthers in log...\n",v1,v2,cv12,lc1,lc2); + } + fprintf(ficlog,"Var %.4e %.4e cov %.4e Eigen %.3e %.3e\n",v1,v2,cv12,lc1,lc2); /* Eigen vectors */ v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12)); v21=sqrt(1.-v11*v11); @@ -2172,7 +2412,7 @@ void varprob(char optionfilefiname[], do /* mu2+ v21*lc1*cost + v21*lc2*sin(t) */ if(first==1){ first=0; - fprintf(ficgp,"\nset parametric;set nolabel"); + fprintf(ficgp,"\nset parametric;set nolabel"); fprintf(ficgp,"\nset log y;set log x; set xlabel \"p%1d%1d (year-1)\";set ylabel \"p%1d%1d (year-1)\"",k2,l2,k1,l1); fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65"); fprintf(fichtm,"\n
    Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year-1 :varpijgr%s%d%1d%1d-%1d%1d.png, ",k2,l2,k1,l1,optionfilefiname, j1,k2,l2,k1,l1,optionfilefiname, j1,k2,l2,k1,l1); @@ -2180,16 +2420,25 @@ void varprob(char optionfilefiname[], do fprintf(ficgp,"\nset out \"varpijgr%s%d%1d%1d-%1d%1d.png\"",optionfilefiname, j1,k2,l2,k1,l1); fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu2,mu1); fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k2,l2,k1,l1); - 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)) t \"%d\"",\ + /* 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)) t \"%d\"",\ mu2,std,v21,sqrt(lc1),v21,sqrt(lc2), \ mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),(int) age); + */ + 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",\ + mu2,std,v21,sqrt(lc1),v21,sqrt(lc2), \ + mu1,std,v11,sqrt(lc1),v12,sqrt(lc2)); }else{ first=0; fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k2,l2,k1,l1); fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu2,mu1); + /* 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)) t \"%d\"",\ mu2,std,v21,sqrt(lc1),v21,sqrt(lc2), \ mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),(int) age); + */ + 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",\ + mu2,std,v21,sqrt(lc1),v21,sqrt(lc2), \ + mu1,std,v11,sqrt(lc1),v12,sqrt(lc2)); }/* if first */ } /* age mod 5 */ } /* end loop age */ @@ -2227,6 +2476,7 @@ void printinghtml(char fileres[], char t /*char optionfilehtm[FILENAMELENGTH];*/ if((fichtm=fopen(optionfilehtm,"a"))==NULL) { printf("Problem with %s \n",optionfilehtm), exit(0); + fprintf(ficlog,"Problem with %s \n",optionfilehtm), exit(0); } fprintf(fichtm,"\n",version,title,datafile,firstpass,lastpass,stepm, weightopt,model,imx,jmin,jmax,jmean,fileres,fileres,filelog,filelog,optionfilegnuplot,optionfilegnuplot); fclose(fichtm); printinghtml(fileres,title,datafile, firstpass, lastpass, stepm, weightopt,model,imx,jmin,jmax,jmean,rfileres,popforecast,estepm,jprev1,mprev1,anprev1,jprev2,mprev2,anprev2); @@ -3447,8 +3784,10 @@ Interval (in months) between two waves: strcat(filerespl,fileres); if((ficrespl=fopen(filerespl,"w"))==NULL) { printf("Problem with Prev limit resultfile: %s\n", filerespl);goto end; + fprintf(ficlog,"Problem with Prev limit resultfile: %s\n", filerespl);goto end; } printf("Computing prevalence limit: result on file '%s' \n", filerespl); + fprintf(ficlog,"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); @@ -3472,9 +3811,16 @@ Interval (in months) between two waves: 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<=cptcoveff;j++) + 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); @@ -3492,8 +3838,10 @@ Interval (in months) between two waves: 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;*/ @@ -3553,6 +3901,7 @@ Interval (in months) between two waves: 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); } @@ -3562,30 +3911,36 @@ Interval (in months) between two waves: 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 LEs with variances: file '%s' \n", filerest); + fprintf(ficlog,"Computing Total LEs with variances: 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(fileresv,"v"); + 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); calagedate=-1; -prevalence(ageminpar, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax,mint,anint,dateprev1,dateprev2, calagedate); + prevalence(ageminpar, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax,mint,anint,dateprev1,dateprev2, calagedate); k=0; for(cptcov=1;cptcov<=i1;cptcov++){ for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){ - k=k+1; + 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]]); @@ -3607,8 +3962,10 @@ prevalence(ageminpar, agemax, s, agev, n 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, estepm); - + varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k, estepm, cptcov,cptcod,0); + if(popbased==1){ + varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k, estepm, cptcov,cptcod,popbased); + } fprintf(ficrest,"#Total LEs with variances: e.. (std) "); @@ -3701,9 +4058,15 @@ free_matrix(mint,1,maxwav,1,n); fclose(ficgp); - if(erreur >0) + if(erreur >0){ printf("End of Imach with error or warning %d\n",erreur); - else printf("End of Imach\n"); + fprintf(ficlog,"End of Imach with error or warning %d\n",erreur); + }else{ + printf("End of Imach\n"); + fprintf(ficlog,"End of Imach\n"); + } + printf("See log file on %s\n",filelog); + fclose(ficlog); /* 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);*/