--- imach/src/imach.c 2002/02/22 17:54:20 1.22
+++ imach/src/imach.c 2002/02/28 17:49:07 1.27
@@ -1,5 +1,5 @@
-/* $Id: imach.c,v 1.22 2002/02/22 17:54:20 brouard Exp $
- Interpolate Markov Chain
+/* $Id: imach.c,v 1.27 2002/02/28 17:49:07 lievre Exp $
+ Interpolated Markov Chain
Short summary of the programme:
@@ -94,8 +94,8 @@ int **dh; /* dh[mi][i] is number of step
double jmean; /* Mean space between 2 waves */
double **oldm, **newm, **savm; /* Working pointers to matrices */
double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
-FILE *fic,*ficpar, *ficparo,*ficres, *ficrespl, *ficrespij, *ficrest,*ficresf;
-FILE *ficgp, *fichtm,*ficresprob,*ficpop;
+FILE *fic,*ficpar, *ficparo,*ficres, *ficrespl, *ficrespij, *ficrest,*ficresf,*ficrespop;
+FILE *ficgp,*ficresprob,*ficpop;
FILE *ficreseij;
char filerese[FILENAMELENGTH];
FILE *ficresvij;
@@ -123,7 +123,7 @@ FILE *ficreseij;
static double maxarg1,maxarg2;
#define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
#define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
-
+
#define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
#define rint(a) floor(a+0.5)
@@ -1176,7 +1176,7 @@ void lubksb(double **a, int n, int *indx
}
/************ Frequencies ********************/
-void freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2)
+void freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2,double jprev1, double mprev1,double anprev1,double jprev2, double mprev2,double anprev2)
{ /* Some frequencies */
int i, m, jk, k1,i1, j1, bool, z1,z2,j;
@@ -1236,6 +1236,9 @@ void freqsummary(char fileres[], int ag
}
}
}
+
+ fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);
+
if (cptcovn>0) {
fprintf(ficresp, "\n#********** Variable ");
for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);
@@ -1620,7 +1623,7 @@ void varevsij(char fileres[], double ***
for(i=1; i<=nlstate;i++)
prlim[i][i]=probs[(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++)
@@ -1632,7 +1635,7 @@ void varevsij(char fileres[], double ***
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) {
for(i=1; i<=nlstate;i++)
prlim[i][i]=probs[(int)age][i][ij];
@@ -1684,7 +1687,7 @@ 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);
@@ -1876,138 +1879,655 @@ if (i== 4) fprintf(ficresprob,"%.3e %.3e
}
free_vector(xp,1,npar);
fclose(ficresprob);
- exit(0);
-}
-
-/***********************************************/
-/**************** Main Program *****************/
-/***********************************************/
-
-int main(int argc, char *argv[])
-{
- int i,j, k, n=MAXN,iter,m,size,cptcode, cptcod;
- double agedeb, agefin,hf;
- double agemin=1.e20, agemax=-1.e20;
+}
- double fret;
- double **xi,tmp,delta;
+/******************* 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 optionfile[],char optionfilehtm[] ){
+ int jj1, k1, i1, cpt;
+ FILE *fichtm;
+ /*char optionfilehtm[FILENAMELENGTH];*/
- double dum; /* Dummy variable */
- double ***p3mat;
- int *indx;
- char line[MAXLINE], linepar[MAXLINE];
- char title[MAXLINE];
- char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH];
- char optionfilext[10], optionfilefiname[FILENAMELENGTH], optionfilegnuplot[FILENAMELENGTH], plotcmd[FILENAMELENGTH];
-
- char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH], fileresf[FILENAMELENGTH];;
+ strcpy(optionfilehtm,optionfile);
+ strcat(optionfilehtm,".htm");
+ if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
+ printf("Problem with %s \n",optionfilehtm), exit(0);
+ }
- char filerest[FILENAMELENGTH];
- char fileregp[FILENAMELENGTH];
- char popfile[FILENAMELENGTH];
- char path[80],pathc[80],pathcd[80],pathtot[80],model[20];
- int firstobs=1, lastobs=10;
- int sdeb, sfin; /* Status at beginning and end */
- int c, h , cpt,l;
- int ju,jl, mi;
- int i1,j1, k1,k2,k3,jk,aa,bb, stepsize, ij;
- int jnais,jdc,jint4,jint1,jint2,jint3,**outcome,**adl,*tab;
- int mobilav=0,popforecast=0;
- int hstepm, nhstepm;
- int *popage;/*boolprev=0 if date and zero if wave*/
- double jprev1, mprev1,anprev1,jprev2, mprev2,anprev2;
+ fprintf(fichtm,"
Imach, Version 0.7
+Title=%s
Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s
- double bage, fage, age, agelim, agebase;
- double ftolpl=FTOL;
- double **prlim;
- double *severity;
- double ***param; /* Matrix of parameters */
- double *p;
- double **matcov; /* Matrix of covariance */
- double ***delti3; /* Scale */
- double *delti; /* Scale */
- double ***eij, ***vareij;
- double **varpl; /* Variances of prevalence limits by age */
- double *epj, vepp;
- double kk1, kk2;
- double *popeffectif,*popcount;
- double dateprev1, dateprev2,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,jprojmean,mprojmean,anprojmean, calagedate;
- double yp,yp1,yp2;
+Total number of observations=%d
+Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf
+
+- Outputs files
\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
+ - Prevalences and population forecasting: f%s
+
",title,datafile,firstpass,lastpass,stepm, weightopt,model,imx,jmin,jmax,jmean,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres);
+
+fprintf(fichtm," - Graphs
");
- char version[80]="Imach version 0.7, February 2002, INED-EUROREVES ";
- char *alph[]={"a","a","b","c","d","e"}, str[4];
+ 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,"
************ 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
");
+ }
+ fprintf(fichtm,"
- Probabilities: pe%s%d.gif
+",strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1);
+ for(cpt=1; cpt- Prevalence of disability : p%s%d%d.gif
+",strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1);
+ }
+ 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,jj1,strtok(optionfile, "."),cpt,jj1);
+ }
+ for(cpt=1; cpt<=nlstate;cpt++) {
+ fprintf(fichtm,"\n
- Health life expectancies by age and initial health state (%d): exp%s%d%d.gif
+",cpt,strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1);
+ }
+ fprintf(fichtm,"\n
- Total life expectancy by age and
+health expectancies in states (1) and (2): e%s%d.gif
+",strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1);
+fprintf(fichtm,"\n");
+ }
+ }
+fclose(fichtm);
+}
- char z[1]="c", occ;
-#include
-#include
- char stra[80], strb[80], strc[80], strd[80],stre[80],modelsav[80];
-
- /* long total_usecs;
- struct timeval start_time, end_time;
-
- gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
+/******************* Gnuplot file **************/
+void printinggnuplot(char fileres[],char optionfilefiname[],char optionfile[],char optionfilegnuplot[], double agemin, double agemax, double fage , char pathc[], double p[]){
+ int m,cpt,k1,i,k,j,jk,k2,k3,ij,l;
- printf("\n%s",version);
- if(argc <=1){
- printf("\nEnter the parameter file name: ");
- scanf("%s",pathtot);
- }
- else{
- strcpy(pathtot,argv[1]);
+ strcpy(optionfilegnuplot,optionfilefiname);
+ strcat(optionfilegnuplot,".plt");
+ if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
+ printf("Problem with file %s",optionfilegnuplot);
}
- /*if(getcwd(pathcd, 80)!= NULL)printf ("Error pathcd\n");*/
- /*cygwin_split_path(pathtot,path,optionfile);
- printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
- /* cutv(path,optionfile,pathtot,'\\');*/
-
- split(pathtot,path,optionfile,optionfilext,optionfilefiname);
- printf("pathtot=%s, path=%s, optionfile=%s optionfilext=%s optionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
- chdir(path);
- replace(pathc,path);
-
-/*-------- arguments in the command line --------*/
- strcpy(fileres,"r");
- strcat(fileres, optionfilefiname);
- strcat(fileres,".txt"); /* Other files have txt extension */
+#ifdef windows
+ fprintf(ficgp,"cd \"%s\" \n",pathc);
+#endif
+m=pow(2,cptcoveff);
+
+ /* 1eme*/
+ for (cpt=1; cpt<= nlstate ; cpt ++) {
+ for (k1=1; k1<= m ; k1 ++) {
- /*---------arguments file --------*/
+#ifdef windows
+ fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter gif small size 400,300\nplot [%.f:%.f] \"vpl%s\" every :::%d::%d u 1:2 \"\%%lf",agemin,fage,fileres,k1-1,k1-1);
+#endif
+#ifdef unix
+fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nplot [%.f:%.f] \"vpl%s\" u 1:2 \"\%%lf",agemin,fage,fileres);
+#endif
- if((ficpar=fopen(optionfile,"r"))==NULL) {
- printf("Problem with optionfile %s\n",optionfile);
- goto end;
+for (i=1; i<= nlstate ; i ++) {
+ if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
+ else fprintf(ficgp," \%%*lf (\%%*lf)");
+}
+ fprintf(ficgp,"\" t\"Stationary prevalence\" w l 0,\"vpl%s\" every :::%d::%d u 1:($2+2*$3) \"\%%lf",fileres,k1-1,k1-1);
+ for (i=1; i<= nlstate ; i ++) {
+ if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
+ else fprintf(ficgp," \%%*lf (\%%*lf)");
+}
+ fprintf(ficgp,"\" t\"95\%% CI\" w l 1,\"vpl%s\" every :::%d::%d u 1:($2-2*$3) \"\%%lf",fileres,k1-1,k1-1);
+ for (i=1; i<= nlstate ; i ++) {
+ if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
+ else fprintf(ficgp," \%%*lf (\%%*lf)");
+}
+ fprintf(ficgp,"\" t\"\" w l 1,\"p%s\" every :::%d::%d u 1:($%d) t\"Observed prevalence \" w l 2",fileres,k1-1,k1-1,2+4*(cpt-1));
+#ifdef unix
+fprintf(ficgp,"\nset ter gif small size 400,300");
+#endif
+fprintf(ficgp,"\nset out \"v%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
+ }
}
+ /*2 eme*/
- strcpy(filereso,"o");
- strcat(filereso,fileres);
- if((ficparo=fopen(filereso,"w"))==NULL) {
- printf("Problem with Output resultfile: %s\n", filereso);goto end;
+ for (k1=1; k1<= m ; k1 ++) {
+ fprintf(ficgp,"set ylabel \"Years\" \nset ter gif small size 400,300\nplot [%.f:%.f] ",agemin,fage);
+
+ for (i=1; i<= nlstate+1 ; i ++) {
+ k=2*i;
+ fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:2 \"\%%lf",fileres,k1-1,k1-1);
+ for (j=1; j<= nlstate+1 ; j ++) {
+ if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
+ else fprintf(ficgp," \%%*lf (\%%*lf)");
+}
+ if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l ,");
+ else fprintf(ficgp,"\" t\"LE in state (%d)\" w l ,",i-1);
+ fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2-$3*2) \"\%%lf",fileres,k1-1,k1-1);
+ for (j=1; j<= nlstate+1 ; j ++) {
+ if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
+ else fprintf(ficgp," \%%*lf (\%%*lf)");
+}
+ fprintf(ficgp,"\" t\"\" w l 0,");
+ fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2+$3*2) \"\%%lf",fileres,k1-1,k1-1);
+ 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,");
+ }
+ fprintf(ficgp,"\nset out \"e%s%d.gif\" \nreplot\n\n",strtok(optionfile, "."),k1);
}
+
+ /*3eme*/
- /* Reads comments: lines beginning with '#' */
- while((c=getc(ficpar))=='#' && c!= EOF){
- ungetc(c,ficpar);
- fgets(line, MAXLINE, ficpar);
- puts(line);
- fputs(line,ficparo);
- }
- ungetc(c,ficpar);
+ for (k1=1; k1<= m ; k1 ++) {
+ for (cpt=1; cpt<= nlstate ; cpt ++) {
+ k=2+nlstate*(cpt-1);
+ fprintf(ficgp,"set ter gif small size 400,300\nplot [%.f:%.f] \"e%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",agemin,fage,fileres,k1-1,k1-1,k,cpt);
+ for (i=1; i< nlstate ; i ++) {
+ fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",fileres,k1-1,k1-1,k+i,cpt,i+1);
+ }
+ fprintf(ficgp,"\nset out \"exp%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
+ }
+ }
+
+ /* CV preval stat */
+ for (k1=1; k1<= m ; k1 ++) {
+ for (cpt=1; cpt=(agemin-((int)calagedate %12)/12.); agedeb--){
+ nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
+ nhstepm = nhstepm/hstepm;
+
+ 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);
+
+ for (h=0; h<=nhstepm; h++){
+ if (h==(int) (calagedate+YEARM*cpt)) {
+ fprintf(ficresf,"\n %.f ",agedeb+h*hstepm/YEARM*stepm);
+ }
+ for(j=1; j<=nlstate+ndeath;j++) {
+ kk1=0.;kk2=0;
+ for(i=1; i<=nlstate;i++) {
+ if (mobilav==1)
+ kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod];
+ else {
+ kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod];
+ }
+
+ }
+ if (h==(int)(calagedate+12*cpt)){
+ fprintf(ficresf," %.3f", kk1);
+
+ }
+ }
+ }
+ free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
+ }
+ }
+ }
+ }
+
+ if (mobilav==1) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
+
+ fclose(ficresf);
+}
+/************** Forecasting ******************/
+populforecast(char fileres[], double anproj1,double mproj1,double jproj1,double agemin, double agemax,double dateprev1, double dateprev2, int mobilav, double agedeb, double fage, int popforecast, char popfile[], double anproj2,double p[], int i2){
+
+ int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h;
+ int *popage;
+ double calagedate, agelim, kk1, kk2, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
+ double *popeffectif,*popcount;
+ double ***p3mat,***tabpop,***tabpopprev;
+ char filerespop[FILENAMELENGTH];
+
+tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
+tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
+ agelim=AGESUP;
+calagedate=(anproj1+mproj1/12.+jproj1/365.-dateintmean)*YEARM;
+
+ prevalence(agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax,mint,anint,dateprev1,dateprev2, calagedate);
+
+
+ strcpy(filerespop,"pop");
+ strcat(filerespop,fileres);
+ if((ficrespop=fopen(filerespop,"w"))==NULL) {
+ printf("Problem with forecast resultfile: %s\n", filerespop);
+ }
+ printf("Computing forecasting: result on file '%s' \n", filerespop);
+
+ if (cptcoveff==0) ncodemax[cptcoveff]=1;
+
+ if (mobilav==1) {
+ mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
+ movingaverage(agedeb, fage, agemin, mobaverage);
+ }
+
+ 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);
+ }
+ 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=(agemin-((int)calagedate %12)/12.); agedeb--){
+ nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
+ nhstepm = nhstepm/hstepm;
+
+ 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);
+
+ for (h=0; h<=nhstepm; h++){
+ if (h==(int) (calagedate+YEARM*cpt)) {
+ fprintf(ficrespop,"\n %.f ",agedeb+h*hstepm/YEARM*stepm);
+ }
+ for(j=1; j<=nlstate+ndeath;j++) {
+ kk1=0.;kk2=0;
+ for(i=1; i<=nlstate;i++) {
+ if (mobilav==1)
+ kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod];
+ else {
+ kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod];
+ }
+ }
+ if (h==(int)(calagedate+12*cpt)){
+ tabpop[(int)(agedeb)][j][cptcod]=kk1;
+ /*fprintf(ficrespop," %.3f", kk1);
+ if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*/
+ }
+ }
+ for(i=1; i<=nlstate;i++){
+ kk1=0.;
+ for(j=1; j<=nlstate;j++){
+ kk1= kk1+Tabpop[(int)(agedeb)][j][cptcod];
+ }
+ tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedate+12*cpt)*hstepm/YEARM*stepm-1)];
+ }
+
+if (h==(int)(calagedate+12*cpt)) for(j=1; j<=nlstate;j++) fprintf(ficrespop," %.3f",tabpopprev[(int)(agedeb+1)][j][cptcod]);
+ }
+ free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
+ }
+ }
+
+ /******/
+
+ for (cpt=1; cpt<=4;cpt++) {
+ fprintf(ficrespop,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+cpt);
+ for (agedeb=(fage-((int)calagedate %12/12.)); agedeb>=(agemin-((int)calagedate %12)/12.); agedeb--){
+ nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
+ nhstepm = nhstepm/hstepm;
+
+ 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);
+ for (h=0; h<=nhstepm; h++){
+ if (h==(int) (calagedate+YEARM*cpt)) {
+ fprintf(ficresf,"\n %.f ",agedeb+h*hstepm/YEARM*stepm);
+ }
+ for(j=1; j<=nlstate+ndeath;j++) {
+ kk1=0.;kk2=0;
+ for(i=1; i<=nlstate;i++) {
+ kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod];
+ }
+ if (h==(int)(calagedate+12*cpt)) fprintf(ficresf," %.3f", kk1);
+ }
+ }
+ free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
+ }
+ }
+ }
+ }
+
+ if (mobilav==1) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
+
+ if (popforecast==1) {
+ free_ivector(popage,0,AGESUP);
+ free_vector(popeffectif,0,AGESUP);
+ free_vector(popcount,0,AGESUP);
+ }
+ free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
+ free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
+ fclose(ficrespop);
+}
+
+/***********************************************/
+/**************** Main Program *****************/
+/***********************************************/
+
+int main(int argc, char *argv[])
+{
+
+ int i,j, k, n=MAXN,iter,m,size,cptcode, cptcod;
+ double agedeb, agefin,hf;
+ double agemin=1.e20, agemax=-1.e20;
+
+ double fret;
+ double **xi,tmp,delta;
+
+ double dum; /* Dummy variable */
+ double ***p3mat;
+ int *indx;
+ char line[MAXLINE], linepar[MAXLINE];
+ char title[MAXLINE];
+ char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH];
+ char optionfilext[10], optionfilefiname[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilegnuplot[FILENAMELENGTH], plotcmd[FILENAMELENGTH];
+
+ char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH];
+
+ char filerest[FILENAMELENGTH];
+ char fileregp[FILENAMELENGTH];
+ char popfile[FILENAMELENGTH];
+ char path[80],pathc[80],pathcd[80],pathtot[80],model[20];
+ int firstobs=1, lastobs=10;
+ int sdeb, sfin; /* Status at beginning and end */
+ int c, h , cpt,l;
+ int ju,jl, mi;
+ int i1,j1, k1,k2,k3,jk,aa,bb, stepsize, ij;
+ int jnais,jdc,jint4,jint1,jint2,jint3,**outcome,**adl,*tab;
+ int mobilav=0,popforecast=0;
+ int hstepm, nhstepm;
+ double jprev1, mprev1,anprev1,jprev2, mprev2,anprev2;
+
+ double bage, fage, age, agelim, agebase;
+ double ftolpl=FTOL;
+ double **prlim;
+ double *severity;
+ double ***param; /* Matrix of parameters */
+ double *p;
+ double **matcov; /* Matrix of covariance */
+ double ***delti3; /* Scale */
+ double *delti; /* Scale */
+ double ***eij, ***vareij;
+ double **varpl; /* Variances of prevalence limits by age */
+ double *epj, vepp;
+ double kk1, kk2;
+ double dateprev1, dateprev2,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2;
+
+
+ char version[80]="Imach version 0.7, February 2002, INED-EUROREVES ";
+ char *alph[]={"a","a","b","c","d","e"}, str[4];
+
+
+ char z[1]="c", occ;
+#include
+#include
+ char stra[80], strb[80], strc[80], strd[80],stre[80],modelsav[80];
+
+ /* long total_usecs;
+ struct timeval start_time, end_time;
+
+ gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
+
+
+ printf("\n%s",version);
+ if(argc <=1){
+ printf("\nEnter the parameter file name: ");
+ scanf("%s",pathtot);
+ }
+ else{
+ strcpy(pathtot,argv[1]);
+ }
+ /*if(getcwd(pathcd, 80)!= NULL)printf ("Error pathcd\n");*/
+ /*cygwin_split_path(pathtot,path,optionfile);
+ printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
+ /* cutv(path,optionfile,pathtot,'\\');*/
+
+ split(pathtot,path,optionfile,optionfilext,optionfilefiname);
+ printf("pathtot=%s, path=%s, optionfile=%s optionfilext=%s optionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
+ chdir(path);
+ replace(pathc,path);
+
+/*-------- arguments in the command line --------*/
+
+ strcpy(fileres,"r");
+ strcat(fileres, optionfilefiname);
+ strcat(fileres,".txt"); /* Other files have txt extension */
+
+ /*---------arguments file --------*/
+
+ if((ficpar=fopen(optionfile,"r"))==NULL) {
+ printf("Problem with optionfile %s\n",optionfile);
+ goto end;
+ }
+
+ strcpy(filereso,"o");
+ strcat(filereso,fileres);
+ if((ficparo=fopen(filereso,"w"))==NULL) {
+ printf("Problem with Output resultfile: %s\n", filereso);goto end;
+ }
+
+ /* Reads comments: lines beginning with '#' */
+ while((c=getc(ficpar))=='#' && c!= EOF){
+ ungetc(c,ficpar);
+ fgets(line, MAXLINE, ficpar);
+ puts(line);
+ fputs(line,ficparo);
+ }
+ ungetc(c,ficpar);
+
+ fscanf(ficpar,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%lf stepm=%d ncov=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n",title, datafile, &lastobs, &firstpass,&lastpass,&ftol, &stepm, &ncov, &nlstate,&ndeath, &maxwav, &mle, &weightopt,model);
+ printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncov=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncov, nlstate,ndeath, maxwav, mle, weightopt,model);
+ fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncov=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncov,nlstate,ndeath,maxwav, mle, weightopt,model);
+while((c=getc(ficpar))=='#' && c!= EOF){
+ ungetc(c,ficpar);
+ fgets(line, MAXLINE, ficpar);
+ puts(line);
+ fputs(line,ficparo);
+ }
+ ungetc(c,ficpar);
+
+
covar=matrix(0,NCOVMAX,1,n);
cptcovn=0;
if (strlen(model)>1) cptcovn=nbocc(model,'+')+1;
@@ -2420,276 +2940,106 @@ printf("Total number of individuals= %d,
fprintf(ficres," %.5e",delti[jk]);
jk++;
}
- printf("\n");
- fprintf(ficres,"\n");
- }
- }
- }
-
- k=1;
- fprintf(ficres,"# Covariance\n");
- printf("# Covariance\n");
- for(i=1;i<=npar;i++){
- /* if (k>nlstate) k=1;
- i1=(i-1)/(ncovmodel*nlstate)+1;
- fprintf(ficres,"%s%d%d",alph[k],i1,tab[i]);
- printf("%s%d%d",alph[k],i1,tab[i]);*/
- fprintf(ficres,"%3d",i);
- printf("%3d",i);
- for(j=1; j<=i;j++){
- fprintf(ficres," %.5e",matcov[i][j]);
- printf(" %.5e",matcov[i][j]);
- }
- fprintf(ficres,"\n");
- printf("\n");
- k++;
- }
-
- while((c=getc(ficpar))=='#' && c!= EOF){
- ungetc(c,ficpar);
- fgets(line, MAXLINE, ficpar);
- puts(line);
- fputs(line,ficparo);
- }
- ungetc(c,ficpar);
-
- fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf\n",&agemin,&agemax, &bage, &fage);
-
- if (fage <= 2) {
- bage = agemin;
- fage = agemax;
- }
-
- fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
- fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax,bage,fage);
- fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax,bage,fage);
-
- while((c=getc(ficpar))=='#' && c!= EOF){
- ungetc(c,ficpar);
- fgets(line, MAXLINE, ficpar);
- puts(line);
- fputs(line,ficparo);
- }
- ungetc(c,ficpar);
-
- fscanf(ficpar,"begin-prev-date=%lf/%lf/%lf end-prev-date=%lf/%lf/%lf mob_average=%d\n",&jprev1, &mprev1,&anprev1,&jprev2, &mprev2,&anprev2,&mobilav);
- fprintf(ficparo,"begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mob_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
- fprintf(ficres,"begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mob_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
-
- while((c=getc(ficpar))=='#' && c!= EOF){
- ungetc(c,ficpar);
- fgets(line, MAXLINE, ficpar);
- puts(line);
- fputs(line,ficparo);
- }
- ungetc(c,ficpar);
-
-
- dateprev1=anprev1+mprev1/12.+jprev1/365.;
- dateprev2=anprev2+mprev2/12.+jprev2/365.;
-
- fscanf(ficpar,"pop_based=%d\n",&popbased);
- fprintf(ficparo,"pop_based=%d\n",popbased);
- fprintf(ficres,"pop_based=%d\n",popbased);
-
- while((c=getc(ficpar))=='#' && c!= EOF){
- ungetc(c,ficpar);
- fgets(line, MAXLINE, ficpar);
- puts(line);
- fputs(line,ficparo);
- }
- ungetc(c,ficpar);
- fscanf(ficpar,"popforecast=%d popfile=%s starting-proj-date=%lf/%lf/%lf final-proj-date=%lf/%lf/%lf\n",&popforecast,popfile,&jproj1,&mproj1,&anproj1,&jproj2,&mproj2,&anproj2);
-fprintf(ficparo,"popforecast=%d popfile=%s starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf\n",popforecast,popfile,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2);
-fprintf(ficres,"popforecast=%d popfile=%s starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf\n",popforecast,popfile,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2);
-
- freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax,mint,anint,dateprev1,dateprev2);
-
-
- /*------------ gnuplot -------------*/
- /*chdir(pathcd);*/
- strcpy(optionfilegnuplot,optionfilefiname);
- strcat(optionfilegnuplot,".plt");
- if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
- printf("Problem with file %s",optionfilegnuplot);goto end;
- }
-#ifdef windows
- fprintf(ficgp,"cd \"%s\" \n",pathc);
-#endif
-m=pow(2,cptcoveff);
-
- /* 1eme*/
- for (cpt=1; cpt<= nlstate ; cpt ++) {
- for (k1=1; k1<= m ; k1 ++) {
-
-#ifdef windows
- fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter gif small size 400,300\nplot [%.f:%.f] \"vpl%s\" every :::%d::%d u 1:2 \"\%%lf",agemin,fage,fileres,k1-1,k1-1);
-#endif
-#ifdef unix
-fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nplot [%.f:%.f] \"vpl%s\" u 1:2 \"\%%lf",agemin,fage,fileres);
-#endif
-
-for (i=1; i<= nlstate ; i ++) {
- if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
- else fprintf(ficgp," \%%*lf (\%%*lf)");
-}
- fprintf(ficgp,"\" t\"Stationary prevalence\" w l 0,\"vpl%s\" every :::%d::%d u 1:($2+2*$3) \"\%%lf",fileres,k1-1,k1-1);
- for (i=1; i<= nlstate ; i ++) {
- if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
- else fprintf(ficgp," \%%*lf (\%%*lf)");
-}
- fprintf(ficgp,"\" t\"95\%% CI\" w l 1,\"vpl%s\" every :::%d::%d u 1:($2-2*$3) \"\%%lf",fileres,k1-1,k1-1);
- for (i=1; i<= nlstate ; i ++) {
- if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
- else fprintf(ficgp," \%%*lf (\%%*lf)");
-}
- fprintf(ficgp,"\" t\"\" w l 1,\"p%s\" every :::%d::%d u 1:($%d) t\"Observed prevalence \" w l 2",fileres,k1-1,k1-1,2+4*(cpt-1));
-#ifdef unix
-fprintf(ficgp,"\nset ter gif small size 400,300");
-#endif
-fprintf(ficgp,"\nset out \"v%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
- }
- }
- /*2 eme*/
-
- for (k1=1; k1<= m ; k1 ++) {
- fprintf(ficgp,"set ylabel \"Years\" \nset ter gif small size 400,300\nplot [%.f:%.f] ",agemin,fage);
-
- for (i=1; i<= nlstate+1 ; i ++) {
- k=2*i;
- fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:2 \"\%%lf",fileres,k1-1,k1-1);
- for (j=1; j<= nlstate+1 ; j ++) {
- if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
- else fprintf(ficgp," \%%*lf (\%%*lf)");
-}
- if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l ,");
- else fprintf(ficgp,"\" t\"LE in state (%d)\" w l ,",i-1);
- fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2-$3*2) \"\%%lf",fileres,k1-1,k1-1);
- for (j=1; j<= nlstate+1 ; j ++) {
- if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
- else fprintf(ficgp," \%%*lf (\%%*lf)");
-}
- fprintf(ficgp,"\" t\"\" w l 0,");
- fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2+$3*2) \"\%%lf",fileres,k1-1,k1-1);
- 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,");
- }
- fprintf(ficgp,"\nset out \"e%s%d.gif\" \nreplot\n\n",strtok(optionfile, "."),k1);
- }
-
- /*3eme*/
-
- for (k1=1; k1<= m ; k1 ++) {
- for (cpt=1; cpt<= nlstate ; cpt ++) {
- k=2+nlstate*(cpt-1);
- fprintf(ficgp,"set ter gif small size 400,300\nplot [%.f:%.f] \"e%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",agemin,fage,fileres,k1-1,k1-1,k,cpt);
- for (i=1; i< nlstate ; i ++) {
- fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",fileres,k1-1,k1-1,k+i,cpt,i+1);
- }
- fprintf(ficgp,"\nset out \"exp%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
- }
- }
-
- /* CV preval stat */
- for (k1=1; k1<= m ; k1 ++) {
- for (cpt=1; cptnlstate) k=1;
+ i1=(i-1)/(ncovmodel*nlstate)+1;
+ fprintf(ficres,"%s%d%d",alph[k],i1,tab[i]);
+ printf("%s%d%d",alph[k],i1,tab[i]);*/
+ fprintf(ficres,"%3d",i);
+ printf("%3d",i);
+ for(j=1; j<=i;j++){
+ fprintf(ficres," %.5e",matcov[i][j]);
+ printf(" %.5e",matcov[i][j]);
+ }
+ fprintf(ficres,"\n");
+ printf("\n");
+ k++;
+ }
+
+ while((c=getc(ficpar))=='#' && c!= EOF){
+ ungetc(c,ficpar);
+ fgets(line, MAXLINE, ficpar);
+ puts(line);
+ fputs(line,ficparo);
+ }
+ ungetc(c,ficpar);
+
+ fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf\n",&agemin,&agemax, &bage, &fage);
+
+ if (fage <= 2) {
+ bage = agemin;
+ fage = agemax;
+ }
+
+ fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
+ fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax,bage,fage);
+ fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax,bage,fage);
+
+ while((c=getc(ficpar))=='#' && c!= EOF){
+ ungetc(c,ficpar);
+ fgets(line, MAXLINE, ficpar);
+ puts(line);
+ fputs(line,ficparo);
}
-
- fclose(ficgp);
- /* end gnuplot */
-
-chdir(path);
-
- free_ivector(wav,1,imx);
- free_imatrix(dh,1,lastpass-firstpass+1,1,imx);
- free_imatrix(mw,1,lastpass-firstpass+1,1,imx);
- free_ivector(num,1,n);
- free_vector(agedc,1,n);
- /*free_matrix(covar,1,NCOVMAX,1,n);*/
- fclose(ficparo);
- fclose(ficres);
- /* }*/
-
- /*________fin mle=1_________*/
-
-
+ ungetc(c,ficpar);
- /* No more information from the sample is required now */
+ fscanf(ficpar,"begin-prev-date=%lf/%lf/%lf end-prev-date=%lf/%lf/%lf\n",&jprev1, &mprev1,&anprev1,&jprev2, &mprev2,&anprev2);
+ fprintf(ficparo,"begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);
+ fprintf(ficres,"begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);
+
+ while((c=getc(ficpar))=='#' && c!= EOF){
+ ungetc(c,ficpar);
+ fgets(line, MAXLINE, ficpar);
+ puts(line);
+ fputs(line,ficparo);
+ }
+ ungetc(c,ficpar);
+
+
+ dateprev1=anprev1+mprev1/12.+jprev1/365.;
+ dateprev2=anprev2+mprev2/12.+jprev2/365.;
+
+ fscanf(ficpar,"pop_based=%d\n",&popbased);
+ fprintf(ficparo,"pop_based=%d\n",popbased);
+ fprintf(ficres,"pop_based=%d\n",popbased);
+
+ while((c=getc(ficpar))=='#' && c!= EOF){
+ ungetc(c,ficpar);
+ fgets(line, MAXLINE, ficpar);
+ puts(line);
+ fputs(line,ficparo);
+ }
+ ungetc(c,ficpar);
+ fscanf(ficpar,"popforecast=%d popfile=%s starting-proj-date=%lf/%lf/%lf final-proj-date=%lf/%lf/%lf mov_average=%d\n",&popforecast,popfile,&jproj1,&mproj1,&anproj1,&jproj2,&mproj2,&anproj2,&mobilav);
+fprintf(ficparo,"popforecast=%d popfile=%s starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mov_average=%d\n",popforecast,popfile,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilav);
+fprintf(ficres,"popforecast=%d popfile=%s starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mov_average=%d\n",popforecast,popfile,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilav);
+
+ freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax,mint,anint,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);
+
+/*------------ gnuplot -------------*/
+ printinggnuplot(fileres,optionfilefiname,optionfile,optionfilegnuplot, agemin,agemax,fage, pathc,p);
+
+/*------------ free_vector -------------*/
+ chdir(path);
+
+ free_ivector(wav,1,imx);
+ free_imatrix(dh,1,lastpass-firstpass+1,1,imx);
+ free_imatrix(mw,1,lastpass-firstpass+1,1,imx);
+ free_ivector(num,1,n);
+ free_vector(agedc,1,n);
+ /*free_matrix(covar,1,NCOVMAX,1,n);*/
+ fclose(ficparo);
+ fclose(ficres);
+
/* Reads comments: lines beginning with '#' */
while((c=getc(ficpar))=='#' && c!= EOF){
ungetc(c,ficpar);
@@ -2704,68 +3054,9 @@ chdir(path);
fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax,bage,fage);
/*--------- index.htm --------*/
- strcpy(optionfilehtm,optionfile);
- strcat(optionfilehtm,".htm");
- if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
- printf("Problem with %s \n",optionfilehtm);goto end;
- }
-
- fprintf(fichtm," Imach, Version 0.7
-Titre=%s
Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s
-Total number of observations=%d
-Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf
-
-- Outputs files
\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
- - Prevalences and population forecasting: f%s
-
",title,datafile,firstpass,lastpass,stepm, weightopt,model,imx,jmin,jmax,jmean,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres);
-
- fprintf(fichtm," - Graphs
");
-
- m=cptcoveff;
- 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,"
************ Results for covariates");
- for (cpt=1; cpt<=cptcoveff;cpt++)
- fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtab[j1][cpt]]);
- fprintf(fichtm," ************\n
");
- }
- fprintf(fichtm,"
- Probabilities: pe%s%d.gif
-",strtok(optionfile, "."),j1,strtok(optionfile, "."),j1);
- for(cpt=1; cpt- Prevalence of disability : p%s%d%d.gif
-",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);
+ printinghtml(fileres,title,datafile, firstpass, lastpass, stepm, weightopt,model,imx,jmin,jmax,jmean,optionfile,optionfilehtm);
+
/*--------------- Prevalence limit --------------*/
strcpy(filerespl,"pl");
@@ -2864,152 +3155,19 @@ fclose(fichtm);
fclose(ficrespij);
- if(stepm == 1) {
- /*---------- Forecasting ------------------*/
- calagedate=(anproj1+mproj1/12.+jproj1/365.-dateintmean)*YEARM;
-
- /*printf("calage= %f", calagedate);*/
-
- prevalence(agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax,mint,anint,dateprev1,dateprev2, calagedate);
-
-
- strcpy(fileresf,"f");
- strcat(fileresf,fileres);
- if((ficresf=fopen(fileresf,"w"))==NULL) {
- printf("Problem with forecast resultfile: %s\n", fileresf);goto end;
- }
- printf("Computing forecasting: result on file '%s' \n", fileresf);
-
- free_matrix(mint,1,maxwav,1,n);
- free_matrix(anint,1,maxwav,1,n);
- free_matrix(agev,1,maxwav,1,imx);
- /* Mobile average */
-
- if (cptcoveff==0) ncodemax[cptcoveff]=1;
-
- if (mobilav==1) {
- mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
- for (agedeb=bage+3; agedeb<=fage-2; agedeb++)
- for (i=1; i<=nlstate;i++)
- for (cptcod=1;cptcod<=ncodemax[cptcov];cptcod++)
- mobaverage[(int)agedeb][i][cptcod]=0.;
-
- for (agedeb=bage+4; agedeb<=fage; agedeb++){
- for (i=1; i<=nlstate;i++){
- for (cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
- for (cpt=0;cpt<=4;cpt++){
- mobaverage[(int)agedeb-2][i][cptcod]=mobaverage[(int)agedeb-2][i][cptcod]+probs[(int)agedeb-cpt][i][cptcod];
- }
- mobaverage[(int)agedeb-2][i][cptcod]=mobaverage[(int)agedeb-2][i][cptcod]/5;
- }
- }
- }
- }
-
- stepsize=(int) (stepm+YEARM-1)/YEARM;
- if (stepm<=12) stepsize=1;
-
- agelim=AGESUP;
- /*hstepm=stepsize*YEARM; *//* Every year of age */
- hstepm=1;
- hstepm=hstepm/stepm; /* Typically 2 years, = 2 years/6 months = 4 */
- yp1=modf(dateintmean,&yp);
- 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;
-
- fprintf(ficresf,"# Estimated date of observed prevalence: %.lf/%.lf/%.lf ",jprojmean,mprojmean,anprojmean);
-
- if (popforecast==1) {
- if((ficpop=fopen(popfile,"r"))==NULL) {
- printf("Problem with population file : %s\n",popfile);goto end;
- }
- 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=(bage-((int)calagedate %12)/12.); agedeb--){ /* If stepm=6 months */
- nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
- nhstepm = nhstepm/hstepm;
- /*printf("agedeb=%.lf stepm=%d hstepm=%d nhstepm=%d \n",agedeb,stepm,hstepm,nhstepm);*/
-
- 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);
-
- for (h=0; h<=nhstepm; h++){
- if (h==(int) (calagedate+YEARM*cpt)) {
- fprintf(ficresf,"\n %.f ",agedeb+h*hstepm/YEARM*stepm);
- }
- for(j=1; j<=nlstate+ndeath;j++) {
- kk1=0.;kk2=0;
- for(i=1; i<=nlstate;i++) {
- if (mobilav==1)
- kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod];
- else {
- kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod];
- /* fprintf(ficresf," p3=%.3f p=%.3f ", p3mat[i][j][h], probs[(int)(agedeb)+1][i][cptcod]);*/
- }
- if (popforecast==1) kk2=kk1*popeffectif[(int)agedeb];
- }
-
- if (h==(int)(calagedate+12*cpt)){
- fprintf(ficresf," %.3f", kk1);
-
- if (popforecast==1) fprintf(ficresf," [%.f]", kk2);
- }
- }
- }
- free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
- }
- }
- }
- }
- if (mobilav==1) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
- if (popforecast==1) {
- free_ivector(popage,0,AGESUP);
- free_vector(popeffectif,0,AGESUP);
- free_vector(popcount,0,AGESUP);
- }
- free_imatrix(s,1,maxwav+1,1,n);
- free_vector(weight,1,n);
- fclose(ficresf);
- }/* End forecasting */
+ /*---------- Forecasting ------------------*/
+ if(stepm == 1) {
+ prevforecast(fileres, anproj1,mproj1,jproj1, agemin,agemax, dateprev1, dateprev2,mobilav, agedeb, fage, popforecast, popfile, anproj2,p, i1);
+populforecast(fileres, anproj1,mproj1,jproj1, agemin,agemax, dateprev1, dateprev2,mobilav, agedeb, fage, popforecast, popfile, anproj2,p, i1);
+ free_matrix(mint,1,maxwav,1,n);
+ free_matrix(anint,1,maxwav,1,n); free_imatrix(s,1,maxwav+1,1,n);
+ free_vector(weight,1,n);}
else{
erreur=108;
printf("Error %d!! You can only forecast the prevalences if the optimization\n has been performed with stepm = 1 (month) instead of %d\n", erreur, stepm);
}
+
/*---------- Health expectancies and variances ------------*/
@@ -3059,12 +3217,14 @@ fclose(fichtm);
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);
-
+ 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);
@@ -3093,20 +3253,16 @@ fclose(fichtm);
}
}
}
-
-
-
- fclose(ficreseij);
- fclose(ficresvij);
+ fclose(ficreseij);
+ fclose(ficresvij);
fclose(ficrest);
fclose(ficpar);
free_vector(epj,1,nlstate+1);
- /* scanf("%d ",i); */
-
+
/*------- Variance limit prevalence------*/
-strcpy(fileresvpl,"vpl");
+ strcpy(fileresvpl,"vpl");
strcat(fileresvpl,fileres);
if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
printf("Problem with variance prev lim resultfile: %s\n", fileresvpl);
@@ -3114,19 +3270,19 @@ strcpy(fileresvpl,"vpl");
}
printf("Computing Variance-covariance of Prevalence limit: file '%s' \n", fileresvpl);
- 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<=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;
+ 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<=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);
- }
+ }
}
fclose(ficresvpl);
@@ -3145,7 +3301,7 @@ strcpy(fileresvpl,"vpl");
free_matrix(matcov,1,npar,1,npar);
free_vector(delti,1,npar);
-
+ free_matrix(agev,1,maxwav,1,imx);
free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
if(erreur >0)