--- imach/src/imach.c 2017/05/13 10:25:05 1.267 +++ imach/src/imach.c 2017/06/29 09:47:08 1.274 @@ -1,6 +1,27 @@ -/* $Id: imach.c,v 1.267 2017/05/13 10:25:05 brouard Exp $ +/* $Id: imach.c,v 1.274 2017/06/29 09:47:08 brouard Exp $ $State: Exp $ $Log: imach.c,v $ + Revision 1.274 2017/06/29 09:47:08 brouard + Summary: Version 0.99r14 + + Revision 1.273 2017/06/27 11:06:02 brouard + Summary: More documentation on projections + + Revision 1.272 2017/06/27 10:22:40 brouard + Summary: Color of backprojection changed from 6 to 5(yellow) + + Revision 1.271 2017/06/27 10:17:50 brouard + Summary: Some bug with rint + + Revision 1.270 2017/05/24 05:45:29 brouard + *** empty log message *** + + Revision 1.269 2017/05/23 08:39:25 brouard + Summary: Code into subroutine, cleanings + + Revision 1.268 2017/05/18 20:09:32 brouard + Summary: backprojection and confidence intervals of backprevalence + Revision 1.267 2017/05/13 10:25:05 brouard Summary: temporary save for backprojection @@ -989,6 +1010,7 @@ typedef struct { #define YEARM 12. /**< Number of months per year */ /* #define AGESUP 130 */ #define AGESUP 150 +#define AGEINF 0 #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */ #define AGEBASE 40 #define AGEOVERFLOW 1.e20 @@ -1003,12 +1025,12 @@ typedef struct { #define ODIRSEPARATOR '\\' #endif -/* $Id: imach.c,v 1.267 2017/05/13 10:25:05 brouard Exp $ */ +/* $Id: imach.c,v 1.274 2017/06/29 09:47:08 brouard Exp $ */ /* $State: Exp $ */ #include "version.h" char version[]=__IMACH_VERSION__; char copyright[]="February 2016,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121), Intel Software 2015-2018"; -char fullversion[]="$Revision: 1.267 $ $Date: 2017/05/13 10:25:05 $"; +char fullversion[]="$Revision: 1.274 $ $Date: 2017/06/29 09:47:08 $"; char strstart[80]; char optionfilext[10], optionfilefiname[FILENAMELENGTH]; int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */ @@ -1080,8 +1102,7 @@ FILE *ficrescveij; char filerescve[FILENAMELENGTH]; FILE *ficresvij; char fileresv[FILENAMELENGTH]; -FILE *ficresvpl; -char fileresvpl[FILENAMELENGTH]; + char title[MAXLINE]; char model[MAXLINE]; /**< The model line */ char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH]; @@ -1179,8 +1200,8 @@ double *agedc; double **covar; /**< covar[j,i], value of jth covariate for individual i, * covar=matrix(0,NCOVMAX,1,n); * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */ -double **coqvar; /* Fixed quantitative covariate iqv */ -double ***cotvar; /* Time varying covariate itv */ +double **coqvar; /* Fixed quantitative covariate nqv */ +double ***cotvar; /* Time varying covariate ntv */ double ***cotqvar; /* Time varying quantitative covariate itqv */ double idx; int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */ @@ -2742,7 +2763,7 @@ Earliest age to start was %d-%d=%d, ncvl /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, ageminpar, agemaxpar, dnewm, doldm, dsavm,ij)); /\* Bug Valgrind *\/ */ /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij)); /\* Bug Valgrind *\/ */ out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij)); /* Bug Valgrind */ - /* if((int)age == 70){ */ + /* if((int)age == 86 || (int)age == 87){ */ /* printf(" Backward prevalim age=%d agefin=%d \n", (int) age, (int) agefin); */ /* for(i=1; i<=nlstate+ndeath; i++) { */ /* printf("%d newm= ",i); */ @@ -2753,7 +2774,7 @@ Earliest age to start was %d-%d=%d, ncvl /* for(j=1;j<=nlstate+ndeath;j++) { */ /* printf("%f ",oldm[i][j]); */ /* } */ - /* printf(" pmmij "); */ + /* printf(" bmmij "); */ /* for(j=1;j<=nlstate+ndeath;j++) { */ /* printf("%f ",pmmij[i][j]); */ /* } */ @@ -2781,9 +2802,9 @@ Earliest age to start was %d-%d=%d, ncvl meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column */ maxmax=FMAX(maxmax,meandiff[i]); /* printf("Back age= %d meandiff[%d]=%f, agefin=%d max[%d]=%f min[%d]=%f maxmax=%f\n", (int)age, i, meandiff[i],(int)agefin, i, max[i], i, min[i],maxmax); */ - } /* j loop */ + } /* i loop */ *ncvyear= -( (int)age- (int)agefin); - /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear);*/ + /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */ if(maxmax < ftolpl){ /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */ free_vector(min,1,nlstate); @@ -2905,7 +2926,7 @@ double **pmij(double **ps, double *cov, double **out, **pmij(); double sumnew=0.; double agefin; - + double k3=0.; /* constant of the w_x diagonal matrixe (in order for B to sum to 1 even for death state) */ double **dnewm, **dsavm, **doldm; double **bbmij; @@ -2914,55 +2935,66 @@ double **pmij(double **ps, double *cov, dsavm=ddsavms; agefin=cov[2]; + /* Bx = Diag(w_x) P_x Diag(Sum_i w^i_x p^ij_x */ /* bmij *//* age is cov[2], ij is included in cov, but we need for the observed prevalence (with this covariate ij) at beginning of transition */ /* dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate); */ + + /* P_x */ pmmij=pmij(pmmij,cov,ncovmodel,x,nlstate); /*This is forward probability from agefin to agefin + stepm */ - /* outputs pmmij which is a stochastic matrix */ - /* We do have the matrix Px in savm and we need pij */ + /* outputs pmmij which is a stochastic matrix in row */ + + /* Diag(w_x) */ + /* Problem with prevacurrent which can be zero */ + sumnew=0.; + /*for (ii=1;ii<=nlstate+ndeath;ii++){*/ + for (ii=1;ii<=nlstate;ii++){ /* Only on live states */ + /* printf(" agefin=%d, ii=%d, ij=%d, prev=%f\n",(int)agefin,ii, ij, prevacurrent[(int)agefin][ii][ij]); */ + sumnew+=prevacurrent[(int)agefin][ii][ij]; + } + if(sumnew >0.01){ /* At least some value in the prevalence */ + for (ii=1;ii<=nlstate+ndeath;ii++){ + for (j=1;j<=nlstate+ndeath;j++) + doldm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij]/sumnew : 0.0); + } + }else{ + for (ii=1;ii<=nlstate+ndeath;ii++){ + for (j=1;j<=nlstate+ndeath;j++) + doldm[ii][j]=(ii==j ? 1./nlstate : 0.0); + } + /* if(sumnew <0.9){ */ + /* printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */ + /* } */ + } + k3=0.0; /* We put the last diagonal to 0 */ + for (ii=nlstate+1;ii<=nlstate+ndeath;ii++){ + doldm[ii][ii]= k3; + } + /* End doldm, At the end doldm is diag[(w_i)] */ + + /* left Product of this diag matrix by pmmij=Px (dnewm=dsavm*doldm) */ + bbmij=matprod2(dnewm, doldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, pmmij); /* Bug Valgrind */ + + /* Diag(Sum_i w^i_x p^ij_x */ + /* w1 p11 + w2 p21 only on live states N1./N..*N11/N1. + N2./N..*N21/N2.=(N11+N21)/N..=N.1/N.. */ for (j=1;j<=nlstate+ndeath;j++){ - sumnew=0.; /* w1 p11 + w2 p21 only on live states N1./N..*N11/N1. + N2./N..*N21/N2.=(N11+N21)/N..=N.1/N.. */ + sumnew=0.; for (ii=1;ii<=nlstate;ii++){ /* sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij]; */ - sumnew+=pmmij[ii][j]*prevacurrent[(int)agefin][ii][ij]; /* Yes prevalence at beginning of transition */ + sumnew+=pmmij[ii][j]*doldm[ii][ii]; /* Yes prevalence at beginning of transition */ } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */ - if(sumnew >= 1.e-10){ - for (ii=1;ii<=nlstate+ndeath;ii++){ + for (ii=1;ii<=nlstate+ndeath;ii++){ /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */ - /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */ + /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */ /* }else if(agefin >= agemaxpar+stepm/YEARM){ */ - /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */ + /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */ /* }else */ - doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); - } /*End ii */ - }else{ /* We put the identity matrix */ - for (ii=1;ii<=nlstate+ndeath;ii++){ - doldm[ii][j]=(ii==j ? 1. : 0.0); - } /*End ii */ - /* printf("Problem internal bmij A: sum_i w_i*p_ij=N.j/N.. <1.e-10 i=%d, j=%d, sumnew=%lf,agefin=%d\n",ii,j,sumnew, (int)agefin); */ - } - } /* End j, At the end doldm is diag[1/(w_1p1i+w_2 p2i)] or identity*/ - /* left Product of this diag matrix by dsavm=Px (dnewm=dsavm*doldm) */ - /* bbmij=matprod2(dnewm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, doldm); /\* Bug Valgrind *\/ */ - bbmij=matprod2(dnewm, pmmij,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, doldm); /* Bug Valgrind */ - /* dsavm=doldm; /\* dsavm is now diag [1/(w_1p1i+w_2 p2i)] but can be overwritten*\/ */ - /* doldm=dnewm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */ - /* dnewm=dsavm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */ - /* left Product of this matrix by diag matrix of prevalences (savm) */ - for (j=1;j<=nlstate+ndeath;j++){ - sumnew=0.; - for (ii=1;ii<=nlstate+ndeath;ii++){ - sumnew+=prevacurrent[(int)agefin][ii][ij]; - dsavm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij] : 0.0); - } - /* if(sumnew <0.9){ */ - /* printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */ - /* } */ - } /* End j, At the end dsavm is diag[(w_i)] */ - /* What if dsavm doesn't sum ii to 1? */ - /* ps=matprod2(doldm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dnewm); /\* Bug Valgrind *\/ */ - ps=matprod2(ps, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dnewm); /* Bug Valgrind */ - /* newm or out is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */ + dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); + } /*End ii */ + } /* End j, At the end dsavm is diag[1/(w_1p1i+w_2 p2i)] for ALL states even if the sum is only for live states */ + + ps=matprod2(ps, dnewm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dsavm); /* Bug Valgrind */ + /* ps is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */ /* end bmij */ return ps; /*pointer is unchanged */ } @@ -3223,7 +3255,7 @@ double ***hbxij(double ***po, int nhstep newm=savm; /* Covariates have to be included here again */ cov[1]=1.; - agexact=age-((h-1)*hstepm + (d))*stepm/YEARM; /* age just before transition, d or d-1? */ + agexact=age-( (h-1)*hstepm + (d) )*stepm/YEARM; /* age just before transition, d or d-1? */ /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */ cov[2]=agexact; if(nagesqr==1) @@ -3279,11 +3311,12 @@ double ***hbxij(double ***po, int nhstep for(i=1; i<=nlstate+ndeath; i++) for(j=1;j<=nlstate+ndeath;j++) { po[i][j][h]=newm[i][j]; - /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/ + /* if(h==nhstepm) */ + /* printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]); */ } - /*printf("h=%d ",h);*/ + /* printf("h=%d %.1f ",h, agexact); */ } /* end h */ - /* printf("\n H=%d \n",h); */ + /* printf("\n H=%d nhs=%d \n",h, nhstepm); */ return po; } @@ -3738,7 +3771,7 @@ double funcone( double *x) s1=s[mw[mi][i]][i]; s2=s[mw[mi+1][i]][i]; /* if(s2==-1){ */ - /* printf(" s1=%d, s2=%d i=%d \n", s1, s2, i); */ + /* printf(" ERROR s1=%d, s2=%d i=%d \n", s1, s2, i); */ /* /\* exit(1); *\/ */ /* } */ bbh=(double)bh[mi][i]/(double)stepm; @@ -3771,7 +3804,7 @@ double funcone( double *x) fprintf(ficresilk,"%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\ %11.6f %11.6f %11.6f ", \ num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw, - 2*weight[i]*lli,out[s1][s2],savm[s1][s2]); + 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: 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); @@ -3824,7 +3857,7 @@ void likelione(FILE *ficres,double p[], else if(mle >=1) fprintf(fichtm,"\n
File of contributions to the likelihood computed with optimized parameters mle = %d.",mle); fprintf(fichtm," You should at least run with mle >= 1 to get starting values corresponding to the optimized parameters in order to visualize the real contribution of each individual/wave: %s
\n",subdirf(fileresilk),subdirf(fileresilk)); - + fprintf(fichtm,"\n
Equation of the model: model=1+age+%s
\n",model); for (k=1; k<= nlstate ; k++) { fprintf(fichtm,"
- Probability p%dj by origin %d and destination j. Dot's sizes are related to corresponding weight: %s-p%dj.png
\ @@ -4291,70 +4324,7 @@ void pstamp(FILE *fichier) fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart); } -int linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb) { - /* y=a+bx regression */ - double sumx = 0.0; /* sum of x */ - double sumx2 = 0.0; /* sum of x**2 */ - double sumxy = 0.0; /* sum of x * y */ - double sumy = 0.0; /* sum of y */ - double sumy2 = 0.0; /* sum of y**2 */ - double sume2; /* sum of square or residuals */ - double yhat; - - double denom=0; - int i; - int ne=*no; - - for ( i=ifi, ne=0;i<=ila;i++) { - if(!isfinite(x[i]) || !isfinite(y[i])){ - /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */ - continue; - } - ne=ne+1; - sumx += x[i]; - sumx2 += x[i]*x[i]; - sumxy += x[i] * y[i]; - sumy += y[i]; - sumy2 += y[i]*y[i]; - denom = (ne * sumx2 - sumx*sumx); - /* printf("ne=%d, i=%d,x[%d]=%f, y[%d]=%f sumx=%f, sumx2=%f, sumxy=%f, sumy=%f, sumy2=%f, denom=%f\n",ne,i,i,x[i],i,y[i], sumx, sumx2,sumxy, sumy, sumy2,denom); */ - } - - denom = (ne * sumx2 - sumx*sumx); - if (denom == 0) { - // vertical, slope m is infinity - *b = INFINITY; - *a = 0; - if (r) *r = 0; - return 1; - } - - *b = (ne * sumxy - sumx * sumy) / denom; - *a = (sumy * sumx2 - sumx * sumxy) / denom; - if (r!=NULL) { - *r = (sumxy - sumx * sumy / ne) / /* compute correlation coeff */ - sqrt((sumx2 - sumx*sumx/ne) * - (sumy2 - sumy*sumy/ne)); - } - *no=ne; - for ( i=ifi, ne=0;i<=ila;i++) { - if(!isfinite(x[i]) || !isfinite(y[i])){ - /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */ - continue; - } - ne=ne+1; - yhat = y[i] - *a -*b* x[i]; - sume2 += yhat * yhat ; - - denom = (ne * sumx2 - sumx*sumx); - /* printf("ne=%d, i=%d,x[%d]=%f, y[%d]=%f sumx=%f, sumx2=%f, sumxy=%f, sumy=%f, sumy2=%f, denom=%f\n",ne,i,i,x[i],i,y[i], sumx, sumx2,sumxy, sumy, sumy2,denom); */ - } - *sb = sqrt(sume2/(ne-2)/(sumx2 - sumx * sumx /ne)); - *sa= *sb * sqrt(sumx2/ne); - - return 0; -} /************ Frequencies ********************/ void freqsummary(char fileres[], double p[], double pstart[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \ @@ -4367,8 +4337,8 @@ void freqsummary(char fileres[], double int mi; /* Effective wave */ int first; double ***freq; /* Frequencies */ - double *x, *y, a,b,r, sa, sb; /* for regression, y=b+m*x and r is the correlation coefficient */ - int no; + double *x, *y, a=0.,b=0.,r=1., sa=0., sb=0.; /* for regression, y=b+m*x and r is the correlation coefficient */ + int no=0, linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb); double *meanq; double **meanqt; double *pp, **prop, *posprop, *pospropt; @@ -4806,6 +4776,8 @@ Title=%s
Datafile=%s Firstpass=%d La y[iage]= log(freq[i][k][iage]/freq[i][i][iage]); /* printf("i=%d, k=%d, s1=%d, j1=%d, jj=%d, y[%d]=%f\n",i,k,s1,j1,jj, iage, y[iage]); */ } + /* Some are not finite, but linreg will ignore these ages */ + no=0; linreg(iagemin,iagemax,&no,x,y,&a,&b,&r, &sa, &sb ); /* y= a+b*x with standard errors */ pstart[s1]=b; pstart[s1-1]=a; @@ -4909,6 +4881,72 @@ Title=%s
Datafile=%s Firstpass=%d La /* End of freqsummary */ } +/* Simple linear regression */ +int linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb) { + + /* y=a+bx regression */ + double sumx = 0.0; /* sum of x */ + double sumx2 = 0.0; /* sum of x**2 */ + double sumxy = 0.0; /* sum of x * y */ + double sumy = 0.0; /* sum of y */ + double sumy2 = 0.0; /* sum of y**2 */ + double sume2 = 0.0; /* sum of square or residuals */ + double yhat; + + double denom=0; + int i; + int ne=*no; + + for ( i=ifi, ne=0;i<=ila;i++) { + if(!isfinite(x[i]) || !isfinite(y[i])){ + /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */ + continue; + } + ne=ne+1; + sumx += x[i]; + sumx2 += x[i]*x[i]; + sumxy += x[i] * y[i]; + sumy += y[i]; + sumy2 += y[i]*y[i]; + denom = (ne * sumx2 - sumx*sumx); + /* printf("ne=%d, i=%d,x[%d]=%f, y[%d]=%f sumx=%f, sumx2=%f, sumxy=%f, sumy=%f, sumy2=%f, denom=%f\n",ne,i,i,x[i],i,y[i], sumx, sumx2,sumxy, sumy, sumy2,denom); */ + } + + denom = (ne * sumx2 - sumx*sumx); + if (denom == 0) { + // vertical, slope m is infinity + *b = INFINITY; + *a = 0; + if (r) *r = 0; + return 1; + } + + *b = (ne * sumxy - sumx * sumy) / denom; + *a = (sumy * sumx2 - sumx * sumxy) / denom; + if (r!=NULL) { + *r = (sumxy - sumx * sumy / ne) / /* compute correlation coeff */ + sqrt((sumx2 - sumx*sumx/ne) * + (sumy2 - sumy*sumy/ne)); + } + *no=ne; + for ( i=ifi, ne=0;i<=ila;i++) { + if(!isfinite(x[i]) || !isfinite(y[i])){ + /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */ + continue; + } + ne=ne+1; + yhat = y[i] - *a -*b* x[i]; + sume2 += yhat * yhat ; + + denom = (ne * sumx2 - sumx*sumx); + /* printf("ne=%d, i=%d,x[%d]=%f, y[%d]=%f sumx=%f, sumx2=%f, sumxy=%f, sumy=%f, sumy2=%f, denom=%f\n",ne,i,i,x[i],i,y[i], sumx, sumx2,sumxy, sumy, sumy2,denom); */ + } + *sb = sqrt(sume2/(double)(ne-2)/(sumx2 - sumx * sumx /(double)ne)); + *sa= *sb * sqrt(sumx2/ne); + + return 0; +} + /************ Prevalence ********************/ 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) { @@ -5441,7 +5479,7 @@ void concatwav(int wav[], int **dh, int /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm. nhstepm is the number of hstepm from age to agelim nstepm is the number of stepm from age to agelin. - Look at hpijx to understand the reason of that which relies in memory size + Look at hpijx to understand the reason which relies in memory size consideration and note for a fixed period like estepm months */ /* We decided (b) to get a life expectancy respecting the most precise curvature of the survival function given by stepm (the optimization length). Unfortunately it @@ -5672,7 +5710,8 @@ void concatwav(int wav[], int **dh, int /* if((int)age==70)printf("i=%2d,j=%2d,h=%2d,age=%3d,%9.4f,%9.4f,%9.4f\n",i,j,h,(int)age,p3mat[i][j][h],hf,eij[i][j][(int)age]);*/ } - + + /* Standard deviation of expectancies ij */ fprintf(ficresstdeij,"%3.0f",age ); for(i=1; i<=nlstate;i++){ eip=0.; @@ -5687,6 +5726,7 @@ void concatwav(int wav[], int **dh, int } fprintf(ficresstdeij,"\n"); + /* Variance of expectancies ij */ fprintf(ficrescveij,"%3.0f",age ); for(i=1; i<=nlstate;i++) for(j=1; j<=nlstate;j++){ @@ -6040,12 +6080,12 @@ void concatwav(int wav[], int **dh, int } /* 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 *ncvyearp, int ij, char strstart[], int nres) + void varprevlim(char fileresvpl[], FILE *ficresvpl, 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 *ncvyearp, int ij, char strstart[], int nres) { /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/ /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/ - double **dnewm,**doldm; + double **dnewmpar,**doldm; int i, j, nhstepm, hstepm; double *xp; double *gp, *gm; @@ -6064,7 +6104,7 @@ void concatwav(int wav[], int **dh, int fprintf(ficresvpl,"\n"); xp=vector(1,npar); - dnewm=matrix(1,nlstate,1,npar); + dnewmpar=matrix(1,nlstate,1,npar); doldm=matrix(1,nlstate,1,nlstate); hstepm=1*YEARM; /* Every year of age */ @@ -6134,11 +6174,11 @@ void concatwav(int wav[], int **dh, int for(i=1;i<=nlstate;i++) varpl[i][(int)age] =0.; if((int)age==79 ||(int)age== 80 ||(int)age== 81){ - matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov); - matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg); + matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov); + matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg); }else{ - matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov); - matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg); + matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov); + matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg); } for(i=1;i<=nlstate;i++) varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */ @@ -6159,7 +6199,132 @@ void concatwav(int wav[], int **dh, int free_vector(xp,1,npar); free_matrix(doldm,1,nlstate,1,npar); - free_matrix(dnewm,1,nlstate,1,nlstate); + free_matrix(dnewmpar,1,nlstate,1,nlstate); + +} + + +/************ Variance of backprevalence limit ******************/ + void varbrevlim(char fileresvbl[], FILE *ficresvbl, double **varbpl, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **bprlim, double ftolpl, int mobilavproj, int *ncvyearp, int ij, char strstart[], int nres) +{ + /* Variance of backward prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/ + /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/ + + double **dnewmpar,**doldm; + int i, j, nhstepm, hstepm; + double *xp; + double *gp, *gm; + double **gradg, **trgradg; + double **mgm, **mgp; + double age,agelim; + int theta; + + pstamp(ficresvbl); + fprintf(ficresvbl,"# Standard deviation of back (stable) prevalences \n"); + fprintf(ficresvbl,"# Age "); + if(nresult >=1) + fprintf(ficresvbl," Result# "); + for(i=1; i<=nlstate;i++) + fprintf(ficresvbl," %1d-%1d",i,i); + fprintf(ficresvbl,"\n"); + + xp=vector(1,npar); + dnewmpar=matrix(1,nlstate,1,npar); + doldm=matrix(1,nlstate,1,nlstate); + + hstepm=1*YEARM; /* Every year of age */ + hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */ + agelim = AGEINF; + for (age=fage; age>=bage; age --){ /* If stepm=6 months */ + nhstepm=(int) rint((age-agelim)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */ + if (stepm >= YEARM) hstepm=1; + nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */ + gradg=matrix(1,npar,1,nlstate); + mgp=matrix(1,npar,1,nlstate); + mgm=matrix(1,npar,1,nlstate); + gp=vector(1,nlstate); + gm=vector(1,nlstate); + + for(theta=1; theta <=npar; theta++){ + for(i=1; i<=npar; i++){ /* Computes gradient */ + xp[i] = x[i] + (i==theta ?delti[theta]:0); + } + if(mobilavproj > 0 ) + bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres); + else + bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres); + for(i=1;i<=nlstate;i++){ + gp[i] = bprlim[i][i]; + mgp[theta][i] = bprlim[i][i]; + } + for(i=1; i<=npar; i++) /* Computes gradient */ + xp[i] = x[i] - (i==theta ?delti[theta]:0); + if(mobilavproj > 0 ) + bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres); + else + bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres); + for(i=1;i<=nlstate;i++){ + gm[i] = bprlim[i][i]; + mgm[theta][i] = bprlim[i][i]; + } + for(i=1;i<=nlstate;i++) + gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta]; + /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */ + } /* End theta */ + + trgradg =matrix(1,nlstate,1,npar); + + for(j=1; j<=nlstate;j++) + for(theta=1; theta <=npar; theta++) + trgradg[j][theta]=gradg[theta][j]; + /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */ + /* printf("\nmgm mgp %d ",(int)age); */ + /* for(j=1; j<=nlstate;j++){ */ + /* printf(" %d ",j); */ + /* for(theta=1; theta <=npar; theta++) */ + /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */ + /* printf("\n "); */ + /* } */ + /* } */ + /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */ + /* printf("\n gradg %d ",(int)age); */ + /* for(j=1; j<=nlstate;j++){ */ + /* printf("%d ",j); */ + /* for(theta=1; theta <=npar; theta++) */ + /* printf("%d %lf ",theta,gradg[theta][j]); */ + /* printf("\n "); */ + /* } */ + /* } */ + + for(i=1;i<=nlstate;i++) + varbpl[i][(int)age] =0.; + if((int)age==79 ||(int)age== 80 ||(int)age== 81){ + matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov); + matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg); + }else{ + matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov); + matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg); + } + for(i=1;i<=nlstate;i++) + varbpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */ + + fprintf(ficresvbl,"%.0f ",age ); + if(nresult >=1) + fprintf(ficresvbl,"%d ",nres ); + for(i=1; i<=nlstate;i++) + fprintf(ficresvbl," %.5f (%.5f)",bprlim[i][i],sqrt(varbpl[i][(int)age])); + fprintf(ficresvbl,"\n"); + free_vector(gp,1,nlstate); + free_vector(gm,1,nlstate); + free_matrix(mgm,1,npar,1,nlstate); + free_matrix(mgp,1,npar,1,nlstate); + free_matrix(gradg,1,npar,1,nlstate); + free_matrix(trgradg,1,nlstate,1,npar); + } /* End age */ + + free_vector(xp,1,npar); + free_matrix(doldm,1,nlstate,1,npar); + free_matrix(dnewmpar,1,nlstate,1,nlstate); } @@ -6505,8 +6670,8 @@ void printinghtml(char fileresu[], char int lastpass, int stepm, int weightopt, char model[],\ int imx,int jmin, int jmax, double jmeanint,char rfileres[],\ int popforecast, int mobilav, int prevfcast, int mobilavproj, int backcast, int estepm , \ - double jprev1, double mprev1,double anprev1, double dateprev1, \ - double jprev2, double mprev2,double anprev2, double dateprev2){ + double jprev1, double mprev1,double anprev1, double dateprev1, double dateproj1, double dateback1, \ + double jprev2, double mprev2,double anprev2, double dateprev2, double dateproj2, double dateback2){ int jj1, k1, i1, cpt, k4, nres; fprintf(fichtm,"