version 1.1.1.1, 2000/12/28 18:49:56
|
version 1.2, 2001/03/13 18:10:26
|
Line 1
|
Line 1
|
|
|
/*********************** Imach ************************************** |
/*********************** Imach **************************************
|
This program computes Healthy Life Expectancies from cross-longitudinal |
This program computes Healthy Life Expectancies from cross-longitudinal
|
data. Cross-longitudinal consist in a first survey ("cross") where |
data. Cross-longitudinal consist in a first survey ("cross") where
|
individuals from different ages are interviewed on their health status |
individuals from different ages are interviewed on their health status
|
or degree of disability. At least a second wave of interviews |
or degree of disability. At least a second wave of interviews
|
("longitudinal") should measure each new individual health status. |
("longitudinal") should measure each new individual health status.
|
Health expectancies are computed from the transistions observed between |
Health expectancies are computed from the transistions observed between
|
waves and are computed for each degree of severity of disability (number |
waves and are computed for each degree of severity of disability (number
|
of life states). More degrees you consider, more time is necessary to |
of life states). More degrees you consider, more time is necessary to
|
reach the Maximum Likekilhood of the parameters involved in the model. |
reach the Maximum Likelihood of the parameters involved in the model.
|
The simplest model is the multinomial logistic model where pij is |
The simplest model is the multinomial logistic model where pij is
|
the probabibility to be observed in state j at the second wave conditional |
the probabibility to be observed in state j at the second wave conditional
|
to be observed in state i at the first wave. Therefore the model is: |
to be observed in state i at the first wave. Therefore the model is:
|
log(pij/pii)= aij + bij*age+ cij*sex + etc , where 'age' is age and 'sex' |
log(pij/pii)= aij + bij*age+ cij*sex + etc , where 'age' is age and 'sex'
|
is a covariate. If you want to have a more complex model than "constant and |
is a covariate. If you want to have a more complex model than "constant and
|
age", you should modify the program where the markup |
age", you should modify the program where the markup
|
*Covariates have to be included here again* invites you to do it. |
*Covariates have to be included here again* invites you to do it.
|
More covariates you add, less is the speed of the convergence. |
More covariates you add, less is the speed of the convergence.
|
|
|
The advantage that this computer programme claims, comes from that if the |
The advantage that this computer programme claims, comes from that if the
|
delay between waves is not identical for each individual, or if some |
delay between waves is not identical for each individual, or if some
|
individual missed an interview, the information is not rounded or lost, but |
individual missed an interview, the information is not rounded or lost, but
|
taken into account using an interpolation or extrapolation. |
taken into account using an interpolation or extrapolation.
|
hPijx is the probability to be |
hPijx is the probability to be
|
observed in state i at age x+h conditional to the observed state i at age |
observed in state i at age x+h conditional to the observed state i at age
|
x. The delay 'h' can be split into an exact number (nh*stepm) of |
x. The delay 'h' can be split into an exact number (nh*stepm) of
|
unobserved intermediate states. This elementary transition (by month or |
unobserved intermediate states. This elementary transition (by month or
|
quarter trimester, semester or year) is model as a multinomial logistic. |
quarter trimester, semester or year) is model as a multinomial logistic.
|
The hPx matrix is simply the matrix product of nh*stepm elementary matrices |
The hPx matrix is simply the matrix product of nh*stepm elementary matrices
|
and the contribution of each individual to the likelihood is simply hPijx. |
and the contribution of each individual to the likelihood is simply hPijx.
|
|
|
Also this programme outputs the covariance matrix of the parameters but also |
Also this programme outputs the covariance matrix of the parameters but also
|
of the life expectancies. It also computes the prevalence limits. |
of the life expectancies. It also computes the prevalence limits.
|
|
|
Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr). |
Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
|
Institut national d'études démographiques, Paris. |
Institut national d'études démographiques, Paris.
|
This software have been partly granted by Euro-REVES, a concerted action |
This software have been partly granted by Euro-REVES, a concerted action
|
from the European Union. |
from the European Union.
|
It is copyrighted identically to a GNU software product, ie programme and |
It is copyrighted identically to a GNU software product, ie programme and
|
software can be distributed freely for non commercial use. Latest version |
software can be distributed freely for non commercial use. Latest version
|
can be accessed at http://euroreves.ined.fr/imach . |
can be accessed at http://euroreves.ined.fr/imach .
|
**********************************************************************/ |
**********************************************************************/
|
|
|
#include <math.h> |
#include <math.h>
|
#include <stdio.h> |
#include <stdio.h>
|
#include <stdlib.h> |
#include <stdlib.h>
|
#include <unistd.h> |
#include <unistd.h>
|
|
|
#define MAXLINE 256 |
#define MAXLINE 256
|
#define FILENAMELENGTH 80 |
#define FILENAMELENGTH 80
|
/*#define DEBUG*/ |
/*#define DEBUG*/
|
/*#define win*/ |
#define windows
|
|
|
#define MAXPARM 30 /* Maximum number of parameters for the optimization */ |
#define MAXPARM 30 /* Maximum number of parameters for the optimization */
|
#define NPARMAX 64 /* (nlstate+ndeath-1)*nlstate*ncov */ |
#define NPARMAX 64 /* (nlstate+ndeath-1)*nlstate*ncovmodel */
|
|
|
#define NINTERVMAX 8 |
#define NINTERVMAX 8
|
#define NLSTATEMAX 8 /* Maximum number of live states (for func) */ |
#define NLSTATEMAX 8 /* Maximum number of live states (for func) */
|
#define NDEATHMAX 8 /* Maximum number of dead states (for func) */ |
#define NDEATHMAX 8 /* Maximum number of dead states (for func) */
|
#define NCOVMAX 8 /* Maximum number of covariates */ |
#define NCOVMAX 8 /* Maximum number of covariates */
|
#define MAXN 20000 |
#define MAXN 80000
|
#define YEARM 12. /* Number of months per year */ |
#define YEARM 12. /* Number of months per year */
|
#define AGESUP 130 |
#define AGESUP 130
|
#define AGEBASE 40 |
#define AGEBASE 40
|
|
|
|
|
int nvar; |
int nvar;
|
|
static int cptcov;
|
int npar=NPARMAX; |
int cptcovn;
|
int nlstate=2; /* Number of live states */ |
int npar=NPARMAX;
|
int ndeath=1; /* Number of dead states */ |
int nlstate=2; /* Number of live states */
|
int ncov; /* Total number of covariables including constant a12*1 +b12*x ncov=2 */ |
int ndeath=1; /* Number of dead states */
|
|
int ncovmodel, ncov; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
|
int *wav; /* Number of waves for this individuual 0 is possible */ |
|
int maxwav; /* Maxim number of waves */ |
int *wav; /* Number of waves for this individuual 0 is possible */
|
int mle, weightopt; |
int maxwav; /* Maxim number of waves */
|
int **mw; /* mw[mi][i] is number of the mi wave for this individual */ |
int mle, weightopt;
|
int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */ |
int **mw; /* mw[mi][i] is number of the mi wave for this individual */
|
double **oldm, **newm, **savm; /* Working pointers to matrices */ |
int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
|
double **oldms, **newms, **savms; /* Fixed working pointers to matrices */ |
double **oldm, **newm, **savm; /* Working pointers to matrices */
|
FILE *fic,*ficpar, *ficparo,*ficres, *ficrespl, *ficrespij, *ficrest; |
double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
|
FILE *ficgp, *fichtm; |
FILE *fic,*ficpar, *ficparo,*ficres, *ficrespl, *ficrespij, *ficrest;
|
|
FILE *ficgp, *fichtm;
|
#define NR_END 1 |
FILE *ficreseij;
|
#define FREE_ARG char* |
char filerese[FILENAMELENGTH];
|
#define FTOL 1.0e-10 |
FILE *ficresvij;
|
|
char fileresv[FILENAMELENGTH];
|
#define NRANSI |
FILE *ficresvpl;
|
#define ITMAX 200 |
char fileresvpl[FILENAMELENGTH];
|
|
|
#define TOL 2.0e-4 |
|
|
|
#define CGOLD 0.3819660 |
|
#define ZEPS 1.0e-10 |
#define NR_END 1
|
#define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d); |
#define FREE_ARG char*
|
|
#define FTOL 1.0e-10
|
#define GOLD 1.618034 |
|
#define GLIMIT 100.0 |
#define NRANSI
|
#define TINY 1.0e-20 |
#define ITMAX 200
|
|
|
static double maxarg1,maxarg2; |
#define TOL 2.0e-4
|
#define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2)) |
|
#define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2)) |
#define CGOLD 0.3819660
|
|
#define ZEPS 1.0e-10
|
#define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a)) |
#define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
|
#define rint(a) floor(a+0.5) |
|
|
#define GOLD 1.618034
|
static double sqrarg; |
#define GLIMIT 100.0
|
#define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg) |
#define TINY 1.0e-20
|
#define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;} |
|
|
static double maxarg1,maxarg2;
|
int imx; |
#define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
|
int stepm; |
#define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
|
/* Stepm, step in month: minimum step interpolation*/ |
|
|
#define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
|
int m,nb; |
#define rint(a) floor(a+0.5)
|
int *num, firstpass=0, lastpass=2,*cod; |
|
double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint; |
static double sqrarg;
|
double **pmmij; |
#define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
|
|
#define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
|
double *weight; |
|
int **s; /* Status */ |
int imx;
|
double *agedc, **covar, idx; |
int stepm;
|
|
/* Stepm, step in month: minimum step interpolation*/
|
|
|
double ftol=FTOL; /* Tolerance for computing Max Likelihood */ |
int m,nb;
|
double ftolhess; /* Tolerance for computing hessian */ |
int *num, firstpass=0, lastpass=4,*cod, *ncodemax;
|
|
double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
|
|
double **pmmij;
|
/******************************************/ |
|
|
double *weight;
|
void replace(char *s, char*t) |
int **s; /* Status */
|
{ |
double *agedc, **covar, idx;
|
int i; |
int **nbcode, *Tcode, *Tvar, **codtab;
|
int lg=20; |
|
i=0; |
double ftol=FTOL; /* Tolerance for computing Max Likelihood */
|
lg=strlen(t); |
double ftolhess; /* Tolerance for computing hessian */
|
for(i=0; i<= lg; i++) { |
|
(s[i] = t[i]); |
|
if (t[i]== '\\') s[i]='/'; |
/******************************************/
|
} |
|
} |
void replace(char *s, char*t)
|
void cut(char *u,char *v, char*t) |
{
|
{ |
int i;
|
int i,lg,j,p; |
int lg=20;
|
i=0; |
i=0;
|
for(j=0; j<=strlen(t); j++) { |
lg=strlen(t);
|
if(t[j]=='\\') p=j; |
for(i=0; i<= lg; i++) {
|
} |
(s[i] = t[i]);
|
|
if (t[i]== '\\') s[i]='/';
|
lg=strlen(t); |
}
|
for(j=0; j<p; j++) { |
}
|
(u[j] = t[j]); |
|
u[p]='\0'; |
int nbocc(char *s, char occ)
|
} |
{
|
|
int i,j=0;
|
for(j=0; j<= lg; j++) { |
int lg=20;
|
if (j>=(p+1))(v[j-p-1] = t[j]); |
i=0;
|
} |
lg=strlen(s);
|
} |
for(i=0; i<= lg; i++) {
|
|
if (s[i] == occ ) j++;
|
/********************** nrerror ********************/ |
}
|
|
return j;
|
void nrerror(char error_text[]) |
}
|
{ |
|
fprintf(stderr,"ERREUR ...\n"); |
void cutv(char *u,char *v, char*t, char occ)
|
fprintf(stderr,"%s\n",error_text); |
{
|
exit(1); |
int i,lg,j,p;
|
} |
i=0;
|
/*********************** vector *******************/ |
if (t[0]== occ) p=0;
|
double *vector(int nl, int nh) |
for(j=0; j<=strlen(t)-1; j++) {
|
{ |
if((t[j]!= occ) && (t[j+1]==occ)) p=j+1;
|
double *v; |
}
|
v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double))); |
|
if (!v) nrerror("allocation failure in vector"); |
lg=strlen(t);
|
return v-nl+NR_END; |
for(j=0; j<p; j++) {
|
} |
(u[j] = t[j]);
|
|
u[p]='\0';
|
/************************ free vector ******************/ |
}
|
void free_vector(double*v, int nl, int nh) |
|
{ |
for(j=0; j<= lg; j++) {
|
free((FREE_ARG)(v+nl-NR_END)); |
if (j>=(p+1))(v[j-p-1] = t[j]);
|
} |
}
|
|
}
|
/************************ivector *******************************/ |
|
int *ivector(long nl,long nh) |
|
{ |
/********************** nrerror ********************/
|
int *v; |
|
v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int))); |
void nrerror(char error_text[])
|
if (!v) nrerror("allocation failure in ivector"); |
{
|
return v-nl+NR_END; |
fprintf(stderr,"ERREUR ...\n");
|
} |
fprintf(stderr,"%s\n",error_text);
|
|
exit(1);
|
/******************free ivector **************************/ |
}
|
void free_ivector(int *v, long nl, long nh) |
/*********************** vector *******************/
|
{ |
double *vector(int nl, int nh)
|
free((FREE_ARG)(v+nl-NR_END)); |
{
|
} |
double *v;
|
|
v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
|
/******************* imatrix *******************************/ |
if (!v) nrerror("allocation failure in vector");
|
int **imatrix(long nrl, long nrh, long ncl, long nch) |
return v-nl+NR_END;
|
/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */ |
}
|
{ |
|
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1; |
/************************ free vector ******************/
|
int **m; |
void free_vector(double*v, int nl, int nh)
|
|
{
|
/* allocate pointers to rows */ |
free((FREE_ARG)(v+nl-NR_END));
|
m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*))); |
}
|
if (!m) nrerror("allocation failure 1 in matrix()"); |
|
m += NR_END; |
/************************ivector *******************************/
|
m -= nrl; |
int *ivector(long nl,long nh)
|
|
{
|
|
int *v;
|
/* allocate rows and set pointers to them */ |
v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
|
m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int))); |
if (!v) nrerror("allocation failure in ivector");
|
if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); |
return v-nl+NR_END;
|
m[nrl] += NR_END; |
}
|
m[nrl] -= ncl; |
|
|
/******************free ivector **************************/
|
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol; |
void free_ivector(int *v, long nl, long nh)
|
|
{
|
/* return pointer to array of pointers to rows */ |
free((FREE_ARG)(v+nl-NR_END));
|
return m; |
}
|
} |
|
|
/******************* imatrix *******************************/
|
/****************** free_imatrix *************************/ |
int **imatrix(long nrl, long nrh, long ncl, long nch)
|
void free_imatrix(m,nrl,nrh,ncl,nch) |
/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
|
int **m; |
{
|
long nch,ncl,nrh,nrl; |
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
|
/* free an int matrix allocated by imatrix() */ |
int **m;
|
{ |
|
free((FREE_ARG) (m[nrl]+ncl-NR_END)); |
/* allocate pointers to rows */
|
free((FREE_ARG) (m+nrl-NR_END)); |
m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
|
} |
if (!m) nrerror("allocation failure 1 in matrix()");
|
|
m += NR_END;
|
/******************* matrix *******************************/ |
m -= nrl;
|
double **matrix(long nrl, long nrh, long ncl, long nch) |
|
{ |
|
long i, nrow=nrh-nrl+1, ncol=nch-ncl+1; |
/* allocate rows and set pointers to them */
|
double **m; |
m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
|
|
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
|
m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*))); |
m[nrl] += NR_END;
|
if (!m) nrerror("allocation failure 1 in matrix()"); |
m[nrl] -= ncl;
|
m += NR_END; |
|
m -= nrl; |
for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
|
|
|
m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double))); |
/* return pointer to array of pointers to rows */
|
if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); |
return m;
|
m[nrl] += NR_END; |
}
|
m[nrl] -= ncl; |
|
|
/****************** free_imatrix *************************/
|
for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol; |
void free_imatrix(m,nrl,nrh,ncl,nch)
|
return m; |
int **m;
|
} |
long nch,ncl,nrh,nrl;
|
|
/* free an int matrix allocated by imatrix() */
|
/*************************free matrix ************************/ |
{
|
void free_matrix(double **m, long nrl, long nrh, long ncl, long nch) |
free((FREE_ARG) (m[nrl]+ncl-NR_END));
|
{ |
free((FREE_ARG) (m+nrl-NR_END));
|
free((FREE_ARG)(m[nrl]+ncl-NR_END)); |
}
|
free((FREE_ARG)(m+nrl-NR_END)); |
|
} |
/******************* matrix *******************************/
|
|
double **matrix(long nrl, long nrh, long ncl, long nch)
|
/******************* ma3x *******************************/ |
{
|
double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh) |
long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
|
{ |
double **m;
|
long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1; |
|
double ***m; |
m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
|
|
if (!m) nrerror("allocation failure 1 in matrix()");
|
m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*))); |
m += NR_END;
|
if (!m) nrerror("allocation failure 1 in matrix()"); |
m -= nrl;
|
m += NR_END; |
|
m -= nrl; |
m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
|
|
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
|
m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double))); |
m[nrl] += NR_END;
|
if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); |
m[nrl] -= ncl;
|
m[nrl] += NR_END; |
|
m[nrl] -= ncl; |
for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
|
|
return m;
|
for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol; |
}
|
|
|
m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double))); |
/*************************free matrix ************************/
|
if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()"); |
void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
|
m[nrl][ncl] += NR_END; |
{
|
m[nrl][ncl] -= nll; |
free((FREE_ARG)(m[nrl]+ncl-NR_END));
|
for (j=ncl+1; j<=nch; j++) |
free((FREE_ARG)(m+nrl-NR_END));
|
m[nrl][j]=m[nrl][j-1]+nlay; |
}
|
|
|
for (i=nrl+1; i<=nrh; i++) { |
/******************* ma3x *******************************/
|
m[i][ncl]=m[i-1l][ncl]+ncol*nlay; |
double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
|
for (j=ncl+1; j<=nch; j++) |
{
|
m[i][j]=m[i][j-1]+nlay; |
long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
|
} |
double ***m;
|
return m; |
|
} |
m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
|
|
if (!m) nrerror("allocation failure 1 in matrix()");
|
/*************************free ma3x ************************/ |
m += NR_END;
|
void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh) |
m -= nrl;
|
{ |
|
free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END)); |
m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
|
free((FREE_ARG)(m[nrl]+ncl-NR_END)); |
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
|
free((FREE_ARG)(m+nrl-NR_END)); |
m[nrl] += NR_END;
|
} |
m[nrl] -= ncl;
|
|
|
/***************** f1dim *************************/ |
for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
|
extern int ncom; |
|
extern double *pcom,*xicom; |
m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
|
extern double (*nrfunc)(double []); |
if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
|
|
m[nrl][ncl] += NR_END;
|
double f1dim(double x) |
m[nrl][ncl] -= nll;
|
{ |
for (j=ncl+1; j<=nch; j++)
|
int j; |
m[nrl][j]=m[nrl][j-1]+nlay;
|
double f; |
|
double *xt; |
for (i=nrl+1; i<=nrh; i++) {
|
|
m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
|
xt=vector(1,ncom); |
for (j=ncl+1; j<=nch; j++)
|
for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j]; |
m[i][j]=m[i][j-1]+nlay;
|
f=(*nrfunc)(xt); |
}
|
free_vector(xt,1,ncom); |
return m;
|
return f; |
}
|
} |
|
|
/*************************free ma3x ************************/
|
/*****************brent *************************/ |
void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
|
double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin) |
{
|
{ |
free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
|
int iter; |
free((FREE_ARG)(m[nrl]+ncl-NR_END));
|
double a,b,d,etemp; |
free((FREE_ARG)(m+nrl-NR_END));
|
double fu,fv,fw,fx; |
}
|
double ftemp; |
|
double p,q,r,tol1,tol2,u,v,w,x,xm; |
/***************** f1dim *************************/
|
double e=0.0; |
extern int ncom;
|
|
extern double *pcom,*xicom;
|
a=(ax < cx ? ax : cx); |
extern double (*nrfunc)(double []);
|
b=(ax > cx ? ax : cx); |
|
x=w=v=bx; |
double f1dim(double x)
|
fw=fv=fx=(*f)(x); |
{
|
for (iter=1;iter<=ITMAX;iter++) { |
int j;
|
xm=0.5*(a+b); |
double f;
|
tol2=2.0*(tol1=tol*fabs(x)+ZEPS); |
double *xt;
|
/* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/ |
|
printf(".");fflush(stdout); |
xt=vector(1,ncom);
|
#ifdef DEBUG |
for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
|
printf("br %d,x=%.10e xm=%.10e b=%.10e a=%.10e tol=%.10e tol1=%.10e tol2=%.10e x-xm=%.10e fx=%.12e fu=%.12e,fw=%.12e,ftemp=%.12e,ftol=%.12e\n",iter,x,xm,b,a,tol,tol1,tol2,(x-xm),fx,fu,fw,ftemp,ftol); |
f=(*nrfunc)(xt);
|
/* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */ |
free_vector(xt,1,ncom);
|
#endif |
return f;
|
if (fabs(x-xm) <= (tol2-0.5*(b-a))){ |
}
|
*xmin=x; |
|
return fx; |
/*****************brent *************************/
|
} |
double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
|
ftemp=fu; |
{
|
if (fabs(e) > tol1) { |
int iter;
|
r=(x-w)*(fx-fv); |
double a,b,d,etemp;
|
q=(x-v)*(fx-fw); |
double fu,fv,fw,fx;
|
p=(x-v)*q-(x-w)*r; |
double ftemp;
|
q=2.0*(q-r); |
double p,q,r,tol1,tol2,u,v,w,x,xm;
|
if (q > 0.0) p = -p; |
double e=0.0;
|
q=fabs(q); |
|
etemp=e; |
a=(ax < cx ? ax : cx);
|
e=d; |
b=(ax > cx ? ax : cx);
|
if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) |
x=w=v=bx;
|
d=CGOLD*(e=(x >= xm ? a-x : b-x)); |
fw=fv=fx=(*f)(x);
|
else { |
for (iter=1;iter<=ITMAX;iter++) {
|
d=p/q; |
xm=0.5*(a+b);
|
u=x+d; |
tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
|
if (u-a < tol2 || b-u < tol2) |
/* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
|
d=SIGN(tol1,xm-x); |
printf(".");fflush(stdout);
|
} |
#ifdef DEBUG
|
} else { |
printf("br %d,x=%.10e xm=%.10e b=%.10e a=%.10e tol=%.10e tol1=%.10e tol2=%.10e x-xm=%.10e fx=%.12e fu=%.12e,fw=%.12e,ftemp=%.12e,ftol=%.12e\n",iter,x,xm,b,a,tol,tol1,tol2,(x-xm),fx,fu,fw,ftemp,ftol);
|
d=CGOLD*(e=(x >= xm ? a-x : b-x)); |
/* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
|
} |
#endif
|
u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d)); |
if (fabs(x-xm) <= (tol2-0.5*(b-a))){
|
fu=(*f)(u); |
*xmin=x;
|
if (fu <= fx) { |
return fx;
|
if (u >= x) a=x; else b=x; |
}
|
SHFT(v,w,x,u) |
ftemp=fu;
|
SHFT(fv,fw,fx,fu) |
if (fabs(e) > tol1) {
|
} else { |
r=(x-w)*(fx-fv);
|
if (u < x) a=u; else b=u; |
q=(x-v)*(fx-fw);
|
if (fu <= fw || w == x) { |
p=(x-v)*q-(x-w)*r;
|
v=w; |
q=2.0*(q-r);
|
w=u; |
if (q > 0.0) p = -p;
|
fv=fw; |
q=fabs(q);
|
fw=fu; |
etemp=e;
|
} else if (fu <= fv || v == x || v == w) { |
e=d;
|
v=u; |
if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
|
fv=fu; |
d=CGOLD*(e=(x >= xm ? a-x : b-x));
|
} |
else {
|
} |
d=p/q;
|
} |
u=x+d;
|
nrerror("Too many iterations in brent"); |
if (u-a < tol2 || b-u < tol2)
|
*xmin=x; |
d=SIGN(tol1,xm-x);
|
return fx; |
}
|
} |
} else {
|
|
d=CGOLD*(e=(x >= xm ? a-x : b-x));
|
/****************** mnbrak ***********************/ |
}
|
|
u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
|
void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc, |
fu=(*f)(u);
|
double (*func)(double)) |
if (fu <= fx) {
|
{ |
if (u >= x) a=x; else b=x;
|
double ulim,u,r,q, dum; |
SHFT(v,w,x,u)
|
double fu; |
SHFT(fv,fw,fx,fu)
|
|
} else {
|
*fa=(*func)(*ax); |
if (u < x) a=u; else b=u;
|
*fb=(*func)(*bx); |
if (fu <= fw || w == x) {
|
if (*fb > *fa) { |
v=w;
|
SHFT(dum,*ax,*bx,dum) |
w=u;
|
SHFT(dum,*fb,*fa,dum) |
fv=fw;
|
} |
fw=fu;
|
*cx=(*bx)+GOLD*(*bx-*ax); |
} else if (fu <= fv || v == x || v == w) {
|
*fc=(*func)(*cx); |
v=u;
|
while (*fb > *fc) { |
fv=fu;
|
r=(*bx-*ax)*(*fb-*fc); |
}
|
q=(*bx-*cx)*(*fb-*fa); |
}
|
u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/ |
}
|
(2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); |
nrerror("Too many iterations in brent");
|
ulim=(*bx)+GLIMIT*(*cx-*bx); |
*xmin=x;
|
if ((*bx-u)*(u-*cx) > 0.0) { |
return fx;
|
fu=(*func)(u); |
}
|
} else if ((*cx-u)*(u-ulim) > 0.0) { |
|
fu=(*func)(u); |
/****************** mnbrak ***********************/
|
if (fu < *fc) { |
|
SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx)) |
void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
|
SHFT(*fb,*fc,fu,(*func)(u)) |
double (*func)(double))
|
} |
{
|
} else if ((u-ulim)*(ulim-*cx) >= 0.0) { |
double ulim,u,r,q, dum;
|
u=ulim; |
double fu;
|
fu=(*func)(u); |
|
} else { |
*fa=(*func)(*ax);
|
u=(*cx)+GOLD*(*cx-*bx); |
*fb=(*func)(*bx);
|
fu=(*func)(u); |
if (*fb > *fa) {
|
} |
SHFT(dum,*ax,*bx,dum)
|
SHFT(*ax,*bx,*cx,u) |
SHFT(dum,*fb,*fa,dum)
|
SHFT(*fa,*fb,*fc,fu) |
}
|
} |
*cx=(*bx)+GOLD*(*bx-*ax);
|
} |
*fc=(*func)(*cx);
|
|
while (*fb > *fc) {
|
/*************** linmin ************************/ |
r=(*bx-*ax)*(*fb-*fc);
|
|
q=(*bx-*cx)*(*fb-*fa);
|
int ncom; |
u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
|
double *pcom,*xicom; |
(2.0*SIGN(FMAX(fabs(q-r),TINY),q-r));
|
double (*nrfunc)(double []); |
ulim=(*bx)+GLIMIT*(*cx-*bx);
|
|
if ((*bx-u)*(u-*cx) > 0.0) {
|
void linmin(double p[], double xi[], int n, double *fret,double (*func)(double [])) |
fu=(*func)(u);
|
{ |
} else if ((*cx-u)*(u-ulim) > 0.0) {
|
double brent(double ax, double bx, double cx, |
fu=(*func)(u);
|
double (*f)(double), double tol, double *xmin); |
if (fu < *fc) {
|
double f1dim(double x); |
SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
|
void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, |
SHFT(*fb,*fc,fu,(*func)(u))
|
double *fc, double (*func)(double)); |
}
|
int j; |
} else if ((u-ulim)*(ulim-*cx) >= 0.0) {
|
double xx,xmin,bx,ax; |
u=ulim;
|
double fx,fb,fa; |
fu=(*func)(u);
|
|
} else {
|
ncom=n; |
u=(*cx)+GOLD*(*cx-*bx);
|
pcom=vector(1,n); |
fu=(*func)(u);
|
xicom=vector(1,n); |
}
|
nrfunc=func; |
SHFT(*ax,*bx,*cx,u)
|
for (j=1;j<=n;j++) { |
SHFT(*fa,*fb,*fc,fu)
|
pcom[j]=p[j]; |
}
|
xicom[j]=xi[j]; |
}
|
} |
|
ax=0.0; |
/*************** linmin ************************/
|
xx=1.0; |
|
mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); |
int ncom;
|
*fret=brent(ax,xx,bx,f1dim,TOL,&xmin); |
double *pcom,*xicom;
|
#ifdef DEBUG |
double (*nrfunc)(double []);
|
printf("retour brent fret=%.12e xmin=%.12e\n",*fret,xmin); |
|
#endif |
void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
|
for (j=1;j<=n;j++) { |
{
|
xi[j] *= xmin; |
double brent(double ax, double bx, double cx,
|
p[j] += xi[j]; |
double (*f)(double), double tol, double *xmin);
|
} |
double f1dim(double x);
|
free_vector(xicom,1,n); |
void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
|
free_vector(pcom,1,n); |
double *fc, double (*func)(double));
|
} |
int j;
|
|
double xx,xmin,bx,ax;
|
/*************** powell ************************/ |
double fx,fb,fa;
|
void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret, |
|
double (*func)(double [])) |
ncom=n;
|
|
pcom=vector(1,n);
|
{ |
xicom=vector(1,n);
|
|
nrfunc=func;
|
|
for (j=1;j<=n;j++) {
|
void linmin(double p[], double xi[], int n, double *fret, |
pcom[j]=p[j];
|
double (*func)(double [])); |
xicom[j]=xi[j];
|
int i,ibig,j; |
}
|
double del,t,*pt,*ptt,*xit; |
ax=0.0;
|
double fp,fptt; |
xx=1.0;
|
double *xits; |
mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim);
|
pt=vector(1,n); |
*fret=brent(ax,xx,bx,f1dim,TOL,&xmin);
|
ptt=vector(1,n); |
#ifdef DEBUG
|
xit=vector(1,n); |
printf("retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
|
xits=vector(1,n); |
#endif
|
*fret=(*func)(p); |
for (j=1;j<=n;j++) {
|
for (j=1;j<=n;j++) pt[j]=p[j]; |
xi[j] *= xmin;
|
for (*iter=1;;++(*iter)) { |
p[j] += xi[j];
|
fp=(*fret); |
}
|
ibig=0; |
free_vector(xicom,1,n);
|
del=0.0; |
free_vector(pcom,1,n);
|
printf("\nPowell iter=%d -2*LL=%.12f",*iter,*fret); |
}
|
for (i=1;i<=n;i++) |
|
printf(" %d %.12f",i, p[i]); |
/*************** powell ************************/
|
printf("\n"); |
void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
|
for (i=1;i<=n;i++) { |
double (*func)(double []))
|
for (j=1;j<=n;j++) xit[j]=xi[j][i]; |
{
|
fptt=(*fret); |
void linmin(double p[], double xi[], int n, double *fret,
|
#ifdef DEBUG |
double (*func)(double []));
|
printf("fret=%lf \n",*fret); |
int i,ibig,j;
|
#endif |
double del,t,*pt,*ptt,*xit;
|
printf("%d",i);fflush(stdout); |
double fp,fptt;
|
linmin(p,xit,n,fret,func); |
double *xits;
|
if (fabs(fptt-(*fret)) > del) { |
pt=vector(1,n);
|
del=fabs(fptt-(*fret)); |
ptt=vector(1,n);
|
ibig=i; |
xit=vector(1,n);
|
} |
xits=vector(1,n);
|
#ifdef DEBUG |
*fret=(*func)(p);
|
printf("%d %.12e",i,(*fret)); |
for (j=1;j<=n;j++) pt[j]=p[j];
|
for (j=1;j<=n;j++) { |
for (*iter=1;;++(*iter)) {
|
xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5); |
fp=(*fret);
|
printf(" x(%d)=%.12e",j,xit[j]); |
ibig=0;
|
} |
del=0.0;
|
for(j=1;j<=n;j++) |
printf("\nPowell iter=%d -2*LL=%.12f",*iter,*fret);
|
printf(" p=%.12e",p[j]); |
for (i=1;i<=n;i++)
|
printf("\n"); |
printf(" %d %.12f",i, p[i]);
|
#endif |
printf("\n");
|
} |
for (i=1;i<=n;i++) {
|
if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { |
for (j=1;j<=n;j++) xit[j]=xi[j][i];
|
#ifdef DEBUG |
fptt=(*fret);
|
int k[2],l; |
#ifdef DEBUG
|
k[0]=1; |
printf("fret=%lf \n",*fret);
|
k[1]=-1; |
#endif
|
printf("Max: %.12e",(*func)(p)); |
printf("%d",i);fflush(stdout);
|
for (j=1;j<=n;j++) |
linmin(p,xit,n,fret,func);
|
printf(" %.12e",p[j]); |
if (fabs(fptt-(*fret)) > del) {
|
printf("\n"); |
del=fabs(fptt-(*fret));
|
for(l=0;l<=1;l++) { |
ibig=i;
|
for (j=1;j<=n;j++) { |
}
|
ptt[j]=p[j]+(p[j]-pt[j])*k[l]; |
#ifdef DEBUG
|
printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]); |
printf("%d %.12e",i,(*fret));
|
} |
for (j=1;j<=n;j++) {
|
printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p))); |
xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
|
} |
printf(" x(%d)=%.12e",j,xit[j]);
|
#endif |
}
|
|
for(j=1;j<=n;j++)
|
|
printf(" p=%.12e",p[j]);
|
free_vector(xit,1,n); |
printf("\n");
|
free_vector(xits,1,n); |
#endif
|
free_vector(ptt,1,n); |
}
|
free_vector(pt,1,n); |
if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) {
|
return; |
#ifdef DEBUG
|
} |
int k[2],l;
|
if (*iter == ITMAX) nrerror("powell exceeding maximum iterations."); |
k[0]=1;
|
for (j=1;j<=n;j++) { |
k[1]=-1;
|
ptt[j]=2.0*p[j]-pt[j]; |
printf("Max: %.12e",(*func)(p));
|
xit[j]=p[j]-pt[j]; |
for (j=1;j<=n;j++)
|
pt[j]=p[j]; |
printf(" %.12e",p[j]);
|
} |
printf("\n");
|
fptt=(*func)(ptt); |
for(l=0;l<=1;l++) {
|
if (fptt < fp) { |
for (j=1;j<=n;j++) {
|
t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); |
ptt[j]=p[j]+(p[j]-pt[j])*k[l];
|
if (t < 0.0) { |
printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
|
linmin(p,xit,n,fret,func); |
}
|
for (j=1;j<=n;j++) { |
printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
|
xi[j][ibig]=xi[j][n]; |
}
|
xi[j][n]=xit[j]; |
#endif
|
} |
|
#ifdef DEBUG |
|
printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig); |
free_vector(xit,1,n);
|
for(j=1;j<=n;j++) |
free_vector(xits,1,n);
|
printf(" %.12e",xit[j]); |
free_vector(ptt,1,n);
|
printf("\n"); |
free_vector(pt,1,n);
|
#endif |
return;
|
} |
}
|
} |
if (*iter == ITMAX) nrerror("powell exceeding maximum iterations.");
|
} |
for (j=1;j<=n;j++) {
|
} |
ptt[j]=2.0*p[j]-pt[j];
|
|
xit[j]=p[j]-pt[j];
|
/**** Prevalence limit ****************/ |
pt[j]=p[j];
|
|
}
|
double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl) |
fptt=(*func)(ptt);
|
{ |
if (fptt < fp) {
|
/* Computes the prevalence limit in each live state at age x by left multiplying the unit |
t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt);
|
matrix by transitions matrix until convergence is reached */ |
if (t < 0.0) {
|
|
linmin(p,xit,n,fret,func);
|
int i, ii,j,k; |
for (j=1;j<=n;j++) {
|
double min, max, maxmin, maxmax,sumnew=0.; |
xi[j][ibig]=xi[j][n];
|
double **matprod2(); |
xi[j][n]=xit[j];
|
double **out, cov[NCOVMAX], **pmij(); |
}
|
double **newm; |
#ifdef DEBUG
|
double agefin, delaymax=50 ; /* Max number of years to converge */ |
printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
|
|
for(j=1;j<=n;j++)
|
for (ii=1;ii<=nlstate+ndeath;ii++) |
printf(" %.12e",xit[j]);
|
for (j=1;j<=nlstate+ndeath;j++){ |
printf("\n");
|
oldm[ii][j]=(ii==j ? 1.0 : 0.0); |
#endif
|
} |
}
|
/* Even if hstepm = 1, at least one multiplication by the unit matrix */ |
}
|
for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){ |
}
|
newm=savm; |
}
|
/* Covariates have to be included here again */ |
|
cov[1]=1.; |
/**** Prevalence limit ****************/
|
cov[2]=agefin; |
|
out=matprod2(newm, pmij(pmmij,cov,ncov,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); |
double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int ij)
|
/* printf("age=%f agefin=%f po=%f pn=%f\n",age,agefin,oldm[1][1],newm[1][1]);*/ |
{
|
|
/* Computes the prevalence limit in each live state at age x by left multiplying the unit
|
savm=oldm; |
matrix by transitions matrix until convergence is reached */
|
oldm=newm; |
|
maxmax=0.; |
int i, ii,j,k;
|
for(j=1;j<=nlstate;j++){ |
double min, max, maxmin, maxmax,sumnew=0.;
|
min=1.; |
double **matprod2();
|
max=0.; |
double **out, cov[NCOVMAX], **pmij();
|
for(i=1; i<=nlstate; i++) { |
double **newm;
|
sumnew=0; |
double agefin, delaymax=50 ; /* Max number of years to converge */
|
for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k]; |
|
prlim[i][j]= newm[i][j]/(1-sumnew); |
for (ii=1;ii<=nlstate+ndeath;ii++)
|
max=FMAX(max,prlim[i][j]); |
for (j=1;j<=nlstate+ndeath;j++){
|
min=FMIN(min,prlim[i][j]); |
oldm[ii][j]=(ii==j ? 1.0 : 0.0);
|
} |
}
|
maxmin=max-min; |
/* Even if hstepm = 1, at least one multiplication by the unit matrix */
|
maxmax=FMAX(maxmax,maxmin); |
for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
|
} |
newm=savm;
|
if(maxmax < ftolpl){ |
/* Covariates have to be included here again */
|
return prlim; |
cov[1]=1.;
|
} |
cov[2]=agefin;
|
} |
if (cptcovn>0){
|
} |
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]]);*/}
|
|
}
|
/*************** transition probabilities **********/ |
out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
|
|
|
double **pmij(double **ps, double *cov, int ncov, double *x, int nlstate ) |
savm=oldm;
|
{ |
oldm=newm;
|
double s1, s2; |
maxmax=0.;
|
/*double t34;*/ |
for(j=1;j<=nlstate;j++){
|
int i,j,j1, nc, ii, jj; |
min=1.;
|
|
max=0.;
|
for(i=1; i<= nlstate; i++){ |
for(i=1; i<=nlstate; i++) {
|
for(j=1; j<i;j++){ |
sumnew=0;
|
for (nc=1, s2=0.;nc <=ncov; nc++){ |
for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
|
/*s2 += param[i][j][nc]*cov[nc];*/ |
prlim[i][j]= newm[i][j]/(1-sumnew);
|
s2 += x[(i-1)*nlstate*ncov+(j-1)*ncov+nc+(i-1)*(ndeath-1)*ncov]*cov[nc]; |
max=FMAX(max,prlim[i][j]);
|
/*printf("Int j<i s1=%.17e, s2=%.17e\n",s1,s2);*/ |
min=FMIN(min,prlim[i][j]);
|
} |
}
|
ps[i][j]=s2; |
maxmin=max-min;
|
/*printf("s1=%.17e, s2=%.17e\n",s1,s2);*/ |
maxmax=FMAX(maxmax,maxmin);
|
} |
}
|
for(j=i+1; j<=nlstate+ndeath;j++){ |
if(maxmax < ftolpl){
|
for (nc=1, s2=0.;nc <=ncov; nc++){ |
return prlim;
|
s2 += x[(i-1)*nlstate*ncov+(j-2)*ncov+nc+(i-1)*(ndeath-1)*ncov]*cov[nc]; |
}
|
/*printf("Int j>i s1=%.17e, s2=%.17e %lx %lx\n",s1,s2,s1,s2);*/ |
}
|
} |
}
|
ps[i][j]=s2; |
|
} |
/*************** transition probabilities **********/
|
} |
|
for(i=1; i<= nlstate; i++){ |
double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
|
s1=0; |
{
|
for(j=1; j<i; j++) |
double s1, s2;
|
s1+=exp(ps[i][j]); |
/*double t34;*/
|
for(j=i+1; j<=nlstate+ndeath; j++) |
int i,j,j1, nc, ii, jj;
|
s1+=exp(ps[i][j]); |
|
ps[i][i]=1./(s1+1.); |
for(i=1; i<= nlstate; i++){
|
for(j=1; j<i; j++) |
for(j=1; j<i;j++){
|
ps[i][j]= exp(ps[i][j])*ps[i][i]; |
for (nc=1, s2=0.;nc <=ncovmodel; nc++){
|
for(j=i+1; j<=nlstate+ndeath; j++) |
/*s2 += param[i][j][nc]*cov[nc];*/
|
ps[i][j]= exp(ps[i][j])*ps[i][i]; |
s2 += x[(i-1)*nlstate*ncovmodel+(j-1)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];
|
/* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */ |
/*printf("Int j<i s1=%.17e, s2=%.17e\n",s1,s2);*/
|
} /* end i */ |
}
|
|
ps[i][j]=s2;
|
for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){ |
/*printf("s1=%.17e, s2=%.17e\n",s1,s2);*/
|
for(jj=1; jj<= nlstate+ndeath; jj++){ |
}
|
ps[ii][jj]=0; |
for(j=i+1; j<=nlstate+ndeath;j++){
|
ps[ii][ii]=1; |
for (nc=1, s2=0.;nc <=ncovmodel; nc++){
|
} |
s2 += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];
|
} |
/*printf("Int j>i s1=%.17e, s2=%.17e %lx %lx\n",s1,s2,s1,s2);*/
|
|
}
|
/* for(ii=1; ii<= nlstate+ndeath; ii++){ |
ps[i][j]=s2;
|
for(jj=1; jj<= nlstate+ndeath; jj++){ |
}
|
printf("%lf ",ps[ii][jj]); |
}
|
} |
for(i=1; i<= nlstate; i++){
|
printf("\n "); |
s1=0;
|
} |
for(j=1; j<i; j++)
|
printf("\n ");printf("%lf ",cov[2]);*/ |
s1+=exp(ps[i][j]);
|
/* |
for(j=i+1; j<=nlstate+ndeath; j++)
|
for(i=1; i<= npar; i++) printf("%f ",x[i]); |
s1+=exp(ps[i][j]);
|
goto end;*/ |
ps[i][i]=1./(s1+1.);
|
return ps; |
for(j=1; j<i; j++)
|
} |
ps[i][j]= exp(ps[i][j])*ps[i][i];
|
|
for(j=i+1; j<=nlstate+ndeath; j++)
|
/**************** Product of 2 matrices ******************/ |
ps[i][j]= exp(ps[i][j])*ps[i][i];
|
|
/* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
|
double **matprod2(double **out, double **in,long nrl, long nrh, long ncl, long nch, long ncolol, long ncoloh, double **b) |
} /* end i */
|
{ |
|
/* Computes the matric product of in(1,nrh-nrl+1)(1,nch-ncl+1) times |
for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
|
b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */ |
for(jj=1; jj<= nlstate+ndeath; jj++){
|
/* in, b, out are matrice of pointers which should have been initialized |
ps[ii][jj]=0;
|
before: only the contents of out is modified. The function returns |
ps[ii][ii]=1;
|
a pointer to pointers identical to out */ |
}
|
long i, j, k; |
}
|
for(i=nrl; i<= nrh; i++) |
|
for(k=ncolol; k<=ncoloh; k++) |
/* for(ii=1; ii<= nlstate+ndeath; ii++){
|
for(j=ncl,out[i][k]=0.; j<=nch; j++) |
for(jj=1; jj<= nlstate+ndeath; jj++){
|
out[i][k] +=in[i][j]*b[j][k]; |
printf("%lf ",ps[ii][jj]);
|
|
}
|
return out; |
printf("\n ");
|
} |
}
|
|
printf("\n ");printf("%lf ",cov[2]);*/
|
|
/*
|
/************* Higher Matrix Product ***************/ |
for(i=1; i<= npar; i++) printf("%f ",x[i]);
|
|
goto end;*/
|
double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm ) |
return ps;
|
{ |
}
|
/* Computes the transition matrix starting at age 'age' over 'nhstepm*hstepm*stepm' month |
|
duration (i.e. until |
/**************** Product of 2 matrices ******************/
|
age (in years) age+nhstepm*stepm/12) by multiplying nhstepm*hstepm matrices. |
|
Output is stored in matrix po[i][j][h] for h every 'hstepm' step |
double **matprod2(double **out, double **in,long nrl, long nrh, long ncl, long nch, long ncolol, long ncoloh, double **b)
|
(typically every 2 years instead of every month which is too big). |
{
|
Model is determined by parameters x and covariates have to be |
/* Computes the matric product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
|
included manually here. |
b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
|
|
/* in, b, out are matrice of pointers which should have been initialized
|
*/ |
before: only the contents of out is modified. The function returns
|
|
a pointer to pointers identical to out */
|
int i, j, d, h; |
long i, j, k;
|
double **out, cov[NCOVMAX]; |
for(i=nrl; i<= nrh; i++)
|
double **newm; |
for(k=ncolol; k<=ncoloh; k++)
|
|
for(j=ncl,out[i][k]=0.; j<=nch; j++)
|
/* Hstepm could be zero and should return the unit matrix */ |
out[i][k] +=in[i][j]*b[j][k];
|
for (i=1;i<=nlstate+ndeath;i++) |
|
for (j=1;j<=nlstate+ndeath;j++){ |
return out;
|
oldm[i][j]=(i==j ? 1.0 : 0.0); |
}
|
po[i][j][0]=(i==j ? 1.0 : 0.0); |
|
} |
|
/* Even if hstepm = 1, at least one multiplication by the unit matrix */ |
/************* Higher Matrix Product ***************/
|
for(h=1; h <=nhstepm; h++){ |
|
for(d=1; d <=hstepm; d++){ |
double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij )
|
newm=savm; |
{
|
/* Covariates have to be included here again */ |
/* Computes the transition matrix starting at age 'age' over 'nhstepm*hstepm*stepm' month
|
cov[1]=1.; |
duration (i.e. until
|
cov[2]=age+((h-1)*hstepm + (d-1))*stepm/YEARM; |
age (in years) age+nhstepm*stepm/12) by multiplying nhstepm*hstepm matrices.
|
/*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/ |
Output is stored in matrix po[i][j][h] for h every 'hstepm' step
|
out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, |
(typically every 2 years instead of every month which is too big).
|
pmij(pmmij,cov,ncov,x,nlstate)); |
Model is determined by parameters x and covariates have to be
|
savm=oldm; |
included manually here.
|
oldm=newm; |
|
} |
*/
|
for(i=1; i<=nlstate+ndeath; i++) |
|
for(j=1;j<=nlstate+ndeath;j++) { |
int i, j, d, h, k;
|
po[i][j][h]=newm[i][j]; |
double **out, cov[NCOVMAX];
|
/*printf("i=%d j=%d h=%d po[i][j][h]=%f ",i,j,h,po[i][j][h]); |
double **newm;
|
*/ |
|
} |
/* Hstepm could be zero and should return the unit matrix */
|
} /* end h */ |
for (i=1;i<=nlstate+ndeath;i++)
|
return po; |
for (j=1;j<=nlstate+ndeath;j++){
|
} |
oldm[i][j]=(i==j ? 1.0 : 0.0);
|
|
po[i][j][0]=(i==j ? 1.0 : 0.0);
|
|
}
|
/*************** log-likelihood *************/ |
/* Even if hstepm = 1, at least one multiplication by the unit matrix */
|
double func( double *x) |
for(h=1; h <=nhstepm; h++){
|
{ |
for(d=1; d <=hstepm; d++){
|
int i, ii, j, k, mi, d; |
newm=savm;
|
double l, ll[NLSTATEMAX], cov[NCOVMAX]; |
/* Covariates have to be included here again */
|
double **out; |
cov[1]=1.;
|
double sw; /* Sum of weights */ |
cov[2]=age+((h-1)*hstepm + (d-1))*stepm/YEARM;
|
double lli; /* Individual log likelihood */ |
if (cptcovn>0){
|
long ipmx; |
for (k=1; k<=cptcovn;k++) cov[2+k]=nbcode[Tvar[k]][codtab[ij][k]];
|
/*extern weight */ |
}
|
/* We are differentiating ll according to initial status */ |
/*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
|
/* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/ |
/*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
|
/*for(i=1;i<imx;i++) |
out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
|
printf(" %d\n",s[4][i]); |
pmij(pmmij,cov,ncovmodel,x,nlstate));
|
*/ |
savm=oldm;
|
|
oldm=newm;
|
for(k=1; k<=nlstate; k++) ll[k]=0.; |
}
|
for (i=1,ipmx=0, sw=0.; i<=imx; i++){ |
for(i=1; i<=nlstate+ndeath; i++)
|
for(mi=1; mi<= wav[i]-1; mi++){ |
for(j=1;j<=nlstate+ndeath;j++) {
|
for (ii=1;ii<=nlstate+ndeath;ii++) |
po[i][j][h]=newm[i][j];
|
for (j=1;j<=nlstate+ndeath;j++) oldm[ii][j]=(ii==j ? 1.0 : 0.0); |
/*printf("i=%d j=%d h=%d po[i][j][h]=%f ",i,j,h,po[i][j][h]);
|
for(d=0; d<dh[mi][i]; d++){ |
*/
|
newm=savm; |
}
|
cov[1]=1.; |
} /* end h */
|
cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM; |
return po;
|
out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, |
}
|
1,nlstate+ndeath,pmij(pmmij,cov,ncov,x,nlstate)); |
|
savm=oldm; |
|
oldm=newm; |
/*************** log-likelihood *************/
|
|
double func( double *x)
|
|
{
|
} /* end mult */ |
int i, ii, j, k, mi, d;
|
|
double l, ll[NLSTATEMAX], cov[NCOVMAX];
|
lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); |
double **out;
|
/* printf(" %f ",out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ |
double sw; /* Sum of weights */
|
ipmx +=1; |
double lli; /* Individual log likelihood */
|
sw += weight[i]; |
long ipmx;
|
ll[s[mw[mi][i]][i]] += 2*weight[i]*lli; |
/*extern weight */
|
} /* end of wave */ |
/* We are differentiating ll according to initial status */
|
} /* end of individual */ |
/* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
|
|
/*for(i=1;i<imx;i++)
|
for(k=1,l=0.; k<=nlstate; k++) l += ll[k]; |
printf(" %d\n",s[4][i]);
|
/* 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 */ |
|
return -l; |
for(k=1; k<=nlstate; k++) ll[k]=0.;
|
} |
for (i=1,ipmx=0, sw=0.; i<=imx; i++){
|
|
for(mi=1; mi<= wav[i]-1; mi++){
|
|
for (ii=1;ii<=nlstate+ndeath;ii++)
|
/*********** Maximum Likelihood Estimation ***************/ |
for (j=1;j<=nlstate+ndeath;j++) oldm[ii][j]=(ii==j ? 1.0 : 0.0);
|
|
for(d=0; d<dh[mi][i]; d++){
|
void mlikeli(FILE *ficres,double p[], int npar, int ncov, int nlstate, double ftol, double (*func)(double [])) |
newm=savm;
|
{ |
cov[1]=1.;
|
int i,j, iter; |
cov[2]=agev[mw[mi][i]][i]+d*stepm/YEARM;
|
double **xi,*delti; |
if (cptcovn>0){
|
double fret; |
for (k=1; k<=cptcovn;k++) {
|
xi=matrix(1,npar,1,npar); |
cov[2+k]=covar[Tvar[k]][i];
|
for (i=1;i<=npar;i++) |
/* printf("k=%d cptcovn=%d %lf\n",k,cptcovn,covar[Tvar[k]][i]);*/
|
for (j=1;j<=npar;j++) |
}
|
xi[i][j]=(i==j ? 1.0 : 0.0); |
}
|
printf("Powell\n"); |
out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
|
powell(p,xi,npar,ftol,&iter,&fret,func); |
1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
|
|
savm=oldm;
|
printf("\n#Number of iterations = %d, -2 Log likelihood = %.12f\n",iter,func(p)); |
oldm=newm;
|
fprintf(ficres,"#Number of iterations = %d, -2 Log likelihood = %.12f ",iter,func(p)); |
} /* end mult */
|
|
|
} |
lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);
|
|
/* printf(" %f ",out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/
|
/**** Computes Hessian and covariance matrix ***/ |
ipmx +=1;
|
void hesscov(double **matcov, double p[], int npar, double delti[], double ftolhess, double (*func)(double [])) |
sw += weight[i];
|
{ |
ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
|
double **a,**y,*x,pd; |
} /* end of wave */
|
double **hess; |
} /* end of individual */
|
int i, j,jk; |
|
int *indx; |
for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
|
|
/* printf("l1=%f l2=%f ",ll[1],ll[2]); */
|
double hessii(double p[], double delta, int theta, double delti[]); |
l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
|
double hessij(double p[], double delti[], int i, int j); |
|
void lubksb(double **a, int npar, int *indx, double b[]) ; |
return -l;
|
void ludcmp(double **a, int npar, int *indx, double *d) ; |
}
|
|
|
|
|
hess=matrix(1,npar,1,npar); |
/*********** Maximum Likelihood Estimation ***************/
|
|
|
printf("\nCalculation of the hessian matrix. Wait...\n"); |
void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
|
for (i=1;i<=npar;i++){ |
{
|
printf("%d",i);fflush(stdout); |
int i,j, iter;
|
hess[i][i]=hessii(p,ftolhess,i,delti); |
double **xi,*delti;
|
/*printf(" %f ",p[i]);*/ |
double fret;
|
} |
xi=matrix(1,npar,1,npar);
|
|
for (i=1;i<=npar;i++)
|
for (i=1;i<=npar;i++) { |
for (j=1;j<=npar;j++)
|
for (j=1;j<=npar;j++) { |
xi[i][j]=(i==j ? 1.0 : 0.0);
|
if (j>i) { |
printf("Powell\n");
|
printf(".%d%d",i,j);fflush(stdout); |
powell(p,xi,npar,ftol,&iter,&fret,func);
|
hess[i][j]=hessij(p,delti,i,j); |
|
hess[j][i]=hess[i][j]; |
printf("\n#Number of iterations = %d, -2 Log likelihood = %.12f\n",iter,func(p));
|
} |
fprintf(ficres,"#Number of iterations = %d, -2 Log likelihood = %.12f ",iter,func(p));
|
} |
|
} |
}
|
printf("\n"); |
|
|
/**** Computes Hessian and covariance matrix ***/
|
printf("\nInverting the hessian to get the covariance matrix. Wait...\n"); |
void hesscov(double **matcov, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
|
|
{
|
a=matrix(1,npar,1,npar); |
double **a,**y,*x,pd;
|
y=matrix(1,npar,1,npar); |
double **hess;
|
x=vector(1,npar); |
int i, j,jk;
|
indx=ivector(1,npar); |
int *indx;
|
for (i=1;i<=npar;i++) |
|
for (j=1;j<=npar;j++) a[i][j]=hess[i][j]; |
double hessii(double p[], double delta, int theta, double delti[]);
|
ludcmp(a,npar,indx,&pd); |
double hessij(double p[], double delti[], int i, int j);
|
|
void lubksb(double **a, int npar, int *indx, double b[]) ;
|
for (j=1;j<=npar;j++) { |
void ludcmp(double **a, int npar, int *indx, double *d) ;
|
for (i=1;i<=npar;i++) x[i]=0; |
|
x[j]=1; |
|
lubksb(a,npar,indx,x); |
hess=matrix(1,npar,1,npar);
|
for (i=1;i<=npar;i++){ |
|
matcov[i][j]=x[i]; |
printf("\nCalculation of the hessian matrix. Wait...\n");
|
} |
for (i=1;i<=npar;i++){
|
} |
printf("%d",i);fflush(stdout);
|
|
hess[i][i]=hessii(p,ftolhess,i,delti);
|
printf("\n#Hessian matrix#\n"); |
/*printf(" %f ",p[i]);*/
|
for (i=1;i<=npar;i++) { |
}
|
for (j=1;j<=npar;j++) { |
|
printf("%.3e ",hess[i][j]); |
for (i=1;i<=npar;i++) {
|
} |
for (j=1;j<=npar;j++) {
|
printf("\n"); |
if (j>i) {
|
} |
printf(".%d%d",i,j);fflush(stdout);
|
|
hess[i][j]=hessij(p,delti,i,j);
|
/* Recompute Inverse */ |
hess[j][i]=hess[i][j];
|
for (i=1;i<=npar;i++) |
}
|
for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; |
}
|
ludcmp(a,npar,indx,&pd); |
}
|
|
printf("\n");
|
/* printf("\n#Hessian matrix recomputed#\n"); |
|
|
printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
|
for (j=1;j<=npar;j++) { |
|
for (i=1;i<=npar;i++) x[i]=0; |
a=matrix(1,npar,1,npar);
|
x[j]=1; |
y=matrix(1,npar,1,npar);
|
lubksb(a,npar,indx,x); |
x=vector(1,npar);
|
for (i=1;i<=npar;i++){ |
indx=ivector(1,npar);
|
y[i][j]=x[i]; |
for (i=1;i<=npar;i++)
|
printf("%.3e ",y[i][j]); |
for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
|
} |
ludcmp(a,npar,indx,&pd);
|
printf("\n"); |
|
} |
for (j=1;j<=npar;j++) {
|
*/ |
for (i=1;i<=npar;i++) x[i]=0;
|
|
x[j]=1;
|
free_matrix(a,1,npar,1,npar); |
lubksb(a,npar,indx,x);
|
free_matrix(y,1,npar,1,npar); |
for (i=1;i<=npar;i++){
|
free_vector(x,1,npar); |
matcov[i][j]=x[i];
|
free_ivector(indx,1,npar); |
}
|
free_matrix(hess,1,npar,1,npar); |
}
|
|
|
|
printf("\n#Hessian matrix#\n");
|
} |
for (i=1;i<=npar;i++) {
|
|
for (j=1;j<=npar;j++) {
|
/*************** hessian matrix ****************/ |
printf("%.3e ",hess[i][j]);
|
double hessii( double x[], double delta, int theta, double delti[]) |
}
|
{ |
printf("\n");
|
int i; |
}
|
int l=1, lmax=20; |
|
double k1,k2; |
/* Recompute Inverse */
|
double p2[NPARMAX+1]; |
for (i=1;i<=npar;i++)
|
double res; |
for (j=1;j<=npar;j++) a[i][j]=matcov[i][j];
|
double delt, delts, nkhi=10.,nkhif=1., khi=1.e-4; |
ludcmp(a,npar,indx,&pd);
|
double fx; |
|
int k=0,kmax=10; |
/* printf("\n#Hessian matrix recomputed#\n");
|
double l1; |
|
|
for (j=1;j<=npar;j++) {
|
fx=func(x); |
for (i=1;i<=npar;i++) x[i]=0;
|
for (i=1;i<=npar;i++) p2[i]=x[i]; |
x[j]=1;
|
for(l=0 ; l <=lmax; l++){ |
lubksb(a,npar,indx,x);
|
l1=pow(10,l); |
for (i=1;i<=npar;i++){
|
delts=delt; |
y[i][j]=x[i];
|
for(k=1 ; k <kmax; k=k+1){ |
printf("%.3e ",y[i][j]);
|
delt = delta*(l1*k); |
}
|
p2[theta]=x[theta] +delt; |
printf("\n");
|
k1=func(p2)-fx; |
}
|
p2[theta]=x[theta]-delt; |
*/
|
k2=func(p2)-fx; |
|
/*res= (k1-2.0*fx+k2)/delt/delt; */ |
free_matrix(a,1,npar,1,npar);
|
res= (k1+k2)/delt/delt/2.; /* Divided by because L and not 2*L */ |
free_matrix(y,1,npar,1,npar);
|
|
free_vector(x,1,npar);
|
#ifdef DEBUG |
free_ivector(indx,1,npar);
|
printf("%d %d k1=%.12e k2=%.12e xk1=%.12e xk2=%.12e delt=%.12e res=%.12e l=%d k=%d,fx=%.12e\n",theta,theta,k1,k2,x[theta]+delt,x[theta]-delt,delt,res, l, k,fx); |
free_matrix(hess,1,npar,1,npar);
|
#endif |
|
/*if(fabs(k1-2.0*fx+k2) <1.e-13){ */ |
|
if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){ |
}
|
k=kmax; |
|
} |
/*************** hessian matrix ****************/
|
else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */ |
double hessii( double x[], double delta, int theta, double delti[])
|
k=kmax; l=lmax*10.; |
{
|
} |
int i;
|
else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ |
int l=1, lmax=20;
|
delts=delt; |
double k1,k2;
|
} |
double p2[NPARMAX+1];
|
} |
double res;
|
} |
double delt, delts, nkhi=10.,nkhif=1., khi=1.e-4;
|
delti[theta]=delts; |
double fx;
|
return res; |
int k=0,kmax=10;
|
|
double l1;
|
} |
|
|
fx=func(x);
|
double hessij( double x[], double delti[], int thetai,int thetaj) |
for (i=1;i<=npar;i++) p2[i]=x[i];
|
{ |
for(l=0 ; l <=lmax; l++){
|
int i; |
l1=pow(10,l);
|
int l=1, l1, lmax=20; |
delts=delt;
|
double k1,k2,k3,k4,res,fx; |
for(k=1 ; k <kmax; k=k+1){
|
double p2[NPARMAX+1]; |
delt = delta*(l1*k);
|
int k; |
p2[theta]=x[theta] +delt;
|
|
k1=func(p2)-fx;
|
fx=func(x); |
p2[theta]=x[theta]-delt;
|
for (k=1; k<=2; k++) { |
k2=func(p2)-fx;
|
for (i=1;i<=npar;i++) p2[i]=x[i]; |
/*res= (k1-2.0*fx+k2)/delt/delt; */
|
p2[thetai]=x[thetai]+delti[thetai]/k; |
res= (k1+k2)/delt/delt/2.; /* Divided by because L and not 2*L */
|
p2[thetaj]=x[thetaj]+delti[thetaj]/k; |
|
k1=func(p2)-fx; |
#ifdef DEBUG
|
|
printf("%d %d k1=%.12e k2=%.12e xk1=%.12e xk2=%.12e delt=%.12e res=%.12e l=%d k=%d,fx=%.12e\n",theta,theta,k1,k2,x[theta]+delt,x[theta]-delt,delt,res, l, k,fx);
|
p2[thetai]=x[thetai]+delti[thetai]/k; |
#endif
|
p2[thetaj]=x[thetaj]-delti[thetaj]/k; |
/*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
|
k2=func(p2)-fx; |
if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
|
|
k=kmax;
|
p2[thetai]=x[thetai]-delti[thetai]/k; |
}
|
p2[thetaj]=x[thetaj]+delti[thetaj]/k; |
else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
|
k3=func(p2)-fx; |
k=kmax; l=lmax*10.;
|
|
}
|
p2[thetai]=x[thetai]-delti[thetai]/k; |
else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
|
p2[thetaj]=x[thetaj]-delti[thetaj]/k; |
delts=delt;
|
k4=func(p2)-fx; |
}
|
res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /* Because of L not 2*L */ |
}
|
#ifdef DEBUG |
}
|
printf("%d %d k=%d, k1=%.12e k2=%.12e k3=%.12e k4=%.12e delti/k=%.12e deltj/k=%.12e, xi-de/k=%.12e xj-de/k=%.12e res=%.12e k1234=%.12e,k1-2=%.12e,k3-4=%.12e\n",thetai,thetaj,k,k1,k2,k3,k4,delti[thetai]/k,delti[thetaj]/k,x[thetai]-delti[thetai]/k,x[thetaj]-delti[thetaj]/k, res,k1-k2-k3+k4,k1-k2,k3-k4); |
delti[theta]=delts;
|
#endif |
return res;
|
} |
}
|
return res; |
|
} |
double hessij( double x[], double delti[], int thetai,int thetaj)
|
|
{
|
/************** Inverse of matrix **************/ |
int i;
|
void ludcmp(double **a, int n, int *indx, double *d) |
int l=1, l1, lmax=20;
|
{ |
double k1,k2,k3,k4,res,fx;
|
int i,imax,j,k; |
double p2[NPARMAX+1];
|
double big,dum,sum,temp; |
int k;
|
double *vv; |
|
|
fx=func(x);
|
vv=vector(1,n); |
for (k=1; k<=2; k++) {
|
*d=1.0; |
for (i=1;i<=npar;i++) p2[i]=x[i];
|
for (i=1;i<=n;i++) { |
p2[thetai]=x[thetai]+delti[thetai]/k;
|
big=0.0; |
p2[thetaj]=x[thetaj]+delti[thetaj]/k;
|
for (j=1;j<=n;j++) |
k1=func(p2)-fx;
|
if ((temp=fabs(a[i][j])) > big) big=temp; |
|
if (big == 0.0) nrerror("Singular matrix in routine ludcmp"); |
p2[thetai]=x[thetai]+delti[thetai]/k;
|
vv[i]=1.0/big; |
p2[thetaj]=x[thetaj]-delti[thetaj]/k;
|
} |
k2=func(p2)-fx;
|
for (j=1;j<=n;j++) { |
|
for (i=1;i<j;i++) { |
p2[thetai]=x[thetai]-delti[thetai]/k;
|
sum=a[i][j]; |
p2[thetaj]=x[thetaj]+delti[thetaj]/k;
|
for (k=1;k<i;k++) sum -= a[i][k]*a[k][j]; |
k3=func(p2)-fx;
|
a[i][j]=sum; |
|
} |
p2[thetai]=x[thetai]-delti[thetai]/k;
|
big=0.0; |
p2[thetaj]=x[thetaj]-delti[thetaj]/k;
|
for (i=j;i<=n;i++) { |
k4=func(p2)-fx;
|
sum=a[i][j]; |
res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /* Because of L not 2*L */
|
for (k=1;k<j;k++) |
#ifdef DEBUG
|
sum -= a[i][k]*a[k][j]; |
printf("%d %d k=%d, k1=%.12e k2=%.12e k3=%.12e k4=%.12e delti/k=%.12e deltj/k=%.12e, xi-de/k=%.12e xj-de/k=%.12e res=%.12e k1234=%.12e,k1-2=%.12e,k3-4=%.12e\n",thetai,thetaj,k,k1,k2,k3,k4,delti[thetai]/k,delti[thetaj]/k,x[thetai]-delti[thetai]/k,x[thetaj]-delti[thetaj]/k, res,k1-k2-k3+k4,k1-k2,k3-k4);
|
a[i][j]=sum; |
#endif
|
if ( (dum=vv[i]*fabs(sum)) >= big) { |
}
|
big=dum; |
return res;
|
imax=i; |
}
|
} |
|
} |
/************** Inverse of matrix **************/
|
if (j != imax) { |
void ludcmp(double **a, int n, int *indx, double *d)
|
for (k=1;k<=n;k++) { |
{
|
dum=a[imax][k]; |
int i,imax,j,k;
|
a[imax][k]=a[j][k]; |
double big,dum,sum,temp;
|
a[j][k]=dum; |
double *vv;
|
} |
|
*d = -(*d); |
vv=vector(1,n);
|
vv[imax]=vv[j]; |
*d=1.0;
|
} |
for (i=1;i<=n;i++) {
|
indx[j]=imax; |
big=0.0;
|
if (a[j][j] == 0.0) a[j][j]=TINY; |
for (j=1;j<=n;j++)
|
if (j != n) { |
if ((temp=fabs(a[i][j])) > big) big=temp;
|
dum=1.0/(a[j][j]); |
if (big == 0.0) nrerror("Singular matrix in routine ludcmp");
|
for (i=j+1;i<=n;i++) a[i][j] *= dum; |
vv[i]=1.0/big;
|
} |
}
|
} |
for (j=1;j<=n;j++) {
|
free_vector(vv,1,n); /* Doesn't work */ |
for (i=1;i<j;i++) {
|
; |
sum=a[i][j];
|
} |
for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
|
|
a[i][j]=sum;
|
void lubksb(double **a, int n, int *indx, double b[]) |
}
|
{ |
big=0.0;
|
int i,ii=0,ip,j; |
for (i=j;i<=n;i++) {
|
double sum; |
sum=a[i][j];
|
|
for (k=1;k<j;k++)
|
for (i=1;i<=n;i++) { |
sum -= a[i][k]*a[k][j];
|
ip=indx[i]; |
a[i][j]=sum;
|
sum=b[ip]; |
if ( (dum=vv[i]*fabs(sum)) >= big) {
|
b[ip]=b[i]; |
big=dum;
|
if (ii) |
imax=i;
|
for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j]; |
}
|
else if (sum) ii=i; |
}
|
b[i]=sum; |
if (j != imax) {
|
} |
for (k=1;k<=n;k++) {
|
for (i=n;i>=1;i--) { |
dum=a[imax][k];
|
sum=b[i]; |
a[imax][k]=a[j][k];
|
for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j]; |
a[j][k]=dum;
|
b[i]=sum/a[i][i]; |
}
|
} |
*d = -(*d);
|
} |
vv[imax]=vv[j];
|
|
}
|
/************ Frequencies ********************/ |
indx[j]=imax;
|
void freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev, int nlstate, int imx) |
if (a[j][j] == 0.0) a[j][j]=TINY;
|
{ /* Some frequencies */ |
if (j != n) {
|
|
dum=1.0/(a[j][j]);
|
int i, m, jk; |
for (i=j+1;i<=n;i++) a[i][j] *= dum;
|
double ***freq; /* Frequencies */ |
}
|
double *pp; |
}
|
double pos; |
free_vector(vv,1,n); /* Doesn't work */
|
FILE *ficresp; |
;
|
char fileresp[FILENAMELENGTH]; |
}
|
|
|
pp=vector(1,nlstate); |
void lubksb(double **a, int n, int *indx, double b[])
|
|
{
|
strcpy(fileresp,"p"); |
int i,ii=0,ip,j;
|
strcat(fileresp,fileres); |
double sum;
|
if((ficresp=fopen(fileresp,"w"))==NULL) { |
|
printf("Problem with prevalence resultfile: %s\n", fileresp); |
for (i=1;i<=n;i++) {
|
exit(0); |
ip=indx[i];
|
} |
sum=b[ip];
|
|
b[ip]=b[i];
|
freq= ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,agemin,agemax+3); |
if (ii)
|
for (i=-1; i<=nlstate+ndeath; i++) |
for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
|
for (jk=-1; jk<=nlstate+ndeath; jk++) |
else if (sum) ii=i;
|
for(m=agemin; m <= agemax+3; m++) |
b[i]=sum;
|
freq[i][jk][m]=0; |
}
|
|
for (i=n;i>=1;i--) {
|
for (i=1; i<=imx; i++) { |
sum=b[i];
|
for(m=firstpass; m<= lastpass-1; m++){ |
for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
|
if(agev[m][i]==0) agev[m][i]=agemax+1; |
b[i]=sum/a[i][i];
|
if(agev[m][i]==1) agev[m][i]=agemax+2; |
}
|
freq[s[m][i]][s[m+1][i]][(int)agev[m][i]] += weight[i]; |
}
|
freq[s[m][i]][s[m+1][i]][(int) agemax+3] += weight[i]; |
|
} |
/************ Frequencies ********************/
|
} |
void freqsummary(char fileres[], int agemin, int agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax)
|
|
{ /* Some frequencies */
|
fprintf(ficresp, "#"); |
|
for(i=1; i<=nlstate;i++) |
int i, m, jk, k1, i1, j1, bool, z1,z2,j;
|
fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i); |
double ***freq; /* Frequencies */
|
fprintf(ficresp, "\n"); |
double *pp;
|
|
double pos;
|
for(i=(int)agemin; i <= (int)agemax+3; i++){ |
FILE *ficresp;
|
if(i==(int)agemax+3) |
char fileresp[FILENAMELENGTH];
|
printf("Total"); |
|
else |
pp=vector(1,nlstate);
|
printf("Age %d", i); |
|
for(jk=1; jk <=nlstate ; jk++){ |
strcpy(fileresp,"p");
|
for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++) |
strcat(fileresp,fileres);
|
pp[jk] += freq[jk][m][i]; |
if((ficresp=fopen(fileresp,"w"))==NULL) {
|
} |
printf("Problem with prevalence resultfile: %s\n", fileresp);
|
for(jk=1; jk <=nlstate ; jk++){ |
exit(0);
|
for(m=-1, pos=0; m <=0 ; m++) |
}
|
pos += freq[jk][m][i]; |
freq= ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,agemin,agemax+3);
|
if(pp[jk]>=1.e-10) |
j1=0;
|
printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]); |
|
else |
j=cptcovn;
|
printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk); |
if (cptcovn<1) {j=1;ncodemax[1]=1;}
|
} |
|
for(jk=1; jk <=nlstate ; jk++){ |
for(k1=1; k1<=j;k1++){
|
for(m=1, pp[jk]=0; m <=nlstate+ndeath; m++) |
for(i1=1; i1<=ncodemax[k1];i1++){
|
pp[jk] += freq[jk][m][i]; |
j1++;
|
} |
|
for(jk=1,pos=0; jk <=nlstate ; jk++) |
for (i=-1; i<=nlstate+ndeath; i++)
|
pos += pp[jk]; |
for (jk=-1; jk<=nlstate+ndeath; jk++)
|
for(jk=1; jk <=nlstate ; jk++){ |
for(m=agemin; m <= agemax+3; m++)
|
if(pos>=1.e-5) |
freq[i][jk][m]=0;
|
printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos); |
|
else |
for (i=1; i<=imx; i++) {
|
printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk); |
bool=1;
|
if( i <= (int) agemax){ |
if (cptcovn>0) {
|
if(pos>=1.e-5) |
for (z1=1; z1<=cptcovn; z1++)
|
fprintf(ficresp," %d %.5f %.0f %.0f",i,pp[jk]/pos, pp[jk],pos); |
if (covar[Tvar[z1]][i]!= nbcode[Tvar[z1]][codtab[j1][z1]]) bool=0;
|
else |
}
|
fprintf(ficresp," %d NaNq %.0f %.0f",i,pp[jk],pos); |
if (bool==1) {
|
} |
for(m=firstpass; m<=lastpass-1; m++){
|
} |
if(agev[m][i]==0) agev[m][i]=agemax+1;
|
for(jk=-1; jk <=nlstate+ndeath; jk++) |
if(agev[m][i]==1) agev[m][i]=agemax+2;
|
for(m=-1; m <=nlstate+ndeath; m++) |
freq[s[m][i]][s[m+1][i]][(int)agev[m][i]] += weight[i];
|
if(freq[jk][m][i] !=0 ) printf(" %d%d=%.0f",jk,m,freq[jk][m][i]); |
freq[s[m][i]][s[m+1][i]][(int) agemax+3] += weight[i];
|
if(i <= (int) agemax) |
}
|
fprintf(ficresp,"\n"); |
}
|
printf("\n"); |
}
|
} |
if (cptcovn>0) {
|
|
fprintf(ficresp, "\n#Variable");
|
fclose(ficresp); |
for (z1=1; z1<=cptcovn; z1++) fprintf(ficresp, " V%d=%d",Tvar[z1],nbcode[Tvar[z1]][codtab[j1][z1]]);
|
free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath,(int) agemin,(int) agemax+3); |
}
|
free_vector(pp,1,nlstate); |
fprintf(ficresp, "\n#");
|
|
for(i=1; i<=nlstate;i++)
|
} /* End of Freq */ |
fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
|
|
fprintf(ficresp, "\n");
|
/************* Waves Concatenation ***************/ |
|
|
for(i=(int)agemin; i <= (int)agemax+3; i++){
|
void concatwav(int wav[], int **dh, int **mw, int **s, double *agedc, double **agev, int firstpass, int lastpass, int imx, int nlstate, int stepm) |
if(i==(int)agemax+3)
|
{ |
printf("Total");
|
/* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i. |
else
|
Death is a valid wave (if date is known). |
printf("Age %d", i);
|
mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i |
for(jk=1; jk <=nlstate ; jk++){
|
dh[m][i] of dh[mw[mi][i][i] is the delay between two effective waves m=mw[mi][i] |
for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
|
and mw[mi+1][i]. dh depends on stepm. |
pp[jk] += freq[jk][m][i];
|
*/ |
}
|
|
for(jk=1; jk <=nlstate ; jk++){
|
int i, mi, m; |
for(m=-1, pos=0; m <=0 ; m++)
|
int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1; |
pos += freq[jk][m][i];
|
float sum=0.; |
if(pp[jk]>=1.e-10)
|
|
printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
|
for(i=1; i<=imx; i++){ |
else
|
mi=0; |
printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
|
m=firstpass; |
}
|
while(s[m][i] <= nlstate){ |
for(jk=1; jk <=nlstate ; jk++){
|
if(s[m][i]>=1) |
for(m=1, pp[jk]=0; m <=nlstate+ndeath; m++)
|
mw[++mi][i]=m; |
pp[jk] += freq[jk][m][i];
|
if(m >=lastpass) |
}
|
break; |
for(jk=1,pos=0; jk <=nlstate ; jk++)
|
else |
pos += pp[jk];
|
m++; |
for(jk=1; jk <=nlstate ; jk++){
|
}/* end while */ |
if(pos>=1.e-5)
|
if (s[m][i] > nlstate){ |
printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
|
mi++; /* Death is another wave */ |
else
|
/* if(mi==0) never been interviewed correctly before death */ |
printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
|
/* Only death is a correct wave */ |
if( i <= (int) agemax){
|
mw[mi][i]=m; |
if(pos>=1.e-5)
|
} |
fprintf(ficresp," %d %.5f %.0f %.0f",i,pp[jk]/pos, pp[jk],pos);
|
|
else
|
wav[i]=mi; |
fprintf(ficresp," %d NaNq %.0f %.0f",i,pp[jk],pos);
|
if(mi==0) |
}
|
printf("Warning, no any valid information for:%d line=%d\n",num[i],i); |
}
|
} |
for(jk=-1; jk <=nlstate+ndeath; jk++)
|
|
for(m=-1; m <=nlstate+ndeath; m++)
|
for(i=1; i<=imx; i++){ |
if(freq[jk][m][i] !=0 ) printf(" %d%d=%.0f",jk,m,freq[jk][m][i]);
|
for(mi=1; mi<wav[i];mi++){ |
if(i <= (int) agemax)
|
if (stepm <=0) |
fprintf(ficresp,"\n");
|
dh[mi][i]=1; |
printf("\n");
|
else{ |
}
|
if (s[mw[mi+1][i]][i] > nlstate) { |
}
|
j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12); |
}
|
if(j=0) j=1; /* Survives at least one month after exam */ |
|
} |
fclose(ficresp);
|
else{ |
free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath,(int) agemin,(int) agemax+3);
|
j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12)); |
free_vector(pp,1,nlstate);
|
k=k+1; |
|
if (j >= jmax) jmax=j; |
} /* End of Freq */
|
else if (j <= jmin)jmin=j; |
|
sum=sum+j; |
/************* Waves Concatenation ***************/
|
} |
|
jk= j/stepm; |
void concatwav(int wav[], int **dh, int **mw, int **s, double *agedc, double **agev, int firstpass, int lastpass, int imx, int nlstate, int stepm)
|
jl= j -jk*stepm; |
{
|
ju= j -(jk+1)*stepm; |
/* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
|
if(jl <= -ju) |
Death is a valid wave (if date is known).
|
dh[mi][i]=jk; |
mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
|
else |
dh[m][i] of dh[mw[mi][i][i] is the delay between two effective waves m=mw[mi][i]
|
dh[mi][i]=jk+1; |
and mw[mi+1][i]. dh depends on stepm.
|
if(dh[mi][i]==0) |
*/
|
dh[mi][i]=1; /* At least one step */ |
|
} |
int i, mi, m;
|
} |
int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
|
} |
float sum=0.;
|
printf("Delay (in months) between two waves Min=%d Max=%d Mean=%f\n\n ",jmin, jmax,sum/k); |
|
} |
for(i=1; i<=imx; i++){
|
|
mi=0;
|
/*********** Health Expectancies ****************/ |
m=firstpass;
|
|
while(s[m][i] <= nlstate){
|
void evsij(char fileres[], double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm) |
if(s[m][i]>=1)
|
{ |
mw[++mi][i]=m;
|
/* Health expectancies */ |
if(m >=lastpass)
|
int i, j, nhstepm, hstepm, h; |
break;
|
double age, agelim,hf; |
else
|
double ***p3mat; |
m++;
|
|
}/* end while */
|
FILE *ficreseij; |
if (s[m][i] > nlstate){
|
char filerese[FILENAMELENGTH]; |
mi++; /* Death is another wave */
|
|
/* if(mi==0) never been interviewed correctly before death */
|
strcpy(filerese,"e"); |
/* Only death is a correct wave */
|
strcat(filerese,fileres); |
mw[mi][i]=m;
|
if((ficreseij=fopen(filerese,"w"))==NULL) { |
}
|
printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0); |
|
} |
wav[i]=mi;
|
printf("Computing Health Expectancies: result on file '%s' \n", filerese); |
if(mi==0)
|
|
printf("Warning, no any valid information for:%d line=%d\n",num[i],i);
|
fprintf(ficreseij,"# Health expectancies\n"); |
}
|
fprintf(ficreseij,"# Age"); |
|
for(i=1; i<=nlstate;i++) |
for(i=1; i<=imx; i++){
|
for(j=1; j<=nlstate;j++) |
for(mi=1; mi<wav[i];mi++){
|
fprintf(ficreseij," %1d-%1d",i,j); |
if (stepm <=0)
|
fprintf(ficreseij,"\n"); |
dh[mi][i]=1;
|
|
else{
|
hstepm=1*YEARM; /* Every j years of age (in month) */ |
if (s[mw[mi+1][i]][i] > nlstate) {
|
hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */ |
j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
|
|
if(j=0) j=1; /* Survives at least one month after exam */
|
agelim=AGESUP; |
}
|
for (age=bage; age<=fage; age ++){ /* If stepm=6 months */ |
else{
|
/* nhstepm age range expressed in number of stepm */ |
j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
|
nhstepm=(int) rint((agelim-age)*YEARM/stepm); |
/*printf("i=%d agevi+1=%lf agevi=%lf j=%d\n", i,agev[mw[mi+1][i]][i],agev[mw[mi][i]][i],j);*/
|
/* Typically if 20 years = 20*12/6=40 stepm */ |
|
if (stepm >= YEARM) hstepm=1; |
k=k+1;
|
nhstepm = nhstepm/hstepm;/* Expressed in hstepm, typically 40/4=10 */ |
if (j >= jmax) jmax=j;
|
p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); |
else if (j <= jmin)jmin=j;
|
/* Computed by stepm unit matrices, product of hstepm matrices, stored |
sum=sum+j;
|
in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */ |
}
|
hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm); |
jk= j/stepm;
|
|
jl= j -jk*stepm;
|
|
ju= j -(jk+1)*stepm;
|
for(i=1; i<=nlstate;i++) |
if(jl <= -ju)
|
for(j=1; j<=nlstate;j++) |
dh[mi][i]=jk;
|
for (h=0, eij[i][j][(int)age]=0; h<=nhstepm; h++){ |
else
|
eij[i][j][(int)age] +=p3mat[i][j][h]; |
dh[mi][i]=jk+1;
|
} |
if(dh[mi][i]==0)
|
|
dh[mi][i]=1; /* At least one step */
|
hf=1; |
}
|
if (stepm >= YEARM) hf=stepm/YEARM; |
}
|
fprintf(ficreseij,"%.0f",age ); |
}
|
for(i=1; i<=nlstate;i++) |
printf("Delay (in months) between two waves Min=%d Max=%d Mean=%f\n\n ",jmin, jmax,sum/k);
|
for(j=1; j<=nlstate;j++){ |
}
|
fprintf(ficreseij," %.4f", hf*eij[i][j][(int)age]); |
/*********** Tricode ****************************/
|
} |
void tricode(int *Tvar, int **nbcode, int imx)
|
fprintf(ficreseij,"\n"); |
{
|
free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); |
int Ndum[80],ij, k, j, i;
|
} |
int cptcode=0;
|
fclose(ficreseij); |
for (k=0; k<79; k++) Ndum[k]=0;
|
} |
for (k=1; k<=7; k++) ncodemax[k]=0;
|
|
|
/************ Variance ******************/ |
for (j=1; j<=cptcovn; j++) {
|
void varevsij(char fileres[], double ***vareij, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl) |
for (i=1; i<=imx; i++) {
|
{ |
ij=(int)(covar[Tvar[j]][i]);
|
/* Variance of health expectancies */ |
Ndum[ij]++;
|
/* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/ |
if (ij > cptcode) cptcode=ij;
|
double **newm; |
}
|
double **dnewm,**doldm; |
/*printf("cptcode=%d cptcovn=%d ",cptcode,cptcovn);*/
|
int i, j, nhstepm, hstepm, h; |
for (i=0; i<=cptcode; i++) {
|
int k; |
if(Ndum[i]!=0) ncodemax[j]++;
|
FILE *ficresvij; |
}
|
char fileresv[FILENAMELENGTH]; |
|
double *xp; |
ij=1;
|
double **gp, **gm; |
for (i=1; i<=ncodemax[j]; i++) {
|
double ***gradg, ***trgradg; |
for (k=0; k<=79; k++) {
|
double ***p3mat; |
if (Ndum[k] != 0) {
|
double age,agelim; |
nbcode[Tvar[j]][ij]=k;
|
int theta; |
ij++;
|
|
}
|
strcpy(fileresv,"v"); |
if (ij > ncodemax[j]) break;
|
strcat(fileresv,fileres); |
}
|
if((ficresvij=fopen(fileresv,"w"))==NULL) { |
}
|
printf("Problem with variance resultfile: %s\n", fileresv);exit(0); |
}
|
} |
|
printf("Computing Variance-covariance of DFLEs: file '%s' \n", fileresv); |
}
|
|
|
|
/*********** Health Expectancies ****************/
|
fprintf(ficresvij,"# Covariances of life expectancies\n"); |
|
fprintf(ficresvij,"# Age"); |
void evsij(char fileres[], double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int ij)
|
for(i=1; i<=nlstate;i++) |
{
|
for(j=1; j<=nlstate;j++) |
/* Health expectancies */
|
fprintf(ficresvij," Cov(e%1d, e%1d)",i,j); |
int i, j, nhstepm, hstepm, h;
|
fprintf(ficresvij,"\n"); |
double age, agelim,hf;
|
|
double ***p3mat;
|
xp=vector(1,npar); |
|
dnewm=matrix(1,nlstate,1,npar); |
fprintf(ficreseij,"# Health expectancies\n");
|
doldm=matrix(1,nlstate,1,nlstate); |
fprintf(ficreseij,"# Age");
|
|
for(i=1; i<=nlstate;i++)
|
hstepm=1*YEARM; /* Every year of age */ |
for(j=1; j<=nlstate;j++)
|
hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */ |
fprintf(ficreseij," %1d-%1d",i,j);
|
agelim = AGESUP; |
fprintf(ficreseij,"\n");
|
for (age=bage; age<=fage; age ++){ /* If stepm=6 months */ |
|
nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */ |
hstepm=1*YEARM; /* Every j years of age (in month) */
|
if (stepm >= YEARM) hstepm=1; |
hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
|
nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */ |
|
p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); |
agelim=AGESUP;
|
gradg=ma3x(0,nhstepm,1,npar,1,nlstate); |
for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
|
gp=matrix(0,nhstepm,1,nlstate); |
/* nhstepm age range expressed in number of stepm */
|
gm=matrix(0,nhstepm,1,nlstate); |
nhstepm=(int) rint((agelim-age)*YEARM/stepm);
|
|
/* Typically if 20 years = 20*12/6=40 stepm */
|
for(theta=1; theta <=npar; theta++){ |
if (stepm >= YEARM) hstepm=1;
|
for(i=1; i<=npar; i++){ /* Computes gradient */ |
nhstepm = nhstepm/hstepm;/* Expressed in hstepm, typically 40/4=10 */
|
xp[i] = x[i] + (i==theta ?delti[theta]:0); |
p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
|
} |
/* Computed by stepm unit matrices, product of hstepm matrices, stored
|
hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm); |
in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
|
prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl); |
hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, ij);
|
for(j=1; j<= nlstate; j++){ |
|
for(h=0; h<=nhstepm; h++){ |
|
for(i=1, gp[h][j]=0.;i<=nlstate;i++) |
for(i=1; i<=nlstate;i++)
|
gp[h][j] += prlim[i][i]*p3mat[i][j][h]; |
for(j=1; j<=nlstate;j++)
|
} |
for (h=0, eij[i][j][(int)age]=0; h<=nhstepm; h++){
|
} |
eij[i][j][(int)age] +=p3mat[i][j][h];
|
|
}
|
for(i=1; i<=npar; i++) /* Computes gradient */ |
|
xp[i] = x[i] - (i==theta ?delti[theta]:0); |
hf=1;
|
hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm); |
if (stepm >= YEARM) hf=stepm/YEARM;
|
prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl); |
fprintf(ficreseij,"%.0f",age );
|
for(j=1; j<= nlstate; j++){ |
for(i=1; i<=nlstate;i++)
|
for(h=0; h<=nhstepm; h++){ |
for(j=1; j<=nlstate;j++){
|
for(i=1, gm[h][j]=0.;i<=nlstate;i++) |
fprintf(ficreseij," %.4f", hf*eij[i][j][(int)age]);
|
gm[h][j] += prlim[i][i]*p3mat[i][j][h]; |
}
|
} |
fprintf(ficreseij,"\n");
|
} |
free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
|
for(j=1; j<= nlstate; j++) |
}
|
for(h=0; h<=nhstepm; h++){ |
}
|
gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta]; |
|
} |
/************ Variance ******************/
|
} /* End theta */ |
void varevsij(char fileres[], double ***vareij, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int ij)
|
|
{
|
trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); |
/* Variance of health expectancies */
|
|
/* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
|
for(h=0; h<=nhstepm; h++) |
double **newm;
|
for(j=1; j<=nlstate;j++) |
double **dnewm,**doldm;
|
for(theta=1; theta <=npar; theta++) |
int i, j, nhstepm, hstepm, h;
|
trgradg[h][j][theta]=gradg[h][theta][j]; |
int k, cptcode;
|
|
double *xp;
|
for(i=1;i<=nlstate;i++) |
double **gp, **gm;
|
for(j=1;j<=nlstate;j++) |
double ***gradg, ***trgradg;
|
vareij[i][j][(int)age] =0.; |
double ***p3mat;
|
for(h=0;h<=nhstepm;h++){ |
double age,agelim;
|
for(k=0;k<=nhstepm;k++){ |
int theta;
|
matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov); |
|
matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]); |
fprintf(ficresvij,"# Covariances of life expectancies\n");
|
for(i=1;i<=nlstate;i++) |
fprintf(ficresvij,"# Age");
|
for(j=1;j<=nlstate;j++) |
for(i=1; i<=nlstate;i++)
|
vareij[i][j][(int)age] += doldm[i][j]; |
for(j=1; j<=nlstate;j++)
|
} |
fprintf(ficresvij," Cov(e%1d, e%1d)",i,j);
|
} |
fprintf(ficresvij,"\n");
|
h=1; |
|
if (stepm >= YEARM) h=stepm/YEARM; |
xp=vector(1,npar);
|
fprintf(ficresvij,"%.0f ",age ); |
dnewm=matrix(1,nlstate,1,npar);
|
for(i=1; i<=nlstate;i++) |
doldm=matrix(1,nlstate,1,nlstate);
|
for(j=1; j<=nlstate;j++){ |
|
fprintf(ficresvij," %.4f", h*vareij[i][j][(int)age]); |
hstepm=1*YEARM; /* Every year of age */
|
} |
hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
|
fprintf(ficresvij,"\n"); |
agelim = AGESUP;
|
free_matrix(gp,0,nhstepm,1,nlstate); |
for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
|
free_matrix(gm,0,nhstepm,1,nlstate); |
nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
|
free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate); |
if (stepm >= YEARM) hstepm=1;
|
free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar); |
nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
|
free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); |
p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
|
} /* End age */ |
gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
|
fclose(ficresvij); |
gp=matrix(0,nhstepm,1,nlstate);
|
free_vector(xp,1,npar); |
gm=matrix(0,nhstepm,1,nlstate);
|
free_matrix(doldm,1,nlstate,1,npar); |
|
free_matrix(dnewm,1,nlstate,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);
|
|
}
|
/************ Variance of prevlim ******************/ |
hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
|
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) |
prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
|
{ |
for(j=1; j<= nlstate; j++){
|
/* Variance of health expectancies */ |
for(h=0; h<=nhstepm; h++){
|
/* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/ |
for(i=1, gp[h][j]=0.;i<=nlstate;i++)
|
double **newm; |
gp[h][j] += prlim[i][i]*p3mat[i][j][h];
|
double **dnewm,**doldm; |
}
|
int i, j, nhstepm, hstepm; |
}
|
int k; |
|
FILE *ficresvpl; |
for(i=1; i<=npar; i++) /* Computes gradient */
|
char fileresvpl[FILENAMELENGTH]; |
xp[i] = x[i] - (i==theta ?delti[theta]:0);
|
double *xp; |
hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
|
double *gp, *gm; |
prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
|
double **gradg, **trgradg; |
for(j=1; j<= nlstate; j++){
|
double age,agelim; |
for(h=0; h<=nhstepm; h++){
|
int theta; |
for(i=1, gm[h][j]=0.;i<=nlstate;i++)
|
|
gm[h][j] += prlim[i][i]*p3mat[i][j][h];
|
strcpy(fileresvpl,"vpl"); |
}
|
strcat(fileresvpl,fileres); |
}
|
if((ficresvpl=fopen(fileresvpl,"w"))==NULL) { |
for(j=1; j<= nlstate; j++)
|
printf("Problem with variance prev lim resultfile: %s\n", fileresvpl); |
for(h=0; h<=nhstepm; h++){
|
exit(0); |
gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
|
} |
}
|
printf("Computing Variance-covariance of Prevalence limit: file '%s' \n", fileresvpl); |
} /* End theta */
|
|
|
|
trgradg =ma3x(0,nhstepm,1,nlstate,1,npar);
|
fprintf(ficresvpl,"# Standard deviation of prevalences limit\n"); |
|
fprintf(ficresvpl,"# Age"); |
for(h=0; h<=nhstepm; h++)
|
for(i=1; i<=nlstate;i++) |
for(j=1; j<=nlstate;j++)
|
fprintf(ficresvpl," %1d-%1d",i,i); |
for(theta=1; theta <=npar; theta++)
|
fprintf(ficresvpl,"\n"); |
trgradg[h][j][theta]=gradg[h][theta][j];
|
|
|
xp=vector(1,npar); |
for(i=1;i<=nlstate;i++)
|
dnewm=matrix(1,nlstate,1,npar); |
for(j=1;j<=nlstate;j++)
|
doldm=matrix(1,nlstate,1,nlstate); |
vareij[i][j][(int)age] =0.;
|
|
for(h=0;h<=nhstepm;h++){
|
hstepm=1*YEARM; /* Every year of age */ |
for(k=0;k<=nhstepm;k++){
|
hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */ |
matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
|
agelim = AGESUP; |
matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
|
for (age=bage; age<=fage; age ++){ /* If stepm=6 months */ |
for(i=1;i<=nlstate;i++)
|
nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */ |
for(j=1;j<=nlstate;j++)
|
if (stepm >= YEARM) hstepm=1; |
vareij[i][j][(int)age] += doldm[i][j];
|
nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */ |
}
|
gradg=matrix(1,npar,1,nlstate); |
}
|
gp=vector(1,nlstate); |
h=1;
|
gm=vector(1,nlstate); |
if (stepm >= YEARM) h=stepm/YEARM;
|
|
fprintf(ficresvij,"%.0f ",age );
|
for(theta=1; theta <=npar; theta++){ |
for(i=1; i<=nlstate;i++)
|
for(i=1; i<=npar; i++){ /* Computes gradient */ |
for(j=1; j<=nlstate;j++){
|
xp[i] = x[i] + (i==theta ?delti[theta]:0); |
fprintf(ficresvij," %.4f", h*vareij[i][j][(int)age]);
|
} |
}
|
prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl); |
fprintf(ficresvij,"\n");
|
for(i=1;i<=nlstate;i++) |
free_matrix(gp,0,nhstepm,1,nlstate);
|
gp[i] = prlim[i][i]; |
free_matrix(gm,0,nhstepm,1,nlstate);
|
|
free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
|
for(i=1; i<=npar; i++) /* Computes gradient */ |
free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
|
xp[i] = x[i] - (i==theta ?delti[theta]:0); |
free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
|
prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl); |
} /* End age */
|
for(i=1;i<=nlstate;i++) |
|
gm[i] = prlim[i][i]; |
free_vector(xp,1,npar);
|
|
free_matrix(doldm,1,nlstate,1,npar);
|
for(i=1;i<=nlstate;i++) |
free_matrix(dnewm,1,nlstate,1,nlstate);
|
gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta]; |
|
} /* End theta */ |
}
|
|
|
trgradg =matrix(1,nlstate,1,npar); |
/************ 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)
|
for(j=1; j<=nlstate;j++) |
{
|
for(theta=1; theta <=npar; theta++) |
/* Variance of prevalence limit */
|
trgradg[j][theta]=gradg[theta][j]; |
/* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
|
|
double **newm;
|
for(i=1;i<=nlstate;i++) |
double **dnewm,**doldm;
|
varpl[i][(int)age] =0.; |
int i, j, nhstepm, hstepm;
|
matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov); |
int k, cptcode;
|
matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg); |
double *xp;
|
for(i=1;i<=nlstate;i++) |
double *gp, *gm;
|
varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */ |
double **gradg, **trgradg;
|
|
double age,agelim;
|
fprintf(ficresvpl,"%.0f ",age ); |
int theta;
|
for(i=1; i<=nlstate;i++) |
|
fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age])); |
fprintf(ficresvpl,"# Standard deviation of prevalences limit\n");
|
fprintf(ficresvpl,"\n"); |
fprintf(ficresvpl,"# Age");
|
free_vector(gp,1,nlstate); |
for(i=1; i<=nlstate;i++)
|
free_vector(gm,1,nlstate); |
fprintf(ficresvpl," %1d-%1d",i,i);
|
free_matrix(gradg,1,npar,1,nlstate); |
fprintf(ficresvpl,"\n");
|
free_matrix(trgradg,1,nlstate,1,npar); |
|
} /* End age */ |
xp=vector(1,npar);
|
fclose(ficresvpl); |
dnewm=matrix(1,nlstate,1,npar);
|
free_vector(xp,1,npar); |
doldm=matrix(1,nlstate,1,nlstate);
|
free_matrix(doldm,1,nlstate,1,npar); |
|
free_matrix(dnewm,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 = AGESUP;
|
|
for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
|
|
nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
|
|
if (stepm >= YEARM) hstepm=1;
|
/***********************************************/ |
nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
|
/**************** Main Program *****************/ |
gradg=matrix(1,npar,1,nlstate);
|
/***********************************************/ |
gp=vector(1,nlstate);
|
|
gm=vector(1,nlstate);
|
/*int main(int argc, char *argv[])*/ |
|
int main() |
for(theta=1; theta <=npar; theta++){
|
{ |
for(i=1; i<=npar; i++){ /* Computes gradient */
|
|
xp[i] = x[i] + (i==theta ?delti[theta]:0);
|
int i,j, k, n=MAXN,iter,m,size; |
}
|
double agedeb, agefin,hf; |
prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
|
double agemin=1.e20, agemax=-1.e20; |
for(i=1;i<=nlstate;i++)
|
|
gp[i] = prlim[i][i];
|
double fret; |
|
double **xi,tmp,delta; |
for(i=1; i<=npar; i++) /* Computes gradient */
|
|
xp[i] = x[i] - (i==theta ?delti[theta]:0);
|
double dum; /* Dummy variable */ |
prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ij);
|
double ***p3mat; |
for(i=1;i<=nlstate;i++)
|
int *indx; |
gm[i] = prlim[i][i];
|
char line[MAXLINE], linepar[MAXLINE]; |
|
char title[MAXLINE]; |
for(i=1;i<=nlstate;i++)
|
char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH]; |
gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
|
char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH]; |
} /* End theta */
|
char filerest[FILENAMELENGTH]; |
|
char fileregp[FILENAMELENGTH]; |
trgradg =matrix(1,nlstate,1,npar);
|
char path[80],pathc[80],pathcd[80],pathtot[80]; |
|
int firstobs=1, lastobs=10; |
for(j=1; j<=nlstate;j++)
|
int sdeb, sfin; /* Status at beginning and end */ |
for(theta=1; theta <=npar; theta++)
|
int c, h , cpt,l; |
trgradg[j][theta]=gradg[theta][j];
|
int ju,jl, mi; |
|
int i1,j1, k1,jk,aa,bb, stepsize; |
for(i=1;i<=nlstate;i++)
|
int jnais,jdc,jint4,jint1,jint2,jint3,**outcome,**adl,*tab; |
varpl[i][(int)age] =0.;
|
|
matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
|
int hstepm, nhstepm; |
matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
|
double bage, fage, age, agelim, agebase; |
for(i=1;i<=nlstate;i++)
|
double ftolpl=FTOL; |
varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
|
double **prlim; |
|
double *severity; |
fprintf(ficresvpl,"%.0f ",age );
|
double ***param; /* Matrix of parameters */ |
for(i=1; i<=nlstate;i++)
|
double *p; |
fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
|
double **matcov; /* Matrix of covariance */ |
fprintf(ficresvpl,"\n");
|
double ***delti3; /* Scale */ |
free_vector(gp,1,nlstate);
|
double *delti; /* Scale */ |
free_vector(gm,1,nlstate);
|
double ***eij, ***vareij; |
free_matrix(gradg,1,npar,1,nlstate);
|
double **varpl; /* Variances of prevalence limits by age */ |
free_matrix(trgradg,1,nlstate,1,npar);
|
double *epj, vepp; |
} /* End age */
|
char version[80]="Imach version 0.64, May 2000, INED-EUROREVES "; |
|
char *alph[]={"a","a","b","c","d","e"}, str[4]; |
free_vector(xp,1,npar);
|
char z[1]="c"; |
free_matrix(doldm,1,nlstate,1,npar);
|
#include <sys/time.h> |
free_matrix(dnewm,1,nlstate,1,nlstate);
|
#include <time.h> |
|
|
}
|
/* long total_usecs; |
|
struct timeval start_time, end_time; |
|
|
|
gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */ |
/***********************************************/
|
|
/**************** Main Program *****************/
|
|
/***********************************************/
|
printf("\nIMACH, Version 0.64"); |
|
printf("\nEnter the parameter file name: "); |
/*int main(int argc, char *argv[])*/
|
#define windows 1 |
int main()
|
#ifdef windows |
{
|
scanf("%s",pathtot); |
|
getcwd(pathcd, size); |
int i,j, k, n=MAXN,iter,m,size,cptcode, aaa, cptcod;
|
cut(path,optionfile,pathtot); |
double agedeb, agefin,hf;
|
chdir(path); |
double agemin=1.e20, agemax=-1.e20;
|
replace(pathc,path); |
|
#endif |
double fret;
|
#ifdef unix |
double **xi,tmp,delta;
|
scanf("%s",optionfile); |
|
#endif |
double dum; /* Dummy variable */
|
|
double ***p3mat;
|
/*-------- arguments in the command line --------*/ |
int *indx;
|
|
char line[MAXLINE], linepar[MAXLINE];
|
strcpy(fileres,"r"); |
char title[MAXLINE];
|
strcat(fileres, optionfile); |
char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH];
|
|
char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filereso[FILENAMELENGTH];
|
/*---------arguments file --------*/ |
char filerest[FILENAMELENGTH];
|
|
char fileregp[FILENAMELENGTH];
|
if((ficpar=fopen(optionfile,"r"))==NULL) { |
char path[80],pathc[80],pathcd[80],pathtot[80],model[20];
|
printf("Problem with optionfile %s\n",optionfile); |
int firstobs=1, lastobs=10;
|
goto end; |
int sdeb, sfin; /* Status at beginning and end */
|
} |
int c, h , cpt,l;
|
|
int ju,jl, mi;
|
strcpy(filereso,"o"); |
int i1,j1, k1,jk,aa,bb, stepsize;
|
strcat(filereso,fileres); |
int jnais,jdc,jint4,jint1,jint2,jint3,**outcome,**adl,*tab;
|
if((ficparo=fopen(filereso,"w"))==NULL) { |
|
printf("Problem with Output resultfile: %s\n", filereso);goto end; |
int hstepm, nhstepm;
|
} |
double bage, fage, age, agelim, agebase;
|
|
double ftolpl=FTOL;
|
/*--------- index.htm --------*/ |
double **prlim;
|
|
double *severity;
|
if((fichtm=fopen("index.htm","w"))==NULL) { |
double ***param; /* Matrix of parameters */
|
printf("Problem with index.htm \n");goto end; |
double *p;
|
} |
double **matcov; /* Matrix of covariance */
|
|
double ***delti3; /* Scale */
|
fprintf(fichtm,"<body><ul><li>Outputs files<br><br>\n |
double *delti; /* Scale */
|
- Observed prevalence in each state: <a href=\"p%s\">p%s</a> <br>\n |
double ***eij, ***vareij;
|
- Estimated parameters and the covariance matrix: <a href=\"%s\">%s</a> <br> |
double **varpl; /* Variances of prevalence limits by age */
|
- Stationary prevalence in each state: <a href=\"pl%s\">pl%s</a> <br> |
double *epj, vepp;
|
- Transition probabilities: <a href=\"pij%s\">pij%s</a><br> |
char version[80]="Imach version 62c, May 1999, INED-EUROREVES ";
|
- Copy of the parameter file: <a href=\"o%s\">o%s</a><br> |
char *alph[]={"a","a","b","c","d","e"}, str[4];
|
- Life expectancies by age and initial health status: <a href=\"e%s\">e%s</a> <br> |
char z[1]="c", occ;
|
- Variances of life expectancies by age and initial health status: <a href=\"v%s\">v%s</a><br> |
#include <sys/time.h>
|
- Health expectancies with their variances: <a href=\"t%s\">t%s</a> <br> |
#include <time.h>
|
- Standard deviation of stationary prevalences: <a href=\"vpl%s\">vpl%s</a> <br><br>",fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres); |
char stra[80], strb[80], strc[80], strd[80],stre[80],modelsav[80];
|
|
/* long total_usecs;
|
fprintf(fichtm," <li>Graphs<br> <br>"); |
struct timeval start_time, end_time;
|
|
|
for(cpt=1; cpt<nlstate;cpt++) |
gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
|
fprintf(fichtm,"- Prevalence of disability: p%s1.gif<br> |
|
<img src=\"p%s1.gif\"><br>",strtok(optionfile, "."),strtok(optionfile, "."),cpt); |
|
for(cpt=1; cpt<=nlstate;cpt++) |
printf("\nIMACH, Version 0.63");
|
fprintf(fichtm,"- Observed and stationary prevalence (with confident |
printf("\nEnter the parameter file name: ");
|
interval) in state (%d): v%s%d.gif <br> |
|
<img src=\"v%s%d.gif\"><br>",cpt,strtok(optionfile, "."),cpt,strtok(optionfile, "."),cpt); |
#ifdef windows
|
|
scanf("%s",pathtot);
|
for(cpt=1; cpt<=nlstate;cpt++) |
getcwd(pathcd, size);
|
fprintf(fichtm,"- Health life expectancies by age and initial health state (%d): exp%s%d.gif <br> |
cutv(path,optionfile,pathtot,'\\');
|
<img src=\"ex%s%d.gif\"><br>",cpt,strtok(optionfile, "."),cpt,strtok(optionfile, "."),cpt); |
chdir(path);
|
|
replace(pathc,path);
|
fprintf(fichtm,"- Total life expectancy by age and |
#endif
|
health expectancies in states (1) and (2): e%s.gif<br> |
#ifdef unix
|
<img src=\"e%s.gif\"></li> </ul></body>",strtok(optionfile, "."),strtok(optionfile, ".")); |
scanf("%s",optionfile);
|
|
#endif
|
|
|
fclose(fichtm); |
/*-------- arguments in the command line --------*/
|
|
|
/* Reads comments: lines beginning with '#' */ |
strcpy(fileres,"r");
|
while((c=getc(ficpar))=='#' && c!= EOF){ |
strcat(fileres, optionfile);
|
ungetc(c,ficpar); |
|
fgets(line, MAXLINE, ficpar); |
/*---------arguments file --------*/
|
puts(line); |
|
fputs(line,ficparo); |
if((ficpar=fopen(optionfile,"r"))==NULL) {
|
} |
printf("Problem with optionfile %s\n",optionfile);
|
ungetc(c,ficpar); |
goto end;
|
|
}
|
fscanf(ficpar,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%lf stepm=%d ncov=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\n",title, datafile, &lastobs, &firstpass,&lastpass,&ftol, &stepm, &ncov, &nlstate,&ndeath, &maxwav, &mle, &weightopt); |
|
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\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncov, nlstate,ndeath, maxwav, mle, weightopt); |
strcpy(filereso,"o");
|
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\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncov,nlstate,ndeath,maxwav, mle, weightopt); |
strcat(filereso,fileres);
|
|
if((ficparo=fopen(filereso,"w"))==NULL) {
|
nvar=ncov-1; /* Suppressing age as a basic covariate */ |
printf("Problem with Output resultfile: %s\n", filereso);goto end;
|
|
}
|
/* Read guess parameters */ |
|
/* Reads comments: lines beginning with '#' */ |
/* Reads comments: lines beginning with '#' */
|
while((c=getc(ficpar))=='#' && c!= EOF){ |
while((c=getc(ficpar))=='#' && c!= EOF){
|
ungetc(c,ficpar); |
ungetc(c,ficpar);
|
fgets(line, MAXLINE, ficpar); |
fgets(line, MAXLINE, ficpar);
|
puts(line); |
puts(line);
|
fputs(line,ficparo); |
fputs(line,ficparo);
|
} |
}
|
ungetc(c,ficpar); |
ungetc(c,ficpar);
|
|
|
param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncov); |
fscanf(ficpar,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%lf stepm=%d ncov=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=%s\n",title, datafile, &lastobs, &firstpass,&lastpass,&ftol, &stepm, &ncov, &nlstate,&ndeath, &maxwav, &mle, &weightopt,model);
|
for(i=1; i <=nlstate; i++) |
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);
|
for(j=1; j <=nlstate+ndeath-1; j++){ |
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);
|
fscanf(ficpar,"%1d%1d",&i1,&j1); |
|
fprintf(ficparo,"%1d%1d",i1,j1); |
covar=matrix(1,NCOVMAX,1,n);
|
printf("%1d%1d",i,j); |
if (strlen(model)<=1) cptcovn=0;
|
for(k=1; k<=ncov;k++){ |
else {
|
fscanf(ficpar," %lf",¶m[i][j][k]); |
j=0;
|
printf(" %lf",param[i][j][k]); |
j=nbocc(model,'+');
|
fprintf(ficparo," %lf",param[i][j][k]); |
cptcovn=j+1;
|
} |
}
|
fscanf(ficpar,"\n"); |
|
printf("\n"); |
ncovmodel=2+cptcovn;
|
fprintf(ficparo,"\n"); |
nvar=ncovmodel-1; /* Suppressing age as a basic covariate */
|
} |
|
|
/* Read guess parameters */
|
npar= (nlstate+ndeath-1)*nlstate*ncov; |
/* Reads comments: lines beginning with '#' */
|
p=param[1][1]; |
while((c=getc(ficpar))=='#' && c!= EOF){
|
|
ungetc(c,ficpar);
|
/* Reads comments: lines beginning with '#' */ |
fgets(line, MAXLINE, ficpar);
|
while((c=getc(ficpar))=='#' && c!= EOF){ |
puts(line);
|
ungetc(c,ficpar); |
fputs(line,ficparo);
|
fgets(line, MAXLINE, ficpar); |
}
|
puts(line); |
ungetc(c,ficpar);
|
fputs(line,ficparo); |
|
} |
param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
|
ungetc(c,ficpar); |
for(i=1; i <=nlstate; i++)
|
|
for(j=1; j <=nlstate+ndeath-1; j++){
|
delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncov); |
fscanf(ficpar,"%1d%1d",&i1,&j1);
|
delti=vector(1,npar); /* Scale of each paramater (output from hesscov) */ |
fprintf(ficparo,"%1d%1d",i1,j1);
|
for(i=1; i <=nlstate; i++){ |
printf("%1d%1d",i,j);
|
for(j=1; j <=nlstate+ndeath-1; j++){ |
for(k=1; k<=ncovmodel;k++){
|
fscanf(ficpar,"%1d%1d",&i1,&j1); |
fscanf(ficpar," %lf",¶m[i][j][k]);
|
printf("%1d%1d",i,j); |
printf(" %lf",param[i][j][k]);
|
fprintf(ficparo,"%1d%1d",i1,j1); |
fprintf(ficparo," %lf",param[i][j][k]);
|
for(k=1; k<=ncov;k++){ |
}
|
fscanf(ficpar,"%le",&delti3[i][j][k]); |
fscanf(ficpar,"\n");
|
printf(" %le",delti3[i][j][k]); |
printf("\n");
|
fprintf(ficparo," %le",delti3[i][j][k]); |
fprintf(ficparo,"\n");
|
} |
}
|
fscanf(ficpar,"\n"); |
|
printf("\n"); |
npar= (nlstate+ndeath-1)*nlstate*ncovmodel;
|
fprintf(ficparo,"\n"); |
p=param[1][1];
|
} |
|
} |
/* Reads comments: lines beginning with '#' */
|
delti=delti3[1][1]; |
while((c=getc(ficpar))=='#' && c!= EOF){
|
|
ungetc(c,ficpar);
|
/* Reads comments: lines beginning with '#' */ |
fgets(line, MAXLINE, ficpar);
|
while((c=getc(ficpar))=='#' && c!= EOF){ |
puts(line);
|
ungetc(c,ficpar); |
fputs(line,ficparo);
|
fgets(line, MAXLINE, ficpar); |
}
|
puts(line); |
ungetc(c,ficpar);
|
fputs(line,ficparo); |
|
} |
delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
|
ungetc(c,ficpar); |
delti=vector(1,npar); /* Scale of each paramater (output from hesscov) */
|
|
for(i=1; i <=nlstate; i++){
|
matcov=matrix(1,npar,1,npar); |
for(j=1; j <=nlstate+ndeath-1; j++){
|
for(i=1; i <=npar; i++){ |
fscanf(ficpar,"%1d%1d",&i1,&j1);
|
fscanf(ficpar,"%s",&str); |
printf("%1d%1d",i,j);
|
printf("%s",str); |
fprintf(ficparo,"%1d%1d",i1,j1);
|
fprintf(ficparo,"%s",str); |
for(k=1; k<=ncovmodel;k++){
|
for(j=1; j <=i; j++){ |
fscanf(ficpar,"%le",&delti3[i][j][k]);
|
fscanf(ficpar," %le",&matcov[i][j]); |
printf(" %le",delti3[i][j][k]);
|
printf(" %.5le",matcov[i][j]); |
fprintf(ficparo," %le",delti3[i][j][k]);
|
fprintf(ficparo," %.5le",matcov[i][j]); |
}
|
} |
fscanf(ficpar,"\n");
|
fscanf(ficpar,"\n"); |
printf("\n");
|
printf("\n"); |
fprintf(ficparo,"\n");
|
fprintf(ficparo,"\n"); |
}
|
} |
}
|
for(i=1; i <=npar; i++) |
delti=delti3[1][1];
|
for(j=i+1;j<=npar;j++) |
|
matcov[i][j]=matcov[j][i]; |
/* Reads comments: lines beginning with '#' */
|
|
while((c=getc(ficpar))=='#' && c!= EOF){
|
printf("\n"); |
ungetc(c,ficpar);
|
|
fgets(line, MAXLINE, ficpar);
|
|
puts(line);
|
if(mle==1){ |
fputs(line,ficparo);
|
/*-------- data file ----------*/ |
}
|
if((ficres =fopen(fileres,"w"))==NULL) { |
ungetc(c,ficpar);
|
printf("Problem with resultfile: %s\n", fileres);goto end; |
|
} |
matcov=matrix(1,npar,1,npar);
|
fprintf(ficres,"#%s\n",version); |
for(i=1; i <=npar; i++){
|
|
fscanf(ficpar,"%s",&str);
|
if((fic=fopen(datafile,"r"))==NULL) { |
printf("%s",str);
|
printf("Problem with datafile: %s\n", datafile);goto end; |
fprintf(ficparo,"%s",str);
|
} |
for(j=1; j <=i; j++){
|
|
fscanf(ficpar," %le",&matcov[i][j]);
|
n= lastobs; |
printf(" %.5le",matcov[i][j]);
|
severity = vector(1,maxwav); |
fprintf(ficparo," %.5le",matcov[i][j]);
|
outcome=imatrix(1,maxwav+1,1,n); |
}
|
num=ivector(1,n); |
fscanf(ficpar,"\n");
|
moisnais=vector(1,n); |
printf("\n");
|
annais=vector(1,n); |
fprintf(ficparo,"\n");
|
moisdc=vector(1,n); |
}
|
andc=vector(1,n); |
for(i=1; i <=npar; i++)
|
agedc=vector(1,n); |
for(j=i+1;j<=npar;j++)
|
cod=ivector(1,n); |
matcov[i][j]=matcov[j][i];
|
weight=vector(1,n); |
|
for(i=1;i<=n;i++) weight[i]=1.0; /* Equal weights, 1 by default */ |
printf("\n");
|
mint=matrix(1,maxwav,1,n); |
|
anint=matrix(1,maxwav,1,n); |
|
covar=matrix(1,NCOVMAX,1,n); |
if(mle==1){
|
s=imatrix(1,maxwav+1,1,n); |
/*-------- data file ----------*/
|
adl=imatrix(1,maxwav+1,1,n); |
if((ficres =fopen(fileres,"w"))==NULL) {
|
tab=ivector(1,NCOVMAX); |
printf("Problem with resultfile: %s\n", fileres);goto end;
|
i=1; |
}
|
while (fgets(line, MAXLINE, fic) != NULL) { |
fprintf(ficres,"#%s\n",version);
|
if ((i >= firstobs) && (i <lastobs)) { |
|
sscanf(line,"%d %lf %lf %lf %lf/%lf %lf/%lf %lf/%lf %d %lf/%lf %d %lf/%lf %d %lf/%lf %d", &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]); |
if((fic=fopen(datafile,"r"))==NULL) {
|
i=i+1; |
printf("Problem with datafile: %s\n", datafile);goto end;
|
} |
}
|
} |
|
imx=i-1; /* Number of individuals */ |
n= lastobs;
|
|
severity = vector(1,maxwav);
|
fclose(fic); |
outcome=imatrix(1,maxwav+1,1,n);
|
|
num=ivector(1,n);
|
if (weightopt != 1) { /* Maximisation without weights*/ |
moisnais=vector(1,n);
|
for(i=1;i<=n;i++) weight[i]=1.0; |
annais=vector(1,n);
|
} |
moisdc=vector(1,n);
|
/*-calculation of age at interview from date of interview and age at death -*/ |
andc=vector(1,n);
|
agev=matrix(1,maxwav,1,imx); |
agedc=vector(1,n);
|
|
cod=ivector(1,n);
|
for (i=1; i<=imx; i++) { |
weight=vector(1,n);
|
agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]); |
for(i=1;i<=n;i++) weight[i]=1.0; /* Equal weights, 1 by default */
|
for(m=1; (m<= maxwav); m++){ |
mint=matrix(1,maxwav,1,n);
|
if(s[m][i] >0){ |
anint=matrix(1,maxwav,1,n);
|
if (s[m][i] == nlstate+1) { |
s=imatrix(1,maxwav+1,1,n);
|
if(agedc[i]>0) |
adl=imatrix(1,maxwav+1,1,n);
|
agev[m][i]=agedc[i]; |
tab=ivector(1,NCOVMAX);
|
else{ |
ncodemax=ivector(1,NCOVMAX);
|
printf("Warning negative age at death: %d line:%d\n",num[i],i); |
|
agev[m][i]=-1; |
i=1;
|
} |
while (fgets(line, MAXLINE, fic) != NULL) {
|
} |
if ((i >= firstobs) && (i <=lastobs)) {
|
else if(s[m][i] !=9){ /* Should no more exist */ |
|
agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]); |
for (j=maxwav;j>=1;j--){
|
if(mint[m][i]==99 || anint[m][i]==9999) |
cutv(stra, strb,line,' '); s[j][i]=atoi(strb);
|
agev[m][i]=1; |
strcpy(line,stra);
|
else if(agev[m][i] <agemin){ |
cutv(stra, strb,line,'/'); anint[j][i]=(double)(atoi(strb)); strcpy(line,stra);
|
agemin=agev[m][i]; |
cutv(stra, strb,line,' '); mint[j][i]=(double)(atoi(strb)); strcpy(line,stra);
|
/*printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], agemin);*/ |
}
|
} |
|
else if(agev[m][i] >agemax){ |
cutv(stra, strb,line,'/'); andc[i]=(double)(atoi(strb)); strcpy(line,stra);
|
agemax=agev[m][i]; |
cutv(stra, strb,line,' '); moisdc[i]=(double)(atoi(strb)); strcpy(line,stra);
|
/* printf(" anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.0f\n",m,i,anint[m][i], i,annais[i], agemax);*/ |
|
} |
cutv(stra, strb,line,'/'); annais[i]=(double)(atoi(strb)); strcpy(line,stra);
|
/*agev[m][i]=anint[m][i]-annais[i];*/ |
cutv(stra, strb,line,' '); moisnais[i]=(double)(atoi(strb)); strcpy(line,stra);
|
/* agev[m][i] = age[i]+2*m;*/ |
|
} |
cutv(stra, strb,line,' '); weight[i]=(double)(atoi(strb)); strcpy(line,stra);
|
else { /* =9 */ |
for (j=ncov;j>=1;j--){
|
agev[m][i]=1; |
cutv(stra, strb,line,' '); covar[j][i]=(double)(atoi(strb)); strcpy(line,stra);
|
s[m][i]=-1; |
}
|
} |
num[i]=atol(stra);
|
} |
|
else /*= 0 Unknown */ |
/* 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]));*/
|
agev[m][i]=1; |
|
} |
/*printf("%d %.lf %.lf %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]),(covar[3][i]), (covar[4][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]));*/
|
|
|
} |
i=i+1;
|
for (i=1; i<=imx; i++) { |
}
|
for(m=1; (m<= maxwav); m++){ |
}
|
if (s[m][i] > (nlstate+ndeath)) { |
/*scanf("%d",i);*/
|
printf("Error: Wrong value in nlstate or ndeath\n"); |
|
goto end; |
imx=i-1; /* Number of individuals */
|
} |
|
} |
/* Calculation of the number of parameter from char model*/
|
} |
Tvar=ivector(1,8);
|
|
|
printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, agemin, agemax); |
if (strlen(model) >1){
|
|
j=0;
|
free_vector(severity,1,maxwav); |
j=nbocc(model,'+');
|
free_imatrix(outcome,1,maxwav+1,1,n); |
cptcovn=j+1;
|
free_vector(moisnais,1,n); |
|
free_vector(annais,1,n); |
strcpy(modelsav,model);
|
free_matrix(mint,1,maxwav,1,n); |
if (j==0) {
|
free_matrix(anint,1,maxwav,1,n); |
cutv(stra,strb,modelsav,'V'); Tvar[1]=atoi(strb);
|
free_vector(moisdc,1,n); |
}
|
free_vector(andc,1,n); |
else {
|
|
for(i=j; i>=1;i--){
|
|
cutv(stra,strb,modelsav,'+');
|
wav=ivector(1,imx); |
if (strchr(strb,'*')) {
|
dh=imatrix(1,lastpass-firstpass+1,1,imx); |
cutv(strd,strc,strb,'*');
|
mw=imatrix(1,lastpass-firstpass+1,1,imx); |
cutv(strb,stre,strc,'V');Tvar[i+1]=ncov+1;
|
|
cutv(strb,strc,strd,'V');
|
/* Concatenates waves */ |
for (k=1; k<=lastobs;k++)
|
concatwav(wav, dh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm); |
covar[ncov+1][k]=covar[atoi(stre)][k]*covar[atoi(strc)][k];
|
|
}
|
/* Calculates basic frequencies. Computes observed prevalence at single age |
else {
|
and prints on file fileres'p'. */ |
cutv(strd,strc,strb,'V');
|
freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx); |
Tvar[i+1]=atoi(strc);
|
|
}
|
pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */ |
strcpy(modelsav,stra);
|
oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */ |
}
|
newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */ |
/*cutv(strd,strc,stra,'V');*/
|
savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */ |
Tvar[1]=atoi(strc);
|
oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */ |
}
|
|
}
|
/* For Powell, parameters are in a vector p[] starting at p[1] |
/*printf("tvar=%d ",Tvar[1]);*/
|
so we point p on param[1][1] so that p[1] maps on param[1][1][1] */ |
/*scanf("%d ",i);*/
|
p=param[1][1]; /* *(*(*(param +1)+1)+0) */ |
fclose(fic);
|
|
|
mlikeli(ficres,p, npar, ncov, nlstate, ftol, func); |
if (weightopt != 1) { /* Maximisation without weights*/
|
|
for(i=1;i<=n;i++) weight[i]=1.0;
|
|
}
|
/*--------- results files --------------*/ |
/*-calculation of age at interview from date of interview and age at death -*/
|
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\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncov, nlstate, ndeath, maxwav, mle,weightopt); |
agev=matrix(1,maxwav,1,imx);
|
|
|
jk=1; |
for (i=1; i<=imx; i++) {
|
fprintf(ficres,"# Parameters\n"); |
agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
|
printf("# Parameters\n"); |
for(m=1; (m<= maxwav); m++){
|
for(i=1,jk=1; i <=nlstate; i++){ |
if (mint[m][i]==99 || anint[m][i]==9999) s[m][i]=-1;
|
for(k=1; k <=(nlstate+ndeath); k++){ |
if(s[m][i] >0){
|
if (k != i) |
if (s[m][i] == nlstate+1) {
|
{ |
if(agedc[i]>0)
|
printf("%d%d ",i,k); |
if(moisdc[i]!=99 && andc[i]!=9999)
|
fprintf(ficres,"%1d%1d ",i,k); |
agev[m][i]=agedc[i];
|
for(j=1; j <=ncov; j++){ |
else{
|
printf("%f ",p[jk]); |
printf("Warning negative age at death: %d line:%d\n",num[i],i);
|
fprintf(ficres,"%f ",p[jk]); |
agev[m][i]=-1;
|
jk++; |
}
|
} |
}
|
printf("\n"); |
else if(s[m][i] !=9){ /* Should no more exist */
|
fprintf(ficres,"\n"); |
agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
|
} |
if(mint[m][i]==99 || anint[m][i]==9999){
|
} |
agev[m][i]=1;
|
} |
/* printf("i=%d m=%d agev=%lf \n",i,m, agev[m][i]); */
|
|
}
|
/* Computing hessian and covariance matrix */ |
else if(agev[m][i] <agemin){
|
ftolhess=ftol; /* Usually correct */ |
agemin=agev[m][i];
|
hesscov(matcov, p, npar, delti, ftolhess, func); |
/*printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], agemin);*/
|
fprintf(ficres,"# Scales\n"); |
}
|
printf("# Scales\n"); |
else if(agev[m][i] >agemax){
|
for(i=1,jk=1; i <=nlstate; i++){ |
agemax=agev[m][i];
|
for(j=1; j <=nlstate+ndeath; j++){ |
/* printf(" anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.0f\n",m,i,anint[m][i], i,annais[i], agemax);*/
|
if (j!=i) { |
}
|
fprintf(ficres,"%1d%1d",i,j); |
/*agev[m][i]=anint[m][i]-annais[i];*/
|
printf("%1d%1d",i,j); |
/* agev[m][i] = age[i]+2*m;*/
|
for(k=1; k<=ncov;k++){ |
}
|
printf(" %.5e",delti[jk]); |
else { /* =9 */
|
fprintf(ficres," %.5e",delti[jk]); |
agev[m][i]=1;
|
jk++; |
s[m][i]=-1;
|
} |
}
|
printf("\n"); |
}
|
fprintf(ficres,"\n"); |
else /*= 0 Unknown */
|
} |
agev[m][i]=1;
|
} |
}
|
} |
|
|
}
|
k=1; |
for (i=1; i<=imx; i++) {
|
fprintf(ficres,"# Covariance\n"); |
for(m=1; (m<= maxwav); m++){
|
printf("# Covariance\n"); |
if (s[m][i] > (nlstate+ndeath)) {
|
for(i=1;i<=npar;i++){ |
printf("Error: Wrong value in nlstate or ndeath\n");
|
/* if (k>nlstate) k=1; |
goto end;
|
i1=(i-1)/(ncov*nlstate)+1; |
}
|
fprintf(ficres,"%s%d%d",alph[k],i1,tab[i]); |
}
|
printf("%s%d%d",alph[k],i1,tab[i]);*/ |
}
|
fprintf(ficres,"%3d",i); |
|
printf("%3d",i); |
printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, agemin, agemax);
|
for(j=1; j<=i;j++){ |
|
fprintf(ficres," %.5e",matcov[i][j]); |
free_vector(severity,1,maxwav);
|
printf(" %.5e",matcov[i][j]); |
free_imatrix(outcome,1,maxwav+1,1,n);
|
} |
free_vector(moisnais,1,n);
|
fprintf(ficres,"\n"); |
free_vector(annais,1,n);
|
printf("\n"); |
free_matrix(mint,1,maxwav,1,n);
|
k++; |
free_matrix(anint,1,maxwav,1,n);
|
} |
free_vector(moisdc,1,n);
|
|
free_vector(andc,1,n);
|
while((c=getc(ficpar))=='#' && c!= EOF){ |
|
ungetc(c,ficpar); |
|
fgets(line, MAXLINE, ficpar); |
wav=ivector(1,imx);
|
puts(line); |
dh=imatrix(1,lastpass-firstpass+1,1,imx);
|
fputs(line,ficparo); |
mw=imatrix(1,lastpass-firstpass+1,1,imx);
|
} |
|
ungetc(c,ficpar); |
/* Concatenates waves */
|
|
concatwav(wav, dh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
|
fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf\n",&agemin,&agemax, &bage, &fage); |
|
|
|
if (fage <= 2) { |
Tcode=ivector(1,100);
|
bage = agemin; |
nbcode=imatrix(1,nvar,1,8);
|
fage = agemax; |
ncodemax[1]=1;
|
} |
if (cptcovn > 0) tricode(Tvar,nbcode,imx);
|
|
|
fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n"); |
codtab=imatrix(1,100,1,10);
|
fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax,bage,fage); |
h=0;
|
/*------------ gnuplot -------------*/ |
m=pow(2,cptcovn);
|
chdir(pathcd); |
|
if((ficgp=fopen("graph.gp","w"))==NULL) { |
for(k=1;k<=cptcovn; k++){
|
printf("Problem with file graph.gp");goto end; |
for(i=1; i <=(m/pow(2,k));i++){
|
} |
for(j=1; j <= ncodemax[k]; j++){
|
#ifdef windows |
for(cpt=1; cpt <=(m/pow(2,cptcovn+1-k)); cpt++){
|
fprintf(ficgp,"cd \"%s\" \n",pathc); |
h++;
|
#endif |
if (h>m) h=1;codtab[h][k]=j;
|
/* 1eme*/ |
}
|
|
}
|
for (cpt=1; cpt<= nlstate ; cpt ++) { |
}
|
#ifdef windows |
}
|
fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter gif small size 400,300\nplot [%.f:%.f] \"vpl%s\" u 1:%d \"\%%lf",agemin,fage,fileres,cpt*2); |
|
#endif |
/*for(i=1; i <=m ;i++){
|
#ifdef unix |
for(k=1; k <=cptcovn; k++){
|
fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nplot [%.f:%.f] \"vpl%s\" u 1:%d \"\%%lf",agemin,fage,fileres,cpt*2); |
printf("i=%d k=%d %d ",i,k,codtab[i][k]);
|
#endif |
}
|
for (i=1; i<= nlstate ; i ++) fprintf(ficgp," \%%lf (\%%lf)"); |
printf("\n");
|
fprintf(ficgp,"\" t\"Stationary prevalence\" w l 0,\"vpl%s\" u 1:($%d+2*$%d) \"\%%lf",fileres,2*cpt,cpt*2+1); |
}
|
for (i=1; i<= nlstate ; i ++) fprintf(ficgp," \%%lf (\%%lf)"); |
scanf("%d",i);*/
|
fprintf(ficgp,"\" t\"95\%% CI\" w l 1,\"vpl%s\" u 1:($%d-2*$%d) \"\%%lf",fileres,2*cpt,2*cpt+1); |
|
for (i=1; i<= nlstate ; i ++) fprintf(ficgp," \%%lf (\%%lf)"); |
/* Calculates basic frequencies. Computes observed prevalence at single age
|
fprintf(ficgp,"\" t\"\" w l 1,\"p%s\" u 1:($%d) t\"Observed prevalence \" w l 2",fileres,2+4*(cpt-1)); |
and prints on file fileres'p'. */
|
#ifdef unix |
freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvar,nbcode, ncodemax);
|
fprintf(ficgp,"\nset ter gif small size 400,300"); |
|
#endif |
pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
|
fprintf(ficgp,"\nset out \"v%s%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt); |
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 */
|
/*2 eme*/ |
oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
|
|
|
fprintf(ficgp,"set ylabel \"Years\" \nset ter gif small size 400,300\nplot [%.f:%.f] ",agemin,fage); |
/* For Powell, parameters are in a vector p[] starting at p[1]
|
for (i=1; i<= nlstate+1 ; i ++) { |
so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
|
k=2*i; |
p=param[1][1]; /* *(*(*(param +1)+1)+0) */
|
fprintf(ficgp,"\"t%s\" u 1:%d \"\%%lf \%%lf (\%%lf) \%%lf (\%%lf)",fileres,k); |
/*scanf("%d",i);*/
|
for (j=1; j< nlstate ; j ++) fprintf(ficgp," \%%lf (\%%lf)"); |
mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
|
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\" u 1:($%d-2*$%d) \"\%%lf \%%lf (\%%lf) \%%lf (\%%lf)",fileres,k,k+1); |
/*--------- results files --------------*/
|
for (j=1; j< nlstate ; j ++) fprintf(ficgp," \%%lf (\%%lf)"); |
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);
|
fprintf(ficgp,"\" t\"\" w l 0,"); |
|
fprintf(ficgp,"\"t%s\" u 1:($%d+2*$%d) \"\%%lf \%%lf (\%%lf) \%%lf (\%%lf)",fileres,k,k+1); |
jk=1;
|
for (j=1; j< nlstate ; j ++) fprintf(ficgp," \%%lf (\%%lf)"); |
fprintf(ficres,"# Parameters\n");
|
if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l 0"); |
printf("# Parameters\n");
|
else fprintf(ficgp,"\" t\"\" w l 0,"); |
for(i=1,jk=1; i <=nlstate; i++){
|
} |
for(k=1; k <=(nlstate+ndeath); k++){
|
fprintf(ficgp,"\nset out \"e%s.gif\" \nreplot\n\n",strtok(optionfile, ".")); |
if (k != i)
|
|
{
|
/*3eme*/ |
printf("%d%d ",i,k);
|
for (cpt=1; cpt<= nlstate ; cpt ++) { |
fprintf(ficres,"%1d%1d ",i,k);
|
k=2+nlstate*(cpt-1); |
for(j=1; j <=ncovmodel; j++){
|
fprintf(ficgp,"set ter gif small size 400,300\nplot [%.f:%.f] \"e%s\" u 1:%d t \"e%d1\" w l",agemin,fage,fileres,k,cpt); |
printf("%f ",p[jk]);
|
for (i=1; i< nlstate ; i ++) { |
fprintf(ficres,"%f ",p[jk]);
|
fprintf(ficgp,",\"e%s\" u 1:%d t \"e%d%d\" w l",fileres,k+1,cpt,i+1); |
jk++;
|
} |
}
|
fprintf(ficgp,"\nset out \"ex%s%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt); |
printf("\n");
|
} |
fprintf(ficres,"\n");
|
|
}
|
/* CV preval stat */ |
}
|
for (cpt=1; cpt<nlstate ; cpt ++) { |
}
|
k=3; |
|
fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter gif small size 400,300\nplot [%.f:%.f] \"pij%s\" u 2:($%d/($%d",agemin,agemax,fileres,k+cpt,k); |
/* Computing hessian and covariance matrix */
|
for (i=1; i< nlstate ; i ++) |
ftolhess=ftol; /* Usually correct */
|
fprintf(ficgp,"+$%d",k+i); |
hesscov(matcov, p, npar, delti, ftolhess, func);
|
fprintf(ficgp,")) t\"prev(%d,%d)\" w l",cpt,cpt+1); |
fprintf(ficres,"# Scales\n");
|
|
printf("# Scales\n");
|
l=3+(nlstate+ndeath)*cpt; |
for(i=1,jk=1; i <=nlstate; i++){
|
fprintf(ficgp,",\"pij%s\" u 2:($%d/($%d",fileres,l+cpt,l); |
for(j=1; j <=nlstate+ndeath; j++){
|
|
if (j!=i) {
|
for (i=1; i< nlstate ; i ++) { |
fprintf(ficres,"%1d%1d",i,j);
|
l=3+(nlstate+ndeath)*cpt; |
printf("%1d%1d",i,j);
|
fprintf(ficgp,"+$%d",l+i); |
for(k=1; k<=ncovmodel;k++){
|
} |
printf(" %.5e",delti[jk]);
|
fprintf(ficgp,")) t\"prev(%d,%d)\" w l\n",cpt+1,cpt+1); |
fprintf(ficres," %.5e",delti[jk]);
|
|
jk++;
|
|
}
|
fprintf(ficgp,"set out \"p%s%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt); |
printf("\n");
|
} |
fprintf(ficres,"\n");
|
|
}
|
|
}
|
fclose(ficgp); |
}
|
|
|
chdir(path); |
k=1;
|
free_matrix(agev,1,maxwav,1,imx); |
fprintf(ficres,"# Covariance\n");
|
free_ivector(wav,1,imx); |
printf("# Covariance\n");
|
free_imatrix(dh,1,lastpass-firstpass+1,1,imx); |
for(i=1;i<=npar;i++){
|
free_imatrix(mw,1,lastpass-firstpass+1,1,imx); |
/* if (k>nlstate) k=1;
|
|
i1=(i-1)/(ncovmodel*nlstate)+1;
|
free_imatrix(s,1,maxwav+1,1,n); |
fprintf(ficres,"%s%d%d",alph[k],i1,tab[i]);
|
|
printf("%s%d%d",alph[k],i1,tab[i]);*/
|
|
fprintf(ficres,"%3d",i);
|
free_ivector(num,1,n); |
printf("%3d",i);
|
free_vector(agedc,1,n); |
for(j=1; j<=i;j++){
|
free_vector(weight,1,n); |
fprintf(ficres," %.5e",matcov[i][j]);
|
free_matrix(covar,1,NCOVMAX,1,n); |
printf(" %.5e",matcov[i][j]);
|
fclose(ficparo); |
}
|
fclose(ficres); |
fprintf(ficres,"\n");
|
} |
printf("\n");
|
|
k++;
|
/*________fin mle=1_________*/ |
}
|
|
|
|
while((c=getc(ficpar))=='#' && c!= EOF){
|
|
ungetc(c,ficpar);
|
/* No more information from the sample is required now */ |
fgets(line, MAXLINE, ficpar);
|
/* Reads comments: lines beginning with '#' */ |
puts(line);
|
while((c=getc(ficpar))=='#' && c!= EOF){ |
fputs(line,ficparo);
|
ungetc(c,ficpar); |
}
|
fgets(line, MAXLINE, ficpar); |
ungetc(c,ficpar);
|
puts(line); |
|
fputs(line,ficparo); |
fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf\n",&agemin,&agemax, &bage, &fage);
|
} |
|
ungetc(c,ficpar); |
if (fage <= 2) {
|
|
bage = agemin;
|
fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf\n",&agemin,&agemax, &bage, &fage); |
fage = agemax;
|
printf("agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax, bage, fage); |
}
|
fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax,bage,fage); |
|
|
fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
|
/*--------------- Prevalence limit --------------*/ |
fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax,bage,fage);
|
|
/*------------ gnuplot -------------*/
|
strcpy(filerespl,"pl"); |
chdir(pathcd);
|
strcat(filerespl,fileres); |
if((ficgp=fopen("graph.plt","w"))==NULL) {
|
if((ficrespl=fopen(filerespl,"w"))==NULL) { |
printf("Problem with file graph.gp");goto end;
|
printf("Problem with Prev limit resultfile: %s\n", filerespl);goto end; |
}
|
} |
#ifdef windows
|
printf("Computing prevalence limit: result on file '%s' \n", filerespl); |
fprintf(ficgp,"cd \"%s\" \n",pathc);
|
fprintf(ficrespl,"#Prevalence limit\n"); |
#endif
|
fprintf(ficrespl,"#Age "); |
m=pow(2,cptcovn);
|
for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i); |
|
fprintf(ficrespl,"\n"); |
/* 1eme*/
|
|
for (cpt=1; cpt<= nlstate ; cpt ++) {
|
prlim=matrix(1,nlstate,1,nlstate); |
for (k1=1; k1<= m ; k1 ++) {
|
pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */ |
|
oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */ |
#ifdef windows
|
newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */ |
fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter gif small size 400,300\nplot [%.f:%.f] \"vpl%s\" every :::%d::%d u 1:2 \"\%%lf",agemin,fage,fileres,k1-1,k1-1);
|
savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */ |
#endif
|
oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */ |
#ifdef unix
|
|
fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nplot [%.f:%.f] \"vpl%s\" u 1:2 \"\%%lf",agemin,fage,fileres);
|
agebase=agemin; |
#endif
|
agelim=agemax; |
|
ftolpl=1.e-10; |
for (i=1; i<= nlstate ; i ++) {
|
for (age=agebase; age<=agelim; age++){ |
if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
|
prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl); |
else fprintf(ficgp," \%%*lf (\%%*lf)");
|
fprintf(ficrespl,"%.0f",age ); |
}
|
for(i=1; i<=nlstate;i++) |
fprintf(ficgp,"\" t\"Stationary prevalence\" w l 0,\"vpl%s\" every :::%d::%d u 1:($2+2*$3) \"\%%lf",fileres,k1-1,k1-1);
|
fprintf(ficrespl," %.5f", prlim[i][i]); |
for (i=1; i<= nlstate ; i ++) {
|
fprintf(ficrespl,"\n"); |
if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
|
} |
else fprintf(ficgp," \%%*lf (\%%*lf)");
|
fclose(ficrespl); |
}
|
|
fprintf(ficgp,"\" t\"95\%% CI\" w l 1,\"vpl%s\" every :::%d::%d u 1:($2-2*$3) \"\%%lf",fileres,k1-1,k1-1);
|
/*------------- h Pij x at various ages ------------*/ |
for (i=1; i<= nlstate ; i ++) {
|
|
if (i==cpt) fprintf(ficgp," \%%lf (\%%lf)");
|
strcpy(filerespij,"pij"); strcat(filerespij,fileres); |
else fprintf(ficgp," \%%*lf (\%%*lf)");
|
if((ficrespij=fopen(filerespij,"w"))==NULL) { |
}
|
printf("Problem with Pij resultfile: %s\n", filerespij);goto end; |
fprintf(ficgp,"\" t\"\" w l 1,\"p%s\" every :::%d::%d u 1:($%d) t\"Observed prevalence \" w l 2",fileres,k1-1,k1-1,2+4*(cpt-1));
|
} |
#ifdef unix
|
printf("Computing pij: result on file '%s' \n", filerespij); |
fprintf(ficgp,"\nset ter gif small size 400,300");
|
stepsize=(int) (stepm+YEARM-1)/YEARM; |
#endif
|
if (stepm<=24) stepsize=2; |
fprintf(ficgp,"\nset out \"v%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
|
|
}
|
agelim=AGESUP; |
}
|
hstepm=stepsize*YEARM; /* Every year of age */ |
/*2 eme*/
|
hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */ |
|
for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */ |
for (k1=1; k1<= m ; k1 ++) {
|
nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */ |
fprintf(ficgp,"set ylabel \"Years\" \nset ter gif small size 400,300\nplot [%.f:%.f] ",agemin,fage);
|
nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */ |
|
p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); |
for (i=1; i<= nlstate+1 ; i ++) {
|
oldm=oldms;savm=savms; |
k=2*i;
|
hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm); |
fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:2 \"\%%lf",fileres,k1-1,k1-1);
|
fprintf(ficrespij,"# Age"); |
for (j=1; j<= nlstate+1 ; j ++) {
|
for(i=1; i<=nlstate;i++) |
if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
|
for(j=1; j<=nlstate+ndeath;j++) |
else fprintf(ficgp," \%%*lf (\%%*lf)");
|
fprintf(ficrespij," %1d-%1d",i,j); |
}
|
fprintf(ficrespij,"\n"); |
if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l ,");
|
for (h=0; h<=nhstepm; h++){ |
else fprintf(ficgp,"\" t\"LE in state (%d)\" w l ,",i-1);
|
fprintf(ficrespij,"%.0f %.0f",agedeb, agedeb+ h*hstepm/YEARM*stepm ); |
fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2-$3*2) \"\%%lf",fileres,k1-1,k1-1);
|
for(i=1; i<=nlstate;i++) |
for (j=1; j<= nlstate+1 ; j ++) {
|
for(j=1; j<=nlstate+ndeath;j++) |
if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
|
fprintf(ficrespij," %.5f", p3mat[i][j][h]); |
else fprintf(ficgp," \%%*lf (\%%*lf)");
|
fprintf(ficrespij,"\n"); |
}
|
} |
fprintf(ficgp,"\" t\"\" w l 0,");
|
free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); |
fprintf(ficgp,"\"t%s\" every :::%d::%d u 1:($2+$3*2) \"\%%lf",fileres,k1-1,k1-1);
|
fprintf(ficrespij,"\n"); |
for (j=1; j<= nlstate+1 ; j ++) {
|
} |
if (j==i) fprintf(ficgp," \%%lf (\%%lf)");
|
fclose(ficrespij); |
else fprintf(ficgp," \%%*lf (\%%*lf)");
|
|
}
|
/*---------- Health expectancies and variances ------------*/ |
if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l 0");
|
|
else fprintf(ficgp,"\" t\"\" w l 0,");
|
eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage); |
}
|
oldm=oldms;savm=savms; |
fprintf(ficgp,"\nset out \"e%s%d.gif\" \nreplot\n\n",strtok(optionfile, "."),k1);
|
evsij(fileres, eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm); |
}
|
|
|
vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage); |
/*3eme*/
|
oldm=oldms;savm=savms; |
|
varevsij(fileres, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl); |
for (k1=1; k1<= m ; k1 ++) {
|
|
for (cpt=1; cpt<= nlstate ; cpt ++) {
|
strcpy(filerest,"t"); |
k=2+nlstate*(cpt-1);
|
strcat(filerest,fileres); |
fprintf(ficgp,"set ter gif small size 400,300\nplot [%.f:%.f] \"e%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",agemin,fage,fileres,k1-1,k1-1,k,cpt);
|
if((ficrest=fopen(filerest,"w"))==NULL) { |
for (i=1; i< nlstate ; i ++) {
|
printf("Problem with total LE resultfile: %s\n", filerest);goto end; |
fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",fileres,k1-1,k1-1,k+i,cpt,i+1);
|
} |
}
|
printf("Computing Total LEs with variances: file '%s' \n", filerest); |
fprintf(ficgp,"\nset out \"exp%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
|
fprintf(ficrest,"#Total LEs with variances: e.. (std) "); |
}
|
for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i); |
}
|
fprintf(ficrest,"\n"); |
|
|
/* CV preval stat */
|
hf=1; |
for (k1=1; k1<= m ; k1 ++) {
|
if (stepm >= YEARM) hf=stepm/YEARM; |
for (cpt=1; cpt<nlstate ; cpt ++) {
|
epj=vector(1,nlstate+1); |
k=3;
|
for(age=bage; age <=fage ;age++){ |
fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter gif small size 400,300\nplot [%.f:%.f] \"pij%s\" u ($1==%d ? ($3):1/0):($%d/($%d",agemin,agemax,fileres,k1,k+cpt+1,k+1);
|
prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl); |
for (i=1; i< nlstate ; i ++)
|
fprintf(ficrest," %.0f",age); |
fprintf(ficgp,"+$%d",k+i+1);
|
for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){ |
fprintf(ficgp,")) t\"prev(%d,%d)\" w l",cpt,cpt+1);
|
for(i=1, epj[j]=0.;i <=nlstate;i++) { |
|
epj[j] += prlim[i][i]*hf*eij[i][j][(int)age]; |
l=3+(nlstate+ndeath)*cpt;
|
} |
fprintf(ficgp,",\"pij%s\" u ($1==%d ? ($3):1/0):($%d/($%d",fileres,k1,l+cpt+1,l+1);
|
epj[nlstate+1] +=epj[j]; |
for (i=1; i< nlstate ; i ++) {
|
} |
l=3+(nlstate+ndeath)*cpt;
|
for(i=1, vepp=0.;i <=nlstate;i++) |
fprintf(ficgp,"+$%d",l+i+1);
|
for(j=1;j <=nlstate;j++) |
}
|
vepp += vareij[i][j][(int)age]; |
fprintf(ficgp,")) t\"prev(%d,%d)\" w l\n",cpt+1,cpt+1);
|
fprintf(ficrest," %.2f (%.2f)", epj[nlstate+1],hf*sqrt(vepp)); |
fprintf(ficgp,"set out \"p%s%d%d.gif\" \nreplot\n\n",strtok(optionfile, "."),cpt,k1);
|
for(j=1;j <=nlstate;j++){ |
}
|
fprintf(ficrest," %.2f (%.2f)", epj[j],hf*sqrt(vareij[j][j][(int)age])); |
}
|
} |
|
fprintf(ficrest,"\n"); |
/* proba elementaires */
|
} |
for(i=1,jk=1; i <=nlstate; i++){
|
fclose(ficrest); |
for(k=1; k <=(nlstate+ndeath); k++){
|
fclose(ficpar); |
if (k != i) {
|
free_vector(epj,1,nlstate+1); |
/* fprintf(ficgp,"%1d%1d ",i,k);*/
|
|
for(j=1; j <=ncovmodel; j++){
|
/*------- Variance limit prevalence------*/ |
fprintf(ficgp,"%s%1d%1d=%f ",alph[j],i,k,p[jk]);
|
|
jk++;
|
varpl=matrix(1,nlstate,(int) bage, (int) fage); |
fprintf(ficgp,"\n");
|
oldm=oldms;savm=savms; |
}
|
varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl); |
}
|
|
}
|
|
}
|
free_matrix(varpl,1,nlstate,(int) bage, (int)fage); |
for(jk=1; jk <=m; jk++) {
|
|
fprintf(ficgp,"\nset ter gif small size 400,300\nset log y\nplot [%.f:%.f] ",agemin,agemax);
|
free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage); |
for(i=1; i <=nlstate; i++) {
|
free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage); |
for(k=1; k <=(nlstate+ndeath); k++){
|
|
if (k != i) {
|
|
fprintf(ficgp," exp(a%d%d+b%d%d*x",i,k,i,k);
|
free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath); |
for(j=3; j <=ncovmodel; j++)
|
free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath); |
fprintf(ficgp,"+%s%d%d*%d",alph[j],i,k,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
|
free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath); |
fprintf(ficgp,")/(1");
|
free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath); |
for(k1=1; k1 <=(nlstate+ndeath); k1++)
|
|
if (k1 != i) {
|
free_matrix(matcov,1,npar,1,npar); |
fprintf(ficgp,"+exp(a%d%d+b%d%d*x",i,k1,i,k1);
|
free_vector(delti,1,npar); |
for(j=3; j <=ncovmodel; j++)
|
|
fprintf(ficgp,"+%s%d%d*%d",alph[j],i,k,nbcode[Tvar[j-2]][codtab[jk][j-2]]);
|
free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncov); |
fprintf(ficgp,")");
|
|
}
|
printf("End of Imach\n"); |
fprintf(ficgp,") t \"p%d%d\" ", i,k);
|
/* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */ |
if ((i+k)!= (nlstate*2+ndeath)) fprintf(ficgp,",");
|
|
}
|
/* 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);*/ |
}
|
/*------ End -----------*/ |
fprintf(ficgp,"\nset out \"pe%s%d.gif\" \nreplot\n\n",strtok(optionfile, "."),jk);
|
|
}
|
end: |
fclose(ficgp);
|
#ifdef windows |
|
chdir(pathcd); |
chdir(path);
|
#endif |
free_matrix(agev,1,maxwav,1,imx);
|
system("gnuplot graph.gp"); |
free_ivector(wav,1,imx);
|
|
free_imatrix(dh,1,lastpass-firstpass+1,1,imx);
|
#ifdef windows |
free_imatrix(mw,1,lastpass-firstpass+1,1,imx);
|
while (z[0] != 'q') { |
|
chdir(pathcd); |
free_imatrix(s,1,maxwav+1,1,n);
|
printf("\nType e to edit output files, c to start again, and q for exiting: "); |
|
scanf("%s",z); |
|
if (z[0] == 'c') system("./imach"); |
free_ivector(num,1,n);
|
else if (z[0] == 'e') { |
free_vector(agedc,1,n);
|
chdir(path); |
free_vector(weight,1,n);
|
system("index.htm"); |
/*free_matrix(covar,1,NCOVMAX,1,n);*/
|
} |
fclose(ficparo);
|
else if (z[0] == 'q') exit(0); |
fclose(ficres);
|
} |
}
|
#endif |
|
} |
/*________fin mle=1_________*/
|
|
|
|
|
|
|
|
/* No more information from the sample is required now */
|
|
/* Reads comments: lines beginning with '#' */
|
|
while((c=getc(ficpar))=='#' && c!= EOF){
|
|
ungetc(c,ficpar);
|
|
fgets(line, MAXLINE, ficpar);
|
|
puts(line);
|
|
fputs(line,ficparo);
|
|
}
|
|
ungetc(c,ficpar);
|
|
|
|
fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf\n",&agemin,&agemax, &bage, &fage);
|
|
printf("agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax, bage, fage);
|
|
fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f\n",agemin,agemax,bage,fage);
|
|
/*--------- index.htm --------*/
|
|
|
|
if((fichtm=fopen("index.htm","w"))==NULL) {
|
|
printf("Problem with index.htm \n");goto end;
|
|
}
|
|
|
|
fprintf(fichtm,"<body><ul> Imach, Version 0.63<hr> <li>Outputs files<br><br>\n
|
|
- Observed prevalence in each state: <a href=\"p%s\">p%s</a> <br>\n
|
|
- Estimated parameters and the covariance matrix: <a href=\"%s\">%s</a> <br>
|
|
- Stationary prevalence in each state: <a href=\"pl%s\">pl%s</a> <br>
|
|
- Transition probabilities: <a href=\"pij%s\">pij%s</a><br>
|
|
- Copy of the parameter file: <a href=\"o%s\">o%s</a><br>
|
|
- Life expectancies by age and initial health status: <a href=\"e%s\">e%s</a> <br>
|
|
- Variances of life expectancies by age and initial health status: <a href=\"v%s\">v%s</a><br>
|
|
- Health expectancies with their variances: <a href=\"t%s\">t%s</a> <br>
|
|
- Standard deviation of stationary prevalences: <a href=\"vpl%s\">vpl%s</a> <br><br>",fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres,fileres);
|
|
|
|
fprintf(fichtm," <li>Graphs</li>\n<p>");
|
|
|
|
m=cptcovn;
|
|
if (cptcovn < 1) {m=1;ncodemax[1]=1;}
|
|
|
|
j1=0;
|
|
for(k1=1; k1<=m;k1++){
|
|
for(i1=1; i1<=ncodemax[k1];i1++){
|
|
j1++;
|
|
if (cptcovn > 0) {
|
|
fprintf(fichtm,"<hr>************ Results for covariates");
|
|
for (cpt=1; cpt<=cptcovn;cpt++)
|
|
fprintf(fichtm," V%d=%d ",Tvar[cpt],nbcode[Tvar[cpt]][codtab[j1][cpt]]);
|
|
fprintf(fichtm," ************\n<hr>");
|
|
}
|
|
fprintf(fichtm,"<br>- Probabilities: pe%s%d.gif<br>
|
|
<img src=\"pe%s%d.gif\">",strtok(optionfile, "."),j1,strtok(optionfile, "."),j1);
|
|
for(cpt=1; cpt<nlstate;cpt++){
|
|
fprintf(fichtm,"<br>- Prevalence of disability : p%s%d%d.gif<br>
|
|
<img src=\"p%s%d%d.gif\">",strtok(optionfile, "."),cpt,j1,strtok(optionfile, "."),cpt,j1);
|
|
}
|
|
for(cpt=1; cpt<=nlstate;cpt++) {
|
|
fprintf(fichtm,"<br>- Observed and stationary prevalence (with confident
|
|
interval) in state (%d): v%s%d%d.gif <br>
|
|
<img src=\"v%s%d%d.gif\">",cpt,strtok(optionfile, "."),cpt,j1,strtok(optionfile, "."),cpt,j1);
|
|
}
|
|
for(cpt=1; cpt<=nlstate;cpt++) {
|
|
fprintf(fichtm,"\n<br>- Health life expectancies by age and initial health state (%d): exp%s%d%d.gif <br>
|
|
<img src=\"exp%s%d%d.gif\">",cpt,strtok(optionfile, "."),cpt,j1,strtok(optionfile, "."),cpt,j1);
|
|
}
|
|
fprintf(fichtm,"\n<br>- Total life expectancy by age and
|
|
health expectancies in states (1) and (2): e%s%d.gif<br>
|
|
<img src=\"e%s%d.gif\">",strtok(optionfile, "."),j1,strtok(optionfile, "."),j1);
|
|
fprintf(fichtm,"\n</body>");
|
|
}
|
|
}
|
|
fclose(fichtm);
|
|
|
|
/*--------------- Prevalence limit --------------*/
|
|
|
|
strcpy(filerespl,"pl");
|
|
strcat(filerespl,fileres);
|
|
if((ficrespl=fopen(filerespl,"w"))==NULL) {
|
|
printf("Problem with Prev limit resultfile: %s\n", filerespl);goto end;
|
|
}
|
|
printf("Computing prevalence limit: result on file '%s' \n", filerespl);
|
|
fprintf(ficrespl,"#Prevalence limit\n");
|
|
fprintf(ficrespl,"#Age ");
|
|
for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
|
|
fprintf(ficrespl,"\n");
|
|
|
|
prlim=matrix(1,nlstate,1,nlstate);
|
|
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 */
|
|
oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
|
|
k=0;
|
|
agebase=agemin;
|
|
agelim=agemax;
|
|
ftolpl=1.e-10;
|
|
i1=cptcovn;
|
|
if (cptcovn < 1){i1=1;}
|
|
|
|
for(cptcov=1;cptcov<=i1;cptcov++){
|
|
for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
|
|
k=k+1;
|
|
/*printf("cptcov=%d cptcod=%d codtab=%d nbcode=%d\n",cptcov, cptcod,Tcode[cptcode],codtab[cptcod][cptcov]);*/
|
|
fprintf(ficrespl,"\n#****** ");
|
|
for(j=1;j<=cptcovn;j++)
|
|
fprintf(ficrespl,"V%d=%d ",Tvar[j],nbcode[Tvar[j]][codtab[k][j]]);
|
|
fprintf(ficrespl,"******\n");
|
|
|
|
for (age=agebase; age<=agelim; age++){
|
|
prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
|
|
fprintf(ficrespl,"%.0f",age );
|
|
for(i=1; i<=nlstate;i++)
|
|
fprintf(ficrespl," %.5f", prlim[i][i]);
|
|
fprintf(ficrespl,"\n");
|
|
}
|
|
}
|
|
}
|
|
fclose(ficrespl);
|
|
/*------------- h Pij x at various ages ------------*/
|
|
|
|
strcpy(filerespij,"pij"); strcat(filerespij,fileres);
|
|
if((ficrespij=fopen(filerespij,"w"))==NULL) {
|
|
printf("Problem with Pij resultfile: %s\n", filerespij);goto end;
|
|
}
|
|
printf("Computing pij: result on file '%s' \n", filerespij);
|
|
|
|
stepsize=(int) (stepm+YEARM-1)/YEARM;
|
|
if (stepm<=24) stepsize=2;
|
|
|
|
agelim=AGESUP;
|
|
hstepm=stepsize*YEARM; /* Every year of age */
|
|
hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
|
|
|
|
k=0;
|
|
for(cptcov=1;cptcov<=i1;cptcov++){
|
|
for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
|
|
k=k+1;
|
|
fprintf(ficrespij,"\n#****** ");
|
|
for(j=1;j<=cptcovn;j++)
|
|
fprintf(ficrespij,"V%d=%d ",Tvar[j],nbcode[Tvar[j]][codtab[k][j]]);
|
|
fprintf(ficrespij,"******\n");
|
|
|
|
for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
|
|
nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
|
|
nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
|
|
p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
|
|
oldm=oldms;savm=savms;
|
|
hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
|
|
fprintf(ficrespij,"# Age");
|
|
for(i=1; i<=nlstate;i++)
|
|
for(j=1; j<=nlstate+ndeath;j++)
|
|
fprintf(ficrespij," %1d-%1d",i,j);
|
|
fprintf(ficrespij,"\n");
|
|
for (h=0; h<=nhstepm; h++){
|
|
fprintf(ficrespij,"%d %.0f %.0f",k,agedeb, agedeb+ h*hstepm/YEARM*stepm );
|
|
for(i=1; i<=nlstate;i++)
|
|
for(j=1; j<=nlstate+ndeath;j++)
|
|
fprintf(ficrespij," %.5f", p3mat[i][j][h]);
|
|
fprintf(ficrespij,"\n");
|
|
}
|
|
free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
|
|
fprintf(ficrespij,"\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
fclose(ficrespij);
|
|
|
|
/*---------- Health expectancies and variances ------------*/
|
|
|
|
strcpy(filerest,"t");
|
|
strcat(filerest,fileres);
|
|
if((ficrest=fopen(filerest,"w"))==NULL) {
|
|
printf("Problem with total LE resultfile: %s\n", filerest);goto end;
|
|
}
|
|
printf("Computing Total LEs with variances: file '%s' \n", filerest);
|
|
|
|
|
|
strcpy(filerese,"e");
|
|
strcat(filerese,fileres);
|
|
if((ficreseij=fopen(filerese,"w"))==NULL) {
|
|
printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
|
|
}
|
|
printf("Computing Health Expectancies: result on file '%s' \n", filerese);
|
|
|
|
strcpy(fileresv,"v");
|
|
strcat(fileresv,fileres);
|
|
if((ficresvij=fopen(fileresv,"w"))==NULL) {
|
|
printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
|
|
}
|
|
printf("Computing Variance-covariance of DFLEs: file '%s' \n", fileresv);
|
|
|
|
k=0;
|
|
for(cptcov=1;cptcov<=i1;cptcov++){
|
|
for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
|
|
k=k+1;
|
|
fprintf(ficrest,"\n#****** ");
|
|
for(j=1;j<=cptcovn;j++)
|
|
fprintf(ficrest,"V%d=%d ",Tvar[j],nbcode[Tvar[j]][codtab[k][j]]);
|
|
fprintf(ficrest,"******\n");
|
|
|
|
fprintf(ficreseij,"\n#****** ");
|
|
for(j=1;j<=cptcovn;j++)
|
|
fprintf(ficreseij,"V%d=%d ",j,nbcode[j][codtab[k][j]]);
|
|
fprintf(ficreseij,"******\n");
|
|
|
|
fprintf(ficresvij,"\n#****** ");
|
|
for(j=1;j<=cptcovn;j++)
|
|
fprintf(ficresvij,"V%d=%d ",j,nbcode[j][codtab[k][j]]);
|
|
fprintf(ficresvij,"******\n");
|
|
|
|
eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
|
|
oldm=oldms;savm=savms;
|
|
evsij(fileres, eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k);
|
|
vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
|
|
oldm=oldms;savm=savms;
|
|
varevsij(fileres, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k);
|
|
|
|
fprintf(ficrest,"#Total LEs with variances: e.. (std) ");
|
|
for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
|
|
fprintf(ficrest,"\n");
|
|
|
|
hf=1;
|
|
if (stepm >= YEARM) hf=stepm/YEARM;
|
|
epj=vector(1,nlstate+1);
|
|
for(age=bage; age <=fage ;age++){
|
|
prevalim(prlim, nlstate, p, age, oldm, savm,ftolpl,k);
|
|
fprintf(ficrest," %.0f",age);
|
|
for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
|
|
for(i=1, epj[j]=0.;i <=nlstate;i++) {
|
|
epj[j] += prlim[i][i]*hf*eij[i][j][(int)age];
|
|
}
|
|
epj[nlstate+1] +=epj[j];
|
|
}
|
|
for(i=1, vepp=0.;i <=nlstate;i++)
|
|
for(j=1;j <=nlstate;j++)
|
|
vepp += vareij[i][j][(int)age];
|
|
fprintf(ficrest," %.2f (%.2f)", epj[nlstate+1],hf*sqrt(vepp));
|
|
for(j=1;j <=nlstate;j++){
|
|
fprintf(ficrest," %.2f (%.2f)", epj[j],hf*sqrt(vareij[j][j][(int)age]));
|
|
}
|
|
fprintf(ficrest,"\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
fclose(ficreseij);
|
|
fclose(ficresvij);
|
|
fclose(ficrest);
|
|
fclose(ficpar);
|
|
free_vector(epj,1,nlstate+1);
|
|
/* scanf("%d ",i); */
|
|
|
|
/*------- Variance limit prevalence------*/
|
|
|
|
strcpy(fileresvpl,"vpl");
|
|
strcat(fileresvpl,fileres);
|
|
if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
|
|
printf("Problem with variance prev lim resultfile: %s\n", fileresvpl);
|
|
exit(0);
|
|
}
|
|
printf("Computing Variance-covariance of Prevalence limit: file '%s' \n", fileresvpl);
|
|
|
|
k=0;
|
|
for(cptcov=1;cptcov<=i1;cptcov++){
|
|
for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
|
|
k=k+1;
|
|
fprintf(ficresvpl,"\n#****** ");
|
|
for(j=1;j<=cptcovn;j++)
|
|
fprintf(ficresvpl,"V%d=%d ",Tvar[j],nbcode[Tvar[j]][codtab[k][j]]);
|
|
fprintf(ficresvpl,"******\n");
|
|
|
|
varpl=matrix(1,nlstate,(int) bage, (int) fage);
|
|
oldm=oldms;savm=savms;
|
|
varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl,k);
|
|
}
|
|
}
|
|
|
|
fclose(ficresvpl);
|
|
|
|
/*---------- End : free ----------------*/
|
|
free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
|
|
|
|
free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
|
|
free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
|
|
|
|
|
|
free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
|
|
free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
|
|
free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
|
|
free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
|
|
|
|
free_matrix(matcov,1,npar,1,npar);
|
|
free_vector(delti,1,npar);
|
|
|
|
free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
|
|
|
|
printf("End of Imach\n");
|
|
/* 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);*/
|
|
/*------ End -----------*/
|
|
|
|
end:
|
|
#ifdef windows
|
|
chdir(pathcd);
|
|
#endif
|
|
system("wgnuplot ../gp37mgw/graph.plt");
|
|
|
|
#ifdef windows
|
|
while (z[0] != 'q') {
|
|
chdir(pathcd);
|
|
printf("\nType e to edit output files, c to start again, and q for exiting: ");
|
|
scanf("%s",z);
|
|
if (z[0] == 'c') system("./imach");
|
|
else if (z[0] == 'e') {
|
|
chdir(path);
|
|
system("index.htm");
|
|
}
|
|
else if (z[0] == 'q') exit(0);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
|