\r
int nvar;\r
static int cptcov;\r
-int cptcovn, cptcovage=0;\r
+int cptcovn, cptcovage=0, cptcoveff=0;\r
int npar=NPARMAX;\r
int nlstate=2; /* Number of live states */\r
int ndeath=1; /* Number of dead states */\r
double *weight;\r
int **s; /* Status */\r
double *agedc, **covar, idx;\r
-int **nbcode, *Tcode, *Tvar, **codtab;\r
+int **nbcode, *Tcode, *Tvar, **codtab, **Tvard, *Tprod, cptcovprod, *Tvaraff;\r
\r
double ftol=FTOL; /* Tolerance for computing Max Likelihood */\r
double ftolhess; /* Tolerance for computing hessian */\r
\r
-\r
+/**************** split *************************/\r
static int split( char *path, char *dirc, char *name )\r
{\r
char *s; /* pointer */\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
+ cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];\r
+ /*printf("ij=%d Tvar[k]=%d nbcode=%d cov=%lf\n",ij, Tvar[k],nbcode[Tvar[k]][codtab[ij][Tvar[k]]],cov[2+k]);*/\r
}\r
for (k=1; k<=cptcovage;k++)\r
cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];\r
- \r
+ for (k=1; k<=cptcovprod;k++)\r
+ cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];\r
+\r
+ /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/\r
+\r
+ /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/\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
/* Covariates have to be included here again */\r
cov[1]=1.;\r
cov[2]=age+((h-1)*hstepm + (d-1))*stepm/YEARM;\r
- if (cptcovn>0){\r
- for (k=1; k<=cptcovn;k++) cov[2+k]=nbcode[Tvar[k]][codtab[ij][k]];\r
- }\r
+ for (k=1; k<=cptcovn;k++) cov[2+k]=nbcode[Tvar[k]][codtab[ij][Tvar[k]]];\r
+for (k=1; k<=cptcovage;k++)\r
+ cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2];\r
+ for (k=1; k<=cptcovprod;k++)\r
+ cov[2+Tprod[k]]=nbcode[Tvard[k][1]][codtab[ij][Tvard[k][1]]]*nbcode[Tvard[k][2]][codtab[ij][Tvard[k][2]]];\r
+\r
+\r
/*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/\r
/*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/\r
out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, \r
freq= ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,agemin,agemax+3);\r
j1=0;\r
\r
- j=cptcovn;\r
+ j=cptcoveff;\r
if (cptcovn<1) {j=1;ncodemax[1]=1;}\r
\r
for(k1=1; k1<=j;k1++){\r
for (i=1; i<=imx; i++) {\r
bool=1;\r
if (cptcovn>0) {\r
- for (z1=1; z1<=cptcovn; z1++) \r
- if (covar[Tvar[z1]][i]!= nbcode[Tvar[z1]][codtab[j1][z1]]) bool=0;\r
+ for (z1=1; z1<=cptcoveff; z1++) \r
+ if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtab[j1][z1]]) bool=0;\r
}\r
if (bool==1) {\r
for(m=firstpass; m<=lastpass-1; m++){\r
}\r
}\r
if (cptcovn>0) {\r
- fprintf(ficresp, "\n#Variable"); \r
- for (z1=1; z1<=cptcovn; z1++) fprintf(ficresp, " V%d=%d",Tvar[z1],nbcode[Tvar[z1]][codtab[j1][z1]]);\r
+ fprintf(ficresp, "\n#********** Variable "); \r
+ for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtab[j1][z1]]);\r
}\r
- fprintf(ficresp, "\n#");\r
+ fprintf(ficresp, "**********\n#");\r
for(i=1; i<=nlstate;i++) \r
fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);\r
fprintf(ficresp, "\n");\r
/*********** Tricode ****************************/\r
void tricode(int *Tvar, int **nbcode, int imx)\r
{\r
- int Ndum[80],ij=1, k, j, i, Ntvar[20];\r
+ int Ndum[20],ij=1, k, j, i;\r
int cptcode=0;\r
- for (k=0; k<79; k++) Ndum[k]=0;\r
+ cptcoveff=0; \r
+ \r
+ for (k=0; k<19; k++) Ndum[k]=0;\r
for (k=1; k<=7; k++) ncodemax[k]=0;\r
\r
- for (j=1; j<=cptcovn; j++) {\r
+ for (j=1; j<=(cptcovn+2*cptcovprod); j++) {\r
for (i=1; i<=imx; i++) {\r
ij=(int)(covar[Tvar[j]][i]);\r
Ndum[ij]++; \r
if (ij > cptcode) cptcode=ij; \r
}\r
+\r
/*printf("cptcode=%d cptcovn=%d ",cptcode,cptcovn);*/\r
for (i=0; i<=cptcode; i++) {\r
if(Ndum[i]!=0) ncodemax[j]++;\r
}\r
- \r
ij=1; \r
+\r
for (i=1; i<=ncodemax[j]; i++) {\r
- for (k=0; k<=79; k++) {\r
+ for (k=0; k<=19; k++) {\r
if (Ndum[k] != 0) {\r
nbcode[Tvar[j]][ij]=k; \r
+ /* printf("ij=%d ",nbcode[Tvar[2]][1]);*/\r
ij++;\r
}\r
if (ij > ncodemax[j]) break; \r
} \r
} \r
- } \r
- \r
+ } \r
+ for (i=1; i<=10; i++) {\r
+ ij=Tvar[i];\r
+ Ndum[ij]++; \r
+ }\r
+ ij=1;\r
+ for (i=1; i<=cptcovn; i++) {\r
+ if((Ndum[i]!=0) && (i<=ncov)){\r
+ Tvaraff[i]=ij; \r
+ ij++;\r
+ }\r
+ }\r
+ \r
+ for (j=1; j<=(cptcovn+2*cptcovprod); j++) {\r
+ if ((Tvar[j]>= cptcoveff) && (Tvar[j] <=ncov)) cptcoveff=Tvar[j];\r
+ /*printf("j=%d %d\n",j,Tvar[j]);*/\r
+ }\r
+ \r
+ /* printf("cptcoveff=%d Tvaraff=%d %d\n",cptcoveff, Tvaraff[1],Tvaraff[2]);\r
+ scanf("%d",i);*/\r
}\r
\r
/*********** Health Expectancies ****************/\r
int sdeb, sfin; /* Status at beginning and end */\r
int c, h , cpt,l;\r
int ju,jl, mi;\r
- int i1,j1, k1,k2,k3,jk,aa,bb, stepsize;\r
+ int i1,j1, k1,k2,k3,jk,aa,bb, stepsize, ij;\r
int jnais,jdc,jint4,jint1,jint2,jint3,**outcome,**adl,*tab;\r
\r
int hstepm, nhstepm;\r
imx=i-1; /* Number of individuals */\r
\r
/* Calculation of the number of parameter from char model*/\r
- Tvar=ivector(1,15); \r
+ Tvar=ivector(1,15); \r
+ Tprod=ivector(1,15); \r
+ Tvaraff=ivector(1,15); \r
+ Tvard=imatrix(1,15,1,2);\r
Tage=ivector(1,15); \r
\r
if (strlen(model) >1){\r
- j=0, j1=0;\r
+ j=0, j1=0, k1=1, k2=1;\r
j=nbocc(model,'+');\r
j1=nbocc(model,'*');\r
cptcovn=j+1;\r
+ cptcovprod=j1;\r
\r
strcpy(modelsav,model); \r
- if (j==0) {\r
+ if (j==0) {\r
if (j1==0){\r
- cutv(stra,strb,modelsav,'V');\r
- Tvar[1]=atoi(strb);\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
+ cutv(stra,strb,modelsav,'*');\r
+ Tage[1]=1; cptcovage++;\r
+ if (strcmp(stra,"age")==0) {\r
+ cptcovprod--;\r
+ cutv(strd,strc,strb,'V');\r
+ Tvar[1]=atoi(strc);\r
+ }\r
+ else if (strcmp(strb,"age")==0) {\r
+ cptcovprod--;\r
+ cutv(strd,strc,stra,'V');\r
+ Tvar[1]=atoi(strc);\r
+ }\r
+ else {\r
+ cutv(strd,strc,strb,'V');\r
+ cutv(stre,strd,stra,'V');\r
+ Tvar[1]=ncov+1;\r
+ for (k=1; k<=lastobs;k++) \r
+ covar[ncov+1][k]=covar[atoi(strc)][k]*covar[atoi(strd)][k];\r
+ }\r
+ /*printf("%s %s %s\n", stra,strb,modelsav);\r
+printf("%d ",Tvar[1]);\r
+scanf("%d",i);*/\r
}\r
}\r
- else {\r
+ else {\r
for(i=j; i>=1;i--){\r
cutv(stra,strb,modelsav,'+');\r
- /*printf("%s %s %s\n", stra,strb,modelsav);*/\r
+ /*printf("%s %s %s\n", stra,strb,modelsav);\r
+ scanf("%d",i);*/\r
if (strchr(strb,'*')) {\r
cutv(strd,strc,strb,'*');\r
if (strcmp(strc,"age")==0) {\r
+ cptcovprod--;\r
cutv(strb,stre,strd,'V');\r
Tvar[i+1]=atoi(stre);\r
cptcovage++;\r
printf("stre=%s ", stre);\r
}\r
else if (strcmp(strd,"age")==0) {\r
+ cptcovprod--;\r
cutv(strb,stre,strc,'V');\r
Tvar[i+1]=atoi(stre);\r
cptcovage++;\r
}\r
else {\r
cutv(strb,stre,strc,'V');\r
- Tvar[i+1]=ncov+1;\r
+ Tvar[i+1]=ncov+k1;\r
cutv(strb,strc,strd,'V'); \r
+ Tprod[k1]=i+1;\r
+ Tvard[k1][1]=atoi(strc);\r
+ Tvard[k1][2]=atoi(stre);\r
+ Tvar[cptcovn+k2]=Tvard[k1][1];\r
+ Tvar[cptcovn+k2+1]=Tvard[k1][2]; \r
for (k=1; k<=lastobs;k++) \r
- covar[ncov+1][k]=covar[atoi(stre)][k]*covar[atoi(strc)][k];\r
+ covar[ncov+k1][k]=covar[atoi(stre)][k]*covar[atoi(strc)][k];\r
+ k1++;\r
+ k2=k2+2;\r
}\r
}\r
else {\r
cutv(strd,strc,strb,'V');\r
/* printf("%s %s %s", strd,strc,strb);*/\r
-\r
- Tvar[i+1]=atoi(strc);\r
+ Tvar[i+1]=atoi(strc);\r
}\r
strcpy(modelsav,stra); \r
}\r
Tvar[1]=atoi(strc);\r
}\r
}\r
-\r
- /* printf("tvar=%d %d cptcovage=%d %d",Tvar[1],Tvar[2],cptcovage,Tage[1]);\r
- scanf("%d ",i);*/\r
+ /* for (i=1; i<=5; i++)\r
+ printf("i=%d %d ",i,Tvar[i]);*/\r
+ /* printf("tvar=%d %d cptcovage=%d %d",Tvar[1],Tvar[2],cptcovage,Tage[1]);*/\r
+ /*printf("cptcovprod=%d ", cptcovprod);*/\r
+ /* scanf("%d ",i);*/\r
fclose(fic);\r
\r
- if(mle==1){\r
+ /* if(mle==1){*/\r
if (weightopt != 1) { /* Maximisation without weights*/\r
for(i=1;i<=n;i++) weight[i]=1.0;\r
}\r
\r
\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
+ nbcode=imatrix(1,nvar,1,8); \r
+ ncodemax[1]=1;\r
+ if (cptcovn > 0) tricode(Tvar,nbcode,imx);\r
+ \r
codtab=imatrix(1,100,1,10);\r
h=0;\r
- m=pow(2,cptcovn);\r
+ m=pow(2,cptcoveff);\r
\r
- for(k=1;k<=cptcovn; k++){\r
+ for(k=1;k<=cptcoveff; k++){\r
for(i=1; i <=(m/pow(2,k));i++){\r
for(j=1; j <= ncodemax[k]; j++){\r
- for(cpt=1; cpt <=(m/pow(2,cptcovn+1-k)); cpt++){\r
+ for(cpt=1; cpt <=(m/pow(2,cptcoveff+1-k)); cpt++){\r
h++;\r
if (h>m) h=1;codtab[h][k]=j;\r
} \r
}\r
} \r
\r
- /* for(i=1; i <=m ;i++){ \r
+\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
+ printf("i=%d k=%d %d %d",i,k,codtab[i][k], cptcoveff);\r
}\r
printf("\n");\r
}\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
/* For Powell, parameters are in a vector p[] starting at p[1]\r
so we point p on param[1][1] so that p[1] maps on param[1][1][1] */\r
p=param[1][1]; /* *(*(*(param +1)+1)+0) */\r
- \r
- mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);\r
\r
+ if(mle==1){\r
+ mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);\r
+ }\r
\r
/*--------- results files --------------*/\r
fprintf(ficres,"\ntitle=%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
}\r
}\r
-\r
+ if(mle==1){\r
/* Computing hessian and covariance matrix */\r
ftolhess=ftol; /* Usually correct */\r
hesscov(matcov, p, npar, delti, ftolhess, func);\r
+ }\r
fprintf(ficres,"# Scales\n");\r
printf("# Scales\n");\r
for(i=1,jk=1; i <=nlstate; i++){\r
\r
fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");\r
fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax,bage,fage);\r
+\r
+ \r
/*------------ gnuplot -------------*/\r
chdir(pathcd);\r
if((ficgp=fopen("graph.plt","w"))==NULL) {\r
#ifdef windows\r
fprintf(ficgp,"cd \"%s\" \n",pathc);\r
#endif\r
-m=pow(2,cptcovn);\r
+m=pow(2,cptcoveff);\r
\r
/* 1eme*/\r
for (cpt=1; cpt<= nlstate ; cpt ++) {\r
for(k=1; k<=(nlstate+ndeath); k++) {\r
if (k != k2){\r
fprintf(ficgp," exp(p%d+p%d*x",i,i+1);\r
-\r
- for(j=3; j <=ncovmodel; j++) \r
+ij=1;\r
+ for(j=3; j <=ncovmodel; j++) {\r
+ if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) {\r
+ fprintf(ficgp,"+p%d*%d*x",i+j-1,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);\r
+ ij++;\r
+ }\r
+ else\r
fprintf(ficgp,"+p%d*%d",i+j-1,nbcode[Tvar[j-2]][codtab[jk][j-2]]);\r
- fprintf(ficgp,")/(1");\r
+ }\r
+ fprintf(ficgp,")/(1");\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
+ij=1;\r
+ for(j=3; j <=ncovmodel; j++){\r
+ if(((j-2)==Tage[ij]) &&(ij <=cptcovage)) {\r
+ fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][Tvar[j-2]]]);\r
+ ij++;\r
+ }\r
+ else\r
fprintf(ficgp,"+p%d*%d",k3+(k1-1)*ncovmodel+1+j-2,nbcode[Tvar[j-2]][codtab[jk][j-2]]);\r
+ }\r
fprintf(ficgp,")");\r
}\r
fprintf(ficgp,") t \"p%d%d\" ", k2,k);\r
/*free_matrix(covar,1,NCOVMAX,1,n);*/\r
fclose(ficparo);\r
fclose(ficres);\r
- }\r
+ /* }*/\r
\r
/*________fin mle=1_________*/\r
\r
\r
fprintf(fichtm," <li>Graphs</li>\n<p>");\r
\r
- m=cptcovn;\r
+ m=cptcoveff;\r
if (cptcovn < 1) {m=1;ncodemax[1]=1;}\r
\r
j1=0;\r
j1++;\r
if (cptcovn > 0) {\r
fprintf(fichtm,"<hr>************ Results for covariates");\r
- for (cpt=1; cpt<=cptcovn;cpt++) \r
- fprintf(fichtm," V%d=%d ",Tvar[cpt],nbcode[Tvar[cpt]][codtab[j1][cpt]]);\r
+ for (cpt=1; cpt<=cptcoveff;cpt++) \r
+ fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtab[j1][cpt]]);\r
fprintf(fichtm," ************\n<hr>");\r
}\r
fprintf(fichtm,"<br>- Probabilities: pe%s%d.gif<br>\r
agebase=agemin;\r
agelim=agemax;\r
ftolpl=1.e-10;\r
- i1=cptcovn;\r
+ i1=cptcoveff;\r
if (cptcovn < 1){i1=1;}\r
\r
for(cptcov=1;cptcov<=i1;cptcov++){\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
- for(j=1;j<=cptcovn;j++) \r
- fprintf(ficrespl," V%d=%d ",Tvar[j],nbcode[Tvar[j]][codtab[k][j]]);\r
+ for(j=1;j<=cptcoveff;j++) \r
+ fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);\r
fprintf(ficrespl,"******\n");\r
\r
for (age=agebase; age<=agelim; age++){\r
for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){\r
k=k+1;\r
fprintf(ficrespij,"\n#****** ");\r
- for(j=1;j<=cptcovn;j++) \r
- fprintf(ficrespij,"V%d=%d ",Tvar[j],nbcode[Tvar[j]][codtab[k][j]]);\r
+ for(j=1;j<=cptcoveff;j++) \r
+ fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);\r
fprintf(ficrespij,"******\n");\r
\r
for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */\r
for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){\r
k=k+1;\r
fprintf(ficrest,"\n#****** ");\r
- for(j=1;j<=cptcovn;j++) \r
- fprintf(ficrest,"V%d=%d ",Tvar[j],nbcode[Tvar[j]][codtab[k][j]]);\r
+ for(j=1;j<=cptcoveff;j++) \r
+ fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);\r
fprintf(ficrest,"******\n");\r
\r
fprintf(ficreseij,"\n#****** ");\r
- for(j=1;j<=cptcovn;j++) \r
+ for(j=1;j<=cptcoveff;j++) \r
fprintf(ficreseij,"V%d=%d ",j,nbcode[j][codtab[k][j]]);\r
fprintf(ficreseij,"******\n");\r
\r
fprintf(ficresvij,"\n#****** ");\r
- for(j=1;j<=cptcovn;j++) \r
+ for(j=1;j<=cptcoveff;j++) \r
fprintf(ficresvij,"V%d=%d ",j,nbcode[j][codtab[k][j]]);\r
fprintf(ficresvij,"******\n");\r
\r
for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){\r
k=k+1;\r
fprintf(ficresvpl,"\n#****** ");\r
- for(j=1;j<=cptcovn;j++) \r
- fprintf(ficresvpl,"V%d=%d ",Tvar[j],nbcode[Tvar[j]][codtab[k][j]]);\r
+ for(j=1;j<=cptcoveff;j++) \r
+ fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtab[k][j]]);\r
fprintf(ficresvpl,"******\n");\r
\r
varpl=matrix(1,nlstate,(int) bage, (int) fage);\r