--- imach/src/imach.c 2003/05/27 17:26:53 1.78 +++ imach/src/imach.c 2003/06/23 17:54:56 1.88 @@ -1,4 +1,44 @@ -/* $Id: imach.c,v 1.78 2003/05/27 17:26:53 brouard Exp $ +/* $Id: imach.c,v 1.88 2003/06/23 17:54:56 brouard Exp $ + $State: Exp $ + $Log: imach.c,v $ + Revision 1.88 2003/06/23 17:54:56 brouard + * imach.c (Repository): Create a sub-directory where all the secondary files are. Only imach, htm, gp and r(imach) are on the main directory. Correct time and other things. + + Revision 1.87 2003/06/18 12:26:01 brouard + Version 0.96 + + Revision 1.86 2003/06/17 20:04:08 brouard + (Module): Change position of html and gnuplot routines and added + routine fileappend. + + Revision 1.85 2003/06/17 13:12:43 brouard + * imach.c (Repository): Check when date of death was earlier that + current date of interview. It may happen when the death was just + prior to the death. In this case, dh was negative and likelihood + was wrong (infinity). We still send an "Error" but patch by + assuming that the date of death was just one stepm after the + interview. + (Repository): Because some people have very long ID (first column) + we changed int to long in num[] and we added a new lvector for + memory allocation. But we also truncated to 8 characters (left + truncation) + (Repository): No more line truncation errors. + + Revision 1.84 2003/06/13 21:44:43 brouard + * imach.c (Repository): Replace "freqsummary" at a correct + place. It differs from routine "prevalence" which may be called + many times. Probs is memory consuming and must be used with + parcimony. + Version 0.95a3 (should output exactly the same maximization than 0.8a2) + + Revision 1.83 2003/06/10 13:39:11 lievre + *** empty log message *** + + Revision 1.82 2003/06/05 15:57:20 brouard + Add log in imach.c and fullversion number is now printed. + +*/ +/* Interpolated Markov Chain Short summary of the programme: @@ -58,6 +98,7 @@ read parameterfile read datafile concatwav + freqsummary if (mle >= 1) mlikeli print results files @@ -91,12 +132,16 @@ #include #include +#include +#include +#include "timeval.h" + #define MAXLINE 256 #define GNUPLOTPROGRAM "gnuplot" /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/ -#define FILENAMELENGTH 80 +#define FILENAMELENGTH 132 /*#define DEBUG*/ -#define windows +/*#define windows*/ #define GLOCK_ERROR_NOPATH -1 /* empty path */ #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */ @@ -111,15 +156,19 @@ #define YEARM 12. /* Number of months per year */ #define AGESUP 130 #define AGEBASE 40 -#ifdef windows -#define DIRSEPARATOR '\\' -#define ODIRSEPARATOR '/' -#else +#ifdef unix #define DIRSEPARATOR '/' #define ODIRSEPARATOR '\\' +#else +#define DIRSEPARATOR '\\' +#define ODIRSEPARATOR '/' #endif -char version[80]="Imach version 0.95a, May 2003, INED-EUROREVES "; +/* $Id: imach.c,v 1.88 2003/06/23 17:54:56 brouard Exp $ */ +/* $State: Exp $ */ + +char version[]="Imach version 0.96a, June 2003, INED-EUROREVES "; +char fullversion[]="$Revision: 1.88 $ $Date: 2003/06/23 17:54:56 $"; int erreur; /* Error number */ int nvar; int cptcovn=0, cptcovage=0, cptcoveff=0,cptcov; @@ -132,6 +181,8 @@ int popbased=0; int *wav; /* Number of waves for this individuual 0 is possible */ int maxwav; /* Maxim number of waves */ int jmin, jmax; /* min, max spacing between 2 waves */ +int gipmx, gsw; /* Global variables on the number of contributions + to the likelihood and the sum of weights (done by funcone)*/ int mle, weightopt; int **mw; /* mw[mi][i] is number of the mi wave for this individual */ int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */ @@ -142,6 +193,12 @@ double **oldm, **newm, **savm; /* Workin double **oldms, **newms, **savms; /* Fixed working pointers to matrices */ FILE *fic,*ficpar, *ficparo,*ficres, *ficrespl, *ficrespij, *ficrest,*ficresf,*ficrespop; FILE *ficlog, *ficrespow; +int globpr; /* Global variable for printing or not */ +double fretone; /* Only one call to likelihood */ +long ipmx; /* Number of contributions */ +double sw; /* Sum of weights */ +char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */ +FILE *ficresilk; FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor; FILE *ficresprobmorprev; FILE *fichtm; /* Html File */ @@ -154,8 +211,12 @@ char fileresvpl[FILENAMELENGTH]; char title[MAXLINE]; char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH]; char optionfilext[10], optionfilefiname[FILENAMELENGTH], plotcmd[FILENAMELENGTH]; +char tmpout[FILENAMELENGTH]; +char command[FILENAMELENGTH]; +int outcmd=0; char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH]; +char lfileres[FILENAMELENGTH]; char filelog[FILENAMELENGTH]; /* Log file */ char filerest[FILENAMELENGTH]; char fileregp[FILENAMELENGTH]; @@ -199,7 +260,8 @@ int estepm; /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/ int m,nb; -int *num, firstpass=0, lastpass=4,*cod, *ncodemax, *Tage; +long *num; +int firstpass=0, lastpass=4,*cod, *ncodemax, *Tage; double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint; double **pmmij, ***probs; double dateintmean=0; @@ -239,11 +301,12 @@ static int split( char *path, char *dirc dirc[l1-l2] = 0; /* add zero */ } l1 = strlen( dirc ); /* length of directory */ -#ifdef windows + /*#ifdef windows if ( dirc[l1-1] != '\\' ) { dirc[l1] = '\\'; dirc[l1+1] = 0; } #else if ( dirc[l1-1] != '/' ) { dirc[l1] = '/'; dirc[l1+1] = 0; } #endif + */ ss = strrchr( name, '.' ); /* find last / */ ss++; strcpy(ext,ss); /* save extension */ @@ -327,31 +390,31 @@ void free_vector(double*v, int nl, int n } /************************ivector *******************************/ -char *cvector(long nl,long nh) +int *ivector(long nl,long nh) { - char *v; - v=(char *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(char))); - if (!v) nrerror("allocation failure in cvector"); + int *v; + v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int))); + if (!v) nrerror("allocation failure in ivector"); return v-nl+NR_END; } /******************free ivector **************************/ -void free_cvector(char *v, long nl, long nh) +void free_ivector(int *v, long nl, long nh) { free((FREE_ARG)(v+nl-NR_END)); } -/************************ivector *******************************/ -int *ivector(long nl,long nh) +/************************lvector *******************************/ +long *lvector(long nl,long nh) { - int *v; - v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int))); + 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 ivector **************************/ -void free_ivector(int *v, long nl, long nh) +/******************free lvector **************************/ +void free_lvector(long *v, long nl, long nh) { free((FREE_ARG)(v+nl-NR_END)); } @@ -410,7 +473,7 @@ 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) ou print m[1][70]; print m+1 or print &(m[1]) + /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) */ } @@ -1163,7 +1226,42 @@ double func( double *x) ll[s[mw[mi][i]][i]] += 2*weight[i]*lli; } /* end of wave */ } /* end of individual */ - }else{ /* ml=4 no inter-extrapolation */ + }else if (mle==4){ /* ml=4 no inter-extrapolation */ + for (i=1,ipmx=0, sw=0.; i<=imx; i++){ + for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i]; + for(mi=1; mi<= wav[i]-1; mi++){ + for (ii=1;ii<=nlstate+ndeath;ii++) + for (j=1;j<=nlstate+ndeath;j++){ + oldm[ii][j]=(ii==j ? 1.0 : 0.0); + savm[ii][j]=(ii==j ? 1.0 : 0.0); + } + for(d=0; d nlstate){ + lli=log(out[s1][s2] - savm[s1][s2]); + }else{ + lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */ + } + ipmx +=1; + sw += weight[i]; + ll[s[mw[mi][i]][i]] += 2*weight[i]*lli; +/* printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */ + } /* end of wave */ + } /* end of individual */ + }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */ for (i=1,ipmx=0, sw=0.; i<=imx; i++){ for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i]; for(mi=1; mi<= wav[i]-1; mi++){ @@ -1185,10 +1283,13 @@ double func( double *x) oldm=newm; } /* end mult */ + s1=s[mw[mi][i]][i]; + s2=s[mw[mi+1][i]][i]; lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */ ipmx +=1; sw += weight[i]; ll[s[mw[mi][i]][i]] += 2*weight[i]*lli; + /*printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]);*/ } /* end of wave */ } /* end of individual */ } /* End of if */ @@ -1198,6 +1299,155 @@ double func( double *x) return -l; } +/*************** log-likelihood *************/ +double funcone( double *x) +{ + /* Same as likeli but slower because of a lot of printf and if */ + int i, ii, j, k, mi, d, kk; + double l, ll[NLSTATEMAX], cov[NCOVMAX]; + double **out; + double lli; /* Individual log likelihood */ + double llt; + int s1, s2; + double bbh, survp; + /*extern weight */ + /* We are differentiating ll according to initial status */ + /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/ + /*for(i=1;i nlstate && (mle <5) ){ /* Jackson */ + lli=log(out[s1][s2] - savm[s1][s2]); + } else if (mle==1){ + lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */ + } else if(mle==2){ + lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* linear interpolation */ + } else if(mle==3){ /* exponential inter-extrapolation */ + lli= (savm[s1][s2]>(double)1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* exponential inter-extrapolation */ + } else if (mle==4){ /* mle=4 no inter-extrapolation */ + lli=log(out[s1][s2]); /* Original formula */ + } else{ /* ml>=5 no inter-extrapolation no jackson =0.8a */ + lli=log(out[s1][s2]); /* Original formula */ + } /* End of if */ + ipmx +=1; + sw += weight[i]; + ll[s[mw[mi][i]][i]] += 2*weight[i]*lli; +/* printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */ + if(globpr){ + fprintf(ficresilk,"%9d %6d %1d %1d %1d %1d %3d %10.6f %6.4f\ + %10.6f %10.6f %10.6f ", \ + num[i],i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i], + 2*weight[i]*lli,out[s1][s2],savm[s1][s2]); + for(k=1,llt=0.,l=0.; k<=nlstate; k++){ + llt +=ll[k]*gipmx/gsw; + fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw); + } + fprintf(ficresilk," %10.6f\n", -llt); + } + } /* end of wave */ + } /* end of individual */ + for(k=1,l=0.; k<=nlstate; k++) l += ll[k]; + /* printf("l1=%f l2=%f ",ll[1],ll[2]); */ + l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */ + if(globpr==0){ /* First time we count the contributions and weights */ + gipmx=ipmx; + gsw=sw; + } + return -l; +} + +char *subdirf(char fileres[]) +{ + + strcpy(tmpout,optionfilefiname); + strcat(tmpout,"/"); /* Add to the right */ + strcat(tmpout,fileres); + return tmpout; +} + +char *subdirf2(char fileres[], char *preop) +{ + + strcpy(tmpout,optionfilefiname); + strcat(tmpout,"/"); + strcat(tmpout,preop); + strcat(tmpout,fileres); + return tmpout; +} +char *subdirf3(char fileres[], char *preop, char *preop2) +{ + + strcpy(tmpout,optionfilefiname); + strcat(tmpout,"/"); + strcat(tmpout,preop); + strcat(tmpout,preop2); + strcat(tmpout,fileres); + return tmpout; +} + +void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*funcone)(double [])) +{ + /* This routine should help understanding what is done with + the selection of individuals/waves and + to check the exact contribution to the likelihood. + Plotting could be done. + */ + int k; + + if(*globpri !=0){ /* Just counts and sums, no printings */ + strcpy(fileresilk,"ilk"); + strcat(fileresilk,fileres); + if((ficresilk=fopen(fileresilk,"w"))==NULL) { + printf("Problem with resultfile: %s\n", fileresilk); + fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk); + } + fprintf(ficresilk, "#individual(line's_record) s1 s2 wave# effective_wave# number_of_matrices_product pij weight -2ln(pij)*weight 0pij_x 0pij_(x-stepm) cumulating_loglikeli_by_health_state(reweighted=-2ll*weightXnumber_of_contribs/sum_of_weights) and_total\n"); + fprintf(ficresilk, "#num_i i s1 s2 mi mw dh likeli weight 2wlli out sav "); + /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */ + for(k=1; k<=nlstate; k++) + fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k); + fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n"); + } + + *fretone=(*funcone)(p); + if(*globpri !=0){ + fclose(ficresilk); + fprintf(fichtm,"\n
File of contributions to the likelihood: %s
\n",subdirf(fileresilk),subdirf(fileresilk)); + fflush(fichtm); + } + return; +} + /*********** Maximum Likelihood Estimation ***************/ @@ -1206,6 +1456,7 @@ void mlikeli(FILE *ficres,double p[], in int i,j, iter; 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++) @@ -1223,6 +1474,7 @@ void mlikeli(FILE *ficres,double p[], in 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); fclose(ficrespow); @@ -1490,7 +1742,7 @@ void lubksb(double **a, int n, int *indx } /************ Frequencies ********************/ -void freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, int *Tvaraff, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2,double jprev1, double mprev1,double anprev1,double jprev2, double mprev2,double anprev2) +void freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, int *Tvaraff, int **nbcode, int *ncodemax,double **mint,double **anint) { /* Some frequencies */ int i, m, jk, k1,i1, j1, bool, z1,z2,j; @@ -1544,7 +1796,7 @@ void freqsummary(char fileres[], int ia if (bool==1){ for(m=firstpass; m<=lastpass; m++){ k2=anint[m][i]+(mint[m][i]/12.); - if ((k2>=dateprev1) && (k2<=dateprev2)) { + /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/ if(agev[m][i]==0) agev[m][i]=iagemax+1; if(agev[m][i]==1) agev[m][i]=iagemax+2; if (s[m][i]>0 && s[m][i]<=nlstate) prop[s[m][i]][(int)agev[m][i]] += weight[i]; @@ -1557,12 +1809,12 @@ void freqsummary(char fileres[], int ia dateintsum=dateintsum+k2; k2cpt++; } - } + /*}*/ } } } - fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); + /* 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 "); @@ -1623,7 +1875,7 @@ void freqsummary(char fileres[], int ia if( i <= iagemax){ if(pos>=1.e-5){ fprintf(ficresp," %d %.5f %.0f %.0f",i,prop[jk][i]/posprop, prop[jk][i],posprop); - probs[i][jk][j1]= pp[jk]/pos; + /*probs[i][jk][j1]= pp[jk]/pos;*/ /*printf("\ni=%d jk=%d j1=%d %.5f %.0f %.0f %f",i,jk,j1,pp[jk]/pos, pp[jk],pos,probs[i][jk][j1]);*/ } else @@ -1656,7 +1908,7 @@ void freqsummary(char fileres[], int ia } /************ Prevalence ********************/ -void prevalence(double agemin, double agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2, int firstpass, int lastpass) +void prevalence(double ***probs, double agemin, double agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2, int firstpass, int lastpass) { /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people in each health status at the date of interview (if between dateprev1 and dateprev2). @@ -1775,11 +2027,11 @@ void concatwav(int wav[], int **dh, int wav[i]=mi; if(mi==0){ if(first==0){ - printf("Warning! None valid information for:%d line=%d (skipped) and may be others, see log file\n",num[i],i); + printf("Warning! None valid information for:%ld line=%d (skipped) and may be others, see log file\n",num[i],i); first=1; } if(first==1){ - fprintf(ficlog,"Warning! None valid information for:%d line=%d (skipped)\n",num[i],i); + fprintf(ficlog,"Warning! None valid information for:%ld line=%d (skipped)\n",num[i],i); } } /* end mi==0 */ } /* End individuals */ @@ -1791,15 +2043,21 @@ void concatwav(int wav[], int **dh, int else{ if (s[mw[mi+1][i]][i] > nlstate) { /* A death */ if (agedc[i] < 2*AGESUP) { - j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12); - if(j==0) j=1; /* Survives at least one month after exam */ - k=k+1; - if (j >= jmax) jmax=j; - if (j <= jmin) jmin=j; - sum=sum+j; - /*if (j<0) printf("j=%d num=%d \n",j,i);*/ - /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/ - if(j<0)printf("Error! Negative delay (%d to death) between waves %d and %d of individual %d at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]); + j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12); + if(j==0) j=1; /* Survives at least one month after exam */ + else if(j<0){ + printf("Error! Negative delay (%d to death) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]); + j=1; /* Careful Patch */ + printf(" We assumed that the date of interview was correct (and not the date of death) and postponed the death %d month(s) (one stepm) after the interview.\n You MUST fix the contradiction between dates.\n",stepm); + printf("Error! Negative delay (%d to death) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]); + fprintf(ficlog," We assumed that the date of interview was correct (and not the date of death) and postponed the death %d month(s) (one stepm) after the interview.\n You MUST fix the contradiction between dates.\n",stepm); + } + k=k+1; + if (j >= jmax) jmax=j; + if (j <= jmin) jmin=j; + sum=sum+j; + /*if (j<0) printf("j=%d num=%d \n",j,i);*/ + /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/ } } else{ @@ -1810,13 +2068,16 @@ void concatwav(int wav[], int **dh, int else if (j <= jmin)jmin=j; /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */ /*printf("%d %lf %d %d %d\n", i,agev[mw[mi][i]][i],j,s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);*/ - if(j<0)printf("Error! Negative delay (%d) between waves %d and %d of individual %d at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]); + if(j<0){ + printf("Error! Negative delay (%d) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]); + fprintf(ficlog,"Error! Negative delay (%d) between waves %d and %d of individual %ld at line %d who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]); + } sum=sum+j; } jk= j/stepm; jl= j -jk*stepm; ju= j -(jk+1)*stepm; - if(mle <=1){ + if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */ if(jl==0){ dh[mi][i]=jk; bh[mi][i]=0; @@ -1841,8 +2102,8 @@ void concatwav(int wav[], int **dh, int bh[mi][i]=ju; /* At least one step */ /* printf(" bh=%d ju=%d jl=%d dh=%d jk=%d stepm=%d %d\n",bh[mi][i],ju,jl,dh[mi][i],jk,stepm,i);*/ } - } - } /* end if mle */ + } /* end if mle */ + } } /* end wave */ } jmean=sum/k; @@ -2143,23 +2404,10 @@ void varevsij(char optionfilefiname[], d 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 probabilities of dying over estepm months as a weighted average (i.e global mortality independent of initial healh state)

  • \n"); - fprintf(fichtm,"\n
    %s
    \n",digitp); - } + fprintf(ficgp,"\n# Routine varevsij"); + fprintf(fichtm,"\n
  • Computing probabilities of dying over estepm months as a weighted average (i.e global mortality independent of initial healh state)

  • \n"); + fprintf(fichtm,"\n
    %s
    \n",digitp); +/* } */ varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath); fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are the stable prevalence in health states i\n"); @@ -2371,14 +2619,15 @@ void varevsij(char optionfilefiname[], d /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */ /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */ /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */ - fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l 1 ",fileresprobmorprev); - fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95\%% interval\" w l 2 ",fileresprobmorprev); - fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l 2 ",fileresprobmorprev); - fprintf(fichtm,"\n
    File (multiple files are possible if covariates are present): %s\n",fileresprobmorprev,fileresprobmorprev); - fprintf(fichtm,"\n
    Probability is computed over estepm=%d months.

    \n", estepm,digitp,optionfilefiname,digit); + fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l 1 ",subdirf(fileresprobmorprev)); + fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95\%% interval\" w l 2 ",subdirf(fileresprobmorprev)); + fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l 2 ",subdirf(fileresprobmorprev)); + fprintf(fichtm,"\n
    File (multiple files are possible if covariates are present): %s\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev)); + fprintf(fichtm,"\n
    Probability is computed over estepm=%d months.

    \n", estepm,subdirf3(optionfilefiname,"varmuptjgr",digitp),digit); /* fprintf(fichtm,"\n
    Probability is computed over estepm=%d months and then divided by estepm and multiplied by %.0f in order to have the probability to die over a year

    \n", stepm,YEARM,digitp,digit); */ - fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.png\";replot;",digitp,optionfilefiname,digit); +/* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.png\";replot;",digitp,optionfilefiname,digit); */ + fprintf(ficgp,"\nset out \"%s%s.png\";replot;",digitp,subdirf3(optionfilefiname,"varmuptjgr",digitp),digit); free_vector(xp,1,npar); free_matrix(doldm,1,nlstate,1,nlstate); @@ -2388,9 +2637,9 @@ void varevsij(char optionfilefiname[], d free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath); if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); fclose(ficresprobmorprev); - fclose(ficgp); - fclose(fichtm); -} + fflush(ficgp); + fflush(fichtm); +} /* end varevsij */ /************ Variance of prevlim ******************/ void varprevlim(char fileres[], double **varpl, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int ij) @@ -2546,28 +2795,13 @@ void varprob(char optionfilefiname[], do mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage); varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage); first=1; - if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { - printf("Problem with gnuplot file: %s\n", optionfilegnuplot); - fprintf(ficlog,"Problem with gnuplot file: %s\n", optionfilegnuplot); - exit(0); - } - else{ - fprintf(ficgp,"\n# Routine varprob"); - } - if((fichtm=fopen(optionfilehtm,"a"))==NULL) { - printf("Problem with html file: %s\n", optionfilehtm); - fprintf(ficlog,"Problem with html file: %s\n", optionfilehtm); - exit(0); - } - else{ - fprintf(fichtm,"\n
  • Computing and drawing one step probabilities with their confidence intervals

  • \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"); - - } + fprintf(ficgp,"\n# Routine varprob"); + 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"); cov[1]=1; tj=cptcoveff; @@ -2749,10 +2983,14 @@ void varprob(char optionfilefiname[], do fprintf(ficgp,"\nset parametric;unset label"); fprintf(ficgp,"\nset log y;set log x; set xlabel \"p%1d%1d (year-1)\";set ylabel \"p%1d%1d (year-1)\"",k1,l1,k2,l2); fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65"); - fprintf(fichtm,"\n
    Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year-1 :varpijgr%s%d%1d%1d-%1d%1d.png, ",k1,l1,k2,l2,optionfilefiname, j1,k1,l1,k2,l2,optionfilefiname, j1,k1,l1,k2,l2); - fprintf(fichtm,"\n
    ",optionfilefiname, j1,k1,l1,k2,l2); + fprintf(fichtm,"\n
    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(fichtm,"\n
    ",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2); fprintf(fichtm,"\n
    Correlation at age %d (%.3f),",(int) age, c12); - fprintf(ficgp,"\nset out \"varpijgr%s%d%1d%1d-%1d%1d.png\"",optionfilefiname, j1,k1,l1,k2,l2); + fprintf(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",\ @@ -2769,7 +3007,7 @@ void varprob(char optionfilefiname[], do }/* if first */ } /* age mod 5 */ } /* end loop age */ - fprintf(ficgp,"\nset out \"varpijgr%s%d%1d%1d-%1d%1d.png\";replot;",optionfilefiname, j1,k1,l1,k2,l2); + fprintf(ficgp,"\nset out \"%s%d%1d%1d-%1d%1d.png\";replot;",subdirf2(optionfilefiname,"varpijgr"), j1,k1,l1,k2,l2); first=1; } /*l12 */ } /* k12 */ @@ -2783,8 +3021,7 @@ void varprob(char optionfilefiname[], do fclose(ficresprob); fclose(ficresprobcov); fclose(ficresprobcor); - fclose(ficgp); - fclose(fichtm); + /* fclose(ficgp);*/ } @@ -2797,18 +3034,21 @@ void printinghtml(char fileres[], char t double jprev2, double mprev2,double anprev2){ int jj1, k1, i1, cpt; /*char optionfilehtm[FILENAMELENGTH];*/ - if((fichtm=fopen(optionfilehtm,"a"))==NULL) { - printf("Problem with %s \n",optionfilehtm), exit(0); - fprintf(ficlog,"Problem with %s \n",optionfilehtm), exit(0); - } - - fprintf(fichtm,"
    • Result files (first order: no variance)

      \n - - Observed prevalence in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): p%s
      \n - - Estimated transition probabilities over %d (stepm) months: pij%s
      \n - - Stable prevalence in each health state: pl%s
      \n - - Life expectancies by age and initial health status (estepm=%2d months): - e%s
      \n
    • ", \ - jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,fileres,fileres,stepm,fileres,fileres,fileres,fileres,estepm,fileres,fileres); +/* if((fichtm=fopen(optionfilehtm,"a"))==NULL) { */ +/* printf("Problem with %s \n",optionfilehtm), exit(0); */ +/* fprintf(ficlog,"Problem with %s \n",optionfilehtm), exit(0); */ +/* } */ + + fprintf(fichtm,"
      • Result files (first order: no variance)

        \n \ + - Observed prevalence in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): %s
        \n \ + - Estimated transition probabilities over %d (stepm) months: %s
        \n \ + - Stable prevalence in each health state: %s
        \n \ + - Life expectancies by age and initial health status (estepm=%2d months): \ + %s
        \n
      • ", \ + jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirf2(fileres,"p"),subdirf2(fileres,"p"),\ + stepm,subdirf2(fileres,"pij"),subdirf2(fileres,"pij"),\ + subdirf2(fileres,"pl"),subdirf2(fileres,"pl"),\ + estepm,subdirf2(fileres,"e"),subdirf2(fileres,"e")); fprintf(fichtm," \n
        • Graphs
        • "); @@ -2826,36 +3066,44 @@ fprintf(fichtm," \n

          • Graphs fprintf(fichtm," ************\n
            "); } /* Pij */ - fprintf(fichtm,"
            - Pij or Conditional probabilities to be observed in state j being in state i, %d (stepm) months before: pe%s%d1.png
            -",stepm,strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1); + fprintf(fichtm,"
            - Pij or Conditional probabilities to be observed in state j being in state i, %d (stepm) months before: %s%d1.png
            \ +",stepm,subdirf2(optionfilefiname,"pe"),jj1,subdirf2(optionfilefiname,"pe"),jj1); /* Quasi-incidences */ - fprintf(fichtm,"
            - Pij or Conditional probabilities to be observed in state j being in state i %d (stepm) months before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too: pe%s%d2.png
            -",stepm,strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1); + 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); /* Stable prevalence in each health state */ for(cpt=1; cpt- Stable prevalence in each health state : p%s%d%d.png
            -",strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1); + fprintf(fichtm,"
            - Stable prevalence in each health state : p%s%d%d.png
            \ +",subdirf2(optionfilefiname,"p"),cpt,jj1,subdirf2(optionfilefiname,"p"),cpt,jj1); } for(cpt=1; cpt<=nlstate;cpt++) { - fprintf(fichtm,"\n
            - Health life expectancies by age and initial health state (%d): exp%s%d%d.png
            -",cpt,strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1); + fprintf(fichtm,"\n
            - Health life expectancies by age and initial health state (%d): %s%d%d.png
            \ +",cpt,subdirf2(optionfilefiname,"exo"),cpt,jj1,subdirf2(optionfilefiname,"exp"),cpt,jj1); } - fprintf(fichtm,"\n
            - Total life expectancy by age and -health expectancies in states (1) and (2): e%s%d.png
            -",strtok(optionfile, "."),jj1,strtok(optionfile, "."),jj1); + 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,"\n
        • Result files (second order: variances)

          \n - - Parameter file with estimated parameters and covariance matrix: %s
          \n - - Variance of one-step probabilities: prob%s
          \n - - Variance-covariance of one-step probabilities: probcov%s
          \n - - Correlation matrix of one-step probabilities: probcor%s
          \n - - Variances and covariances of life expectancies by age and initial health status (estepm=%d months): v%s
          \n - - Health expectancies with their variances (no covariance): t%s
          \n - - Standard deviation of stable prevalences: vpl%s
          \n",rfileres,rfileres,fileres,fileres,fileres,fileres,fileres,fileres, estepm, fileres,fileres,fileres,fileres,fileres,fileres); + fprintf(fichtm,"\n
        • Result files (second order: variances)

          \n\ + - Parameter file with estimated parameters and covariance matrix: %s
          \n\ + - Variance of one-step probabilities: %s
          \n\ + - Variance-covariance of one-step probabilities: %s
          \n\ + - Correlation matrix of one-step probabilities: %s
          \n\ + - Variances and covariances of life expectancies by age and initial health status (estepm=%d months): %s
          \n\ + - Health expectancies with their variances (no covariance): %s
          \n\ + - Standard deviation of stable prevalences: %s
          \n",\ + rfileres,rfileres,\ + subdirf2(fileres,"prob"),subdirf2(fileres,"prob"),\ + subdirf2(fileres,"probcov"),subdirf2(fileres,"probcov"),\ + subdirf2(fileres,"probcor"),subdirf2(fileres,"probcor"),\ + estepm, subdirf2(fileres,"v"),subdirf2(fileres,"v"),\ + subdirf2(fileres,"t"),subdirf2(fileres,"t"),\ + subdirf2(fileres,"vpl"),subdirf2(fileres,"vpl")); /* if(popforecast==1) fprintf(fichtm,"\n */ /* - Prevalences forecasting: f%s
          \n */ @@ -2879,76 +3127,84 @@ fprintf(fichtm,"
          • Graphs"); } for(cpt=1; cpt<=nlstate;cpt++) { - fprintf(fichtm,"
            - Observed and period prevalence (with confident -interval) in state (%d): v%s%d%d.png
            -",cpt,strtok(optionfile, "."),cpt,jj1,strtok(optionfile, "."),cpt,jj1); + fprintf(fichtm,"
            - Observed and period prevalence (with confident\ +interval) in state (%d): %s%d%d.png
            \ +",cpt,subdirf2(optionfilefiname,"pe"),cpt,jj1,subdirf2(optionfilefiname,"pe"),cpt,jj1); } } /* end i1 */ }/* End k1 */ fprintf(fichtm,"
          "); -fclose(fichtm); + fflush(fichtm); } /******************* Gnuplot file **************/ -void printinggnuplot(char fileres[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){ +void printinggnuplot(char fileres[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char path[], 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); - } +/* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */ +/* printf("Problem with file %s",optionfilegnuplot); */ +/* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */ +/* } */ /*#ifdef windows */ - fprintf(ficgp,"cd \"%s\" \n",pathc); + fprintf(ficgp,"cd \"%s\" \n",path); /*#endif */ -m=pow(2,cptcoveff); - + m=pow(2,cptcoveff); + + strcpy(dirfileres,optionfilefiname); + strcpy(optfileres,"vpl"); /* 1eme*/ for (cpt=1; cpt<= nlstate ; cpt ++) { for (k1=1; k1<= m ; k1 ++) { - fprintf(ficgp,"\nset out \"v%s%d%d.png\" \n",strtok(optionfile, "."),cpt,k1); - fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter png small\nset size 0.65,0.65\nplot [%.f:%.f] \"vpl%s\" every :::%d::%d u 1:2 \"\%%lf",ageminpar,fage,fileres,k1-1,k1-1); + fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"v"),cpt,k1); + fprintf(ficgp,"\n#set out \"v%s%d%d.png\" \n",optionfilefiname,cpt,k1); + fprintf(ficgp,"set xlabel \"Age\" \n\ +set ylabel \"Probability\" \n\ +set ter png small\n\ +set size 0.65,0.65\n\ +plot [%.f:%.f] \"%s\" every :::%d::%d u 1:2 \"\%%lf",ageminpar,fage,subdirf2(fileres,"vpl"),k1-1,k1-1); for (i=1; i<= nlstate ; i ++) { if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)"); else fprintf(ficgp," \%%*lf (\%%*lf)"); } - fprintf(ficgp,"\" t\"Stable prevalence\" w l 0,\"vpl%s\" every :::%d::%d u 1:($2+1.96*$3) \"\%%lf",fileres,k1-1,k1-1); + fprintf(ficgp,"\" t\"Stable prevalence\" w l 0,\"%s\" every :::%d::%d u 1:($2+1.96*$3) \"\%%lf",subdirf2(fileres,"vpl"),k1-1,k1-1); for (i=1; i<= nlstate ; i ++) { if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)"); else fprintf(ficgp," \%%*lf (\%%*lf)"); } - fprintf(ficgp,"\" t\"95\%% CI\" w l 1,\"vpl%s\" every :::%d::%d u 1:($2-1.96*$3) \"\%%lf",fileres,k1-1,k1-1); + fprintf(ficgp,"\" t\"95\%% CI\" w l 1,\"%s\" every :::%d::%d u 1:($2-1.96*$3) \"\%%lf",subdirf2(fileres,"vpl"),k1-1,k1-1); for (i=1; i<= nlstate ; i ++) { if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)"); else fprintf(ficgp," \%%*lf (\%%*lf)"); } - fprintf(ficgp,"\" t\"\" w l 1,\"p%s\" every :::%d::%d u 1:($%d) t\"Observed prevalence \" w l 2",fileres,k1-1,k1-1,2+4*(cpt-1)); + fprintf(ficgp,"\" t\"\" w l 1,\"%s\" every :::%d::%d u 1:($%d) t\"Observed prevalence \" w l 2",subdirf2(fileres,"p"),k1-1,k1-1,2+4*(cpt-1)); } } /*2 eme*/ for (k1=1; k1<= m ; k1 ++) { - fprintf(ficgp,"\nset out \"e%s%d.png\" \n",strtok(optionfile, "."),k1); + fprintf(ficgp,"\nset out \"%s%d.png\" \n",subdirf2(optionfilefiname,"e"),k1); fprintf(ficgp,"set ylabel \"Years\" \nset ter png small\nset size 0.65,0.65\nplot [%.f:%.f] ",ageminpar,fage); for (i=1; i<= nlstate+1 ; i ++) { k=2*i; - fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:2 \"\%%lf",fileres,k1-1,k1-1); + fprintf(ficgp,"\"%s\" every :::%d::%d u 1:2 \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1); for (j=1; j<= nlstate+1 ; j ++) { if (j==i) fprintf(ficgp," \%%lf (\%%lf)"); else fprintf(ficgp," \%%*lf (\%%*lf)"); } if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l ,"); else fprintf(ficgp,"\" t\"LE in state (%d)\" w l ,",i-1); - fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2-$3*2) \"\%%lf",fileres,k1-1,k1-1); + fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2-$3*2) \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1); for (j=1; j<= nlstate+1 ; j ++) { if (j==i) fprintf(ficgp," \%%lf (\%%lf)"); else fprintf(ficgp," \%%*lf (\%%*lf)"); } fprintf(ficgp,"\" t\"\" w l 0,"); - fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2+$3*2) \"\%%lf",fileres,k1-1,k1-1); + fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2+$3*2) \"\%%lf",subdirf2(fileres,"t"),k1-1,k1-1); for (j=1; j<= nlstate+1 ; j ++) { if (j==i) fprintf(ficgp," \%%lf (\%%lf)"); else fprintf(ficgp," \%%*lf (\%%*lf)"); @@ -2963,8 +3219,10 @@ m=pow(2,cptcoveff); for (k1=1; k1<= m ; k1 ++) { for (cpt=1; cpt<= nlstate ; cpt ++) { k=2+nlstate*(2*cpt-2); - fprintf(ficgp,"\nset out \"exp%s%d%d.png\" \n",strtok(optionfile, "."),cpt,k1); - fprintf(ficgp,"set ter png small\nset size 0.65,0.65\nplot [%.f:%.f] \"e%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",ageminpar,fage,fileres,k1-1,k1-1,k,cpt); + 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); @@ -2974,7 +3232,7 @@ m=pow(2,cptcoveff); */ for (i=1; i< nlstate ; i ++) { - fprintf(ficgp," ,\"e%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",fileres,k1-1,k1-1,k+2*i,cpt,i+1); + fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+2*i,cpt,i+1); } } @@ -2984,15 +3242,17 @@ m=pow(2,cptcoveff); for (k1=1; k1<= m ; k1 ++) { for (cpt=1; cpt<=nlstate ; cpt ++) { k=3; - fprintf(ficgp,"\nset out \"p%s%d%d.png\" \n",strtok(optionfile, "."),cpt,k1); - fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter png small\nset size 0.65,0.65\nplot [%.f:%.f] \"pij%s\" u ($1==%d ? ($3):1/0):($%d/($%d",ageminpar,agemaxpar,fileres,k1,k+cpt+1,k+1); + fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"p"),cpt,k1); + fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\ +set ter png small\nset size 0.65,0.65\n\ +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 ++) + for (i=1; i< nlstate ; i ++) fprintf(ficgp,"+$%d",k+i+1); fprintf(ficgp,")) t\"prev(%d,%d)\" w l",cpt,cpt+1); l=3+(nlstate+ndeath)*cpt; - fprintf(ficgp,",\"pij%s\" u ($1==%d ? ($3):1/0):($%d/($%d",fileres,k1,l+cpt+1,l+1); + fprintf(ficgp,",\"%s\" u ($1==%d ? ($3):1/0):($%d/($%d",subdirf2(fileres,"pij"),k1,l+cpt+1,l+1); for (i=1; i< nlstate ; i ++) { l=3+(nlstate+ndeath)*cpt; fprintf(ficgp,"+$%d",l+i+1); @@ -3016,7 +3276,7 @@ m=pow(2,cptcoveff); for(ng=1; ng<=2;ng++){ /* Number of graphics: first is probabilities second is incidence per year*/ for(jk=1; jk <=m; jk++) { - fprintf(ficgp,"\nset out \"pe%s%d%d.png\" \n",strtok(optionfile, "."),jk,ng); + fprintf(ficgp,"\nset out \"%s%d%d.png\" \n",subdirf2(optionfilefiname,"pe"),jk,ng); if (ng==2) fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n"); else @@ -3063,7 +3323,7 @@ m=pow(2,cptcoveff); } /* end k2 */ } /* end jk */ } /* end ng */ - fclose(ficgp); + fflush(ficgp); } /* end gnuplot */ @@ -3126,7 +3386,7 @@ prevforecast(char fileres[], double anpr char fileresf[FILENAMELENGTH]; agelim=AGESUP; - prevalence(ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); + prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); strcpy(fileresf,"f"); strcat(fileresf,fileres); @@ -3249,7 +3509,7 @@ populforecast(char fileres[], double anp agelim=AGESUP; calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; - prevalence(ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); + prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); strcpy(filerespop,"pop"); @@ -3391,7 +3651,137 @@ populforecast(char fileres[], double anp free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); fclose(ficrespop); +} /* End of popforecast */ + +int fileappend(FILE *fichier, char *optionfich) +{ + if((fichier=fopen(optionfich,"a"))==NULL) { + printf("Problem with file: %s\n", optionfich); + fprintf(ficlog,"Problem with file: %s\n", optionfich); + return (0); + } + fflush(fichier); + return (1); } +void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo) +{ + + char ca[32], cb[32], cc[32]; + int i,j, k, l, li, lj, lk, ll, jj, npar, itimes; + int numlinepar; + + printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); + fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); + for(i=1; i <=nlstate; i++){ + jj=0; + for(j=1; j <=nlstate+ndeath; j++){ + if(j==i) continue; + jj++; + /*ca[0]= k+'a'-1;ca[1]='\0';*/ + printf("%1d%1d",i,j); + fprintf(ficparo,"%1d%1d",i,j); + for(k=1; k<=ncovmodel;k++){ + /* printf(" %lf",param[i][j][k]); */ + /* fprintf(ficparo," %lf",param[i][j][k]); */ + printf(" 0."); + fprintf(ficparo," 0."); + } + printf("\n"); + fprintf(ficparo,"\n"); + } + } + printf("# Scales (for hessian or gradient estimation)\n"); + fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n"); + npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/ + for(i=1; i <=nlstate; i++){ + jj=0; + for(j=1; j <=nlstate+ndeath; j++){ + if(j==i) continue; + jj++; + fprintf(ficparo,"%1d%1d",i,j); + printf("%1d%1d",i,j); + fflush(stdout); + for(k=1; k<=ncovmodel;k++){ + /* printf(" %le",delti3[i][j][k]); */ + /* fprintf(ficparo," %le",delti3[i][j][k]); */ + printf(" 0."); + fprintf(ficparo," 0."); + } + numlinepar++; + printf("\n"); + fprintf(ficparo,"\n"); + } + } + printf("# Covariance matrix\n"); +/* # 121 Var(a12)\n\ */ +/* # 122 Cov(b12,a12) Var(b12)\n\ */ +/* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */ +/* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */ +/* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */ +/* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */ +/* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */ +/* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */ + fflush(stdout); + fprintf(ficparo,"# Covariance matrix\n"); + /* # 121 Var(a12)\n\ */ + /* # 122 Cov(b12,a12) Var(b12)\n\ */ + /* # ...\n\ */ + /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */ + + for(itimes=1;itimes<=2;itimes++){ + jj=0; + for(i=1; i <=nlstate; i++){ + for(j=1; j <=nlstate+ndeath; j++){ + if(j==i) continue; + for(k=1; k<=ncovmodel;k++){ + jj++; + ca[0]= k+'a'-1;ca[1]='\0'; + if(itimes==1){ + printf("#%1d%1d%d",i,j,k); + fprintf(ficparo,"#%1d%1d%d",i,j,k); + }else{ + printf("%1d%1d%d",i,j,k); + fprintf(ficparo,"%1d%1d%d",i,j,k); + /* printf(" %.5le",matcov[i][j]); */ + } + ll=0; + for(li=1;li <=nlstate; li++){ + for(lj=1;lj <=nlstate+ndeath; lj++){ + if(lj==li) continue; + for(lk=1;lk<=ncovmodel;lk++){ + ll++; + if(ll<=jj){ + cb[0]= lk +'a'-1;cb[1]='\0'; + if(ll -#include + char stra[80], strb[80], strc[80], strd[80],stre[80],modelsav[80]; + char strstart[80], *strt, strtend[80]; + char *stratrunc; + int lstra; + + long total_usecs; + struct timeval start_time, end_time, curr_time; + struct timezone tzp; + extern int gettimeofday(); + struct tm tmg, tm, *gmtime(), *localtime(); + long time_value; + extern long time(); - /* long total_usecs; - struct timeval start_time, end_time; - - gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */ + /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */ + (void) gettimeofday(&start_time,&tzp); + tm = *localtime(&start_time.tv_sec); + tmg = *gmtime(&start_time.tv_sec); + strcpy(strstart,asctime(&tm)); + +/* printf("Localtime (at start)=%s",strstart); */ +/* tp.tv_sec = tp.tv_sec +86400; */ +/* tm = *localtime(&start_time.tv_sec); */ +/* tmg.tm_year=tmg.tm_year +dsign*dyear; */ +/* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */ +/* tmg.tm_hour=tmg.tm_hour + 1; */ +/* tp.tv_sec = mktime(&tmg); */ +/* strt=asctime(&tmg); */ +/* printf("Time(after) =%s",strstart); */ +/* (void) time (&time_value); +* printf("time=%d,t-=%d\n",time_value,time_value-86400); +* tm = *localtime(&time_value); +* strstart=asctime(&tm); +* printf("tim_value=%d,asctime=%s\n",time_value,strstart); +*/ + getcwd(pathcd, size); - printf("\n%s",version); + printf("\n%s\n%s",version,fullversion); if(argc <=1){ printf("\nEnter the parameter file name: "); scanf("%s",pathtot); @@ -3462,15 +3885,25 @@ int main(int argc, char *argv[]) else{ strcpy(pathtot,argv[1]); } - /*if(getcwd(pathcd, 80)!= NULL)printf ("Error pathcd\n");*/ + /*if(getcwd(pathcd, MAXLINE)!= 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); + printf("pathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname); chdir(path); - replace(pathc,path); + strcpy(command,"mkdir "); + strcat(command,optionfilefiname); + if((outcmd=system(command)) != 0){ + printf("Problem creating directory or it already exists %s%s, err=%d\n",path,optionfilefiname,outcmd); + /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */ + /* fclose(ficlog); */ +/* exit(1); */ + } +/* if((imk=mkdir(optionfilefiname))<0){ */ +/* perror("mkdir"); */ +/* } */ /*-------- arguments in the command line --------*/ @@ -3482,9 +3915,16 @@ int main(int argc, char *argv[]) goto end; } fprintf(ficlog,"Log filename:%s\n",filelog); - fprintf(ficlog,"\n%s",version); + fprintf(ficlog,"\n%s\n%s",version,fullversion); fprintf(ficlog,"\nEnter the parameter file name: "); - fprintf(ficlog,"pathtot=%s, path=%s, optionfile=%s optionfilext=%s optionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname); + fprintf(ficlog,"pathtot=%s\n\ + path=%s \n\ + optionfile=%s\n\ + optionfilext=%s\n\ + optionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname); + + printf("Localtime (at start):%s",strstart); + fprintf(ficlog,"Localtime (at start): %s",strstart); fflush(ficlog); /* */ @@ -3497,37 +3937,49 @@ int main(int argc, char *argv[]) if((ficpar=fopen(optionfile,"r"))==NULL) { printf("Problem with optionfile %s\n",optionfile); fprintf(ficlog,"Problem with optionfile %s\n",optionfile); + fflush(ficlog); goto end; } + + strcpy(filereso,"o"); strcat(filereso,fileres); - if((ficparo=fopen(filereso,"w"))==NULL) { + if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */ printf("Problem with Output resultfile: %s\n", filereso); fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso); + fflush(ficlog); goto end; } /* Reads comments: lines beginning with '#' */ + numlinepar=0; while((c=getc(ficpar))=='#' && c!= EOF){ ungetc(c,ficpar); fgets(line, MAXLINE, ficpar); + numlinepar++; puts(line); fputs(line,ficparo); + fputs(line,ficlog); } ungetc(c,ficpar); fscanf(ficpar,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%lf stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d model=%s\n",title, datafile, &lastobs, &firstpass,&lastpass,&ftol, &stepm, &ncovcol, &nlstate,&ndeath, &maxwav, &mle, &weightopt,model); + numlinepar++; printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate,ndeath, maxwav, mle, weightopt,model); fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol,nlstate,ndeath,maxwav, mle, weightopt,model); + fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol,nlstate,ndeath,maxwav, mle, weightopt,model); + fflush(ficlog); while((c=getc(ficpar))=='#' && c!= EOF){ ungetc(c,ficpar); fgets(line, MAXLINE, ficpar); + numlinepar++; puts(line); fputs(line,ficparo); + fputs(line,ficlog); } ungetc(c,ficpar); - + covar=matrix(0,NCOVMAX,1,n); cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement*/ @@ -3535,21 +3987,38 @@ int main(int argc, char *argv[]) ncovmodel=2+cptcovn; /*Number of variables = cptcovn + intercept + age */ nvar=ncovmodel-1; /* Suppressing age as a basic covariate */ - + + if(mle==-1){ /* Print a wizard for help writing covariance matrix */ + prwizard(ncovmodel, nlstate, ndeath, model, ficparo); + printf(" You choose mle=-1, look at file %s for a template of covariance matrix \n",filereso); + fprintf(ficlog," You choose mle=-1, look at file %s for a template of covariance matrix \n",filereso); + fclose (ficparo); + fclose (ficlog); + exit(0); + } /* Read guess parameters */ /* Reads comments: lines beginning with '#' */ while((c=getc(ficpar))=='#' && c!= EOF){ ungetc(c,ficpar); fgets(line, MAXLINE, ficpar); + numlinepar++; puts(line); fputs(line,ficparo); + fputs(line,ficlog); } ungetc(c,ficpar); - + param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); - for(i=1; i <=nlstate; i++) - for(j=1; j <=nlstate+ndeath-1; j++){ + for(i=1; i <=nlstate; i++){ + j=0; + for(jj=1; jj <=nlstate+ndeath; jj++){ + if(jj==i) continue; + j++; fscanf(ficpar,"%1d%1d",&i1,&j1); + if ((i1 != i) && (j1 != j)){ + printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1); + exit(1); + } fprintf(ficparo,"%1d%1d",i1,j1); if(mle==1) printf("%1d%1d",i,j); @@ -3565,12 +4034,15 @@ int main(int argc, char *argv[]) fprintf(ficparo," %lf",param[i][j][k]); } fscanf(ficpar,"\n"); + numlinepar++; if(mle==1) printf("\n"); fprintf(ficlog,"\n"); fprintf(ficparo,"\n"); } - + } + fflush(ficlog); + npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/ p=param[1][1]; @@ -3579,8 +4051,10 @@ int main(int argc, char *argv[]) while((c=getc(ficpar))=='#' && c!= EOF){ ungetc(c,ficpar); fgets(line, MAXLINE, ficpar); + numlinepar++; puts(line); fputs(line,ficparo); + fputs(line,ficlog); } ungetc(c,ficpar); @@ -3589,18 +4063,28 @@ int main(int argc, char *argv[]) for(i=1; i <=nlstate; i++){ for(j=1; j <=nlstate+ndeath-1; j++){ fscanf(ficpar,"%1d%1d",&i1,&j1); + if ((i1-i)*(j1-j)!=0){ + printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1); + exit(1); + } printf("%1d%1d",i,j); fprintf(ficparo,"%1d%1d",i1,j1); + fprintf(ficlog,"%1d%1d",i1,j1); for(k=1; k<=ncovmodel;k++){ fscanf(ficpar,"%le",&delti3[i][j][k]); printf(" %le",delti3[i][j][k]); fprintf(ficparo," %le",delti3[i][j][k]); + fprintf(ficlog," %le",delti3[i][j][k]); } fscanf(ficpar,"\n"); + numlinepar++; printf("\n"); fprintf(ficparo,"\n"); + fprintf(ficlog,"\n"); } } + fflush(ficlog); + delti=delti3[1][1]; @@ -3610,8 +4094,10 @@ int main(int argc, char *argv[]) while((c=getc(ficpar))=='#' && c!= EOF){ ungetc(c,ficpar); fgets(line, MAXLINE, ficpar); + numlinepar++; puts(line); fputs(line,ficparo); + fputs(line,ficlog); } ungetc(c,ficpar); @@ -3626,13 +4112,12 @@ int main(int argc, char *argv[]) fscanf(ficpar," %le",&matcov[i][j]); if(mle==1){ printf(" %.5le",matcov[i][j]); - fprintf(ficlog," %.5le",matcov[i][j]); } - else - fprintf(ficlog," %.5le",matcov[i][j]); + fprintf(ficlog," %.5le",matcov[i][j]); fprintf(ficparo," %.5le",matcov[i][j]); } fscanf(ficpar,"\n"); + numlinepar++; if(mle==1) printf("\n"); fprintf(ficlog,"\n"); @@ -3646,6 +4131,7 @@ int main(int argc, char *argv[]) printf("\n"); fprintf(ficlog,"\n"); + fflush(ficlog); /*-------- Rewriting paramater file ----------*/ strcpy(rfileres,"r"); /* "Rparameterfile */ @@ -3667,7 +4153,7 @@ int main(int argc, char *argv[]) n= lastobs; severity = vector(1,maxwav); outcome=imatrix(1,maxwav+1,1,n); - num=ivector(1,n); + num=lvector(1,n); moisnais=vector(1,n); annais=vector(1,n); moisdc=vector(1,n); @@ -3703,10 +4189,16 @@ int main(int argc, char *argv[]) for (j=ncovcol;j>=1;j--){ cutv(stra, strb,line,' '); covar[j][i]=(double)(atoi(strb)); strcpy(line,stra); } - num[i]=atol(stra); + lstra=strlen(stra); + if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */ + stratrunc = &(stra[lstra-9]); + num[i]=atol(stratrunc); + } + else + num[i]=atol(stra); /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){ - printf("%d %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]),weight[i], (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]), (mint[2][i]), (anint[2][i]), (s[2][i]), (mint[3][i]), (anint[3][i]), (s[3][i]), (mint[4][i]), (anint[4][i]), (s[4][i])); ij=ij+1;}*/ + printf("%ld %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]),weight[i], (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]), (mint[2][i]), (anint[2][i]), (s[2][i]), (mint[3][i]), (anint[3][i]), (s[3][i]), (mint[4][i]), (anint[4][i]), (s[4][i])); ij=ij+1;}*/ i=i+1; } @@ -3722,7 +4214,7 @@ int main(int argc, char *argv[]) }*/ /* for (i=1; i<=imx; i++){ if (s[4][i]==9) s[4][i]=-1; - printf("%d %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]), (weight[i]), (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]), (mint[2][i]), (anint[2][i]), (s[2][i]), (mint[3][i]), (anint[3][i]), (s[3][i]), (mint[4][i]), (anint[4][i]), (s[4][i]));}*/ + printf("%ld %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]), (weight[i]), (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]), (mint[2][i]), (anint[2][i]), (s[2][i]), (mint[3][i]), (anint[3][i]), (s[3][i]), (mint[4][i]), (anint[4][i]), (s[4][i]));}*/ for (i=1; i<=imx; i++) @@ -3825,14 +4317,14 @@ int main(int argc, char *argv[]) s[m][i]=-1; } if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){ - printf("Error! Date of death (month %2d and year %4d) of individual %d on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results are biased\n",(int)moisdc[i],(int)andc[i],num[i],i); - fprintf(ficlog,"Error! Date of death (month %2d and year %4d) of individual %d on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results are biased\n",(int)moisdc[i],(int)andc[i],num[i],i); + printf("Error! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results are biased\n",(int)moisdc[i],(int)andc[i],num[i],i); + fprintf(ficlog,"Error! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results are biased\n",(int)moisdc[i],(int)andc[i],num[i],i); s[m][i]=-1; } if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){ - printf("Error! Month of death of individual %d on line %d was unknown %2d, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,(int)moisdc[i]); - fprintf(ficlog,"Error! Month of death of individual %d on line %d was unknown %f, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,moisdc[i]); - s[m][i]=-1; + printf("Error! Month of death of individual %ld on line %d was unknown %2d, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,(int)moisdc[i]); + fprintf(ficlog,"Error! Month of death of individual %ld on line %d was unknown %f, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,moisdc[i]); + s[m][i]=-1; /* We prefer to skip it (and to skip it in version 0.8a1 too */ } } } @@ -3848,8 +4340,8 @@ int main(int argc, char *argv[]) /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/ else { if ((int)andc[i]!=9999){ - printf("Warning negative age at death: %d line:%d\n",num[i],i); - fprintf(ficlog,"Warning negative age at death: %d line:%d\n",num[i],i); + printf("Warning negative age at death: %ld line:%d\n",num[i],i); + fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i); agev[m][i]=-1; } } @@ -3893,7 +4385,7 @@ int main(int argc, char *argv[]) /*for (i=1; i<=imx; i++){ for (m=firstpass; (m\nIMaCh %s\n %s
          %s
          \ +
          \n\ +Title=%s
          Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s
          \n\ +\n\ +
          \ +
          • Parameter files

            \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
          \n",\ + fileres,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt,\ + model,fileres,fileres,\ + filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart); + /*fclose(fichtm);*/ + fflush(fichtm); + + strcpy(pathr,path); + strcat(pathr,optionfilefiname); + chdir(optionfilefiname); /* Move to directory named optionfile */ + strcpy(lfileres,fileres); + strcat(lfileres,"/"); + strcat(lfileres,optionfilefiname); + + /* replace(pathc,path);*/ + /* 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); - pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */ + fprintf(fichtm,"\n"); + fprintf(fichtm,"
          Total number of observations=%d
          \n\ +Youngest age at first (selected) pass %.2f, oldest age %.2f
          \n\ +Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf
          \n",\ + imx,agemin,agemax,jmin,jmax,jmean); + 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 */ @@ -3966,6 +4509,18 @@ int main(int argc, char *argv[]) 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*/ + likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */ + printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw); + for (k=1; k<=npar;k++) + printf(" %d %8.5f",k,p[k]); + printf("\n"); + globpr=1; /* to print the contributions */ + likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */ + printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw); + for (k=1; k<=npar;k++) + printf(" %d %8.5f",k,p[k]); + printf("\n"); if(mle>=1){ /* Could be 1 or 2 */ mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func); } @@ -3997,7 +4552,7 @@ int main(int argc, char *argv[]) } } } - if(mle==1){ + if(mle!=0){ /* Computing hessian and covariance matrix */ ftolhess=ftol; /* Usually correct */ hesscov(matcov, p, npar, delti, ftolhess, func); @@ -4128,53 +4683,23 @@ int main(int argc, char *argv[]) fprintf(ficparo,"popforecast=%d popfile=%s popfiledate=%.lf/%.lf/%.lf last-popfiledate=%.lf/%.lf/%.lf\n",popforecast,popfile,jpyram,mpyram,anpyram,jpyram1,mpyram1,anpyram1); fprintf(ficres,"popforecast=%d popfile=%s popfiledate=%.lf/%.lf/%.lf last-popfiledate=%.lf/%.lf/%.lf\n",popforecast,popfile,jpyram,mpyram,anpyram,jpyram1,mpyram1,anpyram1); - probs= ma3x(1,AGESUP,1,NCOVMAX, 1,NCOVMAX); - freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); + /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint);*/ + /*,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/ - /*------------ gnuplot -------------*/ - strcpy(optionfilegnuplot,optionfilefiname); - strcat(optionfilegnuplot,".gp"); - if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) { - printf("Problem with file %s",optionfilegnuplot); - } - else{ - fprintf(ficgp,"\n# %s\n", version); - fprintf(ficgp,"# %s\n", optionfilegnuplot); - fprintf(ficgp,"set missing 'NaNq'\n"); - } - fclose(ficgp); - printinggnuplot(fileres, ageminpar,agemaxpar,fage, pathc,p); - /*--------- index.htm --------*/ - - strcpy(optionfilehtm,optionfile); - strcat(optionfilehtm,".htm"); - if((fichtm=fopen(optionfilehtm,"w"))==NULL) { - printf("Problem with %s \n",optionfilehtm), exit(0); - } + printinggnuplot(fileres, optionfilefiname,ageminpar,agemaxpar,fage, path,p); - fprintf(fichtm," %s
          \n -Title=%s
          Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=%s
          \n -\n -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 -
          -
          • Parameter files

            \n - - Copy of the parameter file: o%s
            \n - - Log file of the run: %s
            \n - - Gnuplot file name: %s
          \n",version,title,datafile,firstpass,lastpass,stepm, weightopt,model,imx,agemin,agemax,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); + printinghtml(fileres,title,datafile, firstpass, lastpass, stepm, weightopt,\ + model,imx,jmin,jmax,jmean,rfileres,popforecast,estepm,\ + jprev1,mprev1,anprev1,jprev2,mprev2,anprev2); /*------------ free_vector -------------*/ - chdir(path); + /* chdir(path); */ free_ivector(wav,1,imx); free_imatrix(dh,1,lastpass-firstpass+1,1,imx); free_imatrix(bh,1,lastpass-firstpass+1,1,imx); free_imatrix(mw,1,lastpass-firstpass+1,1,imx); - free_ivector(num,1,n); + free_lvector(num,1,n); free_vector(agedc,1,n); /*free_matrix(covar,0,NCOVMAX,1,n);*/ /*free_matrix(covar,1,NCOVMAX,1,n);*/ @@ -4293,6 +4818,7 @@ Interval (in months) between two waves: fclose(ficrespij); + probs= ma3x(1,AGESUP,1,NCOVMAX, 1,NCOVMAX); /*---------- Forecasting ------------------*/ /*if((stepm == 1) && (strcmp(model,".")==0)){*/ @@ -4340,7 +4866,7 @@ Interval (in months) between two waves: 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(agemin, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); + 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); */ @@ -4484,8 +5010,8 @@ ageminpar, agemax, s[lastpass][imx], age free_ivector(Tage,1,15); free_ivector(Tcode,1,100); - /* fclose(fichtm);*/ - /* fclose(ficgp);*/ /* ALready done */ + fflush(fichtm); + fflush(ficgp); if(erreur >0){ @@ -4498,15 +5024,28 @@ ageminpar, agemax, s[lastpass][imx], age 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);*/ - /*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("Localtime at start %s\nLocaltime at end %s",strstart, strtend); + fprintf(ficlog,"Localtime at start %s\nLocal time at end %s",strstart, strtend); + /* printf("Total time used %d Sec\n", asc_time(end_time.tv_sec -start_time.tv_sec);*/ + + 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); + fprintf(ficlog,"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);*/ +/* if(fileappend(fichtm,optionfilehtm)){ */ + fprintf(fichtm,"
          Local time at start %s
          Local time at end %s
          ",strstart, strtend); + fclose(fichtm); + fclose(ficgp); /*------ End -----------*/ end: #ifdef windows /* chdir(pathcd);*/ #endif + chdir(path); /*system("wgnuplot graph.plt");*/ /*system("../gp37mgw/wgnuplot graph.plt");*/ /*system("cd ../gp37mgw");*/ @@ -4532,3 +5071,4 @@ ageminpar, agemax, s[lastpass][imx], age } +