#define GLOCK_ERROR_NOPATH -1 /* empty path */\r
#define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */\r
\r
-\r
-\r
#define MAXPARM 30 /* Maximum number of parameters for the optimization */\r
#define NPARMAX 64 /* (nlstate+ndeath-1)*nlstate*ncovmodel */\r
\r
\r
int nvar;\r
static int cptcov;\r
-int cptcovn;\r
+int cptcovn, cptcovage=0;\r
int npar=NPARMAX;\r
int nlstate=2; /* Number of live states */\r
int ndeath=1; /* Number of dead states */\r
FILE *ficresvpl;\r
char fileresvpl[FILENAMELENGTH];\r
\r
-\r
-\r
-\r
#define NR_END 1\r
#define FREE_ARG char*\r
#define FTOL 1.0e-10\r
/* Stepm, step in month: minimum step interpolation*/\r
\r
int m,nb;\r
-int *num, firstpass=0, lastpass=4,*cod, *ncodemax;\r
+int *num, firstpass=0, lastpass=4,*cod, *ncodemax, *Tage;\r
double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;\r
double **pmmij;\r
\r
\r
void cutv(char *u,char *v, char*t, char occ)\r
{\r
- int i,lg,j,p;\r
+ int i,lg,j,p=0;\r
i=0;\r
for(j=0; j<=strlen(t)-1; j++) {\r
if((t[j]!= occ) && (t[j+1]== occ)) p=j+1;\r
lg=strlen(t);\r
for(j=0; j<p; j++) {\r
(u[j] = t[j]);\r
- u[p]='\0';\r
}\r
+ u[p]='\0';\r
\r
for(j=0; j<= lg; j++) {\r
if (j>=(p+1))(v[j-p-1] = t[j]);\r
for (j=1;j<=nlstate+ndeath;j++){\r
oldm[ii][j]=(ii==j ? 1.0 : 0.0);\r
}\r
- /* Even if hstepm = 1, at least one multiplication by the unit matrix */\r
+\r
+ cov[1]=1.;\r
+ \r
+ /* Even if hstepm = 1, at least one multiplication by the unit matrix */\r
for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){\r
newm=savm;\r
/* Covariates have to be included here again */\r
- cov[1]=1.;\r
- cov[2]=agefin;\r
- if (cptcovn>0){\r
- for (k=1; k<=cptcovn;k++) {cov[2+k]=nbcode[Tvar[k]][codtab[ij][k]];/*printf("Tcode[ij]=%d nbcode=%d\n",Tcode[ij],nbcode[k][Tcode[ij]]);*/}\r
- }\r
+ cov[2]=agefin;\r
+ \r
+ for (k=1; k<=cptcovn;k++) {\r
+ cov[2+k]=nbcode[Tvar[k]][codtab[ij][k]];\r
+\r
+/*printf("Tcode[ij]=%d nbcode=%d\n",Tcode[ij],nbcode[k][Tcode[ij]]);*/\r
+ }\r
+ for (k=1; k<=cptcovage;k++)\r
+ cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];\r
+ \r
out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);\r
\r
savm=oldm;\r
/*************** log-likelihood *************/\r
double func( double *x)\r
{\r
- int i, ii, j, k, mi, d;\r
+ int i, ii, j, k, mi, d, kk;\r
double l, ll[NLSTATEMAX], cov[NCOVMAX];\r
double **out;\r
double sw; /* Sum of weights */\r
/*for(i=1;i<imx;i++) \r
printf(" %d\n",s[4][i]);\r
*/\r
+ cov[1]=1.;\r
\r
for(k=1; k<=nlstate; k++) ll[k]=0.;\r
for (i=1,ipmx=0, sw=0.; i<=imx; i++){\r
+ for (k=1; k<=cptcovn;k++) cov[2+k]=covar[Tvar[k]][i];\r
for(mi=1; mi<= wav[i]-1; mi++){\r
for (ii=1;ii<=nlstate+ndeath;ii++)\r
for (j=1;j<=nlstate+ndeath;j++) oldm[ii][j]=(ii==j ? 1.0 : 0.0);\r
for(d=0; d<dh[mi][i]; d++){\r
- newm=savm;\r
- cov[1]=1.;\r
- cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;\r
- if (cptcovn>0){\r
- for (k=1; k<=cptcovn;k++) cov[2+k]=covar[1+k-1][i];\r
- }\r
+ newm=savm;\r
+ cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;\r
+ for (kk=1; kk<=cptcovage;kk++) {\r
+ cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];\r
+ /*printf("%d %d",kk,Tage[kk]);*/\r
+ }\r
+ /*cov[4]=covar[1][i]*cov[2];scanf("%d", i);*/\r
+ /*cov[3]=pow(cov[2],2)/1000.;*/\r
+\r
out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,\r
1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));\r
savm=oldm;\r
/*********** Tricode ****************************/\r
void tricode(int *Tvar, int **nbcode, int imx)\r
{\r
- int Ndum[80],ij, k, j, i;\r
+ int Ndum[80],ij=1, k, j, i, Ntvar[20];\r
int cptcode=0;\r
for (k=0; k<79; k++) Ndum[k]=0;\r
for (k=1; k<=7; k++) ncodemax[k]=0;\r
- \r
+\r
for (j=1; j<=cptcovn; j++) {\r
for (i=1; i<=imx; i++) {\r
ij=(int)(covar[Tvar[j]][i]);\r
} \r
} \r
} \r
-\r
- }\r
+ \r
+}\r
\r
/*********** Health Expectancies ****************/\r
\r
printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncov=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncov, nlstate,ndeath, maxwav, mle, weightopt,model);\r
fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncov=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncov,nlstate,ndeath,maxwav, mle, weightopt,model);\r
\r
- covar=matrix(1,NCOVMAX,1,n); \r
+ covar=matrix(0,NCOVMAX,1,n); \r
if (strlen(model)<=1) cptcovn=0;\r
else {\r
j=0;\r
printf("\n");\r
\r
\r
- if(mle==1){\r
/*-------- data file ----------*/\r
if((ficres =fopen(fileres,"w"))==NULL) {\r
printf("Problem with resultfile: %s\n", fileres);goto end;\r
imx=i-1; /* Number of individuals */\r
\r
/* Calculation of the number of parameter from char model*/\r
- Tvar=ivector(1,8); \r
+ Tvar=ivector(1,15); \r
+ Tage=ivector(1,15); \r
\r
if (strlen(model) >1){\r
- j=0;\r
+ j=0, j1=0;\r
j=nbocc(model,'+');\r
+ j1=nbocc(model,'*');\r
cptcovn=j+1;\r
\r
strcpy(modelsav,model); \r
if (j==0) {\r
- cutv(stra,strb,modelsav,'V'); Tvar[1]=atoi(strb);\r
+ if (j1==0){\r
+ cutv(stra,strb,modelsav,'V');\r
+ Tvar[1]=atoi(strb);\r
+ }\r
+ else if (j1==1) {\r
+ cutv(stra,strb,modelsav,'*');\r
+ /* printf("stra=%s strb=%s modelsav=%s ",stra,strb,modelsav);*/\r
+ Tage[1]=1; cptcovage++;\r
+ if (strcmp(stra,"age")==0) {\r
+ cutv(strd,strc,strb,'V');\r
+ Tvar[1]=atoi(strc);\r
+ }\r
+ else if (strcmp(strb,"age")==0) {\r
+ cutv(strd,strc,stra,'V');\r
+ Tvar[1]=atoi(strc);\r
+ }\r
+ else {printf("Error"); exit(0);}\r
+ }\r
}\r
else {\r
for(i=j; i>=1;i--){\r
cutv(stra,strb,modelsav,'+');\r
+ /*printf("%s %s %s\n", stra,strb,modelsav);*/\r
if (strchr(strb,'*')) {\r
cutv(strd,strc,strb,'*');\r
- cutv(strb,stre,strc,'V');Tvar[i+1]=ncov+1;\r
- cutv(strb,strc,strd,'V'); \r
- for (k=1; k<=lastobs;k++) \r
- covar[ncov+1][k]=covar[atoi(stre)][k]*covar[atoi(strc)][k];\r
+ if (strcmp(strc,"age")==0) {\r
+ cutv(strb,stre,strd,'V');\r
+ Tvar[i+1]=atoi(stre);\r
+ cptcovage++;\r
+ Tage[cptcovage]=i+1;\r
+ printf("stre=%s ", stre);\r
+ }\r
+ else if (strcmp(strd,"age")==0) {\r
+ cutv(strb,stre,strc,'V');\r
+ Tvar[i+1]=atoi(stre);\r
+ cptcovage++;\r
+ Tage[cptcovage]=i+1;\r
+ }\r
+ else {\r
+ cutv(strb,stre,strc,'V');\r
+ Tvar[i+1]=ncov+1;\r
+ cutv(strb,strc,strd,'V'); \r
+ for (k=1; k<=lastobs;k++) \r
+ covar[ncov+1][k]=covar[atoi(stre)][k]*covar[atoi(strc)][k];\r
+ }\r
}\r
- else {cutv(strd,strc,strb,'V');\r
+ else {\r
+ cutv(strd,strc,strb,'V');\r
+ /* printf("%s %s %s", strd,strc,strb);*/\r
+\r
Tvar[i+1]=atoi(strc);\r
}\r
strcpy(modelsav,stra); \r
Tvar[1]=atoi(strc);\r
}\r
}\r
- /*printf("tvar=%d ",Tvar[1]);\r
- scanf("%d ",i);*/ \r
+\r
+ /* printf("tvar=%d %d cptcovage=%d %d",Tvar[1],Tvar[2],cptcovage,Tage[1]);\r
+ scanf("%d ",i);*/\r
fclose(fic);\r
\r
+ if(mle==1){\r
if (weightopt != 1) { /* Maximisation without weights*/\r
for(i=1;i<=n;i++) weight[i]=1.0;\r
}\r
concatwav(wav, dh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);\r
\r
\r
-Tcode=ivector(1,100);\r
+ Tcode=ivector(1,100);\r
nbcode=imatrix(1,nvar,1,8); \r
ncodemax[1]=1;\r
if (cptcovn > 0) tricode(Tvar,nbcode,imx);\r
}\r
} \r
\r
- /*for(i=1; i <=m ;i++){ \r
+ /* for(i=1; i <=m ;i++){ \r
for(k=1; k <=cptcovn; k++){\r
printf("i=%d k=%d %d ",i,k,codtab[i][k]);\r
}\r
printf("\n");\r
- }*/\r
- /*scanf("%d",i);*/\r
+ }\r
+ scanf("%d",i);*/\r
\r
/* Calculates basic frequencies. Computes observed prevalence at single age\r
and prints on file fileres'p'. */\r
- freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax);\r
+ freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax);\r
\r
pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */\r
oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */\r
fprintf(ficgp," exp(p%d+p%d*x",i,i+1);\r
\r
for(j=3; j <=ncovmodel; j++) \r
- fprintf(ficgp,"+p%d*%d",k2,nbcode[Tvar[j-2]][codtab[jk][j-2]]);\r
+ fprintf(ficgp,"+p%d*%d",i+j-1,nbcode[Tvar[j-2]][codtab[jk][j-2]]);\r
fprintf(ficgp,")/(1");\r
-\r
- for(k1=1; k1 <=nlstate+1; k1=k1+2){ \r
- fprintf(ficgp,"+exp(p%d+p%d*x",k1+k3-1,k1+k3);\r
-\r
- for(j=3; j <=ncovmodel; j++)\r
- fprintf(ficgp,"+p%d*%d",k2,nbcode[Tvar[j-2]][codtab[jk][j-2]]);\r
- fprintf(ficgp,")");\r
+ \r
+ for(k1=1; k1 <=nlstate; k1++){ \r
+ fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);\r
+ for(j=3; j <=ncovmodel; j++)\r
+ fprintf(ficgp,"+p%d*%d",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][j-2]]);\r
+ fprintf(ficgp,")");\r
}\r
fprintf(ficgp,") t \"p%d%d\" ", k2,k);\r
if ((k+k2)!= (nlstate*2+ndeath)) fprintf(ficgp,",");\r
- i=i+ncovmodel;\r
+ i=i+ncovmodel;\r
}\r
}\r
}\r
- fprintf(ficgp,"\nset out \"pe%s%d.gif\" \nreplot\n\n",strtok(optionfile, "."),jk); \r
- }\r
+ fprintf(ficgp,"\nset out \"pe%s%d.gif\" \nreplot\n\n",strtok(optionfile, "."),jk); \r
+ }\r
\r
fclose(ficgp);\r
\r
for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){\r
k=k+1;\r
/*printf("cptcov=%d cptcod=%d codtab=%d nbcode=%d\n",cptcov, cptcod,Tcode[cptcode],codtab[cptcod][cptcov]);*/\r
- fprintf(ficrespl,"\n#****** ");\r
+ fprintf(ficrespl,"\n#******");\r
for(j=1;j<=cptcovn;j++) \r
- fprintf(ficrespl,"V%d=%d ",Tvar[j],nbcode[Tvar[j]][codtab[k][j]]);\r
+ fprintf(ficrespl," V%d=%d ",Tvar[j],nbcode[Tvar[j]][codtab[k][j]]);\r
fprintf(ficrespl,"******\n");\r
\r
for (age=agebase; age<=agelim; age++){\r